Deleted Added
full compact
savefile.c (17683) savefile.c (26175)
1/*
2 * Copyright (c) 1993, 1994, 1995, 1996
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.
1/*
2 * Copyright (c) 1993, 1994, 1995, 1996
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#ifndef lint
22static char rcsid[] =
23 "@(#)$Header: savefile.c,v 1.30 96/07/15 00:48:52 leres Exp $ (LBL)";
24#endif
25
26/*
20 *
27 * savefile.c - supports offline use of tcpdump
28 * Extraction/creation by Jeffrey Mogul, DECWRL
29 * Modified by Steve McCanne, LBL.
30 *
31 * Used to save the received packet headers, after filtering, to
32 * a file, and then read them later.
33 * The first record in the file contains saved values for the machine
34 * dependent values so we can print the dump file on any architecture.
35 */
36
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
32static const char rcsid[] =
33 "@(#) $Header: savefile.c,v 1.36 96/12/10 23:15:02 leres Exp $ (LBL)";
34#endif
35
37#include <sys/types.h>
38#include <sys/time.h>
39
40#include <errno.h>
41#include <memory.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <unistd.h>

--- 15 unchanged lines hidden (view full) ---

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) \
36#include <sys/types.h>
37#include <sys/time.h>
38
39#include <errno.h>
40#include <memory.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <unistd.h>

--- 15 unchanged lines hidden (view full) ---

59 * Note that the packets are always written in network byte order.
60 *
61 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
62 * machine (if the file was written in little-end order).
63 */
64#define SWAPLONG(y) \
65((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
66#define SWAPSHORT(y) \
68 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
67 ( (((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
75static int
76sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)

--- 151 unchanged lines hidden (view full) ---

228 * This can happen due to Solaris 2.3 systems tripping
229 * over the BUFMOD problem and not setting the snapshot
230 * correctly in the savefile header. If the caplen isn't
231 * grossly wrong, try to salvage.
232 */
233 static u_char *tp = NULL;
234 static int tsize = 0;
235
68
69#define SFERR_TRUNC 1
70#define SFERR_BADVERSION 2
71#define SFERR_BADF 3
72#define SFERR_EOF 4 /* not really an error, just a status */
73
74static int
75sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)

--- 151 unchanged lines hidden (view full) ---

227 * This can happen due to Solaris 2.3 systems tripping
228 * over the BUFMOD problem and not setting the snapshot
229 * correctly in the savefile header. If the caplen isn't
230 * grossly wrong, try to salvage.
231 */
232 static u_char *tp = NULL;
233 static int tsize = 0;
234
235 if (hdr->caplen > 65535) {
236 sprintf(p->errbuf, "bogus savefile header");
237 return (-1);
238 }
236 if (tsize < hdr->caplen) {
237 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
238 if (tp != NULL)
239 free((u_char *)tp);
240 tp = (u_char *)malloc(tsize);
241 if (tp == NULL) {
239 if (tsize < hdr->caplen) {
240 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
241 if (tp != NULL)
242 free((u_char *)tp);
243 tp = (u_char *)malloc(tsize);
244 if (tp == NULL) {
245 tsize = 0;
242 sprintf(p->errbuf, "BUFMOD hack malloc");
243 return (-1);
244 }
245 }
246 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
247 sprintf(p->errbuf, "truncated dump file");
248 return (-1);
249 }
246 sprintf(p->errbuf, "BUFMOD hack malloc");
247 return (-1);
248 }
249 }
250 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
251 sprintf(p->errbuf, "truncated dump file");
252 return (-1);
253 }
254 /*
255 * We can only keep up to buflen bytes. Since caplen > buflen
256 * is exactly how we got here, we know we can only keep the
257 * first buflen bytes and must drop the remainder. Adjust
258 * caplen accordingly, so we don't get confused later as
259 * to how many bytes we have to play with.
260 */
261 hdr->caplen = buflen;
250 memcpy((char *)buf, (char *)tp, buflen);
251
252 } else {
253 /* read the packet itself */
254
255 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
256 sprintf(p->errbuf, "truncated dump file");
257 return (-1);

--- 83 unchanged lines hidden ---
262 memcpy((char *)buf, (char *)tp, buflen);
263
264 } else {
265 /* read the packet itself */
266
267 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
268 sprintf(p->errbuf, "truncated dump file");
269 return (-1);

--- 83 unchanged lines hidden ---