forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.c
2319 lines (2060 loc) · 69.2 KB
/
capture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* capture.c
* Routines for packet capture windows
*
* $Id: capture.c,v 1.208 2003/07/23 05:01:15 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_LIBPCAP
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifndef _WIN32
/*
* Define various POSIX macros (and, in the case of WCOREDUMP, non-POSIX
* macros) on UNIX systems that don't have them.
*/
#ifndef WIFEXITED
# define WIFEXITED(status) (((status) & 0177) == 0)
#endif
#ifndef WIFSTOPPED
# define WIFSTOPPED(status) (((status) & 0177) == 0177)
#endif
#ifndef WIFSIGNALED
# define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
#endif
#ifndef WEXITSTATUS
# define WEXITSTATUS(status) ((status) >> 8)
#endif
#ifndef WTERMSIG
# define WTERMSIG(status) ((status) & 0177)
#endif
#ifndef WCOREDUMP
# define WCOREDUMP(status) ((status) & 0200)
#endif
#ifndef WSTOPSIG
# define WSTOPSIG(status) ((status) >> 8)
#endif
#endif /* _WIN32 */
#ifdef HAVE_IO_H
# include <io.h>
#endif
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <signal.h>
#include <errno.h>
#include <pcap.h>
#ifdef NEED_SNPRINTF_H
# include "snprintf.h"
#endif
#ifdef _WIN32
#include <process.h> /* For spawning child process */
#endif
/*
* XXX - the various BSDs appear to define BSD in <sys/param.h>; we don't
* want to include it if it's not present on this platform, however.
*/
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__APPLE__)
#ifndef BSD
#define BSD
#endif /* BSD */
#endif /* defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__APPLE__) */
/*
* We don't want to do a "select()" on the pcap_t's file descriptor on
* BSD (because "select()" doesn't work correctly on BPF devices on at
* least some releases of some flavors of BSD), and we don't want to do
* it on Windows (because "select()" is something for sockets, not for
* arbitrary handles).
*
* We *do* want to do it on other platforms, as, on other platforms (with
* the possible exception of Ultrix and Digital UNIX), the read timeout
* doesn't expire if no packets have arrived, so a "pcap_dispatch()" call
* will block until packets arrive, causing the UI to hang.
*/
#if !defined(BSD) && !defined(_WIN32)
# define MUST_DO_SELECT
#endif
#include <epan/packet.h>
#include "file.h"
#include "capture.h"
#include "util.h"
#include "pcap-util.h"
#include "simple_dialog.h"
#include "prefs.h"
#include "globals.h"
#include "conditions.h"
#include "capture_stop_conditions.h"
#include "ringbuffer.h"
#include "wiretap/libpcap.h"
#include "wiretap/wtap.h"
#include "wiretap/wtap-capture.h"
#include "packet-atalk.h"
#include "packet-atm.h"
#include "packet-clip.h"
#include "packet-eth.h"
#include "packet-fddi.h"
#include "packet-null.h"
#include "packet-ppp.h"
#include "packet-raw.h"
#include "packet-sll.h"
#include "packet-tr.h"
#include "packet-ieee80211.h"
#include "packet-chdlc.h"
#include "packet-prism.h"
#include "packet-ipfc.h"
#include "packet-arcnet.h"
#ifdef _WIN32
#include "capture-wpcap.h"
#endif
/*
* Capture options.
*/
capture_options capture_opts;
static int sync_pipe[2]; /* used to sync father */
enum PIPES { READ, WRITE }; /* Constants 0 and 1 for READ and WRITE */
int quit_after_cap; /* Makes a "capture only mode". Implies -k */
gboolean capture_child; /* if this is the child for "-S" */
static int fork_child = -1; /* If not -1, in parent, process ID of child */
static guint cap_input_id;
/*
* Indications sent out on the sync pipe.
*/
#define SP_CAPSTART ';' /* capture start message */
#define SP_PACKET_COUNT '*' /* followed by count of packets captured since last message */
#define SP_ERROR_MSG '!' /* followed by length of error message that follows */
#define SP_DROPS '#' /* followed by count of packets dropped in capture */
#ifdef _WIN32
static guint cap_timer_id;
static int cap_timer_cb(gpointer); /* Win32 kludge to check for pipe input */
#endif
static void cap_file_input_cb(gpointer, gint, GdkInputCondition);
static void wait_for_child(gboolean);
#ifndef _WIN32
static char *signame(int);
#endif
static void capture_delete_cb(GtkWidget *, GdkEvent *, gpointer);
static void capture_stop_cb(GtkWidget *, gpointer);
static void capture_pcap_cb(guchar *, const struct pcap_pkthdr *,
const guchar *);
static void get_capture_file_io_error(char *, int, const char *, int, gboolean);
static void popup_errmsg(const char *);
static void send_errmsg_to_parent(const char *);
static float pct(gint, gint);
static void stop_capture(int signo);
typedef struct _loop_data {
gboolean go; /* TRUE as long as we're supposed to keep capturing */
gint max; /* Number of packets we're supposed to capture - 0 means infinite */
int err; /* if non-zero, error seen while capturing */
gint linktype;
gint sync_packets;
gboolean pcap_err; /* TRUE if error from pcap */
gboolean from_pipe; /* TRUE if we are capturing data from a pipe */
packet_counts counts;
wtap_dumper *pdh;
#ifndef _WIN32
gboolean modified; /* TRUE if data in the pipe uses modified pcap headers */
gboolean byte_swapped; /* TRUE if data in the pipe is byte swapped */
unsigned int bytes_to_read, bytes_read; /* Used by pipe_dispatch */
enum {
STATE_EXPECT_REC_HDR, STATE_READ_REC_HDR,
STATE_EXPECT_DATA, STATE_READ_DATA
} pipe_state;
enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } pipe_err;
#endif
} loop_data;
#ifndef _WIN32
static void adjust_header(loop_data *, struct pcap_hdr *, struct pcaprec_hdr *);
static int pipe_open_live(char *, struct pcap_hdr *, loop_data *, char *, int);
static int pipe_dispatch(int, loop_data *, struct pcap_hdr *, \
struct pcaprec_modified_hdr *, guchar *, char *, int);
#endif
/* Win32 needs the O_BINARY flag for open() */
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifdef _WIN32
/* Win32 needs a handle to the child capture process */
int child_process;
#endif
/* Add a string pointer to a NULL-terminated array of string pointers. */
static char **
add_arg(char **args, int *argc, char *arg)
{
/* Grow the array; "*argc" currently contains the number of string
pointers, *not* counting the NULL pointer at the end, so we have
to add 2 in order to get the new size of the array, including the
new pointer and the terminating NULL pointer. */
args = g_realloc(args, (*argc + 2) * sizeof (char *));
/* Stuff the pointer into the penultimate element of the array, which
is the one at the index specified by "*argc". */
args[*argc] = arg;
/* Now bump the count. */
(*argc)++;
/* We overwrite the NULL pointer; put it back right after the
element we added. */
args[*argc] = NULL;
return args;
}
#ifdef _WIN32
/* Given a string, return a pointer to a quote-encapsulated version of
the string, so we can pass it as an argument with "spawnvp" even
if it contains blanks. */
char *
quote_encapsulate(const char *string)
{
char *encapsulated_string;
encapsulated_string = g_new(char, strlen(string) + 3);
sprintf(encapsulated_string, "\"%s\"", string);
return encapsulated_string;
}
#endif
/* Open a specified file, or create a temporary file, and start a capture
to the file in question. */
void
do_capture(const char *save_file)
{
char tmpname[128+1];
gboolean is_tempfile;
guchar c;
int i;
guint byte_count;
char *msg;
int err;
int capture_succeeded;
gboolean stats_known;
struct pcap_stat stats;
gchar *capfile_name;
if (save_file != NULL) {
/* If the Sync option is set, we return to the caller while the capture
* is in progress. Therefore we need to take a copy of save_file in
* case the caller destroys it after we return.
*/
capfile_name = g_strdup(save_file);
if (capture_opts.ringbuffer_on) {
/* ringbuffer is enabled */
cfile.save_file_fd = ringbuf_init(capfile_name,
capture_opts.ringbuffer_num_files);
} else {
/* Try to open/create the specified file for use as a capture buffer. */
cfile.save_file_fd = open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
0600);
}
is_tempfile = FALSE;
} else {
/* Choose a random name for the capture buffer */
cfile.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
capfile_name = g_strdup(tmpname);
is_tempfile = TRUE;
}
if (cfile.save_file_fd == -1) {
if (is_tempfile) {
simple_dialog(ESD_TYPE_CRIT, NULL,
"The temporary file to which the capture would be saved (\"%s\")"
"could not be opened: %s.", capfile_name, strerror(errno));
} else {
if (capture_opts.ringbuffer_on) {
ringbuf_error_cleanup();
}
simple_dialog(ESD_TYPE_CRIT, NULL,
file_open_error_message(errno, TRUE, WTAP_FILE_PCAP), capfile_name);
}
g_free(capfile_name);
return;
}
close_cap_file(&cfile);
g_assert(cfile.save_file == NULL);
cfile.save_file = capfile_name;
/* cfile.save_file is "g_free"ed below, which is equivalent to
"g_free(capfile_name)". */
if (capture_opts.sync_mode) { /* do the capture in a child process */
char ssnap[24];
char scount[24]; /* need a constant for len of numbers */
char sautostop_filesize[24]; /* need a constant for len of numbers */
char sautostop_duration[24]; /* need a constant for len of numbers */
char save_file_fd[24];
char errmsg[1024+1];
int error;
int argc;
char **argv;
#ifdef _WIN32
char sync_pipe_fd[24];
char *fontstring;
char *filterstring;
#endif
/* Allocate the string pointer array with enough space for the
terminating NULL pointer. */
argc = 0;
argv = g_malloc(sizeof (char *));
*argv = NULL;
/* Now add those arguments used on all platforms. */
argv = add_arg(argv, &argc, CHILD_NAME);
argv = add_arg(argv, &argc, "-i");
argv = add_arg(argv, &argc, cfile.iface);
argv = add_arg(argv, &argc, "-w");
argv = add_arg(argv, &argc, cfile.save_file);
argv = add_arg(argv, &argc, "-W");
sprintf(save_file_fd,"%d",cfile.save_file_fd); /* in lieu of itoa */
argv = add_arg(argv, &argc, save_file_fd);
if (capture_opts.has_autostop_count) {
argv = add_arg(argv, &argc, "-c");
sprintf(scount,"%d",capture_opts.autostop_count);
argv = add_arg(argv, &argc, scount);
}
if (capture_opts.has_snaplen) {
argv = add_arg(argv, &argc, "-s");
sprintf(ssnap,"%d",capture_opts.snaplen);
argv = add_arg(argv, &argc, ssnap);
}
if (capture_opts.has_autostop_filesize) {
argv = add_arg(argv, &argc, "-a");
sprintf(sautostop_filesize,"filesize:%d",capture_opts.autostop_filesize);
argv = add_arg(argv, &argc, sautostop_filesize);
}
if (capture_opts.has_autostop_duration) {
argv = add_arg(argv, &argc, "-a");
sprintf(sautostop_duration,"duration:%d",capture_opts.autostop_duration);
argv = add_arg(argv, &argc, sautostop_duration);
}
if (!capture_opts.promisc_mode)
argv = add_arg(argv, &argc, "-p");
#ifdef _WIN32
/* Create a pipe for the child process */
if(_pipe(sync_pipe, 512, O_BINARY) < 0) {
/* Couldn't create the pipe between parent and child. */
error = errno;
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
simple_dialog(ESD_TYPE_CRIT, NULL, "Couldn't create sync pipe: %s",
strerror(error));
return;
}
/* Convert font name to a quote-encapsulated string and pass to child */
argv = add_arg(argv, &argc, "-m");
fontstring = quote_encapsulate(prefs.gui_font_name);
argv = add_arg(argv, &argc, fontstring);
/* Convert pipe write handle to a string and pass to child */
argv = add_arg(argv, &argc, "-Z");
itoa(sync_pipe[WRITE], sync_pipe_fd, 10);
argv = add_arg(argv, &argc, sync_pipe_fd);
/* Convert filter string to a quote delimited string and pass to child */
filterstring = NULL;
if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
argv = add_arg(argv, &argc, "-f");
filterstring = quote_encapsulate(cfile.cfilter);
argv = add_arg(argv, &argc, filterstring);
}
/* Spawn process */
fork_child = spawnvp(_P_NOWAIT, ethereal_path, argv);
g_free(fontstring);
if (filterstring) {
g_free(filterstring);
}
/* Keep a copy for later evaluation by _cwait() */
child_process = fork_child;
#else
if (pipe(sync_pipe) < 0) {
/* Couldn't create the pipe between parent and child. */
error = errno;
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
simple_dialog(ESD_TYPE_CRIT, NULL, "Couldn't create sync pipe: %s",
strerror(error));
return;
}
argv = add_arg(argv, &argc, "-m");
argv = add_arg(argv, &argc, prefs.gui_font_name);
if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
argv = add_arg(argv, &argc, "-f");
argv = add_arg(argv, &argc, cfile.cfilter);
}
if ((fork_child = fork()) == 0) {
/*
* Child process - run Ethereal with the right arguments to make
* it just pop up the live capture dialog box and capture with
* the specified capture parameters, writing to the specified file.
*
* args: -i interface specification
* -w file to write
* -W file descriptor to write
* -c count to capture
* -s snaplen
* -m / -b fonts
* -f "filter expression"
*/
close(1);
dup(sync_pipe[WRITE]);
close(sync_pipe[READ]);
execvp(ethereal_path, argv);
snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
ethereal_path, strerror(errno));
send_errmsg_to_parent(errmsg);
/* Exit with "_exit()", so that we don't close the connection
to the X server (and cause stuff buffered up by our parent but
not yet sent to be sent, as that stuff should only be sent by
our parent). */
_exit(2);
}
#endif
/* Parent process - read messages from the child process over the
sync pipe. */
g_free(argv); /* free up arg array */
/* Close the write side of the pipe, so that only the child has it
open, and thus it completely closes, and thus returns to us
an EOF indication, if the child closes it (either deliberately
or by exiting abnormally). */
close(sync_pipe[WRITE]);
/* Close the save file FD, as we won't be using it - we'll be opening
it and reading the save file through Wiretap. */
close(cfile.save_file_fd);
if (fork_child == -1) {
/* We couldn't even create the child process. */
error = errno;
close(sync_pipe[READ]);
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
simple_dialog(ESD_TYPE_CRIT, NULL, "Couldn't create child process: %s",
strerror(error));
return;
}
/* Read a byte count from "sync_pipe[READ]", terminated with a
colon; if the count is 0, the child process created the
capture file and we should start reading from it, otherwise
the capture couldn't start and the count is a count of bytes
of error message, and we should display the message. */
byte_count = 0;
for (;;) {
i = read(sync_pipe[READ], &c, 1);
if (i == 0) {
/* EOF - the child process died.
Close the read side of the sync pipe, remove the capture file,
and report the failure. */
close(sync_pipe[READ]);
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
wait_for_child(TRUE);
return;
}
if (c == SP_CAPSTART || c == SP_ERROR_MSG)
break;
if (!isdigit(c)) {
/* Child process handed us crap.
Close the read side of the sync pipe, remove the capture file,
and report the failure. */
close(sync_pipe[READ]);
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
simple_dialog(ESD_TYPE_WARN, NULL,
"Capture child process sent us a bad message");
return;
}
byte_count = byte_count*10 + c - '0';
}
if (c == SP_CAPSTART) {
/* Success. Open the capture file, and set up to read it. */
err = start_tail_cap_file(cfile.save_file, is_tempfile, &cfile);
if (err == 0) {
/* We were able to open and set up to read the capture file;
arrange that our callback be called whenever it's possible
to read from the sync pipe, so that it's called when
the child process wants to tell us something. */
#ifdef _WIN32
/* Tricky to use pipes in win9x, as no concept of wait. NT can
do this but that doesn't cover all win32 platforms. GTK can do
this but doesn't seem to work over processes. Attempt to do
something similar here, start a timer and check for data on every
timeout. */
cap_timer_id = gtk_timeout_add(1000, cap_timer_cb, NULL);
#else
cap_input_id = gtk_input_add_full(sync_pipe[READ],
GDK_INPUT_READ|GDK_INPUT_EXCEPTION,
cap_file_input_cb,
NULL,
(gpointer) &cfile,
NULL);
#endif
} else {
/* We weren't able to open the capture file; user has been
alerted. Close the sync pipe. */
close(sync_pipe[READ]);
/* Don't unlink the save file - leave it around, for debugging
purposes. */
g_free(cfile.save_file);
cfile.save_file = NULL;
}
} else {
/* Failure - the child process sent us a message indicating
what the problem was. */
if (byte_count == 0) {
/* Zero-length message? */
simple_dialog(ESD_TYPE_WARN, NULL,
"Capture child process failed, but its error message was empty.");
} else {
msg = g_malloc(byte_count + 1);
if (msg == NULL) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Capture child process failed, but its error message was too big.");
} else {
i = read(sync_pipe[READ], msg, byte_count);
msg[byte_count] = '\0';
if (i < 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Capture child process failed: Error %s reading its error message.",
strerror(errno));
} else if (i == 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Capture child process failed: EOF reading its error message.");
wait_for_child(FALSE);
} else
simple_dialog(ESD_TYPE_CRIT, NULL, msg);
g_free(msg);
}
/* Close the sync pipe. */
close(sync_pipe[READ]);
/* Get rid of the save file - the capture never started. */
unlink(cfile.save_file);
g_free(cfile.save_file);
cfile.save_file = NULL;
}
}
} else {
/* Not sync mode. */
capture_succeeded = capture(&stats_known, &stats);
if (quit_after_cap) {
/* DON'T unlink the save file. Presumably someone wants it. */
gtk_exit(0);
}
if (capture_succeeded) {
/* Capture succeeded; read in the capture file. */
if ((err = open_cap_file(cfile.save_file, is_tempfile, &cfile)) == 0) {
/* Set the read filter to NULL. */
cfile.rfcode = NULL;
/* Get the packet-drop statistics.
XXX - there are currently no packet-drop statistics stored
in libpcap captures, and that's what we're reading.
At some point, we will add support in Wiretap to return
packet-drop statistics for capture file formats that store it,
and will make "read_cap_file()" get those statistics from
Wiretap. We clear the statistics (marking them as "not known")
in "open_cap_file()", and "read_cap_file()" will only fetch
them and mark them as known if Wiretap supplies them, so if
we get the statistics now, after calling "open_cap_file()" but
before calling "read_cap_file()", the values we store will
be used by "read_cap_file()".
If a future libpcap capture file format stores the statistics,
we'll put them into the capture file that we write, and will
thus not have to set them here - "read_cap_file()" will get
them from the file and use them. */
if (stats_known) {
cfile.drops_known = TRUE;
/* XXX - on some systems, libpcap doesn't bother filling in
"ps_ifdrop" - it doesn't even set it to zero - so we don't
bother looking at it.
Ideally, libpcap would have an interface that gave us
several statistics - perhaps including various interface
error statistics - and would tell us which of them it
supplies, allowing us to display only the ones it does. */
cfile.drops = stats.ps_drop;
}
switch (read_cap_file(&cfile, &err)) {
case READ_SUCCESS:
case READ_ERROR:
/* Just because we got an error, that doesn't mean we were unable
to read any of the file; we handle what we could get from the
file. */
break;
case READ_ABORTED:
/* Exit by leaving the main loop, so that any quit functions
we registered get called. */
if (gtk_main_level() > 0)
gtk_main_quit();
return;
}
}
}
/* We're not doing a capture any more, so we don't have a save
file. */
if (capture_opts.ringbuffer_on) {
ringbuf_free();
} else {
g_free(cfile.save_file);
}
cfile.save_file = NULL;
}
}
#ifdef _WIN32
/* The timer has expired, see if there's stuff to read from the pipe,
if so call the cap_file_input_cb */
static gint
cap_timer_cb(gpointer data)
{
HANDLE handle;
DWORD avail = 0;
gboolean result, result1;
DWORD childstatus;
/* Oddly enough although Named pipes don't work on win9x,
PeekNamedPipe does !!! */
handle = (HANDLE) _get_osfhandle (sync_pipe[READ]);
result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
/* Get the child process exit status */
result1 = GetExitCodeProcess((HANDLE)child_process, &childstatus);
/* If the Peek returned an error, or there are bytes to be read
or the childwatcher thread has terminated then call the normal
callback */
if (!result || avail > 0 || childstatus != STILL_ACTIVE) {
/* avoid reentrancy problems and stack overflow */
gtk_timeout_remove(cap_timer_id);
/* And call the real handler */
cap_file_input_cb((gpointer) &cfile, 0, 0);
/* Return false so that the timer is not run again */
return FALSE;
}
else {
/* No data so let timer run again */
return TRUE;
}
}
#endif
/* There's stuff to read from the sync pipe, meaning the child has sent
us a message, or the sync pipe has closed, meaning the child has
closed it (perhaps because it exited). */
static void
cap_file_input_cb(gpointer data, gint source _U_,
GdkInputCondition condition _U_)
{
capture_file *cf = (capture_file *)data;
#define BUFSIZE 4096
char buffer[BUFSIZE+1], *p = buffer, *q = buffer, *msg, *r;
int nread, msglen, chars_to_copy;
int to_read = 0;
int err;
#ifndef _WIN32
/* avoid reentrancy problems and stack overflow */
gtk_input_remove(cap_input_id);
#endif
if ((nread = read(sync_pipe[READ], buffer, BUFSIZE)) <= 0) {
/* The child has closed the sync pipe, meaning it's not going to be
capturing any more packets. Pick up its exit status, and
complain if it did anything other than exit with status 0. */
wait_for_child(FALSE);
/* Read what remains of the capture file, and finish the capture.
XXX - do something if this fails? */
switch (finish_tail_cap_file(cf, &err)) {
case READ_SUCCESS:
case READ_ERROR:
/* Just because we got an error, that doesn't mean we were unable
to read any of the file; we handle what we could get from the
file. */
break;
case READ_ABORTED:
/* Exit by leaving the main loop, so that any quit functions
we registered get called. */
gtk_main_quit();
return;
}
/* We're not doing a capture any more, so we don't have a save
file. */
g_free(cf->save_file);
cf->save_file = NULL;
return;
}
buffer[nread] = '\0';
while (nread != 0) {
/* look for (possibly multiple) indications */
switch (*q) {
case SP_PACKET_COUNT :
to_read += atoi(p);
p = q + 1;
q++;
nread--;
break;
case SP_DROPS :
cf->drops_known = TRUE;
cf->drops = atoi(p);
p = q + 1;
q++;
nread--;
break;
case SP_ERROR_MSG :
msglen = atoi(p);
p = q + 1;
q++;
nread--;
/* Read the entire message.
XXX - if the child hasn't sent it all yet, this could cause us
to hang until they do. */
msg = g_malloc(msglen + 1);
r = msg;
while (msglen != 0) {
if (nread == 0) {
/* Read more. */
if ((nread = read(sync_pipe[READ], buffer, BUFSIZE)) <= 0)
break;
p = buffer;
q = buffer;
}
chars_to_copy = MIN(msglen, nread);
memcpy(r, q, chars_to_copy);
r += chars_to_copy;
q += chars_to_copy;
nread -= chars_to_copy;
msglen -= chars_to_copy;
}
*r = '\0';
simple_dialog(ESD_TYPE_CRIT, NULL, msg);
g_free(msg);
break;
default :
q++;
nread--;
break;
}
}
/* Read from the capture file the number of records the child told us
it added.
XXX - do something if this fails? */
switch (continue_tail_cap_file(cf, to_read, &err)) {
case READ_SUCCESS:
case READ_ERROR:
/* Just because we got an error, that doesn't mean we were unable
to read any of the file; we handle what we could get from the
file.
XXX - abort on a read error? */
break;
case READ_ABORTED:
/* Kill the child capture process; the user wants to exit, and we
shouldn't just leave it running. */
#ifdef _WIN32
/* XXX - kill it. */
#else
kill(fork_child, SIGTERM); /* SIGTERM so it can clean up if necessary */
#endif
break;
}
/* restore pipe handler */
#ifdef _WIN32
cap_timer_id = gtk_timeout_add(1000, cap_timer_cb, NULL);
#else
cap_input_id = gtk_input_add_full (sync_pipe[READ],
GDK_INPUT_READ|GDK_INPUT_EXCEPTION,
cap_file_input_cb,
NULL,
(gpointer) cf,
NULL);
#endif
}
static void
wait_for_child(gboolean always_report)
{
int wstatus;
#ifdef _WIN32
/* XXX - analyze the wait stuatus and display more information
in the dialog box? */
if (_cwait(&wstatus, child_process, _WAIT_CHILD) == -1) {
simple_dialog(ESD_TYPE_WARN, NULL, "Child capture process stopped unexpectedly");
}
#else
if (wait(&wstatus) != -1) {
if (WIFEXITED(wstatus)) {
/* The child exited; display its exit status, if it's not zero,
and even if it's zero if "always_report" is true. */
if (always_report || WEXITSTATUS(wstatus) != 0) {
simple_dialog(ESD_TYPE_WARN, NULL,
"Child capture process exited: exit status %d",
WEXITSTATUS(wstatus));
}
} else if (WIFSTOPPED(wstatus)) {
/* It stopped, rather than exiting. "Should not happen." */
simple_dialog(ESD_TYPE_WARN, NULL,
"Child capture process stopped: %s",
signame(WSTOPSIG(wstatus)));
} else if (WIFSIGNALED(wstatus)) {
/* It died with a signal. */
simple_dialog(ESD_TYPE_WARN, NULL,
"Child capture process died: %s%s",
signame(WTERMSIG(wstatus)),
WCOREDUMP(wstatus) ? " - core dumped" : "");
} else {
/* What? It had to either have exited, or stopped, or died with
a signal; what happened here? */
simple_dialog(ESD_TYPE_WARN, NULL,
"Child capture process died: wait status %#o", wstatus);
}
}
/* No more child process. */
fork_child = -1;
#endif
}
#ifndef _WIN32
static char *
signame(int sig)
{
char *sigmsg;
static char sigmsg_buf[6+1+3+1];
switch (sig) {
case SIGHUP:
sigmsg = "Hangup";
break;
case SIGINT:
sigmsg = "Interrupted";
break;
case SIGQUIT:
sigmsg = "Quit";
break;
case SIGILL:
sigmsg = "Illegal instruction";
break;
case SIGTRAP:
sigmsg = "Trace trap";
break;
case SIGABRT:
sigmsg = "Abort";
break;
case SIGFPE:
sigmsg = "Arithmetic exception";
break;
case SIGKILL:
sigmsg = "Killed";
break;
case SIGBUS:
sigmsg = "Bus error";
break;
case SIGSEGV:
sigmsg = "Segmentation violation";
break;
/* http://metalab.unc.edu/pub/Linux/docs/HOWTO/GCC-HOWTO
Linux is POSIX compliant. These are not POSIX-defined signals ---
ISO/IEC 9945-1:1990 (IEEE Std 1003.1-1990), paragraph B.3.3.1.1 sez:
``The signals SIGBUS, SIGEMT, SIGIOT, SIGTRAP, and SIGSYS
were omitted from POSIX.1 because their behavior is
implementation dependent and could not be adequately catego-
rized. Conforming implementations may deliver these sig-
nals, but must document the circumstances under which they
are delivered and note any restrictions concerning their
delivery.''
So we only check for SIGSYS on those systems that happen to
implement them (a system can be POSIX-compliant and implement
them, it's just that POSIX doesn't *require* a POSIX-compliant
system to implement them).