5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <stdlib.h> /* for exit() */
46 #include "ringbuffer.h"
47 #include "clopts_common.h"
48 #include "cmdarg_err.h"
49 #include "version_info.h"
52 #include "capture-pcap-util.h"
55 #include "capture-wpcap.h"
58 #include "sync_pipe.h"
61 #include "capture_loop.h"
62 #include "capture_sync.h"
64 #include "simple_dialog.h"
67 #include "file_util.h"
70 /*#define DEBUG_DUMPCAP*/
72 gboolean capture_child = FALSE; /* FALSE: standalone call, TRUE: this is an Wireshark capture child */
75 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
76 const char *message, gpointer user_data _U_);
78 /* capture related options */
79 capture_options global_capture_opts;
80 capture_options *capture_opts = &global_capture_opts;
83 void exit_main(int err) __attribute__ ((noreturn));
85 void exit_main(int err);
90 print_usage(gboolean print_ver) {
98 "Dumpcap " VERSION "%s\n"
99 "Capture network packets and dump them into a libpcap file.\n"
100 "See http://www.wireshark.org for more information.\n",
101 wireshark_svnversion);
105 fprintf(output, "\nUsage: dumpcap [options] ...\n");
106 fprintf(output, "\n");
107 fprintf(output, "Capture interface:\n");
108 fprintf(output, " -i <interface> name or idx of interface (def: first none loopback)\n");
109 fprintf(output, " -f <capture filter> packet filter in libpcap filter syntax\n");
110 fprintf(output, " -s <snaplen> packet snapshot length (def: 65535)\n");
111 fprintf(output, " -p don't capture in promiscuous mode\n");
113 fprintf(output, " -B <buffer size> size of kernel buffer (def: 1MB)\n");
115 fprintf(output, " -y <link type> link layer type (def: first appropriate)\n");
116 fprintf(output, " -D print list of interfaces and exit\n");
117 fprintf(output, " -L print list of link-layer types of iface and exit\n");
118 fprintf(output, "\n");
119 fprintf(output, "Stop conditions:\n");
120 fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
121 fprintf(output, " -a <autostop cond.> ... duration:NUM - stop after NUM seconds\n");
122 fprintf(output, " filesize:NUM - stop this file after NUM KB\n");
123 fprintf(output, " files:NUM - stop after NUM files\n");
124 /*fprintf(output, "\n");*/
125 fprintf(output, "Output (files):\n");
126 fprintf(output, " -w <filename> name of file to save (def: tempfile)\n");
127 fprintf(output, " -b <ringbuffer opt.> ... duration:NUM - switch to next file after NUM secs\n");
128 fprintf(output, " filesize:NUM - switch to next file after NUM KB\n");
129 fprintf(output, " files:NUM - ringbuffer: replace after NUM files\n");
130 /*fprintf(output, "\n");*/
131 fprintf(output, "Miscellaneous:\n");
132 fprintf(output, " -v print version information and exit\n");
133 fprintf(output, " -h display this help and exit\n");
134 fprintf(output, "\n");
135 fprintf(output, "Example: dumpcap -i eth0 -a duration:60 -w output.pcap\n");
136 fprintf(output, "\"Capture network packets from interface eth0 until 60s passed into output.pcap\"\n");
137 fprintf(output, "\n");
138 fprintf(output, "Use Ctrl-C to stop capturing at any time.\n");
142 show_version(GString *comp_info_str, GString *runtime_info_str)
146 "Dumpcap " VERSION "%s\n"
151 "See http://www.wireshark.org for more information.\n",
152 wireshark_svnversion, get_copyright_info() ,comp_info_str->str, runtime_info_str->str);
156 * Report an error in command-line arguments.
159 cmdarg_err(const char *fmt, ...)
164 /* XXX - convert to g_log */
167 fprintf(stderr, "dumpcap: ");
168 vfprintf(stderr, fmt, ap);
169 fprintf(stderr, "\n");
175 * Report additional information for an error in command-line arguments.
178 cmdarg_err_cont(const char *fmt, ...)
183 /* XXX - convert to g_log */
186 vfprintf(stderr, fmt, ap);
187 fprintf(stderr, "\n");
194 BOOL WINAPI ConsoleCtrlHandlerRoutine(DWORD dwCtrlType)
196 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
197 "Console: Control signal");
198 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
199 "Console: Control signal, CtrlType: %u", dwCtrlType);
201 /* Keep capture running if we're a service and a user logs off */
202 if (capture_child || (dwCtrlType != CTRL_LOGOFF_EVENT)) {
211 void exit_main(int status)
214 /* Shutdown windows sockets */
217 /* can be helpful for debugging */
219 printf("Press any key\n");
229 /* And now our feature presentation... [ fade to music ] */
231 main(int argc, char *argv[])
235 gboolean arg_error = FALSE;
241 gboolean start_capture = TRUE;
242 gboolean stats_known;
243 struct pcap_stat stats;
244 GLogLevelFlags log_flags;
245 gboolean list_link_layer_types = FALSE;
248 #define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:Z"
251 #define OPTSTRING_WIN32 "B:"
253 #define OPTSTRING_WIN32 ""
256 char optstring[sizeof(OPTSTRING_INIT) + sizeof(OPTSTRING_WIN32) - 1] =
257 OPTSTRING_INIT OPTSTRING_WIN32;
260 /* Load wpcap if possible. Do this before collecting the run-time version information */
263 /* ... and also load the packet.dll from wpcap */
264 /* XXX - currently not required, may change later. */
265 /*wpcap_packet_load();*/
267 /* Start windows sockets */
268 WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
270 /* Set handler for Ctrl+C key */
271 SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
275 /* the default_log_handler will use stdout, which makes trouble in */
276 /* capture child mode, as it uses stdout for it's sync_pipe */
277 /* so do the filtering in the console_log_handler and not here */
280 G_LOG_LEVEL_CRITICAL|
285 G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION;
287 g_log_set_handler(NULL,
289 console_log_handler, NULL /* user_data */);
290 g_log_set_handler(LOG_DOMAIN_MAIN,
292 console_log_handler, NULL /* user_data */);
293 g_log_set_handler(LOG_DOMAIN_CAPTURE,
295 console_log_handler, NULL /* user_data */);
296 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
298 console_log_handler, NULL /* user_data */);
300 /* Set the initial values in the capture_opts. This might be overwritten
301 by the command line parameters. */
302 capture_opts_init(capture_opts, NULL);
304 /* Default to capturing the entire packet. */
305 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
307 /* We always save to a file - if no file was specified, we save to a
309 capture_opts->saving_to_file = TRUE;
310 capture_opts->has_ring_num_files = TRUE;
312 /* Now get our args */
313 while ((opt = getopt(argc, argv, optstring)) != -1) {
315 case 'h': /* Print help and exit */
319 case 'v': /* Show version and exit */
321 GString *comp_info_str;
322 GString *runtime_info_str;
323 /* Assemble the compile-time version information string */
324 comp_info_str = g_string_new("Compiled with ");
325 get_compiled_version_info(comp_info_str, NULL);
327 /* Assemble the run-time version information string */
328 runtime_info_str = g_string_new("Running ");
329 get_runtime_version_info(runtime_info_str, NULL);
330 show_version(comp_info_str, runtime_info_str);
331 g_string_free(comp_info_str, TRUE);
332 g_string_free(runtime_info_str, TRUE);
336 /*** capture option specific ***/
337 case 'a': /* autostop criteria */
338 case 'b': /* Ringbuffer option */
339 case 'c': /* Capture x packets */
340 case 'f': /* capture filter */
341 case 'i': /* Use interface x */
342 case 'p': /* Don't capture in promiscuous mode */
343 case 's': /* Set the snapshot (capture) length */
344 case 'w': /* Write to capture file x */
345 case 'y': /* Set the pcap data link type */
347 case 'B': /* Buffer size */
349 status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
354 /*** hidden option: Wireshark child mode (using binary output messages) ***/
356 capture_child = TRUE;
358 /* set output pipe to binary mode, to avoid ugly text conversions */
359 _setmode(1, O_BINARY);
363 /*** all non capture option specific ***/
364 case 'D': /* Print a list of capture devices and exit */
365 status = capture_opts_list_interfaces();
368 case 'L': /* Print list of link-layer types and exit */
369 list_link_layer_types = TRUE;
372 case '?': /* Bad flag - print usage message */
373 cmdarg_err("Invalid Option: %s", argv[optind-1]);
381 /* user specified file name as regular command-line argument */
382 /* XXX - use it as the capture file name (or something else)? */
389 * Extra command line arguments were specified; complain.
390 * XXX - interpret as capture filter, as tcpdump and tshark do?
392 cmdarg_err("Invalid argument: %s", argv[0]);
401 if (list_link_layer_types) {
402 /* We're supposed to list the link-layer types for an interface;
403 did the user also specify a capture file to be read? */
404 /* No - did they specify a ring buffer option? */
405 if (capture_opts->multi_files_on) {
406 cmdarg_err("Ring buffer requested, but a capture isn't being done.");
410 /* No - was the ring buffer option specified and, if so, does it make
412 if (capture_opts->multi_files_on) {
413 /* Ring buffer works only under certain conditions:
414 a) ring buffer does not work with temporary files;
415 b) it makes no sense to enable the ring buffer if the maximum
416 file size is set to "infinite". */
417 if (capture_opts->save_file == NULL) {
418 cmdarg_err("Ring buffer requested, but capture isn't being saved to a permanent file.");
419 capture_opts->multi_files_on = FALSE;
421 if (!capture_opts->has_autostop_filesize && !capture_opts->has_file_duration) {
422 cmdarg_err("Ring buffer requested, but no maximum capture file size or duration were specified.");
423 /* XXX - this must be redesigned as the conditions changed */
424 /* capture_opts->multi_files_on = FALSE;*/
429 if (capture_opts_trim_iface(capture_opts, NULL) == FALSE) {
430 cmdarg_err("No capture interfaces available (maybe lack of privileges?).");
434 /* Let the user know what interface was chosen. */
435 /* get_interface_descriptive_name() is not available! */
436 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", capture_opts->iface);
438 if (list_link_layer_types) {
439 status = capture_opts_list_link_layer_types(capture_opts);
443 capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);
444 capture_opts_trim_ring_num_files(capture_opts);
446 /* Now start the capture. */
448 if(capture_loop_start(capture_opts, &stats_known, &stats) == TRUE) {
459 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
460 const char *message, gpointer user_data _U_)
467 /* ignore log message, if log_level isn't interesting */
468 if( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
469 #ifndef DEBUG_DUMPCAP
474 /* create a "timestamp" */
476 today = localtime(&curr);
478 switch(log_level & G_LOG_LEVEL_MASK) {
479 case G_LOG_LEVEL_ERROR:
482 case G_LOG_LEVEL_CRITICAL:
485 case G_LOG_LEVEL_WARNING:
488 case G_LOG_LEVEL_MESSAGE:
491 case G_LOG_LEVEL_INFO:
494 case G_LOG_LEVEL_DEBUG:
498 fprintf(stderr, "unknown log_level %u\n", log_level);
500 g_assert_not_reached();
503 /* don't use printf (stdout), in child mode we're using stdout for the sync_pipe */
504 if(log_level & G_LOG_LEVEL_MESSAGE) {
505 /* normal user messages without additional infos */
506 fprintf(stderr, "%s\n", message);
509 /* info/debug messages with additional infos */
510 fprintf(stderr, "%02u:%02u:%02u %8s %s %s\n",
511 today->tm_hour, today->tm_min, today->tm_sec,
512 log_domain != NULL ? log_domain : "",
519 /****************************************************************************************************************/
520 /* indication report routines */
524 report_packet_count(int packet_count)
526 char tmp[SP_DECISIZE+1+1];
527 static int count = 0;
530 g_snprintf(tmp, sizeof(tmp), "%d", packet_count);
531 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
532 pipe_write_block(1, SP_PACKET_COUNT, tmp);
534 count += packet_count;
535 fprintf(stderr, "\rPackets: %u ", count);
536 /* stderr could be line buffered */
542 report_new_capture_file(const char *filename)
545 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
546 pipe_write_block(1, SP_FILE, filename);
548 fprintf(stderr, "File: %s\n", filename);
549 /* stderr could be line buffered */
555 report_cfilter_error(const char *cfilter, const char *errmsg)
558 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
559 pipe_write_block(1, SP_BAD_FILTER, errmsg);
562 "Invalid capture filter: \"%s\"!\n"
564 "That string isn't a valid capture filter (%s).\n"
565 "See the User's Guide for a description of the capture filter syntax.\n",
571 report_capture_error(const char *error_msg, const char *secondary_error_msg)
574 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
575 "Primary Error: %s", error_msg);
576 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
577 "Secondary Error: %s", secondary_error_msg);
578 sync_pipe_errmsg_to_parent(error_msg, secondary_error_msg);
580 fprintf(stderr, "%s\n%s\n", error_msg, secondary_error_msg);
585 report_packet_drops(int drops)
587 char tmp[SP_DECISIZE+1+1];
589 g_snprintf(tmp, sizeof(tmp), "%d", drops);
592 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets dropped: %s", tmp);
593 pipe_write_block(1, SP_DROPS, tmp);
595 fprintf(stderr, "Packets dropped: %s\n", tmp);
596 /* stderr could be line buffered */
602 /****************************************************************************************************************/
603 /* signal_pipe handling */
608 signal_pipe_check_running(void)
610 /* any news from our parent (stdin)? -> just stop the capture */
616 /* if we are running standalone, no check required */
621 handle = (HANDLE) GetStdHandle(STD_INPUT_HANDLE);
622 result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
624 if(!result || avail > 0) {
625 /* peek failed or some bytes really available */
626 /* (if not piping from stdin this would fail) */
627 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
628 "Signal pipe: Stop capture");
629 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
630 "Signal pipe: handle: %x result: %u avail: %u", handle, result, avail);
633 /* pipe ok and no bytes available */
640 /****************************************************************************************************************/
644 const char *netsnmp_get_version(void) { return ""; }