Note what we have to be careful of before we start using Unicode in the
[obnox/wireshark/wip.git] / capture_opts.c
1 /* capture_opts.c
2  * Routines for capture options setting
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <string.h>
32 #include <ctype.h>
33
34 #include <pcap.h>
35
36 #include <glib.h>
37
38 #include <epan/packet.h>
39
40 #include "capture.h"
41 #include "capture_opts.h"
42 #include "ringbuffer.h"
43 #include "clopts_common.h"
44 #include "cmdarg_err.h"
45
46 void
47 capture_opts_init(capture_options *capture_opts, void *cfile)
48 {
49   capture_opts->cf                      = cfile;            
50   capture_opts->cfilter                     = g_strdup("");     /* No capture filter string specified */
51   capture_opts->iface                   = NULL;             /* Default is "pick the first interface" */
52 #ifdef _WIN32
53   capture_opts->buffer_size             = 1;                /* 1 MB */
54 #endif
55   capture_opts->has_snaplen             = FALSE;
56   capture_opts->snaplen                 = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
57                                                                     infinite, in effect */
58   capture_opts->promisc_mode            = TRUE;             /* promiscuous mode is the default */
59   capture_opts->linktype                = -1;               /* the default linktype */
60   capture_opts->save_file               = NULL;
61   capture_opts->real_time_mode          = TRUE;
62   capture_opts->show_info               = TRUE;
63   capture_opts->quit_after_cap          = FALSE;
64   capture_opts->restart                 = FALSE;
65
66   capture_opts->multi_files_on          = FALSE;
67   capture_opts->has_file_duration       = FALSE;
68   capture_opts->file_duration           = 60;               /* 1 min */
69   capture_opts->has_ring_num_files      = FALSE;
70   capture_opts->ring_num_files          = RINGBUFFER_MIN_NUM_FILES;
71
72   capture_opts->has_autostop_files      = FALSE;
73   capture_opts->autostop_files          = 1;
74   capture_opts->has_autostop_packets    = FALSE;            
75   capture_opts->autostop_packets        = 0;
76   capture_opts->has_autostop_filesize   = FALSE;
77   capture_opts->autostop_filesize       = 1024;             /* 1 MB */
78   capture_opts->has_autostop_duration   = FALSE;
79   capture_opts->autostop_duration       = 60;               /* 1 min */
80
81
82   capture_opts->fork_child              = -1;               /* invalid process handle */
83 #ifdef _WIN32
84   capture_opts->signal_pipe_fd          = -1;
85 #endif
86   capture_opts->state                   = CAPTURE_STOPPED;
87 }
88
89
90 /* log content of capture_opts */
91 void
92 capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts) {
93     g_log(log_domain, log_level, "CAPTURE OPTIONS    :");
94     g_log(log_domain, log_level, "CFile              : 0x%p", capture_opts->cf);
95     g_log(log_domain, log_level, "Filter             : %s", capture_opts->cfilter);
96     g_log(log_domain, log_level, "Interface          : %s", capture_opts->iface);
97 #ifdef _WIN32
98     g_log(log_domain, log_level, "BufferSize         : %u (MB)", capture_opts->buffer_size);
99 #endif
100     g_log(log_domain, log_level, "SnapLen         (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
101     g_log(log_domain, log_level, "Promisc            : %u", capture_opts->promisc_mode);
102     g_log(log_domain, log_level, "LinkType           : %d", capture_opts->linktype);
103     g_log(log_domain, log_level, "SaveFile           : %s", (capture_opts->save_file) ? capture_opts->save_file : "");
104     g_log(log_domain, log_level, "RealTimeMode       : %u", capture_opts->real_time_mode);
105     g_log(log_domain, log_level, "ShowInfo           : %u", capture_opts->show_info);
106     g_log(log_domain, log_level, "QuitAfterCap       : %u", capture_opts->quit_after_cap);
107
108     g_log(log_domain, log_level, "MultiFilesOn       : %u", capture_opts->multi_files_on);
109     g_log(log_domain, log_level, "FileDuration    (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
110     g_log(log_domain, log_level, "RingNumFiles    (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
111
112     g_log(log_domain, log_level, "AutostopFiles   (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
113     g_log(log_domain, log_level, "AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
114     g_log(log_domain, log_level, "AutostopFilesize(%u): %u (KB)", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
115     g_log(log_domain, log_level, "AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
116
117     g_log(log_domain, log_level, "ForkChild          : %d", capture_opts->fork_child);
118 #ifdef _WIN32
119     g_log(log_domain, log_level, "SignalPipeFd       : %d", capture_opts->signal_pipe_fd);
120 #endif
121 }
122
123 /*
124  * Given a string of the form "<autostop criterion>:<value>", as might appear
125  * as an argument to a "-a" option, parse it and set the criterion in
126  * question.  Return an indication of whether it succeeded or failed
127  * in some fashion.
128  */
129 static gboolean
130 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
131 {
132   gchar *p, *colonp;
133
134   colonp = strchr(autostoparg, ':');
135   if (colonp == NULL)
136     return FALSE;
137
138   p = colonp;
139   *p++ = '\0';
140
141   /*
142    * Skip over any white space (there probably won't be any, but
143    * as we allow it in the preferences file, we might as well
144    * allow it here).
145    */
146   while (isspace((guchar)*p))
147     p++;
148   if (*p == '\0') {
149     /*
150      * Put the colon back, so if our caller uses, in an
151      * error message, the string they passed us, the message
152      * looks correct.
153      */
154     *colonp = ':';
155     return FALSE;
156   }
157   if (strcmp(autostoparg,"duration") == 0) {
158     capture_opts->has_autostop_duration = TRUE;
159     capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
160   } else if (strcmp(autostoparg,"filesize") == 0) {
161     capture_opts->has_autostop_filesize = TRUE;
162     capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
163   } else if (strcmp(autostoparg,"files") == 0) {
164     capture_opts->multi_files_on = TRUE;
165     capture_opts->has_autostop_files = TRUE;
166     capture_opts->autostop_files = get_positive_int(p,"autostop files");
167   } else {
168     return FALSE;
169   }
170   *colonp = ':'; /* put the colon back */
171   return TRUE;
172 }
173
174 /*
175  * Given a string of the form "<ring buffer file>:<duration>", as might appear
176  * as an argument to a "-b" option, parse it and set the arguments in
177  * question.  Return an indication of whether it succeeded or failed
178  * in some fashion.
179  */
180 static gboolean
181 get_ring_arguments(capture_options *capture_opts, const char *arg)
182 {
183   gchar *p = NULL, *colonp;
184
185   colonp = strchr(arg, ':');
186   if (colonp == NULL)
187     return TRUE;
188
189   p = colonp;
190   *p++ = '\0';
191
192   /*
193    * Skip over any white space (there probably won't be any, but
194    * as we allow it in the preferences file, we might as well
195    * allow it here).
196    */
197   while (isspace((guchar)*p))
198     p++;
199   if (*p == '\0') {
200     /*
201      * Put the colon back, so if our caller uses, in an
202      * error message, the string they passed us, the message
203      * looks correct.
204      */
205     *colonp = ':';
206     return FALSE;
207   }
208
209   if (strcmp(arg,"files") == 0) {
210     capture_opts->has_ring_num_files = TRUE;
211     capture_opts->ring_num_files = get_natural_int(p, "number of ring buffer files");
212   } else if (strcmp(arg,"filesize") == 0) {
213     capture_opts->has_autostop_filesize = TRUE;
214     capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
215   } else if (strcmp(arg,"duration") == 0) {
216     capture_opts->has_file_duration = TRUE;
217     capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
218   }
219
220   *colonp = ':';        /* put the colon back */
221   return TRUE;
222 }
223
224
225 #ifdef _WIN32
226 /*
227  * Given a string of the form "<pipe name>:<file descriptor>", as might appear
228  * as an argument to a "-Z" option, parse it and set the arguments in
229  * question.  Return an indication of whether it succeeded or failed
230  * in some fashion.
231  */
232 static gboolean
233 get_pipe_arguments(capture_options *capture_opts, const char *arg)
234 {
235   gchar *p = NULL, *colonp;
236   int pipe_fd;
237
238
239   colonp = strchr(arg, ':');
240   if (colonp == NULL)
241     return TRUE;
242
243   p = colonp;
244   *p++ = '\0';
245
246   /*
247    * Skip over any white space (there probably won't be any, but
248    * as we allow it in the preferences file, we might as well
249    * allow it here).
250    */
251   while (isspace((guchar)*p))
252     p++;
253   if (*p == '\0') {
254     /*
255      * Put the colon back, so if our caller uses, in an
256      * error message, the string they passed us, the message
257      * looks correct.
258      */
259     *colonp = ':';
260     return FALSE;
261   }
262
263   if (strcmp(arg,"sync") == 0) {
264     /* associate stdout with sync pipe */
265     pipe_fd = get_natural_int(p, "sync pipe file descriptor");
266     if (dup2(pipe_fd, 1) < 0) {
267       cmdarg_err("Unable to dup sync pipe handle");
268       return FALSE;
269     }
270   } else if (strcmp(arg,"signal") == 0) {
271     /* associate stdin with signal pipe */
272     pipe_fd = get_natural_int(p, "signal pipe file descriptor");
273     if (dup2(pipe_fd, 0) < 0) {
274       cmdarg_err("Unable to dup signal pipe handle");
275       return FALSE;
276     }
277   }
278
279   *colonp = ':';        /* put the colon back */
280   return TRUE;
281 }
282 #endif
283
284
285 void
286 capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture)
287 {
288     switch(opt) {
289     case 'a':        /* autostop criteria */
290         if (set_autostop_criterion(capture_opts, optarg) == FALSE) {
291           cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg);
292           exit(1);
293         }
294         break;
295     case 'b':        /* Ringbuffer option */
296         capture_opts->multi_files_on = TRUE;
297         if (get_ring_arguments(capture_opts, optarg) == FALSE) {
298           cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg);
299           exit(1);
300         }
301         break;
302 #ifdef _WIN32
303     case 'B':        /* Buffer size */
304         capture_opts->buffer_size = get_positive_int(optarg, "buffer size");
305         break;
306 #endif
307     case 'c':        /* Capture xxx packets */
308         capture_opts->has_autostop_packets = TRUE;
309         capture_opts->autostop_packets = get_positive_int(optarg, "packet count");
310         break;
311     case 'f':        /* capture filter */
312         if (capture_opts->cfilter)
313             g_free(capture_opts->cfilter);
314         capture_opts->cfilter = g_strdup(optarg);
315         break;
316     case 'H':        /* Hide capture info dialog box */
317         capture_opts->show_info = FALSE;
318         break;
319     case 'i':        /* Use interface xxx */
320         capture_opts->iface = g_strdup(optarg);
321         break;
322     case 'k':        /* Start capture immediately */
323         *start_capture = TRUE;
324         break;
325     /*case 'l':*/    /* Automatic scrolling in live capture mode */
326     case 'p':        /* Don't capture in promiscuous mode */
327         capture_opts->promisc_mode = FALSE;
328         break;
329     case 'Q':        /* Quit after capture (just capture to file) */
330         capture_opts->quit_after_cap  = TRUE;
331         *start_capture   = TRUE;  /*** -Q implies -k !! ***/
332         break;
333     case 's':        /* Set the snapshot (capture) length */
334         capture_opts->has_snaplen = TRUE;
335         capture_opts->snaplen = get_positive_int(optarg, "snapshot length");
336         break;
337     case 'S':        /* "Real-Time" mode: used for following file ala tail -f */
338         capture_opts->real_time_mode = TRUE;
339         break;
340     case 'w':        /* Write to capture file xxx */
341         capture_opts->save_file = g_strdup(optarg);
342             break;
343     case 'y':        /* Set the pcap data link type */
344 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
345         capture_opts->linktype = pcap_datalink_name_to_val(optarg);
346         if (capture_opts->linktype == -1) {
347           cmdarg_err("The specified data link type \"%s\" isn't valid",
348                   optarg);
349           exit(1);
350         }
351 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
352         /* XXX - just treat it as a number */
353         capture_opts->linktype = get_natural_int(optarg, "data link type");
354 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
355         break;
356 #ifdef _WIN32
357       /* Hidden option supporting Sync mode */
358     case 'Z':        /* Write to pipe FD XXX */
359        if (get_pipe_arguments(capture_opts, optarg) == FALSE) {
360           cmdarg_err("Invalid or unknown -Z flag \"%s\"", optarg);
361           exit(1);
362         }
363         break;
364 #endif /* _WIN32 */
365     default:
366         /* the caller is responsible to send us only the right opt's */
367         g_assert_not_reached();
368     }
369 }
370
371
372 void capture_opts_trim(capture_options *capture_opts, int snaplen_min)
373 {
374   if (capture_opts->snaplen < 1)
375     capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
376   else if (capture_opts->snaplen < snaplen_min)
377     capture_opts->snaplen = snaplen_min;
378
379   /* Check the value range of the ring_num_files parameter */
380   if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES)
381     capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
382 #if RINGBUFFER_MIN_NUM_FILES > 0
383   else if (capture_opts->ring_num_files < RINGBUFFER_MIN_NUM_FILES)
384     capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
385 #endif
386 }
387
388 #endif /* HAVE_LIBPCAP */