]> The Tcpdump Group git mirrors - libpcap/blob - testprogs/opentest.c
TESTrun: Add more reject tests with pcap_ether_hostton().
[libpcap] / testprogs / opentest.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 #include "varattrs.h"
23
24 #ifndef lint
25 static const char copyright[] _U_ =
26 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27 The Regents of the University of California. All rights reserved.\n";
28 #endif
29
30 #include <pcap.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #ifdef _WIN32
36 #include "getopt.h"
37 #else
38 #include <unistd.h>
39 #endif
40 #include <errno.h>
41
42 #include "pcap/funcattrs.h"
43
44 #ifdef _WIN32
45 #include "portability.h"
46 #endif
47
48 #define MAXIMUM_SNAPLEN 262144
49
50 static char *program_name;
51
52 /* Forwards */
53 static void PCAP_NORETURN usage(void);
54 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
55 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
56
57 int
58 main(int argc, char **argv)
59 {
60 register int op;
61 register char *cp, *device;
62 int dorfmon, dopromisc, snaplen, useactivate, useopen, bufsize;
63 char ebuf[PCAP_ERRBUF_SIZE];
64 pcap_if_t *devlist;
65 pcap_t *pd;
66 int status = 0;
67
68 device = NULL;
69 dorfmon = 0;
70 dopromisc = 0;
71 snaplen = MAXIMUM_SNAPLEN;
72 bufsize = 0;
73 useactivate = 0;
74 useopen = 0;
75 if ((cp = strrchr(argv[0], '/')) != NULL)
76 program_name = cp + 1;
77 else
78 program_name = argv[0];
79
80 opterr = 0;
81 while ((op = getopt(argc, argv, "i:Iops:aB:")) != -1) {
82 switch (op) {
83
84 case 'i':
85 device = strdup(optarg);
86 break;
87
88 case 'I':
89 dorfmon = 1;
90 useactivate = 1; /* required for rfmon */
91 break;
92
93 case 'o':
94 useopen = 1;
95 break;
96
97 case 'p':
98 dopromisc = 1;
99 break;
100
101 case 's': {
102 char *end;
103 long long_snaplen;
104
105 long_snaplen = strtol(optarg, &end, 0);
106 if (optarg == end || *end != '\0'
107 || long_snaplen < 0
108 || long_snaplen > MAXIMUM_SNAPLEN)
109 error("invalid snaplen %s", optarg);
110 else {
111 if (snaplen == 0)
112 snaplen = MAXIMUM_SNAPLEN;
113 else
114 snaplen = (int)long_snaplen;
115 }
116 break;
117 }
118
119 case 'B':
120 bufsize = atoi(optarg)*1024;
121 if (bufsize <= 0)
122 error("invalid packet buffer size %s", optarg);
123 useactivate = 1; /* required for bufsize */
124 break;
125
126 case 'a':
127 useactivate = 1;
128 break;
129
130 default:
131 usage();
132 /* NOTREACHED */
133 }
134 }
135
136 if (device == NULL) {
137 if (pcap_findalldevs(&devlist, ebuf) == -1)
138 error("%s", ebuf);
139 if (devlist == NULL)
140 error("no interfaces available for capture");
141 device = strdup(devlist->name);
142 pcap_freealldevs(devlist);
143 }
144 if (useactivate) {
145 pd = pcap_create(device, ebuf);
146 if (pd == NULL)
147 error("%s: pcap_create failed: %s", device, ebuf);
148 status = pcap_set_snaplen(pd, snaplen);
149 if (status != 0)
150 error("%s: pcap_set_snaplen failed: %s",
151 device, pcap_statustostr(status));
152 if (dopromisc) {
153 status = pcap_set_promisc(pd, 1);
154 if (status != 0)
155 error("%s: pcap_set_promisc failed: %s",
156 device, pcap_statustostr(status));
157 }
158 if (dorfmon) {
159 status = pcap_set_rfmon(pd, 1);
160 if (status != 0)
161 error("%s: pcap_set_rfmon failed: %s",
162 device, pcap_statustostr(status));
163 }
164 status = pcap_set_timeout(pd, 1000);
165 if (status != 0)
166 error("%s: pcap_set_timeout failed: %s",
167 device, pcap_statustostr(status));
168 if (bufsize != 0) {
169 status = pcap_set_buffer_size(pd, bufsize);
170 if (status != 0)
171 error("%s: pcap_set_buffer_size failed: %s",
172 device, pcap_statustostr(status));
173 }
174 status = pcap_activate(pd);
175 if (status < 0) {
176 /*
177 * pcap_activate() failed.
178 */
179 error("%s: %s\n(%s)", device,
180 pcap_statustostr(status), pcap_geterr(pd));
181 } else if (status > 0) {
182 /*
183 * pcap_activate() succeeded, but it's warning us
184 * of a problem it had.
185 */
186 warning("%s: %s\n(%s)", device,
187 pcap_statustostr(status), pcap_geterr(pd));
188 } else
189 printf("%s opened successfully\n", device);
190 } else if (useopen) {
191 *ebuf = '\0';
192 pd = pcap_open(device, 65535, PCAP_OPENFLAG_PROMISCUOUS,
193 1000, NULL, ebuf);
194 if (pd == NULL)
195 error("%s", ebuf);
196 else if (*ebuf)
197 warning("%s", ebuf);
198 else
199 printf("%s opened successfully\n", device);
200 } else {
201 *ebuf = '\0';
202 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
203 if (pd == NULL)
204 error("%s", ebuf);
205 else if (*ebuf)
206 warning("%s", ebuf);
207 else
208 printf("%s opened successfully\n", device);
209 }
210 free(device);
211 pcap_close(pd);
212 exit(status < 0 ? 1 : 0);
213 }
214
215 static void
216 usage(void)
217 {
218 (void)fprintf(stderr,
219 "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
220 program_name);
221 exit(1);
222 }
223
224 /* VARARGS */
225 static void
226 error(const char *fmt, ...)
227 {
228 va_list ap;
229
230 (void)fprintf(stderr, "%s: ", program_name);
231 va_start(ap, fmt);
232 (void)vfprintf(stderr, fmt, ap);
233 va_end(ap);
234 if (*fmt) {
235 fmt += strlen(fmt);
236 if (fmt[-1] != '\n')
237 (void)fputc('\n', stderr);
238 }
239 exit(1);
240 /* NOTREACHED */
241 }
242
243 /* VARARGS */
244 static void
245 warning(const char *fmt, ...)
246 {
247 va_list ap;
248
249 (void)fprintf(stderr, "%s: WARNING: ", program_name);
250 va_start(ap, fmt);
251 (void)vfprintf(stderr, fmt, ap);
252 va_end(ap);
253 if (*fmt) {
254 fmt += strlen(fmt);
255 if (fmt[-1] != '\n')
256 (void)fputc('\n', stderr);
257 }
258 }