]>
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
;
46 * This was introduced by Clang:
48 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
50 * in some version (which version?); it has been picked up by GCC 5.0.
52 #ifndef __has_attribute
54 * It's a macro, so you can check whether it's defined to check
55 * whether it's supported.
57 * If it's not, define it to always return 0, so that we move on to
58 * the fallback checks.
60 #define __has_attribute(x) 0
63 #if __has_attribute(noreturn) \
64 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
65 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
66 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
67 || (defined(__HP_aCC) && __HP_aCC >= 61000)
69 * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
70 * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
71 * versions of XL C support this?), or HP aCC A.06.10 and later.
73 #define PCAP_NORETURN __attribute((noreturn))
74 #elif defined( _MSC_VER )
75 #define PCAP_NORETURN __declspec(noreturn)
80 #if __has_attribute(__format__) \
81 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
82 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
83 || (defined(__HP_aCC) && __HP_aCC >= 61000)
85 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
86 * and later (do any earlier versions of XL C support this?),
87 * or HP aCC A.06.10 and later.
89 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
91 #define PCAP_PRINTFLIKE(x,y)
95 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
96 static void PCAP_NORETURN
usage(void);
97 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
98 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
99 static char *copy_argv(char **);
104 main(int argc
, char **argv
)
107 register char *cp
, *cmdbuf
, *device
;
113 bpf_u_int32 localnet
, netmask
;
114 struct bpf_program fcode
;
115 char ebuf
[PCAP_ERRBUF_SIZE
];
120 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
121 program_name
= cp
+ 1;
123 program_name
= argv
[0];
126 while ((op
= getopt(argc
, argv
, "i:mnt:")) != -1) {
142 longarg
= strtol(optarg
, &p
, 10);
143 if (p
== optarg
|| *p
!= '\0') {
144 error("Timeout value \"%s\" is not a number",
149 error("Timeout value %ld is negative", longarg
);
152 if (longarg
> INT_MAX
) {
153 error("Timeout value %ld is too large (> %d)",
157 timeout
= (int)longarg
;
166 if (device
== NULL
) {
167 device
= pcap_lookupdev(ebuf
);
172 pd
= pcap_create(device
, ebuf
);
175 status
= pcap_set_snaplen(pd
, 65535);
177 error("%s: pcap_set_snaplen failed: %s",
178 device
, pcap_statustostr(status
));
180 status
= pcap_set_immediate_mode(pd
, 1);
182 error("%s: pcap_set_immediate_mode failed: %s",
183 device
, pcap_statustostr(status
));
185 status
= pcap_set_timeout(pd
, timeout
);
187 error("%s: pcap_set_timeout failed: %s",
188 device
, pcap_statustostr(status
));
189 status
= pcap_activate(pd
);
192 * pcap_activate() failed.
194 error("%s: %s\n(%s)", device
,
195 pcap_statustostr(status
), pcap_geterr(pd
));
196 } else if (status
> 0) {
198 * pcap_activate() succeeded, but it's warning us
199 * of a problem it had.
201 warning("%s: %s\n(%s)", device
,
202 pcap_statustostr(status
), pcap_geterr(pd
));
204 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
209 cmdbuf
= copy_argv(&argv
[optind
]);
211 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
212 error("%s", pcap_geterr(pd
));
214 if (pcap_setfilter(pd
, &fcode
) < 0)
215 error("%s", pcap_geterr(pd
));
216 if (pcap_setnonblock(pd
, nonblock
, ebuf
) == -1)
217 error("pcap_setnonblock failed: %s", ebuf
);
218 printf("Listening on %s\n", device
);
221 status
= pcap_dispatch(pd
, -1, countme
,
222 (u_char
*)&packet_count
);
226 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
227 status
, packet_count
);
232 * We got interrupted, so perhaps we didn't
233 * manage to finish a line we were printing.
234 * Print an extra newline, just in case.
238 (void)fflush(stdout
);
243 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
244 program_name
, pcap_geterr(pd
));
247 exit(status
== -1 ? 1 : 0);
251 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
253 int *counterp
= (int *)user
;
261 (void)fprintf(stderr
, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
268 error(const char *fmt
, ...)
272 (void)fprintf(stderr
, "%s: ", program_name
);
274 (void)vfprintf(stderr
, fmt
, ap
);
279 (void)fputc('\n', stderr
);
287 warning(const char *fmt
, ...)
291 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
293 (void)vfprintf(stderr
, fmt
, ap
);
298 (void)fputc('\n', stderr
);
303 * Copy arg vector into a new buffer, concatenating arguments with spaces.
306 copy_argv(register char **argv
)
309 register u_int len
= 0;
318 len
+= strlen(*p
++) + 1;
320 buf
= (char *)malloc(len
);
322 error("copy_argv: malloc");
326 while ((src
= *p
++) != NULL
) {
327 while ((*dst
++ = *src
++) != '\0')