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
, useopen
, bufsize
;
63 char ebuf
[PCAP_ERRBUF_SIZE
];
71 snaplen
= MAXIMUM_SNAPLEN
;
75 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
76 program_name
= cp
+ 1;
78 program_name
= argv
[0];
81 while ((op
= getopt(argc
, argv
, "i:Iops:aB:")) != -1) {
85 device
= strdup(optarg
);
90 useactivate
= 1; /* required for rfmon */
105 long_snaplen
= strtol(optarg
, &end
, 0);
106 if (optarg
== end
|| *end
!= '\0'
108 || long_snaplen
> MAXIMUM_SNAPLEN
)
109 error("invalid snaplen %s", optarg
);
112 snaplen
= MAXIMUM_SNAPLEN
;
114 snaplen
= (int)long_snaplen
;
120 bufsize
= atoi(optarg
)*1024;
122 error("invalid packet buffer size %s", optarg
);
123 useactivate
= 1; /* required for bufsize */
136 if (device
== NULL
) {
137 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
140 error("no interfaces available for capture");
141 device
= strdup(devlist
->name
);
142 pcap_freealldevs(devlist
);
145 pd
= pcap_create(device
, ebuf
);
147 error("%s: pcap_create failed: %s", device
, ebuf
);
148 status
= pcap_set_snaplen(pd
, snaplen
);
150 error("%s: pcap_set_snaplen failed: %s",
151 device
, pcap_statustostr(status
));
153 status
= pcap_set_promisc(pd
, 1);
155 error("%s: pcap_set_promisc failed: %s",
156 device
, pcap_statustostr(status
));
159 status
= pcap_set_rfmon(pd
, 1);
161 error("%s: pcap_set_rfmon failed: %s",
162 device
, pcap_statustostr(status
));
164 status
= pcap_set_timeout(pd
, 1000);
166 error("%s: pcap_set_timeout failed: %s",
167 device
, pcap_statustostr(status
));
169 status
= pcap_set_buffer_size(pd
, bufsize
);
171 error("%s: pcap_set_buffer_size failed: %s",
172 device
, pcap_statustostr(status
));
174 status
= pcap_activate(pd
);
177 * pcap_activate() failed.
179 error("%s: %s\n(%s)", device
,
180 pcap_statustostr(status
), pcap_geterr(pd
));
181 } else if (status
> 0) {
183 * pcap_activate() succeeded, but it's warning us
184 * of a problem it had.
186 warning("%s: %s\n(%s)", device
,
187 pcap_statustostr(status
), pcap_geterr(pd
));
189 printf("%s opened successfully\n", device
);
190 } else if (useopen
) {
192 pd
= pcap_open(device
, 65535, PCAP_OPENFLAG_PROMISCUOUS
,
199 printf("%s opened successfully\n", device
);
202 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
208 printf("%s opened successfully\n", device
);
212 exit(status
< 0 ? 1 : 0);
218 (void)fprintf(stderr
,
219 "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
226 error(const char *fmt
, ...)
230 (void)fprintf(stderr
, "%s: ", program_name
);
232 (void)vfprintf(stderr
, fmt
, ap
);
237 (void)fputc('\n', stderr
);
245 warning(const char *fmt
, ...)
249 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
251 (void)vfprintf(stderr
, fmt
, ap
);
256 (void)fputc('\n', stderr
);