我写了一个代码来生成ipv6 + UDP数据包,我正面临sendto()
抛出“ sendto failed:Invalid parameter” ,但是经过少量修改,相同的代码仍适用于ipv4 + udp。有人可以帮助我确定我丢失的问题吗?
//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--;
}
答案 0 :(得分:2)
if(setsockopt(s, 41, IP_HDRINCL, val, sizeof(one)) < 0)
IP_HDRINCL
不适用于IPv6,而仅适用于IPv4。在Linux上,您可以使用IPV6_HDRINCL
(与IPPROTO_IPV6
中的setsockopt
组合)。