File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_internal.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (12 months, 3 weeks ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_internal.c,v 1.1.1.3 2023/09/27 11:11:38 misho 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: #include "common.h"
   34: 
   35: void
   36: libnet_diag_dump_hex(const uint8_t *packet, uint32_t len, int swap, FILE *stream)
   37: {
   38:     int i, s_cnt;
   39:     uint16_t *p;
   40: 
   41:     p     = (uint16_t *)packet;
   42:     s_cnt = len / sizeof(uint16_t);
   43: 
   44:     fprintf(stream, "\t");
   45:     for (i = 0; --s_cnt >= 0; i++)
   46:     {
   47:         if ((!(i % 8)))
   48:         {
   49:             fprintf(stream, "\n%02x\t", (i * 2));
   50:         }
   51:         fprintf(stream, "%04x ", swap ? ntohs(*(p++)) : *(p++));
   52:     }
   53: 
   54:     /*
   55:      *  Mop up an odd byte.
   56:      */
   57:     if (len & 1)
   58:     {
   59:         if ((!(i % 8)))
   60:         {
   61:             fprintf(stream, "\n%02x\t", (i * 2));
   62:         }
   63:         fprintf(stream, "%02x ", *(uint8_t *)p);
   64:     }
   65:     fprintf(stream, "\n");
   66: }
   67: 
   68: 
   69: void
   70: libnet_diag_dump_context(libnet_t *l)
   71: {
   72:     if (l == NULL)
   73:     { 
   74:         return;
   75:     } 
   76: 
   77:     fprintf(stderr, "fd:\t\t%d\n", l->fd);
   78:  
   79:     switch (l->injection_type)
   80:     {
   81:         case LIBNET_LINK:
   82:             fprintf(stderr, "injection type:\tLIBNET_LINK\n");
   83:             break;
   84:         case LIBNET_RAW4:
   85:             fprintf(stderr, "injection type:\tLIBNET_RAW4\n");
   86:             break;
   87:         case LIBNET_RAW6:
   88:             fprintf(stderr, "injection type:\tLIBNET_RAW6\n");
   89:             break;
   90:         case LIBNET_LINK_ADV:
   91:             fprintf(stderr, "injection type:\tLIBNET_LINK_ADV\n");
   92:             break;
   93:         case LIBNET_RAW4_ADV:
   94:             fprintf(stderr, "injection type:\tLIBNET_RAW4_ADV\n");
   95:             break;
   96:         case LIBNET_RAW6_ADV:
   97:             fprintf(stderr, "injection type:\tLIBNET_RAW6_ADV\n");
   98:             break;
   99:         default:
  100:             fprintf(stderr, "injection type:\tinvalid injection type %d\n", 
  101:                     l->injection_type);
  102:             break;
  103:     }
  104:     
  105:     fprintf(stderr, "pblock start:\t%p\n", l->protocol_blocks);
  106:     fprintf(stderr, "pblock end:\t%p\n", l->pblock_end);
  107:     fprintf(stderr, "link type:\t%d\n", l->link_type);
  108:     fprintf(stderr, "link offset:\t%d\n", l->link_offset);
  109:     fprintf(stderr, "aligner:\t%d\n", l->aligner);
  110:     fprintf(stderr, "device:\t\t%s\n", l->device);
  111:     fprintf(stderr, "packets sent:\t%lld\n", (long long int)l->stats.packets_sent);
  112:     fprintf(stderr, "packet errors:\t%lld\n", (long long int)l->stats.packet_errors);
  113:     fprintf(stderr, "bytes written:\t%lld\n", (long long int)l->stats.bytes_written);
  114:     fprintf(stderr, "ptag state:\t%d\n", l->ptag_state);
  115:     fprintf(stderr, "context label:\t%s\n", l->label);
  116:     fprintf(stderr, "last errbuf:\t%s\n", l->err_buf);
  117:     fprintf(stderr, "total size:\t%d\n", l->total_size);
  118: }
  119: 
  120: void
  121: libnet_diag_dump_pblock(libnet_t *l)
  122: {
  123:     uint32_t n;
  124:     libnet_pblock_t *p;
  125: 
  126:     for (p = l->protocol_blocks; p; p = p->next)
  127:     {
  128:         fprintf(stderr, "pblock type:\t%s\n", 
  129:                 libnet_diag_dump_pblock_type(p->type));
  130:         fprintf(stderr, "ptag number:\t%d\n", p->ptag);
  131:         fprintf(stderr, "pblock address:\t%p\n", p);
  132:         fprintf(stderr, "next pblock\t%p ", p->next);
  133:         if (p->next)
  134:         {
  135:             fprintf(stderr, "(%s)",
  136:                     libnet_diag_dump_pblock_type(p->next->type));
  137:         }
  138:         fprintf(stderr, "\n");
  139:         fprintf(stderr, "prev pblock\t%p ", p->prev);
  140:         if (p->prev)
  141:         {
  142:             fprintf(stderr, "(%s)",
  143:                     libnet_diag_dump_pblock_type(p->prev->type));
  144:         }
  145:         fprintf(stderr, "\n");
  146:         fprintf(stderr, "buf:\t\t");
  147:         for (n = 0; n < p->b_len; n++)
  148:         {
  149:             fprintf(stderr, "%02x", p->buf[n]);
  150:         }
  151:         fprintf(stderr, "\nbuffer length:\t%d\n", p->b_len);
  152:         if ((p->flags) & LIBNET_PBLOCK_DO_CHECKSUM)
  153:         {
  154:             fprintf(stderr, "checksum flag:\tYes\n");
  155:             fprintf(stderr, "chksum length:\t%d\n", p->h_len);
  156:         }
  157:         else
  158:         {
  159:             fprintf(stderr, "checksum flag:\tNo\n");
  160:         }
  161:         fprintf(stderr, "bytes copied:\t%d\n\n", p->copied);
  162:     }
  163: }
  164: 
  165: char *
  166: libnet_diag_dump_pblock_type(uint8_t type)
  167: {
  168:     switch (type)
  169:     {
  170:         /* below text can be regenerated using ./map-pblock-types */
  171:         case LIBNET_PBLOCK_ARP_H:
  172:             return ("arp");
  173:         case LIBNET_PBLOCK_DHCPV4_H:
  174:             return ("dhcpv4");
  175:         case LIBNET_PBLOCK_DNSV4_H:
  176:             return ("dnsv4");
  177:         case LIBNET_PBLOCK_ETH_H:
  178:             return ("eth");
  179:         case LIBNET_PBLOCK_ICMPV4_H:
  180:             return ("icmpv4");
  181:         case LIBNET_PBLOCK_ICMPV4_ECHO_H:
  182:             return ("icmpv4_echo");
  183:         case LIBNET_PBLOCK_ICMPV4_MASK_H:
  184:             return ("icmpv4_mask");
  185:         case LIBNET_PBLOCK_ICMPV4_UNREACH_H:
  186:             return ("icmpv4_unreach");
  187:         case LIBNET_PBLOCK_ICMPV4_TIMXCEED_H:
  188:             return ("icmpv4_timxceed");
  189:         case LIBNET_PBLOCK_ICMPV4_REDIRECT_H:
  190:             return ("icmpv4_redirect");
  191:         case LIBNET_PBLOCK_ICMPV4_TS_H:
  192:             return ("icmpv4_ts");
  193:         case LIBNET_PBLOCK_IGMP_H:
  194:             return ("igmp");
  195:         case LIBNET_PBLOCK_IPV4_H:
  196:             return ("ipv4");
  197:         case LIBNET_PBLOCK_IPO_H:
  198:             return ("ipo");
  199:         case LIBNET_PBLOCK_IPDATA:
  200:             return ("ipdata");
  201:         case LIBNET_PBLOCK_OSPF_H:
  202:             return ("ospf");
  203:         case LIBNET_PBLOCK_OSPF_HELLO_H:
  204:             return ("ospf_hello");
  205:         case LIBNET_PBLOCK_OSPF_DBD_H:
  206:             return ("ospf_dbd");
  207:         case LIBNET_PBLOCK_OSPF_LSR_H:
  208:             return ("ospf_lsr");
  209:         case LIBNET_PBLOCK_OSPF_LSU_H:
  210:             return ("ospf_lsu");
  211:         case LIBNET_PBLOCK_OSPF_LSA_H:
  212:             return ("ospf_lsa");
  213:         case LIBNET_PBLOCK_OSPF_AUTH_H:
  214:             return ("ospf_auth");
  215:         case LIBNET_PBLOCK_OSPF_CKSUM:
  216:             return ("ospf_cksum");
  217:         case LIBNET_PBLOCK_LS_RTR_H:
  218:             return ("ls_rtr");
  219:         case LIBNET_PBLOCK_LS_NET_H:
  220:             return ("ls_net");
  221:         case LIBNET_PBLOCK_LS_SUM_H:
  222:             return ("ls_sum");
  223:         case LIBNET_PBLOCK_LS_AS_EXT_H:
  224:             return ("ls_as_ext");
  225:         case LIBNET_PBLOCK_NTP_H:
  226:             return ("ntp");
  227:         case LIBNET_PBLOCK_RIP_H:
  228:             return ("rip");
  229:         case LIBNET_PBLOCK_TCP_H:
  230:             return ("tcp");
  231:         case LIBNET_PBLOCK_TCPO_H:
  232:             return ("tcpo");
  233:         case LIBNET_PBLOCK_TCPDATA:
  234:             return ("tcpdata");
  235:         case LIBNET_PBLOCK_UDP_H:
  236:             return ("udp");
  237:         case LIBNET_PBLOCK_VRRP_H:
  238:             return ("vrrp");
  239:         case LIBNET_PBLOCK_DATA_H:
  240:             return ("data");
  241:         case LIBNET_PBLOCK_CDP_H:
  242:             return ("cdp");
  243:         case LIBNET_PBLOCK_IPSEC_ESP_HDR_H:
  244:             return ("ipsec_esp_hdr");
  245:         case LIBNET_PBLOCK_IPSEC_ESP_FTR_H:
  246:             return ("ipsec_esp_ftr");
  247:         case LIBNET_PBLOCK_IPSEC_AH_H:
  248:             return ("ipsec_ah");
  249:         case LIBNET_PBLOCK_802_1Q_H:
  250:             return ("802_1q");
  251:         case LIBNET_PBLOCK_802_2_H:
  252:             return ("802_2");
  253:         case LIBNET_PBLOCK_802_2SNAP_H:
  254:             return ("802_2snap");
  255:         case LIBNET_PBLOCK_802_3_H:
  256:             return ("802_3");
  257:         case LIBNET_PBLOCK_STP_CONF_H:
  258:             return ("stp_conf");
  259:         case LIBNET_PBLOCK_STP_TCN_H:
  260:             return ("stp_tcn");
  261:         case LIBNET_PBLOCK_ISL_H:
  262:             return ("isl");
  263:         case LIBNET_PBLOCK_IPV6_H:
  264:             return ("ipv6");
  265:         case LIBNET_PBLOCK_802_1X_H:
  266:             return ("802_1x");
  267:         case LIBNET_PBLOCK_RPC_CALL_H:
  268:             return ("rpc_call");
  269:         case LIBNET_PBLOCK_MPLS_H:
  270:             return ("mpls");
  271:         case LIBNET_PBLOCK_FDDI_H:
  272:             return ("fddi");
  273:         case LIBNET_PBLOCK_TOKEN_RING_H:
  274:             return ("token_ring");
  275:         case LIBNET_PBLOCK_BGP4_HEADER_H:
  276:             return ("bgp4_header");
  277:         case LIBNET_PBLOCK_BGP4_OPEN_H:
  278:             return ("bgp4_open");
  279:         case LIBNET_PBLOCK_BGP4_UPDATE_H:
  280:             return ("bgp4_update");
  281:         case LIBNET_PBLOCK_BGP4_NOTIFICATION_H:
  282:             return ("bgp4_notification");
  283:         case LIBNET_PBLOCK_GRE_H:
  284:             return ("gre");
  285:         case LIBNET_PBLOCK_GRE_SRE_H:
  286:             return ("gre_sre");
  287:         case LIBNET_PBLOCK_IPV6_FRAG_H:
  288:             return ("ipv6_frag");
  289:         case LIBNET_PBLOCK_IPV6_ROUTING_H:
  290:             return ("ipv6_routing");
  291:         case LIBNET_PBLOCK_IPV6_DESTOPTS_H:
  292:             return ("ipv6_destopts");
  293:         case LIBNET_PBLOCK_IPV6_HBHOPTS_H:
  294:             return ("ipv6_hbhopts");
  295:         case LIBNET_PBLOCK_SEBEK_H:
  296:             return ("sebek");
  297:         case LIBNET_PBLOCK_HSRP_H:
  298:             return ("hsrp");
  299:         case LIBNET_PBLOCK_ICMPV6_H:
  300:             return ("icmpv6");
  301:         case LIBNET_PBLOCK_ICMPV6_ECHO_H:
  302:             return ("icmpv6_echo");
  303:         case LIBNET_PBLOCK_ICMPV6_UNREACH_H:
  304:             return ("icmpv6_unreach");
  305:         case LIBNET_PBLOCK_ICMPV6_NDP_NSOL_H:
  306:             return ("icmpv6_ndp_nsol");
  307:         case LIBNET_PBLOCK_ICMPV6_NDP_NADV_H:
  308:             return ("icmpv6_ndp_nadv");
  309:         case LIBNET_PBLOCK_ICMPV6_NDP_OPT_H:
  310:             return ("icmpv6_ndp_opt");
  311:     }
  312:     return ("unrecognized pblock");
  313: }
  314: 
  315: /**
  316:  * Local Variables:
  317:  *  indent-tabs-mode: nil
  318:  *  c-file-style: "stroustrup"
  319:  * End:
  320:  */

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