1 /* Reorder the frames from an input dump file, and write to output dump file.
2 * Martin Mathieson and Jakub Jawadzki
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
22 #include <wiretap/wtap.h>
24 #ifndef HAVE_GETOPT_LONG
25 #include "wsutil/wsgetopt.h"
28 #include <ui/cmdarg_err.h>
29 #include <wsutil/filesystem.h>
30 #include <wsutil/file_util.h>
31 #include <wsutil/privileges.h>
33 #include <version_info.h>
34 #include <wiretap/wtap_opttypes.h>
37 #include <wsutil/plugins.h>
40 #include <wsutil/report_message.h>
42 #include "ui/failure_message.h"
44 #define INVALID_OPTION 1
46 #define OUTPUT_FILE_ERROR 1
48 /* Show command-line usage */
50 print_usage(FILE *output)
52 fprintf(output, "\n");
53 fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
54 fprintf(output, "\n");
55 fprintf(output, "Options:\n");
56 fprintf(output, " -n don't write to output file if the input file is ordered.\n");
57 fprintf(output, " -h display this help and exit.\n");
60 /* Remember where this frame was in the file */
61 typedef struct FrameRecord_t {
69 /**************************************************/
72 /* Enable this symbol to see debug output */
73 /* #define REORDER_DEBUG */
76 #define DEBUG_PRINT printf
78 #define DEBUG_PRINT(...)
80 /**************************************************/
84 frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
85 wtap_rec *rec, Buffer *buf, const char *infile,
91 DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
95 /* Re-read the frame from the stored location */
96 if (!wtap_seek_read(wth, frame->offset, rec, buf, &err, &err_info)) {
98 /* Print a message noting that the read failed somewhere along the line. */
100 "reordercap: An error occurred while re-reading \"%s\".\n",
102 cfile_read_failure_message("reordercap", infile, err, err_info);
107 /* Copy, and set length and timestamp from item. */
108 /* TODO: remove when wtap_seek_read() fills in rec,
109 including time stamps, for all file types */
110 rec->ts = frame->frame_time;
112 /* Dump frame to outfile */
113 if (!wtap_dump(pdh, rec, ws_buffer_start_ptr(buf), &err, &err_info)) {
114 cfile_write_failure_message("reordercap", infile, outfile, err,
115 err_info, frame->num,
116 wtap_file_type_subtype(wth));
121 /* Comparing timestamps between 2 frames.
122 negative if (t1 < t2)
124 positive if (t1 > t2)
127 frames_compare(gconstpointer a, gconstpointer b)
129 const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
130 const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
132 const nstime_t *time1 = &frame1->frame_time;
133 const nstime_t *time2 = &frame2->frame_time;
135 return nstime_cmp(time1, time2);
139 * General errors and warnings are reported with an console message
143 failure_warning_message(const char *msg_format, va_list ap)
145 fprintf(stderr, "reordercap: ");
146 vfprintf(stderr, msg_format, ap);
147 fprintf(stderr, "\n");
151 * Report additional information for an error in command-line arguments.
154 failure_message_cont(const char *msg_format, va_list ap)
156 vfprintf(stderr, msg_format, ap);
157 fprintf(stderr, "\n");
160 /********************************************************************/
162 /********************************************************************/
164 main(int argc, char *argv[])
166 char *init_progfile_dir_error;
168 wtap_dumper *pdh = NULL;
174 guint wrong_order_count = 0;
175 gboolean write_output_regardless = TRUE;
177 wtap_dump_params params;
178 int ret = EXIT_SUCCESS;
181 FrameRecord_t *prevFrame = NULL;
184 static const struct option long_options[] = {
185 {"help", no_argument, NULL, 'h'},
186 {"version", no_argument, NULL, 'v'},
193 cmdarg_err_init(failure_warning_message, failure_message_cont);
195 /* Initialize the version information. */
196 ws_init_version_info("Reordercap (Wireshark)", NULL, NULL, NULL);
199 * Get credential information for later use.
201 init_process_policies();
204 * Attempt to get the pathname of the directory containing the
207 init_progfile_dir_error = init_progfile_dir(argv[0]);
208 if (init_progfile_dir_error != NULL) {
210 "reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
211 init_progfile_dir_error);
212 g_free(init_progfile_dir_error);
215 init_report_message(failure_warning_message, failure_warning_message,
220 /* Process the options first */
221 while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) {
224 write_output_regardless = FALSE;
227 show_help_header("Reorder timestamps of input file frames into output file.");
235 ret = INVALID_OPTION;
240 /* Remaining args are file names */
241 file_count = argc - optind;
242 if (file_count == 2) {
243 infile = argv[optind];
244 outfile = argv[optind+1];
248 ret = INVALID_OPTION;
253 /* TODO: if reordercap is ever changed to give the user a choice of which
254 open_routine reader to use, then the following needs to change. */
255 wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
257 cfile_open_failure_message("reordercap", infile, err, err_info);
261 DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth));
263 wtap_dump_params_init(¶ms, wth);
265 /* Open outfile (same filetype/encap as input file) */
266 if (strcmp(outfile, "-") == 0) {
267 pdh = wtap_dump_open_stdout(wtap_file_type_subtype(wth), WTAP_UNCOMPRESSED, ¶ms, &err);
269 pdh = wtap_dump_open(outfile, wtap_file_type_subtype(wth), WTAP_UNCOMPRESSED, ¶ms, &err);
271 g_free(params.idb_inf);
272 params.idb_inf = NULL;
275 cfile_dump_open_failure_message("reordercap", outfile, err,
276 wtap_file_type_subtype(wth));
277 wtap_dump_params_cleanup(¶ms);
278 ret = OUTPUT_FILE_ERROR;
282 /* Allocate the array of frame pointers. */
283 frames = g_ptr_array_new();
285 /* Read each frame from infile */
287 ws_buffer_init(&buf, 1514);
288 while (wtap_read(wth, &rec, &buf, &err, &err_info, &data_offset)) {
289 FrameRecord_t *newFrameRecord;
291 newFrameRecord = g_slice_new(FrameRecord_t);
292 newFrameRecord->num = frames->len + 1;
293 newFrameRecord->offset = data_offset;
294 if (rec.presence_flags & WTAP_HAS_TS) {
295 newFrameRecord->frame_time = rec.ts;
297 nstime_set_unset(&newFrameRecord->frame_time);
300 if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
304 g_ptr_array_add(frames, newFrameRecord);
305 prevFrame = newFrameRecord;
307 wtap_rec_cleanup(&rec);
308 ws_buffer_free(&buf);
310 /* Print a message noting that the read failed somewhere along the line. */
311 cfile_read_failure_message("reordercap", infile, err, err_info);
314 printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
316 /* Sort the frames */
317 if (wrong_order_count > 0) {
318 g_ptr_array_sort(frames, frames_compare);
321 /* Write out each sorted frame in turn */
323 ws_buffer_init(&buf, 1514);
324 for (i = 0; i < frames->len; i++) {
325 FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
327 /* Avoid writing if already sorted and configured to */
328 if (write_output_regardless || (wrong_order_count > 0)) {
329 frame_write(frame, wth, pdh, &rec, &buf, infile, outfile);
331 g_slice_free(FrameRecord_t, frame);
333 wtap_rec_cleanup(&rec);
334 ws_buffer_free(&buf);
336 if (!write_output_regardless && (wrong_order_count == 0)) {
337 printf("Not writing output file because input file is already in order.\n");
340 /* Free the whole array */
341 g_ptr_array_free(frames, TRUE);
344 if (!wtap_dump_close(pdh, &err)) {
345 cfile_close_failure_message(outfile, err);
346 wtap_dump_params_cleanup(¶ms);
347 ret = OUTPUT_FILE_ERROR;
350 wtap_dump_params_cleanup(¶ms);
352 /* Finally, close infile and release resources. */
362 * Editor modelines - https://www.wireshark.org/tools/modelines.html
367 * indent-tabs-mode: nil
370 * vi: set shiftwidth=4 tabstop=8 expandtab:
371 * :indentSize=4:tabSize=8:noTabs=true: