我正在尝试通过 C Linux 中的 RAW 套接字发送自定义 IPv6 标头。
我已经使用 IP_HDRINCL 套接字选项在 IPv4 中取得了成功,但是,IPv6 没有等效的选项。
我找到了解决方法here http://lists.openwall.net/netdev/2009/01/15/168建议使用socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)
与启用 IP_HDRINCL 套接字选项具有相同的效果。
套接字已成功创建,并且在使用之前没有收到任何错误sendto
与我修改后的标题一起使用。
我这样设置套接字:
static int socketFd = 0;
static struct sockaddr_in6 remote;
int main()
{
socketFd = socket (PF_INET6, SOCK_RAW, IPPROTO_RAW);
if (socketFd < 0)
{
printf ("An error ocurred while creating the socket.\n");
exit (2);
}
remote.sin6_family = AF_INET6;
remote.sin6_port = htons (25000);
if (inet_pton (AF_INET6, "fd00:c0de::70d6:4ab9:115d:8cda", &(remote.sin6_addr)) != 1)
{
close (socketFd);
printf ("Unable to parse IPv6 address.\n");
exit (2);
}
/*More code */
...
return 0;
}
然后,我有这个回调函数,它应该发送我的自定义 IPv6 数据包,但是sendto
失败返回 EINVAL。
static void sendPacket ()
{
char buffer[BUFSIZ];
const size_t len = sizeof(struct ip6_hdr) + sizeof(struct UDP_hdr);
struct ip6_hdr *ip6 = (struct ip6_hdr*) (buffer);
struct UDP_hdr *udp = (struct UDP_hdr *) (buffer + sizeof(struct ip6_hdr));
memset (buffer, 0, BUFSIZ);
ip6->ip6_ctlun.ip6_un2_vfc = 0x60;
ip6->ip6_dst = remote.sin6_addr;
ip6->ip6_flow = 60;
ip6->ip6_hops = 64;
ip6->ip6_nxt = 17;
ip6->ip6_plen = sizeof(struct UDP_hdr);
if (inet_pton (AF_INET6, "fd00:c0de::62a4:4cff:1234:5678", &(ip6->ip6_src)) != 1)
{
printf ("Error while parsing spoofed IPv6 address.\n");
return;
}
// Fabricate the UDP header. Source port number, redundant
udp->uh_sport = htons (21000);
// Destination port number
udp->uh_dport = remote.sin6_port;
udp->uh_ulen = htons (sizeof(struct UDP_hdr));
if (sendto (socketFd, buffer, len, 0, (struct sockaddr *) &remote, sizeof(remote)) < 0)
{
perror ("sendto");
printf ("Error while sending packet.\n");
}
}
我已经调试了程序,并且 ip6_hdr 结构中的所有值似乎都是正确的,结构 UDP_hdr 中的值也是正确的。我错过了什么吗?