示例#1
0
static
void assert_lengths(libnet_t* l, int ip_len, int ip_ihl, int payload_s)
{
    uint8_t* pkt1 = NULL;
    uint32_t pkt1_sz = 0;
    struct libnet_ipv4_hdr* h1;
    uint8_t* payload = NULL;


    int r = libnet_pblock_coalesce(l, &pkt1, &pkt1_sz);
    assert(r >= 0);

    print_pblocks(l);

    libnet_diag_dump_hex(pkt1, 14, 1, stdout);
    libnet_diag_dump_hex(pkt1+14, pkt1_sz-14, 1, stdout);

    // check ip IHL value, total ip pkt length, and options value
    h1 = (struct libnet_ipv4_hdr*) (pkt1+14);
    assert_eq(h1->ip_hl, ip_ihl); 
    assert_eq(ntohs(h1->ip_len), ip_len);

    payload = ((uint8_t*) h1) + ip_ihl * 4;
    if(payload_s > 0) {
        assert(payload[0] == (uint8_t)'\x99');
        assert(payload[payload_s-1] == (uint8_t)'\x99');
    }
}
int
main(int argc, char *argv[])
{
    libnet_t *l;
    int r;
    char *device = "eth0";
    struct libnet_ether_addr *mac_address;
    struct in6_addr src_ip;
    struct libnet_in6_addr dst_ip;
    char errbuf[LIBNET_ERRBUF_SIZE];
    libnet_ptag_t icmp_ptag = 0;
    libnet_ptag_t ipv6_ptag = 0;
    char payload[24] = { 0 };

    memset(&src_ip, 0x66, sizeof(src_ip));

    l = libnet_init( LIBNET_RAW6, device, errbuf);

    assert(l);

    mac_address = libnet_get_hwaddr(l);
    assert(mac_address);

    dst_ip = libnet_name2addr6(l, "::1" /* BCAST_ADDR - defined where? */, LIBNET_DONT_RESOLVE);

    memcpy(payload,src_ip.s6_addr,16);
    payload[16] = 2; /* 2 for Target Link-layer Address */
    payload[17] = 1; /* The length of the option */
    memcpy(payload+18,mac_address->ether_addr_octet, 6);

    /* 0x2000: RSO */
    icmp_ptag = libnet_build_icmpv4_echo(
            136,0,0,0x2000,0,
            (uint8_t *)payload,sizeof(payload), l, LIBNET_PTAG_INITIALIZER);
    assert(icmp_ptag);

    ipv6_ptag = libnet_build_ipv6(
            0, 0,
            LIBNET_ICMPV6_H + sizeof(payload), /* ICMPV6_H == ICMPV4_H, luckily */
            IPPROTO_ICMP6,
            255,
            *(struct libnet_in6_addr*)&src_ip,
            dst_ip,
            NULL, 0,
            l, 0);
    assert(icmp_ptag);

    print_pblocks(l);

    {
       uint8_t* pkt1 = NULL;
       uint32_t pkt1_sz = 0;
       r = libnet_pblock_coalesce(l, &pkt1, &pkt1_sz);
       assert(r >= 0);

       libnet_diag_dump_hex(pkt1, LIBNET_IPV6_H, 0, stdout);
       libnet_diag_dump_hex(pkt1+LIBNET_IPV6_H, pkt1_sz-LIBNET_IPV6_H, 0, stdout);

       free(pkt1);
       pkt1 = NULL;
    }

    r = libnet_write(l);
    assert(r >= 0);

    return (EXIT_SUCCESS);
}