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