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"
58 #include "capture_loop.h"
59 #include "capture_sync.h"
61 #include "simple_dialog.h"
64 #include "file_util.h"
67 /*#define DEBUG_DUMPCAP*/
69 gboolean capture_child = FALSE; /* FALSE: standalone call, TRUE: this is an Ethereal capture child */
72 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
73 const char *message, gpointer user_data _U_);
75 /* capture related options */
76 capture_options global_capture_opts;
77 capture_options *capture_opts = &global_capture_opts;
80 void exit_main(int err) __attribute__ ((noreturn));
82 void exit_main(int err);
87 print_usage(gboolean print_ver) {
95 "Dumpcap " VERSION "%s\n"
96 "Capture network packets and dump them into a libpcap file.\n"
97 "See http://www.ethereal.com for more information.\n",
102 fprintf(output, "\nUsage: dumpcap [options] ...\n");
103 fprintf(output, "\n");
104 fprintf(output, "Capture interface:\n");
105 fprintf(output, " -i <interface> name or idx of interface (def: first none loopback)\n");
106 fprintf(output, " -f <capture filter> packet filter in libpcap filter syntax\n");
107 fprintf(output, " -s <snaplen> packet snapshot length (def: 65535)\n");
108 fprintf(output, " -p don't capture in promiscuous mode\n");
110 fprintf(output, " -B <buffer size> size of kernel buffer (def: 1MB)\n");
112 fprintf(output, " -y <link type> link layer type (def: first appropriate)\n");
113 fprintf(output, " -D print list of interfaces and exit\n");
114 fprintf(output, " -L print list of link-layer types of iface and exit\n");
115 fprintf(output, "\n");
116 fprintf(output, "Stop conditions:\n");
117 fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
118 fprintf(output, " -a <autostop cond.> ... duration:NUM - stop after NUM seconds\n");
119 fprintf(output, " filesize:NUM - stop this file after NUM KB\n");
120 fprintf(output, " files:NUM - stop after NUM files\n");
121 /*fprintf(output, "\n");*/
122 fprintf(output, "Output (files):\n");
123 fprintf(output, " -w <filename> name of file to save (def: tempfile)\n");
124 fprintf(output, " -b <ringbuffer opt.> ... duration:NUM - switch to next file after NUM secs\n");
125 fprintf(output, " filesize:NUM - switch to next file after NUM KB\n");
126 fprintf(output, " files:NUM - ringbuffer: replace after NUM files\n");
127 /*fprintf(output, "\n");*/
128 fprintf(output, "Miscellaneous:\n");
129 fprintf(output, " -v print version information and exit\n");
130 fprintf(output, " -h display this help and exit\n");
131 fprintf(output, "\n");
132 fprintf(output, "Example: dumpcap -i eth0 -a duration:60 -w output.pcap\n");
133 fprintf(output, "\"Capture network packets from interface eth0 until 60s passed into output.pcap\"\n");
134 fprintf(output, "\n");
135 fprintf(output, "Use Ctrl-C to stop capturing at any time.\n");
139 show_version(GString *comp_info_str, GString *runtime_info_str)
143 "Dumpcap " VERSION "%s\n"
148 "See http://www.ethereal.com for more information.\n",
149 svnversion, get_copyright_info() ,comp_info_str->str, runtime_info_str->str);
153 * Report an error in command-line arguments.
156 cmdarg_err(const char *fmt, ...)
161 /* XXX - convert to g_log */
164 fprintf(stderr, "dumpcap: ");
165 vfprintf(stderr, fmt, ap);
166 fprintf(stderr, "\n");
172 * Report additional information for an error in command-line arguments.
175 cmdarg_err_cont(const char *fmt, ...)
180 /* XXX - convert to g_log */
183 vfprintf(stderr, fmt, ap);
184 fprintf(stderr, "\n");
191 BOOL WINAPI ConsoleCtrlHandlerRoutine(DWORD dwCtrlType)
193 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
195 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
196 "Console: Ctrl+C CtrlType: %u", dwCtrlType);
204 void exit_main(int status)
207 /* Shutdown windows sockets */
211 /* can be helpful for debugging */
213 printf("Press any key\n");
221 /* And now our feature presentation... [ fade to music ] */
223 main(int argc, char *argv[])
227 gboolean arg_error = FALSE;
228 GString *comp_info_str;
229 GString *runtime_info_str;
235 gboolean start_capture = TRUE;
236 gboolean stats_known;
237 struct pcap_stat stats;
238 GLogLevelFlags log_flags;
239 gboolean list_link_layer_types = FALSE;
242 #define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:Z"
245 #define OPTSTRING_WIN32 "B:"
247 #define OPTSTRING_WIN32 ""
250 char optstring[sizeof(OPTSTRING_INIT) + sizeof(OPTSTRING_WIN32) - 1] =
251 OPTSTRING_INIT OPTSTRING_WIN32;
254 /* Load wpcap if possible. Do this before collecting the run-time version information */
257 /* ... and also load the packet.dll from wpcap */
258 /* XXX - currently not required, may change later. */
259 /*wpcap_packet_load();*/
261 /* Start windows sockets */
262 WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
264 /* Set handler for Ctrl+C key */
265 SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
268 /* Assemble the compile-time version information string */
269 comp_info_str = g_string_new("Compiled ");
270 g_string_append(comp_info_str, "with ");
271 get_compiled_version_info(comp_info_str);
273 /* Assemble the run-time version information string */
274 runtime_info_str = g_string_new("Running ");
275 get_runtime_version_info(runtime_info_str);
277 /* the default_log_handler will use stdout, which makes trouble in */
278 /* capture child mode, as it uses stdout for it's sync_pipe */
279 /* so do the filtering in the console_log_handler and not here */
282 G_LOG_LEVEL_CRITICAL|
287 G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION;
289 g_log_set_handler(NULL,
291 console_log_handler, NULL /* user_data */);
292 g_log_set_handler(LOG_DOMAIN_MAIN,
294 console_log_handler, NULL /* user_data */);
295 g_log_set_handler(LOG_DOMAIN_CAPTURE,
297 console_log_handler, NULL /* user_data */);
298 g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
300 console_log_handler, NULL /* user_data */);
302 /* Set the initial values in the capture_opts. This might be overwritten
303 by the command line parameters. */
304 capture_opts_init(capture_opts, NULL);
306 capture_opts->snaplen = MIN_PACKET_SIZE;
307 capture_opts->has_ring_num_files = TRUE;
309 /* Now get our args */
310 while ((opt = getopt(argc, argv, optstring)) != -1) {
312 case 'h': /* Print help and exit */
316 case 'v': /* Show version and exit */
317 show_version(comp_info_str, runtime_info_str);
320 /*** capture option specific ***/
321 case 'a': /* autostop criteria */
322 case 'b': /* Ringbuffer option */
323 case 'c': /* Capture x packets */
324 case 'f': /* capture filter */
325 case 'i': /* Use interface x */
326 case 'p': /* Don't capture in promiscuous mode */
327 case 's': /* Set the snapshot (capture) length */
328 case 'w': /* Write to capture file x */
329 case 'y': /* Set the pcap data link type */
331 case 'B': /* Buffer size */
333 status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
338 /*** hidden option: Ethereal child mode (using binary output messages) ***/
340 capture_child = TRUE;
342 /* set output pipe to binary mode, to avoid ugly text conversions */
343 _setmode(1, O_BINARY);
347 /*** all non capture option specific ***/
348 case 'D': /* Print a list of capture devices and exit */
349 status = capture_opts_list_interfaces();
352 case 'L': /* Print list of link-layer types and exit */
353 list_link_layer_types = TRUE;
356 case '?': /* Bad flag - print usage message */
357 cmdarg_err("Invalid Option: %s", argv[optind-1]);
365 /* user specified file name as regular command-line argument */
366 /* XXX - use it as the capture file name (or something else)? */
373 * Extra command line arguments were specified; complain.
375 cmdarg_err("Invalid argument: %s", argv[0]);
384 if (list_link_layer_types) {
385 /* We're supposed to list the link-layer types for an interface;
386 did the user also specify a capture file to be read? */
387 /* No - did they specify a ring buffer option? */
388 if (capture_opts->multi_files_on) {
389 cmdarg_err("Ring buffer requested, but a capture isn't being done.");
393 /* No - was the ring buffer option specified and, if so, does it make
395 if (capture_opts->multi_files_on) {
396 /* Ring buffer works only under certain conditions:
397 a) ring buffer does not work with temporary files;
398 b) it makes no sense to enable the ring buffer if the maximum
399 file size is set to "infinite". */
400 if (capture_opts->save_file == NULL) {
401 cmdarg_err("Ring buffer requested, but capture isn't being saved to a permanent file.");
402 capture_opts->multi_files_on = FALSE;
404 if (!capture_opts->has_autostop_filesize && !capture_opts->has_file_duration) {
405 cmdarg_err("Ring buffer requested, but no maximum capture file size or duration were specified.");
406 /* XXX - this must be redesigned as the conditions changed */
407 /* capture_opts->multi_files_on = FALSE;*/
412 if (capture_opts_trim_iface(capture_opts, NULL) == FALSE) {
413 cmdarg_err("No capture interfaces available (maybe lack of privileges?).");
417 /* Let the user know what interface was chosen. */
418 /* get_interface_descriptive_name() is not available! */
419 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_MESSAGE, "Interface: %s\n", capture_opts->iface);
421 if (list_link_layer_types) {
422 status = capture_opts_list_link_layer_types(capture_opts);
426 capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);
427 capture_opts_trim_ring_num_files(capture_opts);
429 /* Now start the capture. */
431 if(capture_loop_start(capture_opts, &stats_known, &stats) == TRUE) {
442 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
443 const char *message, gpointer user_data _U_)
454 /* ignore log message, if log_level isn't interesting */
455 if( !(log_level & G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO))) {
456 #ifndef DEBUG_DUMPCAP
461 /* create a "timestamp" */
463 today = localtime(&curr);
465 switch(log_level & G_LOG_LEVEL_MASK) {
466 case G_LOG_LEVEL_ERROR:
469 case G_LOG_LEVEL_CRITICAL:
472 case G_LOG_LEVEL_WARNING:
475 case G_LOG_LEVEL_MESSAGE:
478 case G_LOG_LEVEL_INFO:
481 case G_LOG_LEVEL_DEBUG:
485 fprintf(stderr, "unknown log_level %u\n", log_level);
487 g_assert_not_reached();
490 /* don't use printf (stdout), in child mode we're using stdout for the sync_pipe */
491 if(log_level & G_LOG_LEVEL_MESSAGE) {
492 /* normal user messages without additional infos */
493 fprintf(stderr, "%s\n", message);
496 /* info/debug messages with additional infos */
497 fprintf(stderr, "%02u:%02u:%02u %8s %s %s\n",
498 today->tm_hour, today->tm_min, today->tm_sec,
499 log_domain != NULL ? log_domain : "",
506 /****************************************************************************************************************/
507 /* sync_pipe handling */
510 /* write a single message header to the recipient pipe */
512 pipe_write_header(int pipe, char indicator, int length)
514 guchar header[1+3]; /* indicator + 3-byte len */
517 g_assert(length <= SP_MAX_MSG_LEN);
519 /* write header (indicator + 3-byte len) */
520 header[0] = indicator;
521 header[1] = (length >> 16) & 0xFF;
522 header[2] = (length >> 8) & 0xFF;
523 header[3] = (length >> 0) & 0xFF;
526 return write(pipe, header, sizeof header);
530 /* write a message to the recipient pipe in the standard format
531 (3 digit message length (excluding length and indicator field),
532 1 byte message indicator and the rest is the message).
533 If msg is NULL, the message has only a length and indicator. */
535 pipe_write_block(int pipe, char indicator, const char *msg)
540 /*g_warning("write %d enter", pipe);*/
543 len = strlen(msg) + 1; /* including the terminating '\0'! */
548 /* write header (indicator + 3-byte len) */
549 ret = pipe_write_header(pipe, indicator, len);
554 /* write value (if we have one) */
556 /*g_warning("write %d indicator: %c value len: %u msg: %s", pipe, indicator, len, msg);*/
557 ret = write(pipe, msg, len);
562 /*g_warning("write %d indicator: %c no value", pipe, indicator);*/
565 /*g_warning("write %d leave", pipe);*/
570 sync_pipe_packet_count_to_parent(int packet_count)
572 char tmp[SP_DECISIZE+1+1];
573 static int count = 0;
577 g_snprintf(tmp, sizeof(tmp), "%d", packet_count);
578 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
579 pipe_write_block(1, SP_PACKET_COUNT, tmp);
581 count += packet_count;
582 fprintf(stderr, "\rPackets: %u ", count);
583 /* stderr could be line buffered */
589 sync_pipe_filename_to_parent(const char *filename)
592 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_MESSAGE, "File: %s", filename);
595 pipe_write_block(1, SP_FILE, filename);
600 sync_pipe_cfilter_error_to_parent(const char *cfilter _U_, const char *errmsg)
603 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
606 pipe_write_block(1, SP_BAD_FILTER, errmsg);
611 sync_pipe_errmsg_to_parent(const char *error_msg, const char *secondary_error_msg)
614 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_MESSAGE,
615 "Primary Error: %s", error_msg);
616 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_MESSAGE,
617 "Secondary Error: %s", secondary_error_msg);
620 /* first write a "master header" with the length of the two messages plus their "slave headers" */
621 pipe_write_header(1, SP_ERROR_MSG, strlen(error_msg) + 1 + 4 + strlen(secondary_error_msg) + 1 + 4);
622 pipe_write_block(1, SP_ERROR_MSG, error_msg);
623 pipe_write_block(1, SP_ERROR_MSG, secondary_error_msg);
628 sync_pipe_drops_to_parent(int drops)
630 char tmp[SP_DECISIZE+1+1];
633 g_snprintf(tmp, sizeof(tmp), "%d", drops);
634 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_MESSAGE, "Packets dropped: %s", tmp);
637 pipe_write_block(1, SP_DROPS, tmp);
642 /****************************************************************************************************************/
643 /* signal_pipe handling */
648 signal_pipe_check_running(void)
650 /* any news from our parent (stdin)? -> just stop the capture */
656 /* if we are running standalone, no check required */
661 handle = (HANDLE) GetStdHandle(STD_INPUT_HANDLE);
662 result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
664 if(!result || avail > 0) {
665 /* peek failed or some bytes really available */
666 /* (if not piping from stdin this would fail) */
667 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
668 "Signal pipe: Stop capture");
669 g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
670 "Signal pipe: handle: %x result: %u avail: %u", handle, result, avail);
673 /* pipe ok and no bytes available */
680 /****************************************************************************************************************/
684 const char *netsnmp_get_version(void) { return ""; }