1 /* $OpenBSD: savefile.c,v 1.9 2006/03/26 20:58:51 djm Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * savefile.c - supports offline use of tcpdump
24 * Extraction/creation by Jeffrey Mogul, DECWRL
25 * Modified by Steve McCanne, LBL.
26 *
27 * Used to save the received packet headers, after filtering, to
28 * a file, and then read them later.
29 * The first record in the file contains saved values for the machine
30 * dependent values so we can print the dump file on any architecture.
31 */
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35
36 #include <errno.h>
37 #include <memory.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #ifdef HAVE_OS_PROTO_H
43 #include "os-proto.h"
44 #endif
45
46 #include "pcap-int.h"
47
48 __RCSID("$MirOS: src/lib/libpcap/savefile.c,v 1.2 2007/08/24 14:20:02 tg Exp $");
49
50 #define TCPDUMP_MAGIC 0xa1b2c3d4
51
52 /*
53 * We use the "receiver-makes-right" approach to byte order,
54 * because time is at a premium when we are writing the file.
55 * In other words, the pcap_file_header and pcap_pkthdr,
56 * records are written in host byte order.
57 * Note that the packets are always written in network byte order.
58 *
59 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
60 * machine (if the file was written in little-end order).
61 */
62 #define SWAPLONG(y) \
63 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
64 #define SWAPSHORT(y) \
65 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
66
67 #define SFERR_TRUNC 1
68 #define SFERR_BADVERSION 2
69 #define SFERR_BADF 3
70 #define SFERR_EOF 4 /* not really an error, just a status */
71
72 static int
sf_write_header(FILE * fp,int linktype,int thiszone,int snaplen)73 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
74 {
75 struct pcap_file_header hdr;
76
77 hdr.magic = TCPDUMP_MAGIC;
78 hdr.version_major = PCAP_VERSION_MAJOR;
79 hdr.version_minor = PCAP_VERSION_MINOR;
80
81 hdr.thiszone = thiszone;
82 hdr.snaplen = snaplen;
83 hdr.sigfigs = 0;
84 hdr.linktype = linktype;
85
86 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
87 return (-1);
88
89 return (0);
90 }
91
92 static void
swap_hdr(struct pcap_file_header * hp)93 swap_hdr(struct pcap_file_header *hp)
94 {
95 hp->version_major = SWAPSHORT(hp->version_major);
96 hp->version_minor = SWAPSHORT(hp->version_minor);
97 hp->thiszone = SWAPLONG(hp->thiszone);
98 hp->sigfigs = SWAPLONG(hp->sigfigs);
99 hp->snaplen = SWAPLONG(hp->snaplen);
100 hp->linktype = SWAPLONG(hp->linktype);
101 }
102
103 pcap_t *
pcap_open_offline(const char * fname,char * errbuf)104 pcap_open_offline(const char *fname, char *errbuf)
105 {
106 pcap_t *p;
107 FILE *fp;
108
109 if (fname[0] == '-' && fname[1] == '\0')
110 fp = stdin;
111 else {
112 fp = fopen(fname, "r");
113 if (fp == NULL) {
114 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
115 pcap_strerror(errno));
116 return (NULL);
117 }
118 }
119 p = pcap_fopen_offline(fp, errbuf);
120 if (p == NULL) {
121 if (fp != stdin)
122 fclose(fp);
123 }
124 return (p);
125 }
126
127 pcap_t *
pcap_fopen_offline(FILE * fp,char * errbuf)128 pcap_fopen_offline(FILE *fp, char *errbuf)
129 {
130 pcap_t *p;
131 struct pcap_file_header hdr;
132 int linklen;
133
134 p = (pcap_t *)malloc(sizeof(*p));
135 if (p == NULL) {
136 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
137 return (NULL);
138 }
139
140 memset((char *)p, 0, sizeof(*p));
141 /*
142 * Set this field so we don't double-close in pcap_close!
143 */
144 p->fd = -1;
145
146 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
147 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
148 pcap_strerror(errno));
149 goto bad;
150 }
151 if (hdr.magic != TCPDUMP_MAGIC) {
152 if (SWAPLONG(hdr.magic) != TCPDUMP_MAGIC) {
153 snprintf(errbuf, PCAP_ERRBUF_SIZE,
154 "bad dump file format");
155 goto bad;
156 }
157 p->sf.swapped = 1;
158 swap_hdr(&hdr);
159 }
160 if (hdr.version_major < PCAP_VERSION_MAJOR) {
161 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
162 goto bad;
163 }
164 p->tzoff = hdr.thiszone;
165 p->snapshot = hdr.snaplen;
166 p->linktype = hdr.linktype;
167 p->sf.rfile = fp;
168 p->bufsize = hdr.snaplen;
169
170 /* Align link header as required for proper data alignment */
171 /* XXX should handle all types */
172 switch (p->linktype) {
173
174 case DLT_EN10MB:
175 linklen = 14;
176 break;
177
178 case DLT_FDDI:
179 linklen = 13 + 8; /* fddi_header + llc */
180 break;
181
182 case DLT_NULL:
183 default:
184 linklen = 0;
185 break;
186 }
187
188 if (p->bufsize < 0)
189 p->bufsize = BPF_MAXBUFSIZE;
190 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
191 if (p->sf.base == NULL) {
192 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
193 goto bad;
194 }
195 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
196 p->sf.version_major = hdr.version_major;
197 p->sf.version_minor = hdr.version_minor;
198 #ifdef PCAP_FDDIPAD
199 /* XXX padding only needed for kernel fcode */
200 pcap_fddipad = 0;
201 #endif
202
203 return (p);
204 bad:
205 free(p);
206 return (NULL);
207 }
208
209 /*
210 * Read sf_readfile and return the next packet. Return the header in hdr
211 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
212 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
213 */
214 static int
sf_next_packet(pcap_t * p,struct pcap_pkthdr * hdr,u_char * buf,int buflen)215 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
216 {
217 FILE *fp = p->sf.rfile;
218
219 /* read the stamp */
220 if (fread((char *)hdr, sizeof(struct pcap_pkthdr), 1, fp) != 1) {
221 /* probably an EOF, though could be a truncated packet */
222 return (1);
223 }
224
225 if (p->sf.swapped) {
226 /* these were written in opposite byte order */
227 hdr->caplen = SWAPLONG(hdr->caplen);
228 hdr->len = SWAPLONG(hdr->len);
229 hdr->ts.tv_sec = SWAPLONG(hdr->ts.tv_sec);
230 hdr->ts.tv_usec = SWAPLONG(hdr->ts.tv_usec);
231 }
232 /*
233 * We interchanged the caplen and len fields at version 2.3,
234 * in order to match the bpf header layout. But unfortunately
235 * some files were written with version 2.3 in their headers
236 * but without the interchanged fields.
237 */
238 if (p->sf.version_minor < 3 ||
239 (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
240 int t = hdr->caplen;
241 hdr->caplen = hdr->len;
242 hdr->len = t;
243 }
244
245 if (hdr->caplen > buflen) {
246 /*
247 * This can happen due to Solaris 2.3 systems tripping
248 * over the BUFMOD problem and not setting the snapshot
249 * correctly in the savefile header. If the caplen isn't
250 * grossly wrong, try to salvage.
251 */
252 static u_char *tp = NULL;
253 static int tsize = 0;
254
255 if (hdr->caplen > 65535) {
256 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
257 "bogus savefile header");
258 return (-1);
259 }
260
261 if (tsize < hdr->caplen) {
262 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
263 if (tp != NULL)
264 free((u_char *)tp);
265 tp = (u_char *)malloc(tsize);
266 if (tp == NULL) {
267 tsize = 0;
268 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
269 "BUFMOD hack malloc");
270 return (-1);
271 }
272 }
273 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
274 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
275 "truncated dump file");
276 return (-1);
277 }
278 /*
279 * We can only keep up to buflen bytes. Since caplen > buflen
280 * is exactly how we got here, we know we can only keep the
281 * first buflen bytes and must drop the remainder. Adjust
282 * caplen accordingly, so we don't get confused later as
283 * to how many bytes we have to play with.
284 */
285 hdr->caplen = buflen;
286 memcpy((char *)buf, (char *)tp, buflen);
287
288 } else {
289 /* read the packet itself */
290
291 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
292 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
293 "truncated dump file");
294 return (-1);
295 }
296 }
297 return (0);
298 }
299
300 /*
301 * Print out packets stored in the file initialized by sf_read_init().
302 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
303 */
304 int
pcap_offline_read(pcap_t * p,int cnt,pcap_handler callback,u_char * user)305 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
306 {
307 struct bpf_insn *fcode = p->fcode.bf_insns;
308 int status = 0;
309 int n = 0;
310
311 while (status == 0) {
312 struct pcap_pkthdr h;
313
314 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
315 if (status) {
316 if (status == 1)
317 return (0);
318 return (status);
319 }
320
321 if (fcode == NULL ||
322 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
323 (*callback)(user, &h, p->buffer);
324 if (++n >= cnt && cnt > 0)
325 break;
326 }
327 }
328 /*XXX this breaks semantics tcpslice expects */
329 return (n);
330 }
331
332 /*
333 * Output a packet to the initialized dump file.
334 */
335 void
pcap_dump(u_char * user,const struct pcap_pkthdr * h,const u_char * sp)336 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
337 {
338 register FILE *f;
339
340 f = (FILE *)user;
341 /* XXX we should check the return status */
342 (void)fwrite((char *)h, sizeof(*h), 1, f);
343 (void)fwrite((char *)sp, h->caplen, 1, f);
344 }
345
346 static pcap_dumper_t *
pcap_setup_dump(pcap_t * p,FILE * f,const char * fname)347 pcap_setup_dump(pcap_t *p, FILE *f, const char *fname)
348 {
349 if (sf_write_header(f, p->linktype, p->tzoff, p->snapshot) == -1) {
350 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
351 fname, pcap_strerror(errno));
352 if (f != stdout)
353 (void)fclose(f);
354 return (NULL);
355 }
356 return ((pcap_dumper_t *)f);
357 }
358
359 /*
360 * Initialize so that sf_write() will output to the file named 'fname'.
361 */
362 pcap_dumper_t *
pcap_dump_open(pcap_t * p,const char * fname)363 pcap_dump_open(pcap_t *p, const char *fname)
364 {
365 FILE *f;
366 if (fname[0] == '-' && fname[1] == '\0')
367 f = stdout;
368 else {
369 f = fopen(fname, "w");
370 if (f == NULL) {
371 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
372 fname, pcap_strerror(errno));
373 return (NULL);
374 }
375 }
376 return (pcap_setup_dump(p, f, fname));
377 }
378
379 /*
380 * Initialize so that sf_write() will output to the given stream.
381 */
382 pcap_dumper_t *
pcap_dump_fopen(pcap_t * p,FILE * f)383 pcap_dump_fopen(pcap_t *p, FILE *f)
384 {
385 return (pcap_setup_dump(p, f, "stream"));
386 }
387
388 FILE *
pcap_dump_file(pcap_dumper_t * p)389 pcap_dump_file(pcap_dumper_t *p)
390 {
391 return ((FILE *)p);
392 }
393
394 long
pcap_dump_ftell(pcap_dumper_t * p)395 pcap_dump_ftell(pcap_dumper_t *p)
396 {
397 return (ftell((FILE *)p));
398 }
399
400 int
pcap_dump_flush(pcap_dumper_t * p)401 pcap_dump_flush(pcap_dumper_t *p)
402 {
403
404 if (fflush((FILE *)p) == EOF)
405 return (-1);
406 else
407 return (0);
408 }
409
410 void
pcap_dump_close(pcap_dumper_t * p)411 pcap_dump_close(pcap_dumper_t *p)
412 {
413
414 #ifdef notyet
415 if (ferror((FILE *)p))
416 return-an-error;
417 /* XXX should check return from fclose() too */
418 #endif
419 (void)fclose((FILE *)p);
420 }
421