1 /* Reorder the frames from an input dump file, and write to output dump file.
2 * Martin Mathieson and Jakub Jawadzki
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
40 #include "wsutil/wsgetopt.h"
43 /* Show command-line usage */
44 static void usage(void)
46 fprintf(stderr, "Reordercap %s"
48 " (" SVNVERSION " from " SVNPATH ")"
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");
60 /* Remember where this frame was in the file */
61 typedef struct FrameRecord_t {
66 struct wtap_nstime time;
70 /**************************************************/
73 /* Enable this symbol to see debug output */
74 /* #define REORDER_DEBUG */
77 #define DEBUG_PRINT printf
79 #define DEBUG_PRINT(...)
81 /**************************************************/
85 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh)
87 union wtap_pseudo_header pseudo_header;
90 const struct wtap_pkthdr *phdr;
92 struct wtap_pkthdr new_phdr;
94 DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n",
95 frame->offset, frame->length);
97 /* Re-read the first frame from the stored location */
105 DEBUG_PRINT("re-read: err is %d, buf is (%s)\n", err, buf);
107 /* Get packet header */
108 /* XXX, this might not work */
109 phdr = wtap_phdr(wth);
111 /* Copy, and set length and timestamp from item. */
113 new_phdr.len = frame->length;
114 new_phdr.caplen = frame->length;
115 new_phdr.ts = frame->time;
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));
124 /* Comparing timestamps between 2 frames.
130 frames_compare(gconstpointer a, gconstpointer b)
132 const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
133 const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
135 const struct wtap_nstime *time1 = &frame1->time;
136 const struct wtap_nstime *time2 = &frame2->time;
138 if (time1->secs > time2->secs)
140 if (time1->secs < time2->secs)
143 /* time1->secs == time2->secs */
144 if (time1->nsecs > time2->nsecs)
146 if (time1->nsecs < time2->nsecs)
149 /* time1->nsecs == time2->nsecs */
151 if (frame1->num > frame2->num)
153 if (frame1->num < frame2->num)
159 /********************************************************************/
161 /********************************************************************/
162 int main(int argc, char *argv[])
165 wtap_dumper *pdh = NULL;
169 const struct wtap_pkthdr *phdr;
170 guint wrong_order_count = 0;
171 gboolean write_output_regardless = TRUE;
175 FrameRecord_t *prevFrame = NULL;
182 /* Process the options first */
183 while ((opt = getopt(argc, argv, "n")) != -1) {
186 write_output_regardless = FALSE;
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];
206 wth = wtap_open_offline(infile, &err, &err_info, TRUE);
208 printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err));
211 DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
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);
216 printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err));
220 /* Allocate the array of frame pointers. */
221 frames = g_ptr_array_new();
223 /* Read each frame from infile */
224 while (wtap_read(wth, &err, &err_info, &data_offset)) {
225 FrameRecord_t *newFrameRecord;
227 phdr = wtap_phdr(wth);
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;
235 if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
239 g_ptr_array_add(frames, newFrameRecord);
240 prevFrame = newFrameRecord;
242 printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
244 /* Sort the frames */
245 if (wrong_order_count > 0) {
246 g_ptr_array_sort(frames, frames_compare);
249 /* Write out each sorted frame in turn */
250 for (i = 0; i < frames->len; i++) {
251 FrameRecord_t *frame = frames->pdata[i];
253 /* Avoid writing if already sorted and configured to */
254 if (write_output_regardless || (wrong_order_count > 0)) {
255 frame_write(frame, wth, pdh);
257 g_slice_free(FrameRecord_t, frame);
260 if (!write_output_regardless && (wrong_order_count == 0)) {
261 printf("Not writing output file because input file is already in order!\n");
264 /* Free the whole array */
265 g_ptr_array_free(frames, TRUE);
268 if (!wtap_dump_close(pdh, &err)) {
269 printf("Error closing %s: %s\n", outfile, wtap_strerror(err));
273 /* Finally, close infile */