]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libpcap/tests/filtertest.c
Update lldb to release_39 branch r276489 and resolve immediate conflicts.
[FreeBSD/FreeBSD.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 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <pcap.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <arpa/inet.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #ifndef HAVE___ATTRIBUTE__
45 #define __attribute__(x)
46 #endif
47
48 static char *program_name;
49
50 /* Forwards */
51 static void usage(void) __attribute__((noreturn));
52 static void error(const char *, ...)
53     __attribute__((noreturn, format (printf, 1, 2)));
54 static void warn(const char *, ...)
55     __attribute__((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 /* VARARGS */
126 static void
127 warn(const char *fmt, ...)
128 {
129         va_list ap;
130
131         (void)fprintf(stderr, "%s: WARNING: ", program_name);
132         va_start(ap, fmt);
133         (void)vfprintf(stderr, fmt, ap);
134         va_end(ap);
135         if (*fmt) {
136                 fmt += strlen(fmt);
137                 if (fmt[-1] != '\n')
138                         (void)fputc('\n', stderr);
139         }
140 }
141
142 /*
143  * Copy arg vector into a new buffer, concatenating arguments with spaces.
144  */
145 static char *
146 copy_argv(register char **argv)
147 {
148         register char **p;
149         register u_int len = 0;
150         char *buf;
151         char *src, *dst;
152
153         p = argv;
154         if (*p == 0)
155                 return 0;
156
157         while (*p)
158                 len += strlen(*p++) + 1;
159
160         buf = (char *)malloc(len);
161         if (buf == NULL)
162                 error("copy_argv: malloc");
163
164         p = argv;
165         dst = buf;
166         while ((src = *p++) != NULL) {
167                 while ((*dst++ = *src++) != '\0')
168                         ;
169                 dst[-1] = ' ';
170         }
171         dst[-1] = '\0';
172
173         return buf;
174 }
175
176 int
177 main(int argc, char **argv)
178 {
179         char *cp;
180         int op;
181         int dflag;
182         char *infile;
183         int Oflag;
184         long snaplen;
185         int dlt;
186         bpf_u_int32 netmask = PCAP_NETMASK_UNKNOWN;
187         char *cmdbuf;
188         pcap_t *pd;
189         struct bpf_program fcode;
190
191 #ifdef WIN32
192         if(wsockinit() != 0) return 1;
193 #endif /* WIN32 */
194
195         dflag = 1;
196         infile = NULL;
197         Oflag = 1;
198         snaplen = 68;
199   
200         if ((cp = strrchr(argv[0], '/')) != NULL)
201                 program_name = cp + 1;
202         else
203                 program_name = argv[0];
204
205         opterr = 0;
206         while ((op = getopt(argc, argv, "dF:m:Os:")) != -1) {
207                 switch (op) {
208
209                 case 'd':
210                         ++dflag;
211                         break;
212
213                 case 'F':
214                         infile = optarg;
215                         break;
216
217                 case 'O':
218                         Oflag = 0;
219                         break;
220
221                 case 'm': {
222                         in_addr_t addr;
223
224                         addr = inet_addr(optarg);
225                         if (addr == INADDR_NONE)
226                                 error("invalid netmask %s", optarg);
227                         netmask = addr;
228                         break;
229                 }
230
231                 case 's': {
232                         char *end;
233
234                         snaplen = strtol(optarg, &end, 0);
235                         if (optarg == end || *end != '\0'
236                             || snaplen < 0 || snaplen > 65535)
237                                 error("invalid snaplen %s", optarg);
238                         else if (snaplen == 0)
239                                 snaplen = 65535;
240                         break;
241                 }
242
243                 default:
244                         usage();
245                         /* NOTREACHED */
246                 }
247         }
248
249         if (optind >= argc) {
250                 usage();
251                 /* NOTREACHED */
252         }
253
254         dlt = pcap_datalink_name_to_val(argv[optind]);
255         if (dlt < 0)
256                 error("invalid data link type %s", argv[optind]);
257         
258         if (infile)
259                 cmdbuf = read_infile(infile);
260         else
261                 cmdbuf = copy_argv(&argv[optind+1]);
262
263         pd = pcap_open_dead(dlt, snaplen);
264         if (pd == NULL)
265                 error("Can't open fake pcap_t");
266
267         if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
268                 error("%s", pcap_geterr(pd));
269         if (!bpf_validate(fcode.bf_insns, fcode.bf_len))
270                 warn("Filter doesn't pass validation");
271         bpf_dump(&fcode, dflag);
272         pcap_close(pd);
273         exit(0);
274 }
275
276 static void
277 usage(void)
278 {
279         (void)fprintf(stderr, "%s, with %s\n", program_name,
280             pcap_lib_version());
281         (void)fprintf(stderr,
282             "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",
283             program_name);
284         exit(1);
285 }