title: how to sniff all ICMP packets using RAW sockets
body: <p>I am learning RAW sockets. In the below code i am trying to print all ICMP packets headers information. Looks like some error in the code. Can anyone please help me where i am wrong.</p>
  
  <pre><code># include &lt;unistd.h&gt;
  # include &lt;sys/socket.h&gt;
  # include &lt;sys/types.h&gt;
  # include &lt;string.h&gt;
  # include &lt;netinet/in.h&gt;
  # include &lt;stdio.h&gt;
  # include&lt;stdlib.h&gt;
  
  main(){
  int sockfd,retval,n;
  socklen_t clilen;
  struct sockaddr_in cliaddr, servaddr;
  char buf[10000]; 
  
  sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  if (sockfd &lt; 0){
      perror("sock:");
      exit(1);
  }
  clilen = sizeof(struct sockaddr_in);    
  while(1){
      printf(" before recvfrom\n");   
      n=recvfrom(sockfd,buf,10000,0,(struct sockaddr *)&amp;cliaddr,&amp;clilen);
      printf(" rec'd %d bytes\n",n);
      buf[n]='\0';
      printf(" msg from client = %s\n",buf);
  }
  }
  
  o/p
  
  before recvfrom
  rec'd 60 bytes
  msg from client = E
  before recvfrom
  rec'd 52 bytes
  </code></pre>