首页 文章

Libnet11手动构建IPv6数据包

提问于
浏览
1

我正在尝试使用 Libnet11 函数:

int libnet_write_raw_ipv6 (libnet_t *l, u_int8_t *packet, u_int32_t size)

在网络层注入IPv6数据包 . 我创建了IPv6数据包并使用Wireshark捕获它 . Wireshark报道:格式错误的数据包(wireshark说IPv6中的下一个标头值是错误的,我认为有效负载大小太大)

我希望,有人可以用libnet11(libnet_write_raw_ipv6())帮助我使用 minimal code example, showing how to manually build IPv6 packet (带ICMPv6扩展头) .

我假设最小代码可能如下所示:

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);

我试图找到代码示例,但没有成功 . 先感谢您 .

马丁

2 回答

  • 0

    如果你're using C++, then I'推荐你libtins,一个打包嗅探库的数据包 . 这个简短的片段完全符合您的要求:

    #include <tins/tins.h>
    
    using namespace Tins;
    
    void test(const IPv6Address &dst, const IPv6Address &src) {
        PacketSender sender;
        IPv6 ipv6 = IPv6(dst, src) / ICMPv6();
        ipv6.hop_limit(64);
        sender.send(ipv6);
    }
    
    int main() {
        // now use it
        test("f0ef:1234::1", "f000::1");
    }
    
  • 0

    您可以使用原始套接字创建它 . 我也必须做类似的事情,但找不到任何参考 .

    要使用原始套接字,这个link给你一个很好的解释

相关问题