]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/opentest.c
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.
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
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.
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";
42 #include "pcap/funcattrs.h"
45 #include "portability.h"
48 #define MAXIMUM_SNAPLEN 262144
50 static char *program_name
;
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);
58 main(int argc
, char **argv
)
61 register char *cp
, *device
;
62 int dorfmon
, dopromisc
, snaplen
, useactivate
, bufsize
;
63 char ebuf
[PCAP_ERRBUF_SIZE
];
71 snaplen
= MAXIMUM_SNAPLEN
;
74 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
75 program_name
= cp
+ 1;
77 program_name
= argv
[0];
80 while ((op
= getopt(argc
, argv
, "i:Ips:aB:")) != -1) {
89 useactivate
= 1; /* required for rfmon */
100 long_snaplen
= strtol(optarg
, &end
, 0);
101 if (optarg
== end
|| *end
!= '\0'
103 || long_snaplen
> MAXIMUM_SNAPLEN
)
104 error("invalid snaplen %s", optarg
);
107 snaplen
= MAXIMUM_SNAPLEN
;
109 snaplen
= (int)long_snaplen
;
115 bufsize
= atoi(optarg
)*1024;
117 error("invalid packet buffer size %s", optarg
);
118 useactivate
= 1; /* required for bufsize */
131 if (device
== NULL
) {
132 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
135 error("no interfaces available for capture");
136 device
= strdup(devlist
->name
);
137 pcap_freealldevs(devlist
);
140 pd
= pcap_create(device
, ebuf
);
142 error("%s: pcap_create failed: %s", device
, ebuf
);
143 status
= pcap_set_snaplen(pd
, snaplen
);
145 error("%s: pcap_set_snaplen failed: %s",
146 device
, pcap_statustostr(status
));
148 status
= pcap_set_promisc(pd
, 1);
150 error("%s: pcap_set_promisc failed: %s",
151 device
, pcap_statustostr(status
));
154 status
= pcap_set_rfmon(pd
, 1);
156 error("%s: pcap_set_rfmon failed: %s",
157 device
, pcap_statustostr(status
));
159 status
= pcap_set_timeout(pd
, 1000);
161 error("%s: pcap_set_timeout failed: %s",
162 device
, pcap_statustostr(status
));
164 status
= pcap_set_buffer_size(pd
, bufsize
);
166 error("%s: pcap_set_buffer_size failed: %s",
167 device
, pcap_statustostr(status
));
169 status
= pcap_activate(pd
);
172 * pcap_activate() failed.
174 error("%s: %s\n(%s)", device
,
175 pcap_statustostr(status
), pcap_geterr(pd
));
176 } else if (status
> 0) {
178 * pcap_activate() succeeded, but it's warning us
179 * of a problem it had.
181 warning("%s: %s\n(%s)", device
,
182 pcap_statustostr(status
), pcap_geterr(pd
));
184 printf("%s opened successfully\n", device
);
187 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
193 printf("%s opened successfully\n", device
);
196 exit(status
< 0 ? 1 : 0);
202 (void)fprintf(stderr
,
203 "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
210 error(const char *fmt
, ...)
214 (void)fprintf(stderr
, "%s: ", program_name
);
216 (void)vfprintf(stderr
, fmt
, ap
);
221 (void)fputc('\n', stderr
);
229 warning(const char *fmt
, ...)
233 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
235 (void)vfprintf(stderr
, fmt
, ap
);
240 (void)fputc('\n', stderr
);