]>
git.saurik.com Git - apple/network_cmds.git/blob - pcap/savefile.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: savefile.c,v 1.4 1996/07/12 13:19:13 mickey Exp $ */
27 * Copyright (c) 1993, 1994, 1995
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that: (1) source code distributions
32 * retain the above copyright notice and this paragraph in its entirety, (2)
33 * distributions including binary code include the above copyright notice and
34 * this paragraph in its entirety in the documentation or other materials
35 * provided with the distribution, and (3) all advertising materials mentioning
36 * features or use of this software display the following acknowledgement:
37 * ``This product includes software developed by the University of California,
38 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
39 * the University nor the names of its contributors may be used to endorse
40 * or promote products derived from this software without specific prior
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
43 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
48 "@(#)Header: savefile.c,v 1.28 95/10/07 03:09:06 leres Exp (LBL)";
52 * savefile.c - supports offline use of tcpdump
53 * Extraction/creation by Jeffrey Mogul, DECWRL
54 * Modified by Steve McCanne, LBL.
56 * Used to save the received packet headers, after filtering, to
57 * a file, and then read them later.
58 * The first record in the file contains saved values for the machine
59 * dependent values so we can print the dump file on any architecture.
62 #include <sys/types.h>
73 #ifdef HAVE_OS_PROTO_H
79 #define TCPDUMP_MAGIC 0xa1b2c3d4
82 * We use the "receiver-makes-right" approach to byte order,
83 * because time is at a premium when we are writing the file.
84 * In other words, the pcap_file_header and pcap_pkthdr,
85 * records are written in host byte order.
86 * Note that the packets are always written in network byte order.
88 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
89 * machine (if the file was written in little-end order).
92 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
93 #define SWAPSHORT(y) \
94 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
97 #define SFERR_BADVERSION 2
99 #define SFERR_EOF 4 /* not really an error, just a status */
102 sf_write_header(FILE *fp
, int linktype
, int thiszone
, int snaplen
)
104 struct pcap_file_header hdr
;
106 hdr
.magic
= TCPDUMP_MAGIC
;
107 hdr
.version_major
= PCAP_VERSION_MAJOR
;
108 hdr
.version_minor
= PCAP_VERSION_MINOR
;
110 hdr
.thiszone
= thiszone
;
111 hdr
.snaplen
= snaplen
;
113 hdr
.linktype
= linktype
;
115 if (fwrite((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1)
122 swap_hdr(struct pcap_file_header
*hp
)
124 hp
->version_major
= SWAPSHORT(hp
->version_major
);
125 hp
->version_minor
= SWAPSHORT(hp
->version_minor
);
126 hp
->thiszone
= SWAPLONG(hp
->thiszone
);
127 hp
->sigfigs
= SWAPLONG(hp
->sigfigs
);
128 hp
->snaplen
= SWAPLONG(hp
->snaplen
);
129 hp
->linktype
= SWAPLONG(hp
->linktype
);
133 pcap_open_offline(char *fname
, char *errbuf
)
137 struct pcap_file_header hdr
;
140 p
= (pcap_t
*)malloc(sizeof(*p
));
142 strcpy(errbuf
, "out of swap");
146 memset((char *)p
, 0, sizeof(*p
));
148 * Set this field so we don't close stdin in pcap_close!
152 if (fname
[0] == '-' && fname
[1] == '\0')
155 fp
= fopen(fname
, "r");
157 sprintf(errbuf
, "%s: %s", fname
, pcap_strerror(errno
));
161 if (fread((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1) {
162 sprintf(errbuf
, "fread: %s", pcap_strerror(errno
));
165 if (hdr
.magic
!= TCPDUMP_MAGIC
) {
166 if (SWAPLONG(hdr
.magic
) != TCPDUMP_MAGIC
) {
167 sprintf(errbuf
, "bad dump file format");
173 if (hdr
.version_major
< PCAP_VERSION_MAJOR
) {
174 sprintf(errbuf
, "archaic file format");
177 p
->tzoff
= hdr
.thiszone
;
178 p
->snapshot
= hdr
.snaplen
;
179 p
->linktype
= hdr
.linktype
;
181 p
->bufsize
= hdr
.snaplen
;
183 /* Align link header as required for proper data alignment */
184 /* XXX should handle all types */
185 switch (p
->linktype
) {
192 linklen
= 13 + 8; /* fddi_header + llc */
201 p
->sf
.base
= (u_char
*)malloc(p
->bufsize
+ BPF_ALIGNMENT
);
202 p
->buffer
= p
->sf
.base
+ BPF_ALIGNMENT
- (linklen
% BPF_ALIGNMENT
);
203 p
->sf
.version_major
= hdr
.version_major
;
204 p
->sf
.version_minor
= hdr
.version_minor
;
213 * Read sf_readfile and return the next packet. Return the header in hdr
214 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
215 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
218 sf_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
*buf
, int buflen
)
220 FILE *fp
= p
->sf
.rfile
;
223 if (fread((char *)hdr
, sizeof(struct pcap_pkthdr
), 1, fp
) != 1) {
224 /* probably an EOF, though could be a truncated packet */
229 /* these were written in opposite byte order */
230 hdr
->caplen
= SWAPLONG(hdr
->caplen
);
231 hdr
->len
= SWAPLONG(hdr
->len
);
232 hdr
->ts
.tv_sec
= SWAPLONG(hdr
->ts
.tv_sec
);
233 hdr
->ts
.tv_usec
= SWAPLONG(hdr
->ts
.tv_usec
);
236 * We interchanged the caplen and len fields at version 2.3,
237 * in order to match the bpf header layout. But unfortunately
238 * some files were written with version 2.3 in their headers
239 * but without the interchanged fields.
241 if (p
->sf
.version_minor
< 3 ||
242 (p
->sf
.version_minor
== 3 && hdr
->caplen
> hdr
->len
)) {
244 hdr
->caplen
= hdr
->len
;
248 if (hdr
->caplen
> buflen
) {
250 * This can happen due to Solaris 2.3 systems tripping
251 * over the BUFMOD problem and not setting the snapshot
252 * correctly in the savefile header. If the caplen isn't
253 * grossly wrong, try to salvage.
255 static u_char
*tp
= NULL
;
256 static int tsize
= 0;
258 if (tsize
< hdr
->caplen
) {
259 tsize
= ((hdr
->caplen
+ 1023) / 1024) * 1024;
262 tp
= (u_char
*)malloc(tsize
);
264 sprintf(p
->errbuf
, "BUFMOD hack malloc");
268 if (fread((char *)tp
, hdr
->caplen
, 1, fp
) != 1) {
269 sprintf(p
->errbuf
, "truncated dump file");
272 memcpy((char *)buf
, (char *)tp
, buflen
);
275 /* read the packet itself */
277 if (fread((char *)buf
, hdr
->caplen
, 1, fp
) != 1) {
278 sprintf(p
->errbuf
, "truncated dump file");
286 * Print out packets stored in the file initialized by sf_read_init().
287 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
290 pcap_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
292 struct bpf_insn
*fcode
= p
->fcode
.bf_insns
;
296 while (status
== 0) {
297 struct pcap_pkthdr h
;
299 status
= sf_next_packet(p
, &h
, p
->buffer
, p
->bufsize
);
307 bpf_filter(fcode
, p
->buffer
, h
.len
, h
.caplen
)) {
308 (*callback
)(user
, &h
, p
->buffer
);
309 if (++n
>= cnt
&& cnt
> 0)
313 /*XXX this breaks semantics tcpslice expects */
318 * Output a packet to the initialized dump file.
321 pcap_dump(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
326 /* XXX we should check the return status */
327 (void)fwrite((char *)h
, sizeof(*h
), 1, f
);
328 (void)fwrite((char *)sp
, h
->caplen
, 1, f
);
332 * Initialize so that sf_write() will output to the file named 'fname'.
335 pcap_dump_open(pcap_t
*p
, char *fname
)
338 if (fname
[0] == '-' && fname
[1] == '\0')
341 f
= fopen(fname
, "w");
343 sprintf(p
->errbuf
, "%s: %s",
344 fname
, pcap_strerror(errno
));
348 (void)sf_write_header(f
, p
->linktype
, p
->tzoff
, p
->snapshot
);
349 return ((pcap_dumper_t
*)f
);
353 pcap_dump_close(pcap_dumper_t
*p
)
357 if (ferror((FILE *)p
))
359 /* XXX should check return from fclose() too */
361 (void)fclose((FILE *)p
);