forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
2995 lines (2576 loc) · 89.7 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.354 2004/01/31 04:10:04 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_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 <ctype.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
#include <epan/epan.h>
#include <epan/filesystem.h>
#include "color.h"
#include "column.h"
#include <epan/packet.h>
#include "range.h"
#include "print.h"
#include "file.h"
#include "menu.h"
#include "util.h"
#include "simple_dialog.h"
#include "progress_dlg.h"
#include "ui_util.h"
#include "statusbar.h"
#include "prefs.h"
#include <epan/dfilter/dfilter.h>
#include <epan/conversation.h>
#include "globals.h"
#include <epan/epan_dissect.h>
#include "tap.h"
#include "tap_dfilter_dlg.h"
#include "packet-data.h"
#ifdef HAVE_LIBPCAP
gboolean auto_scroll_live;
#endif
static guint32 firstsec, firstusec;
static guint32 prevsec, prevusec;
static guint32 cum_bytes = 0;
static void read_packet(capture_file *cf, long offset);
static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
gboolean refilter, gboolean redissect);
static gboolean match_protocol_tree(capture_file *cf, frame_data *fdata,
void *criterion);
static void match_subtree_text(proto_node *node, gpointer data);
static gboolean match_summary_line(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean match_ascii(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean match_unicode(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean match_binary(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean match_dfilter(capture_file *cf, frame_data *fdata,
void *criterion);
static gboolean find_packet(capture_file *cf,
gboolean (*match_function)(capture_file *, frame_data *, void *),
void *criterion);
static char *cf_open_error_message(int err, gchar *err_info,
gboolean for_writing, int file_type);
static char *file_rename_error_message(int err);
static char *cf_write_error_message(int);
static char *cf_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
typedef struct {
int level;
FILE *fh;
GSList *src_list;
gboolean print_all_levels;
gboolean print_hex_for_data;
char_enc encoding;
gint format; /* text or PostScript */
} print_data;
int
cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
{
wtap *wth;
int err;
gchar *err_info;
int fd;
struct stat cf_stat;
wth = wtap_open_offline(fname, &err, &err_info, TRUE);
if (wth == NULL)
goto fail;
/* Find the size of the file. */
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. */
cf_close(cf);
/* Initialize all data structures used for dissection. */
init_dissection();
/* 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->marked_count = 0;
cf->drops_known = FALSE;
cf->drops = 0;
cf->esec = 0;
cf->eusec = 0;
cf->snap = wtap_snapshot_length(cf->wth);
if (cf->snap == 0) {
/* Snapshot length not known. */
cf->has_snap = FALSE;
cf->snap = WTAP_MAX_PACKET_SIZE;
} else
cf->has_snap = TRUE;
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_ERROR, ESD_BTN_OK,
cf_open_error_message(err, err_info, FALSE, 0), fname);
return (err);
}
/* Reset everything to a pristine state */
void
cf_close(capture_file *cf)
{
/* Die if we're in the middle of reading a file. */
g_assert(cf->state != FILE_READ_IN_PROGRESS);
/* Destroy all windows, which refer to the
capture file we're closing. */
destroy_cfile_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_free(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;
/* No frame selected, no field in that frame selected. */
cf->current_frame = NULL;
cf->finfo_selected = NULL;
/* Clear the packet list. */
packet_list_freeze();
packet_list_clear();
packet_list_thaw();
/* 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? */
statusbar_pop_file_msg();
/* 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(cf);
set_menus_for_capture_in_progress(FALSE);
set_menus_for_selected_tree_row(cf);
/* 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;
static const gchar done_fmt_nodrops[] = " File: %s";
static const gchar done_fmt_drops[] = " File: %s Drops: %u";
gchar *done_msg;
gchar *win_name_fmt = "%s - Ethereal";
gchar *win_name;
name_ptr = cf_get_display_name(cf);
if (!cf->is_tempfile) {
/* Add this filename to the list of recent files in the "Recent Files" submenu */
add_menu_recent_capture_file(cf->filename);
}
if (cf->drops_known) {
msg_len = strlen(name_ptr) + strlen(done_fmt_drops) + 64;
done_msg = g_malloc(msg_len);
snprintf(done_msg, msg_len, done_fmt_drops, name_ptr, cf->drops);
} else {
msg_len = strlen(name_ptr) + strlen(done_fmt_nodrops);
done_msg = g_malloc(msg_len);
snprintf(done_msg, msg_len, done_fmt_nodrops, name_ptr);
}
statusbar_push_file_msg(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
cf_read(capture_file *cf)
{
int err;
gchar *err_info;
gchar *name_ptr, *load_msg, *load_fmt = "%s";
size_t msg_len;
char *errmsg;
char errmsg_errno[1024+1];
gchar err_str[2048+1];
long data_offset;
progdlg_t *progbar = NULL;
gboolean stop_flag;
/*
* XXX - should be "off_t", but Wiretap would need more work to handle
* the full size of "off_t" on platforms where it's more than a "long"
* as well.
*/
long file_pos;
float prog_val;
int fd;
struct stat cf_stat;
GTimeVal start_time;
gchar status_str[100];
int progbar_nextstep;
int progbar_quantum;
cum_bytes=0;
reset_tap_listeners();
tap_dfilter_dlg_update();
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);
statusbar_push_file_msg(load_msg);
/* 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->f_len/N_PROGBAR_UPDATES;
#ifndef O_BINARY
#define O_BINARY 0
#endif
packet_list_freeze();
stop_flag = FALSE;
g_get_current_time(&start_time);
while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
/* 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 >= progbar_nextstep) {
file_pos = lseek(cf->filed, 0, SEEK_CUR);
prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
if (prog_val > 1.0) {
/* The file probably grew while we were reading it.
Update "cf->f_len", and try again. */
fd = wtap_fd(cf->wth);
if (fstat(fd, &cf_stat) >= 0) {
cf->f_len = cf_stat.st_size;
prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
}
/* If it's still > 1, either the "fstat()" failed (in which
case there's not much we can do about it), or the file
*shrank* (in which case there's not much we can do about
it); just clip the progress value at 1.0. */
if (prog_val > 1.0)
prog_val = 1.0;
}
if (progbar == NULL) {
/* Create the progress bar if necessary */
progbar = delayed_create_progress_dlg("Loading", load_msg,
&stop_flag, &start_time, prog_val);
if (progbar != NULL)
g_free(load_msg);
}
if (progbar != NULL) {
g_snprintf(status_str, sizeof(status_str),
"%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
update_progress_dlg(progbar, prog_val, status_str);
}
progbar_nextstep += 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 */
packet_list_thaw(); /* undo our freeze */
cf_close(cf);
return (READ_ABORTED);
}
read_packet(cf, data_offset);
}
/* We're done reading the file; destroy the progress bar if it was created. */
if (progbar == NULL)
g_free(load_msg);
else
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);
/* Allow the protocol dissectors to free up memory that they
* don't need after the sequential run-through of the packets. */
postseq_cleanup_all_protocols();
/* 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;
packet_list_thaw();
statusbar_pop_file_msg();
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)
packet_list_select_row(0);
if (err != 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:
snprintf(errmsg_errno, sizeof(errmsg_errno),
"The capture file appears to be damaged or corrupt.\n(%s)",
err_info);
errmsg = errmsg_errno;
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_ERROR, ESD_BTN_OK, err_str);
return (READ_ERROR);
} else
return (READ_SUCCESS);
}
#ifdef HAVE_LIBPCAP
int
cf_start_tail(char *fname, gboolean is_tempfile, capture_file *cf)
{
int err;
err = cf_open(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);
statusbar_push_file_msg(" <live capture in progress>");
}
return err;
}
read_status_t
cf_continue_tail(capture_file *cf, int to_read, int *err)
{
long data_offset = 0;
gchar *err_info;
*err = 0;
packet_list_freeze();
while (to_read != 0 && (wtap_read(cf->wth, err, &err_info, &data_offset))) {
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--;
}
packet_list_thaw();
/* XXX - this cheats and looks inside the packet list to find the final
row number. */
if (auto_scroll_live && cf->plist_end != NULL)
packet_list_moveto_end();
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
"cf_finish_tail()" will be called, and it will clean up
and exit. */
return READ_ABORTED;
} else if (*err != 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
cf_finish_tail(capture_file *cf, int *err)
{
gchar *err_info;
long data_offset;
packet_list_freeze();
while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
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. */
cf_close(cf);
return READ_ABORTED;
}
packet_list_thaw();
if (auto_scroll_live && cf->plist_end != NULL)
/* XXX - this cheats and looks inside the packet list to find the final
row number. */
packet_list_moveto_end();
/* 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);
/* Allow the protocol dissectors to free up memory that they
* don't need after the sequential run-through of the packets. */
postseq_cleanup_all_protocols();
/* 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. */
statusbar_pop_file_msg();
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 (*err != 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 */
gchar *
cf_get_display_name(capture_file *cf)
{
gchar *displayname;
/* Return a name to use in displays */
if (!cf->is_tempfile) {
/* Get the last component of the file name, and use that. */
if (cf->filename){
displayname = get_basename(cf->filename);
/* Add this filename to the list of recent files in the "Recent Files" submenu */
add_menu_recent_capture_file(cf->filename);
} else {
displayname="<no file>";
}
} else {
/* The file we read is a temporary file from a live capture;
we don't mention its name. */
displayname = "<capture>";
}
return displayname;
}
typedef struct {
color_filter_t *colorf;
epan_dissect_t *edt;
} 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_edt(colorf->c_colorfilter, args->edt))
args->colorf = colorf;
}
}
static int
add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
union wtap_pseudo_header *pseudo_header, const guchar *buf,
gboolean refilter)
{
apply_color_filter_args args;
gint row;
gboolean create_proto_tree = FALSE;
epan_dissect_t *edt;
/* just add some value here until we know if it is being displayed or not */
fdata->cum_bytes = cum_bytes + fdata->pkt_len;
/* 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;
}
/* if this frames is marked as a reference time frame, reset
firstsec and firstusec to this frame */
if(fdata->flags.ref_time){
firstsec = fdata->abs_secs;
firstusec = fdata->abs_usecs;
}
/* 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. */
compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
/* If it's greater than the current elapsed time, set the elapsed time
to it (we check for "greater than" so as not to be confused by
time moving backwards). */
if ((gint32)cf->esec < fdata->rel_secs
|| ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
cf->esec = fdata->rel_secs;
cf->eusec = fdata->rel_usecs;
}
/* Get the time elapsed between the previous displayed packet and
this packet. */
compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
/* If either
we have a display filter and are re-applying it;
we have a list of color filters;
we have tap listeners;
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
|| num_tap_filters != 0)
create_proto_tree = TRUE;
/* Dissect the frame. */
edt = epan_dissect_new(create_proto_tree, FALSE);
if (cf->dfcode != NULL && refilter) {
epan_dissect_prime_dfilter(edt, cf->dfcode);
}
if (filter_list) {
filter_list_prime_edt(edt);
}
tap_queue_init(edt);
epan_dissect_run(edt, pseudo_header, buf, fdata, &cf->cinfo);
tap_push_tapped_queue(edt);
/* 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_edt(cf->dfcode, edt) ? 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.edt = edt;
g_slist_foreach(filter_list, apply_color_filter, &args);
}
}
if( (fdata->flags.passed_dfilter)
|| (edt->pi.fd->flags.ref_time) ){
/* This frame either passed the display filter list or is marked as
a time reference frame. All time reference frames are displayed
even if they dont pass the display filter */
/* if this was a TIME REF frame we should reset the cul bytes field */
if(edt->pi.fd->flags.ref_time){
cum_bytes = fdata->pkt_len;
fdata->cum_bytes = cum_bytes;
}
/* increase cum_bytes with this packets length */
cum_bytes += fdata->pkt_len;
epan_dissect_fill_in_columns(edt);
/* 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 = packet_list_append(cf->cinfo.col_data, fdata);
/* If the packet matches a color filter,
* store matching color_filter_t object in frame data. */
if (filter_list != NULL && (args.colorf != NULL)) {
/* add the matching colorfilter to the frame data */
fdata->color_filter = args.colorf;
/* If packet is marked, use colors from preferences */
if (fdata->flags.marked) {
packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
} else /* if (filter_list != NULL && (args.colorf != NULL)) */ {
packet_list_set_colors(row, &(args.colorf->fg_color),
&(args.colorf->bg_color));
}
} else {
/* No color filter match */
fdata->color_filter = NULL;
if (fdata->flags.marked) {
packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
}
}
/* Set the time of the previous displayed frame to the time of this
frame. */
prevsec = fdata->abs_secs;
prevusec = fdata->abs_usecs;
} 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;
}
epan_dissect_free(edt);
return row;
}
static void
read_packet(capture_file *cf, long offset)
{
const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
const guchar *buf = wtap_buf_ptr(cf->wth);
frame_data *fdata;
int passed;
frame_data *plist_end;
epan_dissect_t *edt;
/* 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->flags.ref_time = 0;
passed = TRUE;
if (cf->rfcode) {
edt = epan_dissect_new(TRUE, FALSE);
epan_dissect_prime_dfilter(edt, cf->rfcode);
epan_dissect_run(edt, pseudo_header, buf, fdata, NULL);
passed = dfilter_apply_edt(cf->rfcode, edt);
epan_dissect_free(edt);
}
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_t *dfcode;
char *filter_new = dftext ? dftext : "";
char *filter_old = cf->dfilter ? cf->dfilter : "";
/* if new filter equals old one, do nothing */
if (strcmp(filter_new, filter_old) == 0) {
return 1;
}
if (dftext == NULL) {
/* The new filter is an empty filter (i.e., display all packets). */
dfcode = NULL;
} else {
/*
* We have a filter; make a copy of it (as we'll be saving it),
* and try to compile it.
*/
dftext = g_strdup(dftext);
if (!dfilter_compile(dftext, &dfcode)) {
/* The attempt failed; report an error. */
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, dfilter_error_msg);
g_free(dftext);
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_free(cf->dfcode);
cf->dfcode = dfcode;
/* Now rescan the packet list, applying the new filter, but not
throwing away information constructed on a previous pass. */
if (dftext == NULL) {
rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
} else {
rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
}
return 1;
}
void
colorize_packets(capture_file *cf)
{
rescan_packets(cf, "Colorizing", "all packets", FALSE, FALSE);
}
void