]> The Tcpdump Group git mirrors - tcpdump/blob - print.c
Add a comment about packet and capture lengths
[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 (invalid_header) {
342 ND_PRINT("]\n");
343 return;
344 }
345
346 /*
347 * At this point:
348 * capture length != 0,
349 * packet length != 0,
350 * packet length >= capture length.
351 */
352
353 ts_print(ndo, &h->ts);
354
355 /*
356 * Printers must check that they're not walking off the end of
357 * the packet.
358 * Rather than pass it all the way down, we set this member
359 * of the netdissect_options structure.
360 */
361 ndo->ndo_snapend = sp + h->caplen;
362
363 hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
364
365 /*
366 * Restore the original snapend, as a printer might have
367 * changed it.
368 */
369 ndo->ndo_snapend = sp + h->caplen;
370 if (ndo->ndo_Xflag) {
371 /*
372 * Print the raw packet data in hex and ASCII.
373 */
374 if (ndo->ndo_Xflag > 1) {
375 /*
376 * Include the link-layer header.
377 */
378 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
379 } else {
380 /*
381 * Don't include the link-layer header - and if
382 * we have nothing past the link-layer header,
383 * print nothing.
384 */
385 if (h->caplen > hdrlen)
386 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
387 h->caplen - hdrlen);
388 }
389 } else if (ndo->ndo_xflag) {
390 /*
391 * Print the raw packet data in hex.
392 */
393 if (ndo->ndo_xflag > 1) {
394 /*
395 * Include the link-layer header.
396 */
397 hex_print(ndo, "\n\t", sp, h->caplen);
398 } else {
399 /*
400 * Don't include the link-layer header - and if
401 * we have nothing past the link-layer header,
402 * print nothing.
403 */
404 if (h->caplen > hdrlen)
405 hex_print(ndo, "\n\t", sp + hdrlen,
406 h->caplen - hdrlen);
407 }
408 } else if (ndo->ndo_Aflag) {
409 /*
410 * Print the raw packet data in ASCII.
411 */
412 if (ndo->ndo_Aflag > 1) {
413 /*
414 * Include the link-layer header.
415 */
416 ascii_print(ndo, sp, h->caplen);
417 } else {
418 /*
419 * Don't include the link-layer header - and if
420 * we have nothing past the link-layer header,
421 * print nothing.
422 */
423 if (h->caplen > hdrlen)
424 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
425 }
426 }
427
428 ND_PRINT("\n");
429 }
430
431 /*
432 * By default, print the specified data out in hex and ASCII.
433 */
434 static void
435 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
436 {
437 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
438 }
439
440 /* VARARGS */
441 static void
442 ndo_error(netdissect_options *ndo, const char *fmt, ...)
443 {
444 va_list ap;
445
446 if(ndo->program_name)
447 (void)fprintf(stderr, "%s: ", ndo->program_name);
448 va_start(ap, fmt);
449 (void)vfprintf(stderr, fmt, ap);
450 va_end(ap);
451 if (*fmt) {
452 fmt += strlen(fmt);
453 if (fmt[-1] != '\n')
454 (void)fputc('\n', stderr);
455 }
456 nd_cleanup();
457 exit(1);
458 /* NOTREACHED */
459 }
460
461 /* VARARGS */
462 static void
463 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
464 {
465 va_list ap;
466
467 if(ndo->program_name)
468 (void)fprintf(stderr, "%s: ", ndo->program_name);
469 (void)fprintf(stderr, "WARNING: ");
470 va_start(ap, fmt);
471 (void)vfprintf(stderr, fmt, ap);
472 va_end(ap);
473 if (*fmt) {
474 fmt += strlen(fmt);
475 if (fmt[-1] != '\n')
476 (void)fputc('\n', stderr);
477 }
478 }
479
480 static int
481 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
482 {
483 va_list args;
484 int ret;
485
486 va_start(args, fmt);
487 ret = vfprintf(stdout, fmt, args);
488 va_end(args);
489
490 if (ret < 0)
491 ndo_error(ndo, "Unable to write output: %s", pcap_strerror(errno));
492 return (ret);
493 }
494
495 void
496 ndo_set_function_pointers(netdissect_options *ndo)
497 {
498 ndo->ndo_default_print=ndo_default_print;
499 ndo->ndo_printf=ndo_printf;
500 ndo->ndo_error=ndo_error;
501 ndo->ndo_warning=ndo_warning;
502 }
503 /*
504 * Local Variables:
505 * c-style: whitesmith
506 * c-basic-offset: 8
507 * End:
508 */