]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/libpcap/filtertest.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / libpcap / filtertest.c
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
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
22 #ifndef lint
23 static const char copyright[] _U_ =
24     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California.  All rights reserved.\n";
26 static const char rcsid[] _U_ =
27     "@(#) $Header: /tcpdump/master/libpcap/filtertest.c,v 1.2 2005/08/08 17:50:13 guy Exp $ (LBL)";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <pcap.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 #ifndef HAVE___ATTRIBUTE__
46 #define __attribute__(x)
47 #endif
48
49 static char *program_name;
50
51 /* Forwards */
52 static void usage(void) __attribute__((noreturn));
53 static void error(const char *, ...)
54     __attribute__((noreturn, format (printf, 1, 2)));
55
56 extern int optind;
57 extern int opterr;
58 extern char *optarg;
59
60 /*
61  * On Windows, we need to open the file in binary mode, so that
62  * we get all the bytes specified by the size we get from "fstat()".
63  * On UNIX, that's not necessary.  O_BINARY is defined on Windows;
64  * we define it as 0 if it's not defined, so it does nothing.
65  */
66 #ifndef O_BINARY
67 #define O_BINARY        0
68 #endif
69
70 static char *
71 read_infile(char *fname)
72 {
73         register int i, fd, cc;
74         register char *cp;
75         struct stat buf;
76
77         fd = open(fname, O_RDONLY|O_BINARY);
78         if (fd < 0)
79                 error("can't open %s: %s", fname, pcap_strerror(errno));
80
81         if (fstat(fd, &buf) < 0)
82                 error("can't stat %s: %s", fname, pcap_strerror(errno));
83
84         cp = malloc((u_int)buf.st_size + 1);
85         if (cp == NULL)
86                 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
87                         fname, pcap_strerror(errno));
88         cc = read(fd, cp, (u_int)buf.st_size);
89         if (cc < 0)
90                 error("read %s: %s", fname, pcap_strerror(errno));
91         if (cc != buf.st_size)
92                 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
93
94         close(fd);
95         /* replace "# comment" with spaces */
96         for (i = 0; i < cc; i++) {
97                 if (cp[i] == '#')
98                         while (i < cc && cp[i] != '\n')
99                                 cp[i++] = ' ';
100         }
101         cp[cc] = '\0';
102         return (cp);
103 }
104
105 /* VARARGS */
106 static void
107 error(const char *fmt, ...)
108 {
109         va_list ap;
110
111         (void)fprintf(stderr, "%s: ", program_name);
112         va_start(ap, fmt);
113         (void)vfprintf(stderr, fmt, ap);
114         va_end(ap);
115         if (*fmt) {
116                 fmt += strlen(fmt);
117                 if (fmt[-1] != '\n')
118                         (void)fputc('\n', stderr);
119         }
120         exit(1);
121         /* NOTREACHED */
122 }
123
124 /*
125  * Copy arg vector into a new buffer, concatenating arguments with spaces.
126  */
127 static char *
128 copy_argv(register char **argv)
129 {
130         register char **p;
131         register u_int len = 0;
132         char *buf;
133         char *src, *dst;
134
135         p = argv;
136         if (*p == 0)
137                 return 0;
138
139         while (*p)
140                 len += strlen(*p++) + 1;
141
142         buf = (char *)malloc(len);
143         if (buf == NULL)
144                 error("copy_argv: malloc");
145
146         p = argv;
147         dst = buf;
148         while ((src = *p++) != NULL) {
149                 while ((*dst++ = *src++) != '\0')
150                         ;
151                 dst[-1] = ' ';
152         }
153         dst[-1] = '\0';
154
155         return buf;
156 }
157
158 int
159 main(int argc, char **argv)
160 {
161         char *cp;
162         int op;
163         int dflag;
164         char *infile;
165         int Oflag;
166         long snaplen;
167         int dlt;
168         char *cmdbuf;
169         pcap_t *pd;
170         struct bpf_program fcode;
171
172 #ifdef WIN32
173         if(wsockinit() != 0) return 1;
174 #endif /* WIN32 */
175
176         dflag = 1;
177         infile = NULL;
178         Oflag = 1;
179         snaplen = 68;
180   
181         if ((cp = strrchr(argv[0], '/')) != NULL)
182                 program_name = cp + 1;
183         else
184                 program_name = argv[0];
185
186         opterr = 0;
187         while ((op = getopt(argc, argv, "dF:Os:")) != -1) {
188                 switch (op) {
189
190                 case 'd':
191                         ++dflag;
192                         break;
193
194                 case 'F':
195                         infile = optarg;
196                         break;
197
198                 case 'O':
199                         Oflag = 0;
200                         break;
201
202                 case 's': {
203                         char *end;
204
205                         snaplen = strtol(optarg, &end, 0);
206                         if (optarg == end || *end != '\0'
207                             || snaplen < 0 || snaplen > 65535)
208                                 error("invalid snaplen %s", optarg);
209                         else if (snaplen == 0)
210                                 snaplen = 65535;
211                         break;
212                 }
213
214                 default:
215                         usage();
216                         /* NOTREACHED */
217                 }
218         }
219
220         if (optind >= argc) {
221                 usage();
222                 /* NOTREACHED */
223         }
224
225         dlt = pcap_datalink_name_to_val(argv[optind]);
226         if (dlt < 0)
227                 error("invalid data link type %s", argv[optind]);
228         
229         if (infile)
230                 cmdbuf = read_infile(infile);
231         else
232                 cmdbuf = copy_argv(&argv[optind+1]);
233
234         pd = pcap_open_dead(dlt, snaplen);
235         if (pd == NULL)
236                 error("Can't open fake pcap_t");
237
238         if (pcap_compile(pd, &fcode, cmdbuf, Oflag, 0) < 0)
239                 error("%s", pcap_geterr(pd));
240         bpf_dump(&fcode, dflag);
241         pcap_close(pd);
242         exit(0);
243 }
244
245 static void
246 usage(void)
247 {
248         (void)fprintf(stderr, "%s, with %s\n", program_name,
249             pcap_lib_version());
250         (void)fprintf(stderr,
251             "Usage: %s [-dO] [ -F file ] [ -s snaplen ] dlt [ expression ]\n",
252             program_name);
253         exit(1);
254 }