Annotation of embedaddon/libnet/src/libnet_internal.c, revision 1.1.1.2
1.1 misho 1: /*
2: * $Id: libnet_internal.c,v 1.14 2004/03/16 18:40:59 mike Exp $
3: *
4: * libnet
5: * libnet_internal.c - secret routines!
6: *
7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8: * All rights reserved.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
31: */
32:
33: #if (HAVE_CONFIG_H)
34: #include "../include/config.h"
35: #endif
36: #if (!(_WIN32) || (__CYGWIN__))
37: #include "../include/libnet.h"
38: #else
39: #include "../include/win32/libnet.h"
40: #endif
41:
42: void
1.1.1.2 ! misho 43: libnet_diag_dump_hex(const uint8_t *packet, uint32_t len, int swap, FILE *stream)
1.1 misho 44: {
45: int i, s_cnt;
1.1.1.2 ! misho 46: uint16_t *p;
1.1 misho 47:
1.1.1.2 ! misho 48: p = (uint16_t *)packet;
! 49: s_cnt = len / sizeof(uint16_t);
1.1 misho 50:
51: fprintf(stream, "\t");
52: for (i = 0; --s_cnt >= 0; i++)
53: {
54: if ((!(i % 8)))
55: {
56: fprintf(stream, "\n%02x\t", (i * 2));
57: }
58: fprintf(stream, "%04x ", swap ? ntohs(*(p++)) : *(p++));
59: }
60:
61: /*
62: * Mop up an odd byte.
63: */
64: if (len & 1)
65: {
66: if ((!(i % 8)))
67: {
68: fprintf(stream, "\n%02x\t", (i * 2));
69: }
1.1.1.2 ! misho 70: fprintf(stream, "%02x ", *(uint8_t *)p);
1.1 misho 71: }
72: fprintf(stream, "\n");
73: }
74:
75:
76: void
77: libnet_diag_dump_context(libnet_t *l)
78: {
79: if (l == NULL)
80: {
81: return;
82: }
83:
84: fprintf(stderr, "fd:\t\t%d\n", l->fd);
85:
86: switch (l->injection_type)
87: {
88: case LIBNET_LINK:
89: fprintf(stderr, "injection type:\tLIBNET_LINK\n");
90: break;
91: case LIBNET_RAW4:
92: fprintf(stderr, "injection type:\tLIBNET_RAW4\n");
93: break;
94: case LIBNET_RAW6:
95: fprintf(stderr, "injection type:\tLIBNET_RAW6\n");
96: break;
97: case LIBNET_LINK_ADV:
98: fprintf(stderr, "injection type:\tLIBNET_LINK_ADV\n");
99: break;
100: case LIBNET_RAW4_ADV:
101: fprintf(stderr, "injection type:\tLIBNET_RAW4_ADV\n");
102: break;
103: case LIBNET_RAW6_ADV:
104: fprintf(stderr, "injection type:\tLIBNET_RAW6_ADV\n");
105: break;
106: default:
107: fprintf(stderr, "injection type:\tinvalid injection type %d\n",
108: l->injection_type);
109: break;
110: }
111:
112: fprintf(stderr, "pblock start:\t%p\n", l->protocol_blocks);
113: fprintf(stderr, "pblock end:\t%p\n", l->pblock_end);
114: fprintf(stderr, "link type:\t%d\n", l->link_type);
115: fprintf(stderr, "link offset:\t%d\n", l->link_offset);
116: fprintf(stderr, "aligner:\t%d\n", l->aligner);
117: fprintf(stderr, "device:\t\t%s\n", l->device);
1.1.1.2 ! misho 118: fprintf(stderr, "packets sent:\t%lld\n", (long long int)l->stats.packets_sent);
! 119: fprintf(stderr, "packet errors:\t%lld\n", (long long int)l->stats.packet_errors);
! 120: fprintf(stderr, "bytes written:\t%lld\n", (long long int)l->stats.bytes_written);
1.1 misho 121: fprintf(stderr, "ptag state:\t%d\n", l->ptag_state);
122: fprintf(stderr, "context label:\t%s\n", l->label);
123: fprintf(stderr, "last errbuf:\t%s\n", l->err_buf);
124: fprintf(stderr, "total size:\t%d\n", l->total_size);
125: }
126:
127: void
128: libnet_diag_dump_pblock(libnet_t *l)
129: {
1.1.1.2 ! misho 130: uint32_t n;
1.1 misho 131: libnet_pblock_t *p;
132:
133: for (p = l->protocol_blocks; p; p = p->next)
134: {
135: fprintf(stderr, "pblock type:\t%s\n",
136: libnet_diag_dump_pblock_type(p->type));
137: fprintf(stderr, "ptag number:\t%d\n", p->ptag);
138: fprintf(stderr, "pblock address:\t%p\n", p);
139: fprintf(stderr, "next pblock\t%p ", p->next);
140: if (p->next)
141: {
142: fprintf(stderr, "(%s)",
143: libnet_diag_dump_pblock_type(p->next->type));
144: }
145: fprintf(stderr, "\n");
146: fprintf(stderr, "prev pblock\t%p ", p->prev);
147: if (p->prev)
148: {
149: fprintf(stderr, "(%s)",
150: libnet_diag_dump_pblock_type(p->prev->type));
151: }
152: fprintf(stderr, "\n");
153: fprintf(stderr, "buf:\t\t");
154: for (n = 0; n < p->b_len; n++)
155: {
156: fprintf(stderr, "%02x", p->buf[n]);
157: }
158: fprintf(stderr, "\nbuffer length:\t%d\n", p->b_len);
159: if ((p->flags) & LIBNET_PBLOCK_DO_CHECKSUM)
160: {
161: fprintf(stderr, "checksum flag:\tYes\n");
162: fprintf(stderr, "chksum length:\t%d\n", p->h_len);
163: }
164: else
165: {
166: fprintf(stderr, "checksum flag:\tNo\n");
167: }
168: fprintf(stderr, "bytes copied:\t%d\n\n", p->copied);
169: }
170: }
171:
172: char *
1.1.1.2 ! misho 173: libnet_diag_dump_pblock_type(uint8_t type)
1.1 misho 174: {
175: switch (type)
176: {
1.1.1.2 ! misho 177: /* below text can be regenerated using ./map-pblock-types */
1.1 misho 178: case LIBNET_PBLOCK_ARP_H:
1.1.1.2 ! misho 179: return ("arp");
1.1 misho 180: case LIBNET_PBLOCK_DHCPV4_H:
1.1.1.2 ! misho 181: return ("dhcpv4");
1.1 misho 182: case LIBNET_PBLOCK_DNSV4_H:
1.1.1.2 ! misho 183: return ("dnsv4");
1.1 misho 184: case LIBNET_PBLOCK_ETH_H:
1.1.1.2 ! misho 185: return ("eth");
1.1 misho 186: case LIBNET_PBLOCK_ICMPV4_H:
1.1.1.2 ! misho 187: return ("icmpv4");
1.1 misho 188: case LIBNET_PBLOCK_ICMPV4_ECHO_H:
1.1.1.2 ! misho 189: return ("icmpv4_echo");
1.1 misho 190: case LIBNET_PBLOCK_ICMPV4_MASK_H:
1.1.1.2 ! misho 191: return ("icmpv4_mask");
1.1 misho 192: case LIBNET_PBLOCK_ICMPV4_UNREACH_H:
1.1.1.2 ! misho 193: return ("icmpv4_unreach");
1.1 misho 194: case LIBNET_PBLOCK_ICMPV4_TIMXCEED_H:
1.1.1.2 ! misho 195: return ("icmpv4_timxceed");
1.1 misho 196: case LIBNET_PBLOCK_ICMPV4_REDIRECT_H:
1.1.1.2 ! misho 197: return ("icmpv4_redirect");
1.1 misho 198: case LIBNET_PBLOCK_ICMPV4_TS_H:
1.1.1.2 ! misho 199: return ("icmpv4_ts");
1.1 misho 200: case LIBNET_PBLOCK_IGMP_H:
1.1.1.2 ! misho 201: return ("igmp");
1.1 misho 202: case LIBNET_PBLOCK_IPV4_H:
1.1.1.2 ! misho 203: return ("ipv4");
1.1 misho 204: case LIBNET_PBLOCK_IPO_H:
1.1.1.2 ! misho 205: return ("ipo");
1.1 misho 206: case LIBNET_PBLOCK_IPDATA:
1.1.1.2 ! misho 207: return ("ipdata");
1.1 misho 208: case LIBNET_PBLOCK_OSPF_H:
1.1.1.2 ! misho 209: return ("ospf");
1.1 misho 210: case LIBNET_PBLOCK_OSPF_HELLO_H:
1.1.1.2 ! misho 211: return ("ospf_hello");
1.1 misho 212: case LIBNET_PBLOCK_OSPF_DBD_H:
1.1.1.2 ! misho 213: return ("ospf_dbd");
1.1 misho 214: case LIBNET_PBLOCK_OSPF_LSR_H:
1.1.1.2 ! misho 215: return ("ospf_lsr");
1.1 misho 216: case LIBNET_PBLOCK_OSPF_LSU_H:
1.1.1.2 ! misho 217: return ("ospf_lsu");
1.1 misho 218: case LIBNET_PBLOCK_OSPF_LSA_H:
1.1.1.2 ! misho 219: return ("ospf_lsa");
1.1 misho 220: case LIBNET_PBLOCK_OSPF_AUTH_H:
1.1.1.2 ! misho 221: return ("ospf_auth");
1.1 misho 222: case LIBNET_PBLOCK_OSPF_CKSUM:
1.1.1.2 ! misho 223: return ("ospf_cksum");
1.1 misho 224: case LIBNET_PBLOCK_LS_RTR_H:
1.1.1.2 ! misho 225: return ("ls_rtr");
1.1 misho 226: case LIBNET_PBLOCK_LS_NET_H:
1.1.1.2 ! misho 227: return ("ls_net");
1.1 misho 228: case LIBNET_PBLOCK_LS_SUM_H:
1.1.1.2 ! misho 229: return ("ls_sum");
1.1 misho 230: case LIBNET_PBLOCK_LS_AS_EXT_H:
1.1.1.2 ! misho 231: return ("ls_as_ext");
1.1 misho 232: case LIBNET_PBLOCK_NTP_H:
1.1.1.2 ! misho 233: return ("ntp");
1.1 misho 234: case LIBNET_PBLOCK_RIP_H:
1.1.1.2 ! misho 235: return ("rip");
1.1 misho 236: case LIBNET_PBLOCK_TCP_H:
1.1.1.2 ! misho 237: return ("tcp");
1.1 misho 238: case LIBNET_PBLOCK_TCPO_H:
1.1.1.2 ! misho 239: return ("tcpo");
1.1 misho 240: case LIBNET_PBLOCK_TCPDATA:
1.1.1.2 ! misho 241: return ("tcpdata");
1.1 misho 242: case LIBNET_PBLOCK_UDP_H:
1.1.1.2 ! misho 243: return ("udp");
1.1 misho 244: case LIBNET_PBLOCK_VRRP_H:
1.1.1.2 ! misho 245: return ("vrrp");
1.1 misho 246: case LIBNET_PBLOCK_DATA_H:
247: return ("data");
248: case LIBNET_PBLOCK_CDP_H:
1.1.1.2 ! misho 249: return ("cdp");
1.1 misho 250: case LIBNET_PBLOCK_IPSEC_ESP_HDR_H:
1.1.1.2 ! misho 251: return ("ipsec_esp_hdr");
1.1 misho 252: case LIBNET_PBLOCK_IPSEC_ESP_FTR_H:
1.1.1.2 ! misho 253: return ("ipsec_esp_ftr");
1.1 misho 254: case LIBNET_PBLOCK_IPSEC_AH_H:
1.1.1.2 ! misho 255: return ("ipsec_ah");
1.1 misho 256: case LIBNET_PBLOCK_802_1Q_H:
1.1.1.2 ! misho 257: return ("802_1q");
1.1 misho 258: case LIBNET_PBLOCK_802_2_H:
1.1.1.2 ! misho 259: return ("802_2");
1.1 misho 260: case LIBNET_PBLOCK_802_2SNAP_H:
1.1.1.2 ! misho 261: return ("802_2snap");
1.1 misho 262: case LIBNET_PBLOCK_802_3_H:
1.1.1.2 ! misho 263: return ("802_3");
1.1 misho 264: case LIBNET_PBLOCK_STP_CONF_H:
1.1.1.2 ! misho 265: return ("stp_conf");
1.1 misho 266: case LIBNET_PBLOCK_STP_TCN_H:
1.1.1.2 ! misho 267: return ("stp_tcn");
1.1 misho 268: case LIBNET_PBLOCK_ISL_H:
1.1.1.2 ! misho 269: return ("isl");
1.1 misho 270: case LIBNET_PBLOCK_IPV6_H:
1.1.1.2 ! misho 271: return ("ipv6");
1.1 misho 272: case LIBNET_PBLOCK_802_1X_H:
1.1.1.2 ! misho 273: return ("802_1x");
1.1 misho 274: case LIBNET_PBLOCK_RPC_CALL_H:
1.1.1.2 ! misho 275: return ("rpc_call");
1.1 misho 276: case LIBNET_PBLOCK_MPLS_H:
1.1.1.2 ! misho 277: return ("mpls");
1.1 misho 278: case LIBNET_PBLOCK_FDDI_H:
1.1.1.2 ! misho 279: return ("fddi");
1.1 misho 280: case LIBNET_PBLOCK_TOKEN_RING_H:
1.1.1.2 ! misho 281: return ("token_ring");
1.1 misho 282: case LIBNET_PBLOCK_BGP4_HEADER_H:
1.1.1.2 ! misho 283: return ("bgp4_header");
1.1 misho 284: case LIBNET_PBLOCK_BGP4_OPEN_H:
1.1.1.2 ! misho 285: return ("bgp4_open");
1.1 misho 286: case LIBNET_PBLOCK_BGP4_UPDATE_H:
1.1.1.2 ! misho 287: return ("bgp4_update");
1.1 misho 288: case LIBNET_PBLOCK_BGP4_NOTIFICATION_H:
1.1.1.2 ! misho 289: return ("bgp4_notification");
1.1 misho 290: case LIBNET_PBLOCK_GRE_H:
1.1.1.2 ! misho 291: return ("gre");
1.1 misho 292: case LIBNET_PBLOCK_GRE_SRE_H:
1.1.1.2 ! misho 293: return ("gre_sre");
1.1 misho 294: case LIBNET_PBLOCK_IPV6_FRAG_H:
1.1.1.2 ! misho 295: return ("ipv6_frag");
1.1 misho 296: case LIBNET_PBLOCK_IPV6_ROUTING_H:
1.1.1.2 ! misho 297: return ("ipv6_routing");
1.1 misho 298: case LIBNET_PBLOCK_IPV6_DESTOPTS_H:
1.1.1.2 ! misho 299: return ("ipv6_destopts");
1.1 misho 300: case LIBNET_PBLOCK_IPV6_HBHOPTS_H:
1.1.1.2 ! misho 301: return ("ipv6_hbhopts");
! 302: case LIBNET_PBLOCK_SEBEK_H:
! 303: return ("sebek");
! 304: case LIBNET_PBLOCK_HSRP_H:
! 305: return ("hsrp");
! 306: case LIBNET_PBLOCK_ICMPV6_H:
! 307: return ("icmpv6");
! 308: case LIBNET_PBLOCK_ICMPV6_UNREACH_H:
! 309: return ("icmpv6_unreach");
! 310: case LIBNET_PBLOCK_ICMPV6_NDP_NSOL_H:
! 311: return ("icmpv6_ndp_nsol");
! 312: case LIBNET_PBLOCK_ICMPV6_NDP_NADV_H:
! 313: return ("icmpv6_ndp_nadv");
! 314: case LIBNET_PBLOCK_ICMPV6_NDP_OPT_H:
! 315: return ("icmpv6_ndp_opt");
1.1 misho 316: }
1.1.1.2 ! misho 317: return ("unrecognized pblock");
1.1 misho 318: }
1.1.1.2 ! misho 319:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>