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