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
},
102 #ifdef HAVE_PCAP_USB_H
104 { usb_linux_48_byte_print
, DLT_USB_LINUX
},
105 #endif /* DLT_USB_LINUX */
106 #ifdef DLT_USB_LINUX_MMAPPED
107 { usb_linux_64_byte_print
, DLT_USB_LINUX_MMAPPED
},
108 #endif /* DLT_USB_LINUX_MMAPPED */
109 #endif /* HAVE_PCAP_USB_H */
110 #ifdef DLT_SYMANTEC_FIREWALL
111 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
114 { chdlc_if_print
, DLT_C_HDLC
},
117 { chdlc_if_print
, DLT_HDLC
},
120 { pppoe_if_print
, DLT_PPP_ETHER
},
122 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
123 { pflog_if_print
, DLT_PFLOG
},
125 { token_if_print
, DLT_IEEE802
},
126 { fddi_if_print
, DLT_FDDI
},
128 { sll_if_print
, DLT_LINUX_SLL
},
131 { fr_if_print
, DLT_FR
},
134 { fr_if_print
, DLT_FRELAY
},
137 { mfr_if_print
, DLT_MFR
},
139 { atm_if_print
, DLT_ATM_RFC1483
},
141 { sunatm_if_print
, DLT_SUNATM
},
144 { enc_if_print
, DLT_ENC
},
146 { sl_if_print
, DLT_SLIP
},
147 #ifdef DLT_SLIP_BSDOS
148 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
151 { ltalk_if_print
, DLT_LTALK
},
153 #ifdef DLT_JUNIPER_ATM1
154 { juniper_atm1_print
, DLT_JUNIPER_ATM1
},
156 #ifdef DLT_JUNIPER_ATM2
157 { juniper_atm2_print
, DLT_JUNIPER_ATM2
},
159 #ifdef DLT_JUNIPER_MFR
160 { juniper_mfr_print
, DLT_JUNIPER_MFR
},
162 #ifdef DLT_JUNIPER_MLFR
163 { juniper_mlfr_print
, DLT_JUNIPER_MLFR
},
165 #ifdef DLT_JUNIPER_MLPPP
166 { juniper_mlppp_print
, DLT_JUNIPER_MLPPP
},
168 #ifdef DLT_JUNIPER_PPPOE
169 { juniper_pppoe_print
, DLT_JUNIPER_PPPOE
},
171 #ifdef DLT_JUNIPER_PPPOE_ATM
172 { juniper_pppoe_atm_print
, DLT_JUNIPER_PPPOE_ATM
},
174 #ifdef DLT_JUNIPER_GGSN
175 { juniper_ggsn_print
, DLT_JUNIPER_GGSN
},
177 #ifdef DLT_JUNIPER_ES
178 { juniper_es_print
, DLT_JUNIPER_ES
},
180 #ifdef DLT_JUNIPER_MONITOR
181 { juniper_monitor_print
, DLT_JUNIPER_MONITOR
},
183 #ifdef DLT_JUNIPER_SERVICES
184 { juniper_services_print
, DLT_JUNIPER_SERVICES
},
186 #ifdef DLT_JUNIPER_ETHER
187 { juniper_ether_print
, DLT_JUNIPER_ETHER
},
189 #ifdef DLT_JUNIPER_PPP
190 { juniper_ppp_print
, DLT_JUNIPER_PPP
},
192 #ifdef DLT_JUNIPER_FRELAY
193 { juniper_frelay_print
, DLT_JUNIPER_FRELAY
},
195 #ifdef DLT_JUNIPER_CHDLC
196 { juniper_chdlc_print
, DLT_JUNIPER_CHDLC
},
199 { pktap_if_print
, DLT_PKTAP
},
201 #ifdef DLT_IEEE802_11_RADIO
202 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
204 #ifdef DLT_IEEE802_11
205 { ieee802_11_if_print
, DLT_IEEE802_11
},
207 #ifdef DLT_IEEE802_11_RADIO_AVS
208 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print
, DLT_PRISM_HEADER
},
213 { ppp_if_print
, DLT_PPP
},
214 #ifdef DLT_PPP_WITHDIRECTION
215 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
218 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
220 #ifdef DLT_PPP_SERIAL
221 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
226 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
229 static void ndo_error(netdissect_options
*ndo _U_
, const char *fmt
, ...)
230 __attribute__((noreturn
))
231 #ifdef __ATTRIBUTE___FORMAT_OK
232 __attribute__((format (printf
, 2, 3)))
233 #endif /* __ATTRIBUTE___FORMAT_OK */
235 static void ndo_warning(netdissect_options
*ndo _U_
, const char *fmt
, ...)
236 #ifdef __ATTRIBUTE___FORMAT_OK
237 __attribute__((format (printf
, 2, 3)))
238 #endif /* __ATTRIBUTE___FORMAT_OK */
241 static int ndo_printf(netdissect_options
*ndo _U_
, const char *fmt
, ...)
242 #ifdef __ATTRIBUTE___FORMAT_OK
243 __attribute ((format (printf
, 2, 3)))
244 #endif /* __ATTRIBUTE___FORMAT_OK */
248 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
,
249 uint32_t timezone_offset
)
252 thiszone
= timezone_offset
;
253 init_addrtoname(ndo
, localnet
, mask
);
258 lookup_printer(int type
)
260 const struct printer
*p
;
262 for (p
= printers
; p
->f
; ++p
)
266 #if defined(DLT_USER2) && defined(DLT_PKTAP)
268 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
271 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
272 * based OSes or the same value as LINKTYPE_PKTAP as it is on
273 * other OSes, to LINKTYPE_PKTAP, so files written with
274 * this version of libpcap for a DLT_PKTAP capture have a link-
275 * layer header type of LINKTYPE_PKTAP.
277 * However, files written on OS X Mavericks for a DLT_PKTAP
278 * capture have a link-layer header type of LINKTYPE_USER2.
279 * If we don't have a printer for DLT_USER2, and type is
280 * DLT_USER2, we look up the printer for DLT_PKTAP and use
283 if (type
== DLT_USER2
) {
284 for (p
= printers
; p
->f
; ++p
)
285 if (DLT_PKTAP
== p
->type
)
295 has_printer(int type
)
297 return (lookup_printer(type
) != NULL
);
301 get_if_printer(netdissect_options
*ndo
, int type
)
306 printer
= lookup_printer(type
);
307 if (printer
== NULL
) {
308 dltname
= pcap_datalink_val_to_name(type
);
310 (*ndo
->ndo_error
)(ndo
,
311 "packet printing is not supported for link type %s: use -w",
314 (*ndo
->ndo_error
)(ndo
,
315 "packet printing is not supported for link type %d: use -w", type
);
321 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
322 const u_char
*sp
, u_int packets_captured
)
326 if(ndo
->ndo_packet_number
)
327 ND_PRINT((ndo
, "%5u ", packets_captured
));
329 ts_print(ndo
, &h
->ts
);
332 * Some printers want to check that they're not walking off the
334 * Rather than pass it all the way down, we set this member
335 * of the netdissect_options structure.
337 ndo
->ndo_snapend
= sp
+ h
->caplen
;
339 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
342 * Restore the original snapend, as a printer might have
345 ndo
->ndo_snapend
= sp
+ h
->caplen
;
346 if (ndo
->ndo_Xflag
) {
348 * Print the raw packet data in hex and ASCII.
350 if (ndo
->ndo_Xflag
> 1) {
352 * Include the link-layer header.
354 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
357 * Don't include the link-layer header - and if
358 * we have nothing past the link-layer header,
361 if (h
->caplen
> hdrlen
)
362 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
365 } else if (ndo
->ndo_xflag
) {
367 * Print the raw packet data in hex.
369 if (ndo
->ndo_xflag
> 1) {
371 * Include the link-layer header.
373 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
376 * Don't include the link-layer header - and if
377 * we have nothing past the link-layer header,
380 if (h
->caplen
> hdrlen
)
381 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
384 } else if (ndo
->ndo_Aflag
) {
386 * Print the raw packet data in ASCII.
388 if (ndo
->ndo_Aflag
> 1) {
390 * Include the link-layer header.
392 ascii_print(ndo
, sp
, h
->caplen
);
395 * Don't include the link-layer header - and if
396 * we have nothing past the link-layer header,
399 if (h
->caplen
> hdrlen
)
400 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
408 * By default, print the specified data out in hex and ASCII.
411 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
413 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
418 ndo_error(netdissect_options
*ndo
, const char *fmt
, ...)
422 if(ndo
->program_name
)
423 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
425 (void)vfprintf(stderr
, fmt
, ap
);
430 (void)fputc('\n', stderr
);
438 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
442 if(ndo
->program_name
)
443 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
444 (void)fprintf(stderr
, "WARNING: ");
446 (void)vfprintf(stderr
, fmt
, ap
);
451 (void)fputc('\n', stderr
);
456 ndo_printf(netdissect_options
*ndo _U_
, const char *fmt
, ...)
462 ret
= vfprintf(stdout
, fmt
, args
);
466 ndo_error(ndo
, "Unable to write output: %s", pcap_strerror(errno
));
471 ndo_set_function_pointers(netdissect_options
*ndo
)
473 ndo
->ndo_default_print
=ndo_default_print
;
474 ndo
->ndo_printf
=ndo_printf
;
475 ndo
->ndo_error
=ndo_error
;
476 ndo
->ndo_warning
=ndo_warning
;
480 * c-style: whitesmith