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.
45 #include "ringbuffer.h"
46 #include "clopts_common.h"
47 #include "cmdarg_err.h"
48 #include "version_info.h"
51 #include "capture-pcap-util.h"
54 #include "capture-wpcap.h"
57 #include "sync_pipe.h"
60 #include "capture_loop.h"
61 #include "capture_sync.h"
63 #include "simple_dialog.h"
66 #include "file_util.h"
69 /*#define DEBUG_DUMPCAP*/
71 gboolean capture_child = FALSE; /* FALSE: standalone call, TRUE: this is an Wireshark capture child */
74 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
75 const char *message, gpointer user_data _U_);
77 /* capture related options */
78 capture_options global_capture_opts;
79 capture_options *capture_opts = &global_capture_opts;
82 void exit_main(int err) __attribute__ ((noreturn));
84 void exit_main(int err);
89 print_usage(gboolean print_ver) {
97 "Dumpcap " VERSION "%s\n"
98 "Capture network packets and dump them into a libpcap file.\n"
99 "See http://www.wireshark.org for more information.\n",
104 fprintf(output, "\nUsage: dumpcap [options] ...\n");
105 fprintf(output, "\n");
106 fprintf(output, "Capture interface:\n");
107 fprintf(output, " -i <interface> name or idx of interface (def: first none loopback)\n");
108 fprintf(output, " -f <capture filter> packet filter in libpcap filter syntax\n");
109 fprintf(output, " -s <snaplen> packet snapshot length (def: 65535)\n");
110 fprintf(output, " -p don't capture in promiscuous mode\n");
112 fprintf(output, " -B <buffer size> size of kernel buffer (def: 1MB)\n");
114 fprintf(output, " -y <link type> link layer type (def: first appropriate)\n");
115 fprintf(output, " -D print list of interfaces and exit\n");
116 fprintf(output, " -L print list of link-layer types of iface and exit\n");
117 fprintf(output, "\n");
118 fprintf(output, "Stop conditions:\n");
119 fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
120 fprintf(output, " -a <autostop cond.> ... duration:NUM - stop after NUM seconds\n");
121 fprintf(output, " filesize:NUM - stop this file after NUM KB\n");
122 fprintf(output, " files:NUM - stop after NUM files\n");
123 /*fprintf(output, "\n");*/
124 fprintf(output, "Output (files):\n");
125 fprintf(output, " -w <filename> name of file to save (def: tempfile)\n");
126 fprintf(output, " -b <ringbuffer opt.> ... duration:NUM - switch to next file after NUM secs\n");
127 fprintf(output, " filesize:NUM - switch to next file after NUM KB\n");
128 fprintf(output, " files:NUM - ringbuffer: replace after NUM files\n");
129 /*fprintf(output, "\n");*/
130 fprintf(output, "Miscellaneous:\n");
131 fprintf(output, " -v print version information and exit\n");
132 fprintf(output, " -h display this help and exit\n");
133 fprintf(output, "\n");
134 fprintf(output, "Example: dumpcap -i eth0 -a duration:60 -w output.pcap\n");
135 fprintf(output, "\"Capture network packets from interface eth0 until 60s passed into output.pcap\"\n");
136 fprintf(output, "\n");
137 fprintf(output, "Use Ctrl-C to stop capturing at any time.\n");
141 show_version(GString *comp_info_str, GString *runtime_info_str)
145 "Dumpcap " VERSION "%s\n"
150 "See http://www.wireshark.org for more information.\n",
151 svnversion, get_copyright_info() ,comp_info_str->str, runtime_info_str->str);
155 * Report an error in command-line arguments.
158 cmdarg_err(const char *fmt, ...)
163 /* XXX - convert to g_log */
166 fprintf(stderr, "dumpcap: ");
167 vfprintf(stderr, fmt, ap);
168 fprintf(stderr, "\n");
174 * Report additional information for an error in command-line arguments.
177 cmdarg_err_cont(const char *fmt, ...)
182 /* XXX - convert to g_log */
185 vfprintf(stderr, fmt, ap);
186 fprintf(stderr, "\n");
193 BOOL WINAPI ConsoleCtrlHandlerRoutine(DWORD dwCtrlType)
195 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
196 "Console: Control signal");
197 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
198 "Console: Control signal, CtrlType: %u", dwCtrlType);
200 /* Keep capture running if we're a service and a user logs off */
201 if (capture_child || (dwCtrlType != CTRL_LOGOFF_EVENT)) {
210 void exit_main(int status)
213 /* Shutdown windows sockets */
216 /* can be helpful for debugging */
218 printf("Press any key\n");
228 /* And now our feature presentation... [ fade to music ] */
230 main(int argc, char *argv[])
234 gboolean arg_error = FALSE;
235 GString *comp_info_str;
236 GString *runtime_info_str;
242 gboolean start_capture = TRUE;
243 gboolean stats_known;
244 struct pcap_stat stats;
245 GLogLevelFlags log_flags;
246 gboolean list_link_layer_types = FALSE;
249 #define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:Z"
252 #define OPTSTRING_WIN32 "B:"
254 #define OPTSTRING_WIN32 ""
257 char optstring[sizeof(OPTSTRING_INIT) + sizeof(OPTSTRING_WIN32) - 1] =
258 OPTSTRING_INIT OPTSTRING_WIN32;
261 /* Load wpcap if possible. Do this before collecting the run-time version information */
264 /* ... and also load the packet.dll from wpcap */
265 /* XXX - currently not required, may change later. */
266 /*wpcap_packet_load();*/
268 /* Start windows sockets */
269 WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
271 /* Set handler for Ctrl+C key */
272 SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
275 /* Assemble the compile-time version information string */
276 comp_info_str = g_string_new("Compiled ");
277 get_compiled_version_info(comp_info_str, NULL);
279 /* Assemble the run-time version information string */
280 runtime_info_str = g_string_new("Running ");
281 get_runtime_version_info(runtime_info_str, NULL);
283 /* the default_log_handler will use stdout, which makes trouble in */
284 /* capture child mode, as it uses stdout for it's sync_pipe */
285 /* so do the filtering in the console_log_handler and not here */
288 G_LOG_LEVEL_CRITICAL|
293 G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION;
295 g_log_set_handler(NULL,
297 console_log_handler, NULL /* user_data */);
298 g_log_set_handler(LOG_DOMAIN_MAIN,
300 console_log_handler, NULL /* user_data */);
301 g_log_set_handler(LOG_DOMAIN_CAPTURE,
303 console_log_handler, NULL /* user_data */);
304 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
306 console_log_handler, NULL /* user_data */);
308 /* Set the initial values in the capture_opts. This might be overwritten
309 by the command line parameters. */
310 capture_opts_init(capture_opts, NULL);
312 /* Default to capturing the entire packet. */
313 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
315 /* We always save to a file - if no file was specified, we save to a
317 capture_opts->saving_to_file = TRUE;
318 capture_opts->has_ring_num_files = TRUE;
320 /* Now get our args */
321 while ((opt = getopt(argc, argv, optstring)) != -1) {
323 case 'h': /* Print help and exit */
327 case 'v': /* Show version and exit */
328 show_version(comp_info_str, runtime_info_str);
331 /*** capture option specific ***/
332 case 'a': /* autostop criteria */
333 case 'b': /* Ringbuffer option */
334 case 'c': /* Capture x packets */
335 case 'f': /* capture filter */
336 case 'i': /* Use interface x */
337 case 'p': /* Don't capture in promiscuous mode */
338 case 's': /* Set the snapshot (capture) length */
339 case 'w': /* Write to capture file x */
340 case 'y': /* Set the pcap data link type */
342 case 'B': /* Buffer size */
344 status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
349 /*** hidden option: Wireshark child mode (using binary output messages) ***/
351 capture_child = TRUE;
353 /* set output pipe to binary mode, to avoid ugly text conversions */
354 _setmode(1, O_BINARY);
358 /*** all non capture option specific ***/
359 case 'D': /* Print a list of capture devices and exit */
360 status = capture_opts_list_interfaces();
363 case 'L': /* Print list of link-layer types and exit */
364 list_link_layer_types = TRUE;
367 case '?': /* Bad flag - print usage message */
368 cmdarg_err("Invalid Option: %s", argv[optind-1]);
376 /* user specified file name as regular command-line argument */
377 /* XXX - use it as the capture file name (or something else)? */
384 * Extra command line arguments were specified; complain.
385 * XXX - interpret as capture filter, as tcpdump and tshark do?
387 cmdarg_err("Invalid argument: %s", argv[0]);
396 if (list_link_layer_types) {
397 /* We're supposed to list the link-layer types for an interface;
398 did the user also specify a capture file to be read? */
399 /* No - did they specify a ring buffer option? */
400 if (capture_opts->multi_files_on) {
401 cmdarg_err("Ring buffer requested, but a capture isn't being done.");
405 /* No - was the ring buffer option specified and, if so, does it make
407 if (capture_opts->multi_files_on) {
408 /* Ring buffer works only under certain conditions:
409 a) ring buffer does not work with temporary files;
410 b) it makes no sense to enable the ring buffer if the maximum
411 file size is set to "infinite". */
412 if (capture_opts->save_file == NULL) {
413 cmdarg_err("Ring buffer requested, but capture isn't being saved to a permanent file.");
414 capture_opts->multi_files_on = FALSE;
416 if (!capture_opts->has_autostop_filesize && !capture_opts->has_file_duration) {
417 cmdarg_err("Ring buffer requested, but no maximum capture file size or duration were specified.");
418 /* XXX - this must be redesigned as the conditions changed */
419 /* capture_opts->multi_files_on = FALSE;*/
424 if (capture_opts_trim_iface(capture_opts, NULL) == FALSE) {
425 cmdarg_err("No capture interfaces available (maybe lack of privileges?).");
429 /* Let the user know what interface was chosen. */
430 /* get_interface_descriptive_name() is not available! */
431 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", capture_opts->iface);
433 if (list_link_layer_types) {
434 status = capture_opts_list_link_layer_types(capture_opts);
438 capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);
439 capture_opts_trim_ring_num_files(capture_opts);
441 /* Now start the capture. */
443 if(capture_loop_start(capture_opts, &stats_known, &stats) == TRUE) {
454 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
455 const char *message, gpointer user_data _U_)
462 /* ignore log message, if log_level isn't interesting */
463 if( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
464 #ifndef DEBUG_DUMPCAP
469 /* create a "timestamp" */
471 today = localtime(&curr);
473 switch(log_level & G_LOG_LEVEL_MASK) {
474 case G_LOG_LEVEL_ERROR:
477 case G_LOG_LEVEL_CRITICAL:
480 case G_LOG_LEVEL_WARNING:
483 case G_LOG_LEVEL_MESSAGE:
486 case G_LOG_LEVEL_INFO:
489 case G_LOG_LEVEL_DEBUG:
493 fprintf(stderr, "unknown log_level %u\n", log_level);
495 g_assert_not_reached();
498 /* don't use printf (stdout), in child mode we're using stdout for the sync_pipe */
499 if(log_level & G_LOG_LEVEL_MESSAGE) {
500 /* normal user messages without additional infos */
501 fprintf(stderr, "%s\n", message);
504 /* info/debug messages with additional infos */
505 fprintf(stderr, "%02u:%02u:%02u %8s %s %s\n",
506 today->tm_hour, today->tm_min, today->tm_sec,
507 log_domain != NULL ? log_domain : "",
514 /****************************************************************************************************************/
515 /* indication report routines */
519 report_packet_count(int packet_count)
521 char tmp[SP_DECISIZE+1+1];
522 static int count = 0;
526 g_snprintf(tmp, sizeof(tmp), "%d", packet_count);
527 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
528 pipe_write_block(1, SP_PACKET_COUNT, tmp);
530 count += packet_count;
531 fprintf(stderr, "\rPackets: %u ", count);
532 /* stderr could be line buffered */
538 report_new_capture_file(const char *filename)
541 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
544 pipe_write_block(1, SP_FILE, filename);
549 report_cfilter_error(const char *cfilter _U_, const char *errmsg)
552 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
555 pipe_write_block(1, SP_BAD_FILTER, errmsg);
560 report_capture_error(const char *error_msg, const char *secondary_error_msg)
563 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
564 "Primary Error: %s", error_msg);
565 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
566 "Secondary Error: %s", secondary_error_msg);
569 sync_pipe_errmsg_to_parent(error_msg, secondary_error_msg);
574 report_packet_drops(int drops)
576 char tmp[SP_DECISIZE+1+1];
579 g_snprintf(tmp, sizeof(tmp), "%d", drops);
580 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets dropped: %s", tmp);
583 pipe_write_block(1, SP_DROPS, tmp);
588 /****************************************************************************************************************/
589 /* signal_pipe handling */
594 signal_pipe_check_running(void)
596 /* any news from our parent (stdin)? -> just stop the capture */
602 /* if we are running standalone, no check required */
607 handle = (HANDLE) GetStdHandle(STD_INPUT_HANDLE);
608 result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
610 if(!result || avail > 0) {
611 /* peek failed or some bytes really available */
612 /* (if not piping from stdin this would fail) */
613 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
614 "Signal pipe: Stop capture");
615 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
616 "Signal pipe: handle: %x result: %u avail: %u", handle, result, avail);
619 /* pipe ok and no bytes available */
626 /****************************************************************************************************************/
630 const char *netsnmp_get_version(void) { return ""; }