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

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>
1.1.1.2 ! misho      36: #ifdef _WIN32
        !            37: #include <winsock2.h>
        !            38: #else
1.1       misho      39: #include <netinet/in.h>
1.1.1.2 ! misho      40: #endif
1.1       misho      41: 
                     42: static void print_pblocks(libnet_t* l)
                     43: {
                     44:     libnet_pblock_t* p = l->protocol_blocks;
                     45: 
                     46:     while(p) {
                     47:         /* h_len is header length for checksumming? "chksum length"? */
                     48:         printf("  tag %d flags %d type %20s/%#x buf %p b_len %2u h_len %2u copied %2u\n",
                     49:                 p->ptag, p->flags,
                     50:                 libnet_diag_dump_pblock_type(p->type), p->type,
                     51:                 p->buf, p->b_len, p->h_len, p->copied);
                     52:         p = p->next;
                     53:     }
                     54:     printf("  link_offset %d aligner %d total_size %u nblocks %d\n",
                     55:             l->link_offset, l->aligner, l->total_size, l->n_pblocks);
                     56: 
                     57: }
                     58: 
                     59: int
                     60: main(int argc, char *argv[])
                     61: {
                     62:     libnet_t *l;
                     63:     int r;
                     64:     char *device = "eth0";
                     65:     struct libnet_ether_addr *mac_address;
                     66:     struct in6_addr src_ip;
                     67:     struct libnet_in6_addr dst_ip;
                     68:     char errbuf[LIBNET_ERRBUF_SIZE];
                     69:     libnet_ptag_t icmp_ptag = 0;
                     70:     libnet_ptag_t ipv6_ptag = 0;
                     71:     char payload[24] = { 0 };
                     72: 
                     73:     memset(&src_ip, 0x66, sizeof(src_ip));
                     74: 
                     75:     l = libnet_init( LIBNET_RAW6, device, errbuf);
                     76: 
                     77:     assert(l);
                     78: 
                     79:     mac_address = libnet_get_hwaddr(l);
                     80:     assert(mac_address);
                     81: 
                     82:     dst_ip = libnet_name2addr6(l, "::1" /* BCAST_ADDR - defined where? */, LIBNET_DONT_RESOLVE);
                     83: 
                     84:     memcpy(payload,src_ip.s6_addr,16);
                     85:     payload[16] = 2; /* 2 for Target Link-layer Address */
                     86:     payload[17] = 1; /* The length of the option */
                     87:     memcpy(payload+18,mac_address->ether_addr_octet, 6);
                     88: 
                     89:     /* 0x2000: RSO */
                     90:     icmp_ptag = libnet_build_icmpv4_echo(
                     91:             136,0,0,0x2000,0,
                     92:             (uint8_t *)payload,sizeof(payload), l, LIBNET_PTAG_INITIALIZER);
                     93:     assert(icmp_ptag);
                     94: 
                     95:     ipv6_ptag = libnet_build_ipv6(
                     96:             0, 0,
                     97:             LIBNET_ICMPV6_H + sizeof(payload), /* ICMPV6_H == ICMPV4_H, luckily */
                     98:             IPPROTO_ICMP6,
                     99:             255,
                    100:             *(struct libnet_in6_addr*)&src_ip,
                    101:             dst_ip,
                    102:             NULL, 0,
                    103:             l, 0);
                    104:     assert(icmp_ptag);
                    105: 
                    106:     print_pblocks(l);
                    107: 
                    108:     {
                    109:        uint8_t* pkt1 = NULL;
                    110:        uint32_t pkt1_sz = 0;
                    111:        r = libnet_pblock_coalesce(l, &pkt1, &pkt1_sz);
                    112:        assert(r >= 0);
                    113: 
                    114:        libnet_diag_dump_hex(pkt1, LIBNET_IPV6_H, 0, stdout);
                    115:        libnet_diag_dump_hex(pkt1+LIBNET_IPV6_H, pkt1_sz-LIBNET_IPV6_H, 0, stdout);
                    116: 
                    117:        free(pkt1);
                    118:        pkt1 = NULL;
                    119:     }
                    120: 
                    121:     r = libnet_write(l);
                    122:     assert(r >= 0);
                    123: 
                    124:     return (EXIT_SUCCESS);
                    125: }
                    126: 

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