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