sf-pcap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. * sf-pcap.c - libpcap-file-format-specific code from savefile.c
  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. #include "pcap-common.h"
  45. #ifdef HAVE_OS_PROTO_H
  46. #include "os-proto.h"
  47. #endif
  48. #include "sf-pcap.h"
  49. /*
  50. * Setting O_BINARY on DOS/Windows is a bit tricky
  51. */
  52. #if defined(_WIN32)
  53. #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
  54. #elif defined(MSDOS)
  55. #if defined(__HIGHC__)
  56. #define SET_BINMODE(f) setmode(f, O_BINARY)
  57. #else
  58. #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
  59. #endif
  60. #endif
  61. /*
  62. * Standard libpcap format.
  63. */
  64. #define TCPDUMP_MAGIC 0xa1b2c3d4
  65. /*
  66. * Alexey Kuznetzov's modified libpcap format.
  67. */
  68. #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
  69. /*
  70. * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
  71. * for another modified format.
  72. */
  73. #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
  74. /*
  75. * Navtel Communcations' format, with nanosecond timestamps,
  76. * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
  77. */
  78. #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
  79. /*
  80. * Normal libpcap format, except for seconds/nanoseconds timestamps,
  81. * as per a request by Ulf Lamping <ulf.lamping@web.de>
  82. */
  83. #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
  84. /*
  85. * Mechanism for storing information about a capture in the upper
  86. * 6 bits of a linktype value in a capture file.
  87. *
  88. * LT_LINKTYPE_EXT(x) extracts the additional information.
  89. *
  90. * The rest of the bits are for a value describing the link-layer
  91. * value. LT_LINKTYPE(x) extracts that value.
  92. */
  93. #define LT_LINKTYPE(x) ((x) & 0x03FFFFFF)
  94. #define LT_LINKTYPE_EXT(x) ((x) & 0xFC000000)
  95. static int pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **datap);
  96. /*
  97. * Private data for reading pcap savefiles.
  98. */
  99. typedef enum {
  100. NOT_SWAPPED,
  101. SWAPPED,
  102. MAYBE_SWAPPED
  103. } swapped_type_t;
  104. typedef enum {
  105. PASS_THROUGH,
  106. SCALE_UP,
  107. SCALE_DOWN
  108. } tstamp_scale_type_t;
  109. struct pcap_sf {
  110. size_t hdrsize;
  111. swapped_type_t lengths_swapped;
  112. tstamp_scale_type_t scale_type;
  113. };
  114. /*
  115. * Check whether this is a pcap savefile and, if it is, extract the
  116. * relevant information from the header.
  117. */
  118. pcap_t *
  119. pcap_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
  120. int *err)
  121. {
  122. struct pcap_file_header hdr;
  123. size_t amt_read;
  124. pcap_t *p;
  125. int swapped = 0;
  126. struct pcap_sf *ps;
  127. /*
  128. * Assume no read errors.
  129. */
  130. *err = 0;
  131. /*
  132. * Check whether the first 4 bytes of the file are the magic
  133. * number for a pcap savefile, or for a byte-swapped pcap
  134. * savefile.
  135. */
  136. if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC &&
  137. magic != NSEC_TCPDUMP_MAGIC) {
  138. magic = SWAPLONG(magic);
  139. if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC &&
  140. magic != NSEC_TCPDUMP_MAGIC)
  141. return (NULL); /* nope */
  142. swapped = 1;
  143. }
  144. /*
  145. * They are. Put the magic number in the header, and read
  146. * the rest of the header.
  147. */
  148. hdr.magic = magic;
  149. amt_read = fread(((char *)&hdr) + sizeof hdr.magic, 1,
  150. sizeof(hdr) - sizeof(hdr.magic), fp);
  151. if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) {
  152. if (ferror(fp)) {
  153. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  154. errno, "error reading dump file");
  155. } else {
  156. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  157. "truncated dump file; tried to read %lu file header bytes, only got %lu",
  158. (unsigned long)sizeof(hdr),
  159. (unsigned long)amt_read);
  160. }
  161. *err = 1;
  162. return (NULL);
  163. }
  164. /*
  165. * If it's a byte-swapped capture file, byte-swap the header.
  166. */
  167. if (swapped) {
  168. hdr.version_major = SWAPSHORT(hdr.version_major);
  169. hdr.version_minor = SWAPSHORT(hdr.version_minor);
  170. hdr.thiszone = SWAPLONG(hdr.thiszone);
  171. hdr.sigfigs = SWAPLONG(hdr.sigfigs);
  172. hdr.snaplen = SWAPLONG(hdr.snaplen);
  173. hdr.linktype = SWAPLONG(hdr.linktype);
  174. }
  175. if (hdr.version_major < PCAP_VERSION_MAJOR) {
  176. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  177. "archaic pcap savefile format");
  178. *err = 1;
  179. return (NULL);
  180. }
  181. /*
  182. * currently only versions 2.[0-4] are supported with
  183. * the exception of 543.0 for DG/UX tcpdump.
  184. */
  185. if (! ((hdr.version_major == PCAP_VERSION_MAJOR &&
  186. hdr.version_minor <= PCAP_VERSION_MINOR) ||
  187. (hdr.version_major == 543 &&
  188. hdr.version_minor == 0))) {
  189. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  190. "unsupported pcap savefile version %u.%u",
  191. hdr.version_major, hdr.version_minor);
  192. *err = 1;
  193. return NULL;
  194. }
  195. /*
  196. * OK, this is a good pcap file.
  197. * Allocate a pcap_t for it.
  198. */
  199. p = pcap_open_offline_common(errbuf, sizeof (struct pcap_sf));
  200. if (p == NULL) {
  201. /* Allocation failed. */
  202. *err = 1;
  203. return (NULL);
  204. }
  205. p->swapped = swapped;
  206. p->version_major = hdr.version_major;
  207. p->version_minor = hdr.version_minor;
  208. p->tzoff = hdr.thiszone;
  209. p->snapshot = hdr.snaplen;
  210. if (p->snapshot <= 0) {
  211. /*
  212. * Bogus snapshot length; use the maximum for this
  213. * link-layer type as a fallback.
  214. *
  215. * XXX - the only reason why snapshot is signed is
  216. * that pcap_snapshot() returns an int, not an
  217. * unsigned int.
  218. */
  219. p->snapshot = max_snaplen_for_dlt(hdr.linktype);
  220. }
  221. p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
  222. p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
  223. p->next_packet_op = pcap_next_packet;
  224. ps = p->priv;
  225. p->opt.tstamp_precision = precision;
  226. /*
  227. * Will we need to scale the timestamps to match what the
  228. * user wants?
  229. */
  230. switch (precision) {
  231. case PCAP_TSTAMP_PRECISION_MICRO:
  232. if (magic == NSEC_TCPDUMP_MAGIC) {
  233. /*
  234. * The file has nanoseconds, the user
  235. * wants microseconds; scale the
  236. * precision down.
  237. */
  238. ps->scale_type = SCALE_DOWN;
  239. } else {
  240. /*
  241. * The file has microseconds, the
  242. * user wants microseconds; nothing to do.
  243. */
  244. ps->scale_type = PASS_THROUGH;
  245. }
  246. break;
  247. case PCAP_TSTAMP_PRECISION_NANO:
  248. if (magic == NSEC_TCPDUMP_MAGIC) {
  249. /*
  250. * The file has nanoseconds, the
  251. * user wants nanoseconds; nothing to do.
  252. */
  253. ps->scale_type = PASS_THROUGH;
  254. } else {
  255. /*
  256. * The file has microoseconds, the user
  257. * wants nanoseconds; scale the
  258. * precision up.
  259. */
  260. ps->scale_type = SCALE_UP;
  261. }
  262. break;
  263. default:
  264. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  265. "unknown time stamp resolution %u", precision);
  266. free(p);
  267. *err = 1;
  268. return (NULL);
  269. }
  270. /*
  271. * We interchanged the caplen and len fields at version 2.3,
  272. * in order to match the bpf header layout. But unfortunately
  273. * some files were written with version 2.3 in their headers
  274. * but without the interchanged fields.
  275. *
  276. * In addition, DG/UX tcpdump writes out files with a version
  277. * number of 543.0, and with the caplen and len fields in the
  278. * pre-2.3 order.
  279. */
  280. switch (hdr.version_major) {
  281. case 2:
  282. if (hdr.version_minor < 3)
  283. ps->lengths_swapped = SWAPPED;
  284. else if (hdr.version_minor == 3)
  285. ps->lengths_swapped = MAYBE_SWAPPED;
  286. else
  287. ps->lengths_swapped = NOT_SWAPPED;
  288. break;
  289. case 543:
  290. ps->lengths_swapped = SWAPPED;
  291. break;
  292. default:
  293. ps->lengths_swapped = NOT_SWAPPED;
  294. break;
  295. }
  296. if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
  297. /*
  298. * XXX - the patch that's in some versions of libpcap
  299. * changes the packet header but not the magic number,
  300. * and some other versions with this magic number have
  301. * some extra debugging information in the packet header;
  302. * we'd have to use some hacks^H^H^H^H^Hheuristics to
  303. * detect those variants.
  304. *
  305. * Ethereal does that, but it does so by trying to read
  306. * the first two packets of the file with each of the
  307. * record header formats. That currently means it seeks
  308. * backwards and retries the reads, which doesn't work
  309. * on pipes. We want to be able to read from a pipe, so
  310. * that strategy won't work; we'd have to buffer some
  311. * data ourselves and read from that buffer in order to
  312. * make that work.
  313. */
  314. ps->hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
  315. if (p->linktype == DLT_EN10MB) {
  316. /*
  317. * This capture might have been done in raw mode
  318. * or cooked mode.
  319. *
  320. * If it was done in cooked mode, p->snapshot was
  321. * passed to recvfrom() as the buffer size, meaning
  322. * that the most packet data that would be copied
  323. * would be p->snapshot. However, a faked Ethernet
  324. * header would then have been added to it, so the
  325. * most data that would be in a packet in the file
  326. * would be p->snapshot + 14.
  327. *
  328. * We can't easily tell whether the capture was done
  329. * in raw mode or cooked mode, so we'll assume it was
  330. * cooked mode, and add 14 to the snapshot length.
  331. * That means that, for a raw capture, the snapshot
  332. * length will be misleading if you use it to figure
  333. * out why a capture doesn't have all the packet data,
  334. * but there's not much we can do to avoid that.
  335. */
  336. p->snapshot += 14;
  337. }
  338. } else
  339. ps->hdrsize = sizeof(struct pcap_sf_pkthdr);
  340. /*
  341. * Allocate a buffer for the packet data.
  342. * Choose the minimum of the file's snapshot length and 2K bytes;
  343. * that should be enough for most network packets - we'll grow it
  344. * if necessary. That way, we don't allocate a huge chunk of
  345. * memory just because there's a huge snapshot length, as the
  346. * snapshot length might be larger than the size of the largest
  347. * packet.
  348. */
  349. p->bufsize = p->snapshot;
  350. if (p->bufsize > 2048)
  351. p->bufsize = 2048;
  352. p->buffer = malloc(p->bufsize);
  353. if (p->buffer == NULL) {
  354. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  355. free(p);
  356. *err = 1;
  357. return (NULL);
  358. }
  359. p->cleanup_op = sf_cleanup;
  360. return (p);
  361. }
  362. /*
  363. * Grow the packet buffer to the specified size.
  364. */
  365. static int
  366. grow_buffer(pcap_t *p, u_int bufsize)
  367. {
  368. void *bigger_buffer;
  369. bigger_buffer = realloc(p->buffer, bufsize);
  370. if (bigger_buffer == NULL) {
  371. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  372. return (0);
  373. }
  374. p->buffer = bigger_buffer;
  375. p->bufsize = bufsize;
  376. return (1);
  377. }
  378. /*
  379. * Read and return the next packet from the savefile. Return the header
  380. * in hdr and a pointer to the contents in data. Return 0 on success, 1
  381. * if there were no more packets, and -1 on an error.
  382. */
  383. static int
  384. pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
  385. {
  386. struct pcap_sf *ps = p->priv;
  387. struct pcap_sf_patched_pkthdr sf_hdr;
  388. FILE *fp = p->rfile;
  389. size_t amt_read;
  390. bpf_u_int32 t;
  391. /*
  392. * Read the packet header; the structure we use as a buffer
  393. * is the longer structure for files generated by the patched
  394. * libpcap, but if the file has the magic number for an
  395. * unpatched libpcap we only read as many bytes as the regular
  396. * header has.
  397. */
  398. amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp);
  399. if (amt_read != ps->hdrsize) {
  400. if (ferror(fp)) {
  401. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  402. errno, "error reading dump file");
  403. return (-1);
  404. } else {
  405. if (amt_read != 0) {
  406. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  407. "truncated dump file; tried to read %lu header bytes, only got %lu",
  408. (unsigned long)ps->hdrsize,
  409. (unsigned long)amt_read);
  410. return (-1);
  411. }
  412. /* EOF */
  413. return (1);
  414. }
  415. }
  416. if (p->swapped) {
  417. /* these were written in opposite byte order */
  418. hdr->caplen = SWAPLONG(sf_hdr.caplen);
  419. hdr->len = SWAPLONG(sf_hdr.len);
  420. hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
  421. hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
  422. } else {
  423. hdr->caplen = sf_hdr.caplen;
  424. hdr->len = sf_hdr.len;
  425. hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
  426. hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
  427. }
  428. switch (ps->scale_type) {
  429. case PASS_THROUGH:
  430. /*
  431. * Just pass the time stamp through.
  432. */
  433. break;
  434. case SCALE_UP:
  435. /*
  436. * File has microseconds, user wants nanoseconds; convert
  437. * it.
  438. */
  439. hdr->ts.tv_usec = hdr->ts.tv_usec * 1000;
  440. break;
  441. case SCALE_DOWN:
  442. /*
  443. * File has nanoseconds, user wants microseconds; convert
  444. * it.
  445. */
  446. hdr->ts.tv_usec = hdr->ts.tv_usec / 1000;
  447. break;
  448. }
  449. /* Swap the caplen and len fields, if necessary. */
  450. switch (ps->lengths_swapped) {
  451. case NOT_SWAPPED:
  452. break;
  453. case MAYBE_SWAPPED:
  454. if (hdr->caplen <= hdr->len) {
  455. /*
  456. * The captured length is <= the actual length,
  457. * so presumably they weren't swapped.
  458. */
  459. break;
  460. }
  461. /* FALLTHROUGH */
  462. case SWAPPED:
  463. t = hdr->caplen;
  464. hdr->caplen = hdr->len;
  465. hdr->len = t;
  466. break;
  467. }
  468. /*
  469. * Is the packet bigger than we consider sane?
  470. */
  471. if (hdr->caplen > max_snaplen_for_dlt(p->linktype)) {
  472. /*
  473. * Yes. This may be a damaged or fuzzed file.
  474. *
  475. * Is it bigger than the snapshot length?
  476. * (We don't treat that as an error if it's not
  477. * bigger than the maximum we consider sane; see
  478. * below.)
  479. */
  480. if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  481. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  482. "invalid packet capture length %u, bigger than "
  483. "snaplen of %d", hdr->caplen, p->snapshot);
  484. } else {
  485. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  486. "invalid packet capture length %u, bigger than "
  487. "maximum of %u", hdr->caplen,
  488. max_snaplen_for_dlt(p->linktype));
  489. }
  490. return (-1);
  491. }
  492. if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  493. /*
  494. * The packet is bigger than the snapshot length
  495. * for this file.
  496. *
  497. * This can happen due to Solaris 2.3 systems tripping
  498. * over the BUFMOD problem and not setting the snapshot
  499. * length correctly in the savefile header.
  500. *
  501. * libpcap 0.4 and later on Solaris 2.3 should set the
  502. * snapshot length correctly in the pcap file header,
  503. * even though they don't set a snapshot length in bufmod
  504. * (the buggy bufmod chops off the *beginning* of the
  505. * packet if a snapshot length is specified); they should
  506. * also reduce the captured length, as supplied to the
  507. * per-packet callback, to the snapshot length if it's
  508. * greater than the snapshot length, so the code using
  509. * libpcap should see the packet cut off at the snapshot
  510. * length, even though the full packet is copied up to
  511. * userland.
  512. *
  513. * However, perhaps some versions of libpcap failed to
  514. * set the snapshot length currectly in the file header
  515. * or the per-packet header, or perhaps this is a
  516. * corrupted safefile or a savefile built/modified by a
  517. * fuzz tester, so we check anyway. We grow the buffer
  518. * to be big enough for the snapshot length, read up
  519. * to the snapshot length, discard the rest of the
  520. * packet, and report the snapshot length as the captured
  521. * length; we don't want to hand our caller a packet
  522. * bigger than the snapshot length, because they might
  523. * be assuming they'll never be handed such a packet,
  524. * and might copy the packet into a snapshot-length-
  525. * sized buffer, assuming it'll fit.
  526. */
  527. size_t bytes_to_discard;
  528. size_t bytes_to_read, bytes_read;
  529. char discard_buf[4096];
  530. if (hdr->caplen > p->bufsize) {
  531. /*
  532. * Grow the buffer to the snapshot length.
  533. */
  534. if (!grow_buffer(p, p->snapshot))
  535. return (-1);
  536. }
  537. /*
  538. * Read the first p->snapshot bytes into the buffer.
  539. */
  540. amt_read = fread(p->buffer, 1, p->snapshot, fp);
  541. if (amt_read != (bpf_u_int32)p->snapshot) {
  542. if (ferror(fp)) {
  543. pcap_fmt_errmsg_for_errno(p->errbuf,
  544. PCAP_ERRBUF_SIZE, errno,
  545. "error reading dump file");
  546. } else {
  547. /*
  548. * Yes, this uses hdr->caplen; technically,
  549. * it's true, because we would try to read
  550. * and discard the rest of those bytes, and
  551. * that would fail because we got EOF before
  552. * the read finished.
  553. */
  554. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  555. "truncated dump file; tried to read %u captured bytes, only got %lu",
  556. p->snapshot, (unsigned long)amt_read);
  557. }
  558. return (-1);
  559. }
  560. /*
  561. * Now read and discard what's left.
  562. */
  563. bytes_to_discard = hdr->caplen - p->snapshot;
  564. bytes_read = amt_read;
  565. while (bytes_to_discard != 0) {
  566. bytes_to_read = bytes_to_discard;
  567. if (bytes_to_read > sizeof (discard_buf))
  568. bytes_to_read = sizeof (discard_buf);
  569. amt_read = fread(discard_buf, 1, bytes_to_read, fp);
  570. bytes_read += amt_read;
  571. if (amt_read != bytes_to_read) {
  572. if (ferror(fp)) {
  573. pcap_fmt_errmsg_for_errno(p->errbuf,
  574. PCAP_ERRBUF_SIZE, errno,
  575. "error reading dump file");
  576. } else {
  577. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  578. "truncated dump file; tried to read %u captured bytes, only got %lu",
  579. hdr->caplen, (unsigned long)bytes_read);
  580. }
  581. return (-1);
  582. }
  583. bytes_to_discard -= amt_read;
  584. }
  585. /*
  586. * Adjust caplen accordingly, so we don't get confused later
  587. * as to how many bytes we have to play with.
  588. */
  589. hdr->caplen = p->snapshot;
  590. } else {
  591. if (hdr->caplen > p->bufsize) {
  592. /*
  593. * Grow the buffer to the next power of 2, or
  594. * the snaplen, whichever is lower.
  595. */
  596. u_int new_bufsize;
  597. new_bufsize = hdr->caplen;
  598. /*
  599. * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  600. */
  601. new_bufsize--;
  602. new_bufsize |= new_bufsize >> 1;
  603. new_bufsize |= new_bufsize >> 2;
  604. new_bufsize |= new_bufsize >> 4;
  605. new_bufsize |= new_bufsize >> 8;
  606. new_bufsize |= new_bufsize >> 16;
  607. new_bufsize++;
  608. if (new_bufsize > (u_int)p->snapshot)
  609. new_bufsize = p->snapshot;
  610. if (!grow_buffer(p, new_bufsize))
  611. return (-1);
  612. }
  613. /* read the packet itself */
  614. amt_read = fread(p->buffer, 1, hdr->caplen, fp);
  615. if (amt_read != hdr->caplen) {
  616. if (ferror(fp)) {
  617. pcap_fmt_errmsg_for_errno(p->errbuf,
  618. PCAP_ERRBUF_SIZE, errno,
  619. "error reading dump file");
  620. } else {
  621. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  622. "truncated dump file; tried to read %u captured bytes, only got %lu",
  623. hdr->caplen, (unsigned long)amt_read);
  624. }
  625. return (-1);
  626. }
  627. }
  628. *data = p->buffer;
  629. if (p->swapped)
  630. swap_pseudo_headers(p->linktype, hdr, *data);
  631. return (0);
  632. }
  633. static int
  634. sf_write_header(pcap_t *p, FILE *fp, int linktype, int thiszone, int snaplen)
  635. {
  636. struct pcap_file_header hdr;
  637. hdr.magic = p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? NSEC_TCPDUMP_MAGIC : TCPDUMP_MAGIC;
  638. hdr.version_major = PCAP_VERSION_MAJOR;
  639. hdr.version_minor = PCAP_VERSION_MINOR;
  640. hdr.thiszone = thiszone;
  641. hdr.snaplen = snaplen;
  642. hdr.sigfigs = 0;
  643. hdr.linktype = linktype;
  644. if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
  645. return (-1);
  646. return (0);
  647. }
  648. /*
  649. * Output a packet to the initialized dump file.
  650. */
  651. void
  652. pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
  653. {
  654. register FILE *f;
  655. struct pcap_sf_pkthdr sf_hdr;
  656. f = (FILE *)user;
  657. sf_hdr.ts.tv_sec = h->ts.tv_sec;
  658. sf_hdr.ts.tv_usec = h->ts.tv_usec;
  659. sf_hdr.caplen = h->caplen;
  660. sf_hdr.len = h->len;
  661. /* XXX we should check the return status */
  662. (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
  663. (void)fwrite(sp, h->caplen, 1, f);
  664. }
  665. static pcap_dumper_t *
  666. pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
  667. {
  668. #if defined(_WIN32) || defined(MSDOS)
  669. /*
  670. * If we're writing to the standard output, put it in binary
  671. * mode, as savefiles are binary files.
  672. *
  673. * Otherwise, we turn off buffering.
  674. * XXX - why? And why not on the standard output?
  675. */
  676. if (f == stdout)
  677. SET_BINMODE(f);
  678. else
  679. setvbuf(f, NULL, _IONBF, 0);
  680. #endif
  681. if (sf_write_header(p, f, linktype, p->tzoff, p->snapshot) == -1) {
  682. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  683. errno, "Can't write to %s", fname);
  684. if (f != stdout)
  685. (void)fclose(f);
  686. return (NULL);
  687. }
  688. return ((pcap_dumper_t *)f);
  689. }
  690. /*
  691. * Initialize so that sf_write() will output to the file named 'fname'.
  692. */
  693. pcap_dumper_t *
  694. pcap_dump_open(pcap_t *p, const char *fname)
  695. {
  696. FILE *f;
  697. int linktype;
  698. /*
  699. * If this pcap_t hasn't been activated, it doesn't have a
  700. * link-layer type, so we can't use it.
  701. */
  702. if (!p->activated) {
  703. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  704. "%s: not-yet-activated pcap_t passed to pcap_dump_open",
  705. fname);
  706. return (NULL);
  707. }
  708. linktype = dlt_to_linktype(p->linktype);
  709. if (linktype == -1) {
  710. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  711. "%s: link-layer type %d isn't supported in savefiles",
  712. fname, p->linktype);
  713. return (NULL);
  714. }
  715. linktype |= p->linktype_ext;
  716. if (fname == NULL) {
  717. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  718. "A null pointer was supplied as the file name");
  719. return NULL;
  720. }
  721. if (fname[0] == '-' && fname[1] == '\0') {
  722. f = stdout;
  723. fname = "standard output";
  724. } else {
  725. /*
  726. * "b" is supported as of C90, so *all* UN*Xes should
  727. * support it, even though it does nothing. It's
  728. * required on Windows, as the file is a binary file
  729. * and must be written in binary mode.
  730. */
  731. f = fopen(fname, "wb");
  732. if (f == NULL) {
  733. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  734. errno, "%s", fname);
  735. return (NULL);
  736. }
  737. }
  738. return (pcap_setup_dump(p, linktype, f, fname));
  739. }
  740. /*
  741. * Initialize so that sf_write() will output to the given stream.
  742. */
  743. pcap_dumper_t *
  744. pcap_dump_fopen(pcap_t *p, FILE *f)
  745. {
  746. int linktype;
  747. linktype = dlt_to_linktype(p->linktype);
  748. if (linktype == -1) {
  749. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  750. "stream: link-layer type %d isn't supported in savefiles",
  751. p->linktype);
  752. return (NULL);
  753. }
  754. linktype |= p->linktype_ext;
  755. return (pcap_setup_dump(p, linktype, f, "stream"));
  756. }
  757. pcap_dumper_t *
  758. pcap_dump_open_append(pcap_t *p, const char *fname)
  759. {
  760. FILE *f;
  761. int linktype;
  762. size_t amt_read;
  763. struct pcap_file_header ph;
  764. linktype = dlt_to_linktype(p->linktype);
  765. if (linktype == -1) {
  766. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  767. "%s: link-layer type %d isn't supported in savefiles",
  768. fname, linktype);
  769. return (NULL);
  770. }
  771. if (fname == NULL) {
  772. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  773. "A null pointer was supplied as the file name");
  774. return NULL;
  775. }
  776. if (fname[0] == '-' && fname[1] == '\0')
  777. return (pcap_setup_dump(p, linktype, stdout, "standard output"));
  778. /*
  779. * "b" is supported as of C90, so *all* UN*Xes should support it,
  780. * even though it does nothing. It's required on Windows, as the
  781. * file is a binary file and must be read in binary mode.
  782. */
  783. f = fopen(fname, "rb+");
  784. if (f == NULL) {
  785. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  786. errno, "%s", fname);
  787. return (NULL);
  788. }
  789. /*
  790. * Try to read a pcap header.
  791. */
  792. amt_read = fread(&ph, 1, sizeof (ph), f);
  793. if (amt_read != sizeof (ph)) {
  794. if (ferror(f)) {
  795. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  796. errno, "%s", fname);
  797. fclose(f);
  798. return (NULL);
  799. } else if (feof(f) && amt_read > 0) {
  800. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  801. "%s: truncated pcap file header", fname);
  802. fclose(f);
  803. return (NULL);
  804. }
  805. }
  806. #if defined(_WIN32) || defined(MSDOS)
  807. /*
  808. * We turn off buffering.
  809. * XXX - why? And why not on the standard output?
  810. */
  811. setvbuf(f, NULL, _IONBF, 0);
  812. #endif
  813. /*
  814. * If a header is already present and:
  815. *
  816. * it's not for a pcap file of the appropriate resolution
  817. * and the right byte order for this machine;
  818. *
  819. * the link-layer header types don't match;
  820. *
  821. * the snapshot lengths don't match;
  822. *
  823. * return an error.
  824. */
  825. if (amt_read > 0) {
  826. /*
  827. * A header is already present.
  828. * Do the checks.
  829. */
  830. switch (ph.magic) {
  831. case TCPDUMP_MAGIC:
  832. if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_MICRO) {
  833. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  834. "%s: different time stamp precision, cannot append to file", fname);
  835. fclose(f);
  836. return (NULL);
  837. }
  838. break;
  839. case NSEC_TCPDUMP_MAGIC:
  840. if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_NANO) {
  841. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  842. "%s: different time stamp precision, cannot append to file", fname);
  843. fclose(f);
  844. return (NULL);
  845. }
  846. break;
  847. case SWAPLONG(TCPDUMP_MAGIC):
  848. case SWAPLONG(NSEC_TCPDUMP_MAGIC):
  849. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  850. "%s: different byte order, cannot append to file", fname);
  851. fclose(f);
  852. return (NULL);
  853. case KUZNETZOV_TCPDUMP_MAGIC:
  854. case SWAPLONG(KUZNETZOV_TCPDUMP_MAGIC):
  855. case NAVTEL_TCPDUMP_MAGIC:
  856. case SWAPLONG(NAVTEL_TCPDUMP_MAGIC):
  857. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  858. "%s: not a pcap file to which we can append", fname);
  859. fclose(f);
  860. return (NULL);
  861. default:
  862. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  863. "%s: not a pcap file", fname);
  864. fclose(f);
  865. return (NULL);
  866. }
  867. /*
  868. * Good version?
  869. */
  870. if (ph.version_major != PCAP_VERSION_MAJOR ||
  871. ph.version_minor != PCAP_VERSION_MINOR) {
  872. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  873. "%s: version is %u.%u, cannot append to file", fname,
  874. ph.version_major, ph.version_minor);
  875. fclose(f);
  876. return (NULL);
  877. }
  878. if ((bpf_u_int32)linktype != ph.linktype) {
  879. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  880. "%s: different linktype, cannot append to file", fname);
  881. fclose(f);
  882. return (NULL);
  883. }
  884. if ((bpf_u_int32)p->snapshot != ph.snaplen) {
  885. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  886. "%s: different snaplen, cannot append to file", fname);
  887. fclose(f);
  888. return (NULL);
  889. }
  890. } else {
  891. /*
  892. * A header isn't present; attempt to write it.
  893. */
  894. if (sf_write_header(p, f, linktype, p->tzoff, p->snapshot) == -1) {
  895. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  896. errno, "Can't write to %s", fname);
  897. (void)fclose(f);
  898. return (NULL);
  899. }
  900. }
  901. /*
  902. * Start writing at the end of the file.
  903. */
  904. if (fseek(f, 0, SEEK_END) == -1) {
  905. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  906. errno, "Can't seek to end of %s", fname);
  907. (void)fclose(f);
  908. return (NULL);
  909. }
  910. return ((pcap_dumper_t *)f);
  911. }
  912. FILE *
  913. pcap_dump_file(pcap_dumper_t *p)
  914. {
  915. return ((FILE *)p);
  916. }
  917. long
  918. pcap_dump_ftell(pcap_dumper_t *p)
  919. {
  920. return (ftell((FILE *)p));
  921. }
  922. #if defined(HAVE_FSEEKO)
  923. /*
  924. * We have fseeko(), so we have ftello().
  925. * If we have large file support (files larger than 2^31-1 bytes),
  926. * ftello() will give us a current file position with more than 32
  927. * bits.
  928. */
  929. int64_t
  930. pcap_dump_ftell64(pcap_dumper_t *p)
  931. {
  932. return (ftello((FILE *)p));
  933. }
  934. #elif defined(_MSC_VER)
  935. /*
  936. * We have Visual Studio; we support only 2005 and later, so we have
  937. * _ftelli64().
  938. */
  939. int64_t
  940. pcap_dump_ftell64(pcap_dumper_t *p)
  941. {
  942. return (_ftelli64((FILE *)p));
  943. }
  944. #else
  945. /*
  946. * We don't have ftello() or _ftelli64(), so fall back on ftell().
  947. * Either long is 64 bits, in which case ftell() should suffice,
  948. * or this is probably an older 32-bit UN*X without large file
  949. * support, which means you'll probably get errors trying to
  950. * write files > 2^31-1, so it won't matter anyway.
  951. *
  952. * XXX - what about MinGW?
  953. */
  954. int64_t
  955. pcap_dump_ftell64(pcap_dumper_t *p)
  956. {
  957. return (ftell((FILE *)p));
  958. }
  959. #endif
  960. int
  961. pcap_dump_flush(pcap_dumper_t *p)
  962. {
  963. if (fflush((FILE *)p) == EOF)
  964. return (-1);
  965. else
  966. return (0);
  967. }
  968. void
  969. pcap_dump_close(pcap_dumper_t *p)
  970. {
  971. #ifdef notyet
  972. if (ferror((FILE *)p))
  973. return-an-error;
  974. /* XXX should check return from fclose() too */
  975. #endif
  976. (void)fclose((FILE *)p);
  977. }