forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
2069 lines (1742 loc) · 63.4 KB
/
file.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
/* file.c
* File I/O routines
*
* $Id: file.c,v 1.212 2000/08/24 09:16:39 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
* 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
#include <gtk/gtk.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#ifdef HAVE_IO_H
#include <io.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef NEED_SNPRINTF_H
# include "snprintf.h"
#endif
#ifdef NEED_STRERROR_H
#include "strerror.h"
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#include "gtk/main.h"
#include "column.h"
#include "packet.h"
#include "print.h"
#include "file.h"
#include "menu.h"
#include "util.h"
#include "simple_dialog.h"
#include "ui_util.h"
#include "prefs.h"
#include "gtk/proto_draw.h"
#include "dfilter.h"
#include "conversation.h"
#include "globals.h"
#include "plugins.h"
extern GtkWidget *packet_list, *info_bar, *byte_view, *tree_view;
extern guint file_ctx;
gboolean auto_scroll_live = FALSE;
static guint32 firstsec, firstusec;
static guint32 prevsec, prevusec;
static void read_packet(capture_file *cf, int offset);
static void rescan_packets(capture_file *cf, const char *action,
gboolean refilter, gboolean redissect);
static void set_selected_row(int row);
static void freeze_clist(capture_file *cf);
static void thaw_clist(capture_file *cf);
static char *file_rename_error_message(int err);
static char *file_close_error_message(int err);
static gboolean copy_binary_file(char *from_filename, char *to_filename);
/* Update the progress bar this many times when reading a file. */
#define N_PROGBAR_UPDATES 100
/* Number of "frame_data" structures per memory chunk.
XXX - is this the right number? */
#define FRAME_DATA_CHUNK_SIZE 1024
int
open_cap_file(char *fname, gboolean is_tempfile, capture_file *cf)
{
wtap *wth;
int err;
FILE_T fh;
int fd;
struct stat cf_stat;
wth = wtap_open_offline(fname, &err, TRUE);
if (wth == NULL)
goto fail;
/* Find the size of the file. */
fh = wtap_file(wth);
fd = wtap_fd(wth);
if (fstat(fd, &cf_stat) < 0) {
err = errno;
wtap_close(wth);
goto fail;
}
/* The open succeeded. Close whatever capture file we had open,
and fill in the information for this file. */
close_cap_file(cf, info_bar);
/* Initialize the table of conversations. */
conversation_init();
/* Initialize protocol-specific variables */
init_all_protocols();
/* We're about to start reading the file. */
cf->state = FILE_READ_IN_PROGRESS;
cf->wth = wth;
cf->filed = fd;
cf->f_len = cf_stat.st_size;
/* Set the file name because we need it to set the follow stream filter.
XXX - is that still true? We need it for other reasons, though,
in any case. */
cf->filename = g_strdup(fname);
/* Indicate whether it's a permanent or temporary file. */
cf->is_tempfile = is_tempfile;
/* If it's a temporary capture buffer file, mark it as not saved. */
cf->user_saved = !is_tempfile;
cf->cd_t = wtap_file_type(cf->wth);
cf->count = 0;
cf->drops = 0;
cf->esec = 0;
cf->eusec = 0;
cf->snap = wtap_snapshot_length(cf->wth);
cf->progbar_quantum = 0;
cf->progbar_nextstep = 0;
firstsec = 0, firstusec = 0;
prevsec = 0, prevusec = 0;
cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
sizeof(frame_data),
FRAME_DATA_CHUNK_SIZE * sizeof(frame_data),
G_ALLOC_AND_FREE);
g_assert(cf->plist_chunk);
return (0);
fail:
simple_dialog(ESD_TYPE_CRIT, NULL,
file_open_error_message(err, FALSE), fname);
return (err);
}
/* Reset everything to a pristine state */
void
close_cap_file(capture_file *cf, void *w)
{
/* Die if we're in the middle of reading a file. */
g_assert(cf->state != FILE_READ_IN_PROGRESS);
/* Destroy all popup packet windows, as they refer to packets in the
capture file we're closing. */
destroy_packet_wins();
if (cf->wth) {
wtap_close(cf->wth);
cf->wth = NULL;
}
/* We have no file open... */
if (cf->filename != NULL) {
/* If it's a temporary file, remove it. */
if (cf->is_tempfile)
unlink(cf->filename);
g_free(cf->filename);
cf->filename = NULL;
}
/* ...which means we have nothing to save. */
cf->user_saved = FALSE;
if (cf->plist_chunk != NULL) {
g_mem_chunk_destroy(cf->plist_chunk);
cf->plist_chunk = NULL;
}
if (cf->rfcode != NULL) {
dfilter_destroy(cf->rfcode);
cf->rfcode = NULL;
}
cf->plist = NULL;
cf->plist_end = NULL;
unselect_packet(cf); /* nothing to select */
cf->first_displayed = NULL;
cf->last_displayed = NULL;
/* Clear the packet list. */
gtk_clist_freeze(GTK_CLIST(packet_list));
gtk_clist_clear(GTK_CLIST(packet_list));
gtk_clist_thaw(GTK_CLIST(packet_list));
/* Clear any file-related status bar messages.
XXX - should be "clear *ALL* file-related status bar messages;
will there ever be more than one on the stack? */
gtk_statusbar_pop(GTK_STATUSBAR(w), file_ctx);
/* Restore the standard title bar message. */
set_main_window_name("The Ethereal Network Analyzer");
/* Disable all menu items that make sense only if you have a capture. */
set_menus_for_capture_file(FALSE);
set_menus_for_unsaved_capture_file(FALSE);
set_menus_for_captured_packets(FALSE);
set_menus_for_selected_packet(FALSE);
set_menus_for_capture_in_progress(FALSE);
set_menus_for_selected_tree_row(FALSE);
/* We have no file open. */
cf->state = FILE_CLOSED;
}
/* Set the file name in the status line, in the name for the main window,
and in the name for the main window's icon. */
static void
set_display_filename(capture_file *cf)
{
gchar *name_ptr;
size_t msg_len;
gchar *done_fmt = " File: %s Drops: %u";
gchar *done_msg;
gchar *win_name_fmt = "%s - Ethereal";
gchar *win_name;
if (!cf->is_tempfile) {
/* Get the last component of the file name, and put that in the
status bar. */
name_ptr = get_basename(cf->filename);
} else {
/* The file we read is a temporary file from a live capture;
we don't mention its name in the status bar. */
name_ptr = "<capture>";
}
msg_len = strlen(name_ptr) + strlen(done_fmt) + 64;
done_msg = g_malloc(msg_len);
snprintf(done_msg, msg_len, done_fmt, name_ptr, cf->drops);
gtk_statusbar_push(GTK_STATUSBAR(info_bar), file_ctx, done_msg);
g_free(done_msg);
msg_len = strlen(name_ptr) + strlen(win_name_fmt) + 1;
win_name = g_malloc(msg_len);
snprintf(win_name, msg_len, win_name_fmt, name_ptr);
set_main_window_name(win_name);
g_free(win_name);
}
read_status_t
read_cap_file(capture_file *cf, int *err)
{
gchar *name_ptr, *load_msg, *load_fmt = " Loading: %s...";
size_t msg_len;
char *errmsg;
char errmsg_errno[1024+1];
gchar err_str[2048+1];
int data_offset;
progdlg_t *progbar;
gboolean stop_flag;
int file_pos;
float prog_val;
name_ptr = get_basename(cf->filename);
msg_len = strlen(name_ptr) + strlen(load_fmt) + 2;
load_msg = g_malloc(msg_len);
snprintf(load_msg, msg_len, load_fmt, name_ptr);
gtk_statusbar_push(GTK_STATUSBAR(info_bar), file_ctx, load_msg);
/* Update the progress bar when it gets to this value. */
cf->progbar_nextstep = 0;
/* When we reach the value that triggers a progress bar update,
bump that value by this amount. */
cf->progbar_quantum = cf->f_len/N_PROGBAR_UPDATES;
#ifndef O_BINARY
#define O_BINARY 0
#endif
freeze_clist(cf);
stop_flag = FALSE;
progbar = create_progress_dlg(load_msg, "Stop", &stop_flag);
g_free(load_msg);
while ((data_offset = wtap_read(cf->wth, err)) > 0) {
/* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
when we update it, we have to run the GTK+ main loop to get it
to repaint what's pending, and doing so may involve an "ioctl()"
to see if there's any pending input from an X server, and doing
that for every packet can be costly, especially on a big file. */
if (data_offset >= cf->progbar_nextstep) {
file_pos = lseek(cf->filed, 0, SEEK_CUR);
prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
update_progress_dlg(progbar, prog_val);
cf->progbar_nextstep += cf->progbar_quantum;
}
if (stop_flag) {
/* Well, the user decided to abort the read. Destroy the progress
bar, close the capture file, and return READ_ABORTED so our caller
can do whatever is appropriate when that happens. */
destroy_progress_dlg(progbar);
cf->state = FILE_READ_ABORTED; /* so that we're allowed to close it */
gtk_clist_thaw(GTK_CLIST(packet_list)); /* undo our freeze */
close_cap_file(cf, info_bar);
return (READ_ABORTED);
}
read_packet(cf, data_offset);
}
/* We're done reading the file; destroy the progress bar. */
destroy_progress_dlg(progbar);
/* We're done reading sequentially through the file. */
cf->state = FILE_READ_DONE;
/* Close the sequential I/O side, to free up memory it requires. */
wtap_sequential_close(cf->wth);
/* Set the file encapsulation type now; we don't know what it is until
we've looked at all the packets, as we don't know until then whether
there's more than one type (and thus whether it's
WTAP_ENCAP_PER_PACKET). */
cf->lnk_t = wtap_file_encap(cf->wth);
cf->current_frame = cf->first_displayed;
thaw_clist(cf);
gtk_statusbar_pop(GTK_STATUSBAR(info_bar), file_ctx);
set_display_filename(cf);
/* Enable menu items that make sense if you have a capture file you've
finished reading. */
set_menus_for_capture_file(TRUE);
set_menus_for_unsaved_capture_file(!cf->user_saved);
/* Enable menu items that make sense if you have some captured packets. */
set_menus_for_captured_packets(TRUE);
/* If we have any displayed packets to select, select the first of those
packets by making the first row the selected row. */
if (cf->first_displayed != NULL)
gtk_signal_emit_by_name(GTK_OBJECT(packet_list), "select_row", 0);
if (data_offset < 0) {
/* Put up a message box noting that the read failed somewhere along
the line. Don't throw out the stuff we managed to read, though,
if any. */
switch (*err) {
case WTAP_ERR_UNSUPPORTED_ENCAP:
errmsg = "The capture file is for a network type that Ethereal doesn't support.";
break;
case WTAP_ERR_CANT_READ:
errmsg = "An attempt to read from the file failed for"
" some unknown reason.";
break;
case WTAP_ERR_SHORT_READ:
errmsg = "The capture file appears to have been cut short"
" in the middle of a packet.";
break;
case WTAP_ERR_BAD_RECORD:
errmsg = "The capture file appears to be damaged or corrupt.";
break;
default:
snprintf(errmsg_errno, sizeof(errmsg_errno),
"An error occurred while reading the"
" capture file: %s.", wtap_strerror(*err));
errmsg = errmsg_errno;
break;
}
snprintf(err_str, sizeof err_str, errmsg);
simple_dialog(ESD_TYPE_CRIT, NULL, err_str);
return (READ_ERROR);
} else
return (READ_SUCCESS);
}
#ifdef HAVE_LIBPCAP
int
start_tail_cap_file(char *fname, gboolean is_tempfile, capture_file *cf)
{
int err;
int i;
err = open_cap_file(fname, is_tempfile, cf);
if (err == 0) {
/* Disable menu items that make no sense if you're currently running
a capture. */
set_menus_for_capture_in_progress(TRUE);
/* Enable menu items that make sense if you have some captured
packets (yes, I know, we don't have any *yet*). */
set_menus_for_captured_packets(TRUE);
for (i = 0; i < cf->cinfo.num_cols; i++) {
if (get_column_resize_type(cf->cinfo.col_fmt[i]) == RESIZE_LIVE)
gtk_clist_set_column_auto_resize(GTK_CLIST(packet_list), i, TRUE);
else {
gtk_clist_set_column_auto_resize(GTK_CLIST(packet_list), i, FALSE);
gtk_clist_set_column_width(GTK_CLIST(packet_list), i,
cf->cinfo.col_width[i]);
gtk_clist_set_column_resizeable(GTK_CLIST(packet_list), i, TRUE);
}
}
gtk_statusbar_push(GTK_STATUSBAR(info_bar), file_ctx,
" <live capture in progress>");
}
return err;
}
read_status_t
continue_tail_cap_file(capture_file *cf, int to_read, int *err)
{
int data_offset = 0;
gtk_clist_freeze(GTK_CLIST(packet_list));
while (to_read != 0 && (data_offset = wtap_read(cf->wth, err)) > 0) {
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to exit Ethereal. Break out of the
loop, and let the code below (which is called even if there
aren't any packets left to read) exit. */
break;
}
read_packet(cf, data_offset);
to_read--;
}
gtk_clist_thaw(GTK_CLIST(packet_list));
/* XXX - this cheats and looks inside the packet list to find the final
row number. */
if (auto_scroll_live && cf->plist_end != NULL)
gtk_clist_moveto(GTK_CLIST(packet_list),
GTK_CLIST(packet_list)->rows - 1, -1, 1.0, 1.0);
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to exit Ethereal. Return READ_ABORTED
so that our caller can kill off the capture child process;
this will cause an EOF on the pipe from the child, so
"finish_tail_cap_file()" will be called, and it will clean up
and exit. */
return READ_ABORTED;
} else if (data_offset < 0) {
/* We got an error reading the capture file.
XXX - pop up a dialog box? */
return (READ_ERROR);
} else
return (READ_SUCCESS);
}
read_status_t
finish_tail_cap_file(capture_file *cf, int *err)
{
int data_offset;
gtk_clist_freeze(GTK_CLIST(packet_list));
while ((data_offset = wtap_read(cf->wth, err)) > 0) {
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to abort the read. Break out of the
loop, and let the code below (which is called even if there
aren't any packets left to read) exit. */
break;
}
read_packet(cf, data_offset);
}
if (cf->state == FILE_READ_ABORTED) {
/* Well, the user decided to abort the read. We're only called
when the child capture process closes the pipe to us (meaning
it's probably exited), so we can just close the capture
file; we return READ_ABORTED so our caller can do whatever
is appropriate when that happens. */
close_cap_file(cf, info_bar);
return READ_ABORTED;
}
thaw_clist(cf);
if (auto_scroll_live && cf->plist_end != NULL)
/* XXX - this cheats and looks inside the packet list to find the final
row number. */
gtk_clist_moveto(GTK_CLIST(packet_list),
GTK_CLIST(packet_list)->rows - 1, -1, 1.0, 1.0);
/* We're done reading sequentially through the file. */
cf->state = FILE_READ_DONE;
/* We're done reading sequentially through the file; close the
sequential I/O side, to free up memory it requires. */
wtap_sequential_close(cf->wth);
/* Set the file encapsulation type now; we don't know what it is until
we've looked at all the packets, as we don't know until then whether
there's more than one type (and thus whether it's
WTAP_ENCAP_PER_PACKET). */
cf->lnk_t = wtap_file_encap(cf->wth);
/* Pop the "<live capture in progress>" message off the status bar. */
gtk_statusbar_pop(GTK_STATUSBAR(info_bar), file_ctx);
set_display_filename(cf);
/* Enable menu items that make sense if you're not currently running
a capture. */
set_menus_for_capture_in_progress(FALSE);
/* Enable menu items that make sense if you have a capture file
you've finished reading. */
set_menus_for_capture_file(TRUE);
set_menus_for_unsaved_capture_file(!cf->user_saved);
if (data_offset < 0) {
/* We got an error reading the capture file.
XXX - pop up a dialog box? */
return (READ_ERROR);
} else
return (READ_SUCCESS);
}
#endif /* HAVE_LIBPCAP */
typedef struct {
color_filter_t *colorf;
proto_tree *protocol_tree;
const guint8 *pd;
frame_data *fdata;
} apply_color_filter_args;
/*
* If no color filter has been applied, apply this one.
* (The "if no color filter has been applied" is to handle the case where
* more than one color filter matches the packet.)
*/
static void
apply_color_filter(gpointer filter_arg, gpointer argp)
{
color_filter_t *colorf = filter_arg;
apply_color_filter_args *args = argp;
if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
if (dfilter_apply(colorf->c_colorfilter, args->protocol_tree, args->pd, args->fdata->cap_len))
args->colorf = colorf;
}
}
static int
add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
union wtap_pseudo_header *pseudo_header, const u_char *buf,
gboolean refilter)
{
apply_color_filter_args args;
gint i, row;
proto_tree *protocol_tree = NULL;
/* We don't yet have a color filter to apply. */
args.colorf = NULL;
/* If we don't have the time stamp of the first packet in the
capture, it's because this is the first packet. Save the time
stamp of this packet as the time stamp of the first packet. */
if (!firstsec && !firstusec) {
firstsec = fdata->abs_secs;
firstusec = fdata->abs_usecs;
}
/* Get the time elapsed between the first packet and this packet. */
cf->esec = fdata->abs_secs - firstsec;
if (firstusec <= fdata->abs_usecs) {
cf->eusec = fdata->abs_usecs - firstusec;
} else {
cf->eusec = (fdata->abs_usecs + 1000000) - firstusec;
cf->esec--;
}
fdata->cinfo = &cf->cinfo;
for (i = 0; i < fdata->cinfo->num_cols; i++) {
fdata->cinfo->col_data[i][0] = '\0';
}
/* If either
we have a display filter and are re-applying it;
we have a list of color filters;
we have plugins to apply;
allocate a protocol tree root node, so that we'll construct
a protocol tree against which a filter expression can be
evaluated. */
if ((cf->dfcode != NULL && refilter) || filter_list != NULL
#ifdef HAVE_PLUGINS
|| enabled_plugins_number > 0
#endif
)
protocol_tree = proto_tree_create_root();
/* Dissect the frame. */
dissect_packet(pseudo_header, buf, fdata, protocol_tree);
/* If we have a display filter, apply it if we're refiltering, otherwise
leave the "passed_dfilter" flag alone.
If we don't have a display filter, set "passed_dfilter" to 1. */
if (cf->dfcode != NULL) {
if (refilter) {
if (cf->dfcode != NULL)
fdata->flags.passed_dfilter = dfilter_apply(cf->dfcode, protocol_tree, buf, fdata->cap_len) ? 1 : 0;
else
fdata->flags.passed_dfilter = 1;
}
} else
fdata->flags.passed_dfilter = 1;
/* If we have color filters, and the frame is to be displayed, apply
the color filters. */
if (fdata->flags.passed_dfilter) {
if (filter_list != NULL) {
args.protocol_tree = protocol_tree;
args.pd = buf;
args.fdata = fdata;
g_slist_foreach(filter_list, apply_color_filter, &args);
}
}
/* There are no more filters to apply, so we don't need any protocol
tree; free it if we created it. */
if (protocol_tree != NULL)
proto_tree_free(protocol_tree);
if (fdata->flags.passed_dfilter) {
/* This frame passed the display filter, so add it to the clist. */
/* If we don't have the time stamp of the previous displayed packet,
it's because this is the first displayed packet. Save the time
stamp of this packet as the time stamp of the previous displayed
packet. */
if (!prevsec && !prevusec) {
prevsec = fdata->abs_secs;
prevusec = fdata->abs_usecs;
}
/* Get the time elapsed between the first packet and this packet. */
fdata->rel_secs = cf->esec;
fdata->rel_usecs = cf->eusec;
/* Get the time elapsed between the previous displayed packet and
this packet. */
fdata->del_secs = fdata->abs_secs - prevsec;
if (prevusec <= fdata->abs_usecs) {
fdata->del_usecs = fdata->abs_usecs - prevusec;
} else {
fdata->del_usecs = (fdata->abs_usecs + 1000000) - prevusec;
fdata->del_secs--;
}
prevsec = fdata->abs_secs;
prevusec = fdata->abs_usecs;
fill_in_columns(fdata);
/* If we haven't yet seen the first frame, this is it.
XXX - we must do this before we add the row to the display,
as, if the display's GtkCList's selection mode is
GTK_SELECTION_BROWSE, when the first entry is added to it,
"select_packet()" will be called, and it will fetch the row
data for the 0th row, and will get a null pointer rather than
"fdata", as "gtk_clist_append()" won't yet have returned and
thus "gtk_clist_set_row_data()" won't yet have been called.
We thus need to leave behind bread crumbs so that
"select_packet()" can find this frame. See the comment
in "select_packet()". */
if (cf->first_displayed == NULL)
cf->first_displayed = fdata;
/* This is the last frame we've seen so far. */
cf->last_displayed = fdata;
row = gtk_clist_append(GTK_CLIST(packet_list), fdata->cinfo->col_data);
gtk_clist_set_row_data(GTK_CLIST(packet_list), row, fdata);
if (fdata->flags.marked) {
gtk_clist_set_background(GTK_CLIST(packet_list), row, &prefs.gui_marked_bg);
gtk_clist_set_foreground(GTK_CLIST(packet_list), row, &prefs.gui_marked_fg);
} else if (filter_list != NULL && (args.colorf != NULL)) {
gtk_clist_set_background(GTK_CLIST(packet_list), row,
&args.colorf->bg_color);
gtk_clist_set_foreground(GTK_CLIST(packet_list), row,
&args.colorf->fg_color);
} else {
gtk_clist_set_background(GTK_CLIST(packet_list), row, &WHITE);
gtk_clist_set_foreground(GTK_CLIST(packet_list), row, &BLACK);
}
} else {
/* This frame didn't pass the display filter, so it's not being added
to the clist, and thus has no row. */
row = -1;
}
fdata->cinfo = NULL;
return row;
}
static void
read_packet(capture_file *cf, int offset)
{
const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
const u_char *buf = wtap_buf_ptr(cf->wth);
frame_data *fdata;
int passed;
proto_tree *protocol_tree;
frame_data *plist_end;
/* Allocate the next list entry, and add it to the list. */
fdata = g_mem_chunk_alloc(cf->plist_chunk);
fdata->next = NULL;
fdata->prev = NULL;
fdata->pfd = NULL;
fdata->pkt_len = phdr->len;
fdata->cap_len = phdr->caplen;
fdata->file_off = offset;
fdata->lnk_t = phdr->pkt_encap;
fdata->abs_secs = phdr->ts.tv_sec;
fdata->abs_usecs = phdr->ts.tv_usec;
fdata->flags.encoding = CHAR_ASCII;
fdata->flags.visited = 0;
fdata->flags.marked = 0;
fdata->cinfo = NULL;
passed = TRUE;
if (cf->rfcode) {
protocol_tree = proto_tree_create_root();
dissect_packet(pseudo_header, buf, fdata, protocol_tree);
passed = dfilter_apply(cf->rfcode, protocol_tree, buf, fdata->cap_len);
proto_tree_free(protocol_tree);
}
if (passed) {
plist_end = cf->plist_end;
fdata->prev = plist_end;
if (plist_end != NULL)
plist_end->next = fdata;
else
cf->plist = fdata;
cf->plist_end = fdata;
cf->count++;
fdata->num = cf->count;
add_packet_to_packet_list(fdata, cf, pseudo_header, buf, TRUE);
} else {
/* XXX - if we didn't have read filters, or if we could avoid
allocating the "frame_data" structure until we knew whether
the frame passed the read filter, we could use a G_ALLOC_ONLY
memory chunk...
...but, at least in one test I did, where I just made the chunk
a G_ALLOC_ONLY chunk and read in a huge capture file, it didn't
seem to save a noticeable amount of time or space. */
g_mem_chunk_free(cf->plist_chunk, fdata);
}
}
int
filter_packets(capture_file *cf, gchar *dftext)
{
dfilter *dfcode;
if (dftext == NULL) {
/* The new filter is an empty filter (i.e., display all packets). */
dfcode = NULL;
} else {
/*
* We have a filter; try to compile it.
*/
if (dfilter_compile(dftext, &dfcode) != 0) {
/* The attempt failed; report an error. */
simple_dialog(ESD_TYPE_CRIT, NULL, dfilter_error_msg);
return 0;
}
/* Was it empty? */
if (dfcode == NULL) {
/* Yes - free the filter text, and set it to null. */
g_free(dftext);
dftext = NULL;
}
}
/* We have a valid filter. Replace the current filter. */
if (cf->dfilter != NULL)
g_free(cf->dfilter);
cf->dfilter = dftext;
if (cf->dfcode != NULL)
dfilter_destroy(cf->dfcode);
cf->dfcode = dfcode;
/* Now rescan the packet list, applying the new filter, but not
throwing away information constructed on a previous pass. */
rescan_packets(cf, "Filtering", TRUE, FALSE);
return 1;
}
void
colorize_packets(capture_file *cf)
{
rescan_packets(cf, "Colorizing", FALSE, FALSE);
}
void
redissect_packets(capture_file *cf)
{
rescan_packets(cf, "Reprocessing", TRUE, TRUE);
}
/* Rescan the list of packets, reconstructing the CList.
"action" describes why we're doing this; it's used in the progress
dialog box.
"refilter" is TRUE if we need to re-evaluate the filter expression.
"redissect" is TRUE if we need to make the dissectors reconstruct
any state information they have (because a preference that affects
some dissector has changed, meaning some dissector might construct
its state differently from the way it was constructed the last time). */
static void
rescan_packets(capture_file *cf, const char *action, gboolean refilter,
gboolean redissect)
{
frame_data *fdata;
progdlg_t *progbar;
gboolean stop_flag;
guint32 progbar_quantum;
guint32 progbar_nextstep;
int count;
frame_data *selected_frame;
int selected_row;
int row;
/* Which frame, if any, is the currently selected frame?
XXX - should the selected frame or the focus frame be the "current"
frame, that frame being the one from which "Find Frame" searches
start? */
selected_frame = cf->current_frame;
/* We don't yet know what row that frame will be on, if any, after we
rebuild the clist, however. */
selected_row = -1;
if (redissect) {
/* We need to re-initialize all the state information that protocols
keep, because some preference that controls a dissector has changed,
which might cause the state information to be constructed differently
by that dissector. */
/* Initialize the table of conversations. */
conversation_init();
/* Initialize protocol-specific variables */
init_all_protocols();
}
/* Freeze the packet list while we redo it, so we don't get any
screen updates while it happens. */
gtk_clist_freeze(GTK_CLIST(packet_list));
/* Clear it out. */
gtk_clist_clear(GTK_CLIST(packet_list));
/* We don't yet know which will be the first and last frames displayed. */
cf->first_displayed = NULL;
cf->last_displayed = NULL;
/* Iterate through the list of frames. Call a routine for each frame
to check whether it should be displayed and, if so, add it to
the display list. */
firstsec = 0;
firstusec = 0;
prevsec = 0;
prevusec = 0;
/* Update the progress bar when it gets to this value. */
progbar_nextstep = 0;
/* When we reach the value that triggers a progress bar update,
bump that value by this amount. */
progbar_quantum = cf->count/N_PROGBAR_UPDATES;
/* Count of packets at which we've looked. */
count = 0;
stop_flag = FALSE;
progbar = create_progress_dlg(action, "Stop", &stop_flag);
for (fdata = cf->plist; fdata != NULL; fdata = fdata->next) {
/* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
when we update it, we have to run the GTK+ main loop to get it
to repaint what's pending, and doing so may involve an "ioctl()"
to see if there's any pending input from an X server, and doing
that for every packet can be costly, especially on a big file. */
if (count >= progbar_nextstep) {
/* let's not divide by zero. I should never be started
* with count == 0, so let's assert that
*/
g_assert(cf->count > 0);
update_progress_dlg(progbar, (gfloat) count / cf->count);
progbar_nextstep += progbar_quantum;
}
if (stop_flag) {
/* Well, the user decided to abort the filtering. Just stop.
XXX - go back to the previous filter? Users probably just
want not to wait for a filtering operation to finish;
unless we cancel by having no filter, reverting to the
previous filter will probably be even more expensive than
continuing the filtering, as it involves going back to the
beginning and filtering, and even with no filter we currently
have to re-generate the entire clist, which is also expensive.
I'm not sure what Network Monitor does, but it doesn't appear
to give you an unfiltered display if you cancel. */
break;
}
count++;
if (redissect) {
/* Since all state for the frame was destroyed, mark the frame
* as not visited, and null out the pointer to the per-frame
* data (the per-frame data itself was freed by
* "init_all_protocols()"). */
fdata->flags.visited = 0;
fdata->pfd = NULL;
}
wtap_seek_read (cf->wth, fdata->file_off, &cf->pseudo_header,
cf->pd, fdata->cap_len);
row = add_packet_to_packet_list(fdata, cf, &cf->pseudo_header, cf->pd,
refilter);
if (fdata == selected_frame)
selected_row = row;
}
if (redissect) {
/* Clear out what remains of the visited flags and per-frame data
pointers.