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(gboolean is_error)
55 fprintf(output, "Reordercap %s"
57 " (" GITVERSION " from " GITBRANCH ")"
60 fprintf(output, "Reorder timestamps of input file frames into output file.\n");
61 fprintf(output, "See http://www.wireshark.org for more information.\n");
62 fprintf(output, "\n");
63 fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
64 fprintf(output, "\n");
65 fprintf(output, "Options:\n");
66 fprintf(output, " -n don't write to output file if the input file is ordered.\n");
67 fprintf(output, " -h display this help and exit.\n");
70 /* Remember where this frame was in the file */
71 typedef struct FrameRecord_t {
79 /**************************************************/
82 /* Enable this symbol to see debug output */
83 /* #define REORDER_DEBUG */
86 #define DEBUG_PRINT printf
88 #define DEBUG_PRINT(...)
90 /**************************************************/
94 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
99 struct wtap_pkthdr phdr;
101 DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
105 /* Re-read the first frame from the stored location */
106 if (!wtap_seek_read(wth, frame->offset, &phdr, buf, &err, &err_info)) {
108 /* Print a message noting that the read failed somewhere along the line. */
110 "reordercap: An error occurred while re-reading \"%s\": %s.\n",
111 infile, wtap_strerror(err));
114 case WTAP_ERR_UNSUPPORTED:
115 case WTAP_ERR_UNSUPPORTED_ENCAP:
116 case WTAP_ERR_BAD_FILE:
117 fprintf(stderr, "(%s)\n", err_info);
125 /* Copy, and set length and timestamp from item. */
126 /* TODO: remove when wtap_seek_read() will read phdr */
127 phdr.ts = frame->time;
129 /* Dump frame to outfile */
130 if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
131 fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
137 /* Comparing timestamps between 2 frames.
143 frames_compare(gconstpointer a, gconstpointer b)
145 const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
146 const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
148 const nstime_t *time1 = &frame1->time;
149 const nstime_t *time2 = &frame2->time;
151 if (time1->secs > time2->secs)
153 if (time1->secs < time2->secs)
156 /* time1->secs == time2->secs */
157 if (time1->nsecs > time2->nsecs)
159 if (time1->nsecs < time2->nsecs)
162 /* time1->nsecs == time2->nsecs */
164 if (frame1->num > frame2->num)
166 if (frame1->num < frame2->num)
172 /********************************************************************/
174 /********************************************************************/
175 int main(int argc, char *argv[])
178 wtap_dumper *pdh = NULL;
183 const struct wtap_pkthdr *phdr;
184 guint wrong_order_count = 0;
185 gboolean write_output_regardless = TRUE;
187 wtapng_section_t *shb_hdr;
188 wtapng_iface_descriptions_t *idb_inf;
191 FrameRecord_t *prevFrame = NULL;
198 /* Process the options first */
199 while ((opt = getopt(argc, argv, "hn")) != -1) {
202 write_output_regardless = FALSE;
213 /* Remaining args are file names */
214 file_count = argc - optind;
215 if (file_count == 2) {
216 infile = argv[optind];
217 outfile = argv[optind+1];
224 init_open_routines();
227 wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
229 fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
233 case WTAP_ERR_UNSUPPORTED:
234 case WTAP_ERR_UNSUPPORTED_ENCAP:
235 case WTAP_ERR_BAD_FILE:
236 fprintf(stderr, "(%s)\n", err_info);
242 DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
244 shb_hdr = wtap_file_get_shb_info(wth);
245 idb_inf = wtap_file_get_idb_info(wth);
247 /* Open outfile (same filetype/encap as input file) */
248 pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
249 65535, FALSE, shb_hdr, idb_inf, &err);
252 fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
253 outfile, wtap_strerror(err));
258 /* Allocate the array of frame pointers. */
259 frames = g_ptr_array_new();
261 /* Read each frame from infile */
262 while (wtap_read(wth, &err, &err_info, &data_offset)) {
263 FrameRecord_t *newFrameRecord;
265 phdr = wtap_phdr(wth);
267 newFrameRecord = g_slice_new(FrameRecord_t);
268 newFrameRecord->num = frames->len + 1;
269 newFrameRecord->offset = data_offset;
270 newFrameRecord->time = phdr->ts;
272 if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
276 g_ptr_array_add(frames, newFrameRecord);
277 prevFrame = newFrameRecord;
280 /* Print a message noting that the read failed somewhere along the line. */
282 "reordercap: An error occurred while reading \"%s\": %s.\n",
283 infile, wtap_strerror(err));
286 case WTAP_ERR_UNSUPPORTED:
287 case WTAP_ERR_UNSUPPORTED_ENCAP:
288 case WTAP_ERR_BAD_FILE:
289 fprintf(stderr, "(%s)\n", err_info);
295 printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
297 /* Sort the frames */
298 if (wrong_order_count > 0) {
299 g_ptr_array_sort(frames, frames_compare);
302 /* Write out each sorted frame in turn */
303 buffer_init(&buf, 1500);
304 for (i = 0; i < frames->len; i++) {
305 FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
307 /* Avoid writing if already sorted and configured to */
308 if (write_output_regardless || (wrong_order_count > 0)) {
309 frame_write(frame, wth, pdh, &buf, infile);
311 g_slice_free(FrameRecord_t, frame);
315 if (!write_output_regardless && (wrong_order_count == 0)) {
316 printf("Not writing output file because input file is already in order!\n");
319 /* Free the whole array */
320 g_ptr_array_free(frames, TRUE);
323 if (!wtap_dump_close(pdh, &err)) {
324 fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
331 /* Finally, close infile */
338 * Editor modelines - http://www.wireshark.org/tools/modelines.html
343 * indent-tabs-mode: nil
346 * vi: set shiftwidth=4 tabstop=8 expandtab:
347 * :indentSize=4:tabSize=8:noTabs=true: