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
12 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
19 #include <ws_exit_codes.h>
20 #include <wsutil/ws_getopt.h>
22 #include <wiretap/wtap.h>
24 #include <wsutil/cmdarg_err.h>
25 #include <wsutil/filesystem.h>
26 #include <wsutil/file_util.h>
27 #include <wsutil/privileges.h>
29 #include <wsutil/version_info.h>
30 #include <wiretap/wtap_opttypes.h>
33 #include <wsutil/plugins.h>
36 #include <wsutil/wslog.h>
38 #include "ui/failure_message.h"
40 /* Additional exit codes */
41 #define OUTPUT_FILE_ERROR 1
43 /* Show command-line usage */
45 print_usage(FILE *output
)
47 fprintf(output
, "\n");
48 fprintf(output
, "Usage: reordercap [options] <infile> <outfile>\n");
49 fprintf(output
, "\n");
50 fprintf(output
, "Options:\n");
51 fprintf(output
, " -n don't write to output file if the input file is ordered.\n");
52 fprintf(output
, " -h, --help display this help and exit.\n");
53 fprintf(output
, " -v, --version print version information and exit.\n");
56 /* Remember where this frame was in the file */
57 typedef struct FrameRecord_t
{
65 /**************************************************/
68 /* Enable this symbol to see debug output */
69 /* #define REORDER_DEBUG */
72 #define DEBUG_PRINT printf
74 #define DEBUG_PRINT(...)
76 /**************************************************/
80 frame_write(FrameRecord_t
*frame
, wtap
*wth
, wtap_dumper
*pdh
,
81 wtap_rec
*rec
, Buffer
*buf
, const char *infile
,
87 DEBUG_PRINT("\nDumping frame (offset=%" PRIu64
")\n",
91 /* Re-read the frame from the stored location */
92 if (!wtap_seek_read(wth
, frame
->offset
, rec
, buf
, &err
, &err_info
)) {
94 /* Print a message noting that the read failed somewhere along the line. */
96 "reordercap: An error occurred while re-reading \"%s\".\n",
98 cfile_read_failure_message(infile
, err
, err_info
);
103 /* Copy, and set length and timestamp from item. */
104 /* TODO: remove when wtap_seek_read() fills in rec,
105 including time stamps, for all file types */
106 rec
->ts
= frame
->frame_time
;
108 /* Dump frame to outfile */
109 if (!wtap_dump(pdh
, rec
, ws_buffer_start_ptr(buf
), &err
, &err_info
)) {
110 cfile_write_failure_message(infile
, outfile
, err
, err_info
, frame
->num
,
111 wtap_file_type_subtype(wth
));
117 /* Comparing timestamps between 2 frames.
118 negative if (t1 < t2)
120 positive if (t1 > t2)
123 frames_compare(const void *a
, const void *b
)
125 const FrameRecord_t
*frame1
= *(const FrameRecord_t
*const *) a
;
126 const FrameRecord_t
*frame2
= *(const FrameRecord_t
*const *) b
;
128 const nstime_t
*time1
= &frame1
->frame_time
;
129 const nstime_t
*time2
= &frame2
->frame_time
;
131 return nstime_cmp(time1
, time2
);
134 /********************************************************************/
136 /********************************************************************/
138 main(int argc
, char *argv
[])
140 char *configuration_init_error
;
142 wtap_dumper
*pdh
= NULL
;
148 unsigned wrong_order_count
= 0;
149 bool write_output_regardless
= true;
151 wtap_dump_params params
;
152 int ret
= EXIT_SUCCESS
;
155 FrameRecord_t
*prevFrame
= NULL
;
158 static const struct ws_option long_options
[] = {
159 {"help", ws_no_argument
, NULL
, 'h'},
160 {"version", ws_no_argument
, NULL
, 'v'},
167 /* Set the program name. */
168 g_set_prgname("reordercap");
170 cmdarg_err_init(stderr_cmdarg_err
, stderr_cmdarg_err_cont
);
172 /* Initialize log handler early so we can have proper logging during startup. */
173 ws_log_init(vcmdarg_err
);
175 /* Early logging command-line initialization. */
176 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, WS_EXIT_INVALID_OPTION
);
178 ws_noisy("Finished log init and parsing command line log arguments");
181 * Get credential information for later use.
183 init_process_policies();
186 * Attempt to get the pathname of the directory containing the
189 configuration_init_error
= configuration_init(argv
[0]);
190 if (configuration_init_error
!= NULL
) {
192 "reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
193 configuration_init_error
);
194 g_free(configuration_init_error
);
197 /* Initialize the version information. */
198 ws_init_version_info("Reordercap", NULL
, NULL
);
200 init_report_failure_message("reordercap");
204 /* Process the options first */
205 while ((opt
= ws_getopt_long(argc
, argv
, "hnv", long_options
, NULL
)) != -1) {
208 write_output_regardless
= false;
211 show_help_header("Reorder timestamps of input file frames into output file.");
219 ret
= WS_EXIT_INVALID_OPTION
;
224 /* Remaining args are file names */
225 file_count
= argc
- ws_optind
;
226 if (file_count
== 2) {
227 infile
= argv
[ws_optind
];
228 outfile
= argv
[ws_optind
+1];
232 ret
= WS_EXIT_INVALID_OPTION
;
237 /* TODO: if reordercap is ever changed to give the user a choice of which
238 open_routine reader to use, then the following needs to change. */
239 wth
= wtap_open_offline(infile
, WTAP_TYPE_AUTO
, &err
, &err_info
, true);
241 cfile_open_failure_message(infile
, err
, err_info
);
242 ret
= WS_EXIT_OPEN_ERROR
;
245 DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth
));
247 /* Allocate the array of frame pointers. */
248 frames
= g_ptr_array_new();
250 /* Read each frame from infile */
252 ws_buffer_init(&buf
, 1514);
253 while (wtap_read(wth
, &rec
, &buf
, &err
, &err_info
, &data_offset
)) {
254 FrameRecord_t
*newFrameRecord
;
256 newFrameRecord
= g_slice_new(FrameRecord_t
);
257 newFrameRecord
->num
= frames
->len
+ 1;
258 newFrameRecord
->offset
= data_offset
;
259 if (rec
.presence_flags
& WTAP_HAS_TS
) {
260 newFrameRecord
->frame_time
= rec
.ts
;
262 nstime_set_unset(&newFrameRecord
->frame_time
);
265 if (prevFrame
&& frames_compare(&newFrameRecord
, &prevFrame
) < 0) {
269 g_ptr_array_add(frames
, newFrameRecord
);
270 prevFrame
= newFrameRecord
;
271 wtap_rec_reset(&rec
);
273 wtap_rec_cleanup(&rec
);
274 ws_buffer_free(&buf
);
276 /* Print a message noting that the read failed somewhere along the line. */
277 cfile_read_failure_message(infile
, err
, err_info
);
280 printf("%u frames, %u out of order\n", frames
->len
, wrong_order_count
);
282 wtap_dump_params_init(¶ms
, wth
);
284 /* Sort the frames */
285 /* XXX - Does this handle multiple SHBs correctly? */
286 if (wrong_order_count
> 0) {
287 g_ptr_array_sort(frames
, frames_compare
);
291 /* Avoid writing if already sorted and configured to */
292 if (write_output_regardless
|| (wrong_order_count
> 0)) {
293 /* Open outfile (same filetype/encap as input file) */
294 if (strcmp(outfile
, "-") == 0) {
295 pdh
= wtap_dump_open_stdout(wtap_file_type_subtype(wth
),
296 WTAP_UNCOMPRESSED
, ¶ms
, &err
, &err_info
);
298 pdh
= wtap_dump_open(outfile
, wtap_file_type_subtype(wth
),
299 WTAP_UNCOMPRESSED
, ¶ms
, &err
, &err_info
);
301 g_free(params
.idb_inf
);
302 params
.idb_inf
= NULL
;
305 cfile_dump_open_failure_message(outfile
, err
, err_info
,
306 wtap_file_type_subtype(wth
));
307 wtap_dump_params_cleanup(¶ms
);
308 ret
= OUTPUT_FILE_ERROR
;
313 /* Write out each sorted frame in turn */
315 ws_buffer_init(&buf
, 1514);
316 for (i
= 0; i
< frames
->len
; i
++) {
317 FrameRecord_t
*frame
= (FrameRecord_t
*)frames
->pdata
[i
];
319 frame_write(frame
, wth
, pdh
, &rec
, &buf
, infile
, outfile
);
321 g_slice_free(FrameRecord_t
, frame
);
324 wtap_rec_cleanup(&rec
);
325 ws_buffer_free(&buf
);
330 if (!wtap_dump_close(pdh
, NULL
, &err
, &err_info
)) {
331 cfile_close_failure_message(outfile
, err
, err_info
);
332 wtap_dump_params_cleanup(¶ms
);
333 ret
= OUTPUT_FILE_ERROR
;
337 printf("Not writing output file because input file is already in order.\n");
339 /* Free frame memory */
340 for (i
= 0; i
< frames
->len
; i
++) {
341 FrameRecord_t
*frame
= (FrameRecord_t
*)frames
->pdata
[i
];
343 g_slice_free(FrameRecord_t
, frame
);
348 /* Free the whole array */
349 g_ptr_array_free(frames
, TRUE
);
351 wtap_dump_params_cleanup(¶ms
);
353 /* Finally, close infile and release resources. */
363 * Editor modelines - https://www.wireshark.org/tools/modelines.html
368 * indent-tabs-mode: nil
371 * vi: set shiftwidth=4 tabstop=8 expandtab:
372 * :indentSize=4:tabSize=8:noTabs=true: