"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "trafshow-3.1/interfaces.c" of archive trafshow-3.1.tgz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2  *	Copyright (c) 1993-1997 JSC Rinet, Novosibirsk, Russia
    3  *
    4  * Redistribution and use in source forms, with and without modification,
    5  * are permitted provided that this entire comment appears intact.
    6  * Redistribution in binary form may occur without any restrictions.
    7  *
    8  * THIS SOFTWARE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
    9  */
   10 
   11 /* interfaces.h -- network interface data-link level routines */
   12 
   13 #ifdef	HAVE_CONFIG_H
   14 #include <config.h>
   15 #endif
   16 
   17 #include <sys/param.h>
   18 #include <sys/types.h>
   19 #ifdef	HAVE_SYS_MBUF_H
   20 #include <sys/mbuf.h>
   21 #endif
   22 #include <netinet/in.h>
   23 #include <netinet/in_systm.h>
   24 #include <netinet/ip.h>
   25 #ifdef	HAVE_NET_SLIP_H
   26 #include <net/slip.h>
   27 #include <net/slcompress.h>
   28 #endif
   29 
   30 #include <pcap.h>
   31 #include "trafshow.h"
   32 
   33 u_char *snapend;
   34 
   35 /* Link-level header length */
   36 #ifndef	ETHER_HDRLEN
   37 #define	ETHER_HDRLEN	sizeof(struct ether_header)
   38 #endif
   39 
   40 #ifndef	SLIP_HDRLEN
   41 #ifdef	SLC_BPFHDR
   42 #define	SLIP_HDRLEN	SLC_BPFHDR	/* if _BSDI_VERSION >= 199510 */
   43 #else
   44 #define	SLIP_HDRLEN	16	/* bpf slip header length */
   45 #endif
   46 #endif
   47 
   48 #ifndef	PPP_HDRLEN
   49 #ifdef	SLC_BPFHDR
   50 #define	PPP_HDRLEN	SLC_BPFHDR	/* if _BSDI_VERSION >= 199510 */
   51 #else
   52 #define	PPP_HDRLEN	4	/* sizeof(struct ppp_header) */
   53 #endif
   54 #endif
   55 
   56 #ifdef	DLT_C_HDLC
   57 #ifndef	CHDLC_HDRLEN
   58 #define	CHDLC_HDRLEN	4	/* sizeof(struct cisco_hdr) */
   59 #endif
   60 #endif
   61 
   62 #ifndef	NULL_HDRLEN
   63 #define	NULL_HDRLEN	4	/* loopback header length */
   64 #endif
   65 
   66 /*
   67  * This is the interface depended routines for ethernet, slip, ppp and lo.
   68  *	'p' is the points to the packet,
   69  *	'length' is the length of the packet,
   70  *	'caplen' is the number of bytes actually captured.
   71  */
   72 void
   73 if_ether(user, h, p)
   74 	char *user;
   75 	struct pcap_pkthdr *h;
   76 	register u_char *p;
   77 {
   78 	u_int caplen = h->caplen;
   79 	u_int length = h->len;
   80 
   81 	if (caplen < ETHER_HDRLEN) return;
   82 
   83 	snapend = p + caplen;
   84 	if (eflag ||
   85 	    ntohs(((struct ether_header *)p)->ether_type) == ETHERTYPE_IP)
   86 		display(p, p + ETHER_HDRLEN, length - ETHER_HDRLEN);
   87 }
   88 
   89 void
   90 if_slip(user, h, p)
   91 	char *user;
   92 	struct pcap_pkthdr *h;
   93 	register u_char *p;
   94 {
   95 	u_int caplen = h->caplen;
   96 	u_int length = h->len;
   97 
   98 	if (caplen < SLIP_HDRLEN) return;
   99 
  100 	snapend = p + caplen;
  101 	display(NULL, p + SLIP_HDRLEN, length - SLIP_HDRLEN);
  102 }
  103 
  104 void
  105 if_ppp(user, h, p)
  106 	char *user;
  107 	struct pcap_pkthdr *h;
  108 	register u_char *p;
  109 {
  110 	u_int caplen = h->caplen;
  111 	u_int length = h->len;
  112 	register hdrlen = 0;
  113 	u_short type;
  114 	u_char *packetp;
  115 
  116 	if (caplen < PPP_HDRLEN) return;
  117 
  118 	packetp = p;
  119 	snapend = p + caplen;
  120 
  121 #ifdef	SLC_BPFHDRLEN
  122 	p += SLC_BPFHDRLEN;	/* pointer to link level header */
  123 #endif
  124 	/* PPP address and PPP control fields may be present (-acfc) */
  125 	if (p[0] == 0xff && p[1] == 0x03) {
  126 		p += 2;
  127 		hdrlen += 2;
  128 	}
  129 	/* Retrive the protocol type */
  130 	if (*p & 01) {	/* Compressed protocol field (pfc) */
  131 		type = *p++;
  132 		hdrlen++;
  133 	} else {	/* Un-compressed protocol field (-pfc) */
  134 		type = ntohs(*(u_short *)p);
  135 		p += 2;
  136 		hdrlen += 2;
  137 	}
  138 	if (type == 0x21) {	/* IP protocol */
  139 #ifdef	SLC_BPFHDR
  140 		p = packetp + SLC_BPFHDR;	/* skip bpf pseudo header */
  141 		hdrlen = SLC_BPFHDR;
  142 #endif
  143 		display(NULL, p, length - hdrlen);
  144 	}
  145 }
  146 
  147 #ifdef	DLT_C_HDLC
  148 void
  149 if_chdlc(user, h, p)
  150 	char *user;
  151 	struct pcap_pkthdr *h;
  152 	register u_char *p;
  153 {
  154 	u_int caplen = h->caplen;
  155 	u_int length = h->len;
  156 
  157 	if (caplen < CHDLC_HDRLEN) return;
  158 
  159 	snapend = p + caplen;
  160 	if (ntohs(*(u_short *)(p + 2)) == 0x0800) /* IP protocol */
  161 		display(NULL, p + CHDLC_HDRLEN, length - CHDLC_HDRLEN);
  162 }
  163 #endif
  164 
  165 #ifdef	DLT_RAW
  166 void
  167 if_rawip(user, h, p)
  168 	char *user;
  169 	struct pcap_pkthdr *h;
  170 	register u_char *p;
  171 {
  172 	u_int caplen = h->caplen;
  173 	u_int length = h->len;
  174 
  175 	snapend = p + caplen;
  176 	display(NULL, p, length);
  177 }
  178 #endif
  179 
  180 void
  181 if_null(user, h, p)
  182 	char *user;
  183 	struct pcap_pkthdr *h;
  184 	register u_char *p;
  185 {
  186 	u_int caplen = h->caplen;
  187 	u_int length = h->len;
  188   	u_int family;
  189 
  190 	memcpy(&family, p, sizeof(family));
  191 	snapend = p + caplen;
  192 	if (family == AF_INET)
  193 		display(NULL, p + NULL_HDRLEN, length - NULL_HDRLEN);
  194 }
  195 
  196 /*
  197  * hardware depended funtions table.
  198  */
  199 static struct if_func {
  200 	void (*f)();
  201 	int type;
  202 } if_funcs[] = {
  203 	{	if_ether,	DLT_EN10MB	},	/* Ethernet */
  204 #ifdef	DLT_IEEE802
  205 	{	if_ether,	DLT_IEEE802	},	/* IEEE 802 */
  206 #endif
  207 	{	if_slip,	DLT_SLIP	},	/* SLIP */
  208 #ifdef	DLT_SLIP_BSDOS
  209 	{	if_slip,	DLT_SLIP_BSDOS	}, /* libpcap stupid fake */
  210 #endif
  211 	{	if_ppp,		DLT_PPP		},	/* PPP */
  212 #ifdef	DLT_PPP_BSDOS
  213 	{	if_ppp,		DLT_PPP_BSDOS	}, /* libpcap stupid fake */
  214 #endif
  215 #ifdef	DLT_C_HDLC
  216 	{	if_chdlc,	DLT_C_HDLC	},	/* Cisco HDLC */
  217 #endif
  218 #ifdef	DLT_RAW
  219 	{	if_rawip,	DLT_RAW		},	/* raw IP */
  220 #endif
  221 	{	if_null,	DLT_NULL	},	/* loopback */
  222 	{ NULL, 0 },
  223 };
  224 
  225 /*
  226  * Assign data link type to interface function.
  227  */
  228 pcap_handler
  229 lookup_if(type)
  230 	int type;
  231 {
  232 	struct if_func *p;
  233 
  234 	for (p = if_funcs; p->f != NULL; ++p)
  235 		if (type == p->type) return p->f;
  236 	error(0, "unknown data link type 0x%x", type);
  237 
  238 	/* NOTREACHED */
  239 	return 0;
  240 }