2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
35 #include <netdissect-stdinc.h>
37 #include "netdissect.h"
38 #include "addrtoname.h"
46 static const struct printer printers
[] = {
47 { ether_if_print
, DLT_EN10MB
},
49 { ipnet_if_print
, DLT_IPNET
},
51 #ifdef DLT_IEEE802_15_4
52 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
54 #ifdef DLT_IEEE802_15_4_NOFCS
55 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
58 { ppi_if_print
, DLT_PPI
},
60 #ifdef DLT_NETANALYZER
61 { netanalyzer_if_print
, DLT_NETANALYZER
},
63 #ifdef DLT_NETANALYZER_TRANSPARENT
64 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
66 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
67 { nflog_if_print
, DLT_NFLOG
},
70 { cip_if_print
, DLT_CIP
},
73 { cip_if_print
, DLT_ATM_CLIP
},
76 { ipfc_if_print
, DLT_IP_OVER_FC
},
78 { null_if_print
, DLT_NULL
},
80 { null_if_print
, DLT_LOOP
},
82 #ifdef DLT_APPLE_IP_OVER_IEEE1394
83 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
85 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
86 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
89 { lane_if_print
, DLT_LANE8023
},
91 { arcnet_if_print
, DLT_ARCNET
},
92 #ifdef DLT_ARCNET_LINUX
93 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
95 { raw_if_print
, DLT_RAW
},
97 { raw_if_print
, DLT_IPV4
},
100 { raw_if_print
, DLT_IPV6
},
103 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
104 #endif /* DLT_USB_LINUX */
105 #ifdef DLT_USB_LINUX_MMAPPED
106 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
107 #endif /* DLT_USB_LINUX_MMAPPED */
108 #ifdef DLT_SYMANTEC_FIREWALL
109 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
112 { chdlc_if_print
, DLT_C_HDLC
},
115 { chdlc_if_print
, DLT_HDLC
},
118 { pppoe_if_print
, DLT_PPP_ETHER
},
120 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
121 { pflog_if_print
, DLT_PFLOG
},
123 { token_if_print
, DLT_IEEE802
},
124 { fddi_if_print
, DLT_FDDI
},
126 { sll_if_print
, DLT_LINUX_SLL
},
129 { fr_if_print
, DLT_FR
},
132 { fr_if_print
, DLT_FRELAY
},
135 { mfr_if_print
, DLT_MFR
},
137 { atm_if_print
, DLT_ATM_RFC1483
},
139 { sunatm_if_print
, DLT_SUNATM
},
142 { enc_if_print
, DLT_ENC
},
144 { sl_if_print
, DLT_SLIP
},
145 #ifdef DLT_SLIP_BSDOS
146 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
149 { ltalk_if_print
, DLT_LTALK
},
151 #ifdef DLT_JUNIPER_ATM1
152 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
154 #ifdef DLT_JUNIPER_ATM2
155 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
157 #ifdef DLT_JUNIPER_MFR
158 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
160 #ifdef DLT_JUNIPER_MLFR
161 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
163 #ifdef DLT_JUNIPER_MLPPP
164 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
166 #ifdef DLT_JUNIPER_PPPOE
167 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
169 #ifdef DLT_JUNIPER_PPPOE_ATM
170 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
172 #ifdef DLT_JUNIPER_GGSN
173 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
175 #ifdef DLT_JUNIPER_ES
176 { juniper_es_if_print
, DLT_JUNIPER_ES
},
178 #ifdef DLT_JUNIPER_MONITOR
179 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
181 #ifdef DLT_JUNIPER_SERVICES
182 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
184 #ifdef DLT_JUNIPER_ETHER
185 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
187 #ifdef DLT_JUNIPER_PPP
188 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
190 #ifdef DLT_JUNIPER_FRELAY
191 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
193 #ifdef DLT_JUNIPER_CHDLC
194 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
197 { pktap_if_print
, DLT_PKTAP
},
199 #ifdef DLT_IEEE802_11_RADIO
200 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
202 #ifdef DLT_IEEE802_11
203 { ieee802_11_if_print
, DLT_IEEE802_11
},
205 #ifdef DLT_IEEE802_11_RADIO_AVS
206 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
208 #ifdef DLT_PRISM_HEADER
209 { prism_if_print
, DLT_PRISM_HEADER
},
211 { ppp_if_print
, DLT_PPP
},
212 #ifdef DLT_PPP_WITHDIRECTION
213 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
216 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
218 #ifdef DLT_PPP_SERIAL
219 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
224 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
227 static void NORETURN
ndo_error(netdissect_options
*ndo
,
228 FORMAT_STRING(const char *fmt
), ...)
230 static void ndo_warning(netdissect_options
*ndo
,
231 FORMAT_STRING(const char *fmt
), ...)
234 static int ndo_printf(netdissect_options
*ndo
,
235 FORMAT_STRING(const char *fmt
), ...)
239 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
,
240 uint32_t timezone_offset
)
243 thiszone
= timezone_offset
;
244 init_addrtoname(ndo
, localnet
, mask
);
249 lookup_printer(int type
)
251 const struct printer
*p
;
253 for (p
= printers
; p
->f
; ++p
)
257 #if defined(DLT_USER2) && defined(DLT_PKTAP)
259 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
262 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
263 * based OSes or the same value as LINKTYPE_PKTAP as it is on
264 * other OSes, to LINKTYPE_PKTAP, so files written with
265 * this version of libpcap for a DLT_PKTAP capture have a link-
266 * layer header type of LINKTYPE_PKTAP.
268 * However, files written on OS X Mavericks for a DLT_PKTAP
269 * capture have a link-layer header type of LINKTYPE_USER2.
270 * If we don't have a printer for DLT_USER2, and type is
271 * DLT_USER2, we look up the printer for DLT_PKTAP and use
274 if (type
== DLT_USER2
) {
275 for (p
= printers
; p
->f
; ++p
)
276 if (DLT_PKTAP
== p
->type
)
286 has_printer(int type
)
288 return (lookup_printer(type
) != NULL
);
292 get_if_printer(netdissect_options
*ndo
, int type
)
297 printer
= lookup_printer(type
);
298 if (printer
== NULL
) {
299 dltname
= pcap_datalink_val_to_name(type
);
301 (*ndo
->ndo_error
)(ndo
,
302 "packet printing is not supported for link type %s: use -w",
305 (*ndo
->ndo_error
)(ndo
,
306 "packet printing is not supported for link type %d: use -w", type
);
312 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
313 const u_char
*sp
, u_int packets_captured
)
317 if(ndo
->ndo_packet_number
)
318 ND_PRINT("%5u ", packets_captured
);
320 ts_print(ndo
, &h
->ts
);
323 * Printers must check that they're not walking off the end of
325 * Rather than pass it all the way down, we set this member
326 * of the netdissect_options structure.
328 ndo
->ndo_snapend
= sp
+ h
->caplen
;
330 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
333 * Restore the original snapend, as a printer might have
336 ndo
->ndo_snapend
= sp
+ h
->caplen
;
337 if (ndo
->ndo_Xflag
) {
339 * Print the raw packet data in hex and ASCII.
341 if (ndo
->ndo_Xflag
> 1) {
343 * Include the link-layer header.
345 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
348 * Don't include the link-layer header - and if
349 * we have nothing past the link-layer header,
352 if (h
->caplen
> hdrlen
)
353 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
356 } else if (ndo
->ndo_xflag
) {
358 * Print the raw packet data in hex.
360 if (ndo
->ndo_xflag
> 1) {
362 * Include the link-layer header.
364 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
367 * Don't include the link-layer header - and if
368 * we have nothing past the link-layer header,
371 if (h
->caplen
> hdrlen
)
372 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
375 } else if (ndo
->ndo_Aflag
) {
377 * Print the raw packet data in ASCII.
379 if (ndo
->ndo_Aflag
> 1) {
381 * Include the link-layer header.
383 ascii_print(ndo
, sp
, h
->caplen
);
386 * Don't include the link-layer header - and if
387 * we have nothing past the link-layer header,
390 if (h
->caplen
> hdrlen
)
391 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
399 * By default, print the specified data out in hex and ASCII.
402 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
404 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
409 ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
413 if(ndo
->program_name
)
414 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
416 (void)vfprintf(stderr
, fmt
, ap
);
421 (void)fputc('\n', stderr
);
430 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
434 if(ndo
->program_name
)
435 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
436 (void)fprintf(stderr
, "WARNING: ");
438 (void)vfprintf(stderr
, fmt
, ap
);
443 (void)fputc('\n', stderr
);
448 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
454 ret
= vfprintf(stdout
, fmt
, args
);
458 ndo_error(ndo
, "Unable to write output: %s", pcap_strerror(errno
));
463 ndo_set_function_pointers(netdissect_options
*ndo
)
465 ndo
->ndo_default_print
=ndo_default_print
;
466 ndo
->ndo_printf
=ndo_printf
;
467 ndo
->ndo_error
=ndo_error
;
468 ndo
->ndo_warning
=ndo_warning
;
472 * c-style: whitesmith