savefile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  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. * savefile.c - supports offline use of tcpdump
  22. * Extraction/creation by Jeffrey Mogul, DECWRL
  23. * Modified by Steve McCanne, LBL.
  24. *
  25. * Used to save the received packet headers, after filtering, to
  26. * a file, and then read them later.
  27. * The first record in the file contains saved values for the machine
  28. * dependent values so we can print the dump file on any architecture.
  29. */
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. #include <pcap-types.h>
  34. #ifdef _WIN32
  35. #include <io.h>
  36. #include <fcntl.h>
  37. #endif /* _WIN32 */
  38. #include <errno.h>
  39. #include <memory.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "pcap-int.h"
  44. #ifdef HAVE_OS_PROTO_H
  45. #include "os-proto.h"
  46. #endif
  47. #include "sf-pcap.h"
  48. #include "sf-pcapng.h"
  49. #ifdef _WIN32
  50. /*
  51. * These aren't exported on Windows, because they would only work if both
  52. * WinPcap and the code using it were to use the Universal CRT; otherwise,
  53. * a FILE structure in WinPcap and a FILE structure in the code using it
  54. * could be different if they're using different versions of the C runtime.
  55. *
  56. * Instead, pcap/pcap.h defines them as macros that wrap the hopen versions,
  57. * with the wrappers calling _fileno() and _get_osfhandle() themselves,
  58. * so that they convert the appropriate CRT version's FILE structure to
  59. * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
  60. * and Win64 ABIs).
  61. */
  62. static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
  63. static pcap_t *pcap_fopen_offline(FILE *, char *);
  64. #endif
  65. /*
  66. * Setting O_BINARY on DOS/Windows is a bit tricky
  67. */
  68. #if defined(_WIN32)
  69. #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
  70. #elif defined(MSDOS)
  71. #if defined(__HIGHC__)
  72. #define SET_BINMODE(f) setmode(f, O_BINARY)
  73. #else
  74. #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
  75. #endif
  76. #endif
  77. static int
  78. sf_getnonblock(pcap_t *p _U_)
  79. {
  80. /*
  81. * This is a savefile, not a live capture file, so never say
  82. * it's in non-blocking mode.
  83. */
  84. return (0);
  85. }
  86. static int
  87. sf_setnonblock(pcap_t *p, int nonblock _U_)
  88. {
  89. /*
  90. * This is a savefile, not a live capture file, so reject
  91. * requests to put it in non-blocking mode. (If it's a
  92. * pipe, it could be put in non-blocking mode, but that
  93. * would significantly complicate the code to read packets,
  94. * as it would have to handle reading partial packets and
  95. * keeping the state of the read.)
  96. */
  97. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  98. "Savefiles cannot be put into non-blocking mode");
  99. return (-1);
  100. }
  101. static int
  102. sf_stats(pcap_t *p, struct pcap_stat *ps _U_)
  103. {
  104. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  105. "Statistics aren't available from savefiles");
  106. return (-1);
  107. }
  108. #ifdef _WIN32
  109. static struct pcap_stat *
  110. sf_stats_ex(pcap_t *p, int *size)
  111. {
  112. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  113. "Statistics aren't available from savefiles");
  114. return (NULL);
  115. }
  116. static int
  117. sf_setbuff(pcap_t *p, int dim)
  118. {
  119. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  120. "The kernel buffer size cannot be set while reading from a file");
  121. return (-1);
  122. }
  123. static int
  124. sf_setmode(pcap_t *p, int mode)
  125. {
  126. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  127. "impossible to set mode while reading from a file");
  128. return (-1);
  129. }
  130. static int
  131. sf_setmintocopy(pcap_t *p, int size)
  132. {
  133. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  134. "The mintocopy parameter cannot be set while reading from a file");
  135. return (-1);
  136. }
  137. static HANDLE
  138. sf_getevent(pcap_t *pcap)
  139. {
  140. (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
  141. "The read event cannot be retrieved while reading from a file");
  142. return (INVALID_HANDLE_VALUE);
  143. }
  144. static int
  145. sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
  146. size_t *lenp _U_)
  147. {
  148. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  149. "An OID get request cannot be performed on a file");
  150. return (PCAP_ERROR);
  151. }
  152. static int
  153. sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
  154. size_t *lenp _U_)
  155. {
  156. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  157. "An OID set request cannot be performed on a file");
  158. return (PCAP_ERROR);
  159. }
  160. static u_int
  161. sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
  162. {
  163. strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
  164. PCAP_ERRBUF_SIZE);
  165. return (0);
  166. }
  167. static int
  168. sf_setuserbuffer(pcap_t *p, int size)
  169. {
  170. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  171. "The user buffer cannot be set when reading from a file");
  172. return (-1);
  173. }
  174. static int
  175. sf_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
  176. {
  177. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  178. "Live packet dumping cannot be performed when reading from a file");
  179. return (-1);
  180. }
  181. static int
  182. sf_live_dump_ended(pcap_t *p, int sync)
  183. {
  184. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  185. "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
  186. return (-1);
  187. }
  188. static PAirpcapHandle
  189. sf_get_airpcap_handle(pcap_t *pcap)
  190. {
  191. return (NULL);
  192. }
  193. #endif
  194. static int
  195. sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
  196. {
  197. strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
  198. PCAP_ERRBUF_SIZE);
  199. return (-1);
  200. }
  201. /*
  202. * Set direction flag: Which packets do we accept on a forwarding
  203. * single device? IN, OUT or both?
  204. */
  205. static int
  206. sf_setdirection(pcap_t *p, pcap_direction_t d _U_)
  207. {
  208. pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  209. "Setting direction is not supported on savefiles");
  210. return (-1);
  211. }
  212. void
  213. sf_cleanup(pcap_t *p)
  214. {
  215. if (p->rfile != stdin)
  216. (void)fclose(p->rfile);
  217. if (p->buffer != NULL)
  218. free(p->buffer);
  219. pcap_freecode(&p->fcode);
  220. }
  221. pcap_t *
  222. pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
  223. char *errbuf)
  224. {
  225. FILE *fp;
  226. pcap_t *p;
  227. if (fname == NULL) {
  228. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  229. "A null pointer was supplied as the file name");
  230. return (NULL);
  231. }
  232. if (fname[0] == '-' && fname[1] == '\0')
  233. {
  234. fp = stdin;
  235. #if defined(_WIN32) || defined(MSDOS)
  236. /*
  237. * We're reading from the standard input, so put it in binary
  238. * mode, as savefiles are binary files.
  239. */
  240. SET_BINMODE(fp);
  241. #endif
  242. }
  243. else {
  244. /*
  245. * "b" is supported as of C90, so *all* UN*Xes should
  246. * support it, even though it does nothing. It's
  247. * required on Windows, as the file is a binary file
  248. * and must be read in binary mode.
  249. */
  250. fp = fopen(fname, "rb");
  251. if (fp == NULL) {
  252. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  253. errno, "%s", fname);
  254. return (NULL);
  255. }
  256. }
  257. p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
  258. if (p == NULL) {
  259. if (fp != stdin)
  260. fclose(fp);
  261. }
  262. return (p);
  263. }
  264. pcap_t *
  265. pcap_open_offline(const char *fname, char *errbuf)
  266. {
  267. return (pcap_open_offline_with_tstamp_precision(fname,
  268. PCAP_TSTAMP_PRECISION_MICRO, errbuf));
  269. }
  270. #ifdef _WIN32
  271. pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
  272. char *errbuf)
  273. {
  274. int fd;
  275. FILE *file;
  276. fd = _open_osfhandle(osfd, _O_RDONLY);
  277. if ( fd < 0 )
  278. {
  279. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  280. errno, "_open_osfhandle");
  281. return NULL;
  282. }
  283. file = _fdopen(fd, "rb");
  284. if ( file == NULL )
  285. {
  286. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  287. errno, "_fdopen");
  288. return NULL;
  289. }
  290. return pcap_fopen_offline_with_tstamp_precision(file, precision,
  291. errbuf);
  292. }
  293. pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
  294. {
  295. return pcap_hopen_offline_with_tstamp_precision(osfd,
  296. PCAP_TSTAMP_PRECISION_MICRO, errbuf);
  297. }
  298. #endif
  299. static pcap_t *(*check_headers[])(bpf_u_int32, FILE *, u_int, char *, int *) = {
  300. pcap_check_header,
  301. pcap_ng_check_header
  302. };
  303. #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
  304. #ifdef _WIN32
  305. static
  306. #endif
  307. pcap_t *
  308. pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
  309. char *errbuf)
  310. {
  311. register pcap_t *p;
  312. bpf_u_int32 magic;
  313. size_t amt_read;
  314. u_int i;
  315. int err;
  316. /*
  317. * Read the first 4 bytes of the file; the network analyzer dump
  318. * file formats we support (pcap and pcapng), and several other
  319. * formats we might support in the future (such as snoop, DOS and
  320. * Windows Sniffer, and Microsoft Network Monitor) all have magic
  321. * numbers that are unique in their first 4 bytes.
  322. */
  323. amt_read = fread((char *)&magic, 1, sizeof(magic), fp);
  324. if (amt_read != sizeof(magic)) {
  325. if (ferror(fp)) {
  326. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  327. errno, "error reading dump file");
  328. } else {
  329. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  330. "truncated dump file; tried to read %lu file header bytes, only got %lu",
  331. (unsigned long)sizeof(magic),
  332. (unsigned long)amt_read);
  333. }
  334. return (NULL);
  335. }
  336. /*
  337. * Try all file types.
  338. */
  339. for (i = 0; i < N_FILE_TYPES; i++) {
  340. p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
  341. if (p != NULL) {
  342. /* Yup, that's it. */
  343. goto found;
  344. }
  345. if (err) {
  346. /*
  347. * Error trying to read the header.
  348. */
  349. return (NULL);
  350. }
  351. }
  352. /*
  353. * Well, who knows what this mess is....
  354. */
  355. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
  356. return (NULL);
  357. found:
  358. p->rfile = fp;
  359. /* Padding only needed for live capture fcode */
  360. p->fddipad = 0;
  361. #if !defined(_WIN32) && !defined(MSDOS)
  362. /*
  363. * You can do "select()" and "poll()" on plain files on most
  364. * platforms, and should be able to do so on pipes.
  365. *
  366. * You can't do "select()" on anything other than sockets in
  367. * Windows, so, on Win32 systems, we don't have "selectable_fd".
  368. */
  369. p->selectable_fd = fileno(fp);
  370. #endif
  371. p->read_op = pcap_offline_read;
  372. p->inject_op = sf_inject;
  373. p->setfilter_op = install_bpf_program;
  374. p->setdirection_op = sf_setdirection;
  375. p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
  376. p->getnonblock_op = sf_getnonblock;
  377. p->setnonblock_op = sf_setnonblock;
  378. p->stats_op = sf_stats;
  379. #ifdef _WIN32
  380. p->stats_ex_op = sf_stats_ex;
  381. p->setbuff_op = sf_setbuff;
  382. p->setmode_op = sf_setmode;
  383. p->setmintocopy_op = sf_setmintocopy;
  384. p->getevent_op = sf_getevent;
  385. p->oid_get_request_op = sf_oid_get_request;
  386. p->oid_set_request_op = sf_oid_set_request;
  387. p->sendqueue_transmit_op = sf_sendqueue_transmit;
  388. p->setuserbuffer_op = sf_setuserbuffer;
  389. p->live_dump_op = sf_live_dump;
  390. p->live_dump_ended_op = sf_live_dump_ended;
  391. p->get_airpcap_handle_op = sf_get_airpcap_handle;
  392. #endif
  393. /*
  394. * For offline captures, the standard one-shot callback can
  395. * be used for pcap_next()/pcap_next_ex().
  396. */
  397. p->oneshot_callback = pcap_oneshot;
  398. /*
  399. * Savefiles never require special BPF code generation.
  400. */
  401. p->bpf_codegen_flags = 0;
  402. p->activated = 1;
  403. return (p);
  404. }
  405. #ifdef _WIN32
  406. static
  407. #endif
  408. pcap_t *
  409. pcap_fopen_offline(FILE *fp, char *errbuf)
  410. {
  411. return (pcap_fopen_offline_with_tstamp_precision(fp,
  412. PCAP_TSTAMP_PRECISION_MICRO, errbuf));
  413. }
  414. /*
  415. * Read packets from a capture file, and call the callback for each
  416. * packet.
  417. * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
  418. */
  419. int
  420. pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  421. {
  422. struct bpf_insn *fcode;
  423. int status = 0;
  424. int n = 0;
  425. u_char *data;
  426. while (status == 0) {
  427. struct pcap_pkthdr h;
  428. /*
  429. * Has "pcap_breakloop()" been called?
  430. * If so, return immediately - if we haven't read any
  431. * packets, clear the flag and return -2 to indicate
  432. * that we were told to break out of the loop, otherwise
  433. * leave the flag set, so that the *next* call will break
  434. * out of the loop without having read any packets, and
  435. * return the number of packets we've processed so far.
  436. */
  437. if (p->break_loop) {
  438. if (n == 0) {
  439. p->break_loop = 0;
  440. return (-2);
  441. } else
  442. return (n);
  443. }
  444. status = p->next_packet_op(p, &h, &data);
  445. if (status) {
  446. if (status == 1)
  447. return (0);
  448. return (status);
  449. }
  450. if ((fcode = p->fcode.bf_insns) == NULL ||
  451. bpf_filter(fcode, data, h.len, h.caplen)) {
  452. (*callback)(user, &h, data);
  453. if (++n >= cnt && cnt > 0)
  454. break;
  455. }
  456. }
  457. /*XXX this breaks semantics tcpslice expects */
  458. return (n);
  459. }