Install the new menu files.
[obnox/wireshark/wip.git] / merge.c
1 /* Combine multiple dump files, either by appending or by merging by timestamp
2  *
3  * $Id$
4  *
5  * Written by Scott Renfro <scott@renfro.org> based on
6  * editcap by Richard Sharpe and Guy Harris
7  *
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <glib.h>
17 #include <errno.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <string.h>
28 #include "wtap.h"
29 #include "merge.h"
30
31 /*
32  * Scan through the arguments and open the input files
33  */
34 gboolean
35 merge_open_in_files(int in_file_count, char *const *in_file_names,
36                     merge_in_file_t **in_files, int *err, gchar **err_info,
37                     int *err_fileno)
38 {
39   int i, j;
40   size_t files_size = in_file_count * sizeof(merge_in_file_t);
41   merge_in_file_t *files;
42   gint64 size;
43
44   files = (merge_in_file_t *)g_malloc(files_size);
45   *in_files = files;
46
47   for (i = 0; i < in_file_count; i++) {
48     files[i].filename    = in_file_names[i];
49     files[i].wth         = wtap_open_offline(in_file_names[i], err, err_info, FALSE);
50     files[i].data_offset = 0;
51     files[i].state       = PACKET_NOT_PRESENT;
52     if (!files[i].wth) {
53       /* Close the files we've already opened. */
54       for (j = 0; j < i; j++)
55         wtap_close(files[j].wth);
56       *err_fileno = i;
57       return FALSE;
58     }
59     size = wtap_file_size(files[i].wth, err);
60     if (size == -1) {
61       for (j = 0; j <= i; j++)
62         wtap_close(files[j].wth);
63       *err_fileno = i;
64       return FALSE;
65     }
66     files[i].size = size;
67   }
68   return TRUE;
69 }
70
71 /*
72  * Scan through and close each input file
73  */
74 void
75 merge_close_in_files(int count, merge_in_file_t in_files[])
76 {
77   int i;
78   for (i = 0; i < count; i++) {
79     wtap_close(in_files[i].wth);
80   }
81 }
82
83 /*
84  * Select an output frame type based on the input files
85  * From Guy: If all files have the same frame type, then use that.
86  *           Otherwise select WTAP_ENCAP_PER_PACKET.  If the selected
87  *           output file type doesn't support per packet frame types,
88  *           then the wtap_dump_open call will fail with a reasonable
89  *           error condition.
90  */
91 int
92 merge_select_frame_type(int count, merge_in_file_t files[])
93 {
94   int i;
95   int selected_frame_type;
96
97   selected_frame_type = wtap_file_encap(files[0].wth);
98
99   for (i = 1; i < count; i++) {
100     int this_frame_type = wtap_file_encap(files[i].wth);
101     if (selected_frame_type != this_frame_type) {
102       selected_frame_type = WTAP_ENCAP_PER_PACKET;
103       break;
104     }
105   }
106
107   return selected_frame_type;
108 }
109
110 /*
111  * Scan through input files and find maximum snapshot length
112  */
113 int
114 merge_max_snapshot_length(int count, merge_in_file_t in_files[])
115 {
116   int i;
117   int max_snapshot = 0;
118   int snapshot_length;
119
120   for (i = 0; i < count; i++) {
121     snapshot_length = wtap_snapshot_length(in_files[i].wth);
122     if (snapshot_length == 0) {
123       /* Snapshot length of input file not known. */
124       snapshot_length = WTAP_MAX_PACKET_SIZE;
125     }
126     if (snapshot_length > max_snapshot)
127       max_snapshot = snapshot_length;
128   }
129   return max_snapshot;
130 }
131
132 /*
133  * returns TRUE if first argument is earlier than second
134  */
135 static gboolean
136 is_earlier(struct wtap_nstime *l, struct wtap_nstime *r) {
137   if (l->secs > r->secs) {  /* left is later */
138     return FALSE;
139   } else if (l->secs < r->secs) { /* left is earlier */
140     return TRUE;
141   } else if (l->nsecs > r->nsecs) { /* tv_sec equal, l.usec later */
142     return FALSE;
143   }
144   /* either one < two or one == two
145    * either way, return one
146    */
147   return TRUE;
148 }
149
150 /*
151  * Read the next packet, in chronological order, from the set of files
152  * to be merged.
153  */
154 wtap *
155 merge_read_packet(int in_file_count, merge_in_file_t in_files[], int *err,
156                   gchar **err_info)
157 {
158   int i;
159   int ei = -1;
160   struct wtap_nstime tv = { sizeof(time_t) > sizeof(int) ? LONG_MAX : INT_MAX, INT_MAX };
161   struct wtap_pkthdr *phdr;
162
163   /*
164    * Make sure we have a packet available from each file, if there are any
165    * packets left in the file in question, and search for the packet
166    * with the earliest time stamp.
167    */
168   for (i = 0; i < in_file_count; i++) {
169     if (in_files[i].state == PACKET_NOT_PRESENT) {
170       /*
171        * No packet available, and we haven't seen an error or EOF yet,
172        * so try to read the next packet.
173        */
174       if (!wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset)) {
175         if (*err != 0) {
176           in_files[i].state = GOT_ERROR;
177           return NULL;
178         }
179         in_files[i].state = AT_EOF;
180       } else
181         in_files[i].state = PACKET_PRESENT;
182     }
183     
184     if (in_files[i].state == PACKET_PRESENT) {
185       phdr = wtap_phdr(in_files[i].wth);
186       if (is_earlier(&phdr->ts, &tv)) {
187         tv = phdr->ts;
188         ei = i;
189       }
190     }
191   }
192
193   if (ei == -1) {
194     /* All the streams are at EOF.  Return an EOF indication. */
195     *err = 0;
196     return NULL;
197   }
198
199   /* We'll need to read another packet from this file. */
200   in_files[ei].state = PACKET_NOT_PRESENT;
201
202   /* Return a pointer to the wtap structure for the file with that frame. */
203   return in_files[ei].wth;
204 }
205
206 /*
207  * Read the next packet, in file sequence order, from the set of files
208  * to be merged.
209  */
210 wtap *
211 merge_append_read_packet(int in_file_count, merge_in_file_t in_files[],
212                          int *err, gchar **err_info)
213 {
214   int i;
215
216   /*
217    * Find the first file not at EOF, and read the next packet from it.
218    */
219   for (i = 0; i < in_file_count; i++) {
220     if (in_files[i].state == AT_EOF)
221       continue; /* This file is already at EOF */
222     if (wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset))
223       break; /* We have a packet */
224     if (*err != 0) {
225       /* Read error - quit immediately. */
226       in_files[i].state = GOT_ERROR;
227       return NULL;
228     }
229     /* EOF - flag this file as being at EOF, and try the next one. */
230     in_files[i].state = AT_EOF;
231   }
232   if (i == in_file_count) {
233     /* All the streams are at EOF.  Return an EOF indication. */
234     *err = 0;
235     return NULL;
236   }
237
238   /* Return a pointer to the wtap structure for the file with that frame. */
239   return in_files[i].wth;
240 }