1

I wrote a code to generate ipv6+UDP packets, i am facing sendto() is throwing "sendto failed : Invalid argument", however with little modification, same code is working for ipv4+udp. can someone help me to identify the problem wr i lost?

    //create a socket
//int s = socket (AF_INET6, SOCK_RAW, IPPROTO_RAW);
int s = socket (AF_INET6, SOCK_RAW, IPPROTO_UDP);
if(s == -1)
{
    perror("System Error: Failed to create raw socket");
    exit(1);
}
    int one=0;
const int *val = &one;

if(setsockopt(s, 41, IP_HDRINCL, val, sizeof(one)) < 0)
{
    perror("setsockopt() error");
    exit(-1);
}

iph  = (struct ip6_hdr*) datagram;
udph = (struct udphdr *) (datagram + sizeof (struct ip6_hdr));
uint32_t tot_pkts = config->num_pkts ;
while(tot_pkts)
{
    //if(send(s, (void*) iph, ntohs(iph->ip6_plen), 0 ) < 0)
    //if (sendto (s, (char*) udph, ntohs(iph->ip6_plen),  0, (struct sockaddr *) (&sin6), sizeof (sin6)) < 0)
    if (sendto (s, (char*) datagram, (ntohs(iph->ip6_plen) + sizeof(struct ip6_hdr)),  0, (struct sockaddr *) (&sin6), sizeof (sin6)) < 0){
        printf("Length ip:%d \n",ntohs(iph->ip6_plen));
        perror("sendto failed ");
    }
    else
    {
        printf("Sending ...Length ip:%d udp:%d\n", ntohs(iph->ip6_plen),ntohs(udph->len));
    }
    tot_pkts--;
}

1 Answers1

2
 if(setsockopt(s, 41, IP_HDRINCL, val, sizeof(one)) < 0)

IP_HDRINCL does not work with IPv6 but only with IPv4. On Linux you can use IPV6_HDRINCL (combined with IPPROTO_IPV6 in setsockopt).

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Hi, thanks for the update. by my badluck still i am facing the same problem. even i commenting put setsockopt() and tried , still problem is exist. – Sakthivel Thandabani Jul 05 '19 at 08:46
  • 1
    @SakthivelThandabani: see https://stackoverflow.com/a/47779888/3081018 and specifically what it linked to. From https://nick-black.com/dankwiki/index.php/Packet_sockets: *" When using an IPv6 raw socket, sin6_port must be set to 0 to avoid an EINVAL ("Invalid Argument") error."*. If it still not works please produce a complete but minimal example instead of showing only the parts you deem relevant. – Steffen Ullrich Jul 05 '19 at 11:21
  • `remote.sin6_port = 0;` This helps. Thanks Steffen – Sakthivel Thandabani Jul 08 '19 at 06:06