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