]> The Tcpdump Group git mirrors - tcpdump/blob - print.c
Rename the fn_printX() functions to nd_printX()
[tcpdump] / print.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "netdissect-stdinc.h"
36
37 #include "netdissect.h"
38 #include "addrtoname.h"
39 #include "print.h"
40 #include "netdissect-alloc.h"
41
42 struct printer {
43 if_printer f;
44 int type;
45 };
46
47 static const struct printer printers[] = {
48 { ether_if_print, DLT_EN10MB },
49 #ifdef DLT_IPNET
50 { ipnet_if_print, DLT_IPNET },
51 #endif
52 #ifdef DLT_IEEE802_15_4
53 { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
54 #endif
55 #ifdef DLT_IEEE802_15_4_NOFCS
56 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
57 #endif
58 #ifdef DLT_PPI
59 { ppi_if_print, DLT_PPI },
60 #endif
61 #ifdef DLT_NETANALYZER
62 { netanalyzer_if_print, DLT_NETANALYZER },
63 #endif
64 #ifdef DLT_NETANALYZER_TRANSPARENT
65 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
66 #endif
67 #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
68 { nflog_if_print, DLT_NFLOG},
69 #endif
70 #ifdef DLT_CIP
71 { cip_if_print, DLT_CIP },
72 #endif
73 #ifdef DLT_ATM_CLIP
74 { cip_if_print, DLT_ATM_CLIP },
75 #endif
76 #ifdef DLT_IP_OVER_FC
77 { ipfc_if_print, DLT_IP_OVER_FC },
78 #endif
79 { null_if_print, DLT_NULL },
80 #ifdef DLT_LOOP
81 { null_if_print, DLT_LOOP },
82 #endif
83 #ifdef DLT_APPLE_IP_OVER_IEEE1394
84 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
85 #endif
86 #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
87 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
88 #endif
89 #ifdef DLT_LANE8023
90 { lane_if_print, DLT_LANE8023 },
91 #endif
92 { arcnet_if_print, DLT_ARCNET },
93 #ifdef DLT_ARCNET_LINUX
94 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
95 #endif
96 { raw_if_print, DLT_RAW },
97 #ifdef DLT_IPV4
98 { raw_if_print, DLT_IPV4 },
99 #endif
100 #ifdef DLT_IPV6
101 { raw_if_print, DLT_IPV6 },
102 #endif
103 #ifdef DLT_USB_LINUX
104 { usb_linux_48_byte_if_print, DLT_USB_LINUX},
105 #endif /* DLT_USB_LINUX */
106 #ifdef DLT_USB_LINUX_MMAPPED
107 { usb_linux_64_byte_if_print, DLT_USB_LINUX_MMAPPED},
108 #endif /* DLT_USB_LINUX_MMAPPED */
109 #ifdef DLT_SYMANTEC_FIREWALL
110 { symantec_if_print, DLT_SYMANTEC_FIREWALL },
111 #endif
112 #ifdef DLT_C_HDLC
113 { chdlc_if_print, DLT_C_HDLC },
114 #endif
115 #ifdef DLT_HDLC
116 { chdlc_if_print, DLT_HDLC },
117 #endif
118 #ifdef DLT_PPP_ETHER
119 { pppoe_if_print, DLT_PPP_ETHER },
120 #endif
121 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
122 { pflog_if_print, DLT_PFLOG },
123 #endif
124 { token_if_print, DLT_IEEE802 },
125 { fddi_if_print, DLT_FDDI },
126 #ifdef DLT_LINUX_SLL
127 { sll_if_print, DLT_LINUX_SLL },
128 #endif
129 #ifdef DLT_FR
130 { fr_if_print, DLT_FR },
131 #endif
132 #ifdef DLT_FRELAY
133 { fr_if_print, DLT_FRELAY },
134 #endif
135 #ifdef DLT_MFR
136 { mfr_if_print, DLT_MFR },
137 #endif
138 { atm_if_print, DLT_ATM_RFC1483 },
139 #ifdef DLT_SUNATM
140 { sunatm_if_print, DLT_SUNATM },
141 #endif
142 #ifdef DLT_ENC
143 { enc_if_print, DLT_ENC },
144 #endif
145 { sl_if_print, DLT_SLIP },
146 #ifdef DLT_SLIP_BSDOS
147 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
148 #endif
149 #ifdef DLT_LTALK
150 { ltalk_if_print, DLT_LTALK },
151 #endif
152 #ifdef DLT_JUNIPER_ATM1
153 { juniper_atm1_if_print, DLT_JUNIPER_ATM1 },
154 #endif
155 #ifdef DLT_JUNIPER_ATM2
156 { juniper_atm2_if_print, DLT_JUNIPER_ATM2 },
157 #endif
158 #ifdef DLT_JUNIPER_MFR
159 { juniper_mfr_if_print, DLT_JUNIPER_MFR },
160 #endif
161 #ifdef DLT_JUNIPER_MLFR
162 { juniper_mlfr_if_print, DLT_JUNIPER_MLFR },
163 #endif
164 #ifdef DLT_JUNIPER_MLPPP
165 { juniper_mlppp_if_print, DLT_JUNIPER_MLPPP },
166 #endif
167 #ifdef DLT_JUNIPER_PPPOE
168 { juniper_pppoe_if_print, DLT_JUNIPER_PPPOE },
169 #endif
170 #ifdef DLT_JUNIPER_PPPOE_ATM
171 { juniper_pppoe_atm_if_print, DLT_JUNIPER_PPPOE_ATM },
172 #endif
173 #ifdef DLT_JUNIPER_GGSN
174 { juniper_ggsn_if_print, DLT_JUNIPER_GGSN },
175 #endif
176 #ifdef DLT_JUNIPER_ES
177 { juniper_es_if_print, DLT_JUNIPER_ES },
178 #endif
179 #ifdef DLT_JUNIPER_MONITOR
180 { juniper_monitor_if_print, DLT_JUNIPER_MONITOR },
181 #endif
182 #ifdef DLT_JUNIPER_SERVICES
183 { juniper_services_if_print, DLT_JUNIPER_SERVICES },
184 #endif
185 #ifdef DLT_JUNIPER_ETHER
186 { juniper_ether_if_print, DLT_JUNIPER_ETHER },
187 #endif
188 #ifdef DLT_JUNIPER_PPP
189 { juniper_ppp_if_print, DLT_JUNIPER_PPP },
190 #endif
191 #ifdef DLT_JUNIPER_FRELAY
192 { juniper_frelay_if_print, DLT_JUNIPER_FRELAY },
193 #endif
194 #ifdef DLT_JUNIPER_CHDLC
195 { juniper_chdlc_if_print, DLT_JUNIPER_CHDLC },
196 #endif
197 #ifdef DLT_PKTAP
198 { pktap_if_print, DLT_PKTAP },
199 #endif
200 #ifdef DLT_IEEE802_11_RADIO
201 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
202 #endif
203 #ifdef DLT_IEEE802_11
204 { ieee802_11_if_print, DLT_IEEE802_11},
205 #endif
206 #ifdef DLT_IEEE802_11_RADIO_AVS
207 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
208 #endif
209 #ifdef DLT_PRISM_HEADER
210 { prism_if_print, DLT_PRISM_HEADER },
211 #endif
212 { ppp_if_print, DLT_PPP },
213 #ifdef DLT_PPP_WITHDIRECTION
214 { ppp_if_print, DLT_PPP_WITHDIRECTION },
215 #endif
216 #ifdef DLT_PPP_BSDOS
217 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
218 #endif
219 #ifdef DLT_PPP_SERIAL
220 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
221 #endif
222 { NULL, 0 },
223 };
224
225 static void ndo_default_print(netdissect_options *ndo, const u_char *bp,
226 u_int length);
227
228 static void NORETURN ndo_error(netdissect_options *ndo,
229 status_exit_codes_t status,
230 FORMAT_STRING(const char *fmt), ...)
231 PRINTFLIKE(3, 4);
232 static void ndo_warning(netdissect_options *ndo,
233 FORMAT_STRING(const char *fmt), ...)
234 PRINTFLIKE(2, 3);
235
236 static int ndo_printf(netdissect_options *ndo,
237 FORMAT_STRING(const char *fmt), ...)
238 PRINTFLIKE(2, 3);
239
240 void
241 init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
242 uint32_t timezone_offset)
243 {
244
245 thiszone = timezone_offset;
246 init_addrtoname(ndo, localnet, mask);
247 init_checksum();
248 }
249
250 if_printer
251 lookup_printer(int type)
252 {
253 const struct printer *p;
254
255 for (p = printers; p->f; ++p)
256 if (type == p->type)
257 return p->f;
258
259 #if defined(DLT_USER2) && defined(DLT_PKTAP)
260 /*
261 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
262 * header.
263 *
264 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
265 * based OSes or the same value as LINKTYPE_PKTAP as it is on
266 * other OSes, to LINKTYPE_PKTAP, so files written with
267 * this version of libpcap for a DLT_PKTAP capture have a link-
268 * layer header type of LINKTYPE_PKTAP.
269 *
270 * However, files written on OS X Mavericks for a DLT_PKTAP
271 * capture have a link-layer header type of LINKTYPE_USER2.
272 * If we don't have a printer for DLT_USER2, and type is
273 * DLT_USER2, we look up the printer for DLT_PKTAP and use
274 * that.
275 */
276 if (type == DLT_USER2) {
277 for (p = printers; p->f; ++p)
278 if (DLT_PKTAP == p->type)
279 return p->f;
280 }
281 #endif
282
283 return NULL;
284 /* NOTREACHED */
285 }
286
287 int
288 has_printer(int type)
289 {
290 return (lookup_printer(type) != NULL);
291 }
292
293 if_printer
294 get_if_printer(netdissect_options *ndo, int type)
295 {
296 const char *dltname;
297 if_printer printer;
298
299 printer = lookup_printer(type);
300 if (printer == NULL) {
301 dltname = pcap_datalink_val_to_name(type);
302 if (dltname != NULL)
303 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
304 "packet printing is not supported for link type %s: use -w",
305 dltname);
306 else
307 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
308 "packet printing is not supported for link type %d: use -w", type);
309 }
310 return printer;
311 }
312
313 void
314 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
315 const u_char *sp, u_int packets_captured)
316 {
317 u_int hdrlen;
318 int invalid_header = 0;
319
320 if(ndo->ndo_packet_number)
321 ND_PRINT("%5u ", packets_captured);
322
323 /* Sanity checks on packet length / capture length */
324 if(h->caplen == 0) {
325 invalid_header = 1;
326 ND_PRINT("[Invalid header: caplen==0");
327 }
328 if (h->len == 0) {
329 if (!invalid_header) {
330 invalid_header = 1;
331 ND_PRINT("[Invalid header:");
332 } else
333 ND_PRINT(",");
334 ND_PRINT(" len==0");
335 } else if (h->len < h->caplen) {
336 if (!invalid_header) {
337 invalid_header = 1;
338 ND_PRINT("[Invalid header:");
339 } else
340 ND_PRINT(",");
341 ND_PRINT(" len(%u) < caplen(%u)", h->len, h->caplen);
342 }
343 if (h->caplen > MAXIMUM_SNAPLEN) {
344 if (!invalid_header) {
345 invalid_header = 1;
346 ND_PRINT("[Invalid header:");
347 } else
348 ND_PRINT(",");
349 ND_PRINT(" caplen(%u) > %u", h->caplen, MAXIMUM_SNAPLEN);
350 }
351 if (h->len > MAXIMUM_SNAPLEN) {
352 if (!invalid_header) {
353 invalid_header = 1;
354 ND_PRINT("[Invalid header:");
355 } else
356 ND_PRINT(",");
357 ND_PRINT(" len(%u) > %u", h->len, MAXIMUM_SNAPLEN);
358 }
359 if (invalid_header) {
360 ND_PRINT("]\n");
361 return;
362 }
363
364 /*
365 * At this point:
366 * capture length != 0,
367 * packet length != 0,
368 * capture length <= MAXIMUM_SNAPLEN,
369 * packet length <= MAXIMUM_SNAPLEN,
370 * packet length >= capture length.
371 *
372 * Currently, there is no D-Bus printer, thus no need for
373 * bigger lengths.
374 */
375
376 ts_print(ndo, &h->ts);
377
378 /*
379 * Printers must check that they're not walking off the end of
380 * the packet.
381 * Rather than pass it all the way down, we set this member
382 * of the netdissect_options structure.
383 */
384 ndo->ndo_snapend = sp + h->caplen;
385
386 hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
387
388 /*
389 * Restore the original snapend, as a printer might have
390 * changed it.
391 */
392 ndo->ndo_snapend = sp + h->caplen;
393 if (ndo->ndo_Xflag) {
394 /*
395 * Print the raw packet data in hex and ASCII.
396 */
397 if (ndo->ndo_Xflag > 1) {
398 /*
399 * Include the link-layer header.
400 */
401 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
402 } else {
403 /*
404 * Don't include the link-layer header - and if
405 * we have nothing past the link-layer header,
406 * print nothing.
407 */
408 if (h->caplen > hdrlen)
409 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
410 h->caplen - hdrlen);
411 }
412 } else if (ndo->ndo_xflag) {
413 /*
414 * Print the raw packet data in hex.
415 */
416 if (ndo->ndo_xflag > 1) {
417 /*
418 * Include the link-layer header.
419 */
420 hex_print(ndo, "\n\t", sp, h->caplen);
421 } else {
422 /*
423 * Don't include the link-layer header - and if
424 * we have nothing past the link-layer header,
425 * print nothing.
426 */
427 if (h->caplen > hdrlen)
428 hex_print(ndo, "\n\t", sp + hdrlen,
429 h->caplen - hdrlen);
430 }
431 } else if (ndo->ndo_Aflag) {
432 /*
433 * Print the raw packet data in ASCII.
434 */
435 if (ndo->ndo_Aflag > 1) {
436 /*
437 * Include the link-layer header.
438 */
439 ascii_print(ndo, sp, h->caplen);
440 } else {
441 /*
442 * Don't include the link-layer header - and if
443 * we have nothing past the link-layer header,
444 * print nothing.
445 */
446 if (h->caplen > hdrlen)
447 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
448 }
449 }
450
451 ND_PRINT("\n");
452 nd_free_all(ndo);
453 }
454
455 /*
456 * By default, print the specified data out in hex and ASCII.
457 */
458 static void
459 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
460 {
461 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
462 }
463
464 /* VARARGS */
465 static void
466 ndo_error(netdissect_options *ndo, status_exit_codes_t status,
467 const char *fmt, ...)
468 {
469 va_list ap;
470
471 if(ndo->program_name)
472 (void)fprintf(stderr, "%s: ", ndo->program_name);
473 va_start(ap, fmt);
474 (void)vfprintf(stderr, fmt, ap);
475 va_end(ap);
476 if (*fmt) {
477 fmt += strlen(fmt);
478 if (fmt[-1] != '\n')
479 (void)fputc('\n', stderr);
480 }
481 nd_cleanup();
482 exit(status);
483 /* NOTREACHED */
484 }
485
486 /* VARARGS */
487 static void
488 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
489 {
490 va_list ap;
491
492 if(ndo->program_name)
493 (void)fprintf(stderr, "%s: ", ndo->program_name);
494 (void)fprintf(stderr, "WARNING: ");
495 va_start(ap, fmt);
496 (void)vfprintf(stderr, fmt, ap);
497 va_end(ap);
498 if (*fmt) {
499 fmt += strlen(fmt);
500 if (fmt[-1] != '\n')
501 (void)fputc('\n', stderr);
502 }
503 }
504
505 static int
506 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
507 {
508 va_list args;
509 int ret;
510
511 va_start(args, fmt);
512 ret = vfprintf(stdout, fmt, args);
513 va_end(args);
514
515 if (ret < 0)
516 ndo_error(ndo, S_ERR_ND_WRITE_FILE,
517 "Unable to write output: %s", pcap_strerror(errno));
518 return (ret);
519 }
520
521 void
522 ndo_set_function_pointers(netdissect_options *ndo)
523 {
524 ndo->ndo_default_print=ndo_default_print;
525 ndo->ndo_printf=ndo_printf;
526 ndo->ndo_error=ndo_error;
527 ndo->ndo_warning=ndo_warning;
528 }