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