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