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"
40 #include "netdissect-alloc.h"
42 #include "pcap-missing.h"
49 static const struct printer printers
[] = {
50 { ether_if_print
, DLT_EN10MB
},
52 { ipnet_if_print
, DLT_IPNET
},
54 #ifdef DLT_IEEE802_15_4
55 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
57 #ifdef DLT_IEEE802_15_4_NOFCS
58 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
61 { ppi_if_print
, DLT_PPI
},
63 #ifdef DLT_NETANALYZER
64 { netanalyzer_if_print
, DLT_NETANALYZER
},
66 #ifdef DLT_NETANALYZER_TRANSPARENT
67 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
70 { nflog_if_print
, DLT_NFLOG
},
73 { cip_if_print
, DLT_CIP
},
76 { cip_if_print
, DLT_ATM_CLIP
},
79 { ipfc_if_print
, DLT_IP_OVER_FC
},
81 { null_if_print
, DLT_NULL
},
83 { null_if_print
, DLT_LOOP
},
85 #ifdef DLT_APPLE_IP_OVER_IEEE1394
86 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
88 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
89 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
92 { lane_if_print
, DLT_LANE8023
},
94 { arcnet_if_print
, DLT_ARCNET
},
95 #ifdef DLT_ARCNET_LINUX
96 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
98 { raw_if_print
, DLT_RAW
},
100 { raw_if_print
, DLT_IPV4
},
103 { raw_if_print
, DLT_IPV6
},
106 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
107 #endif /* DLT_USB_LINUX */
108 #ifdef DLT_USB_LINUX_MMAPPED
109 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
110 #endif /* DLT_USB_LINUX_MMAPPED */
111 #ifdef DLT_SYMANTEC_FIREWALL
112 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
115 { chdlc_if_print
, DLT_C_HDLC
},
118 { chdlc_if_print
, DLT_HDLC
},
121 { pppoe_if_print
, DLT_PPP_ETHER
},
123 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
124 { pflog_if_print
, DLT_PFLOG
},
126 { token_if_print
, DLT_IEEE802
},
127 { fddi_if_print
, DLT_FDDI
},
129 { sll_if_print
, DLT_LINUX_SLL
},
131 #ifdef DLT_LINUX_SLL2
132 { sll2_if_print
, DLT_LINUX_SLL2
},
135 { fr_if_print
, DLT_FR
},
138 { fr_if_print
, DLT_FRELAY
},
141 { mfr_if_print
, DLT_MFR
},
143 { atm_if_print
, DLT_ATM_RFC1483
},
145 { sunatm_if_print
, DLT_SUNATM
},
148 { enc_if_print
, DLT_ENC
},
150 { sl_if_print
, DLT_SLIP
},
151 #ifdef DLT_SLIP_BSDOS
152 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
155 { ltalk_if_print
, DLT_LTALK
},
157 #ifdef DLT_JUNIPER_ATM1
158 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
160 #ifdef DLT_JUNIPER_ATM2
161 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
163 #ifdef DLT_JUNIPER_MFR
164 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
166 #ifdef DLT_JUNIPER_MLFR
167 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
169 #ifdef DLT_JUNIPER_MLPPP
170 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
172 #ifdef DLT_JUNIPER_PPPOE
173 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
175 #ifdef DLT_JUNIPER_PPPOE_ATM
176 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
178 #ifdef DLT_JUNIPER_GGSN
179 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
181 #ifdef DLT_JUNIPER_ES
182 { juniper_es_if_print
, DLT_JUNIPER_ES
},
184 #ifdef DLT_JUNIPER_MONITOR
185 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
187 #ifdef DLT_JUNIPER_SERVICES
188 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
190 #ifdef DLT_JUNIPER_ETHER
191 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
193 #ifdef DLT_JUNIPER_PPP
194 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
196 #ifdef DLT_JUNIPER_FRELAY
197 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
199 #ifdef DLT_JUNIPER_CHDLC
200 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
203 { pktap_if_print
, DLT_PKTAP
},
205 #ifdef DLT_IEEE802_11_RADIO
206 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
208 #ifdef DLT_IEEE802_11
209 { ieee802_11_if_print
, DLT_IEEE802_11
},
211 #ifdef DLT_IEEE802_11_RADIO_AVS
212 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
214 #ifdef DLT_PRISM_HEADER
215 { prism_if_print
, DLT_PRISM_HEADER
},
217 { ppp_if_print
, DLT_PPP
},
218 #ifdef DLT_PPP_WITHDIRECTION
219 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
222 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
224 #ifdef DLT_PPP_SERIAL
225 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
230 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
233 static void NORETURN
ndo_error(netdissect_options
*ndo
,
234 status_exit_codes_t status
,
235 FORMAT_STRING(const char *fmt
), ...)
237 static void ndo_warning(netdissect_options
*ndo
,
238 FORMAT_STRING(const char *fmt
), ...)
241 static int ndo_printf(netdissect_options
*ndo
,
242 FORMAT_STRING(const char *fmt
), ...)
246 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
249 init_addrtoname(ndo
, localnet
, mask
);
254 lookup_printer(int type
)
256 const struct printer
*p
;
258 for (p
= printers
; p
->f
; ++p
)
262 #if defined(DLT_USER2) && defined(DLT_PKTAP)
264 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
267 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
268 * based OSes or the same value as LINKTYPE_PKTAP as it is on
269 * other OSes, to LINKTYPE_PKTAP, so files written with
270 * this version of libpcap for a DLT_PKTAP capture have a link-
271 * layer header type of LINKTYPE_PKTAP.
273 * However, files written on OS X Mavericks for a DLT_PKTAP
274 * capture have a link-layer header type of LINKTYPE_USER2.
275 * If we don't have a printer for DLT_USER2, and type is
276 * DLT_USER2, we look up the printer for DLT_PKTAP and use
279 if (type
== DLT_USER2
) {
280 for (p
= printers
; p
->f
; ++p
)
281 if (DLT_PKTAP
== p
->type
)
291 has_printer(int type
)
293 return (lookup_printer(type
) != NULL
);
297 get_if_printer(netdissect_options
*ndo
, int type
)
302 printer
= lookup_printer(type
);
303 if (printer
== NULL
) {
304 dltname
= pcap_datalink_val_to_name(type
);
306 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
307 "packet printing is not supported for link type %s: use -w",
310 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
311 "packet printing is not supported for link type %d: use -w", type
);
317 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
318 const u_char
*sp
, u_int packets_captured
)
321 int invalid_header
= 0;
323 if (ndo
->ndo_packet_number
)
324 ND_PRINT("%5u ", packets_captured
);
326 /* Sanity checks on packet length / capture length */
327 if (h
->caplen
== 0) {
329 ND_PRINT("[Invalid header: caplen==0");
332 if (!invalid_header
) {
334 ND_PRINT("[Invalid header:");
338 } else if (h
->len
< h
->caplen
) {
339 if (!invalid_header
) {
341 ND_PRINT("[Invalid header:");
344 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
346 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
347 if (!invalid_header
) {
349 ND_PRINT("[Invalid header:");
352 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
354 if (h
->len
> MAXIMUM_SNAPLEN
) {
355 if (!invalid_header
) {
357 ND_PRINT("[Invalid header:");
360 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
362 if (invalid_header
) {
369 * capture length != 0,
370 * packet length != 0,
371 * capture length <= MAXIMUM_SNAPLEN,
372 * packet length <= MAXIMUM_SNAPLEN,
373 * packet length >= capture length.
375 * Currently, there is no D-Bus printer, thus no need for
379 ts_print(ndo
, &h
->ts
);
382 * Printers must check that they're not walking off the end of
384 * Rather than pass it all the way down, we set this member
385 * of the netdissect_options structure.
387 ndo
->ndo_snapend
= sp
+ h
->caplen
;
389 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
392 * Restore the original snapend, as a printer might have
395 ndo
->ndo_snapend
= sp
+ h
->caplen
;
396 if (ndo
->ndo_Xflag
) {
398 * Print the raw packet data in hex and ASCII.
400 if (ndo
->ndo_Xflag
> 1) {
402 * Include the link-layer header.
404 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
407 * Don't include the link-layer header - and if
408 * we have nothing past the link-layer header,
411 if (h
->caplen
> hdrlen
)
412 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
415 } else if (ndo
->ndo_xflag
) {
417 * Print the raw packet data in hex.
419 if (ndo
->ndo_xflag
> 1) {
421 * Include the link-layer header.
423 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
426 * Don't include the link-layer header - and if
427 * we have nothing past the link-layer header,
430 if (h
->caplen
> hdrlen
)
431 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
434 } else if (ndo
->ndo_Aflag
) {
436 * Print the raw packet data in ASCII.
438 if (ndo
->ndo_Aflag
> 1) {
440 * Include the link-layer header.
442 ascii_print(ndo
, sp
, h
->caplen
);
445 * Don't include the link-layer header - and if
446 * we have nothing past the link-layer header,
449 if (h
->caplen
> hdrlen
)
450 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
459 * By default, print the specified data out in hex and ASCII.
462 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
464 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
469 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
470 const char *fmt
, ...)
474 if (ndo
->program_name
)
475 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
477 (void)vfprintf(stderr
, fmt
, ap
);
482 (void)fputc('\n', stderr
);
491 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
495 if (ndo
->program_name
)
496 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
497 (void)fprintf(stderr
, "WARNING: ");
499 (void)vfprintf(stderr
, fmt
, ap
);
504 (void)fputc('\n', stderr
);
509 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
515 ret
= vfprintf(stdout
, fmt
, args
);
519 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
520 "Unable to write output: %s", pcap_strerror(errno
));
525 ndo_set_function_pointers(netdissect_options
*ndo
)
527 ndo
->ndo_default_print
=ndo_default_print
;
528 ndo
->ndo_printf
=ndo_printf
;
529 ndo
->ndo_error
=ndo_error
;
530 ndo
->ndo_warning
=ndo_warning
;