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 <tcpdump-stdinc.h>
37 #include "netdissect.h"
38 #include "interface.h"
39 #include "addrtoname.h"
47 static const struct printer printers
[] = {
48 { ether_if_print
, DLT_EN10MB
},
50 { ipnet_if_print
, DLT_IPNET
},
52 #ifdef DLT_IEEE802_15_4
53 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
55 #ifdef DLT_IEEE802_15_4_NOFCS
56 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
59 { ppi_if_print
, DLT_PPI
},
61 #ifdef DLT_NETANALYZER
62 { netanalyzer_if_print
, DLT_NETANALYZER
},
64 #ifdef DLT_NETANALYZER_TRANSPARENT
65 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
67 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
68 { nflog_if_print
, DLT_NFLOG
},
71 { cip_if_print
, DLT_CIP
},
74 { cip_if_print
, DLT_ATM_CLIP
},
77 { ipfc_if_print
, DLT_IP_OVER_FC
},
79 { null_if_print
, DLT_NULL
},
81 { null_if_print
, DLT_LOOP
},
83 #ifdef DLT_APPLE_IP_OVER_IEEE1394
84 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
86 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
87 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
90 { lane_if_print
, DLT_LANE8023
},
92 { arcnet_if_print
, DLT_ARCNET
},
93 #ifdef DLT_ARCNET_LINUX
94 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
96 { raw_if_print
, DLT_RAW
},
98 { raw_if_print
, DLT_IPV4
},
101 { raw_if_print
, DLT_IPV6
},
103 #ifdef HAVE_PCAP_USB_H
105 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
106 #endif /* DLT_USB_LINUX */
107 #ifdef DLT_USB_LINUX_MMAPPED
108 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
109 #endif /* DLT_USB_LINUX_MMAPPED */
110 #endif /* HAVE_PCAP_USB_H */
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
},
132 { fr_if_print
, DLT_FR
},
135 { fr_if_print
, DLT_FRELAY
},
138 { mfr_if_print
, DLT_MFR
},
140 { atm_if_print
, DLT_ATM_RFC1483
},
142 { sunatm_if_print
, DLT_SUNATM
},
145 { enc_if_print
, DLT_ENC
},
147 { sl_if_print
, DLT_SLIP
},
148 #ifdef DLT_SLIP_BSDOS
149 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
152 { ltalk_if_print
, DLT_LTALK
},
154 #ifdef DLT_JUNIPER_ATM1
155 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
157 #ifdef DLT_JUNIPER_ATM2
158 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
160 #ifdef DLT_JUNIPER_MFR
161 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
163 #ifdef DLT_JUNIPER_MLFR
164 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
166 #ifdef DLT_JUNIPER_MLPPP
167 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
169 #ifdef DLT_JUNIPER_PPPOE
170 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
172 #ifdef DLT_JUNIPER_PPPOE_ATM
173 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
175 #ifdef DLT_JUNIPER_GGSN
176 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
178 #ifdef DLT_JUNIPER_ES
179 { juniper_es_print
, DLT_JUNIPER_ES
},
181 #ifdef DLT_JUNIPER_MONITOR
182 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
184 #ifdef DLT_JUNIPER_SERVICES
185 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
187 #ifdef DLT_JUNIPER_ETHER
188 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
190 #ifdef DLT_JUNIPER_PPP
191 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
193 #ifdef DLT_JUNIPER_FRELAY
194 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
196 #ifdef DLT_JUNIPER_CHDLC
197 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
200 { pktap_if_print
, DLT_PKTAP
},
202 #ifdef DLT_IEEE802_11_RADIO
203 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
205 #ifdef DLT_IEEE802_11
206 { ieee802_11_if_print
, DLT_IEEE802_11
},
208 #ifdef DLT_IEEE802_11_RADIO_AVS
209 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
211 #ifdef DLT_PRISM_HEADER
212 { prism_if_print
, DLT_PRISM_HEADER
},
214 { ppp_if_print
, DLT_PPP
},
215 #ifdef DLT_PPP_WITHDIRECTION
216 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
219 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
221 #ifdef DLT_PPP_SERIAL
222 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
228 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
231 static void ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
232 __attribute__((noreturn
))
233 #ifdef __ATTRIBUTE___FORMAT_OK
234 __attribute__((format (printf
, 2, 3)))
235 #endif /* __ATTRIBUTE___FORMAT_OK */
237 static void ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
238 #ifdef __ATTRIBUTE___FORMAT_OK
239 __attribute__((format (printf
, 2, 3)))
240 #endif /* __ATTRIBUTE___FORMAT_OK */
243 static int tcpdump_printf(netdissect_options
*ndo _U_
, const char *fmt
, ...)
244 #ifdef __ATTRIBUTE___FORMAT_OK
245 __attribute ((format (printf
, 2, 3)))
246 #endif /* __ATTRIBUTE___FORMAT_OK */
250 init_print(netdissect_options
*ndo
, u_int32_t localnet
, u_int32_t mask
,
251 uint32_t timezone_offset
)
254 thiszone
= timezone_offset
;
255 init_addrtoname(ndo
, localnet
, mask
);
260 lookup_printer(int type
)
262 const struct printer
*p
;
264 for (p
= printers
; p
->f
; ++p
)
268 #if defined(DLT_USER2) && defined(DLT_PKTAP)
270 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
273 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
274 * based OSes or the same value as LINKTYPE_PKTAP as it is on
275 * other OSes, to LINKTYPE_PKTAP, so files written with
276 * this version of libpcap for a DLT_PKTAP capture have a link-
277 * layer header type of LINKTYPE_PKTAP.
279 * However, files written on OS X Mavericks for a DLT_PKTAP
280 * capture have a link-layer header type of LINKTYPE_USER2.
281 * If we don't have a printer for DLT_USER2, and type is
282 * DLT_USER2, we look up the printer for DLT_PKTAP and use
285 if (type
== DLT_USER2
) {
286 for (p
= printers
; p
->f
; ++p
)
287 if (DLT_PKTAP
== p
->type
)
297 has_printer(int type
)
300 return (lookup_printer(type
) != NULL
);
304 get_print_info(netdissect_options
*ndo
, int type
)
307 struct print_info printinfo
;
310 printinfo
.printer
= lookup_printer(type
);
311 if (printinfo
.printer
== NULL
) {
312 dltname
= pcap_datalink_val_to_name(type
);
314 (*ndo
->ndo_error
)(ndo
,
315 "packet printing is not supported for link type %s: use -w",
318 (*ndo
->ndo_error
)(ndo
,
319 "packet printing is not supported for link type %d: use -w", type
);
325 pretty_print_packet(struct print_info
*print_info
, const struct pcap_pkthdr
*h
,
326 const u_char
*sp
, u_int packets_captured
)
329 netdissect_options
*ndo
;
331 ndo
= print_info
->ndo
;
333 if(ndo
->ndo_packet_number
)
334 ND_PRINT((ndo
, "%5u ", packets_captured
));
336 ts_print(ndo
, &h
->ts
);
339 * Some printers want to check that they're not walking off the
341 * Rather than pass it all the way down, we set this member
342 * of the netdissect_options structure.
344 ndo
->ndo_snapend
= sp
+ h
->caplen
;
346 hdrlen
= (*print_info
->printer
)(print_info
->ndo
, h
, sp
);
349 * Restore the original snapend, as a printer might have
352 ndo
->ndo_snapend
= sp
+ h
->caplen
;
353 if (ndo
->ndo_Xflag
) {
355 * Print the raw packet data in hex and ASCII.
357 if (ndo
->ndo_Xflag
> 1) {
359 * Include the link-layer header.
361 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
364 * Don't include the link-layer header - and if
365 * we have nothing past the link-layer header,
368 if (h
->caplen
> hdrlen
)
369 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
372 } else if (ndo
->ndo_xflag
) {
374 * Print the raw packet data in hex.
376 if (ndo
->ndo_xflag
> 1) {
378 * Include the link-layer header.
380 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
383 * Don't include the link-layer header - and if
384 * we have nothing past the link-layer header,
387 if (h
->caplen
> hdrlen
)
388 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
391 } else if (ndo
->ndo_Aflag
) {
393 * Print the raw packet data in ASCII.
395 if (ndo
->ndo_Aflag
> 1) {
397 * Include the link-layer header.
399 ascii_print(ndo
, sp
, h
->caplen
);
402 * Don't include the link-layer header - and if
403 * we have nothing past the link-layer header,
406 if (h
->caplen
> hdrlen
)
407 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
415 * By default, print the specified data out in hex and ASCII.
418 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
420 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
425 ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
429 (void)fprintf(stderr
, "%s: ", program_name
);
431 (void)vfprintf(stderr
, fmt
, ap
);
436 (void)fputc('\n', stderr
);
444 ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
448 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
450 (void)vfprintf(stderr
, fmt
, ap
);
455 (void)fputc('\n', stderr
);
460 tcpdump_printf(netdissect_options
*ndo _U_
, const char *fmt
, ...)
466 ret
= vfprintf(stdout
, fmt
, args
);
473 ndo_set_function_pointers(netdissect_options
*ndo
)
476 ndo
->ndo_default_print
=ndo_default_print
;
477 ndo
->ndo_printf
=tcpdump_printf
;
478 ndo
->ndo_error
=ndo_error
;
479 ndo
->ndo_warning
=ndo_warning
;
483 * c-style: whitesmith