]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
ac0de87894c51cf54a74164df5da6642cb74c480
[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.41 2000-07-18 03:43:47 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 <unistd.h>
48
49 #include "pcap-int.h"
50
51 #ifdef HAVE_OS_PROTO_H
52 #include "os-proto.h"
53 #endif
54
55 #define TCPDUMP_MAGIC 0xa1b2c3d4
56 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34
57
58 /*
59 * We use the "receiver-makes-right" approach to byte order,
60 * because time is at a premium when we are writing the file.
61 * In other words, the pcap_file_header and pcap_pkthdr,
62 * records are written in host byte order.
63 * Note that the packets are always written in network byte order.
64 *
65 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
66 * machine (if the file was written in little-end order).
67 */
68 #define SWAPLONG(y) \
69 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
70 #define SWAPSHORT(y) \
71 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
72
73 #define SFERR_TRUNC 1
74 #define SFERR_BADVERSION 2
75 #define SFERR_BADF 3
76 #define SFERR_EOF 4 /* not really an error, just a status */
77
78 static int
79 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
80 {
81 struct pcap_file_header hdr;
82
83 hdr.magic = TCPDUMP_MAGIC;
84 hdr.version_major = PCAP_VERSION_MAJOR;
85 hdr.version_minor = PCAP_VERSION_MINOR;
86
87 hdr.thiszone = thiszone;
88 hdr.snaplen = snaplen;
89 hdr.sigfigs = 0;
90 hdr.linktype = linktype;
91
92 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
93 return (-1);
94
95 return (0);
96 }
97
98 static void
99 swap_hdr(struct pcap_file_header *hp)
100 {
101 hp->version_major = SWAPSHORT(hp->version_major);
102 hp->version_minor = SWAPSHORT(hp->version_minor);
103 hp->thiszone = SWAPLONG(hp->thiszone);
104 hp->sigfigs = SWAPLONG(hp->sigfigs);
105 hp->snaplen = SWAPLONG(hp->snaplen);
106 hp->linktype = SWAPLONG(hp->linktype);
107 }
108
109 pcap_t *
110 pcap_open_offline(const char *fname, char *errbuf)
111 {
112 register pcap_t *p;
113 register FILE *fp;
114 struct pcap_file_header hdr;
115 bpf_u_int32 magic;
116 int linklen;
117
118 p = (pcap_t *)malloc(sizeof(*p));
119 if (p == NULL) {
120 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
121 return (NULL);
122 }
123
124 memset((char *)p, 0, sizeof(*p));
125 /*
126 * Set this field so we don't close stdin in pcap_close!
127 */
128 p->fd = -1;
129
130 if (fname[0] == '-' && fname[1] == '\0')
131 fp = stdin;
132 else {
133 fp = fopen(fname, "r");
134 if (fp == NULL) {
135 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
136 pcap_strerror(errno));
137 goto bad;
138 }
139 }
140 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
141 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
142 pcap_strerror(errno));
143 goto bad;
144 }
145 magic = hdr.magic;
146 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
147 magic = SWAPLONG(magic);
148 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
149 snprintf(errbuf, PCAP_ERRBUF_SIZE,
150 "bad dump file format");
151 goto bad;
152 }
153 p->sf.swapped = 1;
154 swap_hdr(&hdr);
155 }
156 if (magic == PATCHED_TCPDUMP_MAGIC) {
157 /*
158 * XXX - the patch that's in some versions of libpcap
159 * changes the packet header but not the magic number;
160 * we'd have to use some hacks^H^H^H^H^Hheuristics to
161 * detect that.
162 */
163 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
164 } else
165 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
166 if (hdr.version_major < PCAP_VERSION_MAJOR) {
167 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
168 goto bad;
169 }
170 p->tzoff = hdr.thiszone;
171 p->snapshot = hdr.snaplen;
172 p->linktype = hdr.linktype;
173 p->sf.rfile = fp;
174 p->bufsize = hdr.snaplen;
175
176 /* Align link header as required for proper data alignment */
177 /* XXX should handle all types */
178 switch (p->linktype) {
179
180 case DLT_EN10MB:
181 linklen = 14;
182 break;
183
184 case DLT_FDDI:
185 linklen = 13 + 8; /* fddi_header + llc */
186 break;
187
188 case DLT_NULL:
189 default:
190 linklen = 0;
191 break;
192 }
193
194 if (p->bufsize < 0)
195 p->bufsize = BPF_MAXBUFSIZE;
196 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
197 if (p->sf.base == NULL) {
198 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
199 goto bad;
200 }
201 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
202 p->sf.version_major = hdr.version_major;
203 p->sf.version_minor = hdr.version_minor;
204 #ifdef PCAP_FDDIPAD
205 /* XXX padding only needed for kernel fcode */
206 pcap_fddipad = 0;
207 #endif
208
209 return (p);
210 bad:
211 free(p);
212 return (NULL);
213 }
214
215 /*
216 * Read sf_readfile and return the next packet. Return the header in hdr
217 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
218 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
219 */
220 static int
221 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
222 {
223 struct pcap_sf_patched_pkthdr sf_hdr;
224 FILE *fp = p->sf.rfile;
225
226 /*
227 * Read the packet header; the structure we use as a buffer
228 * is the longer structure for files generated by the patched
229 * libpcap, but if the file has the magic number for an
230 * unpatched libpcap we only read as many bytes as the regular
231 * header has.
232 */
233 if (fread(&sf_hdr, p->sf.hdrsize, 1, fp) != 1) {
234 /* probably an EOF, though could be a truncated packet */
235 return (1);
236 }
237
238 if (p->sf.swapped) {
239 /* these were written in opposite byte order */
240 hdr->caplen = SWAPLONG(sf_hdr.caplen);
241 hdr->len = SWAPLONG(sf_hdr.len);
242 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
243 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
244 } else {
245 hdr->caplen = sf_hdr.caplen;
246 hdr->len = sf_hdr.len;
247 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
248 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
249 }
250 /*
251 * We interchanged the caplen and len fields at version 2.3,
252 * in order to match the bpf header layout. But unfortunately
253 * some files were written with version 2.3 in their headers
254 * but without the interchanged fields.
255 */
256 if (p->sf.version_minor < 3 ||
257 (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
258 int t = hdr->caplen;
259 hdr->caplen = hdr->len;
260 hdr->len = t;
261 }
262
263 if (hdr->caplen > buflen) {
264 /*
265 * This can happen due to Solaris 2.3 systems tripping
266 * over the BUFMOD problem and not setting the snapshot
267 * correctly in the savefile header. If the caplen isn't
268 * grossly wrong, try to salvage.
269 */
270 static u_char *tp = NULL;
271 static int tsize = 0;
272
273 if (hdr->caplen > 65535) {
274 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
275 "bogus savefile header");
276 return (-1);
277 }
278
279 if (tsize < hdr->caplen) {
280 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
281 if (tp != NULL)
282 free((u_char *)tp);
283 tp = (u_char *)malloc(tsize);
284 if (tp == NULL) {
285 tsize = 0;
286 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
287 "BUFMOD hack malloc");
288 return (-1);
289 }
290 }
291 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
292 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
293 "truncated dump file");
294 return (-1);
295 }
296 /*
297 * We can only keep up to buflen bytes. Since caplen > buflen
298 * is exactly how we got here, we know we can only keep the
299 * first buflen bytes and must drop the remainder. Adjust
300 * caplen accordingly, so we don't get confused later as
301 * to how many bytes we have to play with.
302 */
303 hdr->caplen = buflen;
304 memcpy((char *)buf, (char *)tp, buflen);
305
306 } else {
307 /* read the packet itself */
308
309 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
310 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
311 "truncated dump file");
312 return (-1);
313 }
314 }
315 return (0);
316 }
317
318 /*
319 * Print out packets stored in the file initialized by sf_read_init().
320 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
321 */
322 int
323 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
324 {
325 struct bpf_insn *fcode = p->fcode.bf_insns;
326 int status = 0;
327 int n = 0;
328
329 while (status == 0) {
330 struct pcap_pkthdr h;
331
332 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
333 if (status) {
334 if (status == 1)
335 return (0);
336 return (status);
337 }
338
339 if (fcode == NULL ||
340 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
341 (*callback)(user, &h, p->buffer);
342 if (++n >= cnt && cnt > 0)
343 break;
344 }
345 }
346 /*XXX this breaks semantics tcpslice expects */
347 return (n);
348 }
349
350 /*
351 * Output a packet to the initialized dump file.
352 */
353 void
354 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
355 {
356 register FILE *f;
357 struct pcap_sf_pkthdr sf_hdr;
358
359 f = (FILE *)user;
360 sf_hdr.ts.tv_sec = h->ts.tv_sec;
361 sf_hdr.ts.tv_usec = h->ts.tv_usec;
362 sf_hdr.caplen = h->caplen;
363 sf_hdr.len = h->len;
364 /* XXX we should check the return status */
365 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
366 (void)fwrite((char *)sp, h->caplen, 1, f);
367 }
368
369 /*
370 * Initialize so that sf_write() will output to the file named 'fname'.
371 */
372 pcap_dumper_t *
373 pcap_dump_open(pcap_t *p, const char *fname)
374 {
375 FILE *f;
376 if (fname[0] == '-' && fname[1] == '\0')
377 f = stdout;
378 else {
379 f = fopen(fname, "w");
380 if (f == NULL) {
381 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
382 fname, pcap_strerror(errno));
383 return (NULL);
384 }
385 }
386 (void)sf_write_header(f, p->linktype, p->tzoff, p->snapshot);
387 return ((pcap_dumper_t *)f);
388 }
389
390 void
391 pcap_dump_close(pcap_dumper_t *p)
392 {
393
394 #ifdef notyet
395 if (ferror((FILE *)p))
396 return-an-error;
397 /* XXX should check return from fclose() too */
398 #endif
399 (void)fclose((FILE *)p);
400 }