Untabify.
[obnox/wireshark/wip.git] / capture_sync.c
1 /* capture_sync.c
2  * Synchronisation between Wireshark capture parent and child instances
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <glib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #ifdef HAVE_FCNTL_H
41 #include <fcntl.h>
42 #endif
43
44 #include <signal.h>
45
46 #ifdef _WIN32
47 #include <wsutil/unicode-utils.h>
48 #endif
49
50 #ifdef HAVE_SYS_WAIT_H
51 # include <sys/wait.h>
52 #endif
53
54 #include "capture-pcap-util.h"
55
56 #ifndef _WIN32
57 /*
58  * Define various POSIX macros (and, in the case of WCOREDUMP, non-POSIX
59  * macros) on UNIX systems that don't have them.
60  */
61 #ifndef WIFEXITED
62 # define WIFEXITED(status)      (((status) & 0177) == 0)
63 #endif
64 #ifndef WIFSTOPPED
65 # define WIFSTOPPED(status)     (((status) & 0177) == 0177)
66 #endif
67 #ifndef WIFSIGNALED
68 # define WIFSIGNALED(status)    (!WIFSTOPPED(status) && !WIFEXITED(status))
69 #endif
70 #ifndef WEXITSTATUS
71 # define WEXITSTATUS(status)    ((status) >> 8)
72 #endif
73 #ifndef WTERMSIG
74 # define WTERMSIG(status)       ((status) & 0177)
75 #endif
76 #ifndef WCOREDUMP
77 # define WCOREDUMP(status)      ((status) & 0200)
78 #endif
79 #ifndef WSTOPSIG
80 # define WSTOPSIG(status)       ((status) >> 8)
81 #endif
82 #endif /* _WIN32 */
83
84 #include <epan/packet.h>
85 #include <epan/prefs.h>
86
87 #include "globals.h"
88 #include "file.h"
89 #include <epan/filesystem.h>
90 #include <epan/report_err.h>
91
92 #include "capture.h"
93 #include "capture_sync.h"
94
95 #include "sync_pipe.h"
96
97 #ifdef _WIN32
98 #include "capture-wpcap.h"
99 #endif
100 #include "ui_util.h"
101 #include <wsutil/file_util.h>
102 #include "log.h"
103
104 #ifdef _WIN32
105 #include <process.h>    /* For spawning child process */
106 #endif
107
108
109
110 #ifndef _WIN32
111 static const char *sync_pipe_signame(int);
112 #endif
113
114
115 static gboolean sync_pipe_input_cb(gint source, gpointer user_data);
116 static int sync_pipe_wait_for_child(int fork_child, gchar **msgp);
117 static void pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len);
118 static int pipe_read_block(int pipe_fd, char *indicator, int len, char *msg);
119
120
121
122 /* Append an arg (realloc) to an argc/argv array */
123 /* (add a string pointer to a NULL-terminated array of string pointers) */
124 static const char **
125 sync_pipe_add_arg(const char **args, int *argc, const char *arg)
126 {
127   /* Grow the array; "*argc" currently contains the number of string
128      pointers, *not* counting the NULL pointer at the end, so we have
129      to add 2 in order to get the new size of the array, including the
130      new pointer and the terminating NULL pointer. */
131   args = g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
132
133   /* Stuff the pointer into the penultimate element of the array, which
134      is the one at the index specified by "*argc". */
135   args[*argc] = arg;
136
137   /* Now bump the count. */
138   (*argc)++;
139
140   /* We overwrite the NULL pointer; put it back right after the
141      element we added. */
142   args[*argc] = NULL;
143
144   return args;
145 }
146
147
148
149 #ifdef _WIN32
150 /* Quote the argument element if necessary, so that it will get
151  * reconstructed correctly in the C runtime startup code.  Note that
152  * the unquoting algorithm in the C runtime is really weird, and
153  * rather different than what Unix shells do. See stdargv.c in the C
154  * runtime sources (in the Platform SDK, in src/crt).
155  *
156  * Stolen from GLib's protect_argv(), an internal routine that quotes
157  * string in an argument list so that they arguments will be handled
158  * correctly in the command-line string passed to CreateProcess()
159  * if that string is constructed by gluing those strings together.
160  */
161 static gchar *
162 protect_arg (const gchar *argv)
163 {
164     gchar *new_arg;
165     const gchar *p = argv;
166     gchar *q;
167     gint len = 0;
168     gboolean need_dblquotes = FALSE;
169
170     while (*p) {
171         if (*p == ' ' || *p == '\t')
172             need_dblquotes = TRUE;
173         else if (*p == '"')
174             len++;
175         else if (*p == '\\') {
176             const gchar *pp = p;
177
178             while (*pp && *pp == '\\')
179                 pp++;
180             if (*pp == '"')
181                 len++;
182         }
183         len++;
184         p++;
185     }
186
187     q = new_arg = g_malloc (len + need_dblquotes*2 + 1);
188     p = argv;
189
190     if (need_dblquotes)
191         *q++ = '"';
192
193     while (*p) {
194         if (*p == '"')
195             *q++ = '\\';
196         else if (*p == '\\') {
197             const gchar *pp = p;
198
199             while (*pp && *pp == '\\')
200                 pp++;
201             if (*pp == '"')
202                 *q++ = '\\';
203         }
204         *q++ = *p;
205         p++;
206     }
207
208     if (need_dblquotes)
209         *q++ = '"';
210     *q++ = '\0';
211
212     return new_arg;
213 }
214
215 /*
216  * Generate a string for a Win32 error.
217  */
218 #define ERRBUF_SIZE    1024
219 static char *
220 win32strerror(DWORD error)
221 {
222     static char errbuf[ERRBUF_SIZE+1];
223     size_t errlen;
224     char *p;
225
226     FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
227                    ERRBUF_SIZE, NULL);
228
229     /*
230      * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
231      * message.  Get rid of it.
232      */
233     errlen = strlen(errbuf);
234     if (errlen >= 2) {
235         errbuf[errlen - 1] = '\0';
236         errbuf[errlen - 2] = '\0';
237     }
238     p = strchr(errbuf, '\0');
239     g_snprintf(p, (gulong)(sizeof errbuf - (p-errbuf)), " (%lu)", error);
240     return errbuf;
241 }
242 #endif
243
244 /* Initialize an argument list and add dumpcap to it. */
245 static const char **
246 init_pipe_args(int *argc) {
247     const char **argv;
248     const char *progfile_dir;
249     char *exename;
250
251     progfile_dir = get_progfile_dir();
252     if (progfile_dir == NULL) {
253       return NULL;
254     }
255
256     /* Allocate the string pointer array with enough space for the
257        terminating NULL pointer. */
258     *argc = 0;
259     argv = g_malloc(sizeof (char *));
260     *argv = NULL;
261
262     /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
263     exename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "dumpcap", progfile_dir);
264
265     /* Make that the first argument in the argument list (argv[0]). */
266     argv = sync_pipe_add_arg(argv, argc, exename);
267
268     return argv;
269 }
270
271 #define ARGV_NUMBER_LEN 24
272 /* a new capture run: start a new dumpcap task and hand over parameters through command line */
273 gboolean
274 sync_pipe_start(capture_options *capture_opts) {
275     char ssnap[ARGV_NUMBER_LEN];
276     char sdlt[ARGV_NUMBER_LEN];
277     char scount[ARGV_NUMBER_LEN];
278     char sfilesize[ARGV_NUMBER_LEN];
279     char sfile_duration[ARGV_NUMBER_LEN];
280     char sring_num_files[ARGV_NUMBER_LEN];
281     char sautostop_files[ARGV_NUMBER_LEN];
282     char sautostop_filesize[ARGV_NUMBER_LEN];
283     char sautostop_duration[ARGV_NUMBER_LEN];
284 #ifdef HAVE_PCAP_REMOTE
285     char sauth[256];
286 #endif
287 #ifdef HAVE_PCAP_SETSAMPLING
288     char ssampling[ARGV_NUMBER_LEN];
289 #endif
290 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
291     char buffer_size[ARGV_NUMBER_LEN];
292 #endif
293 #ifdef _WIN32
294     HANDLE sync_pipe_read;                  /* pipe used to send messages from child to parent */
295     HANDLE sync_pipe_write;                 /* pipe used to send messages from child to parent */
296     HANDLE signal_pipe;                     /* named pipe used to send messages from parent to child (currently only stop) */
297     GString *args = g_string_sized_new(200);
298     gchar *quoted_arg;
299     SECURITY_ATTRIBUTES sa;
300     STARTUPINFO si;
301     PROCESS_INFORMATION pi;
302     int i;
303     char control_id[ARGV_NUMBER_LEN];
304     gchar *signal_pipe_name;
305 #else
306     char errmsg[1024+1];
307     int sync_pipe[2];                       /* pipe used to send messages from child to parent */
308     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
309 #endif
310     int sync_pipe_read_fd;
311     int argc;
312     const char **argv;
313
314
315     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_start");
316     capture_opts_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, capture_opts);
317
318     capture_opts->fork_child = -1;
319
320     argv = init_pipe_args(&argc);
321     if (!argv) {
322         /* We don't know where to find dumpcap. */
323         report_failure("We don't know where to find dumpcap.");
324         return FALSE;
325     }
326
327     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[0]: %s", argv[0]);
328
329     argv = sync_pipe_add_arg(argv, &argc, "-i");
330     argv = sync_pipe_add_arg(argv, &argc, capture_opts->iface);
331
332     if (capture_opts->has_snaplen) {
333       argv = sync_pipe_add_arg(argv, &argc, "-s");
334       g_snprintf(ssnap, ARGV_NUMBER_LEN, "%d",capture_opts->snaplen);
335       argv = sync_pipe_add_arg(argv, &argc, ssnap);
336     }
337
338     if (capture_opts->linktype != -1) {
339       argv = sync_pipe_add_arg(argv, &argc, "-y");
340       g_snprintf(sdlt, ARGV_NUMBER_LEN, "%s",linktype_val_to_name(capture_opts->linktype));
341       argv = sync_pipe_add_arg(argv, &argc, sdlt);
342     }
343
344     if(capture_opts->multi_files_on) {
345       if (capture_opts->has_autostop_filesize) {
346         argv = sync_pipe_add_arg(argv, &argc, "-b");
347         g_snprintf(sfilesize, ARGV_NUMBER_LEN, "filesize:%d",capture_opts->autostop_filesize);
348         argv = sync_pipe_add_arg(argv, &argc, sfilesize);
349       }
350
351       if (capture_opts->has_file_duration) {
352         argv = sync_pipe_add_arg(argv, &argc, "-b");
353         g_snprintf(sfile_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->file_duration);
354         argv = sync_pipe_add_arg(argv, &argc, sfile_duration);
355       }
356
357       if (capture_opts->has_ring_num_files) {
358         argv = sync_pipe_add_arg(argv, &argc, "-b");
359         g_snprintf(sring_num_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->ring_num_files);
360         argv = sync_pipe_add_arg(argv, &argc, sring_num_files);
361       }
362
363       if (capture_opts->has_autostop_files) {
364         argv = sync_pipe_add_arg(argv, &argc, "-a");
365         g_snprintf(sautostop_files, ARGV_NUMBER_LEN, "files:%d",capture_opts->autostop_files);
366         argv = sync_pipe_add_arg(argv, &argc, sautostop_files);
367       }
368     } else {
369         if (capture_opts->has_autostop_filesize) {
370           argv = sync_pipe_add_arg(argv, &argc, "-a");
371           g_snprintf(sautostop_filesize, ARGV_NUMBER_LEN, "filesize:%d",capture_opts->autostop_filesize);
372           argv = sync_pipe_add_arg(argv, &argc, sautostop_filesize);
373         }
374     }
375
376     if (capture_opts->has_autostop_packets) {
377       argv = sync_pipe_add_arg(argv, &argc, "-c");
378       g_snprintf(scount, ARGV_NUMBER_LEN, "%d",capture_opts->autostop_packets);
379       argv = sync_pipe_add_arg(argv, &argc, scount);
380     }
381
382     if (capture_opts->has_autostop_duration) {
383       argv = sync_pipe_add_arg(argv, &argc, "-a");
384       g_snprintf(sautostop_duration, ARGV_NUMBER_LEN, "duration:%d",capture_opts->autostop_duration);
385       argv = sync_pipe_add_arg(argv, &argc, sautostop_duration);
386     }
387
388     if (!capture_opts->promisc_mode)
389       argv = sync_pipe_add_arg(argv, &argc, "-p");
390 #ifdef HAVE_PCAP_CREATE
391     if (capture_opts->monitor_mode)
392       argv = sync_pipe_add_arg(argv, &argc, "-I");
393 #endif
394     if (capture_opts->use_pcapng)
395       argv = sync_pipe_add_arg(argv, &argc, "-n");
396 #ifdef HAVE_PCAP_REMOTE
397     if (capture_opts->datatx_udp)
398       argv = sync_pipe_add_arg(argv, &argc, "-u");
399
400     if (!capture_opts->nocap_rpcap)
401       argv = sync_pipe_add_arg(argv, &argc, "-r");
402
403     if (capture_opts->auth_type == CAPTURE_AUTH_PWD)
404     {
405         argv = sync_pipe_add_arg(argv, &argc, "-A");
406         g_snprintf(sauth, sizeof(sauth), "%s:%s", capture_opts->auth_username,
407                    capture_opts->auth_password);
408         argv = sync_pipe_add_arg(argv, &argc, sauth);
409     }
410 #endif
411 #ifdef HAVE_PCAP_SETSAMPLING
412     if (capture_opts->sampling_method != CAPTURE_SAMP_NONE)
413     {
414         argv = sync_pipe_add_arg(argv, &argc, "-m");
415         g_snprintf(ssampling, ARGV_NUMBER_LEN, "%s:%d",
416              capture_opts->sampling_method == CAPTURE_SAMP_BY_COUNT ? "count" :
417              capture_opts->sampling_method == CAPTURE_SAMP_BY_TIMER ? "timer" :
418              "undef",
419              capture_opts->sampling_param);
420         argv = sync_pipe_add_arg(argv, &argc, ssampling);
421     }
422 #endif
423
424     /* dumpcap should be running in capture child mode (hidden feature) */
425 #ifndef DEBUG_CHILD
426     argv = sync_pipe_add_arg(argv, &argc, "-Z");
427 #ifdef _WIN32
428     g_snprintf(control_id, ARGV_NUMBER_LEN, "%d", GetCurrentProcessId());
429     argv = sync_pipe_add_arg(argv, &argc, control_id);
430 #else
431     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
432 #endif
433 #endif
434
435 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
436     argv = sync_pipe_add_arg(argv, &argc, "-B");
437 #ifdef HAVE_PCAP_REMOTE
438     if (capture_opts->src_type == CAPTURE_IFREMOTE)
439       /* No buffer size when using remote interfaces */
440       g_snprintf(buffer_size, ARGV_NUMBER_LEN, "%d", 1);
441     else
442 #endif
443     g_snprintf(buffer_size, ARGV_NUMBER_LEN, "%d",capture_opts->buffer_size);
444     argv = sync_pipe_add_arg(argv, &argc, buffer_size);
445 #endif
446
447     if (capture_opts->cfilter != NULL && strlen(capture_opts->cfilter) != 0) {
448       argv = sync_pipe_add_arg(argv, &argc, "-f");
449       argv = sync_pipe_add_arg(argv, &argc, capture_opts->cfilter);
450     }
451
452     if(capture_opts->save_file) {
453       argv = sync_pipe_add_arg(argv, &argc, "-w");
454       argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file);
455     }
456
457 #ifdef _WIN32
458     /* init SECURITY_ATTRIBUTES */
459     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
460     sa.bInheritHandle = TRUE;
461     sa.lpSecurityDescriptor = NULL;
462
463     /* Create a pipe for the child process */
464     /* (increase this value if you have trouble while fast capture file switches) */
465     if (! CreatePipe(&sync_pipe_read, &sync_pipe_write, &sa, 5120)) {
466       /* Couldn't create the pipe between parent and child. */
467       report_failure("Couldn't create sync pipe: %s",
468                      win32strerror(GetLastError()));
469       g_free( (gpointer) argv[0]);
470       g_free( (gpointer) argv);
471       return FALSE;
472     }
473
474     /* Create the signal pipe */
475     signal_pipe_name = g_strdup_printf(SIGNAL_PIPE_FORMAT, control_id);
476     signal_pipe = CreateNamedPipe(utf_8to16(signal_pipe_name),
477       PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE, 1, 65535, 65535, 0, NULL);
478     g_free(signal_pipe_name);
479
480     if (signal_pipe == INVALID_HANDLE_VALUE) {
481       /* Couldn't create the signal pipe between parent and child. */
482       report_failure("Couldn't create signal pipe: %s",
483                      win32strerror(GetLastError()));
484       g_free( (gpointer) argv[0]);
485       g_free( (gpointer) argv);
486       return FALSE;
487     }
488
489     /* init STARTUPINFO */
490     memset(&si, 0, sizeof(si));
491     si.cb           = sizeof(si);
492 #ifdef DEBUG_CHILD
493     si.dwFlags = STARTF_USESHOWWINDOW;
494     si.wShowWindow  = SW_SHOW;
495 #else
496     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
497     si.wShowWindow  = SW_HIDE;  /* this hides the console window */
498     si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
499     si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
500     si.hStdError = sync_pipe_write;
501     /*si.hStdError = (HANDLE) _get_osfhandle(2);*/
502 #endif
503
504     /* convert args array into a single string */
505     /* XXX - could change sync_pipe_add_arg() instead */
506     /* there is a drawback here: the length is internally limited to 1024 bytes */
507     for(i=0; argv[i] != 0; i++) {
508         if(i != 0) g_string_append_c(args, ' ');    /* don't prepend a space before the path!!! */
509         quoted_arg = protect_arg(argv[i]);
510         g_string_append(args, quoted_arg);
511         g_free(quoted_arg);
512     }
513
514     /* call dumpcap */
515     if(!CreateProcess(NULL, utf_8to16(args->str), NULL, NULL, TRUE,
516                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
517       report_failure("Couldn't run %s in child process: %s",
518                      args->str, win32strerror(GetLastError()));
519       CloseHandle(sync_pipe_read);
520       CloseHandle(sync_pipe_write);
521       g_free( (gpointer) argv[0]);
522       g_free( (gpointer) argv);
523       return FALSE;
524     }
525     capture_opts->fork_child = (int) pi.hProcess;
526     g_string_free(args, TRUE);
527
528     /* associate the operating system filehandle to a C run-time file handle */
529     /* (good file handle infos at: http://www.flounder.com/handles.htm) */
530     sync_pipe_read_fd = _open_osfhandle( (long) sync_pipe_read, _O_BINARY);
531
532     /* associate the operating system filehandle to a C run-time file handle */
533     capture_opts->signal_pipe_write_fd = _open_osfhandle( (long) signal_pipe, _O_BINARY);
534
535 #else /* _WIN32 */
536     if (pipe(sync_pipe) < 0) {
537       /* Couldn't create the pipe between parent and child. */
538       report_failure("Couldn't create sync pipe: %s", strerror(errno));
539       g_free( (gpointer) argv[0]);
540       g_free(argv);
541       return FALSE;
542     }
543
544     if ((capture_opts->fork_child = fork()) == 0) {
545       /*
546        * Child process - run dumpcap with the right arguments to make
547        * it just capture with the specified capture parameters
548        */
549       dup2(sync_pipe[PIPE_WRITE], 2);
550       ws_close(sync_pipe[PIPE_READ]);
551       execv(argv[0], (gpointer)argv);
552       g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
553                 argv[0], strerror(errno));
554       sync_pipe_errmsg_to_parent(2, errmsg, "");
555
556       /* Exit with "_exit()", so that we don't close the connection
557          to the X server (and cause stuff buffered up by our parent but
558          not yet sent to be sent, as that stuff should only be sent by
559          our parent).  We've sent an error message to the parent, so
560          we exit with an exit status of 1 (any exit status other than
561          0 or 1 will cause an additional message to report that exit
562          status, over and above the error message we sent to the parent). */
563       _exit(1);
564     }
565
566     sync_pipe_read_fd = sync_pipe[PIPE_READ];
567 #endif
568
569     g_free( (gpointer) argv[0]);  /* exename */
570
571     /* Parent process - read messages from the child process over the
572        sync pipe. */
573     g_free( (gpointer) argv);   /* free up arg array */
574
575     /* Close the write side of the pipe, so that only the child has it
576        open, and thus it completely closes, and thus returns to us
577        an EOF indication, if the child closes it (either deliberately
578        or by exiting abnormally). */
579 #ifdef _WIN32
580     CloseHandle(sync_pipe_write);
581 #else
582     ws_close(sync_pipe[PIPE_WRITE]);
583 #endif
584
585     if (capture_opts->fork_child == -1) {
586       /* We couldn't even create the child process. */
587       report_failure("Couldn't create child process: %s", strerror(errno));
588       ws_close(sync_pipe_read_fd);
589 #ifdef _WIN32
590       ws_close(capture_opts->signal_pipe_write_fd);
591 #endif
592       return FALSE;
593     }
594
595     /* we might wait for a moment till child is ready, so update screen now */
596     main_window_update();
597
598     /* We were able to set up to read the capture file;
599        arrange that our callback be called whenever it's possible
600        to read from the sync pipe, so that it's called when
601        the child process wants to tell us something. */
602
603     /* we have a running capture, now wait for the real capture filename */
604     pipe_input_set_handler(sync_pipe_read_fd, (gpointer) capture_opts,
605         &capture_opts->fork_child, sync_pipe_input_cb);
606
607     return TRUE;
608 }
609
610 /*
611  * Open two pipes to dumpcap with the supplied arguments, one for its
612  * standard output and one for its standard error.
613  *
614  * On success, *msg is unchanged and 0 is returned; data_read_fd,
615  * messsage_read_fd, and fork_child point to the standard output pipe's
616  * file descriptor, the standard error pipe's file descriptor, and
617  * the child's PID/handle, respectively.
618  *
619  * On failure, *msg points to an error message for the failure, and -1 is
620  * returned, in which case *msg must be freed with g_free().
621  */
622 /* XXX - This duplicates a lot of code in sync_pipe_start() */
623 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
624 #define PIPE_BUF_SIZE 5120
625 static int
626 sync_pipe_open_command(const char** argv, int *data_read_fd,
627                        int *message_read_fd, int *fork_child, gchar **msg)
628 {
629     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
630 #ifdef _WIN32
631     HANDLE sync_pipe[2];                    /* pipe used to send messages from child to parent */
632     HANDLE data_pipe[2];                    /* pipe used to send data from child to parent */
633     GString *args = g_string_sized_new(200);
634     gchar *quoted_arg;
635     SECURITY_ATTRIBUTES sa;
636     STARTUPINFO si;
637     PROCESS_INFORMATION pi;
638     int i;
639 #else
640     char errmsg[1024+1];
641     int sync_pipe[2];                       /* pipe used to send messages from child to parent */
642     int data_pipe[2];                       /* pipe used to send data from child to parent */
643 #endif
644
645     *fork_child = -1;
646     *data_read_fd = -1;
647     *message_read_fd = -1;
648     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_open_command");
649
650     if (!msg) {
651         /* We can't return anything */
652 #ifdef _WIN32
653         g_string_free(args, TRUE);
654 #endif
655         return -1;
656     }
657
658 #ifdef _WIN32
659     /* init SECURITY_ATTRIBUTES */
660     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
661     sa.bInheritHandle = TRUE;
662     sa.lpSecurityDescriptor = NULL;
663
664     /* Create a pipe for the child process to send us messages */
665     /* (increase this value if you have trouble while fast capture file switches) */
666     if (! CreatePipe(&sync_pipe[PIPE_READ], &sync_pipe[PIPE_WRITE], &sa, 5120)) {
667         /* Couldn't create the message pipe between parent and child. */
668         *msg = g_strdup_printf("Couldn't create sync pipe: %s",
669                                win32strerror(GetLastError()));
670         g_free( (gpointer) argv[0]);
671         g_free( (gpointer) argv);
672         return -1;
673     }
674
675     /* Create a pipe for the child process to send us data */
676     /* (increase this value if you have trouble while fast capture file switches) */
677     if (! CreatePipe(&data_pipe[PIPE_READ], &data_pipe[PIPE_WRITE], &sa, 5120)) {
678         /* Couldn't create the message pipe between parent and child. */
679         *msg = g_strdup_printf("Couldn't create data pipe: %s",
680                                win32strerror(GetLastError()));
681         CloseHandle(sync_pipe[PIPE_READ]);
682         CloseHandle(sync_pipe[PIPE_WRITE]);
683         g_free( (gpointer) argv[0]);
684         g_free( (gpointer) argv);
685         return -1;
686     }
687
688     /* init STARTUPINFO */
689     memset(&si, 0, sizeof(si));
690     si.cb           = sizeof(si);
691 #ifdef DEBUG_CHILD
692     si.dwFlags = STARTF_USESHOWWINDOW;
693     si.wShowWindow  = SW_SHOW;
694 #else
695     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
696     si.wShowWindow  = SW_HIDE;  /* this hides the console window */
697     si.hStdInput = NULL;
698     si.hStdOutput = data_pipe[PIPE_WRITE];
699     si.hStdError = sync_pipe[PIPE_WRITE];
700 #endif
701
702     /* convert args array into a single string */
703     /* XXX - could change sync_pipe_add_arg() instead */
704     /* there is a drawback here: the length is internally limited to 1024 bytes */
705     for(i=0; argv[i] != 0; i++) {
706         if(i != 0) g_string_append_c(args, ' ');    /* don't prepend a space before the path!!! */
707         quoted_arg = protect_arg(argv[i]);
708         g_string_append(args, quoted_arg);
709         g_free(quoted_arg);
710     }
711
712     /* call dumpcap */
713     if(!CreateProcess(NULL, utf_8to16(args->str), NULL, NULL, TRUE,
714                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
715         *msg = g_strdup_printf("Couldn't run %s in child process: %s",
716                                args->str, win32strerror(GetLastError()));
717         CloseHandle(data_pipe[PIPE_READ]);
718         CloseHandle(data_pipe[PIPE_WRITE]);
719         CloseHandle(sync_pipe[PIPE_READ]);
720         CloseHandle(sync_pipe[PIPE_WRITE]);
721         g_free( (gpointer) argv[0]);
722         g_free( (gpointer) argv);
723         return -1;
724     }
725     *fork_child = (int) pi.hProcess;
726     g_string_free(args, TRUE);
727
728     /* associate the operating system filehandles to C run-time file handles */
729     /* (good file handle infos at: http://www.flounder.com/handles.htm) */
730     *data_read_fd = _open_osfhandle( (long) data_pipe[PIPE_READ], _O_BINARY);
731     *message_read_fd = _open_osfhandle( (long) sync_pipe[PIPE_READ], _O_BINARY);
732 #else /* _WIN32 */
733     /* Create a pipe for the child process to send us messages */
734     if (pipe(sync_pipe) < 0) {
735         /* Couldn't create the message pipe between parent and child. */
736         *msg = g_strdup_printf("Couldn't create sync pipe: %s", strerror(errno));
737         g_free( (gpointer) argv[0]);
738         g_free(argv);
739         return -1;
740     }
741
742     /* Create a pipe for the child process to send us data */
743     if (pipe(data_pipe) < 0) {
744         /* Couldn't create the data pipe between parent and child. */
745         *msg = g_strdup_printf("Couldn't create data pipe: %s", strerror(errno));
746         ws_close(sync_pipe[PIPE_READ]);
747         ws_close(sync_pipe[PIPE_WRITE]);
748         g_free( (gpointer) argv[0]);
749         g_free(argv);
750         return -1;
751     }
752
753     if ((*fork_child = fork()) == 0) {
754         /*
755          * Child process - run dumpcap with the right arguments to make
756          * it just capture with the specified capture parameters
757          */
758         dup2(data_pipe[PIPE_WRITE], 1);
759         ws_close(data_pipe[PIPE_READ]);
760         ws_close(data_pipe[PIPE_WRITE]);
761         dup2(sync_pipe[PIPE_WRITE], 2);
762         ws_close(sync_pipe[PIPE_READ]);
763         ws_close(sync_pipe[PIPE_WRITE]);
764         execv(argv[0], (gpointer)argv);
765         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
766                    argv[0], strerror(errno));
767         sync_pipe_errmsg_to_parent(2, errmsg, "");
768
769         /* Exit with "_exit()", so that we don't close the connection
770            to the X server (and cause stuff buffered up by our parent but
771            not yet sent to be sent, as that stuff should only be sent by
772            our parent).  We've sent an error message to the parent, so
773            we exit with an exit status of 1 (any exit status other than
774            0 or 1 will cause an additional message to report that exit
775            status, over and above the error message we sent to the parent). */
776         _exit(1);
777     }
778
779     *data_read_fd = data_pipe[PIPE_READ];
780     *message_read_fd = sync_pipe[PIPE_READ];
781 #endif
782
783     g_free( (gpointer) argv[0]);  /* exename */
784
785     /* Parent process - read messages from the child process over the
786        sync pipe. */
787     g_free( (gpointer) argv);   /* free up arg array */
788
789     /* Close the write sides of the pipes, so that only the child has them
790        open, and thus they completely close, and thus return to us
791        an EOF indication, if the child closes them (either deliberately
792        or by exiting abnormally). */
793 #ifdef _WIN32
794     CloseHandle(data_pipe[PIPE_WRITE]);
795     CloseHandle(sync_pipe[PIPE_WRITE]);
796 #else
797     ws_close(data_pipe[PIPE_WRITE]);
798     ws_close(sync_pipe[PIPE_WRITE]);
799 #endif
800
801     if (*fork_child == -1) {
802         /* We couldn't even create the child process. */
803         *msg = g_strdup_printf("Couldn't create child process: %s", strerror(errno));
804         ws_close(*data_read_fd);
805         ws_close(*message_read_fd);
806         return -1;
807     }
808
809     /* we might wait for a moment till child is ready, so update screen now */
810     main_window_update();
811     return 0;
812 }
813
814 /*
815  * Wait for dumpcap to finish.  On success, *msg is unchanged, and 0 is
816  * returned.  On failure, *msg points to an error message for the
817  * failure, and -1 is returned.  In the latter case, *msg must be
818  * freed with g_free().
819  */
820 static int
821 sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
822                         int *fork_child, gchar **msg)
823 {
824     ws_close(*data_read_fd);
825     if (message_read_fd != NULL)
826         ws_close(*message_read_fd);
827
828 #ifdef _WIN32
829     /* XXX - Should we signal the child somehow? */
830     sync_pipe_kill(*fork_child);
831 #endif
832
833     return sync_pipe_wait_for_child(*fork_child, msg);
834 }
835
836 /*
837  * Run dumpcap with the supplied arguments.
838  *
839  * On success, *data points to a buffer containing the dumpcap output,
840  * *primary_msg and *secondary_message are NULL, and 0 is returned; *data
841  * must be freed with g_free().
842  *
843  * On failure, *data is NULL, *primary_msg points to an error message,
844  * *secondary_msg either points to an additional error message or is
845  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
846  * must be freed with g_free().
847  */
848 /* XXX - This duplicates a lot of code in sync_pipe_start() */
849 /* XXX - assumes PIPE_BUF_SIZE > SP_MAX_MSG_LEN */
850 #define PIPE_BUF_SIZE 5120
851 static int
852 sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
853                       gchar **secondary_msg)
854 {
855   gchar *msg;
856   int data_pipe_read_fd, sync_pipe_read_fd, fork_child, ret;
857   gchar buffer[PIPE_BUF_SIZE+1];
858   int  nread;
859   char indicator;
860   int  primary_msg_len;
861   char *primary_msg_text;
862   int  secondary_msg_len;
863   char *secondary_msg_text;
864   GString *data_buf = NULL;
865   int count;
866
867   ret = sync_pipe_open_command(argv, &data_pipe_read_fd, &sync_pipe_read_fd,
868                                &fork_child, &msg);
869   if (ret == -1) {
870     *primary_msg = msg;
871     *secondary_msg = NULL;
872     *data = NULL;
873     return -1;
874   }
875
876   /*
877    * We were able to set up to read dumpcap's output.  Do so.
878    *
879    * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
880    */
881   nread = pipe_read_block(sync_pipe_read_fd, &indicator, SP_MAX_MSG_LEN,
882                           buffer);
883   if(nread <= 0) {
884     /* We got a read error from the sync pipe, or we got no data at
885        all from the sync pipe, so we're not going to be getting any
886        data or error message from the child process.  Pick up its
887        exit status, and complain.
888
889        We don't have to worry about killing the child, if the sync pipe
890        returned an error. Usually this error is caused as the child killed
891        itself while going down. Even in the rare cases that this isn't the
892        case, the child will get an error when writing to the broken pipe
893        the next time, cleaning itself up then. */
894     ret = sync_pipe_wait_for_child(fork_child, primary_msg);
895     if (ret == 0) {
896       /* No unusual exit status; just report the read problem. */
897       if (nread == 0)
898         *primary_msg = g_strdup("Child dumpcap closed sync pipe prematurely");
899       else {
900         /* Don't report EINTR - that might just be from a ^C. */
901         if (errno == EINTR)
902           *primary_msg = NULL;
903         else
904           *primary_msg = g_strdup_printf("Error reading from sync pipe: %s",
905                                          strerror(errno));
906       }
907     }
908     *secondary_msg = NULL;
909
910     return -1;
911   }
912
913   /* we got a valid message block from the child, process it */
914   switch(indicator) {
915
916   case SP_ERROR_MSG:
917     /*
918      * Error from dumpcap; there will be a primary message and a
919      * secondary message.
920      */
921
922     /* convert primary message */
923     pipe_convert_header(buffer, 4, &indicator, &primary_msg_len);
924     primary_msg_text = buffer+4;
925     /* convert secondary message */
926     pipe_convert_header(primary_msg_text + primary_msg_len, 4, &indicator,
927                         &secondary_msg_len);
928     secondary_msg_text = primary_msg_text + primary_msg_len + 4;
929     /* the capture child will close the sync_pipe, nothing to do */
930
931     /*
932      * Pick up the child status.
933      */
934     ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
935                                   &fork_child, &msg);
936     if (ret == -1) {
937       /*
938        * Child process failed unexpectedly, or wait failed; msg is the
939        * error message.
940        */
941       *primary_msg = msg;
942       *secondary_msg = NULL;
943     } else {
944       /*
945        * Child process failed, but returned the expected exit status.
946        * Return the messages it gave us, and indicate failure.
947        */
948       *primary_msg = g_strdup(primary_msg_text);
949       *secondary_msg = g_strdup(secondary_msg_text);
950       ret = -1;
951     }
952     *data = NULL;
953     break;
954
955   case SP_SUCCESS:
956     /* read the output from the command */
957     data_buf = g_string_new("");
958     while ((count = ws_read(data_pipe_read_fd, buffer, PIPE_BUF_SIZE)) > 0) {
959       buffer[count] = '\0';
960       g_string_append(data_buf, buffer);
961     }
962
963     /*
964      * Pick up the child status.
965      */
966     ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
967                                   &fork_child, &msg);
968     if (ret == -1) {
969       /*
970        * Child process failed unexpectedly, or wait failed; msg is the
971        * error message.
972        */
973       *primary_msg = msg;
974       *secondary_msg = NULL;
975       g_string_free(data_buf, TRUE);
976       *data = NULL;
977     } else {
978       /*
979        * Child process succeeded.
980        */
981       *primary_msg = NULL;
982       *secondary_msg = NULL;
983       *data = data_buf->str;
984       g_string_free(data_buf, FALSE);
985     }
986     break;
987
988   default:
989     /*
990      * Pick up the child status.
991      */
992     ret = sync_pipe_close_command(&data_pipe_read_fd, &sync_pipe_read_fd,
993                                   &fork_child, &msg);
994     if (ret == -1) {
995       /*
996        * Child process failed unexpectedly, or wait failed; msg is the
997        * error message.
998        */
999       *primary_msg = msg;
1000       *secondary_msg = NULL;
1001     } else {
1002       /*
1003        * Child process returned an unknown status.
1004        */
1005       *primary_msg = g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1006                                      indicator);
1007       *secondary_msg = NULL;
1008       ret = -1;
1009     }
1010     *data = NULL;
1011     break;
1012   }
1013   return ret;
1014 }
1015
1016 /*
1017  * Get the list of interfaces using dumpcap.
1018  *
1019  * On success, *data points to a buffer containing the dumpcap output,
1020  * *primary_msg and *secondary_msg are NULL, and 0 is returned.  *data
1021  * must be freed with g_free().
1022  *
1023  * On failure, *data is NULL, *primary_msg points to an error message,
1024  * *secondary_msg either points to an additional error message or is
1025  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1026  * must be freed with g_free().
1027  */
1028 int
1029 sync_interface_list_open(gchar **data, gchar **primary_msg,
1030                          gchar **secondary_msg)
1031 {
1032     int argc;
1033     const char **argv;
1034
1035     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
1036
1037     argv = init_pipe_args(&argc);
1038
1039     if (!argv) {
1040         *primary_msg = g_strdup("We don't know where to find dumpcap.");
1041         *secondary_msg = NULL;
1042         *data = NULL;
1043         return -1;
1044     }
1045
1046     /* Ask for the interface list */
1047     argv = sync_pipe_add_arg(argv, &argc, "-D");
1048
1049 #ifndef DEBUG_CHILD
1050     /* Run dumpcap in capture child mode */
1051     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1052     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1053 #endif
1054     return sync_pipe_run_command(argv, data, primary_msg, secondary_msg);
1055 }
1056
1057 /*
1058  * Get the capabilities of an interface using dumpcap.
1059  *
1060  * On success, *data points to a buffer containing the dumpcap output,
1061  * *primary_msg and *secondary_msg are NULL, and 0 is returned.  *data
1062  * must be freed with g_free().
1063  *
1064  * On failure, *data is NULL, *primary_msg points to an error message,
1065  * *secondary_msg either points to an additional error message or is
1066  * NULL, and -1 is returned; *primary_msg, and *secondary_msg if not NULL,
1067  * must be freed with g_free().
1068  */
1069 int
1070 sync_if_capabilities_open(const gchar *ifname, gboolean monitor_mode,
1071                           gchar **data, gchar **primary_msg,
1072                           gchar **secondary_msg)
1073 {
1074     int argc;
1075     const char **argv;
1076
1077     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_linktype_list_open");
1078
1079     argv = init_pipe_args(&argc);
1080
1081     if (!argv) {
1082         *primary_msg = g_strdup("We don't know where to find dumpcap.");
1083         *secondary_msg = NULL;
1084         *data = NULL;
1085         return -1;
1086     }
1087
1088     /* Ask for the interface capabilities */
1089     argv = sync_pipe_add_arg(argv, &argc, "-i");
1090     argv = sync_pipe_add_arg(argv, &argc, ifname);
1091     argv = sync_pipe_add_arg(argv, &argc, "-L");
1092     if (monitor_mode)
1093         argv = sync_pipe_add_arg(argv, &argc, "-I");
1094
1095 #ifndef DEBUG_CHILD
1096     /* Run dumpcap in capture child mode */
1097     argv = sync_pipe_add_arg(argv, &argc, "-Z");
1098     argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1099 #endif
1100     return sync_pipe_run_command(argv, data, primary_msg, secondary_msg);
1101 }
1102
1103 /*
1104  * Start getting interface statistics using dumpcap.  On success, read_fd
1105  * contains the file descriptor for the pipe's stdout, *msg is unchanged,
1106  * and zero is returned.  On failure, *msg will point to an error message
1107  * that must be g_free()d, and -1 will be returned.
1108  */
1109 int
1110 sync_interface_stats_open(int *data_read_fd, int *fork_child, gchar **msg)
1111 {
1112   int argc;
1113   const char **argv;
1114   int message_read_fd, ret;
1115   gchar buffer[PIPE_BUF_SIZE+1];
1116   int  nread;
1117   char indicator;
1118   int  primary_msg_len;
1119   char *primary_msg_text;
1120   int  secondary_msg_len;
1121   char *secondary_msg_text;
1122
1123   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_stats_open");
1124
1125   argv = init_pipe_args(&argc);
1126
1127   if (!argv) {
1128     *msg = g_strdup("We don't know where to find dumpcap.");
1129     return -1;
1130   }
1131
1132   /* Ask for the interface statistics */
1133   argv = sync_pipe_add_arg(argv, &argc, "-S");
1134
1135 #ifndef DEBUG_CHILD
1136   argv = sync_pipe_add_arg(argv, &argc, "-Z");
1137   argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
1138 #endif
1139   ret = sync_pipe_open_command(argv, data_read_fd, &message_read_fd,
1140                                  fork_child, msg);
1141   if (ret == -1)
1142     return -1;
1143
1144   /*
1145    * We were able to set up to read dumpcap's output.  Do so.
1146    *
1147    * First, wait for an SP_ERROR_MSG message or SP_SUCCESS message.
1148    */
1149   nread = pipe_read_block(message_read_fd, &indicator, SP_MAX_MSG_LEN,
1150                           buffer);
1151   if(nread <= 0) {
1152     /* We got a read error from the sync pipe, or we got no data at
1153        all from the sync pipe, so we're not going to be getting any
1154        data or error message from the child process.  Pick up its
1155        exit status, and complain.
1156
1157        We don't have to worry about killing the child, if the sync pipe
1158        returned an error. Usually this error is caused as the child killed
1159        itself while going down. Even in the rare cases that this isn't the
1160        case, the child will get an error when writing to the broken pipe
1161        the next time, cleaning itself up then. */
1162     ret = sync_pipe_wait_for_child(*fork_child, msg);
1163     if (ret == 0) {
1164       /* No unusual exit status; just report the read problem. */
1165       if (nread == 0)
1166         *msg = g_strdup("Child dumpcap closed sync pipe prematurely");
1167       else {
1168         /* Don't report EINTR - that might just be from a ^C. */
1169         if (errno == EINTR)
1170           *msg = NULL;
1171         else
1172           *msg = g_strdup_printf("Error reading from sync pipe: %s",
1173                                  strerror(errno));
1174       }
1175     }
1176
1177     return -1;
1178   }
1179
1180   /* we got a valid message block from the child, process it */
1181   switch(indicator) {
1182
1183   case SP_ERROR_MSG:
1184     /*
1185      * Error from dumpcap; there will be a primary message and a
1186      * secondary message.
1187      */
1188
1189     /* convert primary message */
1190     pipe_convert_header(buffer, 4, &indicator, &primary_msg_len);
1191     primary_msg_text = buffer+4;
1192     /* convert secondary message */
1193     pipe_convert_header(primary_msg_text + primary_msg_len, 4, &indicator,
1194                         &secondary_msg_len);
1195     secondary_msg_text = primary_msg_text + primary_msg_len + 4;
1196     /* the capture child will close the sync_pipe, nothing to do */
1197
1198     /*
1199      * Pick up the child status.
1200      */
1201     ret = sync_pipe_close_command(data_read_fd, &message_read_fd,
1202                                   fork_child, msg);
1203     if (ret == -1) {
1204       /*
1205        * Child process failed unexpectedly, or wait failed; msg is the
1206        * error message.
1207        */
1208     } else {
1209       /*
1210        * Child process failed, but returned the expected exit status.
1211        * Return the messages it gave us, and indicate failure.
1212        */
1213       *msg = g_strdup(primary_msg_text);
1214       ret = -1;
1215     }
1216     break;
1217
1218   case SP_SUCCESS:
1219     /* Close the message pipe. */
1220     ws_close(message_read_fd);
1221     break;
1222
1223   default:
1224     /*
1225      * Pick up the child status.
1226      */
1227     ret = sync_pipe_close_command(data_read_fd, &message_read_fd,
1228                                   fork_child, msg);
1229     if (ret == -1) {
1230       /*
1231        * Child process failed unexpectedly, or wait failed; msg is the
1232        * error message.
1233        */
1234     } else {
1235       /*
1236        * Child process returned an unknown status.
1237        */
1238       *msg = g_strdup_printf("dumpcap process gave an unexpected message type: 0x%02x",
1239                              indicator);
1240       ret = -1;
1241     }
1242     break;
1243   }
1244   return ret;
1245 }
1246
1247 /* Close down the stats process */
1248 int
1249 sync_interface_stats_close(int *read_fd, int *fork_child, gchar **msg)
1250 {
1251     return sync_pipe_close_command(read_fd, NULL, fork_child, msg);
1252 }
1253
1254 /* read a number of bytes from a pipe */
1255 /* (blocks until enough bytes read or an error occurs) */
1256 static int
1257 pipe_read_bytes(int pipe_fd, char *bytes, int required)
1258 {
1259     int newly;
1260     int offset = 0;
1261
1262     while(required) {
1263         newly = read(pipe_fd, &bytes[offset], required);
1264         if (newly == 0) {
1265             /* EOF */
1266             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1267                   "read from pipe %d: EOF (capture closed?)", pipe_fd);
1268             return offset;
1269         }
1270         if (newly < 0) {
1271             /* error */
1272             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1273                   "read from pipe %d: error(%u): %s", pipe_fd, errno, strerror(errno));
1274             return newly;
1275         }
1276
1277         required -= newly;
1278         offset += newly;
1279     }
1280
1281     return offset;
1282 }
1283
1284 static gboolean pipe_data_available(int pipe_fd) {
1285 #ifdef _WIN32 /* PeekNamedPipe */
1286     HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
1287     DWORD bytes_avail;
1288
1289     if (hPipe == INVALID_HANDLE_VALUE)
1290         return FALSE;
1291
1292     if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
1293         return FALSE;
1294
1295     if (bytes_avail > 0)
1296         return TRUE;
1297     return FALSE;
1298 #else /* select */
1299     fd_set rfds;
1300     struct timeval timeout;
1301
1302     FD_ZERO(&rfds);
1303     FD_SET(pipe_fd, &rfds);
1304     timeout.tv_sec = 0;
1305     timeout.tv_usec = 0;
1306
1307     if (select(pipe_fd+1, &rfds, NULL, NULL, &timeout) > 0)
1308         return TRUE;
1309
1310     return FALSE;
1311 #endif
1312 }
1313
1314 /* Read a line from a pipe, similar to fgets */
1315 int
1316 sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) {
1317     int newly;
1318     int offset = -1;
1319
1320     while(offset < max - 1) {
1321         offset++;
1322         if (! pipe_data_available(pipe_fd))
1323             break;
1324         newly = read(pipe_fd, &bytes[offset], 1);
1325         if (newly == 0) {
1326             /* EOF - not necessarily an error */
1327             break;
1328         } else if (newly < 0) {
1329             /* error */
1330             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1331                   "read from pipe %d: error(%u): %s", pipe_fd, errno, strerror(errno));
1332             return newly;
1333         } else if (bytes[offset] == '\n') {
1334             break;
1335         }
1336     }
1337
1338     if (offset >= 0)
1339         bytes[offset] = '\0';
1340
1341     return offset;
1342 }
1343
1344
1345 /* convert header values (indicator and 3-byte length) */
1346 static void
1347 pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len) {
1348
1349     g_assert(header_len == 4);
1350
1351     /* convert header values */
1352     *indicator = header[0];
1353     *block_len = header[1]<<16 | header[2]<<8 | header[3];
1354 }
1355
1356 /* read a message from the sending pipe in the standard format
1357    (1-byte message indicator, 3-byte message length (excluding length
1358    and indicator field), and the rest is the message) */
1359 static int
1360 pipe_read_block(int pipe_fd, char *indicator, int len, char *msg)
1361 {
1362     int required;
1363     int newly;
1364     guchar header[4];
1365
1366     /* read header (indicator and 3-byte length) */
1367     newly = pipe_read_bytes(pipe_fd, header, 4);
1368     if(newly != 4) {
1369         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1370               "read %d failed to read header: %u", pipe_fd, newly);
1371         return -1;
1372     }
1373
1374     /* convert header values */
1375     pipe_convert_header(header, 4, indicator, &required);
1376
1377     /* only indicator with no value? */
1378     if(required == 0) {
1379         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1380               "read %d indicator: %c empty value", pipe_fd, *indicator);
1381         return 4;
1382     }
1383
1384     /* does the data fit into the given buffer? */
1385     if(required > len) {
1386         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1387               "read %d length error, required %d > len %d, indicator: %u",
1388               pipe_fd, required, len, *indicator);
1389
1390         /* we have a problem here, try to read some more bytes from the pipe to debug where the problem really is */
1391         memcpy(msg, header, sizeof(header));
1392         newly = read(pipe_fd, &msg[sizeof(header)], len-sizeof(header));
1393         g_warning("Unknown message from dumpcap, try to show it as a string: %s", msg);
1394         return -1;
1395     }
1396     len = required;
1397
1398     /* read the actual block data */
1399     newly = pipe_read_bytes(pipe_fd, msg, required);
1400     if(newly != required) {
1401         g_warning("Unknown message from dumpcap, try to show it as a string: %s", msg);
1402         return -1;
1403     }
1404
1405     /* XXX If message is "2part", the msg probably won't be sent to debug log correctly */
1406     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1407           "read %d ok indicator: %c len: %u msg: %s", pipe_fd, *indicator,
1408           len, msg);
1409     return newly + 4;
1410 }
1411
1412
1413 /* There's stuff to read from the sync pipe, meaning the child has sent
1414    us a message, or the sync pipe has closed, meaning the child has
1415    closed it (perhaps because it exited). */
1416 static gboolean
1417 sync_pipe_input_cb(gint source, gpointer user_data)
1418 {
1419   capture_options *capture_opts = (capture_options *)user_data;
1420   int  ret;
1421   char buffer[SP_MAX_MSG_LEN+1];
1422   int  nread;
1423   char indicator;
1424   int  primary_len;
1425   char * primary_msg;
1426   int  secondary_len;
1427   char * secondary_msg;
1428
1429   nread = pipe_read_block(source, &indicator, SP_MAX_MSG_LEN, buffer);
1430   if(nread <= 0) {
1431     /* We got a read error from the sync pipe, or we got no data at
1432        all from the sync pipe, so we're not going to be getting any
1433        data or error message from the child process.  Pick up its
1434        exit status, and complain.
1435
1436        We don't have to worry about killing the child, if the sync pipe
1437        returned an error. Usually this error is caused as the child killed itself
1438        while going down. Even in the rare cases that this isn't the case,
1439        the child will get an error when writing to the broken pipe the next time,
1440        cleaning itself up then. */
1441     ret = sync_pipe_wait_for_child(capture_opts->fork_child, &primary_msg);
1442     if (ret == 0) {
1443       /* No unusual exit status; just report the read problem. */
1444       if (nread == 0)
1445         primary_msg = g_strdup("Child dumpcap closed sync pipe prematurely");
1446       else {
1447         /* Don't report EINTR - that might just be from a ^C. */
1448         if (errno == EINTR)
1449           primary_msg = NULL;
1450         else
1451           primary_msg = g_strdup_printf("Error reading from sync pipe: %s",
1452                                         strerror(errno));
1453       }
1454     }
1455
1456     /* No more child process. */
1457     capture_opts->fork_child = -1;
1458
1459 #ifdef _WIN32
1460     ws_close(capture_opts->signal_pipe_write_fd);
1461 #endif
1462     capture_input_closed(capture_opts, primary_msg);
1463     g_free(primary_msg);
1464     return FALSE;
1465   }
1466
1467   /* we got a valid message block from the child, process it */
1468   switch(indicator) {
1469   case SP_FILE:
1470     if(!capture_input_new_file(capture_opts, buffer)) {
1471       g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: file failed, closing capture");
1472
1473       /* We weren't able to open the new capture file; user has been
1474          alerted. Close the sync pipe. */
1475       ws_close(source);
1476
1477       /* the child has send us a filename which we couldn't open.
1478          this probably means, the child is creating files faster than we can handle it.
1479          this should only be the case for very fast file switches
1480          we can't do much more than telling the child to stop
1481          (this is the "emergency brake" if user e.g. wants to switch files every second) */
1482       sync_pipe_stop(capture_opts);
1483     }
1484     break;
1485   case SP_PACKET_COUNT:
1486     nread = atoi(buffer);
1487     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", nread);
1488     capture_input_new_packets(capture_opts, nread);
1489     break;
1490   case SP_ERROR_MSG:
1491     /* convert primary message */
1492     pipe_convert_header(buffer, 4, &indicator, &primary_len);
1493     primary_msg = buffer+4;
1494     /* convert secondary message */
1495     pipe_convert_header(primary_msg + primary_len, 4, &indicator, &secondary_len);
1496     secondary_msg = primary_msg + primary_len + 4;
1497     /* message output */
1498     capture_input_error_message(capture_opts, primary_msg, secondary_msg);
1499     /* the capture child will close the sync_pipe, nothing to do for now */
1500     /* (an error message doesn't mean we have to stop capturing) */
1501     break;
1502   case SP_BAD_FILTER:
1503     capture_input_cfilter_error_message(capture_opts, buffer);
1504     /* the capture child will close the sync_pipe, nothing to do for now */
1505     break;
1506   case SP_DROPS:
1507     capture_input_drops(capture_opts, (guint32)strtoul(buffer, NULL, 10));
1508     break;
1509   default:
1510     g_assert_not_reached();
1511   }
1512
1513   return TRUE;
1514 }
1515
1516
1517
1518 /* the child process is going down, wait until it's completely terminated */
1519 static int
1520 sync_pipe_wait_for_child(int fork_child, gchar **msgp)
1521 {
1522   int fork_child_status;
1523   int ret;
1524
1525   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: wait till child closed");
1526   g_assert(fork_child != -1);
1527
1528   *msgp = NULL; /* assume no error */
1529   ret = 0;
1530 #ifdef _WIN32
1531   if (_cwait(&fork_child_status, fork_child, _WAIT_CHILD) == -1) {
1532     *msgp = g_strdup_printf("Error from cwait(): %s", strerror(errno));
1533     ret = -1;
1534   }
1535 #else
1536   if (waitpid(fork_child, &fork_child_status, 0) != -1) {
1537     if (WIFEXITED(fork_child_status)) {
1538       /*
1539        * The child exited; return its exit status, if it seems uncommon
1540        * (0=ok, 1=command syntax error, 2=other error).
1541        *
1542        * For an exit status of 0, there's no error to tell the user about.
1543        * For an exit status of 1 or 2, the child will inform us about errors
1544        * through the sync_pipe, so don't return an error.
1545        * If there are situations where the child won't send us such an error
1546        * message, this should be fixed in the child and not worked around
1547        * here!
1548        */
1549       if (WEXITSTATUS(fork_child_status) != 0 &&
1550           WEXITSTATUS(fork_child_status) != 1 &&
1551           WEXITSTATUS(fork_child_status) != 2) {
1552         *msgp = g_strdup_printf("Child dumpcap process exited: exit status %d",
1553                                 WEXITSTATUS(fork_child_status));
1554         ret = -1;
1555       }
1556     } else if (WIFSTOPPED(fork_child_status)) {
1557       /* It stopped, rather than exiting.  "Should not happen." */
1558       *msgp = g_strdup_printf("Child dumpcap process stopped: %s",
1559                               sync_pipe_signame(WSTOPSIG(fork_child_status)));
1560       ret = -1;
1561     } else if (WIFSIGNALED(fork_child_status)) {
1562       /* It died with a signal. */
1563       *msgp = g_strdup_printf("Child dumpcap process died: %s%s",
1564                               sync_pipe_signame(WTERMSIG(fork_child_status)),
1565                               WCOREDUMP(fork_child_status) ? " - core dumped" : "");
1566       ret = -1;
1567     } else {
1568       /* What?  It had to either have exited, or stopped, or died with
1569          a signal; what happened here? */
1570       *msgp = g_strdup_printf("Bad status from waitpid(): %#o",
1571                               fork_child_status);
1572       ret = -1;
1573     }
1574   } else {
1575     *msgp = g_strdup_printf("Error from waitpid(): %s", strerror(errno));
1576     ret = -1;
1577   }
1578 #endif
1579
1580   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed");
1581   return ret;
1582 }
1583
1584
1585 #ifndef _WIN32
1586 /* convert signal to corresponding name */
1587 static const char *
1588 sync_pipe_signame(int sig)
1589 {
1590   const char *sigmsg;
1591   static char sigmsg_buf[6+1+3+1];
1592
1593   switch (sig) {
1594
1595   case SIGHUP:
1596     sigmsg = "Hangup";
1597     break;
1598
1599   case SIGINT:
1600     sigmsg = "Interrupted";
1601     break;
1602
1603   case SIGQUIT:
1604     sigmsg = "Quit";
1605     break;
1606
1607   case SIGILL:
1608     sigmsg = "Illegal instruction";
1609     break;
1610
1611   case SIGTRAP:
1612     sigmsg = "Trace trap";
1613     break;
1614
1615   case SIGABRT:
1616     sigmsg = "Abort";
1617     break;
1618
1619   case SIGFPE:
1620     sigmsg = "Arithmetic exception";
1621     break;
1622
1623   case SIGKILL:
1624     sigmsg = "Killed";
1625     break;
1626
1627   case SIGBUS:
1628     sigmsg = "Bus error";
1629     break;
1630
1631   case SIGSEGV:
1632     sigmsg = "Segmentation violation";
1633     break;
1634
1635   /* http://metalab.unc.edu/pub/Linux/docs/HOWTO/GCC-HOWTO
1636      Linux is POSIX compliant.  These are not POSIX-defined signals ---
1637      ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
1638
1639         ``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS
1640         were omitted from POSIX.1 because their behavior is
1641         implementation dependent and could not be adequately catego-
1642         rized.  Conforming implementations may deliver these sig-
1643         nals, but must document the circumstances under which they
1644         are delivered and note any restrictions concerning their
1645         delivery.''
1646
1647      So we only check for SIGSYS on those systems that happen to
1648      implement them (a system can be POSIX-compliant and implement
1649      them, it's just that POSIX doesn't *require* a POSIX-compliant
1650      system to implement them).
1651    */
1652
1653 #ifdef SIGSYS
1654   case SIGSYS:
1655     sigmsg = "Bad system call";
1656     break;
1657 #endif
1658
1659   case SIGPIPE:
1660     sigmsg = "Broken pipe";
1661     break;
1662
1663   case SIGALRM:
1664     sigmsg = "Alarm clock";
1665     break;
1666
1667   case SIGTERM:
1668     sigmsg = "Terminated";
1669     break;
1670
1671   default:
1672     /* Returning a static buffer is ok in the context we use it here */
1673     g_snprintf(sigmsg_buf, sizeof sigmsg_buf, "Signal %d", sig);
1674     sigmsg = sigmsg_buf;
1675     break;
1676   }
1677   return sigmsg;
1678 }
1679 #endif
1680
1681
1682 #ifdef _WIN32
1683 /* tell the child through the signal pipe that we want to quit the capture */
1684 static void
1685 signal_pipe_capquit_to_child(capture_options *capture_opts)
1686 {
1687     const char quit_msg[] = "QUIT";
1688     int ret;
1689
1690
1691     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "signal_pipe_capquit_to_child");
1692
1693     /* it doesn't matter *what* we send here, the first byte will stop the capture */
1694     /* simply sending a "QUIT" string */
1695     /*pipe_write_block(capture_opts->signal_pipe_write_fd, SP_QUIT, quit_msg);*/
1696     ret = write(capture_opts->signal_pipe_write_fd, quit_msg, sizeof quit_msg);
1697     if(ret == -1) {
1698         g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
1699               "signal_pipe_capquit_to_child: %d header: error %s", capture_opts->signal_pipe_write_fd, strerror(errno));
1700     }
1701 }
1702 #endif
1703
1704
1705 /* user wants to stop the capture run */
1706 void
1707 sync_pipe_stop(capture_options *capture_opts)
1708 {
1709 #ifdef _WIN32
1710   int count;
1711   DWORD childstatus;
1712   gboolean terminate = TRUE;
1713 #endif
1714
1715   if (capture_opts->fork_child != -1) {
1716 #ifndef _WIN32
1717     /* send the SIGINT signal to close the capture child gracefully. */
1718     int sts = kill(capture_opts->fork_child, SIGINT);
1719     if (sts != 0) {
1720         g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
1721               "Sending SIGINT to child failed: %s\n", strerror(errno));
1722     }
1723 #else
1724 #define STOP_SLEEP_TIME 500 /* ms */
1725 #define STOP_CHECK_TIME 50
1726     /* First, use the special signal pipe to try to close the capture child
1727      * gracefully.
1728      */
1729     signal_pipe_capquit_to_child(capture_opts);
1730
1731     /* Next, wait for the process to exit on its own */
1732     for (count = 0; count < STOP_SLEEP_TIME / STOP_CHECK_TIME; count++) {
1733       if (GetExitCodeProcess((HANDLE) capture_opts->fork_child, &childstatus) &&
1734               childstatus != STILL_ACTIVE) {
1735         terminate = FALSE;
1736         break;
1737       }
1738       Sleep(STOP_CHECK_TIME);
1739     }
1740
1741     /* Force the issue. */
1742     if (terminate) {
1743       g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
1744             "sync_pipe_stop: forcing child to exit");
1745       sync_pipe_kill(capture_opts->fork_child);
1746     }
1747 #endif
1748   }
1749 }
1750
1751
1752 /* Wireshark has to exit, force the capture child to close */
1753 void
1754 sync_pipe_kill(int fork_child)
1755 {
1756     if (fork_child != -1) {
1757 #ifndef _WIN32
1758         int sts = kill(fork_child, SIGTERM);    /* SIGTERM so it can clean up if necessary */
1759         if (sts != 0) {
1760             g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_WARNING,
1761                   "Sending SIGTERM to child failed: %s\n", strerror(errno));
1762         }
1763 #else
1764         /* Remark: This is not the preferred method of closing a process!
1765          * the clean way would be getting the process id of the child process,
1766          * then getting window handle hWnd of that process (using EnumChildWindows),
1767          * and then do a SendMessage(hWnd, WM_CLOSE, 0, 0)
1768          *
1769          * Unfortunately, I don't know how to get the process id from the
1770          * handle.  OpenProcess will get an handle (not a window handle)
1771          * from the process ID; it will not get a window handle from the
1772          * process ID.  (How could it?  A process can have more than one
1773          * window.  For that matter, a process might have *no* windows,
1774          * as a process running dumpcap, the normal child process program,
1775          * probably does.)
1776          *
1777          * Hint: GenerateConsoleCtrlEvent() will only work if both processes are
1778          * running in the same console; that's not necessarily the case for
1779          * us, as we might not be running in a console.
1780          * And this also will require to have the process id.
1781          */
1782         TerminateProcess((HANDLE) (fork_child), 0);
1783 #endif
1784     }
1785 }
1786
1787 #endif /* HAVE_LIBPCAP */