header field for sec_vt_command_length
[metze/wireshark/wip.git] / reordercap.c
1 /* Reorder the frames from an input dump file, and write to output dump file.
2  * Martin Mathieson and Jakub Jawadzki
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "wtap.h"
38
39 #ifndef HAVE_GETOPT
40 #include "wsutil/wsgetopt.h"
41 #endif
42
43 /* Show command-line usage */
44 static void usage(gboolean is_error)
45 {
46     FILE *output;
47
48     if (!is_error) {
49         output = stdout;
50     }
51     else {
52         output = stderr;
53     }
54
55     fprintf(output, "Reordercap %s"
56 #ifdef SVNVERSION
57                       " (" SVNVERSION " from " SVNPATH ")"
58 #endif
59                       "\n", VERSION);
60     fprintf(output, "Reorder timestamps of input file frames into output file.\n");
61     fprintf(output, "See http://www.wireshark.org for more information.\n");
62     fprintf(output, "\n");
63     fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
64     fprintf(output, "\n");
65     fprintf(output, "Options:\n");
66     fprintf(output, "  -n        don't write to output file if the input file is ordered.\n");
67     fprintf(output, "  -h        display this help and exit.\n");
68 }
69
70 /* Remember where this frame was in the file */
71 typedef struct FrameRecord_t {
72     gint64       offset;
73     guint        num;
74
75     nstime_t     time;
76 } FrameRecord_t;
77
78
79 /**************************************************/
80 /* Debugging only                                 */
81
82 /* Enable this symbol to see debug output */
83 /* #define REORDER_DEBUG */
84
85 #ifdef REORDER_DEBUG
86 #define DEBUG_PRINT printf
87 #else
88 #define DEBUG_PRINT(...)
89 #endif
90 /**************************************************/
91
92
93 static void
94 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
95             const char *infile)
96 {
97     int    err;
98     gchar  *err_info;
99     struct wtap_pkthdr phdr;
100
101     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n", 
102                 frame->offset);
103
104     
105     /* Re-read the first frame from the stored location */
106     if (!wtap_seek_read(wth, frame->offset, &phdr, buf, &err, &err_info)) {
107         if (err != 0) {
108             /* Print a message noting that the read failed somewhere along the line. */
109             fprintf(stderr,
110                     "reordercap: An error occurred while re-reading \"%s\": %s.\n",
111                     infile, wtap_strerror(err));
112             switch (err) {
113
114             case WTAP_ERR_UNSUPPORTED:
115             case WTAP_ERR_UNSUPPORTED_ENCAP:
116             case WTAP_ERR_BAD_FILE:
117                 fprintf(stderr, "(%s)\n", err_info);
118                 g_free(err_info);
119                 break;
120             }
121             exit(1);
122         }
123     }
124
125     /* Copy, and set length and timestamp from item. */
126     /* TODO: remove when wtap_seek_read() will read phdr */
127     phdr.ts = frame->time;
128
129     /* Dump frame to outfile */
130     if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
131         fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
132                 wtap_strerror(err));
133         exit(1);
134     }
135 }
136
137 /* Comparing timestamps between 2 frames.
138    -1 if (t1 < t2)
139    0  if (t1 == t2)
140    1  if (t1 > t2)
141 */
142 static int
143 frames_compare(gconstpointer a, gconstpointer b)
144 {
145     const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
146     const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
147
148     const nstime_t *time1 = &frame1->time;
149     const nstime_t *time2 = &frame2->time;
150
151     if (time1->secs > time2->secs)
152         return 1;
153     if (time1->secs < time2->secs)
154         return -1;
155
156     /* time1->secs == time2->secs */
157     if (time1->nsecs > time2->nsecs)
158         return 1;
159     if (time1->nsecs < time2->nsecs)
160         return -1;
161
162     /* time1->nsecs == time2->nsecs */
163
164     if (frame1->num > frame2->num)
165         return 1;
166     if (frame1->num < frame2->num)
167         return -1;
168     return 0;
169 }
170
171
172 /********************************************************************/
173 /* Main function.                                                   */
174 /********************************************************************/
175 int main(int argc, char *argv[])
176 {
177     wtap *wth = NULL;
178     wtap_dumper *pdh = NULL;
179     Buffer buf;
180     int err;
181     gchar *err_info;
182     gint64 data_offset;
183     const struct wtap_pkthdr *phdr;
184     guint wrong_order_count = 0;
185     gboolean write_output_regardless = TRUE;
186     guint i;
187     wtapng_section_t            *shb_hdr;
188     wtapng_iface_descriptions_t *idb_inf;
189
190     GPtrArray *frames;
191     FrameRecord_t *prevFrame = NULL;
192
193     int opt;
194     int file_count;
195     char *infile;
196     char *outfile;
197
198     /* Process the options first */
199     while ((opt = getopt(argc, argv, "hn")) != -1) {
200         switch (opt) {
201             case 'n':
202                 write_output_regardless = FALSE;
203                 break;
204             case 'h':
205                 usage(FALSE);
206                 exit(0);
207             case '?':
208                 usage(TRUE);
209                 exit(1);
210         }
211     }
212
213     /* Remaining args are file names */
214     file_count = argc - optind;
215     if (file_count == 2) {
216         infile  = argv[optind];
217         outfile = argv[optind+1];
218     }
219     else {
220         usage(TRUE);
221         exit(1);
222     }
223
224     /* Open infile */
225     wth = wtap_open_offline(infile, &err, &err_info, TRUE);
226     if (wth == NULL) {
227         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
228                 wtap_strerror(err));
229         switch (err) {
230
231         case WTAP_ERR_UNSUPPORTED:
232         case WTAP_ERR_UNSUPPORTED_ENCAP:
233         case WTAP_ERR_BAD_FILE:
234             fprintf(stderr, "(%s)\n", err_info);
235             g_free(err_info);
236             break;
237         }
238         exit(1);
239     }
240     DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
241
242     shb_hdr = wtap_file_get_shb_info(wth);
243     idb_inf = wtap_file_get_idb_info(wth);
244
245     /* Open outfile (same filetype/encap as input file) */
246     pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
247                             65535, FALSE, shb_hdr, idb_inf, &err);
248     g_free(idb_inf);
249     if (pdh == NULL) {
250         fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
251                 outfile, wtap_strerror(err));
252         g_free(shb_hdr);
253         exit(1);
254     }
255
256     /* Allocate the array of frame pointers. */
257     frames = g_ptr_array_new();
258
259     /* Read each frame from infile */
260     while (wtap_read(wth, &err, &err_info, &data_offset)) {
261         FrameRecord_t *newFrameRecord;
262
263         phdr = wtap_phdr(wth);
264
265         newFrameRecord = g_slice_new(FrameRecord_t);
266         newFrameRecord->num = frames->len + 1;
267         newFrameRecord->offset = data_offset;
268         newFrameRecord->time = phdr->ts;
269
270         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
271            wrong_order_count++;
272         }
273
274         g_ptr_array_add(frames, newFrameRecord);
275         prevFrame = newFrameRecord;
276     }
277     if (err != 0) {
278       /* Print a message noting that the read failed somewhere along the line. */
279       fprintf(stderr,
280               "reordercap: An error occurred while reading \"%s\": %s.\n",
281               infile, wtap_strerror(err));
282       switch (err) {
283
284       case WTAP_ERR_UNSUPPORTED:
285       case WTAP_ERR_UNSUPPORTED_ENCAP:
286       case WTAP_ERR_BAD_FILE:
287           fprintf(stderr, "(%s)\n", err_info);
288           g_free(err_info);
289           break;
290       }
291     }
292
293     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
294
295     /* Sort the frames */
296     if (wrong_order_count > 0) {
297         g_ptr_array_sort(frames, frames_compare);
298     }
299
300     /* Write out each sorted frame in turn */
301     buffer_init(&buf, 1500);
302     for (i = 0; i < frames->len; i++) {
303         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
304
305         /* Avoid writing if already sorted and configured to */
306         if (write_output_regardless || (wrong_order_count > 0)) {
307             frame_write(frame, wth, pdh, &buf, infile);
308         }
309         g_slice_free(FrameRecord_t, frame);
310     }
311     buffer_free(&buf);
312
313     if (!write_output_regardless && (wrong_order_count == 0)) {
314         printf("Not writing output file because input file is already in order!\n");
315     }
316
317     /* Free the whole array */
318     g_ptr_array_free(frames, TRUE);
319
320     /* Close outfile */
321     if (!wtap_dump_close(pdh, &err)) {
322         fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
323                 wtap_strerror(err));
324         g_free(shb_hdr);
325         exit(1);
326     }
327     g_free(shb_hdr);
328
329     /* Finally, close infile */
330     wtap_fdclose(wth);
331
332     return 0;
333 }
334
335 /*
336  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
337  *
338  * Local variables:
339  * c-basic-offset: 4
340  * tab-width: 8
341  * indent-tabs-mode: nil
342  * End:
343  *
344  * vi: set shiftwidth=4 tabstop=8 expandtab:
345  * :indentSize=4:tabSize=8:noTabs=true:
346  */