Annotation of embedaddon/libnet/sample/test_ipv6_icmpv4.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Regression test for bugs such as reported in:
                      3:  *   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=418975
                      4:  *
                      5:  * Copyright (c) 2009 Sam Roberts <sroberts@wurldtech.com>
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  *
                     29:  */
                     30: #if (HAVE_CONFIG_H)
                     31: #include "../include/config.h"
                     32: #endif
                     33: #include "./libnet_test.h"
                     34: 
                     35: #include <assert.h>
                     36: 
                     37: #include <netinet/in.h>
                     38: 
                     39: static void print_pblocks(libnet_t* l)
                     40: {
                     41:     libnet_pblock_t* p = l->protocol_blocks;
                     42: 
                     43:     while(p) {
                     44:         /* h_len is header length for checksumming? "chksum length"? */
                     45:         printf("  tag %d flags %d type %20s/%#x buf %p b_len %2u h_len %2u copied %2u\n",
                     46:                 p->ptag, p->flags,
                     47:                 libnet_diag_dump_pblock_type(p->type), p->type,
                     48:                 p->buf, p->b_len, p->h_len, p->copied);
                     49:         p = p->next;
                     50:     }
                     51:     printf("  link_offset %d aligner %d total_size %u nblocks %d\n",
                     52:             l->link_offset, l->aligner, l->total_size, l->n_pblocks);
                     53: 
                     54: }
                     55: 
                     56: int
                     57: main(int argc, char *argv[])
                     58: {
                     59:     libnet_t *l;
                     60:     int r;
                     61:     char *device = "eth0";
                     62:     struct libnet_ether_addr *mac_address;
                     63:     struct in6_addr src_ip;
                     64:     struct libnet_in6_addr dst_ip;
                     65:     char errbuf[LIBNET_ERRBUF_SIZE];
                     66:     libnet_ptag_t icmp_ptag = 0;
                     67:     libnet_ptag_t ipv6_ptag = 0;
                     68:     char payload[24] = { 0 };
                     69: 
                     70:     memset(&src_ip, 0x66, sizeof(src_ip));
                     71: 
                     72:     l = libnet_init( LIBNET_RAW6, device, errbuf);
                     73: 
                     74:     assert(l);
                     75: 
                     76:     mac_address = libnet_get_hwaddr(l);
                     77:     assert(mac_address);
                     78: 
                     79:     dst_ip = libnet_name2addr6(l, "::1" /* BCAST_ADDR - defined where? */, LIBNET_DONT_RESOLVE);
                     80: 
                     81:     memcpy(payload,src_ip.s6_addr,16);
                     82:     payload[16] = 2; /* 2 for Target Link-layer Address */
                     83:     payload[17] = 1; /* The length of the option */
                     84:     memcpy(payload+18,mac_address->ether_addr_octet, 6);
                     85: 
                     86:     /* 0x2000: RSO */
                     87:     icmp_ptag = libnet_build_icmpv4_echo(
                     88:             136,0,0,0x2000,0,
                     89:             (uint8_t *)payload,sizeof(payload), l, LIBNET_PTAG_INITIALIZER);
                     90:     assert(icmp_ptag);
                     91: 
                     92:     ipv6_ptag = libnet_build_ipv6(
                     93:             0, 0,
                     94:             LIBNET_ICMPV6_H + sizeof(payload), /* ICMPV6_H == ICMPV4_H, luckily */
                     95:             IPPROTO_ICMP6,
                     96:             255,
                     97:             *(struct libnet_in6_addr*)&src_ip,
                     98:             dst_ip,
                     99:             NULL, 0,
                    100:             l, 0);
                    101:     assert(icmp_ptag);
                    102: 
                    103:     print_pblocks(l);
                    104: 
                    105:     {
                    106:        uint8_t* pkt1 = NULL;
                    107:        uint32_t pkt1_sz = 0;
                    108:        r = libnet_pblock_coalesce(l, &pkt1, &pkt1_sz);
                    109:        assert(r >= 0);
                    110: 
                    111:        libnet_diag_dump_hex(pkt1, LIBNET_IPV6_H, 0, stdout);
                    112:        libnet_diag_dump_hex(pkt1+LIBNET_IPV6_H, pkt1_sz-LIBNET_IPV6_H, 0, stdout);
                    113: 
                    114:        free(pkt1);
                    115:        pkt1 = NULL;
                    116:     }
                    117: 
                    118:     r = libnet_write(l);
                    119:     assert(r >= 0);
                    120: 
                    121:     return (EXIT_SUCCESS);
                    122: }
                    123: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>