xref: /freebsd/contrib/tcpdump/print-null.c (revision a5779b6e02d0404232959eede5a5d3a5c699adaf)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20a88113a8SBill Fenner  *
21a88113a8SBill Fenner  * $FreeBSD$
224edb46e9SPaul Traina  */
234edb46e9SPaul Traina 
244edb46e9SPaul Traina #ifndef lint
25cc391cceSBruce M Simpson static const char rcsid[] _U_ =
26a5779b6eSRui Paulo     "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.57 2006-03-23 14:58:44 hannes Exp $ (LBL)";
27a88113a8SBill Fenner #endif
28a88113a8SBill Fenner 
29a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
30a88113a8SBill Fenner #include "config.h"
314edb46e9SPaul Traina #endif
324edb46e9SPaul Traina 
33cc391cceSBruce M Simpson #include <tcpdump-stdinc.h>
344edb46e9SPaul Traina 
352ebf6c05SBill Fenner #include <pcap.h>
364edb46e9SPaul Traina #include <stdio.h>
374edb46e9SPaul Traina #include <string.h>
384edb46e9SPaul Traina 
392ebf6c05SBill Fenner #include "interface.h"
40699fc314SBill Fenner #include "addrtoname.h"
414edb46e9SPaul Traina 
42943ee2b1SBill Fenner #include "ip.h"
43943ee2b1SBill Fenner #ifdef INET6
44943ee2b1SBill Fenner #include "ip6.h"
45943ee2b1SBill Fenner #endif
46a5779b6eSRui Paulo #include "af.h"
47943ee2b1SBill Fenner 
48699fc314SBill Fenner /*
49943ee2b1SBill Fenner  * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
50943ee2b1SBill Fenner  * 32-bit integer that specifies the family, e.g. AF_INET.
51943ee2b1SBill Fenner  *
52943ee2b1SBill Fenner  * Note here that "host" refers to the host on which the packets were
53943ee2b1SBill Fenner  * captured; that isn't necessarily *this* host.
54943ee2b1SBill Fenner  *
55943ee2b1SBill Fenner  * The OpenBSD DLT_LOOP packet header is the same, except that the integer
56943ee2b1SBill Fenner  * is in network byte order.
57699fc314SBill Fenner  */
58699fc314SBill Fenner #define	NULL_HDRLEN 4
59699fc314SBill Fenner 
60943ee2b1SBill Fenner /*
61943ee2b1SBill Fenner  * Byte-swap a 32-bit number.
62943ee2b1SBill Fenner  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
63943ee2b1SBill Fenner  * big-endian platforms.)
64943ee2b1SBill Fenner  */
65943ee2b1SBill Fenner #define	SWAPLONG(y) \
66943ee2b1SBill Fenner ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
674edb46e9SPaul Traina 
6829292c17SSam Leffler static inline void
6929292c17SSam Leffler null_hdr_print(u_int family, u_int length)
7029292c17SSam Leffler {
7129292c17SSam Leffler 	if (!qflag) {
7229292c17SSam Leffler 		(void)printf("AF %s (%u)",
7329292c17SSam Leffler 			tok2str(bsd_af_values,"Unknown",family),family);
7429292c17SSam Leffler 	} else {
7529292c17SSam Leffler 		(void)printf("%s",
7629292c17SSam Leffler 			tok2str(bsd_af_values,"Unknown AF %u",family));
7729292c17SSam Leffler 	}
7829292c17SSam Leffler 
7929292c17SSam Leffler 	(void)printf(", length %u: ", length);
8029292c17SSam Leffler }
8129292c17SSam Leffler 
82cc391cceSBruce M Simpson /*
83cc391cceSBruce M Simpson  * This is the top level routine of the printer.  'p' points
84cc391cceSBruce M Simpson  * to the ether header of the packet, 'h->ts' is the timestamp,
85c1ad1296SSam Leffler  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
86cc391cceSBruce M Simpson  * is the number of bytes actually captured.
87cc391cceSBruce M Simpson  */
88cc391cceSBruce M Simpson u_int
89cc391cceSBruce M Simpson null_if_print(const struct pcap_pkthdr *h, const u_char *p)
904edb46e9SPaul Traina {
914edb46e9SPaul Traina 	u_int length = h->len;
924edb46e9SPaul Traina 	u_int caplen = h->caplen;
93943ee2b1SBill Fenner 	u_int family;
944edb46e9SPaul Traina 
95cc391cceSBruce M Simpson 	if (caplen < NULL_HDRLEN) {
96cc391cceSBruce M Simpson 		printf("[|null]");
97cc391cceSBruce M Simpson 		return (NULL_HDRLEN);
98cc391cceSBruce M Simpson 	}
994edb46e9SPaul Traina 
100943ee2b1SBill Fenner 	memcpy((char *)&family, (char *)p, sizeof(family));
101943ee2b1SBill Fenner 
102943ee2b1SBill Fenner 	/*
103943ee2b1SBill Fenner 	 * This isn't necessarily in our host byte order; if this is
104943ee2b1SBill Fenner 	 * a DLT_LOOP capture, it's in network byte order, and if
105943ee2b1SBill Fenner 	 * this is a DLT_NULL capture from a machine with the opposite
106943ee2b1SBill Fenner 	 * byte-order, it's in the opposite byte order from ours.
107943ee2b1SBill Fenner 	 *
108943ee2b1SBill Fenner 	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
109943ee2b1SBill Fenner 	 */
110943ee2b1SBill Fenner 	if ((family & 0xFFFF0000) != 0)
111943ee2b1SBill Fenner 		family = SWAPLONG(family);
112943ee2b1SBill Fenner 
11329292c17SSam Leffler 	if (eflag)
11429292c17SSam Leffler 		null_hdr_print(family, length);
11529292c17SSam Leffler 
1164edb46e9SPaul Traina 	length -= NULL_HDRLEN;
117cc391cceSBruce M Simpson 	caplen -= NULL_HDRLEN;
118cc391cceSBruce M Simpson 	p += NULL_HDRLEN;
1194edb46e9SPaul Traina 
120cc391cceSBruce M Simpson 	switch (family) {
121cc391cceSBruce M Simpson 
122abf25193SMax Laier 	case BSD_AFNUM_INET:
123c1ad1296SSam Leffler 		ip_print(gndo, p, length);
124a88113a8SBill Fenner 		break;
125cc391cceSBruce M Simpson 
126a88113a8SBill Fenner #ifdef INET6
127abf25193SMax Laier 	case BSD_AFNUM_INET6_BSD:
128abf25193SMax Laier 	case BSD_AFNUM_INET6_FREEBSD:
129abf25193SMax Laier 	case BSD_AFNUM_INET6_DARWIN:
130cc391cceSBruce M Simpson 		ip6_print(p, length);
131a88113a8SBill Fenner 		break;
132cc391cceSBruce M Simpson #endif
133cc391cceSBruce M Simpson 
134abf25193SMax Laier 	case BSD_AFNUM_ISO:
135cc391cceSBruce M Simpson 		isoclns_print(p, length, caplen);
136cc391cceSBruce M Simpson 		break;
137cc391cceSBruce M Simpson 
138abf25193SMax Laier 	case BSD_AFNUM_APPLETALK:
139cc391cceSBruce M Simpson 		atalk_print(p, length);
140cc391cceSBruce M Simpson 		break;
141cc391cceSBruce M Simpson 
142abf25193SMax Laier 	case BSD_AFNUM_IPX:
143cc391cceSBruce M Simpson 		ipx_print(p, length);
144cc391cceSBruce M Simpson 		break;
145cc391cceSBruce M Simpson 
146a88113a8SBill Fenner 	default:
147cc391cceSBruce M Simpson 		/* unknown AF_ value */
148cc391cceSBruce M Simpson 		if (!eflag)
14929292c17SSam Leffler 			null_hdr_print(family, length + NULL_HDRLEN);
15017cb103cSSam Leffler 		if (!suppress_default_print)
151cc391cceSBruce M Simpson 			default_print(p, caplen);
152a88113a8SBill Fenner 	}
1534edb46e9SPaul Traina 
154cc391cceSBruce M Simpson 	return (NULL_HDRLEN);
1554edb46e9SPaul Traina }
1564edb46e9SPaul Traina 
157c1ad1296SSam Leffler /*
158c1ad1296SSam Leffler  * Local Variables:
159c1ad1296SSam Leffler  * c-style: whitesmith
160c1ad1296SSam Leffler  * c-basic-offset: 8
161c1ad1296SSam Leffler  * End:
162c1ad1296SSam Leffler  */
163