5933abed371fcbcc2b67819574bfae0c7b7a51aa
[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(void)
45 {
46     fprintf(stderr, "Reordercap %s"
47 #ifdef SVNVERSION
48           " (" SVNVERSION " from " SVNPATH ")"
49 #endif
50           "\n", VERSION);
51     fprintf(stderr, "Reorder timestamps of input file frames into output file.\n");
52     fprintf(stderr, "See http://www.wireshark.org for more information.\n");
53     fprintf(stderr, "\n");
54     fprintf(stderr, "Usage: reordercap [options] <infile> <outfile>\n");
55     fprintf(stderr, "\n");
56     fprintf(stderr, "Options:\n");
57     fprintf(stderr, "  -n        don't write to output file if the input file is ordered.\n");
58 }
59
60 /* Remember where this frame was in the file */
61 typedef struct FrameRecord_t {
62     gint64               offset;
63     guint32              length;
64     guint                num;
65
66     struct wtap_nstime   time;
67 } FrameRecord_t;
68
69
70 /**************************************************/
71 /* Debugging only                                 */
72
73 /* Enable this symbol to see debug output */
74 /* #define REORDER_DEBUG */
75
76 #ifdef REORDER_DEBUG
77 #define DEBUG_PRINT printf
78 #else
79 #define DEBUG_PRINT(...)
80 #endif
81 /**************************************************/
82
83
84 static void
85 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh)
86 {
87     union wtap_pseudo_header pseudo_header;
88     int    err;
89     gchar  *errinfo;
90     const struct wtap_pkthdr *phdr;
91     guint8 buf[65535];
92     struct wtap_pkthdr new_phdr;
93
94     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n", 
95                 frame->offset, frame->length);
96
97     /* Re-read the first frame from the stored location */
98     wtap_seek_read(wth,
99                    frame->offset,
100                    &pseudo_header,
101                    buf,
102                    frame->length,
103                    &err,
104                    &errinfo);
105     DEBUG_PRINT("re-read: err is %d, buf is (%s)\n", err, buf);
106
107     /* Get packet header */
108     /* XXX, this might not work */
109     phdr = wtap_phdr(wth);
110
111     /* Copy, and set length and timestamp from item. */
112     new_phdr = *phdr;
113     new_phdr.len = frame->length;
114     new_phdr.caplen = frame->length;
115     new_phdr.ts = frame->time;
116
117     /* Dump frame to outfile */
118     if (!wtap_dump(pdh, &new_phdr, &pseudo_header, buf, &err)) {
119         printf("Error (%s) writing frame to outfile\n", wtap_strerror(err));
120         exit(1);
121     }
122 }
123
124 /* Comparing timestamps between 2 frames.
125    -1 if (t1 < t2)
126    0  if (t1 == t2)
127    1  if (t1 > t2)
128 */
129 static int
130 frames_compare(gconstpointer a, gconstpointer b)
131 {
132     const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
133     const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
134
135     const struct wtap_nstime *time1 = &frame1->time;
136     const struct wtap_nstime *time2 = &frame2->time;
137
138     if (time1->secs > time2->secs)
139         return 1;
140     if (time1->secs < time2->secs)
141         return -1;
142
143     /* time1->secs == time2->secs */
144     if (time1->nsecs > time2->nsecs)
145         return 1;
146     if (time1->nsecs < time2->nsecs)
147         return -1;
148
149     /* time1->nsecs == time2->nsecs */
150
151     if (frame1->num > frame2->num)
152         return 1;
153     if (frame1->num < frame2->num)
154         return -1;
155     return 0;
156 }
157
158
159 /********************************************************************/
160 /* Main function.                                                   */
161 /********************************************************************/
162 int main(int argc, char *argv[])
163 {
164     wtap *wth = NULL;
165     wtap_dumper *pdh = NULL;
166     int err;
167     gchar *err_info;
168     gint64 data_offset;
169     const struct wtap_pkthdr *phdr;
170     guint wrong_order_count = 0;
171     gboolean write_output_regardless = TRUE;
172     guint i;
173
174     GPtrArray *frames;
175     FrameRecord_t *prevFrame = NULL;
176
177     int opt;
178     int file_count;
179     char *infile;
180     char *outfile;
181
182     /* Process the options first */
183     while ((opt = getopt(argc, argv, "n")) != -1) {
184         switch (opt) {
185             case 'n':
186                 write_output_regardless = FALSE;
187                 break;
188             case '?':
189                 usage();
190                 exit(1);
191         }
192     }
193
194     /* Remaining args are file names */
195     file_count = argc - optind;
196     if (file_count == 2) {
197         infile = argv[optind];
198         outfile = argv[optind+1];
199     }
200     else {
201         usage();
202         exit(1);
203     }
204
205     /* Open infile */
206     wth = wtap_open_offline(infile, &err, &err_info, TRUE);
207     if (wth == NULL) {
208         printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err));
209         exit(1);
210     }
211     DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
212
213     /* Open outfile (same filetype/encap as input file) */
214     pdh = wtap_dump_open(outfile, wtap_file_type(wth), wtap_file_encap(wth), 65535, FALSE, &err);
215     if (pdh == NULL) {
216         printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err));
217         exit(1);
218     }
219
220     /* Allocate the array of frame pointers. */
221     frames = g_ptr_array_new();
222
223     /* Read each frame from infile */
224     while (wtap_read(wth, &err, &err_info, &data_offset)) {
225         FrameRecord_t *newFrameRecord;
226
227         phdr = wtap_phdr(wth);
228
229         newFrameRecord = g_slice_new(FrameRecord_t);
230         newFrameRecord->num = frames->len + 1;
231         newFrameRecord->offset = data_offset;
232         newFrameRecord->length = phdr->len;
233         newFrameRecord->time = phdr->ts;
234
235         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
236            wrong_order_count++;
237         }
238
239         g_ptr_array_add(frames, newFrameRecord);
240         prevFrame = newFrameRecord;
241     }
242     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
243
244     /* Sort the frames */
245     if (wrong_order_count > 0) {
246         g_ptr_array_sort(frames, frames_compare);
247     }
248
249     /* Write out each sorted frame in turn */
250     for (i = 0; i < frames->len; i++) {
251         FrameRecord_t *frame = frames->pdata[i];
252
253         /* Avoid writing if already sorted and configured to */
254         if (write_output_regardless || (wrong_order_count > 0)) {
255             frame_write(frame, wth, pdh);
256         }
257         g_slice_free(FrameRecord_t, frame);
258     }
259
260     if (!write_output_regardless && (wrong_order_count == 0)) {
261         printf("Not writing output file because input file is already in order!\n");
262     }
263
264     /* Free the whole array */
265     g_ptr_array_free(frames, TRUE);
266
267     /* Close outfile */
268     if (!wtap_dump_close(pdh, &err)) {
269         printf("Error closing %s: %s\n", outfile, wtap_strerror(err));
270         exit(1);
271     }
272
273     /* Finally, close infile */
274     wtap_fdclose(wth);
275
276     return 0;
277 }
278