]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
Add SunATM support, based on code from Yen Yen Lim at North Dakota State
[libpcap] / savefile.c
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
31 #ifndef lint
32 static const char rcsid[] =
33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.64 2002-07-11 09:06:46 guy Exp $ (LBL)";
34 #endif
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <errno.h>
41 #include <memory.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "pcap-int.h"
47
48 #ifdef HAVE_OS_PROTO_H
49 #include "os-proto.h"
50 #endif
51
52 #define TCPDUMP_MAGIC 0xa1b2c3d4
53 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34
54
55 /*
56 * We use the "receiver-makes-right" approach to byte order,
57 * because time is at a premium when we are writing the file.
58 * In other words, the pcap_file_header and pcap_pkthdr,
59 * records are written in host byte order.
60 * Note that the packets are always written in network byte order.
61 *
62 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
63 * machine (if the file was written in little-end order).
64 */
65 #define SWAPLONG(y) \
66 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
67 #define SWAPSHORT(y) \
68 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
69
70 #define SFERR_TRUNC 1
71 #define SFERR_BADVERSION 2
72 #define SFERR_BADF 3
73 #define SFERR_EOF 4 /* not really an error, just a status */
74
75 /*
76 * We don't write DLT_* values to the capture file header, because
77 * they're not the same on all platforms.
78 *
79 * Unfortunately, the various flavors of BSD have not always used the same
80 * numerical values for the same data types, and various patches to
81 * libpcap for non-BSD OSes have added their own DLT_* codes for link
82 * layer encapsulation types seen on those OSes, and those codes have had,
83 * in some cases, values that were also used, on other platforms, for other
84 * link layer encapsulation types.
85 *
86 * This means that capture files of a type whose numerical DLT_* code
87 * means different things on different BSDs, or with different versions
88 * of libpcap, can't always be read on systems other than those like
89 * the one running on the machine on which the capture was made.
90 *
91 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
92 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
93 * codes to DLT_* codes when reading a savefile header.
94 *
95 * For those DLT_* codes that have, as far as we know, the same values on
96 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
97 * DLT_xxx; that way, captures of those types can still be read by
98 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
99 * captures of those types written by versions of libpcap that map DLT_
100 * values to LINKTYPE_ values can still be read by older versions
101 * of libpcap.
102 *
103 * The other LINKTYPE_* codes are given values starting at 100, in the
104 * hopes that no DLT_* code will be given one of those values.
105 *
106 * In order to ensure that a given LINKTYPE_* code's value will refer to
107 * the same encapsulation type on all platforms, you should not allocate
108 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
109 * The tcpdump developers will allocate a value for you, and will not
110 * subsequently allocate it to anybody else; that value will be added to
111 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
112 * libpcap release will include it.
113 *
114 * You should, if possible, also contribute patches to libpcap and tcpdump
115 * to handle the new encapsulation type, so that they can also be checked
116 * into the tcpdump.org CVS repository and so that they will appear in
117 * future libpcap and tcpdump releases.
118 */
119 #define LINKTYPE_NULL DLT_NULL
120 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
121 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
122 #define LINKTYPE_AX25 DLT_AX25
123 #define LINKTYPE_PRONET DLT_PRONET
124 #define LINKTYPE_CHAOS DLT_CHAOS
125 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
126 #define LINKTYPE_ARCNET DLT_ARCNET
127 #define LINKTYPE_SLIP DLT_SLIP
128 #define LINKTYPE_PPP DLT_PPP
129 #define LINKTYPE_FDDI DLT_FDDI
130
131 /*
132 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
133 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
134 * field) at the beginning of the packet.
135 *
136 * This is for use when there is always such a header; the address field
137 * might be 0xff, for regular PPP, or it might be an address field for Cisco
138 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
139 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
140 *
141 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
142 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
143 * captures will be written out with a link type that NetBSD's tcpdump
144 * can read.
145 */
146 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
147
148 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
149
150 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
151 #define LINKTYPE_RAW 101 /* raw IP */
152 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
153 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
154 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
155 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
156 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
157 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
158
159 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
160 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
161 #define LINKTYPE_ECONET 115 /* Acorn Econet */
162
163 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
164 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
165 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
166 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
167
168 /*
169 * These types are reserved for future use.
170 */
171 #define LINKTYPE_FRELAY 107 /* Frame Relay */
172 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
173 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
174 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
175 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
176 #define LINKTYPE_IPFILTER 116 /* IP Filter capture files */
177 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
178 #define LINKTYPE_HHDLC 121 /* Siemens HiPath HDLC */
179 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
180 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
181
182 static struct linktype_map {
183 int dlt;
184 int linktype;
185 } map[] = {
186 /*
187 * These DLT_* codes have LINKTYPE_* codes with values identical
188 * to the values of the corresponding DLT_* code.
189 */
190 { DLT_NULL, LINKTYPE_NULL },
191 { DLT_EN10MB, LINKTYPE_ETHERNET },
192 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
193 { DLT_AX25, LINKTYPE_AX25 },
194 { DLT_PRONET, LINKTYPE_PRONET },
195 { DLT_CHAOS, LINKTYPE_CHAOS },
196 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
197 { DLT_ARCNET, LINKTYPE_ARCNET },
198 { DLT_SLIP, LINKTYPE_SLIP },
199 { DLT_PPP, LINKTYPE_PPP },
200 { DLT_FDDI, LINKTYPE_FDDI },
201
202 /*
203 * These DLT_* codes have different values on different
204 * platforms; we map them to LINKTYPE_* codes that
205 * have values that should never be equal to any DLT_*
206 * code.
207 */
208 #ifdef DLT_FR
209 /* BSD/OS Frame Relay */
210 { DLT_FR, LINKTYPE_FRELAY },
211 #endif
212 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
213 { DLT_RAW, LINKTYPE_RAW },
214 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
215 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
216
217 /* BSD/OS Cisco HDLC */
218 { DLT_C_HDLC, LINKTYPE_C_HDLC },
219
220 /*
221 * These DLT_* codes are not on all platforms, but, so far,
222 * there don't appear to be any platforms that define
223 * other codes with those values; we map them to
224 * different LINKTYPE_* values anyway, just in case.
225 */
226
227 /* Linux ATM Classical IP */
228 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
229
230 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
231 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
232
233 /* NetBSD PPP over Ethernet */
234 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
235
236 /* IEEE 802.11 wireless */
237 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
238
239 /* Frame Relay */
240 { DLT_FRELAY, LINKTYPE_FRELAY },
241
242 /* OpenBSD loopback */
243 { DLT_LOOP, LINKTYPE_LOOP },
244
245 /* Linux cooked socket capture */
246 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
247
248 /* Apple LocalTalk hardware */
249 { DLT_LTALK, LINKTYPE_LTALK },
250
251 /* Acorn Econet */
252 { DLT_ECONET, LINKTYPE_ECONET },
253
254 /* For Cisco-internal use */
255 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
256
257 /* Prism II monitor-mode header plus 802.11 header */
258 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
259
260 /* FreeBSD Aironet driver stuff */
261 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
262
263 /* Siemens HiPath HDLC */
264 { DLT_HHDLC, LINKTYPE_HHDLC },
265
266 /* RFC 2625 IP-over-Fibre Channel */
267 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
268
269 /* Solaris+SunATM */
270 { DLT_SUNATM, LINKTYPE_SUNATM },
271
272 /*
273 * Any platform that defines additional DLT_* codes should:
274 *
275 * request a LINKTYPE_* code and value from tcpdump.org,
276 * as per the above;
277 *
278 * add, in their version of libpcap, an entry to map
279 * those DLT_* codes to the corresponding LINKTYPE_*
280 * code;
281 *
282 * redefine, in their "net/bpf.h", any DLT_* values
283 * that collide with the values used by their additional
284 * DLT_* codes, to remove those collisions (but without
285 * making them collide with any of the LINKTYPE_*
286 * values equal to 50 or above; they should also avoid
287 * defining DLT_* values that collide with those
288 * LINKTYPE_* values, either).
289 */
290 { -1, -1 }
291 };
292
293 static int
294 dlt_to_linktype(int dlt)
295 {
296 int i;
297
298 for (i = 0; map[i].dlt != -1; i++) {
299 if (map[i].dlt == dlt)
300 return (map[i].linktype);
301 }
302
303 /*
304 * If we don't have a mapping for this DLT_ code, return an
305 * error; that means that the table above needs to have an
306 * entry added.
307 */
308 return (-1);
309 }
310
311 static int
312 linktype_to_dlt(int linktype)
313 {
314 int i;
315
316 for (i = 0; map[i].linktype != -1; i++) {
317 if (map[i].linktype == linktype)
318 return (map[i].dlt);
319 }
320
321 /*
322 * If we don't have an entry for this link type, return
323 * the link type value; it may be a DLT_ value from an
324 * older version of libpcap.
325 */
326 return linktype;
327 }
328
329 static int
330 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
331 {
332 struct pcap_file_header hdr;
333
334 hdr.magic = TCPDUMP_MAGIC;
335 hdr.version_major = PCAP_VERSION_MAJOR;
336 hdr.version_minor = PCAP_VERSION_MINOR;
337
338 hdr.thiszone = thiszone;
339 hdr.snaplen = snaplen;
340 hdr.sigfigs = 0;
341 hdr.linktype = linktype;
342
343 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
344 return (-1);
345
346 return (0);
347 }
348
349 static void
350 swap_hdr(struct pcap_file_header *hp)
351 {
352 hp->version_major = SWAPSHORT(hp->version_major);
353 hp->version_minor = SWAPSHORT(hp->version_minor);
354 hp->thiszone = SWAPLONG(hp->thiszone);
355 hp->sigfigs = SWAPLONG(hp->sigfigs);
356 hp->snaplen = SWAPLONG(hp->snaplen);
357 hp->linktype = SWAPLONG(hp->linktype);
358 }
359
360 pcap_t *
361 pcap_open_offline(const char *fname, char *errbuf)
362 {
363 register pcap_t *p;
364 register FILE *fp;
365 struct pcap_file_header hdr;
366 bpf_u_int32 magic;
367 int linklen;
368
369 p = (pcap_t *)malloc(sizeof(*p));
370 if (p == NULL) {
371 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
372 return (NULL);
373 }
374
375 memset((char *)p, 0, sizeof(*p));
376 /*
377 * Set this field so we don't close stdin in pcap_close!
378 */
379 p->fd = -1;
380
381 if (fname[0] == '-' && fname[1] == '\0')
382 fp = stdin;
383 else {
384 fp = fopen(fname, "r");
385 if (fp == NULL) {
386 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
387 pcap_strerror(errno));
388 goto bad;
389 }
390 }
391 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
392 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
393 pcap_strerror(errno));
394 goto bad;
395 }
396 magic = hdr.magic;
397 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
398 magic = SWAPLONG(magic);
399 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
400 snprintf(errbuf, PCAP_ERRBUF_SIZE,
401 "bad dump file format");
402 goto bad;
403 }
404 p->sf.swapped = 1;
405 swap_hdr(&hdr);
406 }
407 if (magic == PATCHED_TCPDUMP_MAGIC) {
408 /*
409 * XXX - the patch that's in some versions of libpcap
410 * changes the packet header but not the magic number;
411 * we'd have to use some hacks^H^H^H^H^Hheuristics to
412 * detect that.
413 */
414 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
415 } else
416 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
417 if (hdr.version_major < PCAP_VERSION_MAJOR) {
418 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
419 goto bad;
420 }
421 p->tzoff = hdr.thiszone;
422 p->snapshot = hdr.snaplen;
423 p->linktype = linktype_to_dlt(hdr.linktype);
424 p->sf.rfile = fp;
425 p->bufsize = hdr.snaplen;
426
427 /* Align link header as required for proper data alignment */
428 /* XXX should handle all types */
429 switch (p->linktype) {
430
431 case DLT_EN10MB:
432 linklen = 14;
433 break;
434
435 case DLT_FDDI:
436 linklen = 13 + 8; /* fddi_header + llc */
437 break;
438
439 case DLT_NULL:
440 default:
441 linklen = 0;
442 break;
443 }
444
445 if (p->bufsize < 0)
446 p->bufsize = BPF_MAXBUFSIZE;
447 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
448 if (p->sf.base == NULL) {
449 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
450 goto bad;
451 }
452 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
453 p->sf.version_major = hdr.version_major;
454 p->sf.version_minor = hdr.version_minor;
455 #ifdef PCAP_FDDIPAD
456 /* XXX padding only needed for kernel fcode */
457 pcap_fddipad = 0;
458 #endif
459
460 return (p);
461 bad:
462 free(p);
463 return (NULL);
464 }
465
466 /*
467 * Read sf_readfile and return the next packet. Return the header in hdr
468 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
469 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
470 */
471 static int
472 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
473 {
474 struct pcap_sf_patched_pkthdr sf_hdr;
475 FILE *fp = p->sf.rfile;
476
477 /*
478 * Read the packet header; the structure we use as a buffer
479 * is the longer structure for files generated by the patched
480 * libpcap, but if the file has the magic number for an
481 * unpatched libpcap we only read as many bytes as the regular
482 * header has.
483 */
484 if (fread(&sf_hdr, p->sf.hdrsize, 1, fp) != 1) {
485 /* probably an EOF, though could be a truncated packet */
486 return (1);
487 }
488
489 if (p->sf.swapped) {
490 /* these were written in opposite byte order */
491 hdr->caplen = SWAPLONG(sf_hdr.caplen);
492 hdr->len = SWAPLONG(sf_hdr.len);
493 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
494 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
495 } else {
496 hdr->caplen = sf_hdr.caplen;
497 hdr->len = sf_hdr.len;
498 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
499 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
500 }
501 /*
502 * We interchanged the caplen and len fields at version 2.3,
503 * in order to match the bpf header layout. But unfortunately
504 * some files were written with version 2.3 in their headers
505 * but without the interchanged fields.
506 */
507 if (p->sf.version_minor < 3 ||
508 (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
509 int t = hdr->caplen;
510 hdr->caplen = hdr->len;
511 hdr->len = t;
512 }
513
514 if (hdr->caplen > buflen) {
515 /*
516 * This can happen due to Solaris 2.3 systems tripping
517 * over the BUFMOD problem and not setting the snapshot
518 * correctly in the savefile header. If the caplen isn't
519 * grossly wrong, try to salvage.
520 */
521 static u_char *tp = NULL;
522 static int tsize = 0;
523
524 if (hdr->caplen > 65535) {
525 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
526 "bogus savefile header");
527 return (-1);
528 }
529
530 if (tsize < hdr->caplen) {
531 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
532 if (tp != NULL)
533 free((u_char *)tp);
534 tp = (u_char *)malloc(tsize);
535 if (tp == NULL) {
536 tsize = 0;
537 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
538 "BUFMOD hack malloc");
539 return (-1);
540 }
541 }
542 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
543 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
544 "truncated dump file");
545 return (-1);
546 }
547 /*
548 * We can only keep up to buflen bytes. Since caplen > buflen
549 * is exactly how we got here, we know we can only keep the
550 * first buflen bytes and must drop the remainder. Adjust
551 * caplen accordingly, so we don't get confused later as
552 * to how many bytes we have to play with.
553 */
554 hdr->caplen = buflen;
555 memcpy((char *)buf, (char *)tp, buflen);
556
557 } else {
558 /* read the packet itself */
559
560 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
561 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
562 "truncated dump file");
563 return (-1);
564 }
565 }
566 return (0);
567 }
568
569 /*
570 * Print out packets stored in the file initialized by sf_read_init().
571 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
572 */
573 int
574 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
575 {
576 struct bpf_insn *fcode = p->fcode.bf_insns;
577 int status = 0;
578 int n = 0;
579
580 while (status == 0) {
581 struct pcap_pkthdr h;
582
583 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
584 if (status) {
585 if (status == 1)
586 return (0);
587 return (status);
588 }
589
590 if (fcode == NULL ||
591 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
592 (*callback)(user, &h, p->buffer);
593 if (++n >= cnt && cnt > 0)
594 break;
595 }
596 }
597 /*XXX this breaks semantics tcpslice expects */
598 return (n);
599 }
600
601 /*
602 * Output a packet to the initialized dump file.
603 */
604 void
605 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
606 {
607 register FILE *f;
608 struct pcap_sf_pkthdr sf_hdr;
609
610 f = (FILE *)user;
611 sf_hdr.ts.tv_sec = h->ts.tv_sec;
612 sf_hdr.ts.tv_usec = h->ts.tv_usec;
613 sf_hdr.caplen = h->caplen;
614 sf_hdr.len = h->len;
615 /* XXX we should check the return status */
616 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
617 (void)fwrite((char *)sp, h->caplen, 1, f);
618 }
619
620 /*
621 * Initialize so that sf_write() will output to the file named 'fname'.
622 */
623 pcap_dumper_t *
624 pcap_dump_open(pcap_t *p, const char *fname)
625 {
626 FILE *f;
627 int linktype;
628
629 linktype = dlt_to_linktype(p->linktype);
630 if (linktype == -1) {
631 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
632 "%s: link-layer type %d isn't supported in savefiles",
633 fname, linktype);
634 return (NULL);
635 }
636
637 if (fname[0] == '-' && fname[1] == '\0')
638 f = stdout;
639 else {
640 f = fopen(fname, "w");
641 if (f == NULL) {
642 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
643 fname, pcap_strerror(errno));
644 return (NULL);
645 }
646 }
647 (void)sf_write_header(f, linktype, p->tzoff, p->snapshot);
648 return ((pcap_dumper_t *)f);
649 }
650
651 void
652 pcap_dump_close(pcap_dumper_t *p)
653 {
654
655 #ifdef notyet
656 if (ferror((FILE *)p))
657 return-an-error;
658 /* XXX should check return from fclose() too */
659 #endif
660 (void)fclose((FILE *)p);
661 }