Fix warning: cannot optimize possibly infinite loops. There was no actual infinite...
[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, Buffer *buf)
86 {
87     int    err;
88     gchar  *errinfo;
89     struct wtap_pkthdr phdr;
90
91     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n", 
92                 frame->offset, frame->length);
93
94     
95     /* Re-read the first frame from the stored location */
96     wtap_seek_read(wth,
97                    frame->offset,
98                    &phdr,
99                    buf,
100                    frame->length,
101                    &err,
102                    &errinfo);
103     DEBUG_PRINT("re-read: err is %d, buf is (%s)\n", err, buf);
104
105     /* Copy, and set length and timestamp from item. */
106     /* TODO: remove when wtap_seek_read() will read phdr */
107     phdr.ts = frame->time;
108
109     /* Dump frame to outfile */
110     if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
111         printf("Error (%s) writing frame to outfile\n", wtap_strerror(err));
112         exit(1);
113     }
114 }
115
116 /* Comparing timestamps between 2 frames.
117    -1 if (t1 < t2)
118    0  if (t1 == t2)
119    1  if (t1 > t2)
120 */
121 static int
122 frames_compare(gconstpointer a, gconstpointer b)
123 {
124     const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
125     const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
126
127     const struct wtap_nstime *time1 = &frame1->time;
128     const struct wtap_nstime *time2 = &frame2->time;
129
130     if (time1->secs > time2->secs)
131         return 1;
132     if (time1->secs < time2->secs)
133         return -1;
134
135     /* time1->secs == time2->secs */
136     if (time1->nsecs > time2->nsecs)
137         return 1;
138     if (time1->nsecs < time2->nsecs)
139         return -1;
140
141     /* time1->nsecs == time2->nsecs */
142
143     if (frame1->num > frame2->num)
144         return 1;
145     if (frame1->num < frame2->num)
146         return -1;
147     return 0;
148 }
149
150
151 /********************************************************************/
152 /* Main function.                                                   */
153 /********************************************************************/
154 int main(int argc, char *argv[])
155 {
156     wtap *wth = NULL;
157     wtap_dumper *pdh = NULL;
158     Buffer buf;
159     int err;
160     gchar *err_info;
161     gint64 data_offset;
162     const struct wtap_pkthdr *phdr;
163     guint wrong_order_count = 0;
164     gboolean write_output_regardless = TRUE;
165     guint i;
166     wtapng_section_t            *shb_hdr;
167     wtapng_iface_descriptions_t *idb_inf;
168
169     GPtrArray *frames;
170     FrameRecord_t *prevFrame = NULL;
171
172     int opt;
173     int file_count;
174     char *infile;
175     char *outfile;
176
177     /* Process the options first */
178     while ((opt = getopt(argc, argv, "n")) != -1) {
179         switch (opt) {
180             case 'n':
181                 write_output_regardless = FALSE;
182                 break;
183             case '?':
184                 usage();
185                 exit(1);
186         }
187     }
188
189     /* Remaining args are file names */
190     file_count = argc - optind;
191     if (file_count == 2) {
192         infile = argv[optind];
193         outfile = argv[optind+1];
194     }
195     else {
196         usage();
197         exit(1);
198     }
199
200     /* Open infile */
201     wth = wtap_open_offline(infile, &err, &err_info, TRUE);
202     if (wth == NULL) {
203         printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err));
204         exit(1);
205     }
206     DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
207
208     shb_hdr = wtap_file_get_shb_info(wth);
209     idb_inf = wtap_file_get_idb_info(wth);
210
211     /* Open outfile (same filetype/encap as input file) */
212     pdh = wtap_dump_open_ng(outfile, wtap_file_type(wth), wtap_file_encap(wth),
213                             65535, FALSE, shb_hdr, idb_inf, &err);
214     g_free(idb_inf);
215     if (pdh == NULL) {
216         printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err));
217         g_free(shb_hdr);
218         exit(1);
219     }
220
221     /* Allocate the array of frame pointers. */
222     frames = g_ptr_array_new();
223
224     /* Read each frame from infile */
225     while (wtap_read(wth, &err, &err_info, &data_offset)) {
226         FrameRecord_t *newFrameRecord;
227
228         phdr = wtap_phdr(wth);
229
230         newFrameRecord = g_slice_new(FrameRecord_t);
231         newFrameRecord->num = frames->len + 1;
232         newFrameRecord->offset = data_offset;
233         newFrameRecord->length = phdr->len;
234         newFrameRecord->time = phdr->ts;
235
236         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
237            wrong_order_count++;
238         }
239
240         g_ptr_array_add(frames, newFrameRecord);
241         prevFrame = newFrameRecord;
242     }
243     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
244
245     /* Sort the frames */
246     if (wrong_order_count > 0) {
247         g_ptr_array_sort(frames, frames_compare);
248     }
249
250     /* Write out each sorted frame in turn */
251     buffer_init(&buf, 1500);
252     for (i = 0; i < frames->len; i++) {
253         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
254
255         /* Avoid writing if already sorted and configured to */
256         if (write_output_regardless || (wrong_order_count > 0)) {
257             frame_write(frame, wth, pdh, &buf);
258         }
259         g_slice_free(FrameRecord_t, frame);
260     }
261     buffer_free(&buf);
262
263     if (!write_output_regardless && (wrong_order_count == 0)) {
264         printf("Not writing output file because input file is already in order!\n");
265     }
266
267     /* Free the whole array */
268     g_ptr_array_free(frames, TRUE);
269
270     /* Close outfile */
271     if (!wtap_dump_close(pdh, &err)) {
272         printf("Error closing %s: %s\n", outfile, wtap_strerror(err));
273         g_free(shb_hdr);
274         exit(1);
275     }
276     g_free(shb_hdr);
277
278     /* Finally, close infile */
279     wtap_fdclose(wth);
280
281     return 0;
282 }
283