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