title: Sockets sendto() returning EINVAL
body: <p>I'm trying to send a UDP packet in C. I have the following <code>sendto()</code>:</p>
  
  <pre><code>char* msg = "Hello";
  
  //ret is the return value of getaddrinfo, the address is AF_INET (IPv4)
  //and the sock_type is SOCK_DGRAM (UDP)
  struct sockaddr_in *ip = (struct sockaddr_in *)ret-&gt;ai_addr;
  
  if ((sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)ip, 
                  sizeof(struct sockaddr *))) != -1) {
      printf("msg sent successfully");
  } else {
      printf("Error sending msg: %s\n", strerror(errno));
  }
  </code></pre>
  
  <p>However, it's returning an error saying there's an invalid argument. Looking at the manpage I can't really tell which one is the invalid argument. Any ideas?</p>
  
  <p>EDIT: Here's all my code</p>
  
  <pre><code>#include &lt;stdio.h&gt;  
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;errno.h&gt;
  
  int main(int argc, const char *argv[])
  {
      /*
       * Help the technically challenged among us who have no idea
       * what on God's green Earth they are doing with this thing.
      */
      if (argc != 2) {
          printf("usage: routetracer &lt;ip address or hostname&gt;\n");
          return -1;
      }
  
      /*
       * hints- parameters for return value of getaddrinfo
       * ret- return value of getaddrinfo
       */
      struct addrinfo hints, *ret;
      int status;
      char ipv4[INET_ADDRSTRLEN];
      int ttl = 0;
      char* msg = "Hello";
      int last_hop = 0;
  
      //define what we want from getaddrinfo
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_family = AF_INET; //IPv4
      hints.ai_socktype = SOCK_DGRAM; //UDP packets
  
      //call getaddrinfo to fill ret, w/ error chk
      if ((status = getaddrinfo(argv[1], NULL, &amp;hints, &amp;ret)) != 0) {
          printf("getaddrinfo: %s\n", gai_strerror(status));
          return -1;
      }
  
      //extract IPv4 address from ret
      struct sockaddr_in* ip = (struct sockaddr_in *)ret-&gt;ai_addr;
  
      //convert address from pure numbers to something easier to read
      inet_ntop(ret-&gt;ai_family, &amp;(ip-&gt;sin_addr), ipv4, INET_ADDRSTRLEN);   
  
      //kindly inform the user of which hostname they are connecting to
      printf("Route for: %s\n", ipv4);
  
      //create a socket
      int sock = socket(ret-&gt;ai_family, ret-&gt;ai_socktype, ret-&gt;ai_protocol);
  
      ttl = 1;
      if ((setsockopt(sock, IPPROTO_IP, IP_TTL, &amp;ttl, sizeof(ttl))) != -1) {
          printf("TTL set successfully\n");
      } else {
          printf("Error setting TTL: %s\n", strerror(errno));
      }
  
      if ((sendto(sock, msg, strlen(msg), 0, ret-&gt;ai_addr,
                      ret-&gt;ai_addrlen)) != -1) {
          printf("msg sent successfully");
      } else {
        printf("Error sending msg: %s\n", strerror(errno));
      }
      return 0;
  }
  </code></pre>
  
  <p>Running the program gives the following output:</p>
  
  <pre><code>$ ./routetracer www.google.com
  Route for: 173.194.46.82
  TTL set successfully
  Error sending msg: Invalid argument
  </code></pre>