5 * Ethereal - Network traffic analyzer
6 * By Gerald Combs <gerald@ethereal.com>
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 Ethereal 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.ethereal.com 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.ethereal.com 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,
197 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
198 "Console: Ctrl+C CtrlType: %u", dwCtrlType);
206 void exit_main(int status)
209 /* Shutdown windows sockets */
212 /* can be helpful for debugging */
214 printf("Press any key\n");
224 /* And now our feature presentation... [ fade to music ] */
226 main(int argc, char *argv[])
230 gboolean arg_error = FALSE;
231 GString *comp_info_str;
232 GString *runtime_info_str;
238 gboolean start_capture = TRUE;
239 gboolean stats_known;
240 struct pcap_stat stats;
241 GLogLevelFlags log_flags;
242 gboolean list_link_layer_types = FALSE;
245 #define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:Z"
248 #define OPTSTRING_WIN32 "B:"
250 #define OPTSTRING_WIN32 ""
253 char optstring[sizeof(OPTSTRING_INIT) + sizeof(OPTSTRING_WIN32) - 1] =
254 OPTSTRING_INIT OPTSTRING_WIN32;
257 /* Load wpcap if possible. Do this before collecting the run-time version information */
260 /* ... and also load the packet.dll from wpcap */
261 /* XXX - currently not required, may change later. */
262 /*wpcap_packet_load();*/
264 /* Start windows sockets */
265 WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
267 /* Set handler for Ctrl+C key */
268 SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
271 /* Assemble the compile-time version information string */
272 comp_info_str = g_string_new("Compiled ");
273 g_string_append(comp_info_str, "with ");
274 get_compiled_version_info(comp_info_str);
276 /* Assemble the run-time version information string */
277 runtime_info_str = g_string_new("Running ");
278 get_runtime_version_info(runtime_info_str);
280 /* the default_log_handler will use stdout, which makes trouble in */
281 /* capture child mode, as it uses stdout for it's sync_pipe */
282 /* so do the filtering in the console_log_handler and not here */
285 G_LOG_LEVEL_CRITICAL|
290 G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION;
292 g_log_set_handler(NULL,
294 console_log_handler, NULL /* user_data */);
295 g_log_set_handler(LOG_DOMAIN_MAIN,
297 console_log_handler, NULL /* user_data */);
298 g_log_set_handler(LOG_DOMAIN_CAPTURE,
300 console_log_handler, NULL /* user_data */);
301 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
303 console_log_handler, NULL /* user_data */);
305 /* Set the initial values in the capture_opts. This might be overwritten
306 by the command line parameters. */
307 capture_opts_init(capture_opts, NULL);
309 /* Default to capturing the entire packet. */
310 capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
312 /* We always save to a file - if no file was specified, we save to a
314 capture_opts->saving_to_file = TRUE;
315 capture_opts->has_ring_num_files = TRUE;
317 /* Now get our args */
318 while ((opt = getopt(argc, argv, optstring)) != -1) {
320 case 'h': /* Print help and exit */
324 case 'v': /* Show version and exit */
325 show_version(comp_info_str, runtime_info_str);
328 /*** capture option specific ***/
329 case 'a': /* autostop criteria */
330 case 'b': /* Ringbuffer option */
331 case 'c': /* Capture x packets */
332 case 'f': /* capture filter */
333 case 'i': /* Use interface x */
334 case 'p': /* Don't capture in promiscuous mode */
335 case 's': /* Set the snapshot (capture) length */
336 case 'w': /* Write to capture file x */
337 case 'y': /* Set the pcap data link type */
339 case 'B': /* Buffer size */
341 status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
346 /*** hidden option: Ethereal child mode (using binary output messages) ***/
348 capture_child = TRUE;
350 /* set output pipe to binary mode, to avoid ugly text conversions */
351 _setmode(1, O_BINARY);
355 /*** all non capture option specific ***/
356 case 'D': /* Print a list of capture devices and exit */
357 status = capture_opts_list_interfaces();
360 case 'L': /* Print list of link-layer types and exit */
361 list_link_layer_types = TRUE;
364 case '?': /* Bad flag - print usage message */
365 cmdarg_err("Invalid Option: %s", argv[optind-1]);
373 /* user specified file name as regular command-line argument */
374 /* XXX - use it as the capture file name (or something else)? */
381 * Extra command line arguments were specified; complain.
383 cmdarg_err("Invalid argument: %s", argv[0]);
392 if (list_link_layer_types) {
393 /* We're supposed to list the link-layer types for an interface;
394 did the user also specify a capture file to be read? */
395 /* No - did they specify a ring buffer option? */
396 if (capture_opts->multi_files_on) {
397 cmdarg_err("Ring buffer requested, but a capture isn't being done.");
401 /* No - was the ring buffer option specified and, if so, does it make
403 if (capture_opts->multi_files_on) {
404 /* Ring buffer works only under certain conditions:
405 a) ring buffer does not work with temporary files;
406 b) it makes no sense to enable the ring buffer if the maximum
407 file size is set to "infinite". */
408 if (capture_opts->save_file == NULL) {
409 cmdarg_err("Ring buffer requested, but capture isn't being saved to a permanent file.");
410 capture_opts->multi_files_on = FALSE;
412 if (!capture_opts->has_autostop_filesize && !capture_opts->has_file_duration) {
413 cmdarg_err("Ring buffer requested, but no maximum capture file size or duration were specified.");
414 /* XXX - this must be redesigned as the conditions changed */
415 /* capture_opts->multi_files_on = FALSE;*/
420 if (capture_opts_trim_iface(capture_opts, NULL) == FALSE) {
421 cmdarg_err("No capture interfaces available (maybe lack of privileges?).");
425 /* Let the user know what interface was chosen. */
426 /* get_interface_descriptive_name() is not available! */
427 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", capture_opts->iface);
429 if (list_link_layer_types) {
430 status = capture_opts_list_link_layer_types(capture_opts);
434 capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);
435 capture_opts_trim_ring_num_files(capture_opts);
437 /* Now start the capture. */
439 if(capture_loop_start(capture_opts, &stats_known, &stats) == TRUE) {
450 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
451 const char *message, gpointer user_data _U_)
458 /* ignore log message, if log_level isn't interesting */
459 if( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
460 #ifndef DEBUG_DUMPCAP
465 /* create a "timestamp" */
467 today = localtime(&curr);
469 switch(log_level & G_LOG_LEVEL_MASK) {
470 case G_LOG_LEVEL_ERROR:
473 case G_LOG_LEVEL_CRITICAL:
476 case G_LOG_LEVEL_WARNING:
479 case G_LOG_LEVEL_MESSAGE:
482 case G_LOG_LEVEL_INFO:
485 case G_LOG_LEVEL_DEBUG:
489 fprintf(stderr, "unknown log_level %u\n", log_level);
491 g_assert_not_reached();
494 /* don't use printf (stdout), in child mode we're using stdout for the sync_pipe */
495 if(log_level & G_LOG_LEVEL_MESSAGE) {
496 /* normal user messages without additional infos */
497 fprintf(stderr, "%s\n", message);
500 /* info/debug messages with additional infos */
501 fprintf(stderr, "%02u:%02u:%02u %8s %s %s\n",
502 today->tm_hour, today->tm_min, today->tm_sec,
503 log_domain != NULL ? log_domain : "",
510 /****************************************************************************************************************/
511 /* indication report routines */
515 report_packet_count(int packet_count)
517 char tmp[SP_DECISIZE+1+1];
518 static int count = 0;
522 g_snprintf(tmp, sizeof(tmp), "%d", packet_count);
523 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
524 pipe_write_block(1, SP_PACKET_COUNT, tmp);
526 count += packet_count;
527 fprintf(stderr, "\rPackets: %u ", count);
528 /* stderr could be line buffered */
534 report_new_capture_file(const char *filename)
537 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
540 pipe_write_block(1, SP_FILE, filename);
545 report_cfilter_error(const char *cfilter _U_, const char *errmsg)
548 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
551 pipe_write_block(1, SP_BAD_FILTER, errmsg);
556 report_capture_error(const char *error_msg, const char *secondary_error_msg)
559 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
560 "Primary Error: %s", error_msg);
561 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
562 "Secondary Error: %s", secondary_error_msg);
565 sync_pipe_errmsg_to_parent(error_msg, secondary_error_msg);
570 report_packet_drops(int drops)
572 char tmp[SP_DECISIZE+1+1];
575 g_snprintf(tmp, sizeof(tmp), "%d", drops);
576 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets dropped: %s", tmp);
579 pipe_write_block(1, SP_DROPS, tmp);
584 /****************************************************************************************************************/
585 /* signal_pipe handling */
590 signal_pipe_check_running(void)
592 /* any news from our parent (stdin)? -> just stop the capture */
598 /* if we are running standalone, no check required */
603 handle = (HANDLE) GetStdHandle(STD_INPUT_HANDLE);
604 result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
606 if(!result || avail > 0) {
607 /* peek failed or some bytes really available */
608 /* (if not piping from stdin this would fail) */
609 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
610 "Signal pipe: Stop capture");
611 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
612 "Signal pipe: handle: %x result: %u avail: %u", handle, result, avail);
615 /* pipe ok and no bytes available */
622 /****************************************************************************************************************/
626 const char *netsnmp_get_version(void) { return ""; }