我正尝试通过C Linux中的RAW套接字发送自定义IPv6标头。
我已经使用IP_HDRINCL套接字选项在IPv4中取得了成功,但是没有与IPv6等效的function。
我在这里find了一个解决方法,build议使用socket(AF_INET6,SOCK_RAW,IPPROTO_RAW)与启用IP_HDRINCL套接字选项具有相同的效果。
套接字已经成功创build,我没有得到任何错误,直到我用我的修改头使用sendto函数。
我像这样设置套接字:
static int socketFd = 0; static struct sockaddr_in6 remote; int main() { socketFd = socket (PF_INET6,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; }
然后,我有这个callback函数应该发送我的自定义IPv6数据包,但发送失败返回EINVAL。
如何编辑WPF屏幕键盘背后的文本
如何在ListView中插入彩色文本?
是否有可能在CLion中开发Linux内核模块?
Windows Ce设备不会部署“引导程序无法加载”。
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,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,(struct sockaddr *) &remote,sizeof(remote)) < 0) { perror ("sendto"); printf ("Error while sending packet.n"); } }
我已经debugging了程序,并且ip6_hdr结构中的所有值似乎都是正确的,并且也是结构UDP_hdr中的值。 我错过了什么吗?
为什么常量存储在C内存映射的文本段中?
为什么组长不能在Linux中创build会话
通过unix域套接字传递结构
在MS Windows中运行时缺lessGtk +图标
扫描文件时为什么没有从scanf(c Linux vi)中获取正确的char值?
好的,所以我找到了一个解决方案。 对任何人来说,解决这个问题的方法是使用Pcap库(在这个例子后面)。 但是对于那些必须使用AF_PACKETS的人来说,我发现了一个非常完整的例子,在这个链接中使用RAW套接字来完成这个。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。