| 1 | /* |
| 2 | * $Id: libnet_internal.c,v 1.19 2005/11/29 22:49:19 carlosc 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 |
| 43 | libnet_diag_dump_hex(u_int8_t *packet, u_int32_t len, int swap, FILE *stream) |
| 44 | { |
| 45 | int i, s_cnt; |
| 46 | u_int16_t *p; |
| 47 | |
| 48 | p = (u_int16_t *)packet; |
| 49 | s_cnt = len / sizeof(u_int16_t); |
| 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 | } |
| 70 | fprintf(stream, "%02x ", *(u_int8_t *)p); |
| 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); |
| 118 | fprintf(stderr, "packets sent:\t%lld\n", l->stats.packets_sent); |
| 119 | fprintf(stderr, "packet errors:\t%lld\n", l->stats.packet_errors); |
| 120 | fprintf(stderr, "bytes written:\t%lld\n", l->stats.bytes_written); |
| 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 | { |
| 130 | u_int32_t n; |
| 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, "IP offset:\t%d\n", p->ip_offset); |
| 139 | fprintf(stderr, "pblock address:\t%p\n", p); |
| 140 | fprintf(stderr, "next pblock\t%p ", p->next); |
| 141 | if (p->next) |
| 142 | { |
| 143 | fprintf(stderr, "(%s)", |
| 144 | libnet_diag_dump_pblock_type(p->next->type)); |
| 145 | } |
| 146 | fprintf(stderr, "\n"); |
| 147 | fprintf(stderr, "prev pblock\t%p ", p->prev); |
| 148 | if (p->prev) |
| 149 | { |
| 150 | fprintf(stderr, "(%s)", |
| 151 | libnet_diag_dump_pblock_type(p->prev->type)); |
| 152 | } |
| 153 | fprintf(stderr, "\n"); |
| 154 | fprintf(stderr, "buf:\t\t"); |
| 155 | for (n = 0; n < p->b_len; n++) |
| 156 | { |
| 157 | fprintf(stderr, "%02x", p->buf[n]); |
| 158 | } |
| 159 | fprintf(stderr, "\nbuffer length:\t%d\n", p->b_len); |
| 160 | if ((p->flags) & LIBNET_PBLOCK_DO_CHECKSUM) |
| 161 | { |
| 162 | fprintf(stderr, "checksum flag:\tYes\n"); |
| 163 | fprintf(stderr, "chksum length:\t%d\n", p->h_len); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | fprintf(stderr, "checksum flag:\tNo\n"); |
| 168 | } |
| 169 | fprintf(stderr, "bytes copied:\t%d\n\n", p->copied); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | char * |
| 174 | libnet_diag_dump_pblock_type(u_int8_t type) |
| 175 | { |
| 176 | static int8_t buf[50]; |
| 177 | switch (type) |
| 178 | { |
| 179 | case LIBNET_PBLOCK_ARP_H: |
| 180 | return ("arp header"); |
| 181 | case LIBNET_PBLOCK_DHCPV4_H: |
| 182 | return ("dhcpv4 header"); |
| 183 | case LIBNET_PBLOCK_DNSV4_H: |
| 184 | return ("dnsv4 header"); |
| 185 | case LIBNET_PBLOCK_ETH_H: |
| 186 | return ("ethernet header"); |
| 187 | case LIBNET_PBLOCK_ICMPV4_H: |
| 188 | return ("icmpv4 header"); |
| 189 | case LIBNET_PBLOCK_ICMPV4_ECHO_H: |
| 190 | return ("icmpv4 echo header"); |
| 191 | case LIBNET_PBLOCK_ICMPV4_MASK_H: |
| 192 | return ("icmpv4 mask header"); |
| 193 | case LIBNET_PBLOCK_ICMPV4_UNREACH_H: |
| 194 | return ("icmpv4 unreachable header"); |
| 195 | case LIBNET_PBLOCK_ICMPV4_TIMXCEED_H: |
| 196 | return ("icmpv4 time exceeded header"); |
| 197 | case LIBNET_PBLOCK_ICMPV4_REDIRECT_H: |
| 198 | return ("icmpv4 redirect header"); |
| 199 | case LIBNET_PBLOCK_ICMPV4_TS_H: |
| 200 | return ("icmpv4 timestamp header"); |
| 201 | case LIBNET_PBLOCK_IGMP_H: |
| 202 | return ("igmp header"); |
| 203 | case LIBNET_PBLOCK_IPV4_H: |
| 204 | return ("ipv4 header"); |
| 205 | case LIBNET_PBLOCK_IPO_H: |
| 206 | return ("ip options header"); |
| 207 | case LIBNET_PBLOCK_IPDATA: |
| 208 | return ("ip data"); |
| 209 | case LIBNET_PBLOCK_OSPF_H: |
| 210 | return ("ospf header"); |
| 211 | case LIBNET_PBLOCK_OSPF_HELLO_H: |
| 212 | return ("ospf hello header"); |
| 213 | case LIBNET_PBLOCK_OSPF_DBD_H: |
| 214 | return ("ospf dbd header"); |
| 215 | case LIBNET_PBLOCK_OSPF_LSR_H: |
| 216 | return ("ospf lsr header"); |
| 217 | case LIBNET_PBLOCK_OSPF_LSU_H: |
| 218 | return ("ospf lsu header"); |
| 219 | case LIBNET_PBLOCK_OSPF_LSA_H: |
| 220 | return ("ospf lsa header"); |
| 221 | case LIBNET_PBLOCK_OSPF_AUTH_H: |
| 222 | return ("ospf authentication header"); |
| 223 | case LIBNET_PBLOCK_OSPF_CKSUM: |
| 224 | return ("ospf checksum"); |
| 225 | case LIBNET_PBLOCK_LS_RTR_H: |
| 226 | return ("ospf ls rtr header"); |
| 227 | case LIBNET_PBLOCK_LS_NET_H: |
| 228 | return ("ospf ls net header"); |
| 229 | case LIBNET_PBLOCK_LS_SUM_H: |
| 230 | return ("ospf ls sum header"); |
| 231 | case LIBNET_PBLOCK_LS_AS_EXT_H: |
| 232 | return ("ospf ls as extension header"); |
| 233 | case LIBNET_PBLOCK_NTP_H: |
| 234 | return ("ntp header"); |
| 235 | case LIBNET_PBLOCK_RIP_H: |
| 236 | return ("rip header"); |
| 237 | case LIBNET_PBLOCK_TCP_H: |
| 238 | return ("tcp header"); |
| 239 | case LIBNET_PBLOCK_TCPO_H: |
| 240 | return ("tcp options header"); |
| 241 | case LIBNET_PBLOCK_TCPDATA: |
| 242 | return ("tcp data"); |
| 243 | case LIBNET_PBLOCK_UDP_H: |
| 244 | return ("udp header"); |
| 245 | case LIBNET_PBLOCK_VRRP_H: |
| 246 | return ("vrrp header"); |
| 247 | case LIBNET_PBLOCK_DATA_H: |
| 248 | return ("data"); |
| 249 | case LIBNET_PBLOCK_CDP_H: |
| 250 | return ("cdp header"); |
| 251 | case LIBNET_PBLOCK_IPSEC_ESP_HDR_H: |
| 252 | return ("ipsec esp header"); |
| 253 | case LIBNET_PBLOCK_IPSEC_ESP_FTR_H: |
| 254 | return ("ipsec esp footer"); |
| 255 | case LIBNET_PBLOCK_IPSEC_AH_H: |
| 256 | return ("ipsec authentication header"); |
| 257 | case LIBNET_PBLOCK_802_1Q_H: |
| 258 | return ("802.1q header"); |
| 259 | case LIBNET_PBLOCK_802_2_H: |
| 260 | return ("802.2 header"); |
| 261 | case LIBNET_PBLOCK_802_2SNAP_H: |
| 262 | return ("802.2SNAP header"); |
| 263 | case LIBNET_PBLOCK_802_3_H: |
| 264 | return ("802.3 header"); |
| 265 | case LIBNET_PBLOCK_STP_CONF_H: |
| 266 | return ("stp configuration header"); |
| 267 | case LIBNET_PBLOCK_STP_TCN_H: |
| 268 | return ("stp tcn header"); |
| 269 | case LIBNET_PBLOCK_ISL_H: |
| 270 | return ("isl header"); |
| 271 | case LIBNET_PBLOCK_IPV6_H: |
| 272 | return ("ipv6 header"); |
| 273 | case LIBNET_PBLOCK_802_1X_H: |
| 274 | return ("802.1x header"); |
| 275 | case LIBNET_PBLOCK_RPC_CALL_H: |
| 276 | return ("rpc call header"); |
| 277 | case LIBNET_PBLOCK_MPLS_H: |
| 278 | return ("mlps header"); |
| 279 | case LIBNET_PBLOCK_FDDI_H: |
| 280 | return ("fddi header"); |
| 281 | case LIBNET_PBLOCK_TOKEN_RING_H: |
| 282 | return ("token ring header"); |
| 283 | case LIBNET_PBLOCK_BGP4_HEADER_H: |
| 284 | return ("bgp header"); |
| 285 | case LIBNET_PBLOCK_BGP4_OPEN_H: |
| 286 | return ("bgp open header"); |
| 287 | case LIBNET_PBLOCK_BGP4_UPDATE_H: |
| 288 | return ("bgp update header"); |
| 289 | case LIBNET_PBLOCK_BGP4_NOTIFICATION_H: |
| 290 | return ("bgp notification header"); |
| 291 | case LIBNET_PBLOCK_GRE_H: |
| 292 | return ("gre header"); |
| 293 | case LIBNET_PBLOCK_GRE_SRE_H: |
| 294 | return ("gre sre header"); |
| 295 | case LIBNET_PBLOCK_IPV6_FRAG_H: |
| 296 | return ("ipv6 fragmentation header"); |
| 297 | case LIBNET_PBLOCK_IPV6_ROUTING_H: |
| 298 | return ("ipv6 routing header"); |
| 299 | case LIBNET_PBLOCK_IPV6_DESTOPTS_H: |
| 300 | return ("ipv6 destination options header"); |
| 301 | case LIBNET_PBLOCK_IPV6_HBHOPTS_H: |
| 302 | return ("ipv6 hop by hop options header"); |
| 303 | default: |
| 304 | snprintf(buf, sizeof(buf), |
| 305 | "%s(): unknown pblock type: %d", __func__, type); |
| 306 | return (buf); |
| 307 | } |
| 308 | return ("unreachable code"); |
| 309 | } |
| 310 | /* EOF */ |