【发布时间】:2012-10-23 00:19:29
【问题描述】:
我正在尝试使用 Libnet11 功能:
int libnet_write_raw_ipv6 (libnet_t *l, u_int8_t *packet, u_int32_t size)
在网络层注入 IPv6 数据包。 我创建了 IPv6 数据包并捕获了它 与 Wireshark。 Wireshark 报道: 格式错误的数据包(wireshark 说接下来 IPv6 中的标头值错误且有效负载 我认为尺寸太大了)
我希望,有人可以通过最小的代码示例帮助我, 展示如何手动构建 IPv6 数据包(使用 ICMPv6 扩展头)与 libnet11 (libnet_write_raw_ipv6())。
我假设最小的代码可能如下所示:
packet_len = 40 + 16; // 40B ~ IPv6 packet, 16B ~ ICMPv6 header
u_char *buf = NULL;
struct ip6_hdr *ip6 = NULL;
struct icmp6_hdr *icmp6 = NULL;
l = libnet_init();
if ( (buf = malloc(packet_len)) == NULL ) {
// error
}
// create IPv6 header
ip6 = (struct ip6_hdr *) buf;
ip6->ip6_flow = 0;
ip6->ip6_vfc = 6 << 4;
ip6->ip6_plen = 16; // ICMPv6 packet size
ip6->ip6_nxt = IPPROTO_ICMPV6; // 0x3a
ip6->ip6_hlim = 64;
memcpy(&(ip6->ip6_src), &src_addr, sizeof(struct in6_addr));
memcpy(&(ip6->ip6_dst), &dst_addr, sizeof(struct in6_addr));
// create ICMPv6 header
icmp6 = (struct icmp6_hdr *) (buf + 40); // 40B ~ IPv6 packet size
icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
icmp6->icmp6_code = 0;
icmp6->icmp6_cksum= 0;
icmp6->icmp6_data32[0] = 0;
libnet_do_checksum(l, (u_int8_t *)buf, IPPROTO_ICMPV6, packet_len);
written = libnet_write_raw_ipv6(l, buf, packet_len);
if ( written != packet_len )
perror("Failed to send packet");
libnet_destroy(l);
free(buf);
我试图找到代码示例但没有成功。 提前谢谢你。
马丁
【问题讨论】:
-
wireshark 为下一个标头显示什么?如果字节顺序是 0x3A00 而不是 0x003A,您可能需要调用 htnos() 来翻转字节顺序。
标签: clang ipv6 icmp libnet packet-injection