add Merge functionality to Ethereal in an experimental state.
[metze/wireshark/wip.git] / merge.c
1 /* Combine two dump files, either by appending or by merging by timestamp
2  *
3  * $Id: merge.c,v 1.1 2004/06/17 21:53:25 ulfl Exp $
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
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25
26 #include <string.h>
27 #include "wtap.h"
28
29 #ifdef NEED_GETOPT_H
30 #include "getopt.h"
31 #endif
32
33 #include "cvsversion.h"
34
35 /*
36  * Global variables
37  */
38 static int verbose = 0;                      /* Not so verbose         */
39
40 /*
41  * Structures to manage our files
42  */
43 typedef struct in_file_t {
44   const char *filename;
45   wtap       *wth;
46   int         err;
47   gchar      *err_info;
48   long        data_offset;
49   gboolean    ok;
50 } in_file_t;
51
52 typedef struct out_file_t {
53   const char  *filename;
54   wtap_dumper *pdh;
55   int          file_type;
56   int          frame_type;
57   unsigned int snaplen;
58   int          count;
59 } out_file_t;
60 static out_file_t out_file;
61
62 /*
63  * Routine to write frame to output file
64  */
65 static gboolean
66 write_frame(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
67             union wtap_pseudo_header *pseudo_header, const guchar *buf)
68 {
69   wtap_dumper *pdh = (wtap_dumper*)user;
70   int err;
71   struct wtap_pkthdr snap_phdr;
72
73   if (verbose)
74     printf("Record: %u\n", out_file.count++);
75
76   /* We simply write it, perhaps after truncating it; we could do other
77    * things, like modify it. */
78   if (out_file.snaplen != 0 && phdr->caplen > out_file.snaplen) {
79     snap_phdr = *phdr;
80     snap_phdr.caplen = out_file.snaplen;
81     phdr = &snap_phdr;
82   }
83
84   if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
85     fprintf(stderr, "mergecap: Error writing to %s: %s\n",
86             out_file.filename, wtap_strerror(err));
87     return FALSE;
88   }
89
90   return TRUE;
91 }
92
93
94 static gboolean
95 append_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
96     gchar **err_info)
97 {
98         long            data_offset;
99         int             loop = 0;
100
101         /* Start by clearing error flag */
102         *err = 0;
103
104         while ( (wtap_read(wth, err, err_info, &data_offset)) ) {
105                 if(!write_frame(user, wtap_phdr(wth), data_offset,
106                     wtap_pseudoheader(wth), wtap_buf_ptr(wth)))
107             return FALSE;
108                 if (count > 0 && ++loop >= count)
109                         break;
110         }
111
112     if (*err == 0) {
113                 return TRUE;    /* success */
114     } else {
115                 return FALSE;   /* failure */
116     }
117 }
118
119
120
121 /*
122  * routine to concatenate files
123  */
124 static void
125 append_files(int count, in_file_t in_files[], out_file_t *out_file)
126 {
127   int i;
128   int err;
129   gchar *err_info;
130
131   for (i = 0; i < count; i++) {
132     if (!append_loop(in_files[i].wth, 0, write_frame,
133                    (guchar*)out_file->pdh, &err, &err_info)) {
134           fprintf(stderr, "mergecap: Error appending %s to %s: %s\n",
135                   in_files[i].filename, out_file->filename, wtap_strerror(err));
136           switch (err) {
137
138           case WTAP_ERR_UNSUPPORTED:
139           case WTAP_ERR_UNSUPPORTED_ENCAP:
140           case WTAP_ERR_BAD_RECORD:
141             fprintf(stderr, "(%s)\n", err_info);
142
143             break;
144       }
145     }
146   }
147 }
148
149
150 /*
151  * returns TRUE if first argument is earlier than second
152  */
153 static gboolean
154 is_earlier(struct timeval *l, struct timeval *r) {
155   if (l->tv_sec > r->tv_sec) {  /* left is later */
156     return FALSE;
157   } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
158     return TRUE;
159   } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
160     return FALSE;
161   }
162   /* either one < two or one == two
163    * either way, return one
164    */
165   return TRUE;
166 }
167
168
169 /*
170  * returns index of earliest timestamp in set of input files
171  * or -1 if no valid files remain
172  */
173 static int
174 earliest(int count, in_file_t in_files[]) {
175   int i;
176   int ei = -1;
177   struct timeval tv = {LONG_MAX, LONG_MAX};
178
179   for (i = 0; i < count; i++) {
180     struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
181
182     if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
183       tv = phdr->ts;
184       ei = i;
185     }
186   }
187   return ei;
188 }
189
190 /*
191  * actually merge the files
192  */
193 static gboolean
194 merge(int count, in_file_t in_files[], out_file_t *out_file)
195 {
196   int i;
197
198   /* prime the pump (read in first frame from each file) */
199   for (i = 0; i < count; i++) {
200     in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
201                                &(in_files[i].err_info),
202                                &(in_files[i].data_offset));
203   }
204
205   /* now keep writing the earliest frame until we're out of frames */
206   while ( -1 != (i = earliest(count, in_files))) {
207
208     /* write out earliest frame, and fetch another from its
209      * input file
210      */
211     if(!write_frame((guchar*)out_file->pdh,
212                 wtap_phdr(in_files[i].wth),
213                 in_files[i].data_offset,
214                 wtap_pseudoheader(in_files[i].wth),
215                 wtap_buf_ptr(in_files[i].wth)))
216                 return FALSE;
217     in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
218                                &(in_files[i].err_info),
219                                &(in_files[i].data_offset));
220   }
221
222   return TRUE;
223 }
224
225
226 /*
227  * Select an output frame type based on the input files
228  * From Guy: If all files have the same frame type, then use that.
229  *           Otherwise select WTAP_ENCAP_PER_PACKET.  If the selected
230  *           output file type doesn't support per packet frame types,
231  *           then the wtap_dump_open call will fail with a reasonable
232  *           error condition.
233  */
234 static int
235 select_frame_type(int count, in_file_t files[])
236 {
237   int i;
238   int selected_frame_type;
239
240   selected_frame_type = wtap_file_encap(files[0].wth);
241
242   for (i = 1; i < count; i++) {
243     int this_frame_type = wtap_file_encap(files[i].wth);
244     if (selected_frame_type != this_frame_type) {
245       selected_frame_type = WTAP_ENCAP_PER_PACKET;
246       if (verbose) {
247         fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
248         fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
249         fprintf(stderr, "          %s had type %s (%s)\n",
250                 files[0].filename,
251                 wtap_encap_string(selected_frame_type),
252                 wtap_encap_short_string(selected_frame_type));
253         fprintf(stderr, "          %s had type %s (%s)\n",
254                 files[i].filename,
255                 wtap_encap_string(this_frame_type),
256                 wtap_encap_short_string(this_frame_type));
257       }
258       break;
259     }
260   }
261
262   if (verbose) {
263       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
264               wtap_encap_string(selected_frame_type),
265               wtap_encap_short_string(selected_frame_type));
266   }
267
268   return selected_frame_type;
269 }
270
271
272 /*
273  * Close the output file
274  */
275 static void
276 close_outfile(out_file_t *out_file)
277 {
278   int err;
279   if (!wtap_dump_close(out_file->pdh, &err)) {
280     fprintf(stderr, "mergecap: Error closing file %s: %s\n",
281             out_file->filename, wtap_strerror(err));
282   }
283 }
284
285
286 /*
287  * Open the output file
288  *
289  * Return FALSE if file cannot be opened (so caller can clean up)
290  */
291 static gboolean
292 open_outfile(out_file_t *out_file, int snapshot_len)
293 {
294   int err;
295
296   if (!out_file) {
297     fprintf(stderr, "mergecap: internal error (null out_file)\n");
298     return FALSE;
299   }
300
301   /* Allow output to stdout by using - */
302   if (strncmp(out_file->filename, "-", 2) == 0)
303     out_file->filename = "";
304
305
306   out_file->pdh = wtap_dump_open(out_file->filename, out_file->file_type,
307                                  out_file->frame_type, snapshot_len, &err);
308   if (!out_file->pdh) {
309     fprintf(stderr, "mergecap: Can't open/create %s:\n", out_file->filename);
310     fprintf(stderr, "          %s\n", wtap_strerror(err));
311     return FALSE;
312   }
313   return TRUE;
314 }
315
316
317 /*
318  * Scan through input files and find maximum snapshot length
319  */
320 static int
321 max_snapshot_length(int count, in_file_t in_files[])
322 {
323   int i;
324   int max_snapshot = 0;
325   int snapshot_length;
326
327   for (i = 0; i < count; i++) {
328     snapshot_length = wtap_snapshot_length(in_files[i].wth);
329     if (snapshot_length == 0) {
330       /* Snapshot length of input file not known. */
331       snapshot_length = WTAP_MAX_PACKET_SIZE;
332     }
333     if (snapshot_length > max_snapshot)
334       max_snapshot = snapshot_length;
335   }
336   return max_snapshot;
337 }
338
339
340 /*
341  * Scan through and close each input file
342  */
343 static void
344 close_in_files(int count, in_file_t in_files[])
345 {
346   int i;
347   for (i = 0; i < count; i++) {
348     wtap_close(in_files[i].wth);
349   }
350 }
351
352
353 /*
354  * Scan through the arguments and open the input files
355  */
356 static int
357 open_in_files(int in_file_count, char *argv[], in_file_t *in_files[])
358 {
359   int i;
360   int count = 0;
361   int err;
362   gchar *err_info;
363   in_file_t *files;
364   int files_size = in_file_count * sizeof(in_file_t);
365
366
367   files = g_malloc(files_size);
368   *in_files = files;
369
370   for (i = 0; i < in_file_count; i++) {
371     files[count].filename    = argv[i];
372     files[count].wth         = wtap_open_offline(argv[i], &err, &err_info, FALSE);
373     files[count].err         = 0;
374     files[count].data_offset = 0;
375     files[count].ok          = TRUE;
376     if (!files[count].wth) {
377       fprintf(stderr, "mergecap: skipping %s: %s\n", argv[i],
378               wtap_strerror(err));
379       switch (err) {
380
381       case WTAP_ERR_UNSUPPORTED:
382       case WTAP_ERR_UNSUPPORTED_ENCAP:
383       case WTAP_ERR_BAD_RECORD:
384         fprintf(stderr, "(%s)\n", err_info);
385         g_free(err_info);
386         break;
387       }
388     } else {
389       if (verbose) {
390         fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
391                 wtap_file_type_string(wtap_file_type(files[count].wth)));
392       }
393       count++;
394     }
395   }
396   if (verbose)
397     fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
398     in_file_count);
399
400   return count;
401 }
402
403
404 gboolean
405 merge_two_files(char *out_filename, char *in_file0, char *in_file1, gboolean do_append)
406 {
407   extern char *optarg;
408   extern int   optind;
409   int          in_file_count = 0;
410   in_file_t   *in_files      = NULL;
411   char        *in_filenames[2];
412
413   /* initialize out_file */
414   out_file.filename   = out_filename;
415   out_file.pdh        = NULL;              /* wiretap dumpfile */
416   out_file.file_type  = WTAP_FILE_PCAP;    /* default to "libpcap" */
417   out_file.frame_type = -2;                /* leave type alone */
418   out_file.snaplen    = 0;                 /* no limit */
419   out_file.count      = 1;                 /* frames output */
420
421   /* check for proper args; at a minimum, must have an output
422    * filename and one input file
423    */
424   in_file_count = 2;
425
426   in_filenames[0] = in_file0;
427   in_filenames[1] = in_file1;
428
429   /* open the input files */
430   in_file_count = open_in_files(in_file_count, in_filenames, &in_files);
431   if (in_file_count < 1) {
432     fprintf(stderr, "mergecap: No valid input files\n");
433     return FALSE;
434   }
435
436   /* set the outfile frame type */
437   if (out_file.frame_type == -2)
438     out_file.frame_type = select_frame_type(in_file_count, in_files);
439
440   /* open the outfile */
441   if (!open_outfile(&out_file, max_snapshot_length(in_file_count, in_files))) {
442     close_in_files(in_file_count, in_files);
443     return FALSE;
444   }
445
446   /* do the merge (or append) */
447   if (do_append)
448     append_files(in_file_count, in_files, &out_file);
449   else
450     merge(in_file_count, in_files, &out_file);
451
452   close_in_files(in_file_count, in_files);
453   close_outfile(&out_file);
454
455   free(in_files);
456
457   return TRUE;
458 }