Sakthivel Thandabani
Sakthivel Thandabani

Reputation: 83

Socket programming issue ipv6+udp

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--;
}

Upvotes: 1

Views: 969

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123551

 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).

Upvotes: 2

Related Questions