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