Apply change of Bug 8706 ( "Stream" should be changed to "Follow Stream" in the ...
[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     int    err;
88     gchar  *errinfo;
89     struct wtap_pkthdr phdr;
90     guint8 buf[65535];
91
92     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n", 
93                 frame->offset, frame->length);
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, 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     int err;
159     gchar *err_info;
160     gint64 data_offset;
161     const struct wtap_pkthdr *phdr;
162     guint wrong_order_count = 0;
163     gboolean write_output_regardless = TRUE;
164     guint i;
165
166     GPtrArray *frames;
167     FrameRecord_t *prevFrame = NULL;
168
169     int opt;
170     int file_count;
171     char *infile;
172     char *outfile;
173
174     /* Process the options first */
175     while ((opt = getopt(argc, argv, "n")) != -1) {
176         switch (opt) {
177             case 'n':
178                 write_output_regardless = FALSE;
179                 break;
180             case '?':
181                 usage();
182                 exit(1);
183         }
184     }
185
186     /* Remaining args are file names */
187     file_count = argc - optind;
188     if (file_count == 2) {
189         infile = argv[optind];
190         outfile = argv[optind+1];
191     }
192     else {
193         usage();
194         exit(1);
195     }
196
197     /* Open infile */
198     wth = wtap_open_offline(infile, &err, &err_info, TRUE);
199     if (wth == NULL) {
200         printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err));
201         exit(1);
202     }
203     DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
204
205     /* Open outfile (same filetype/encap as input file) */
206     pdh = wtap_dump_open(outfile, wtap_file_type(wth), wtap_file_encap(wth), 65535, FALSE, &err);
207     if (pdh == NULL) {
208         printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err));
209         exit(1);
210     }
211
212     /* Allocate the array of frame pointers. */
213     frames = g_ptr_array_new();
214
215     /* Read each frame from infile */
216     while (wtap_read(wth, &err, &err_info, &data_offset)) {
217         FrameRecord_t *newFrameRecord;
218
219         phdr = wtap_phdr(wth);
220
221         newFrameRecord = g_slice_new(FrameRecord_t);
222         newFrameRecord->num = frames->len + 1;
223         newFrameRecord->offset = data_offset;
224         newFrameRecord->length = phdr->len;
225         newFrameRecord->time = phdr->ts;
226
227         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
228            wrong_order_count++;
229         }
230
231         g_ptr_array_add(frames, newFrameRecord);
232         prevFrame = newFrameRecord;
233     }
234     printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
235
236     /* Sort the frames */
237     if (wrong_order_count > 0) {
238         g_ptr_array_sort(frames, frames_compare);
239     }
240
241     /* Write out each sorted frame in turn */
242     for (i = 0; i < frames->len; i++) {
243         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
244
245         /* Avoid writing if already sorted and configured to */
246         if (write_output_regardless || (wrong_order_count > 0)) {
247             frame_write(frame, wth, pdh);
248         }
249         g_slice_free(FrameRecord_t, frame);
250     }
251
252     if (!write_output_regardless && (wrong_order_count == 0)) {
253         printf("Not writing output file because input file is already in order!\n");
254     }
255
256     /* Free the whole array */
257     g_ptr_array_free(frames, TRUE);
258
259     /* Close outfile */
260     if (!wtap_dump_close(pdh, &err)) {
261         printf("Error closing %s: %s\n", outfile, wtap_strerror(err));
262         exit(1);
263     }
264
265     /* Finally, close infile */
266     wtap_fdclose(wth);
267
268     return 0;
269 }
270