title: How to receive ICMP request in C with raw sockets
body: <p>This is an assignment, it has to be with raw sockets. I need to program a simple icmp ping.
  I used this as a base <a href="http://www.pdbuchan.com/rawsock/icmp4.c" rel="nofollow noreferrer">http://www.pdbuchan.com/rawsock/icmp4.c</a> . At line 127 is wlan0, Im using eth0. In line 290 I coded this:</p>
  <pre><code>struct sockaddr_in rec;
    
  unsigned char * pkt = (unsigned char *) malloc (IP_MAXPACKET * sizeof (unsigned char));
  
  if (recvfrom (sd, (void*)pkt, IP4_HDRLEN + ICMP_HDRLEN+datalen , 0, NULL, (socklen_t*)sizeof (struct sockaddr)) &lt; 0)  {
    perror (&quot;recvfrom() failed &quot;);
    exit (EXIT_FAILURE);
  }
  struct ip *ip = (struct ip *)pkt;
  struct icmphdr *icmp = (struct icmphdr *)(pkt + IP4_HDRLEN);
  
  printf(&quot;%s %s %d\n&quot;,(char*)inet_ntoa(*(struct in_addr*)&amp;ip-&gt;ip_dst),
          (char*)inet_ntoa(*(struct in_addr*)&amp;ip-&gt;ip_src),
          icmp-&gt;type);
  free (pkt);
  </code></pre>
  <p>The problem is that ip_dst and ip_src are being shown as my machines's IP, and icmp type as 0 and not 8. Wireshark shows both icmp reply and request.
  Probably my recvfrom is wrong, but I heard something about linux own TCP/IP might be handling the packets. If that's true, what is the workaround for that?</p>
  <p>edit: I checked this <a href="https://stackoverflow.com/questions/13082023/raw-socket-listener">raw socket listener</a> but it did not solve my problem.</p>