sf-pcapng.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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-pcapng.c - pcapng-file-format-specific code from savefile.c
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <pcap/pcap-inttypes.h>
  27. #include <errno.h>
  28. #include <memory.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "pcap-int.h"
  33. #include "pcap-common.h"
  34. #ifdef HAVE_OS_PROTO_H
  35. #include "os-proto.h"
  36. #endif
  37. #include "sf-pcapng.h"
  38. /*
  39. * Block types.
  40. */
  41. /*
  42. * Common part at the beginning of all blocks.
  43. */
  44. struct block_header {
  45. bpf_u_int32 block_type;
  46. bpf_u_int32 total_length;
  47. };
  48. /*
  49. * Common trailer at the end of all blocks.
  50. */
  51. struct block_trailer {
  52. bpf_u_int32 total_length;
  53. };
  54. /*
  55. * Common options.
  56. */
  57. #define OPT_ENDOFOPT 0 /* end of options */
  58. #define OPT_COMMENT 1 /* comment string */
  59. /*
  60. * Option header.
  61. */
  62. struct option_header {
  63. u_short option_code;
  64. u_short option_length;
  65. };
  66. /*
  67. * Structures for the part of each block type following the common
  68. * part.
  69. */
  70. /*
  71. * Section Header Block.
  72. */
  73. #define BT_SHB 0x0A0D0D0A
  74. struct section_header_block {
  75. bpf_u_int32 byte_order_magic;
  76. u_short major_version;
  77. u_short minor_version;
  78. uint64_t section_length;
  79. /* followed by options and trailer */
  80. };
  81. /*
  82. * Byte-order magic value.
  83. */
  84. #define BYTE_ORDER_MAGIC 0x1A2B3C4D
  85. /*
  86. * Current version number. If major_version isn't PCAP_NG_VERSION_MAJOR,
  87. * that means that this code can't read the file.
  88. */
  89. #define PCAP_NG_VERSION_MAJOR 1
  90. #define PCAP_NG_VERSION_MINOR 0
  91. /*
  92. * Interface Description Block.
  93. */
  94. #define BT_IDB 0x00000001
  95. struct interface_description_block {
  96. u_short linktype;
  97. u_short reserved;
  98. bpf_u_int32 snaplen;
  99. /* followed by options and trailer */
  100. };
  101. /*
  102. * Options in the IDB.
  103. */
  104. #define IF_NAME 2 /* interface name string */
  105. #define IF_DESCRIPTION 3 /* interface description string */
  106. #define IF_IPV4ADDR 4 /* interface's IPv4 address and netmask */
  107. #define IF_IPV6ADDR 5 /* interface's IPv6 address and prefix length */
  108. #define IF_MACADDR 6 /* interface's MAC address */
  109. #define IF_EUIADDR 7 /* interface's EUI address */
  110. #define IF_SPEED 8 /* interface's speed, in bits/s */
  111. #define IF_TSRESOL 9 /* interface's time stamp resolution */
  112. #define IF_TZONE 10 /* interface's time zone */
  113. #define IF_FILTER 11 /* filter used when capturing on interface */
  114. #define IF_OS 12 /* string OS on which capture on this interface was done */
  115. #define IF_FCSLEN 13 /* FCS length for this interface */
  116. #define IF_TSOFFSET 14 /* time stamp offset for this interface */
  117. /*
  118. * Enhanced Packet Block.
  119. */
  120. #define BT_EPB 0x00000006
  121. struct enhanced_packet_block {
  122. bpf_u_int32 interface_id;
  123. bpf_u_int32 timestamp_high;
  124. bpf_u_int32 timestamp_low;
  125. bpf_u_int32 caplen;
  126. bpf_u_int32 len;
  127. /* followed by packet data, options, and trailer */
  128. };
  129. /*
  130. * Simple Packet Block.
  131. */
  132. #define BT_SPB 0x00000003
  133. struct simple_packet_block {
  134. bpf_u_int32 len;
  135. /* followed by packet data and trailer */
  136. };
  137. /*
  138. * Packet Block.
  139. */
  140. #define BT_PB 0x00000002
  141. struct packet_block {
  142. u_short interface_id;
  143. u_short drops_count;
  144. bpf_u_int32 timestamp_high;
  145. bpf_u_int32 timestamp_low;
  146. bpf_u_int32 caplen;
  147. bpf_u_int32 len;
  148. /* followed by packet data, options, and trailer */
  149. };
  150. /*
  151. * Block cursor - used when processing the contents of a block.
  152. * Contains a pointer into the data being processed and a count
  153. * of bytes remaining in the block.
  154. */
  155. struct block_cursor {
  156. u_char *data;
  157. size_t data_remaining;
  158. bpf_u_int32 block_type;
  159. };
  160. typedef enum {
  161. PASS_THROUGH,
  162. SCALE_UP_DEC,
  163. SCALE_DOWN_DEC,
  164. SCALE_UP_BIN,
  165. SCALE_DOWN_BIN
  166. } tstamp_scale_type_t;
  167. /*
  168. * Per-interface information.
  169. */
  170. struct pcap_ng_if {
  171. uint64_t tsresol; /* time stamp resolution */
  172. tstamp_scale_type_t scale_type; /* how to scale */
  173. uint64_t scale_factor; /* time stamp scale factor for power-of-10 tsresol */
  174. uint64_t tsoffset; /* time stamp offset */
  175. };
  176. /*
  177. * Per-pcap_t private data.
  178. *
  179. * max_blocksize is the maximum size of a block that we'll accept. We
  180. * reject blocks bigger than this, so we don't consume too much memory
  181. * with a truly huge block. It can change as we see IDBs with different
  182. * link-layer header types. (Currently, we don't support IDBs with
  183. * different link-layer header types, but we will support it in the
  184. * future, when we offer file-reading APIs that support it.)
  185. *
  186. * XXX - that's an issue on ILP32 platforms, where the maximum block
  187. * size of 2^31-1 would eat all but one byte of the entire address space.
  188. * It's less of an issue on ILP64/LLP64 platforms, but the actual size
  189. * of the address space may be limited by 1) the number of *significant*
  190. * address bits (currently, x86-64 only supports 48 bits of address), 2)
  191. * any limitations imposed by the operating system; 3) any limitations
  192. * imposed by the amount of available backing store for anonymous pages,
  193. * so we impose a limit regardless of the size of a pointer.
  194. */
  195. struct pcap_ng_sf {
  196. uint64_t user_tsresol; /* time stamp resolution requested by the user */
  197. u_int max_blocksize; /* don't grow buffer size past this */
  198. bpf_u_int32 ifcount; /* number of interfaces seen in this capture */
  199. bpf_u_int32 ifaces_size; /* size of array below */
  200. struct pcap_ng_if *ifaces; /* array of interface information */
  201. };
  202. /*
  203. * Maximum block size for a given maximum snapshot length; we calculate
  204. * this based
  205. *
  206. * We define it as the size of an EPB with a max_snaplen-sized
  207. * packet and 128KB of options.
  208. */
  209. #define MAX_BLOCKSIZE(max_snaplen) (sizeof (struct block_header) + \
  210. sizeof (struct enhanced_packet_block) + \
  211. (max_snaplen) + 131072 + \
  212. sizeof (struct block_trailer))
  213. static void pcap_ng_cleanup(pcap_t *p);
  214. static int pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr,
  215. u_char **data);
  216. static int
  217. read_bytes(FILE *fp, void *buf, size_t bytes_to_read, int fail_on_eof,
  218. char *errbuf)
  219. {
  220. size_t amt_read;
  221. amt_read = fread(buf, 1, bytes_to_read, fp);
  222. if (amt_read != bytes_to_read) {
  223. if (ferror(fp)) {
  224. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  225. errno, "error reading dump file");
  226. } else {
  227. if (amt_read == 0 && !fail_on_eof)
  228. return (0); /* EOF */
  229. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  230. "truncated dump file; tried to read %lu bytes, only got %lu",
  231. (unsigned long)bytes_to_read,
  232. (unsigned long)amt_read);
  233. }
  234. return (-1);
  235. }
  236. return (1);
  237. }
  238. static int
  239. read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
  240. {
  241. struct pcap_ng_sf *ps;
  242. int status;
  243. struct block_header bhdr;
  244. u_char *bdata;
  245. size_t data_remaining;
  246. ps = p->priv;
  247. status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
  248. if (status <= 0)
  249. return (status); /* error or EOF */
  250. if (p->swapped) {
  251. bhdr.block_type = SWAPLONG(bhdr.block_type);
  252. bhdr.total_length = SWAPLONG(bhdr.total_length);
  253. }
  254. /*
  255. * Is this block "too big"?
  256. *
  257. * We choose 16MB as "too big", for now, so that we handle
  258. * "reasonably" large buffers but don't chew up all the
  259. * memory if we read a malformed file.
  260. */
  261. if (bhdr.total_length > 16*1024*1024) {
  262. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  263. "pcapng block size %u > maximum %u",
  264. bhdr.total_length, 16*1024*1024);
  265. return (-1);
  266. }
  267. /*
  268. * Is this block "too small" - i.e., is it shorter than a block
  269. * header plus a block trailer?
  270. */
  271. if (bhdr.total_length < sizeof(struct block_header) +
  272. sizeof(struct block_trailer)) {
  273. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  274. "block in pcapng dump file has a length of %u < %lu",
  275. bhdr.total_length,
  276. (unsigned long)(sizeof(struct block_header) + sizeof(struct block_trailer)));
  277. return (-1);
  278. }
  279. /*
  280. * Is the buffer big enough?
  281. */
  282. if (p->bufsize < bhdr.total_length) {
  283. /*
  284. * No - make it big enough, unless it's too big.
  285. */
  286. void *bigger_buffer;
  287. if (bhdr.total_length > ps->max_blocksize) {
  288. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "block is larger than maximum block size %u",
  289. ps->max_blocksize);
  290. return (-1);
  291. }
  292. bigger_buffer = realloc(p->buffer, bhdr.total_length);
  293. if (bigger_buffer == NULL) {
  294. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  295. return (-1);
  296. }
  297. p->buffer = bigger_buffer;
  298. }
  299. /*
  300. * Copy the stuff we've read to the buffer, and read the rest
  301. * of the block.
  302. */
  303. memcpy(p->buffer, &bhdr, sizeof(bhdr));
  304. bdata = (u_char *)p->buffer + sizeof(bhdr);
  305. data_remaining = bhdr.total_length - sizeof(bhdr);
  306. if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
  307. return (-1);
  308. /*
  309. * Initialize the cursor.
  310. */
  311. cursor->data = bdata;
  312. cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
  313. cursor->block_type = bhdr.block_type;
  314. return (1);
  315. }
  316. static void *
  317. get_from_block_data(struct block_cursor *cursor, size_t chunk_size,
  318. char *errbuf)
  319. {
  320. void *data;
  321. /*
  322. * Make sure we have the specified amount of data remaining in
  323. * the block data.
  324. */
  325. if (cursor->data_remaining < chunk_size) {
  326. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  327. "block of type %u in pcapng dump file is too short",
  328. cursor->block_type);
  329. return (NULL);
  330. }
  331. /*
  332. * Return the current pointer, and skip past the chunk.
  333. */
  334. data = cursor->data;
  335. cursor->data += chunk_size;
  336. cursor->data_remaining -= chunk_size;
  337. return (data);
  338. }
  339. static struct option_header *
  340. get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
  341. {
  342. struct option_header *opthdr;
  343. opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
  344. if (opthdr == NULL) {
  345. /*
  346. * Option header is cut short.
  347. */
  348. return (NULL);
  349. }
  350. /*
  351. * Byte-swap it if necessary.
  352. */
  353. if (p->swapped) {
  354. opthdr->option_code = SWAPSHORT(opthdr->option_code);
  355. opthdr->option_length = SWAPSHORT(opthdr->option_length);
  356. }
  357. return (opthdr);
  358. }
  359. static void *
  360. get_optvalue_from_block_data(struct block_cursor *cursor,
  361. struct option_header *opthdr, char *errbuf)
  362. {
  363. size_t padded_option_len;
  364. void *optvalue;
  365. /* Pad option length to 4-byte boundary */
  366. padded_option_len = opthdr->option_length;
  367. padded_option_len = ((padded_option_len + 3)/4)*4;
  368. optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
  369. if (optvalue == NULL) {
  370. /*
  371. * Option value is cut short.
  372. */
  373. return (NULL);
  374. }
  375. return (optvalue);
  376. }
  377. static int
  378. process_idb_options(pcap_t *p, struct block_cursor *cursor, uint64_t *tsresol,
  379. uint64_t *tsoffset, int *is_binary, char *errbuf)
  380. {
  381. struct option_header *opthdr;
  382. void *optvalue;
  383. int saw_tsresol, saw_tsoffset;
  384. uint8_t tsresol_opt;
  385. u_int i;
  386. saw_tsresol = 0;
  387. saw_tsoffset = 0;
  388. while (cursor->data_remaining != 0) {
  389. /*
  390. * Get the option header.
  391. */
  392. opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
  393. if (opthdr == NULL) {
  394. /*
  395. * Option header is cut short.
  396. */
  397. return (-1);
  398. }
  399. /*
  400. * Get option value.
  401. */
  402. optvalue = get_optvalue_from_block_data(cursor, opthdr,
  403. errbuf);
  404. if (optvalue == NULL) {
  405. /*
  406. * Option value is cut short.
  407. */
  408. return (-1);
  409. }
  410. switch (opthdr->option_code) {
  411. case OPT_ENDOFOPT:
  412. if (opthdr->option_length != 0) {
  413. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  414. "Interface Description Block has opt_endofopt option with length %u != 0",
  415. opthdr->option_length);
  416. return (-1);
  417. }
  418. goto done;
  419. case IF_TSRESOL:
  420. if (opthdr->option_length != 1) {
  421. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  422. "Interface Description Block has if_tsresol option with length %u != 1",
  423. opthdr->option_length);
  424. return (-1);
  425. }
  426. if (saw_tsresol) {
  427. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  428. "Interface Description Block has more than one if_tsresol option");
  429. return (-1);
  430. }
  431. saw_tsresol = 1;
  432. memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
  433. if (tsresol_opt & 0x80) {
  434. /*
  435. * Resolution is negative power of 2.
  436. */
  437. uint8_t tsresol_shift = (tsresol_opt & 0x7F);
  438. if (tsresol_shift > 63) {
  439. /*
  440. * Resolution is too high; 2^-{res}
  441. * won't fit in a 64-bit value.
  442. */
  443. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  444. "Interface Description Block if_tsresol option resolution 2^-%u is too high",
  445. tsresol_shift);
  446. return (-1);
  447. }
  448. *is_binary = 1;
  449. *tsresol = ((uint64_t)1) << tsresol_shift;
  450. } else {
  451. /*
  452. * Resolution is negative power of 10.
  453. */
  454. if (tsresol_opt > 19) {
  455. /*
  456. * Resolution is too high; 2^-{res}
  457. * won't fit in a 64-bit value (the
  458. * largest power of 10 that fits
  459. * in a 64-bit value is 10^19, as
  460. * the largest 64-bit unsigned
  461. * value is ~1.8*10^19).
  462. */
  463. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  464. "Interface Description Block if_tsresol option resolution 10^-%u is too high",
  465. tsresol_opt);
  466. return (-1);
  467. }
  468. *is_binary = 0;
  469. *tsresol = 1;
  470. for (i = 0; i < tsresol_opt; i++)
  471. *tsresol *= 10;
  472. }
  473. break;
  474. case IF_TSOFFSET:
  475. if (opthdr->option_length != 8) {
  476. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  477. "Interface Description Block has if_tsoffset option with length %u != 8",
  478. opthdr->option_length);
  479. return (-1);
  480. }
  481. if (saw_tsoffset) {
  482. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  483. "Interface Description Block has more than one if_tsoffset option");
  484. return (-1);
  485. }
  486. saw_tsoffset = 1;
  487. memcpy(tsoffset, optvalue, sizeof(*tsoffset));
  488. if (p->swapped)
  489. *tsoffset = SWAPLL(*tsoffset);
  490. break;
  491. default:
  492. break;
  493. }
  494. }
  495. done:
  496. return (0);
  497. }
  498. static int
  499. add_interface(pcap_t *p, struct block_cursor *cursor, char *errbuf)
  500. {
  501. struct pcap_ng_sf *ps;
  502. uint64_t tsresol;
  503. uint64_t tsoffset;
  504. int is_binary;
  505. ps = p->priv;
  506. /*
  507. * Count this interface.
  508. */
  509. ps->ifcount++;
  510. /*
  511. * Grow the array of per-interface information as necessary.
  512. */
  513. if (ps->ifcount > ps->ifaces_size) {
  514. /*
  515. * We need to grow the array.
  516. */
  517. bpf_u_int32 new_ifaces_size;
  518. struct pcap_ng_if *new_ifaces;
  519. if (ps->ifaces_size == 0) {
  520. /*
  521. * It's currently empty.
  522. *
  523. * (The Clang static analyzer doesn't do enough,
  524. * err, umm, dataflow *analysis* to realize that
  525. * ps->ifaces_size == 0 if ps->ifaces == NULL,
  526. * and so complains about a possible zero argument
  527. * to realloc(), so we check for the former
  528. * condition to shut it up.
  529. *
  530. * However, it doesn't complain that one of the
  531. * multiplications below could overflow, which is
  532. * a real, albeit extremely unlikely, problem (you'd
  533. * need a pcapng file with tens of millions of
  534. * interfaces).)
  535. */
  536. new_ifaces_size = 1;
  537. new_ifaces = malloc(sizeof (struct pcap_ng_if));
  538. } else {
  539. /*
  540. * It's not currently empty; double its size.
  541. * (Perhaps overkill once we have a lot of interfaces.)
  542. *
  543. * Check for overflow if we double it.
  544. */
  545. if (ps->ifaces_size * 2 < ps->ifaces_size) {
  546. /*
  547. * The maximum number of interfaces before
  548. * ps->ifaces_size overflows is the largest
  549. * possible 32-bit power of 2, as we do
  550. * size doubling.
  551. */
  552. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  553. "more than %u interfaces in the file",
  554. 0x80000000U);
  555. return (0);
  556. }
  557. /*
  558. * ps->ifaces_size * 2 doesn't overflow, so it's
  559. * safe to multiply.
  560. */
  561. new_ifaces_size = ps->ifaces_size * 2;
  562. /*
  563. * Now make sure that's not so big that it overflows
  564. * if we multiply by sizeof (struct pcap_ng_if).
  565. *
  566. * That can happen on 32-bit platforms, with a 32-bit
  567. * size_t; it shouldn't happen on 64-bit platforms,
  568. * with a 64-bit size_t, as new_ifaces_size is
  569. * 32 bits.
  570. */
  571. if (new_ifaces_size * sizeof (struct pcap_ng_if) < new_ifaces_size) {
  572. /*
  573. * As this fails only with 32-bit size_t,
  574. * the multiplication was 32x32->32, and
  575. * the largest 32-bit value that can safely
  576. * be multiplied by sizeof (struct pcap_ng_if)
  577. * without overflow is the largest 32-bit
  578. * (unsigned) value divided by
  579. * sizeof (struct pcap_ng_if).
  580. */
  581. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  582. "more than %u interfaces in the file",
  583. 0xFFFFFFFFU / ((u_int)sizeof (struct pcap_ng_if)));
  584. return (0);
  585. }
  586. new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
  587. }
  588. if (new_ifaces == NULL) {
  589. /*
  590. * We ran out of memory.
  591. * Give up.
  592. */
  593. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  594. "out of memory for per-interface information (%u interfaces)",
  595. ps->ifcount);
  596. return (0);
  597. }
  598. ps->ifaces_size = new_ifaces_size;
  599. ps->ifaces = new_ifaces;
  600. }
  601. /*
  602. * Set the default time stamp resolution and offset.
  603. */
  604. tsresol = 1000000; /* microsecond resolution */
  605. is_binary = 0; /* which is a power of 10 */
  606. tsoffset = 0; /* absolute timestamps */
  607. /*
  608. * Now look for various time stamp options, so we know
  609. * how to interpret the time stamps for this interface.
  610. */
  611. if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
  612. errbuf) == -1)
  613. return (0);
  614. ps->ifaces[ps->ifcount - 1].tsresol = tsresol;
  615. ps->ifaces[ps->ifcount - 1].tsoffset = tsoffset;
  616. /*
  617. * Determine whether we're scaling up or down or not
  618. * at all for this interface.
  619. */
  620. if (tsresol == ps->user_tsresol) {
  621. /*
  622. * The resolution is the resolution the user wants,
  623. * so we don't have to do scaling.
  624. */
  625. ps->ifaces[ps->ifcount - 1].scale_type = PASS_THROUGH;
  626. } else if (tsresol > ps->user_tsresol) {
  627. /*
  628. * The resolution is greater than what the user wants,
  629. * so we have to scale the timestamps down.
  630. */
  631. if (is_binary)
  632. ps->ifaces[ps->ifcount - 1].scale_type = SCALE_DOWN_BIN;
  633. else {
  634. /*
  635. * Calculate the scale factor.
  636. */
  637. ps->ifaces[ps->ifcount - 1].scale_factor = tsresol/ps->user_tsresol;
  638. ps->ifaces[ps->ifcount - 1].scale_type = SCALE_DOWN_DEC;
  639. }
  640. } else {
  641. /*
  642. * The resolution is less than what the user wants,
  643. * so we have to scale the timestamps up.
  644. */
  645. if (is_binary)
  646. ps->ifaces[ps->ifcount - 1].scale_type = SCALE_UP_BIN;
  647. else {
  648. /*
  649. * Calculate the scale factor.
  650. */
  651. ps->ifaces[ps->ifcount - 1].scale_factor = ps->user_tsresol/tsresol;
  652. ps->ifaces[ps->ifcount - 1].scale_type = SCALE_UP_DEC;
  653. }
  654. }
  655. return (1);
  656. }
  657. /*
  658. * Check whether this is a pcapng savefile and, if it is, extract the
  659. * relevant information from the header.
  660. */
  661. pcap_t *
  662. pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
  663. int *err)
  664. {
  665. size_t amt_read;
  666. bpf_u_int32 total_length;
  667. bpf_u_int32 byte_order_magic;
  668. struct block_header *bhdrp;
  669. struct section_header_block *shbp;
  670. pcap_t *p;
  671. int swapped = 0;
  672. struct pcap_ng_sf *ps;
  673. int status;
  674. struct block_cursor cursor;
  675. struct interface_description_block *idbp;
  676. /*
  677. * Assume no read errors.
  678. */
  679. *err = 0;
  680. /*
  681. * Check whether the first 4 bytes of the file are the block
  682. * type for a pcapng savefile.
  683. */
  684. if (magic != BT_SHB) {
  685. /*
  686. * XXX - check whether this looks like what the block
  687. * type would be after being munged by mapping between
  688. * UN*X and DOS/Windows text file format and, if it
  689. * does, look for the byte-order magic number in
  690. * the appropriate place and, if we find it, report
  691. * this as possibly being a pcapng file transferred
  692. * between UN*X and Windows in text file format?
  693. */
  694. return (NULL); /* nope */
  695. }
  696. /*
  697. * OK, they are. However, that's just \n\r\r\n, so it could,
  698. * conceivably, be an ordinary text file.
  699. *
  700. * It could not, however, conceivably be any other type of
  701. * capture file, so we can read the rest of the putative
  702. * Section Header Block; put the block type in the common
  703. * header, read the rest of the common header and the
  704. * fixed-length portion of the SHB, and look for the byte-order
  705. * magic value.
  706. */
  707. amt_read = fread(&total_length, 1, sizeof(total_length), fp);
  708. if (amt_read < sizeof(total_length)) {
  709. if (ferror(fp)) {
  710. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  711. errno, "error reading dump file");
  712. *err = 1;
  713. return (NULL); /* fail */
  714. }
  715. /*
  716. * Possibly a weird short text file, so just say
  717. * "not pcapng".
  718. */
  719. return (NULL);
  720. }
  721. amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
  722. if (amt_read < sizeof(byte_order_magic)) {
  723. if (ferror(fp)) {
  724. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  725. errno, "error reading dump file");
  726. *err = 1;
  727. return (NULL); /* fail */
  728. }
  729. /*
  730. * Possibly a weird short text file, so just say
  731. * "not pcapng".
  732. */
  733. return (NULL);
  734. }
  735. if (byte_order_magic != BYTE_ORDER_MAGIC) {
  736. byte_order_magic = SWAPLONG(byte_order_magic);
  737. if (byte_order_magic != BYTE_ORDER_MAGIC) {
  738. /*
  739. * Not a pcapng file.
  740. */
  741. return (NULL);
  742. }
  743. swapped = 1;
  744. total_length = SWAPLONG(total_length);
  745. }
  746. /*
  747. * Check the sanity of the total length.
  748. */
  749. if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)) {
  750. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  751. "Section Header Block in pcapng dump file has a length of %u < %lu",
  752. total_length,
  753. (unsigned long)(sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)));
  754. *err = 1;
  755. return (NULL);
  756. }
  757. /*
  758. * OK, this is a good pcapng file.
  759. * Allocate a pcap_t for it.
  760. */
  761. p = pcap_open_offline_common(errbuf, sizeof (struct pcap_ng_sf));
  762. if (p == NULL) {
  763. /* Allocation failed. */
  764. *err = 1;
  765. return (NULL);
  766. }
  767. p->swapped = swapped;
  768. ps = p->priv;
  769. /*
  770. * What precision does the user want?
  771. */
  772. switch (precision) {
  773. case PCAP_TSTAMP_PRECISION_MICRO:
  774. ps->user_tsresol = 1000000;
  775. break;
  776. case PCAP_TSTAMP_PRECISION_NANO:
  777. ps->user_tsresol = 1000000000;
  778. break;
  779. default:
  780. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  781. "unknown time stamp resolution %u", precision);
  782. free(p);
  783. *err = 1;
  784. return (NULL);
  785. }
  786. p->opt.tstamp_precision = precision;
  787. /*
  788. * Allocate a buffer into which to read blocks. We default to
  789. * the maximum of:
  790. *
  791. * the total length of the SHB for which we read the header;
  792. *
  793. * 2K, which should be more than large enough for an Enhanced
  794. * Packet Block containing a full-size Ethernet frame, and
  795. * leaving room for some options.
  796. *
  797. * If we find a bigger block, we reallocate the buffer, up to
  798. * the maximum size. We start out with a maximum size based
  799. * on a maximum snapshot length of MAXIMUM_SNAPLEN; if we see
  800. * any link-layer header types with a larger maximum snapshot
  801. * length, we boost the maximum.
  802. */
  803. p->bufsize = 2048;
  804. if (p->bufsize < total_length)
  805. p->bufsize = total_length;
  806. p->buffer = malloc(p->bufsize);
  807. if (p->buffer == NULL) {
  808. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  809. free(p);
  810. *err = 1;
  811. return (NULL);
  812. }
  813. ps->max_blocksize = MAX_BLOCKSIZE(MAXIMUM_SNAPLEN);
  814. /*
  815. * Copy the stuff we've read to the buffer, and read the rest
  816. * of the SHB.
  817. */
  818. bhdrp = (struct block_header *)p->buffer;
  819. shbp = (struct section_header_block *)((u_char *)p->buffer + sizeof(struct block_header));
  820. bhdrp->block_type = magic;
  821. bhdrp->total_length = total_length;
  822. shbp->byte_order_magic = byte_order_magic;
  823. if (read_bytes(fp,
  824. (u_char *)p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
  825. total_length - (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
  826. 1, errbuf) == -1)
  827. goto fail;
  828. if (p->swapped) {
  829. /*
  830. * Byte-swap the fields we've read.
  831. */
  832. shbp->major_version = SWAPSHORT(shbp->major_version);
  833. shbp->minor_version = SWAPSHORT(shbp->minor_version);
  834. /*
  835. * XXX - we don't care about the section length.
  836. */
  837. }
  838. /* currently only SHB version 1.0 is supported */
  839. if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
  840. shbp->minor_version == PCAP_NG_VERSION_MINOR)) {
  841. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  842. "unsupported pcapng savefile version %u.%u",
  843. shbp->major_version, shbp->minor_version);
  844. goto fail;
  845. }
  846. p->version_major = shbp->major_version;
  847. p->version_minor = shbp->minor_version;
  848. /*
  849. * Save the time stamp resolution the user requested.
  850. */
  851. p->opt.tstamp_precision = precision;
  852. /*
  853. * Now start looking for an Interface Description Block.
  854. */
  855. for (;;) {
  856. /*
  857. * Read the next block.
  858. */
  859. status = read_block(fp, p, &cursor, errbuf);
  860. if (status == 0) {
  861. /* EOF - no IDB in this file */
  862. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  863. "the capture file has no Interface Description Blocks");
  864. goto fail;
  865. }
  866. if (status == -1)
  867. goto fail; /* error */
  868. switch (cursor.block_type) {
  869. case BT_IDB:
  870. /*
  871. * Get a pointer to the fixed-length portion of the
  872. * IDB.
  873. */
  874. idbp = get_from_block_data(&cursor, sizeof(*idbp),
  875. errbuf);
  876. if (idbp == NULL)
  877. goto fail; /* error */
  878. /*
  879. * Byte-swap it if necessary.
  880. */
  881. if (p->swapped) {
  882. idbp->linktype = SWAPSHORT(idbp->linktype);
  883. idbp->snaplen = SWAPLONG(idbp->snaplen);
  884. }
  885. /*
  886. * Try to add this interface.
  887. */
  888. if (!add_interface(p, &cursor, errbuf))
  889. goto fail;
  890. goto done;
  891. case BT_EPB:
  892. case BT_SPB:
  893. case BT_PB:
  894. /*
  895. * Saw a packet before we saw any IDBs. That's
  896. * not valid, as we don't know what link-layer
  897. * encapsulation the packet has.
  898. */
  899. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  900. "the capture file has a packet block before any Interface Description Blocks");
  901. goto fail;
  902. default:
  903. /*
  904. * Just ignore it.
  905. */
  906. break;
  907. }
  908. }
  909. done:
  910. p->tzoff = 0; /* XXX - not used in pcap */
  911. p->snapshot = idbp->snaplen;
  912. if (p->snapshot <= 0) {
  913. /*
  914. * Bogus snapshot length; use the maximum for this
  915. * link-layer type as a fallback.
  916. *
  917. * XXX - the only reason why snapshot is signed is
  918. * that pcap_snapshot() returns an int, not an
  919. * unsigned int.
  920. */
  921. p->snapshot = max_snaplen_for_dlt(idbp->linktype);
  922. }
  923. p->linktype = linktype_to_dlt(idbp->linktype);
  924. p->linktype_ext = 0;
  925. /*
  926. * If the maximum block size for a packet with the maximum
  927. * snapshot length for this DLT_ is bigger than the current
  928. * maximum block size, increase the maximum.
  929. */
  930. if (MAX_BLOCKSIZE(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
  931. ps->max_blocksize = MAX_BLOCKSIZE(max_snaplen_for_dlt(p->linktype));
  932. p->next_packet_op = pcap_ng_next_packet;
  933. p->cleanup_op = pcap_ng_cleanup;
  934. return (p);
  935. fail:
  936. free(ps->ifaces);
  937. free(p->buffer);
  938. free(p);
  939. *err = 1;
  940. return (NULL);
  941. }
  942. static void
  943. pcap_ng_cleanup(pcap_t *p)
  944. {
  945. struct pcap_ng_sf *ps = p->priv;
  946. free(ps->ifaces);
  947. sf_cleanup(p);
  948. }
  949. /*
  950. * Read and return the next packet from the savefile. Return the header
  951. * in hdr and a pointer to the contents in data. Return 0 on success, 1
  952. * if there were no more packets, and -1 on an error.
  953. */
  954. static int
  955. pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
  956. {
  957. struct pcap_ng_sf *ps = p->priv;
  958. struct block_cursor cursor;
  959. int status;
  960. struct enhanced_packet_block *epbp;
  961. struct simple_packet_block *spbp;
  962. struct packet_block *pbp;
  963. bpf_u_int32 interface_id = 0xFFFFFFFF;
  964. struct interface_description_block *idbp;
  965. struct section_header_block *shbp;
  966. FILE *fp = p->rfile;
  967. uint64_t t, sec, frac;
  968. /*
  969. * Look for an Enhanced Packet Block, a Simple Packet Block,
  970. * or a Packet Block.
  971. */
  972. for (;;) {
  973. /*
  974. * Read the block type and length; those are common
  975. * to all blocks.
  976. */
  977. status = read_block(fp, p, &cursor, p->errbuf);
  978. if (status == 0)
  979. return (1); /* EOF */
  980. if (status == -1)
  981. return (-1); /* error */
  982. switch (cursor.block_type) {
  983. case BT_EPB:
  984. /*
  985. * Get a pointer to the fixed-length portion of the
  986. * EPB.
  987. */
  988. epbp = get_from_block_data(&cursor, sizeof(*epbp),
  989. p->errbuf);
  990. if (epbp == NULL)
  991. return (-1); /* error */
  992. /*
  993. * Byte-swap it if necessary.
  994. */
  995. if (p->swapped) {
  996. /* these were written in opposite byte order */
  997. interface_id = SWAPLONG(epbp->interface_id);
  998. hdr->caplen = SWAPLONG(epbp->caplen);
  999. hdr->len = SWAPLONG(epbp->len);
  1000. t = ((uint64_t)SWAPLONG(epbp->timestamp_high)) << 32 |
  1001. SWAPLONG(epbp->timestamp_low);
  1002. } else {
  1003. interface_id = epbp->interface_id;
  1004. hdr->caplen = epbp->caplen;
  1005. hdr->len = epbp->len;
  1006. t = ((uint64_t)epbp->timestamp_high) << 32 |
  1007. epbp->timestamp_low;
  1008. }
  1009. goto found;
  1010. case BT_SPB:
  1011. /*
  1012. * Get a pointer to the fixed-length portion of the
  1013. * SPB.
  1014. */
  1015. spbp = get_from_block_data(&cursor, sizeof(*spbp),
  1016. p->errbuf);
  1017. if (spbp == NULL)
  1018. return (-1); /* error */
  1019. /*
  1020. * SPB packets are assumed to have arrived on
  1021. * the first interface.
  1022. */
  1023. interface_id = 0;
  1024. /*
  1025. * Byte-swap it if necessary.
  1026. */
  1027. if (p->swapped) {
  1028. /* these were written in opposite byte order */
  1029. hdr->len = SWAPLONG(spbp->len);
  1030. } else
  1031. hdr->len = spbp->len;
  1032. /*
  1033. * The SPB doesn't give the captured length;
  1034. * it's the minimum of the snapshot length
  1035. * and the packet length.
  1036. */
  1037. hdr->caplen = hdr->len;
  1038. if (hdr->caplen > (bpf_u_int32)p->snapshot)
  1039. hdr->caplen = p->snapshot;
  1040. t = 0; /* no time stamps */
  1041. goto found;
  1042. case BT_PB:
  1043. /*
  1044. * Get a pointer to the fixed-length portion of the
  1045. * PB.
  1046. */
  1047. pbp = get_from_block_data(&cursor, sizeof(*pbp),
  1048. p->errbuf);
  1049. if (pbp == NULL)
  1050. return (-1); /* error */
  1051. /*
  1052. * Byte-swap it if necessary.
  1053. */
  1054. if (p->swapped) {
  1055. /* these were written in opposite byte order */
  1056. interface_id = SWAPSHORT(pbp->interface_id);
  1057. hdr->caplen = SWAPLONG(pbp->caplen);
  1058. hdr->len = SWAPLONG(pbp->len);
  1059. t = ((uint64_t)SWAPLONG(pbp->timestamp_high)) << 32 |
  1060. SWAPLONG(pbp->timestamp_low);
  1061. } else {
  1062. interface_id = pbp->interface_id;
  1063. hdr->caplen = pbp->caplen;
  1064. hdr->len = pbp->len;
  1065. t = ((uint64_t)pbp->timestamp_high) << 32 |
  1066. pbp->timestamp_low;
  1067. }
  1068. goto found;
  1069. case BT_IDB:
  1070. /*
  1071. * Interface Description Block. Get a pointer
  1072. * to its fixed-length portion.
  1073. */
  1074. idbp = get_from_block_data(&cursor, sizeof(*idbp),
  1075. p->errbuf);
  1076. if (idbp == NULL)
  1077. return (-1); /* error */
  1078. /*
  1079. * Byte-swap it if necessary.
  1080. */
  1081. if (p->swapped) {
  1082. idbp->linktype = SWAPSHORT(idbp->linktype);
  1083. idbp->snaplen = SWAPLONG(idbp->snaplen);
  1084. }
  1085. /*
  1086. * If the link-layer type or snapshot length
  1087. * differ from the ones for the first IDB we
  1088. * saw, quit.
  1089. *
  1090. * XXX - just discard packets from those
  1091. * interfaces?
  1092. */
  1093. if (p->linktype != idbp->linktype) {
  1094. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1095. "an interface has a type %u different from the type of the first interface",
  1096. idbp->linktype);
  1097. return (-1);
  1098. }
  1099. if ((bpf_u_int32)p->snapshot != idbp->snaplen) {
  1100. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1101. "an interface has a snapshot length %u different from the type of the first interface",
  1102. idbp->snaplen);
  1103. return (-1);
  1104. }
  1105. /*
  1106. * Try to add this interface.
  1107. */
  1108. if (!add_interface(p, &cursor, p->errbuf))
  1109. return (-1);
  1110. break;
  1111. case BT_SHB:
  1112. /*
  1113. * Section Header Block. Get a pointer
  1114. * to its fixed-length portion.
  1115. */
  1116. shbp = get_from_block_data(&cursor, sizeof(*shbp),
  1117. p->errbuf);
  1118. if (shbp == NULL)
  1119. return (-1); /* error */
  1120. /*
  1121. * Assume the byte order of this section is
  1122. * the same as that of the previous section.
  1123. * We'll check for that later.
  1124. */
  1125. if (p->swapped) {
  1126. shbp->byte_order_magic =
  1127. SWAPLONG(shbp->byte_order_magic);
  1128. shbp->major_version =
  1129. SWAPSHORT(shbp->major_version);
  1130. }
  1131. /*
  1132. * Make sure the byte order doesn't change;
  1133. * pcap_is_swapped() shouldn't change its
  1134. * return value in the middle of reading a capture.
  1135. */
  1136. switch (shbp->byte_order_magic) {
  1137. case BYTE_ORDER_MAGIC:
  1138. /*
  1139. * OK.
  1140. */
  1141. break;
  1142. case SWAPLONG(BYTE_ORDER_MAGIC):
  1143. /*
  1144. * Byte order changes.
  1145. */
  1146. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1147. "the file has sections with different byte orders");
  1148. return (-1);
  1149. default:
  1150. /*
  1151. * Not a valid SHB.
  1152. */
  1153. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1154. "the file has a section with a bad byte order magic field");
  1155. return (-1);
  1156. }
  1157. /*
  1158. * Make sure the major version is the version
  1159. * we handle.
  1160. */
  1161. if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
  1162. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1163. "unknown pcapng savefile major version number %u",
  1164. shbp->major_version);
  1165. return (-1);
  1166. }
  1167. /*
  1168. * Reset the interface count; this section should
  1169. * have its own set of IDBs. If any of them
  1170. * don't have the same interface type, snapshot
  1171. * length, or resolution as the first interface
  1172. * we saw, we'll fail. (And if we don't see
  1173. * any IDBs, we'll fail when we see a packet
  1174. * block.)
  1175. */
  1176. ps->ifcount = 0;
  1177. break;
  1178. default:
  1179. /*
  1180. * Not a packet block, IDB, or SHB; ignore it.
  1181. */
  1182. break;
  1183. }
  1184. }
  1185. found:
  1186. /*
  1187. * Is the interface ID an interface we know?
  1188. */
  1189. if (interface_id >= ps->ifcount) {
  1190. /*
  1191. * Yes. Fail.
  1192. */
  1193. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1194. "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
  1195. interface_id);
  1196. return (-1);
  1197. }
  1198. if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  1199. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1200. "invalid packet capture length %u, bigger than "
  1201. "snaplen of %d", hdr->caplen, p->snapshot);
  1202. return (-1);
  1203. }
  1204. /*
  1205. * Convert the time stamp to seconds and fractions of a second,
  1206. * with the fractions being in units of the file-supplied resolution.
  1207. */
  1208. sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
  1209. frac = t % ps->ifaces[interface_id].tsresol;
  1210. /*
  1211. * Convert the fractions from units of the file-supplied resolution
  1212. * to units of the user-requested resolution.
  1213. */
  1214. switch (ps->ifaces[interface_id].scale_type) {
  1215. case PASS_THROUGH:
  1216. /*
  1217. * The interface resolution is what the user wants,
  1218. * so we're done.
  1219. */
  1220. break;
  1221. case SCALE_UP_DEC:
  1222. /*
  1223. * The interface resolution is less than what the user
  1224. * wants; scale the fractional part up to the units of
  1225. * the resolution the user requested by multiplying by
  1226. * the quotient of the user-requested resolution and the
  1227. * file-supplied resolution.
  1228. *
  1229. * Those resolutions are both powers of 10, and the user-
  1230. * requested resolution is greater than the file-supplied
  1231. * resolution, so the quotient in question is an integer.
  1232. * We've calculated that quotient already, so we just
  1233. * multiply by it.
  1234. */
  1235. frac *= ps->ifaces[interface_id].scale_factor;
  1236. break;
  1237. case SCALE_UP_BIN:
  1238. /*
  1239. * The interface resolution is less than what the user
  1240. * wants; scale the fractional part up to the units of
  1241. * the resolution the user requested by multiplying by
  1242. * the quotient of the user-requested resolution and the
  1243. * file-supplied resolution.
  1244. *
  1245. * The file-supplied resolution is a power of 2, so the
  1246. * quotient is not an integer, so, in order to do this
  1247. * entirely with integer arithmetic, we multiply by the
  1248. * user-requested resolution and divide by the file-
  1249. * supplied resolution.
  1250. *
  1251. * XXX - Is there something clever we could do here,
  1252. * given that we know that the file-supplied resolution
  1253. * is a power of 2? Doing a multiplication followed by
  1254. * a division runs the risk of overflowing, and involves
  1255. * two non-simple arithmetic operations.
  1256. */
  1257. frac *= ps->user_tsresol;
  1258. frac /= ps->ifaces[interface_id].tsresol;
  1259. break;
  1260. case SCALE_DOWN_DEC:
  1261. /*
  1262. * The interface resolution is greater than what the user
  1263. * wants; scale the fractional part up to the units of
  1264. * the resolution the user requested by multiplying by
  1265. * the quotient of the user-requested resolution and the
  1266. * file-supplied resolution.
  1267. *
  1268. * Those resolutions are both powers of 10, and the user-
  1269. * requested resolution is less than the file-supplied
  1270. * resolution, so the quotient in question isn't an
  1271. * integer, but its reciprocal is, and we can just divide
  1272. * by the reciprocal of the quotient. We've calculated
  1273. * the reciprocal of that quotient already, so we must
  1274. * divide by it.
  1275. */
  1276. frac /= ps->ifaces[interface_id].scale_factor;
  1277. break;
  1278. case SCALE_DOWN_BIN:
  1279. /*
  1280. * The interface resolution is greater than what the user
  1281. * wants; convert the fractional part to units of the
  1282. * resolution the user requested by multiplying by the
  1283. * quotient of the user-requested resolution and the
  1284. * file-supplied resolution. We do that by multiplying
  1285. * by the user-requested resolution and dividing by the
  1286. * file-supplied resolution, as the quotient might not
  1287. * fit in an integer.
  1288. *
  1289. * The file-supplied resolution is a power of 2, so the
  1290. * quotient is not an integer, and neither is its
  1291. * reciprocal, so, in order to do this entirely with
  1292. * integer arithmetic, we multiply by the user-requested
  1293. * resolution and divide by the file-supplied resolution.
  1294. *
  1295. * XXX - Is there something clever we could do here,
  1296. * given that we know that the file-supplied resolution
  1297. * is a power of 2? Doing a multiplication followed by
  1298. * a division runs the risk of overflowing, and involves
  1299. * two non-simple arithmetic operations.
  1300. */
  1301. frac *= ps->user_tsresol;
  1302. frac /= ps->ifaces[interface_id].tsresol;
  1303. break;
  1304. }
  1305. #ifdef _WIN32
  1306. /*
  1307. * tv_sec and tv_used in the Windows struct timeval are both
  1308. * longs.
  1309. */
  1310. hdr->ts.tv_sec = (long)sec;
  1311. hdr->ts.tv_usec = (long)frac;
  1312. #else
  1313. /*
  1314. * tv_sec in the UN*X struct timeval is a time_t; tv_usec is
  1315. * suseconds_t in UN*Xes that work the way the current Single
  1316. * UNIX Standard specify - but not all older UN*Xes necessarily
  1317. * support that type, so just cast to int.
  1318. */
  1319. hdr->ts.tv_sec = (time_t)sec;
  1320. hdr->ts.tv_usec = (int)frac;
  1321. #endif
  1322. /*
  1323. * Get a pointer to the packet data.
  1324. */
  1325. *data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
  1326. if (*data == NULL)
  1327. return (-1);
  1328. if (p->swapped)
  1329. swap_pseudo_headers(p->linktype, hdr, *data);
  1330. return (0);
  1331. }