]>
The Tcpdump Group git mirrors - libpcap/blob - tests/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.
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";
40 #define MAXIMUM_SNAPLEN 65535
42 static char *program_name
;
45 * This was introduced by Clang:
47 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
49 * in some version (which version?); it has been picked up by GCC 5.0.
51 #ifndef __has_attribute
53 * It's a macro, so you can check whether it's defined to check
54 * whether it's supported.
56 * If it's not, define it to always return 0, so that we move on to
57 * the fallback checks.
59 #define __has_attribute(x) 0
62 #if __has_attribute(noreturn) \
63 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
64 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
65 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
66 || (defined(__HP_aCC) && __HP_aCC >= 61000)
68 * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
69 * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
70 * versions of XL C support this?), or HP aCC A.06.10 and later.
72 #define PCAP_NORETURN __attribute((noreturn))
73 #elif defined( _MSC_VER )
74 #define PCAP_NORETURN __declspec(noreturn)
79 #if __has_attribute(__format__) \
80 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
81 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
82 || (defined(__HP_aCC) && __HP_aCC >= 61000)
84 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
85 * and later (do any earlier versions of XL C support this?),
86 * or HP aCC A.06.10 and later.
88 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
90 #define PCAP_PRINTFLIKE(x,y)
94 static void PCAP_NORETURN
usage(void);
95 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
96 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
99 main(int argc
, char **argv
)
102 register char *cp
, *device
;
103 int dorfmon
, dopromisc
, snaplen
, useactivate
, bufsize
;
104 char ebuf
[PCAP_ERRBUF_SIZE
];
111 snaplen
= MAXIMUM_SNAPLEN
;
114 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
115 program_name
= cp
+ 1;
117 program_name
= argv
[0];
120 while ((op
= getopt(argc
, argv
, "i:Ips:aB:")) != -1) {
129 useactivate
= 1; /* required for rfmon */
139 snaplen
= strtol(optarg
, &end
, 0);
140 if (optarg
== end
|| *end
!= '\0'
141 || snaplen
< 0 || snaplen
> MAXIMUM_SNAPLEN
)
142 error("invalid snaplen %s", optarg
);
143 else if (snaplen
== 0)
144 snaplen
= MAXIMUM_SNAPLEN
;
149 bufsize
= atoi(optarg
)*1024;
151 error("invalid packet buffer size %s", optarg
);
152 useactivate
= 1; /* required for bufsize */
166 pd
= pcap_create(device
, ebuf
);
169 status
= pcap_set_snaplen(pd
, snaplen
);
171 error("%s: pcap_set_snaplen failed: %s",
172 device
, pcap_statustostr(status
));
174 status
= pcap_set_promisc(pd
, 1);
176 error("%s: pcap_set_promisc failed: %s",
177 device
, pcap_statustostr(status
));
180 status
= pcap_set_rfmon(pd
, 1);
182 error("%s: pcap_set_rfmon failed: %s",
183 device
, pcap_statustostr(status
));
185 status
= pcap_set_timeout(pd
, 1000);
187 error("%s: pcap_set_timeout failed: %s",
188 device
, pcap_statustostr(status
));
190 status
= pcap_set_buffer_size(pd
, bufsize
);
192 error("%s: pcap_set_buffer_size failed: %s",
193 device
, pcap_statustostr(status
));
195 status
= pcap_activate(pd
);
198 * pcap_activate() failed.
200 error("%s: %s\n(%s)", device
,
201 pcap_statustostr(status
), pcap_geterr(pd
));
202 } else if (status
> 0) {
204 * pcap_activate() succeeded, but it's warning us
205 * of a problem it had.
207 warning("%s: %s\n(%s)", device
,
208 pcap_statustostr(status
), pcap_geterr(pd
));
212 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
219 exit(status
< 0 ? 1 : 0);
225 (void)fprintf(stderr
,
226 "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
233 error(const char *fmt
, ...)
237 (void)fprintf(stderr
, "%s: ", program_name
);
239 (void)vfprintf(stderr
, fmt
, ap
);
244 (void)fputc('\n', stderr
);
252 warning(const char *fmt
, ...)
256 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
258 (void)vfprintf(stderr
, fmt
, ap
);
263 (void)fputc('\n', stderr
);