Home | History | Annotate | Line # | Download | only in dist
savefile.c revision 1.3
      1  1.3  christos /*	$NetBSD: savefile.c,v 1.3 2015/03/31 21:39:42 christos Exp $	*/
      2  1.2  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1993, 1994, 1995, 1996, 1997
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      9  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  1.1  christos  * distributions including binary code include the above copyright notice and
     11  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     12  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     13  1.1  christos  * features or use of this software display the following acknowledgement:
     14  1.1  christos  * ``This product includes software developed by the University of California,
     15  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  1.1  christos  * the University nor the names of its contributors may be used to endorse
     17  1.1  christos  * or promote products derived from this software without specific prior
     18  1.1  christos  * written permission.
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  1.1  christos  *
     23  1.1  christos  * savefile.c - supports offline use of tcpdump
     24  1.1  christos  *	Extraction/creation by Jeffrey Mogul, DECWRL
     25  1.1  christos  *	Modified by Steve McCanne, LBL.
     26  1.1  christos  *
     27  1.1  christos  * Used to save the received packet headers, after filtering, to
     28  1.1  christos  * a file, and then read them later.
     29  1.1  christos  * The first record in the file contains saved values for the machine
     30  1.1  christos  * dependent values so we can print the dump file on any architecture.
     31  1.1  christos  */
     32  1.1  christos 
     33  1.2  christos #include <sys/cdefs.h>
     34  1.3  christos __RCSID("$NetBSD: savefile.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
     35  1.1  christos 
     36  1.1  christos #ifdef HAVE_CONFIG_H
     37  1.1  christos #include "config.h"
     38  1.1  christos #endif
     39  1.1  christos 
     40  1.1  christos #ifdef WIN32
     41  1.1  christos #include <pcap-stdinc.h>
     42  1.1  christos #else /* WIN32 */
     43  1.1  christos #if HAVE_INTTYPES_H
     44  1.1  christos #include <inttypes.h>
     45  1.1  christos #elif HAVE_STDINT_H
     46  1.1  christos #include <stdint.h>
     47  1.1  christos #endif
     48  1.1  christos #ifdef HAVE_SYS_BITYPES_H
     49  1.1  christos #include <sys/bitypes.h>
     50  1.1  christos #endif
     51  1.1  christos #include <sys/types.h>
     52  1.1  christos #endif /* WIN32 */
     53  1.1  christos 
     54  1.1  christos #include <errno.h>
     55  1.1  christos #include <memory.h>
     56  1.1  christos #include <stdio.h>
     57  1.1  christos #include <stdlib.h>
     58  1.1  christos #include <string.h>
     59  1.1  christos 
     60  1.1  christos #include "pcap-int.h"
     61  1.1  christos #include "pcap/usb.h"
     62  1.1  christos 
     63  1.1  christos #ifdef HAVE_OS_PROTO_H
     64  1.1  christos #include "os-proto.h"
     65  1.1  christos #endif
     66  1.1  christos 
     67  1.1  christos #include "sf-pcap.h"
     68  1.1  christos #include "sf-pcap-ng.h"
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * Setting O_BINARY on DOS/Windows is a bit tricky
     72  1.1  christos  */
     73  1.1  christos #if defined(WIN32)
     74  1.1  christos   #define SET_BINMODE(f)  _setmode(_fileno(f), _O_BINARY)
     75  1.1  christos #elif defined(MSDOS)
     76  1.1  christos   #if defined(__HIGHC__)
     77  1.1  christos   #define SET_BINMODE(f)  setmode(f, O_BINARY)
     78  1.1  christos   #else
     79  1.1  christos   #define SET_BINMODE(f)  setmode(fileno(f), O_BINARY)
     80  1.1  christos   #endif
     81  1.1  christos #endif
     82  1.1  christos 
     83  1.1  christos static int
     84  1.1  christos sf_getnonblock(pcap_t *p, char *errbuf)
     85  1.1  christos {
     86  1.1  christos 	/*
     87  1.1  christos 	 * This is a savefile, not a live capture file, so never say
     88  1.1  christos 	 * it's in non-blocking mode.
     89  1.1  christos 	 */
     90  1.1  christos 	return (0);
     91  1.1  christos }
     92  1.1  christos 
     93  1.1  christos static int
     94  1.1  christos sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
     95  1.1  christos {
     96  1.1  christos 	/*
     97  1.2  christos 	 * This is a savefile, not a live capture file, so reject
     98  1.2  christos 	 * requests to put it in non-blocking mode.  (If it's a
     99  1.2  christos 	 * pipe, it could be put in non-blocking mode, but that
    100  1.2  christos 	 * would significantly complicate the code to read packets,
    101  1.2  christos 	 * as it would have to handle reading partial packets and
    102  1.2  christos 	 * keeping the state of the read.)
    103  1.1  christos 	 */
    104  1.2  christos 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    105  1.2  christos 	    "Savefiles cannot be put into non-blocking mode");
    106  1.2  christos 	return (-1);
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos static int
    110  1.1  christos sf_stats(pcap_t *p, struct pcap_stat *ps)
    111  1.1  christos {
    112  1.1  christos 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    113  1.1  christos 	    "Statistics aren't available from savefiles");
    114  1.1  christos 	return (-1);
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos #ifdef WIN32
    118  1.1  christos static int
    119  1.1  christos sf_setbuff(pcap_t *p, int dim)
    120  1.1  christos {
    121  1.1  christos 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    122  1.1  christos 	    "The kernel buffer size cannot be set while reading from a file");
    123  1.1  christos 	return (-1);
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos static int
    127  1.1  christos sf_setmode(pcap_t *p, int mode)
    128  1.1  christos {
    129  1.1  christos 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    130  1.1  christos 	    "impossible to set mode while reading from a file");
    131  1.1  christos 	return (-1);
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos static int
    135  1.1  christos sf_setmintocopy(pcap_t *p, int size)
    136  1.1  christos {
    137  1.1  christos 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    138  1.1  christos 	    "The mintocopy parameter cannot be set while reading from a file");
    139  1.1  christos 	return (-1);
    140  1.1  christos }
    141  1.1  christos #endif
    142  1.1  christos 
    143  1.1  christos static int
    144  1.1  christos sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
    145  1.1  christos {
    146  1.1  christos 	strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
    147  1.1  christos 	    PCAP_ERRBUF_SIZE);
    148  1.1  christos 	return (-1);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos /*
    152  1.1  christos  * Set direction flag: Which packets do we accept on a forwarding
    153  1.1  christos  * single device? IN, OUT or both?
    154  1.1  christos  */
    155  1.1  christos static int
    156  1.1  christos sf_setdirection(pcap_t *p, pcap_direction_t d)
    157  1.1  christos {
    158  1.1  christos 	snprintf(p->errbuf, sizeof(p->errbuf),
    159  1.1  christos 	    "Setting direction is not supported on savefiles");
    160  1.1  christos 	return (-1);
    161  1.1  christos }
    162  1.1  christos 
    163  1.2  christos void
    164  1.1  christos sf_cleanup(pcap_t *p)
    165  1.1  christos {
    166  1.2  christos 	if (p->rfile != stdin)
    167  1.2  christos 		(void)fclose(p->rfile);
    168  1.1  christos 	if (p->buffer != NULL)
    169  1.1  christos 		free(p->buffer);
    170  1.2  christos 	pcap_freecode(&p->fcode);
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos pcap_t *
    174  1.2  christos pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
    175  1.2  christos     char *errbuf)
    176  1.1  christos {
    177  1.1  christos 	FILE *fp;
    178  1.1  christos 	pcap_t *p;
    179  1.1  christos 
    180  1.1  christos 	if (fname[0] == '-' && fname[1] == '\0')
    181  1.1  christos 	{
    182  1.1  christos 		fp = stdin;
    183  1.1  christos #if defined(WIN32) || defined(MSDOS)
    184  1.1  christos 		/*
    185  1.1  christos 		 * We're reading from the standard input, so put it in binary
    186  1.1  christos 		 * mode, as savefiles are binary files.
    187  1.1  christos 		 */
    188  1.1  christos 		SET_BINMODE(fp);
    189  1.1  christos #endif
    190  1.1  christos 	}
    191  1.1  christos 	else {
    192  1.1  christos #if !defined(WIN32) && !defined(MSDOS)
    193  1.1  christos 		fp = fopen(fname, "r");
    194  1.1  christos #else
    195  1.1  christos 		fp = fopen(fname, "rb");
    196  1.1  christos #endif
    197  1.1  christos 		if (fp == NULL) {
    198  1.1  christos 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
    199  1.1  christos 			    pcap_strerror(errno));
    200  1.1  christos 			return (NULL);
    201  1.1  christos 		}
    202  1.1  christos 	}
    203  1.2  christos 	p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
    204  1.1  christos 	if (p == NULL) {
    205  1.1  christos 		if (fp != stdin)
    206  1.1  christos 			fclose(fp);
    207  1.1  christos 	}
    208  1.1  christos 	return (p);
    209  1.1  christos }
    210  1.1  christos 
    211  1.2  christos pcap_t *
    212  1.2  christos pcap_open_offline(const char *fname, char *errbuf)
    213  1.2  christos {
    214  1.2  christos 	return (pcap_open_offline_with_tstamp_precision(fname,
    215  1.2  christos 	    PCAP_TSTAMP_PRECISION_MICRO, errbuf));
    216  1.2  christos }
    217  1.2  christos 
    218  1.1  christos #ifdef WIN32
    219  1.2  christos pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
    220  1.2  christos     char *errbuf)
    221  1.1  christos {
    222  1.1  christos 	int fd;
    223  1.1  christos 	FILE *file;
    224  1.1  christos 
    225  1.1  christos 	fd = _open_osfhandle(osfd, _O_RDONLY);
    226  1.3  christos 	if ( fd < 0 )
    227  1.1  christos 	{
    228  1.1  christos 		snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno));
    229  1.1  christos 		return NULL;
    230  1.1  christos 	}
    231  1.1  christos 
    232  1.1  christos 	file = _fdopen(fd, "rb");
    233  1.3  christos 	if ( file == NULL )
    234  1.1  christos 	{
    235  1.1  christos 		snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno));
    236  1.1  christos 		return NULL;
    237  1.1  christos 	}
    238  1.1  christos 
    239  1.2  christos 	return pcap_fopen_offline_with_tstamp_precision(file, precision,
    240  1.2  christos 	    errbuf);
    241  1.2  christos }
    242  1.2  christos 
    243  1.2  christos pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
    244  1.2  christos {
    245  1.2  christos 	return pcap_hopen_offline_with_tstamp_precision(osfd,
    246  1.2  christos 	    PCAP_TSTAMP_PRECISION_MICRO, errbuf);
    247  1.1  christos }
    248  1.1  christos #endif
    249  1.1  christos 
    250  1.2  christos static pcap_t *(*check_headers[])(bpf_u_int32, FILE *, u_int, char *, int *) = {
    251  1.1  christos 	pcap_check_header,
    252  1.1  christos 	pcap_ng_check_header
    253  1.1  christos };
    254  1.1  christos 
    255  1.1  christos #define	N_FILE_TYPES	(sizeof check_headers / sizeof check_headers[0])
    256  1.1  christos 
    257  1.1  christos #ifdef WIN32
    258  1.1  christos static
    259  1.1  christos #endif
    260  1.1  christos pcap_t *
    261  1.2  christos pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
    262  1.2  christos     char *errbuf)
    263  1.1  christos {
    264  1.1  christos 	register pcap_t *p;
    265  1.1  christos 	bpf_u_int32 magic;
    266  1.1  christos 	size_t amt_read;
    267  1.1  christos 	u_int i;
    268  1.2  christos 	int err;
    269  1.1  christos 
    270  1.1  christos 	/*
    271  1.1  christos 	 * Read the first 4 bytes of the file; the network analyzer dump
    272  1.1  christos 	 * file formats we support (pcap and pcap-ng), and several other
    273  1.1  christos 	 * formats we might support in the future (such as snoop, DOS and
    274  1.1  christos 	 * Windows Sniffer, and Microsoft Network Monitor) all have magic
    275  1.1  christos 	 * numbers that are unique in their first 4 bytes.
    276  1.1  christos 	 */
    277  1.1  christos 	amt_read = fread((char *)&magic, 1, sizeof(magic), fp);
    278  1.1  christos 	if (amt_read != sizeof(magic)) {
    279  1.1  christos 		if (ferror(fp)) {
    280  1.1  christos 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
    281  1.1  christos 			    "error reading dump file: %s",
    282  1.1  christos 			    pcap_strerror(errno));
    283  1.1  christos 		} else {
    284  1.1  christos 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
    285  1.1  christos 			    "truncated dump file; tried to read %lu file header bytes, only got %lu",
    286  1.1  christos 			    (unsigned long)sizeof(magic),
    287  1.1  christos 			    (unsigned long)amt_read);
    288  1.1  christos 		}
    289  1.2  christos 		return (NULL);
    290  1.1  christos 	}
    291  1.1  christos 
    292  1.1  christos 	/*
    293  1.1  christos 	 * Try all file types.
    294  1.1  christos 	 */
    295  1.1  christos 	for (i = 0; i < N_FILE_TYPES; i++) {
    296  1.2  christos 		p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
    297  1.2  christos 		if (p != NULL) {
    298  1.2  christos 			/* Yup, that's it. */
    299  1.2  christos 			goto found;
    300  1.2  christos 		}
    301  1.2  christos 		if (err) {
    302  1.1  christos 			/*
    303  1.1  christos 			 * Error trying to read the header.
    304  1.1  christos 			 */
    305  1.2  christos 			return (NULL);
    306  1.1  christos 		}
    307  1.1  christos 	}
    308  1.1  christos 
    309  1.1  christos 	/*
    310  1.1  christos 	 * Well, who knows what this mess is....
    311  1.1  christos 	 */
    312  1.1  christos 	snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
    313  1.2  christos 	return (NULL);
    314  1.1  christos 
    315  1.1  christos found:
    316  1.2  christos 	p->rfile = fp;
    317  1.1  christos 
    318  1.1  christos 	/* Padding only needed for live capture fcode */
    319  1.1  christos 	p->fddipad = 0;
    320  1.1  christos 
    321  1.1  christos #if !defined(WIN32) && !defined(MSDOS)
    322  1.1  christos 	/*
    323  1.1  christos 	 * You can do "select()" and "poll()" on plain files on most
    324  1.1  christos 	 * platforms, and should be able to do so on pipes.
    325  1.1  christos 	 *
    326  1.1  christos 	 * You can't do "select()" on anything other than sockets in
    327  1.1  christos 	 * Windows, so, on Win32 systems, we don't have "selectable_fd".
    328  1.1  christos 	 */
    329  1.1  christos 	p->selectable_fd = fileno(fp);
    330  1.1  christos #endif
    331  1.1  christos 
    332  1.1  christos 	p->read_op = pcap_offline_read;
    333  1.1  christos 	p->inject_op = sf_inject;
    334  1.1  christos 	p->setfilter_op = install_bpf_program;
    335  1.1  christos 	p->setdirection_op = sf_setdirection;
    336  1.1  christos 	p->set_datalink_op = NULL;	/* we don't support munging link-layer headers */
    337  1.1  christos 	p->getnonblock_op = sf_getnonblock;
    338  1.1  christos 	p->setnonblock_op = sf_setnonblock;
    339  1.1  christos 	p->stats_op = sf_stats;
    340  1.1  christos #ifdef WIN32
    341  1.1  christos 	p->setbuff_op = sf_setbuff;
    342  1.1  christos 	p->setmode_op = sf_setmode;
    343  1.1  christos 	p->setmintocopy_op = sf_setmintocopy;
    344  1.1  christos #endif
    345  1.2  christos 
    346  1.2  christos 	/*
    347  1.2  christos 	 * For offline captures, the standard one-shot callback can
    348  1.2  christos 	 * be used for pcap_next()/pcap_next_ex().
    349  1.2  christos 	 */
    350  1.2  christos 	p->oneshot_callback = pcap_oneshot;
    351  1.2  christos 
    352  1.3  christos 	/*
    353  1.3  christos 	 * Savefiles never require special BPF code generation.
    354  1.3  christos 	 */
    355  1.3  christos 	p->bpf_codegen_flags = 0;
    356  1.3  christos 
    357  1.1  christos 	p->activated = 1;
    358  1.1  christos 
    359  1.1  christos 	return (p);
    360  1.2  christos }
    361  1.2  christos 
    362  1.2  christos #ifdef WIN32
    363  1.2  christos static
    364  1.2  christos #endif
    365  1.2  christos pcap_t *
    366  1.2  christos pcap_fopen_offline(FILE *fp, char *errbuf)
    367  1.2  christos {
    368  1.2  christos 	return (pcap_fopen_offline_with_tstamp_precision(fp,
    369  1.2  christos 	    PCAP_TSTAMP_PRECISION_MICRO, errbuf));
    370  1.1  christos }
    371  1.1  christos 
    372  1.1  christos /*
    373  1.1  christos  * Read packets from a capture file, and call the callback for each
    374  1.1  christos  * packet.
    375  1.1  christos  * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
    376  1.1  christos  */
    377  1.1  christos int
    378  1.1  christos pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
    379  1.1  christos {
    380  1.1  christos 	struct bpf_insn *fcode;
    381  1.1  christos 	int status = 0;
    382  1.1  christos 	int n = 0;
    383  1.1  christos 	u_char *data;
    384  1.1  christos 
    385  1.1  christos 	while (status == 0) {
    386  1.1  christos 		struct pcap_pkthdr h;
    387  1.1  christos 
    388  1.1  christos 		/*
    389  1.1  christos 		 * Has "pcap_breakloop()" been called?
    390  1.1  christos 		 * If so, return immediately - if we haven't read any
    391  1.1  christos 		 * packets, clear the flag and return -2 to indicate
    392  1.1  christos 		 * that we were told to break out of the loop, otherwise
    393  1.1  christos 		 * leave the flag set, so that the *next* call will break
    394  1.1  christos 		 * out of the loop without having read any packets, and
    395  1.1  christos 		 * return the number of packets we've processed so far.
    396  1.1  christos 		 */
    397  1.1  christos 		if (p->break_loop) {
    398  1.1  christos 			if (n == 0) {
    399  1.1  christos 				p->break_loop = 0;
    400  1.1  christos 				return (-2);
    401  1.1  christos 			} else
    402  1.1  christos 				return (n);
    403  1.1  christos 		}
    404  1.1  christos 
    405  1.2  christos 		status = p->next_packet_op(p, &h, &data);
    406  1.1  christos 		if (status) {
    407  1.1  christos 			if (status == 1)
    408  1.1  christos 				return (0);
    409  1.1  christos 			return (status);
    410  1.1  christos 		}
    411  1.1  christos 
    412  1.1  christos 		if ((fcode = p->fcode.bf_insns) == NULL ||
    413  1.2  christos 		    bpf_filter(fcode, data, h.len, h.caplen)) {
    414  1.1  christos 			(*callback)(user, &h, data);
    415  1.1  christos 			if (++n >= cnt && cnt > 0)
    416  1.1  christos 				break;
    417  1.1  christos 		}
    418  1.1  christos 	}
    419  1.1  christos 	/*XXX this breaks semantics tcpslice expects */
    420  1.1  christos 	return (n);
    421  1.1  christos }
    422