]> The Tcpdump Group git mirrors - tcpdump/blob - print-ipnet.c
Fix build on FreeBSD.
[tcpdump] / print-ipnet.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <tcpdump-stdinc.h>
6
7 #include <stdio.h>
8 #include <pcap.h>
9
10 #include "netdissect.h"
11 #include "interface.h"
12 #include "addrtoname.h"
13
14 typedef struct ipnet_hdr {
15 uint8_t iph_version;
16 uint8_t iph_family;
17 uint16_t iph_htype;
18 uint32_t iph_pktlen;
19 uint32_t iph_ifindex;
20 uint32_t iph_grifindex;
21 uint32_t iph_zsrc;
22 uint32_t iph_zdst;
23 } ipnet_hdr_t;
24
25 #define IPH_AF_INET 2 /* Matches Solaris's AF_INET */
26 #define IPH_AF_INET6 26 /* Matches Solaris's AF_INET6 */
27
28 #ifdef DLT_IPNET
29
30 static const struct tok ipnet_values[] = {
31 { IPH_AF_INET, "IPv4" },
32 { IPH_AF_INET6, "IPv6" },
33 { 0, NULL }
34 };
35
36 static inline void
37 ipnet_hdr_print(struct netdissect_options *ndo, const u_char *bp, u_int length)
38 {
39 const ipnet_hdr_t *hdr;
40 hdr = (const ipnet_hdr_t *)bp;
41
42 ND_PRINT((ndo, "%d > %d", hdr->iph_zsrc, hdr->iph_zdst));
43
44 if (!ndo->ndo_qflag) {
45 ND_PRINT((ndo,", family %s (%d)",
46 tok2str(ipnet_values, "Unknown",
47 hdr->iph_family),
48 hdr->iph_family));
49 } else {
50 ND_PRINT((ndo,", %s",
51 tok2str(ipnet_values,
52 "Unknown Ethertype (0x%04x)",
53 hdr->iph_family)));
54 }
55
56 ND_PRINT((ndo, ", length %u: ", length));
57 }
58
59 static void
60 ipnet_print(struct netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
61 {
62 ipnet_hdr_t *hdr;
63
64 if (caplen < sizeof(ipnet_hdr_t)) {
65 ND_PRINT((ndo, "[|ipnet]"));
66 return;
67 }
68
69 if (ndo->ndo_eflag)
70 ipnet_hdr_print(ndo, p, length);
71
72 length -= sizeof(ipnet_hdr_t);
73 caplen -= sizeof(ipnet_hdr_t);
74 hdr = (ipnet_hdr_t *)p;
75 p += sizeof(ipnet_hdr_t);
76
77 switch (hdr->iph_family) {
78
79 case IPH_AF_INET:
80 ip_print(ndo, p, length);
81 break;
82
83 #ifdef INET6
84 case IPH_AF_INET6:
85 ip6_print(ndo, p, length);
86 break;
87 #endif /*INET6*/
88
89 default:
90 if (!ndo->ndo_eflag)
91 ipnet_hdr_print(ndo, (u_char *)hdr,
92 length + sizeof(ipnet_hdr_t));
93
94 if (!ndo->ndo_suppress_default_print)
95 ndo->ndo_default_print(ndo, p, caplen);
96 break;
97 }
98 }
99
100 /*
101 * This is the top level routine of the printer. 'p' points
102 * to the ether header of the packet, 'h->ts' is the timestamp,
103 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
104 * is the number of bytes actually captured.
105 */
106 u_int
107 ipnet_if_print(struct netdissect_options *ndo,
108 const struct pcap_pkthdr *h, const u_char *p)
109 {
110 ipnet_print(ndo, p, h->len, h->caplen);
111
112 return (sizeof(ipnet_hdr_t));
113 }
114
115 /*
116 * Local Variables:
117 * c-style: whitesmith
118 * c-basic-offset: 8
119 * End:
120 */
121
122 #endif /* DLT_IPNET */