]>
The Tcpdump Group git mirrors - libpcap/blob - tests/capturetest.c
1cebae01d5ef0d8f365f77a353f4cffa7820a24e
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.
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";
39 #include <sys/types.h>
43 #include "pcap/funcattrs.h"
46 #include "portability.h"
49 static char *program_name
;
52 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
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 static char *copy_argv(char **);
61 main(int argc
, char **argv
)
64 register char *cp
, *cmdbuf
, *device
;
71 bpf_u_int32 localnet
, netmask
;
72 struct bpf_program fcode
;
73 char ebuf
[PCAP_ERRBUF_SIZE
];
78 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
79 program_name
= cp
+ 1;
81 program_name
= argv
[0];
84 while ((op
= getopt(argc
, argv
, "i:mnt:")) != -1) {
100 longarg
= strtol(optarg
, &p
, 10);
101 if (p
== optarg
|| *p
!= '\0') {
102 error("Timeout value \"%s\" is not a number",
107 error("Timeout value %ld is negative", longarg
);
110 if (longarg
> INT_MAX
) {
111 error("Timeout value %ld is too large (> %d)",
115 timeout
= (int)longarg
;
124 if (device
== NULL
) {
125 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
128 error("no interfaces available for capture");
129 device
= strdup(devlist
->name
);
130 pcap_freealldevs(devlist
);
133 pd
= pcap_create(device
, ebuf
);
136 status
= pcap_set_snaplen(pd
, 65535);
138 error("%s: pcap_set_snaplen failed: %s",
139 device
, pcap_statustostr(status
));
141 status
= pcap_set_immediate_mode(pd
, 1);
143 error("%s: pcap_set_immediate_mode failed: %s",
144 device
, pcap_statustostr(status
));
146 status
= pcap_set_timeout(pd
, timeout
);
148 error("%s: pcap_set_timeout failed: %s",
149 device
, pcap_statustostr(status
));
150 status
= pcap_activate(pd
);
153 * pcap_activate() failed.
155 error("%s: %s\n(%s)", device
,
156 pcap_statustostr(status
), pcap_geterr(pd
));
157 } else if (status
> 0) {
159 * pcap_activate() succeeded, but it's warning us
160 * of a problem it had.
162 warning("%s: %s\n(%s)", device
,
163 pcap_statustostr(status
), pcap_geterr(pd
));
165 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
170 cmdbuf
= copy_argv(&argv
[optind
]);
172 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
173 error("%s", pcap_geterr(pd
));
175 if (pcap_setfilter(pd
, &fcode
) < 0)
176 error("%s", pcap_geterr(pd
));
177 if (pcap_setnonblock(pd
, nonblock
, ebuf
) == -1)
178 error("pcap_setnonblock failed: %s", ebuf
);
179 printf("Listening on %s\n", device
);
182 status
= pcap_dispatch(pd
, -1, countme
,
183 (u_char
*)&packet_count
);
187 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
188 status
, packet_count
);
193 * We got interrupted, so perhaps we didn't
194 * manage to finish a line we were printing.
195 * Print an extra newline, just in case.
199 (void)fflush(stdout
);
204 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
205 program_name
, pcap_geterr(pd
));
208 pcap_freecode(&fcode
);
210 exit(status
== -1 ? 1 : 0);
214 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
216 int *counterp
= (int *)user
;
224 (void)fprintf(stderr
, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
231 error(const char *fmt
, ...)
235 (void)fprintf(stderr
, "%s: ", program_name
);
237 (void)vfprintf(stderr
, fmt
, ap
);
242 (void)fputc('\n', stderr
);
250 warning(const char *fmt
, ...)
254 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
256 (void)vfprintf(stderr
, fmt
, ap
);
261 (void)fputc('\n', stderr
);
266 * Copy arg vector into a new buffer, concatenating arguments with spaces.
269 copy_argv(register char **argv
)
272 register u_int len
= 0;
281 len
+= strlen(*p
++) + 1;
283 buf
= (char *)malloc(len
);
285 error("copy_argv: malloc");
289 while ((src
= *p
++) != NULL
) {
290 while ((*dst
++ = *src
++) != '\0')