]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/writecaptest.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.
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";
44 #include <sys/types.h>
48 #include "pcap/funcattrs.h"
51 #include "portability.h"
54 static char *program_name
;
57 static void PCAP_NORETURN
usage(void);
58 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
59 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static char *copy_argv(char **);
66 stop_capture(DWORD ctrltype _U_
)
73 stop_capture(int signum _U_
)
79 #define COMMAND_OPTIONS "Li:s:w:y:"
82 main(int argc
, char **argv
)
85 char *cp
, *cmdbuf
= NULL
, *device
, *end
, *savefile
= NULL
;
89 int show_dlt_types
= 0;
92 bpf_u_int32 localnet
, netmask
;
93 struct bpf_program fcode
;
94 char ebuf
[PCAP_ERRBUF_SIZE
];
96 struct sigaction action
;
99 const char *dlt_name
= NULL
;
104 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
105 program_name
= cp
+ 1;
107 program_name
= argv
[0];
110 while ((op
= getopt(argc
, argv
, COMMAND_OPTIONS
)) != -1) {
122 snaplen
= (int)strtol(optarg
, &end
, 0);
123 if (optarg
== end
|| *end
!= '\0' || snaplen
< 0)
124 error("invalid snaplen %s (must be >= 0)",
143 if (device
== NULL
) {
144 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
147 error("no interfaces available for capture");
148 device
= strdup(devlist
->name
);
149 pcap_freealldevs(devlist
);
151 if (show_dlt_types
) {
152 pd
= pcap_create(device
, ebuf
);
155 status
= pcap_activate(pd
);
158 * pcap_activate() failed.
160 error("%s: %s\n(%s)", device
,
161 pcap_statustostr(status
), pcap_geterr(pd
));
163 ndlts
= pcap_list_datalinks(pd
, &dlts
);
166 * pcap_list_datalinks() failed.
168 error("%s: %s\n(%s)", device
,
169 pcap_statustostr(status
), pcap_geterr(pd
));
171 for (int i
= 0; i
< ndlts
; i
++) {
172 dlt_name
= pcap_datalink_val_to_name(dlts
[i
]);
173 if (dlt_name
== NULL
)
174 printf("DLT %d", dlts
[i
]);
176 printf("%s", dlt_name
);
179 pcap_free_datalinks(dlts
);
184 if (savefile
== NULL
)
185 error("no savefile specified");
189 pd
= pcap_create(device
, ebuf
);
193 status
= pcap_set_snaplen(pd
, snaplen
);
195 error("%s: pcap_set_snaplen failed: %s",
196 device
, pcap_statustostr(status
));
198 status
= pcap_set_timeout(pd
, 100);
200 error("%s: pcap_set_timeout failed: %s",
201 device
, pcap_statustostr(status
));
202 status
= pcap_activate(pd
);
205 * pcap_activate() failed.
207 error("%s: %s\n(%s)", device
,
208 pcap_statustostr(status
), pcap_geterr(pd
));
209 } else if (status
> 0) {
211 * pcap_activate() succeeded, but it's warning us
212 * of a problem it had.
214 warning("%s: %s\n(%s)", device
,
215 pcap_statustostr(status
), pcap_geterr(pd
));
217 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
223 if (dlt_name
!= NULL
) {
224 dlt
= pcap_datalink_name_to_val(dlt_name
);
225 if (dlt
== PCAP_ERROR
)
226 error("%s isn't a valid DLT name", dlt_name
);
227 if (pcap_set_datalink(pd
, dlt
) == PCAP_ERROR
)
228 error("%s: %s", device
, pcap_geterr(pd
));
232 * Don't set a filter unless we were given one on the
233 * command line; if capturing doesn't work, or doesn't
234 * use the snapshot length, without a filter, that's
238 cmdbuf
= copy_argv(&argv
[optind
]);
240 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
241 error("%s", pcap_geterr(pd
));
243 if (pcap_setfilter(pd
, &fcode
) < 0)
244 error("%s", pcap_geterr(pd
));
247 pdd
= pcap_dump_open(pd
, savefile
);
249 error("%s", pcap_geterr(pd
));
252 SetConsoleCtrlHandler(stop_capture
, TRUE
);
254 action
.sa_handler
= stop_capture
;
255 sigemptyset(&action
.sa_mask
);
257 if (sigaction(SIGINT
, &action
, NULL
) == -1)
258 error("Can't catch SIGINT: %s\n", strerror(errno
));
261 printf("Listening on %s, link-type ", device
);
262 dlt
= pcap_datalink(pd
);
263 dlt_name
= pcap_datalink_val_to_name(dlt
);
264 if (dlt_name
== NULL
)
265 printf("DLT %d", dlt
);
267 printf("%s", dlt_name
);
270 status
= pcap_dispatch(pd
, -1, pcap_dump
, (u_char
*)pdd
);
274 printf("%d packets seen\n", status
);
277 printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
278 ps
.ps_recv
, ps
.ps_drop
, ps
.ps_ifdrop
);
283 * We got interrupted, so perhaps we didn't
284 * manage to finish a line we were printing.
285 * Print an extra newline, just in case.
288 printf("Broken out of loop from SIGINT handler\n");
290 (void)fflush(stdout
);
295 (void)fprintf(stderr
, "%s: pcap_dispatch: %s\n",
296 program_name
, pcap_geterr(pd
));
299 if (cmdbuf
!= NULL
) {
300 pcap_freecode(&fcode
);
303 exit(status
== -1 ? 1 : 0);
309 (void)fprintf(stderr
, "Usage: %s -L [ -i interface ] [ -s snaplen ] [ -w file ] [ -y dlt ] [expression]\n",
316 error(const char *fmt
, ...)
320 (void)fprintf(stderr
, "%s: ", program_name
);
322 (void)vfprintf(stderr
, fmt
, ap
);
327 (void)fputc('\n', stderr
);
335 warning(const char *fmt
, ...)
339 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
341 (void)vfprintf(stderr
, fmt
, ap
);
346 (void)fputc('\n', stderr
);
351 * Copy arg vector into a new buffer, concatenating arguments with spaces.
354 copy_argv(register char **argv
)
357 register size_t len
= 0;
366 len
+= strlen(*p
++) + 1;
368 buf
= (char *)malloc(len
);
370 error("copy_argv: malloc");
374 while ((src
= *p
++) != NULL
) {
375 while ((*dst
++ = *src
++) != '\0')