xref: /freebsd/contrib/tcpdump/print-null.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1991, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  */
23 
24 #ifndef lint
25 static const char rcsid[] =
26     "@(#) $Header: /tcpdump/master/tcpdump/print-null.c,v 1.30 1999/12/22 06:27:21 itojun Exp $ (LBL)";
27 #endif
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/file.h>
37 #include <sys/ioctl.h>
38 
39 #if __STDC__
40 struct mbuf;
41 struct rtentry;
42 #endif
43 #include <net/if.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <net/ethernet.h>
49 #include <netinet/ip_var.h>
50 #include <netinet/udp.h>
51 #include <netinet/udp_var.h>
52 #include <netinet/tcp.h>
53 
54 #include <pcap.h>
55 #include <stdio.h>
56 #include <string.h>
57 
58 #ifdef INET6
59 #include <netinet/ip6.h>
60 #endif
61 
62 #include "interface.h"
63 #include "addrtoname.h"
64 
65 #ifndef AF_NS
66 #define AF_NS		6		/* XEROX NS protocols */
67 #endif
68 
69 /*
70  * The DLT_NULL packet header is 4 bytes long. It contains a network
71  * order 32 bit integer that specifies the family, e.g. AF_INET
72  */
73 #define	NULL_HDRLEN 4
74 
75 static void
76 null_print(const u_char *p, const struct ip *ip, u_int length)
77 {
78 	u_int family;
79 
80 	memcpy((char *)&family, (char *)p, sizeof(family));
81 
82 	if (nflag) {
83 		/* XXX just dump the header */
84 		return;
85 	}
86 	switch (family) {
87 
88 	case AF_INET:
89 		printf("ip: ");
90 		break;
91 
92 #ifdef INET6
93 	case AF_INET6:
94 		printf("ip6: ");
95 		break;
96 #endif
97 
98 	case AF_NS:
99 		printf("ns: ");
100 		break;
101 
102 	default:
103 		printf("AF %d: ", family);
104 		break;
105 	}
106 }
107 
108 void
109 null_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
110 {
111 	u_int length = h->len;
112 	u_int caplen = h->caplen;
113 	const struct ip *ip;
114 
115 	ts_print(&h->ts);
116 
117 	/*
118 	 * Some printers want to get back at the link level addresses,
119 	 * and/or check that they're not walking off the end of the packet.
120 	 * Rather than pass them all the way down, we set these globals.
121 	 */
122 	packetp = p;
123 	snapend = p + caplen;
124 
125 	length -= NULL_HDRLEN;
126 
127 	ip = (struct ip *)(p + NULL_HDRLEN);
128 
129 	if (eflag)
130 		null_print(p, ip, length);
131 
132 	switch (ip->ip_v) {
133 	case 4:
134 		ip_print((const u_char *)ip, length);
135 		break;
136 #ifdef INET6
137 	case 6:
138 		ip6_print((const u_char *)ip, length);
139 		break;
140 #endif /* INET6 */
141 	default:
142 		printf("ip v%d", ip->ip_v);
143 		break;
144 	}
145 
146 	if (xflag)
147 		default_print((const u_char *)ip, caplen - NULL_HDRLEN);
148 	putchar('\n');
149 }
150 
151