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)
89 struct wtap_pkthdr phdr;
92 DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u, length=%u)\n",
93 frame->offset, frame->length);
95 /* Re-read the first frame from the stored location */
103 DEBUG_PRINT("re-read: err is %d, buf is (%s)\n", err, buf);
105 /* Copy, and set length and timestamp from item. */
106 /* TODO: remove when wtap_seek_read() will read phdr */
107 phdr.ts = frame->time;
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));
116 /* Comparing timestamps between 2 frames.
122 frames_compare(gconstpointer a, gconstpointer b)
124 const FrameRecord_t *frame1 = *(const FrameRecord_t **) a;
125 const FrameRecord_t *frame2 = *(const FrameRecord_t **) b;
127 const struct wtap_nstime *time1 = &frame1->time;
128 const struct wtap_nstime *time2 = &frame2->time;
130 if (time1->secs > time2->secs)
132 if (time1->secs < time2->secs)
135 /* time1->secs == time2->secs */
136 if (time1->nsecs > time2->nsecs)
138 if (time1->nsecs < time2->nsecs)
141 /* time1->nsecs == time2->nsecs */
143 if (frame1->num > frame2->num)
145 if (frame1->num < frame2->num)
151 /********************************************************************/
153 /********************************************************************/
154 int main(int argc, char *argv[])
157 wtap_dumper *pdh = NULL;
161 const struct wtap_pkthdr *phdr;
162 guint wrong_order_count = 0;
163 gboolean write_output_regardless = TRUE;
167 FrameRecord_t *prevFrame = NULL;
174 /* Process the options first */
175 while ((opt = getopt(argc, argv, "n")) != -1) {
178 write_output_regardless = FALSE;
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];
198 wth = wtap_open_offline(infile, &err, &err_info, TRUE);
200 printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err));
203 DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth));
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);
208 printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err));
212 /* Allocate the array of frame pointers. */
213 frames = g_ptr_array_new();
215 /* Read each frame from infile */
216 while (wtap_read(wth, &err, &err_info, &data_offset)) {
217 FrameRecord_t *newFrameRecord;
219 phdr = wtap_phdr(wth);
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;
227 if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
231 g_ptr_array_add(frames, newFrameRecord);
232 prevFrame = newFrameRecord;
234 printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
236 /* Sort the frames */
237 if (wrong_order_count > 0) {
238 g_ptr_array_sort(frames, frames_compare);
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];
245 /* Avoid writing if already sorted and configured to */
246 if (write_output_regardless || (wrong_order_count > 0)) {
247 frame_write(frame, wth, pdh);
249 g_slice_free(FrameRecord_t, frame);
252 if (!write_output_regardless && (wrong_order_count == 0)) {
253 printf("Not writing output file because input file is already in order!\n");
256 /* Free the whole array */
257 g_ptr_array_free(frames, TRUE);
260 if (!wtap_dump_close(pdh, &err)) {
261 printf("Error closing %s: %s\n", outfile, wtap_strerror(err));
265 /* Finally, close infile */