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