【发布时间】:2019-07-04 14:30:05
【问题描述】:
我写了一个代码来生成 ipv6+UDP 数据包,我面临 sendto() 正在抛出“sendto failed : Invalid argument”,但是几乎没有修改,相同的代码适用于 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--;
}
【问题讨论】:
-
你能ping通你要连接的主机吗?
-
可以ping通。