]>
The Tcpdump Group git mirrors - libpcap/blob - tests/capturetest.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.
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 static char *program_name
;
45 #if defined( _MSC_VER )
46 # define PCAP_NORETURN __declspec(noreturn)
47 #elif defined( __GNUC__ )
48 # define PCAP_NORETURN __attribute__((noreturn))
50 # define PCAP_NORETURN
54 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
55 static void PCAP_NORETURN
usage(void);
56 static void error(const char *, ...);
57 static void warning(const char *, ...);
58 static char *copy_argv(char **);
63 main(int argc
, char **argv
)
66 register char *cp
, *cmdbuf
, *device
;
72 bpf_u_int32 localnet
, netmask
;
73 struct bpf_program fcode
;
74 char ebuf
[PCAP_ERRBUF_SIZE
];
79 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
80 program_name
= cp
+ 1;
82 program_name
= argv
[0];
85 while ((op
= getopt(argc
, argv
, "i:mnt:")) != -1) {
101 longarg
= strtol(optarg
, &p
, 10);
102 if (p
== optarg
|| *p
!= '\0') {
103 error("Timeout value \"%s\" is not a number",
108 error("Timeout value %ld is negative", longarg
);
111 if (longarg
> INT_MAX
) {
112 error("Timeout value %ld is too large (> %d)",
116 timeout
= (int)longarg
;
125 if (device
== NULL
) {
126 device
= pcap_lookupdev(ebuf
);
131 pd
= pcap_create(device
, ebuf
);
134 status
= pcap_set_snaplen(pd
, 65535);
136 error("%s: pcap_set_snaplen failed: %s",
137 device
, pcap_statustostr(status
));
139 status
= pcap_set_immediate_mode(pd
, 1);
141 error("%s: pcap_set_immediate_mode failed: %s",
142 device
, pcap_statustostr(status
));
144 status
= pcap_set_timeout(pd
, timeout
);
146 error("%s: pcap_set_timeout failed: %s",
147 device
, pcap_statustostr(status
));
148 status
= pcap_activate(pd
);
151 * pcap_activate() failed.
153 error("%s: %s\n(%s)", device
,
154 pcap_statustostr(status
), pcap_geterr(pd
));
155 } else if (status
> 0) {
157 * pcap_activate() succeeded, but it's warning us
158 * of a problem it had.
160 warning("%s: %s\n(%s)", device
,
161 pcap_statustostr(status
), pcap_geterr(pd
));
163 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
168 cmdbuf
= copy_argv(&argv
[optind
]);
170 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
171 error("%s", pcap_geterr(pd
));
173 if (pcap_setfilter(pd
, &fcode
) < 0)
174 error("%s", pcap_geterr(pd
));
175 if (pcap_setnonblock(pd
, nonblock
, ebuf
) == -1)
176 error("pcap_setnonblock failed: %s", ebuf
);
177 printf("Listening on %s\n", device
);
180 status
= pcap_dispatch(pd
, -1, countme
,
181 (u_char
*)&packet_count
);
185 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
186 status
, packet_count
);
191 * We got interrupted, so perhaps we didn't
192 * manage to finish a line we were printing.
193 * Print an extra newline, just in case.
197 (void)fflush(stdout
);
202 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
203 program_name
, pcap_geterr(pd
));
206 exit(status
== -1 ? 1 : 0);
210 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
212 int *counterp
= (int *)user
;
220 (void)fprintf(stderr
, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
227 error(const char *fmt
, ...)
231 (void)fprintf(stderr
, "%s: ", program_name
);
233 (void)vfprintf(stderr
, fmt
, ap
);
238 (void)fputc('\n', stderr
);
246 warning(const char *fmt
, ...)
250 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
252 (void)vfprintf(stderr
, fmt
, ap
);
257 (void)fputc('\n', stderr
);
262 * Copy arg vector into a new buffer, concatenating arguments with spaces.
265 copy_argv(register char **argv
)
268 register u_int len
= 0;
277 len
+= strlen(*p
++) + 1;
279 buf
= (char *)malloc(len
);
281 error("copy_argv: malloc");
285 while ((src
= *p
++) != NULL
) {
286 while ((*dst
++ = *src
++) != '\0')