print.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <netdissect-stdinc.h>
  33. #include "netdissect.h"
  34. #include "addrtoname.h"
  35. #include "print.h"
  36. struct printer {
  37. if_printer f;
  38. int type;
  39. };
  40. static const struct printer printers[] = {
  41. { ether_if_print, DLT_EN10MB },
  42. #ifdef DLT_IPNET
  43. { ipnet_if_print, DLT_IPNET },
  44. #endif
  45. #ifdef DLT_IEEE802_15_4
  46. { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
  47. #endif
  48. #ifdef DLT_IEEE802_15_4_NOFCS
  49. { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
  50. #endif
  51. #ifdef DLT_PPI
  52. { ppi_if_print, DLT_PPI },
  53. #endif
  54. #ifdef DLT_NETANALYZER
  55. { netanalyzer_if_print, DLT_NETANALYZER },
  56. #endif
  57. #ifdef DLT_NETANALYZER_TRANSPARENT
  58. { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
  59. #endif
  60. #if defined(DLT_NFLOG) && defined(HAVE_PCAP_NFLOG_H)
  61. { nflog_if_print, DLT_NFLOG},
  62. #endif
  63. #ifdef DLT_CIP
  64. { cip_if_print, DLT_CIP },
  65. #endif
  66. #ifdef DLT_ATM_CLIP
  67. { cip_if_print, DLT_ATM_CLIP },
  68. #endif
  69. #ifdef DLT_IP_OVER_FC
  70. { ipfc_if_print, DLT_IP_OVER_FC },
  71. #endif
  72. { null_if_print, DLT_NULL },
  73. #ifdef DLT_LOOP
  74. { null_if_print, DLT_LOOP },
  75. #endif
  76. #ifdef DLT_APPLE_IP_OVER_IEEE1394
  77. { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
  78. #endif
  79. #if defined(DLT_BLUETOOTH_HCI_H4_WITH_PHDR) && defined(HAVE_PCAP_BLUETOOTH_H)
  80. { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
  81. #endif
  82. #ifdef DLT_LANE8023
  83. { lane_if_print, DLT_LANE8023 },
  84. #endif
  85. { arcnet_if_print, DLT_ARCNET },
  86. #ifdef DLT_ARCNET_LINUX
  87. { arcnet_linux_if_print, DLT_ARCNET_LINUX },
  88. #endif
  89. { raw_if_print, DLT_RAW },
  90. #ifdef DLT_IPV4
  91. { raw_if_print, DLT_IPV4 },
  92. #endif
  93. #ifdef DLT_IPV6
  94. { raw_if_print, DLT_IPV6 },
  95. #endif
  96. #ifdef HAVE_PCAP_USB_H
  97. #ifdef DLT_USB_LINUX
  98. { usb_linux_48_byte_print, DLT_USB_LINUX},
  99. #endif /* DLT_USB_LINUX */
  100. #ifdef DLT_USB_LINUX_MMAPPED
  101. { usb_linux_64_byte_print, DLT_USB_LINUX_MMAPPED},
  102. #endif /* DLT_USB_LINUX_MMAPPED */
  103. #endif /* HAVE_PCAP_USB_H */
  104. #ifdef DLT_SYMANTEC_FIREWALL
  105. { symantec_if_print, DLT_SYMANTEC_FIREWALL },
  106. #endif
  107. #ifdef DLT_C_HDLC
  108. { chdlc_if_print, DLT_C_HDLC },
  109. #endif
  110. #ifdef DLT_HDLC
  111. { chdlc_if_print, DLT_HDLC },
  112. #endif
  113. #ifdef DLT_PPP_ETHER
  114. { pppoe_if_print, DLT_PPP_ETHER },
  115. #endif
  116. #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
  117. { pflog_if_print, DLT_PFLOG },
  118. #endif
  119. { token_if_print, DLT_IEEE802 },
  120. { fddi_if_print, DLT_FDDI },
  121. #ifdef DLT_LINUX_SLL
  122. { sll_if_print, DLT_LINUX_SLL },
  123. #endif
  124. #ifdef DLT_FR
  125. { fr_if_print, DLT_FR },
  126. #endif
  127. #ifdef DLT_FRELAY
  128. { fr_if_print, DLT_FRELAY },
  129. #endif
  130. #ifdef DLT_MFR
  131. { mfr_if_print, DLT_MFR },
  132. #endif
  133. { atm_if_print, DLT_ATM_RFC1483 },
  134. #ifdef DLT_SUNATM
  135. { sunatm_if_print, DLT_SUNATM },
  136. #endif
  137. #ifdef DLT_ENC
  138. { enc_if_print, DLT_ENC },
  139. #endif
  140. { sl_if_print, DLT_SLIP },
  141. #ifdef DLT_SLIP_BSDOS
  142. { sl_bsdos_if_print, DLT_SLIP_BSDOS },
  143. #endif
  144. #ifdef DLT_LTALK
  145. { ltalk_if_print, DLT_LTALK },
  146. #endif
  147. #ifdef DLT_JUNIPER_ATM1
  148. { juniper_atm1_print, DLT_JUNIPER_ATM1 },
  149. #endif
  150. #ifdef DLT_JUNIPER_ATM2
  151. { juniper_atm2_print, DLT_JUNIPER_ATM2 },
  152. #endif
  153. #ifdef DLT_JUNIPER_MFR
  154. { juniper_mfr_print, DLT_JUNIPER_MFR },
  155. #endif
  156. #ifdef DLT_JUNIPER_MLFR
  157. { juniper_mlfr_print, DLT_JUNIPER_MLFR },
  158. #endif
  159. #ifdef DLT_JUNIPER_MLPPP
  160. { juniper_mlppp_print, DLT_JUNIPER_MLPPP },
  161. #endif
  162. #ifdef DLT_JUNIPER_PPPOE
  163. { juniper_pppoe_print, DLT_JUNIPER_PPPOE },
  164. #endif
  165. #ifdef DLT_JUNIPER_PPPOE_ATM
  166. { juniper_pppoe_atm_print, DLT_JUNIPER_PPPOE_ATM },
  167. #endif
  168. #ifdef DLT_JUNIPER_GGSN
  169. { juniper_ggsn_print, DLT_JUNIPER_GGSN },
  170. #endif
  171. #ifdef DLT_JUNIPER_ES
  172. { juniper_es_print, DLT_JUNIPER_ES },
  173. #endif
  174. #ifdef DLT_JUNIPER_MONITOR
  175. { juniper_monitor_print, DLT_JUNIPER_MONITOR },
  176. #endif
  177. #ifdef DLT_JUNIPER_SERVICES
  178. { juniper_services_print, DLT_JUNIPER_SERVICES },
  179. #endif
  180. #ifdef DLT_JUNIPER_ETHER
  181. { juniper_ether_print, DLT_JUNIPER_ETHER },
  182. #endif
  183. #ifdef DLT_JUNIPER_PPP
  184. { juniper_ppp_print, DLT_JUNIPER_PPP },
  185. #endif
  186. #ifdef DLT_JUNIPER_FRELAY
  187. { juniper_frelay_print, DLT_JUNIPER_FRELAY },
  188. #endif
  189. #ifdef DLT_JUNIPER_CHDLC
  190. { juniper_chdlc_print, DLT_JUNIPER_CHDLC },
  191. #endif
  192. #ifdef DLT_PKTAP
  193. { pktap_if_print, DLT_PKTAP },
  194. #endif
  195. #ifdef DLT_IEEE802_11_RADIO
  196. { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
  197. #endif
  198. #ifdef DLT_IEEE802_11
  199. { ieee802_11_if_print, DLT_IEEE802_11},
  200. #endif
  201. #ifdef DLT_IEEE802_11_RADIO_AVS
  202. { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
  203. #endif
  204. #ifdef DLT_PRISM_HEADER
  205. { prism_if_print, DLT_PRISM_HEADER },
  206. #endif
  207. { ppp_if_print, DLT_PPP },
  208. #ifdef DLT_PPP_WITHDIRECTION
  209. { ppp_if_print, DLT_PPP_WITHDIRECTION },
  210. #endif
  211. #ifdef DLT_PPP_BSDOS
  212. { ppp_bsdos_if_print, DLT_PPP_BSDOS },
  213. #endif
  214. #ifdef DLT_PPP_SERIAL
  215. { ppp_hdlc_if_print, DLT_PPP_SERIAL },
  216. #endif
  217. { NULL, 0 },
  218. };
  219. static void ndo_default_print(netdissect_options *ndo, const u_char *bp,
  220. u_int length);
  221. static void ndo_error(netdissect_options *ndo,
  222. FORMAT_STRING(const char *fmt), ...)
  223. NORETURN PRINTFLIKE(2, 3);
  224. static void ndo_warning(netdissect_options *ndo,
  225. FORMAT_STRING(const char *fmt), ...)
  226. PRINTFLIKE(2, 3);
  227. static int ndo_printf(netdissect_options *ndo,
  228. FORMAT_STRING(const char *fmt), ...)
  229. PRINTFLIKE(2, 3);
  230. void
  231. init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
  232. uint32_t timezone_offset)
  233. {
  234. thiszone = timezone_offset;
  235. init_addrtoname(ndo, localnet, mask);
  236. init_checksum();
  237. }
  238. if_printer
  239. lookup_printer(int type)
  240. {
  241. const struct printer *p;
  242. for (p = printers; p->f; ++p)
  243. if (type == p->type)
  244. return p->f;
  245. #if defined(DLT_USER2) && defined(DLT_PKTAP)
  246. /*
  247. * Apple incorrectly chose to use DLT_USER2 for their PKTAP
  248. * header.
  249. *
  250. * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
  251. * based OSes or the same value as LINKTYPE_PKTAP as it is on
  252. * other OSes, to LINKTYPE_PKTAP, so files written with
  253. * this version of libpcap for a DLT_PKTAP capture have a link-
  254. * layer header type of LINKTYPE_PKTAP.
  255. *
  256. * However, files written on OS X Mavericks for a DLT_PKTAP
  257. * capture have a link-layer header type of LINKTYPE_USER2.
  258. * If we don't have a printer for DLT_USER2, and type is
  259. * DLT_USER2, we look up the printer for DLT_PKTAP and use
  260. * that.
  261. */
  262. if (type == DLT_USER2) {
  263. for (p = printers; p->f; ++p)
  264. if (DLT_PKTAP == p->type)
  265. return p->f;
  266. }
  267. #endif
  268. return NULL;
  269. /* NOTREACHED */
  270. }
  271. int
  272. has_printer(int type)
  273. {
  274. return (lookup_printer(type) != NULL);
  275. }
  276. if_printer
  277. get_if_printer(netdissect_options *ndo, int type)
  278. {
  279. const char *dltname;
  280. if_printer printer;
  281. printer = lookup_printer(type);
  282. if (printer == NULL) {
  283. dltname = pcap_datalink_val_to_name(type);
  284. if (dltname != NULL)
  285. (*ndo->ndo_error)(ndo,
  286. "packet printing is not supported for link type %s: use -w",
  287. dltname);
  288. else
  289. (*ndo->ndo_error)(ndo,
  290. "packet printing is not supported for link type %d: use -w", type);
  291. }
  292. return printer;
  293. }
  294. void
  295. pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
  296. const u_char *sp, u_int packets_captured)
  297. {
  298. u_int hdrlen;
  299. if(ndo->ndo_packet_number)
  300. ND_PRINT((ndo, "%5u ", packets_captured));
  301. ts_print(ndo, &h->ts);
  302. /*
  303. * Some printers want to check that they're not walking off the
  304. * end of the packet.
  305. * Rather than pass it all the way down, we set this member
  306. * of the netdissect_options structure.
  307. */
  308. ndo->ndo_snapend = sp + h->caplen;
  309. hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
  310. /*
  311. * Restore the original snapend, as a printer might have
  312. * changed it.
  313. */
  314. ndo->ndo_snapend = sp + h->caplen;
  315. if (ndo->ndo_Xflag) {
  316. /*
  317. * Print the raw packet data in hex and ASCII.
  318. */
  319. if (ndo->ndo_Xflag > 1) {
  320. /*
  321. * Include the link-layer header.
  322. */
  323. hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
  324. } else {
  325. /*
  326. * Don't include the link-layer header - and if
  327. * we have nothing past the link-layer header,
  328. * print nothing.
  329. */
  330. if (h->caplen > hdrlen)
  331. hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
  332. h->caplen - hdrlen);
  333. }
  334. } else if (ndo->ndo_xflag) {
  335. /*
  336. * Print the raw packet data in hex.
  337. */
  338. if (ndo->ndo_xflag > 1) {
  339. /*
  340. * Include the link-layer header.
  341. */
  342. hex_print(ndo, "\n\t", sp, h->caplen);
  343. } else {
  344. /*
  345. * Don't include the link-layer header - and if
  346. * we have nothing past the link-layer header,
  347. * print nothing.
  348. */
  349. if (h->caplen > hdrlen)
  350. hex_print(ndo, "\n\t", sp + hdrlen,
  351. h->caplen - hdrlen);
  352. }
  353. } else if (ndo->ndo_Aflag) {
  354. /*
  355. * Print the raw packet data in ASCII.
  356. */
  357. if (ndo->ndo_Aflag > 1) {
  358. /*
  359. * Include the link-layer header.
  360. */
  361. ascii_print(ndo, sp, h->caplen);
  362. } else {
  363. /*
  364. * Don't include the link-layer header - and if
  365. * we have nothing past the link-layer header,
  366. * print nothing.
  367. */
  368. if (h->caplen > hdrlen)
  369. ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
  370. }
  371. }
  372. ND_PRINT((ndo, "\n"));
  373. }
  374. /*
  375. * By default, print the specified data out in hex and ASCII.
  376. */
  377. static void
  378. ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
  379. {
  380. hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
  381. }
  382. /* VARARGS */
  383. static void
  384. ndo_error(netdissect_options *ndo, const char *fmt, ...)
  385. {
  386. va_list ap;
  387. if(ndo->program_name)
  388. (void)fprintf(stderr, "%s: ", ndo->program_name);
  389. va_start(ap, fmt);
  390. (void)vfprintf(stderr, fmt, ap);
  391. va_end(ap);
  392. if (*fmt) {
  393. fmt += strlen(fmt);
  394. if (fmt[-1] != '\n')
  395. (void)fputc('\n', stderr);
  396. }
  397. nd_cleanup();
  398. exit(1);
  399. /* NOTREACHED */
  400. }
  401. /* VARARGS */
  402. static void
  403. ndo_warning(netdissect_options *ndo, const char *fmt, ...)
  404. {
  405. va_list ap;
  406. if(ndo->program_name)
  407. (void)fprintf(stderr, "%s: ", ndo->program_name);
  408. (void)fprintf(stderr, "WARNING: ");
  409. va_start(ap, fmt);
  410. (void)vfprintf(stderr, fmt, ap);
  411. va_end(ap);
  412. if (*fmt) {
  413. fmt += strlen(fmt);
  414. if (fmt[-1] != '\n')
  415. (void)fputc('\n', stderr);
  416. }
  417. }
  418. static int
  419. ndo_printf(netdissect_options *ndo, const char *fmt, ...)
  420. {
  421. va_list args;
  422. int ret;
  423. va_start(args, fmt);
  424. ret = vfprintf(stdout, fmt, args);
  425. va_end(args);
  426. if (ret < 0)
  427. ndo_error(ndo, "Unable to write output: %s", pcap_strerror(errno));
  428. return (ret);
  429. }
  430. void
  431. ndo_set_function_pointers(netdissect_options *ndo)
  432. {
  433. ndo->ndo_default_print=ndo_default_print;
  434. ndo->ndo_printf=ndo_printf;
  435. ndo->ndo_error=ndo_error;
  436. ndo->ndo_warning=ndo_warning;
  437. }
  438. /*
  439. * Local Variables:
  440. * c-style: whitesmith
  441. * c-basic-offset: 8
  442. * End:
  443. */