print-null.c revision 267654
1285SN/A/*
2462SN/A * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
3285SN/A *	The Regents of the University of California.  All rights reserved.
4285SN/A *
5285SN/A * Redistribution and use in source and binary forms, with or without
6285SN/A * modification, are permitted provided that: (1) source code distributions
7285SN/A * retain the above copyright notice and this paragraph in its entirety, (2)
8285SN/A * distributions including binary code include the above copyright notice and
9285SN/A * this paragraph in its entirety in the documentation or other materials
10285SN/A * provided with the distribution, and (3) all advertising materials mentioning
11285SN/A * features or use of this software display the following acknowledgement:
12285SN/A * ``This product includes software developed by the University of California,
13285SN/A * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14285SN/A * the University nor the names of its contributors may be used to endorse
15285SN/A * or promote products derived from this software without specific prior
16285SN/A * written permission.
17285SN/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18285SN/A * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19285SN/A * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20285SN/A *
21285SN/A * $FreeBSD: releng/9.3/contrib/tcpdump/print-null.c 236192 2012-05-28 19:13:21Z delphij $
22285SN/A */
23285SN/A
24285SN/A#ifndef lint
25285SN/Astatic const char rcsid[] _U_ =
26285SN/A    "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.57 2006-03-23 14:58:44 hannes Exp $ (LBL)";
27285SN/A#endif
28285SN/A
29285SN/A#ifdef HAVE_CONFIG_H
30285SN/A#include "config.h"
31285SN/A#endif
32285SN/A
33285SN/A#include <tcpdump-stdinc.h>
34285SN/A
35285SN/A#include <pcap.h>
36285SN/A#include <stdio.h>
37285SN/A#include <string.h>
38285SN/A
39285SN/A#include "interface.h"
40285SN/A#include "addrtoname.h"
41285SN/A
42285SN/A#include "ip.h"
43285SN/A#ifdef INET6
44285SN/A#include "ip6.h"
45285SN/A#endif
46285SN/A#include "af.h"
47285SN/A
48285SN/A/*
49285SN/A * The DLT_NULL packet header is 4 bytes long. It contains a host-byte-order
50285SN/A * 32-bit integer that specifies the family, e.g. AF_INET.
51285SN/A *
52285SN/A * Note here that "host" refers to the host on which the packets were
53285SN/A * captured; that isn't necessarily *this* host.
54285SN/A *
55285SN/A * The OpenBSD DLT_LOOP packet header is the same, except that the integer
56285SN/A * is in network byte order.
57285SN/A */
58285SN/A#define	NULL_HDRLEN 4
59285SN/A
60285SN/A/*
61285SN/A * Byte-swap a 32-bit number.
62285SN/A * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
63285SN/A * big-endian platforms.)
64285SN/A */
65285SN/A#define	SWAPLONG(y) \
66285SN/A((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
67285SN/A
68285SN/Astatic inline void
69285SN/Anull_hdr_print(u_int family, u_int length)
70285SN/A{
71285SN/A	if (!qflag) {
72285SN/A		(void)printf("AF %s (%u)",
73285SN/A			tok2str(bsd_af_values,"Unknown",family),family);
74285SN/A	} else {
75285SN/A		(void)printf("%s",
76285SN/A			tok2str(bsd_af_values,"Unknown AF %u",family));
77285SN/A	}
78285SN/A
79285SN/A	(void)printf(", length %u: ", length);
80285SN/A}
81285SN/A
82285SN/A/*
83285SN/A * This is the top level routine of the printer.  'p' points
84285SN/A * to the ether header of the packet, 'h->ts' is the timestamp,
85285SN/A * 'h->len' is the length of the packet off the wire, and 'h->caplen'
86285SN/A * is the number of bytes actually captured.
87285SN/A */
88285SN/Au_int
89285SN/Anull_if_print(const struct pcap_pkthdr *h, const u_char *p)
90285SN/A{
91285SN/A	u_int length = h->len;
92285SN/A	u_int caplen = h->caplen;
93285SN/A	u_int family;
94285SN/A
95285SN/A	if (caplen < NULL_HDRLEN) {
96285SN/A		printf("[|null]");
97285SN/A		return (NULL_HDRLEN);
98285SN/A	}
99285SN/A
100285SN/A	memcpy((char *)&family, (char *)p, sizeof(family));
101285SN/A
102	/*
103	 * This isn't necessarily in our host byte order; if this is
104	 * a DLT_LOOP capture, it's in network byte order, and if
105	 * this is a DLT_NULL capture from a machine with the opposite
106	 * byte-order, it's in the opposite byte order from ours.
107	 *
108	 * If the upper 16 bits aren't all zero, assume it's byte-swapped.
109	 */
110	if ((family & 0xFFFF0000) != 0)
111		family = SWAPLONG(family);
112
113	if (eflag)
114		null_hdr_print(family, length);
115
116	length -= NULL_HDRLEN;
117	caplen -= NULL_HDRLEN;
118	p += NULL_HDRLEN;
119
120	switch (family) {
121
122	case BSD_AFNUM_INET:
123		ip_print(gndo, p, length);
124		break;
125
126#ifdef INET6
127	case BSD_AFNUM_INET6_BSD:
128	case BSD_AFNUM_INET6_FREEBSD:
129	case BSD_AFNUM_INET6_DARWIN:
130		ip6_print(gndo, p, length);
131		break;
132#endif
133
134	case BSD_AFNUM_ISO:
135		isoclns_print(p, length, caplen);
136		break;
137
138	case BSD_AFNUM_APPLETALK:
139		atalk_print(p, length);
140		break;
141
142	case BSD_AFNUM_IPX:
143		ipx_print(p, length);
144		break;
145
146	default:
147		/* unknown AF_ value */
148		if (!eflag)
149			null_hdr_print(family, length + NULL_HDRLEN);
150		if (!suppress_default_print)
151			default_print(p, caplen);
152	}
153
154	return (NULL_HDRLEN);
155}
156
157/*
158 * Local Variables:
159 * c-style: whitesmith
160 * c-basic-offset: 8
161 * End:
162 */
163