From: Shashidhar Bhandare
[obnox/wireshark/wip.git] / mergecap.c
1 /* Combine two 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 <errno.h>
17 #include <glib.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
30 #ifdef NEED_GETOPT_H
31 #include "getopt.h"
32 #endif
33
34 #include "svnversion.h"
35 #include "merge.h"
36 #include "file_util.h"
37
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
41
42 static int
43 get_natural_int(const char *string, const char *name)
44 {
45   long number;
46   char *p;
47
48   number = strtol(string, &p, 10);
49   if (p == string || *p != '\0') {
50     fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
51             name, string);
52     exit(1);
53   }
54   if (number < 0) {
55     fprintf(stderr, "mergecap: The specified %s is a negative number\n",
56             name);
57     exit(1);
58   }
59   if (number > INT_MAX) {
60     fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
61             name, INT_MAX);
62     exit(1);
63   }
64   return number;
65 }
66
67 static int
68 get_positive_int(const char *string, const char *name)
69 {
70   long number;
71
72   number = get_natural_int(string, name);
73
74   if (number == 0) {
75     fprintf(stderr, "mergecap: The specified %s is zero\n",
76             name);
77     exit(1);
78   }
79
80   return number;
81 }
82
83 /*
84  * Show the usage
85  */
86 static void
87 usage(void)
88 {
89
90   fprintf(stderr, "Mergecap %s"
91 #ifdef SVNVERSION
92           " (" SVNVERSION ")"
93 #endif
94           "\n", VERSION);
95   fprintf(stderr, "Merge two or more capture files into one.\n");
96   fprintf(stderr, "See http://www.ethereal.com for more information.\n");
97   fprintf(stderr, "\n");
98   fprintf(stderr, "Usage: mergecap [options] -w <outfile|-> <infile> ...\n");
99   fprintf(stderr, "\n");
100   fprintf(stderr, "Output:\n");
101   fprintf(stderr, "  -a                files should be concatenated, not merged\n");
102   fprintf(stderr, "                    Default merges based on frame timestamps\n");
103   fprintf(stderr, "  -s <snaplen>      truncate packets to <snaplen> bytes of data\n");
104   fprintf(stderr, "  -w <outfile|->    set the output filename to <outfile> or '-' for stdout\n");
105   fprintf(stderr, "  -F <capture type> set the output file type, default is libpcap\n");
106   fprintf(stderr, "                    an empty \"-F\" option will list the file types\n");
107   fprintf(stderr, "  -T <encap type>   set the output file encapsulation type,\n");
108   fprintf(stderr, "                    default is the same as the first input file\n");
109   fprintf(stderr, "                    an empty \"-T\" option will list the encapsulation types\n");
110   fprintf(stderr, "\n");
111   fprintf(stderr, "Miscellaneous:\n");
112   fprintf(stderr, "  -h                display this help and exit\n");
113   fprintf(stderr, "  -v                verbose output\n");
114 }
115
116 static void list_capture_types(void) {
117     int i;
118
119     fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
120     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
121       if (wtap_dump_can_open(i))
122         fprintf(stderr, "    %s - %s\n",
123           wtap_file_type_short_string(i), wtap_file_type_string(i));
124     }
125 }
126
127 static void list_encap_types(void) {
128     int i;
129     const char *string;
130
131     fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
132     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
133         string = wtap_encap_short_string(i);
134         if (string != NULL)
135           fprintf(stderr, "    %s - %s\n",
136             string, wtap_encap_string(i));
137     }
138 }
139
140 int
141 main(int argc, char *argv[])
142 {
143   extern char *optarg;
144   extern int   optind;
145   int          opt;
146   gboolean     do_append     = FALSE;
147   gboolean     verbose       = FALSE;
148   int          in_file_count = 0;
149   guint        snaplen = 0;
150   int          file_type = WTAP_FILE_PCAP;      /* default to libpcap format */
151   int          frame_type = -2;
152   int          out_fd;
153   merge_in_file_t   *in_files      = NULL;
154   int          i;
155   wtap        *wth;
156   struct wtap_pkthdr *phdr, snap_phdr;
157   wtap_dumper *pdh;
158   int          open_err, read_err, write_err, close_err;
159   gchar       *err_info;
160   int          err_fileno;
161   char        *out_filename = NULL;
162   gboolean     got_read_error = FALSE, got_write_error = FALSE;
163   int          count;
164
165   /* Process the options first */
166   while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
167
168     switch (opt) {
169     case 'w':
170       out_filename = optarg;
171       break;
172
173     case 'a':
174       do_append = !do_append;
175       break;
176
177     case 'T':
178       frame_type = wtap_short_string_to_encap(optarg);
179       if (frame_type < 0) {
180         fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
181             optarg);
182         list_encap_types();
183         exit(1);
184       }
185       break;
186
187     case 'F':
188       file_type = wtap_short_string_to_file_type(optarg);
189       if (file_type < 0) {
190         fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
191             optarg);
192         list_capture_types();
193         exit(1);
194       }
195       break;
196
197     case 'v':
198       verbose = TRUE;
199       break;
200
201     case 's':
202       snaplen = get_positive_int(optarg, "snapshot length");
203       break;
204
205     case 'h':
206       usage();
207       exit(0);
208       break;
209
210     case '?':              /* Bad options if GNU getopt */
211       switch(optopt) {
212       case'F':
213         list_capture_types();
214         break;
215       case'T':
216         list_encap_types();
217         break;
218       default:
219         usage();
220       }
221       exit(1);
222       break;
223
224     }
225
226   }
227
228   /* check for proper args; at a minimum, must have an output
229    * filename and one input file
230    */
231   in_file_count = argc - optind;
232   if (!out_filename) {
233     fprintf(stderr, "mergecap: an output filename must be set with -w\n");
234     fprintf(stderr, "          run with -h for help\n");
235     return 1;
236   }
237   if (in_file_count < 1) {
238     fprintf(stderr, "mergecap: No input files were specified\n");
239     return 1;
240   }
241
242   /* open the input files */
243   if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
244                            &open_err, &err_info, &err_fileno)) {
245     fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
246         wtap_strerror(open_err));
247     switch (open_err) {
248
249     case WTAP_ERR_UNSUPPORTED:
250     case WTAP_ERR_UNSUPPORTED_ENCAP:
251     case WTAP_ERR_BAD_RECORD:
252       fprintf(stderr, "(%s)\n", err_info);
253       g_free(err_info);
254       break;
255     }
256     return 2;
257   }
258
259   if (verbose) {
260     for (i = 0; i < in_file_count; i++)
261       fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
262               wtap_file_type_string(wtap_file_type(in_files[i].wth)));
263   }
264
265   if (snaplen == 0) {
266     /*
267      * Snapshot length not specified - default to the maximum of the
268      * snapshot lengths of the input files.
269      */
270     snaplen = merge_max_snapshot_length(in_file_count, in_files);
271   }
272
273   /* set the outfile frame type */
274   if (frame_type == -2) {
275     /*
276      * Default to the appropriate frame type for the input files.
277      */
278     frame_type = merge_select_frame_type(in_file_count, in_files);
279     if (verbose) {
280       if (frame_type == WTAP_ENCAP_PER_PACKET) {
281         /*
282          * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
283          */
284         int first_frame_type, this_frame_type;
285
286         first_frame_type = wtap_file_encap(in_files[0].wth);
287         for (i = 1; i < in_file_count; i++) {
288           this_frame_type = wtap_file_encap(in_files[i].wth);
289           if (first_frame_type != this_frame_type) {
290             fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
291             fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
292             fprintf(stderr, "          %s had type %s (%s)\n",
293                     in_files[0].filename,
294                     wtap_encap_string(first_frame_type),
295                     wtap_encap_short_string(first_frame_type));
296             fprintf(stderr, "          %s had type %s (%s)\n",
297                     in_files[i].filename,
298                     wtap_encap_string(this_frame_type),
299                     wtap_encap_short_string(this_frame_type));
300             break;
301           }
302         }
303       }
304       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
305               wtap_encap_string(frame_type),
306               wtap_encap_short_string(frame_type));
307     }
308   }
309
310   /* open the outfile */
311   if (strncmp(out_filename, "-", 2) == 0) {  
312     /* use stdout as the outfile */
313     out_fd = 1 /*stdout*/;
314   } else {
315     /* open the outfile */
316     out_fd = eth_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
317     if (out_fd == -1) {
318       fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
319               out_filename, strerror(errno));
320       exit(1);
321     }
322   }  
323     
324   /* prepare the outfile */
325   pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
326   if (pdh == NULL) {
327     merge_close_in_files(in_file_count, in_files);
328     free(in_files);
329     fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
330             wtap_strerror(open_err));
331     exit(1);
332   }
333
334   /* do the merge (or append) */
335   count = 1;
336   for (;;) {
337     if (do_append)
338       wth = merge_append_read_packet(in_file_count, in_files, &read_err,
339                                      &err_info);
340     else
341       wth = merge_read_packet(in_file_count, in_files, &read_err,
342                               &err_info);
343     if (wth == NULL) {
344       if (read_err != 0)
345         got_read_error = TRUE;
346       break;
347     }
348
349     if (verbose)
350       fprintf(stderr, "Record: %u\n", count++);
351
352     /* We simply write it, perhaps after truncating it; we could do other
353      * things, like modify it. */
354     phdr = wtap_phdr(wth);
355     if (snaplen != 0 && phdr->caplen > snaplen) {
356       snap_phdr = *phdr;
357       snap_phdr.caplen = snaplen;
358       phdr = &snap_phdr;
359     }
360
361     if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
362          wtap_buf_ptr(wth), &write_err)) {
363       got_write_error = TRUE;
364       break;
365     }
366   }
367
368   merge_close_in_files(in_file_count, in_files);
369   if (!got_read_error && !got_write_error) {
370     if (!wtap_dump_close(pdh, &write_err))
371       got_write_error = TRUE;
372   } else
373     wtap_dump_close(pdh, &close_err);
374
375   if (got_read_error) {
376     /*
377      * Find the file on which we got the error, and report the error.
378      */
379     for (i = 0; i < in_file_count; i++) {
380       if (in_files[i].state == GOT_ERROR) {
381         fprintf(stderr, "mergecap: Error reading %s: %s\n",
382                 in_files[i].filename, wtap_strerror(read_err));
383         switch (read_err) {
384
385         case WTAP_ERR_UNSUPPORTED:
386         case WTAP_ERR_UNSUPPORTED_ENCAP:
387         case WTAP_ERR_BAD_RECORD:
388           fprintf(stderr, "(%s)\n", err_info);
389           g_free(err_info);
390           break;
391         }
392       }
393     }
394   }
395
396   if (got_write_error) {
397     fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
398             wtap_strerror(write_err));
399   }
400
401   free(in_files);
402
403   return (!got_read_error && !got_write_error) ? 0 : 2;
404 }