title: Send an UDP packet and receive an ICMP response from router in C
body: <p>I'm trying write a C program that sends an UDP packet to a given IP adress and waits for an ICMP response of a router telling that the time to live expired. It's kept very simple because I just want to understand the mechanism at first. What I need is someone checking my code giving feedback on what's wrong and what's missing. I'm very unexperienced at C-programming but I have to do it as an assignment - giving my best to understand it...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netinet/ip.h&gt; 
  
  // The packet length
  #define PCKT_LEN 8192 
  
  // Source IP, source port, target IP, target port from the command line arguments
  int main(int argc, char *argv[])
  {
      int send, recv, resp;
      int ttl = 1; // Time to live
      char buffer[PCKT_LEN];
  
      // Destination address
      struct sockaddr_in dst;
  
      // ICMP Response
      struct msghdr icmp;
  
      memset(buffer, 0, PCKT_LEN);
  
      // Create a raw socket with UDP protocol
      if ((send = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0)
      {
          printf("Could not process socket() [send].\n");
          return EXIT_FAILURE;
      }
  
      // ICMP Socket
      if ((recv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) &lt; 0)
      {
          printf("Could not process socket() [recv].\n");
          return EXIT_FAILURE;
      }
  
      dst.sin_family      = AF_INET;
      dst.sin_addr.s_addr = inet_addr("74.125.39.106");
      dst.sin_port        = htons(60001);
  
      // Define time to life
      if(setsockopt(send, IPPROTO_IP, IP_TTL, &amp;ttl, sizeof(ttl)) &lt; 0)
      {
          printf("Could not process setsockopt().\n");
          return EXIT_FAILURE;
      }
  
      if(sendto(send, buffer, sizeof(buffer), 0, (struct sockaddr *) &amp;dst, sizeof(dst)) &lt; 0)
      {
          printf("Could not process sendto().\n");
          return EXIT_FAILURE;
      }
  
  
      if((resp = recvmsg(recv, (struct msghdr *) &amp;icmp, IP_RECVERR|MSG_ERRQUEUE)) &lt; 0 )
      {
          printf("Could not process recvmsg().\n");
          return EXIT_FAILURE;
      }
  
      close(send);
      close(recv);
  
      return 0;
  }
  </code></pre>
  
  <p>I keep receiving "Could not process recvmsg()." and I have no clue anymore what else to try. I'd like to receive an ICMP answer and read its senders IP-Address.</p>
  
  <p>Looking forward to helpfull hints.</p>
  
  <p>Best regards!</p>