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