* Martin Mathieson and Jakub Jawadzki
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <ws_exit_codes.h>
#include <wsutil/ws_getopt.h>
#include <wiretap/wtap.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/filesystem.h>
#include <app/application_flavor.h>
#include <wsutil/file_util.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
#include <cli_main.h>
#include <wsutil/version_info.h>
#include <wiretap/wtap_opttypes.h>
#ifdef HAVE_PLUGINS
#include <wsutil/plugins.h>
#endif
#include <wsutil/clopts_common.h>
#include <wsutil/wslog.h>
#include "ui/failure_message.h"
#define OUTPUT_FILE_ERROR 1
static void
print_usage(FILE *output)
{
fprintf(output, "\n");
fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
fprintf(output, "\n");
fprintf(output, "Options:\n");
fprintf(output, " -n don't write to output file if the input file is ordered.\n");
fprintf(output, " -h, --help display this help and exit.\n");
fprintf(output, " -v, --version print version information and exit.\n");
}
typedef struct FrameRecord_t {
int64_t offset;
unsigned num;
nstime_t frame_time;
} FrameRecord_t;
#ifdef REORDER_DEBUG
#define DEBUG_PRINT printf
#else
#define DEBUG_PRINT(...)
#endif
static bool
frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
wtap_rec *rec, const char *infile, const char *outfile)
{
int err;
char *err_info;
DEBUG_PRINT("\nDumping frame (offset=%" PRIu64 ")\n",
frame->offset);
if (!wtap_seek_read(wth, frame->offset, rec, &err, &err_info)) {
if (err != 0) {
fprintf(stderr,
"reordercap: An error occurred while re-reading \"%s\".\n",
infile);
report_cfile_read_failure(infile, err, err_info);
return false;
}
}
including time stamps, for all file types */
rec->ts = frame->frame_time;
if (!wtap_dump(pdh, rec, &err, &err_info)) {
report_cfile_write_failure(infile, outfile, err, err_info, frame->num,
wtap_file_type_subtype(wth));
return false;
}
wtap_rec_reset(rec);
return true;
}
negative if (t1 < t2)
zero if (t1 == t2)
positive if (t1 > t2)
*/
static int
frames_compare(const void *a, const void *b)
{
const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
const nstime_t *time1 = &frame1->frame_time;
const nstime_t *time2 = &frame2->frame_time;
return nstime_cmp(time1, time2);
}
int
main(int argc, char *argv[])
{
char *configuration_init_error;
wtap *wth = NULL;
wtap_dumper *pdh = NULL;
wtap_rec rec;
int err;
char *err_info;
int64_t data_offset;
unsigned wrong_order_count = 0;
bool write_output_regardless = true;
unsigned i;
wtap_dump_params params;
int ret = EXIT_SUCCESS;
GPtrArray *frames;
FrameRecord_t *prevFrame = NULL;
int opt;
static const struct ws_option long_options[] = {
{"help", ws_no_argument, NULL, 'h'},
{"version", ws_no_argument, NULL, 'v'},
LONGOPT_WSLOG
{0, 0, 0, 0 }
};
#define OPTSTRING "hnv"
static const char optstring[] = OPTSTRING;
int file_count;
char *infile;
const char *outfile;
const struct file_extension_info* file_extensions;
unsigned num_extensions;
g_set_prgname("reordercap");
cmdarg_err_init(stderr_cmdarg_err, stderr_cmdarg_err_cont);
ws_log_init(vcmdarg_err, "Reordercap Debug Console");
ws_log_parse_args(&argc, argv, optstring, long_options, vcmdarg_err, WS_EXIT_INVALID_OPTION);
ws_noisy("Finished log init and parsing command line log arguments");
* Get credential information for later use.
*/
init_process_policies();
* Attempt to get the pathname of the directory containing the
* executable file.
*/
configuration_init_error = configuration_init(argv[0], "wireshark");
if (configuration_init_error != NULL) {
fprintf(stderr,
"reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
configuration_init_error);
g_free(configuration_init_error);
}
ws_init_version_info("Reordercap", NULL, application_get_vcs_version_info, NULL, NULL);
init_report_failure_message("reordercap");
application_file_extensions(&file_extensions, &num_extensions);
wtap_init(true, application_configuration_environment_prefix(), file_extensions, num_extensions);
while ((opt = ws_getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
switch (opt) {
case 'n':
write_output_regardless = false;
break;
case 'h':
show_help_header("Reorder timestamps of input file frames into output file.");
print_usage(stdout);
goto clean_exit;
case 'v':
show_version();
goto clean_exit;
case '?':
default:
if (ws_log_is_wslog_arg(opt))
break;
print_usage(stderr);
ret = WS_EXIT_INVALID_OPTION;
goto clean_exit;
}
}
file_count = argc - ws_optind;
if (file_count == 2) {
infile = argv[ws_optind];
outfile = argv[ws_optind+1];
}
else {
print_usage(stderr);
ret = WS_EXIT_INVALID_OPTION;
goto clean_exit;
}
open_routine reader to use, then the following needs to change. */
wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, true, application_configuration_environment_prefix());
if (wth == NULL) {
report_cfile_open_failure(infile, err, err_info);
ret = WS_EXIT_OPEN_ERROR;
goto clean_exit;
}
DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth));
frames = g_ptr_array_new();
wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048);
while (wtap_read(wth, &rec, &err, &err_info, &data_offset)) {
FrameRecord_t *newFrameRecord;
newFrameRecord = g_slice_new(FrameRecord_t);
newFrameRecord->num = frames->len + 1;
newFrameRecord->offset = data_offset;
if (rec.presence_flags & WTAP_HAS_TS) {
newFrameRecord->frame_time = rec.ts;
} else {
nstime_set_unset(&newFrameRecord->frame_time);
}
if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
wrong_order_count++;
}
g_ptr_array_add(frames, newFrameRecord);
prevFrame = newFrameRecord;
wtap_rec_reset(&rec);
}
wtap_rec_cleanup(&rec);
if (err != 0) {
report_cfile_read_failure(infile, err, err_info);
}
printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
wtap_dump_params_init(¶ms, wth);
if (wrong_order_count > 0) {
g_ptr_array_sort(frames, frames_compare);
}
if (write_output_regardless || (wrong_order_count > 0)) {
if (strcmp(outfile, "-") == 0) {
pdh = wtap_dump_open_stdout(wtap_file_type_subtype(wth),
WS_FILE_UNCOMPRESSED, ¶ms, &err, &err_info);
} else {
pdh = wtap_dump_open(outfile, wtap_file_type_subtype(wth),
WS_FILE_UNCOMPRESSED, ¶ms, &err, &err_info);
}
g_free(params.idb_inf);
params.idb_inf = NULL;
if (pdh == NULL) {
report_cfile_dump_open_failure(outfile, err, err_info,
wtap_file_type_subtype(wth));
wtap_dump_params_cleanup(¶ms);
ret = OUTPUT_FILE_ERROR;
goto clean_exit;
}
wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048);
for (i = 0; i < frames->len; i++) {
FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
if (!frame_write(frame, wth, pdh, &rec, infile, outfile))
return EXIT_FAILURE;
g_slice_free(FrameRecord_t, frame);
}
wtap_rec_cleanup(&rec);
if (!wtap_dump_close(pdh, NULL, &err, &err_info)) {
report_cfile_close_failure(outfile, err, err_info);
wtap_dump_params_cleanup(¶ms);
ret = OUTPUT_FILE_ERROR;
goto clean_exit;
}
} else {
printf("Not writing output file because input file is already in order.\n");
for (i = 0; i < frames->len; i++) {
FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
g_slice_free(FrameRecord_t, frame);
}
}
g_ptr_array_free(frames, TRUE);
wtap_dump_params_cleanup(¶ms);
wtap_close(wth);
clean_exit:
wtap_cleanup();
free_progdirs();
return ret;
}
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/