title: sendto() : Invalid argument - raw socket
body: <p>I want to access ip header using raw socket, change TTL value and forward the packet to the client. Adress of server is 192.168.1.5 and adress of client is 192.168.1.3. I have problem with sendto() function, error handling prints out: sendto() failed: Invalid argument. My code is here:</p>
  
  <pre><code>#include&lt;netinet/in.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;netdb.h&gt;
  #include&lt;stdio.h&gt; //For standard things
  #include&lt;stdlib.h&gt;    //malloc
  #include&lt;string.h&gt;    //strlen
  #include&lt;netinet/ip_icmp.h&gt;   //Provides declarations for icmp header
  #include&lt;netinet/udp.h&gt;   //Provides declarations for udp header
  #include&lt;netinet/tcp.h&gt;   //Provides declarations for tcp header
  #include&lt;netinet/ip.h&gt;    //Provides declarations for ip header
  #include&lt;netinet/if_ether.h&gt;  //For ETH_P_ALL
  #include&lt;net/ethernet.h&gt;  //For ether_header
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt;
  #include&lt;sys/ioctl.h&gt;
  #include&lt;sys/time.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;unistd.h&gt;
  
  int main()
  {
  
  int soket;
  
  soket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  
  if (soket&lt;0)
  {
  
      printf("Error creating socket\n");
      return -1;
  }
  else 
      printf("\Socket OK\n\n");
  
  unsigned char *buffer = (unsigned char *) malloc(65536);
  memset (buffer,0,65536);
  struct sockaddr saddr;
  struct sockaddr_in source,dest;
  int saddr_len = sizeof (saddr);
  int dest_len = sizeof (dest);
  
  source.sin_family = AF_INET;
  dest.sin_family = AF_INET;
  // Port numbers
  source.sin_port = htons(53);
  dest.sin_port = htons(53);
  // IP addresses
  source.sin_addr.s_addr = inet_addr ("192.168.1.5");
  dest.sin_addr.s_addr = inet_addr ("192.168.1.3");
  
  setsockopt(soket, SOL_SOCKET , SO_BINDTODEVICE , "eth0" , strlen("eth0")+ 1 
  );
  
  
  if (bind(soket, (struct sockaddr*) &amp;source, sizeof(source)) &lt; 0) {
  perror("bind failed\n");
  close(soket);
  return -1;
  }
  if (recvfrom(soket,buffer,65536,0,(struct sockaddr *)&amp;dest,&amp;dest_len)&lt;0)
  {
      printf ("Paket nije primljen\n");
      return -1;
  }
  
  else
      printf ("Paket je primljen\n\n");
  fflush (stdin);
  
  //EXTRACTING THE IP HEADER
  unsigned short iphdrlen;
  struct iphdr *ip = (struct iphdr*)(buffer + sizeof(struct ethhdr));
  memset(&amp;source, 0, sizeof(source));
  source.sin_addr.s_addr = ip-&gt;saddr;
  memset(&amp;dest, 0, sizeof(dest));
  dest.sin_addr.s_addr = ip-&gt;daddr;
  
  printf ("Changing TTL:\n\n");
  
  ip-&gt;ttl=0;
  
  if(sendto(soket, &amp;ip, sizeof (ip), 0, (struct sockaddr *)&amp;dest, 
  sizeof(dest)) &lt; 0)
  // Verify
  {
  perror("Error sending\n");
  exit(-1);
  }
  else
  {
  printf("Sending OK\n");
  sleep(2);
  } 
  
  close(soket);
  return 0;
  }
  </code></pre>
  
  <p>What is wrong with this code and arguments for sendto()? Can anyone help, please?</p>