remove capture_child flag from capture_opts as it's no longer required
[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 #ifdef HAVE_IO_H
35 # include <io.h>
36 #endif
37
38 #include <pcap.h>
39
40 #include <glib.h>
41
42 #include <epan/packet.h>
43
44 #include "capture.h"
45 #include "ringbuffer.h"
46 #include "clopts_common.h"
47
48 void
49 capture_opts_init(capture_options *capture_opts, void *cfile)
50 {
51   capture_opts->cf                      = cfile;            
52   capture_opts->cfilter                     = g_strdup("");     /* No capture filter string specified */
53   capture_opts->iface                   = NULL;             /* Default is "pick the first interface" */
54 #ifdef _WIN32
55   capture_opts->buffer_size             = 1;                /* 1 MB */
56 #endif
57   capture_opts->has_snaplen             = FALSE;
58   capture_opts->snaplen                 = WTAP_MAX_PACKET_SIZE; /* snapshot length - default is
59                                                                     infinite, in effect */
60   capture_opts->promisc_mode            = TRUE;             /* promiscuous mode is the default */
61   capture_opts->linktype                = -1;               /* the default linktype */
62   capture_opts->save_file               = NULL;
63   capture_opts->sync_mode               = TRUE;
64   capture_opts->show_info               = TRUE;
65   capture_opts->quit_after_cap          = FALSE;
66
67   capture_opts->multi_files_on          = FALSE;
68   capture_opts->has_file_duration       = FALSE;
69   capture_opts->file_duration           = 60;               /* 1 min */
70   capture_opts->has_ring_num_files      = FALSE;
71   capture_opts->ring_num_files          = RINGBUFFER_MIN_NUM_FILES;
72
73   capture_opts->has_autostop_files      = FALSE;
74   capture_opts->autostop_files          = 1;
75   capture_opts->has_autostop_packets    = FALSE;            
76   capture_opts->autostop_packets        = 0;
77   capture_opts->has_autostop_filesize   = FALSE;
78   capture_opts->autostop_filesize       = 1024 * 1024;      /* 1 MB */
79   capture_opts->has_autostop_duration   = FALSE;
80   capture_opts->autostop_duration       = 60;               /* 1 min */
81
82
83   capture_opts->fork_child              = -1;               /* invalid process handle */
84 }
85
86
87 /* debug only: print content of capture_opts to console */
88 void
89 capture_opts_info(capture_options *capture_opts) {
90     g_warning("CAPTURE OPTIONS    :");
91     g_warning("File               : %p", capture_opts->cf);
92     g_warning("Filter             : %s", capture_opts->cfilter);
93     g_warning("Interface          : %s", capture_opts->iface);
94 #ifdef _WIN32
95     g_warning("BufferSize         : %u (MB)", capture_opts->buffer_size);
96 #endif
97     g_warning("SnapLen         (%u): %u", capture_opts->has_snaplen, capture_opts->snaplen);
98     g_warning("Promisc            : %u", capture_opts->promisc_mode);
99     g_warning("LinkType           : %d", capture_opts->linktype);
100     g_warning("SaveFile           : %s", capture_opts->save_file);
101     g_warning("SyncMode           : %u", capture_opts->sync_mode);
102     g_warning("ShowInfo           : %u", capture_opts->show_info);
103     g_warning("QuitAfterCap       : %u", capture_opts->quit_after_cap);
104
105     g_warning("MultiFilesOn       : %u", capture_opts->multi_files_on);
106     g_warning("FileDuration    (%u): %u", capture_opts->has_file_duration, capture_opts->file_duration);
107     g_warning("RingNumFiles    (%u): %u", capture_opts->has_ring_num_files, capture_opts->ring_num_files);
108
109     g_warning("AutostopFiles   (%u): %u", capture_opts->has_autostop_files, capture_opts->autostop_files);
110     g_warning("AutostopPackets (%u): %u", capture_opts->has_autostop_packets, capture_opts->autostop_packets);
111     g_warning("AutostopFilesize(%u): %u", capture_opts->has_autostop_filesize, capture_opts->autostop_filesize);
112     g_warning("AutostopDuration(%u): %u", capture_opts->has_autostop_duration, capture_opts->autostop_duration);
113
114     g_warning("ForkChild          : %d", capture_opts->fork_child);
115 }
116
117 /*
118  * Given a string of the form "<autostop criterion>:<value>", as might appear
119  * as an argument to a "-a" option, parse it and set the criterion in
120  * question.  Return an indication of whether it succeeded or failed
121  * in some fashion.
122  */
123 static gboolean
124 set_autostop_criterion(capture_options *capture_opts, const char *appname, const char *autostoparg)
125 {
126   gchar *p, *colonp;
127
128   colonp = strchr(autostoparg, ':');
129   if (colonp == NULL)
130     return FALSE;
131
132   p = colonp;
133   *p++ = '\0';
134
135   /*
136    * Skip over any white space (there probably won't be any, but
137    * as we allow it in the preferences file, we might as well
138    * allow it here).
139    */
140   while (isspace((guchar)*p))
141     p++;
142   if (*p == '\0') {
143     /*
144      * Put the colon back, so if our caller uses, in an
145      * error message, the string they passed us, the message
146      * looks correct.
147      */
148     *colonp = ':';
149     return FALSE;
150   }
151   if (strcmp(autostoparg,"duration") == 0) {
152     capture_opts->has_autostop_duration = TRUE;
153     capture_opts->autostop_duration = get_positive_int(appname, p,"autostop duration");
154   } else if (strcmp(autostoparg,"filesize") == 0) {
155     capture_opts->has_autostop_filesize = TRUE;
156     capture_opts->autostop_filesize = get_positive_int(appname, p,"autostop filesize");
157   } else if (strcmp(autostoparg,"files") == 0) {
158     capture_opts->multi_files_on = TRUE;
159     capture_opts->has_autostop_files = TRUE;
160     capture_opts->autostop_files = get_positive_int(appname, p,"autostop files");
161   } else {
162     return FALSE;
163   }
164   *colonp = ':'; /* put the colon back */
165   return TRUE;
166 }
167
168 /*
169  * Given a string of the form "<ring buffer file>:<duration>", as might appear
170  * as an argument to a "-b" option, parse it and set the arguments in
171  * question.  Return an indication of whether it succeeded or failed
172  * in some fashion.
173  */
174 static gboolean
175 get_ring_arguments(capture_options *capture_opts, const char *appname, const char *arg)
176 {
177   gchar *p = NULL, *colonp;
178
179   colonp = strchr(arg, ':');
180   if (colonp == NULL)
181     return TRUE;
182
183   p = colonp;
184   *p++ = '\0';
185
186   /*
187    * Skip over any white space (there probably won't be any, but
188    * as we allow it in the preferences file, we might as well
189    * allow it here).
190    */
191   while (isspace((guchar)*p))
192     p++;
193   if (*p == '\0') {
194     /*
195      * Put the colon back, so if our caller uses, in an
196      * error message, the string they passed us, the message
197      * looks correct.
198      */
199     *colonp = ':';
200     return FALSE;
201   }
202
203   if (strcmp(arg,"files") == 0) {
204     capture_opts->has_ring_num_files = TRUE;
205     capture_opts->ring_num_files = get_natural_int(appname, p, "number of ring buffer files");
206   } else if (strcmp(arg,"filesize") == 0) {
207     capture_opts->has_autostop_filesize = TRUE;
208     capture_opts->autostop_filesize = get_positive_int(appname, p, "ring buffer filesize");
209   } else if (strcmp(arg,"duration") == 0) {
210     capture_opts->has_file_duration = TRUE;
211     capture_opts->file_duration = get_positive_int(appname, p, "ring buffer duration");
212   }
213
214   *colonp = ':';        /* put the colon back */
215   return TRUE;
216 }
217
218
219 void
220 capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture)
221 {
222 #ifdef _WIN32
223     int i;
224 #endif
225
226     switch(opt) {
227     case 'a':        /* autostop criteria */
228         if (set_autostop_criterion(capture_opts, appname, optarg) == FALSE) {
229           fprintf(stderr, "%s: Invalid or unknown -a flag \"%s\"\n", appname, optarg);
230           exit(1);
231         }
232         break;
233     case 'b':        /* Ringbuffer option */
234         capture_opts->multi_files_on = TRUE;
235         if (get_ring_arguments(capture_opts, appname, optarg) == FALSE) {
236           fprintf(stderr, "%s: Invalid or unknown -b arg \"%s\"\n", appname, optarg);
237           exit(1);
238         }
239         break;
240     case 'c':        /* Capture xxx packets */
241         capture_opts->has_autostop_packets = TRUE;
242         capture_opts->autostop_packets = get_positive_int(appname, optarg, "packet count");
243         break;
244     case 'f':        /* capture filter */
245         if (capture_opts->cfilter)
246             g_free(capture_opts->cfilter);
247         capture_opts->cfilter = g_strdup(optarg);
248         break;
249     case 'H':        /* Hide capture info dialog box */
250         capture_opts->show_info = FALSE;
251         break;
252     case 'i':        /* Use interface xxx */
253         capture_opts->iface = g_strdup(optarg);
254         break;
255     case 'k':        /* Start capture immediately */
256         *start_capture = TRUE;
257         break;
258     /*case 'l':*/    /* Automatic scrolling in live capture mode */
259     case 'p':        /* Don't capture in promiscuous mode */
260         capture_opts->promisc_mode = FALSE;
261         break;
262     case 'Q':        /* Quit after capture (just capture to file) */
263         capture_opts->quit_after_cap  = TRUE;
264         *start_capture   = TRUE;  /*** -Q implies -k !! ***/
265         break;
266     case 's':        /* Set the snapshot (capture) length */
267         capture_opts->has_snaplen = TRUE;
268         capture_opts->snaplen = get_positive_int(appname, optarg, "snapshot length");
269         break;
270     case 'S':        /* "Sync" mode: used for following file ala tail -f */
271         capture_opts->sync_mode = TRUE;
272         break;
273     case 'w':        /* Write to capture file xxx */
274         capture_opts->save_file = g_strdup(optarg);
275             break;
276     case 'y':        /* Set the pcap data link type */
277 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
278         capture_opts->linktype = pcap_datalink_name_to_val(optarg);
279         if (capture_opts->linktype == -1) {
280           fprintf(stderr, "%s: The specified data link type \"%s\" isn't valid\n",
281                   appname, optarg);
282           exit(1);
283         }
284 #else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
285         /* XXX - just treat it as a number */
286         capture_opts->linktype = get_natural_int(appname, optarg, "data link type");
287 #endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
288         break;
289 #ifdef _WIN32
290       /* Hidden option supporting Sync mode */
291     case 'Z':        /* Write to pipe FD XXX */
292         /* associate stdout with pipe */
293         i = atoi(optarg);
294         if (dup2(i, 1) < 0) {
295           fprintf(stderr, "%s: Unable to dup pipe handle\n", appname);
296           exit(1);
297         }
298         break;
299 #endif /* _WIN32 */
300     default:
301         /* the caller is responsible to send us only the right opt's */
302         g_assert_not_reached();
303     }
304 }
305
306 #endif /* HAVE_LIBPCAP */