2012-09-20 10:28:05 +00:00
|
|
|
/* Reorder the frames from an input dump file, and write to output dump file.
|
2012-10-04 18:24:21 +00:00
|
|
|
* Martin Mathieson and Jakub Jawadzki
|
2012-09-20 10:28:05 +00:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-22 21:13:05 +00:00
|
|
|
#include <config.h>
|
2012-10-03 18:03:01 +00:00
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-10-03 18:03:01 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2014-07-03 04:50:54 +00:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 00:24:52 +00:00
|
|
|
#include <wiretap/wtap.h>
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-12-25 01:10:40 +00:00
|
|
|
#ifndef HAVE_GETOPT_LONG
|
2012-10-03 18:03:01 +00:00
|
|
|
#include "wsutil/wsgetopt.h"
|
|
|
|
#endif
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2017-09-11 15:45:52 +00:00
|
|
|
#include <wsutil/cmdarg_err.h>
|
2014-07-03 08:45:32 +00:00
|
|
|
#include <wsutil/crash_info.h>
|
2016-03-21 22:51:12 +00:00
|
|
|
#include <wsutil/filesystem.h>
|
2015-02-20 00:05:49 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2016-03-21 22:51:12 +00:00
|
|
|
#include <wsutil/privileges.h>
|
2016-04-19 07:03:08 +00:00
|
|
|
#include <ws_version_info.h>
|
2016-01-26 01:17:21 +00:00
|
|
|
#include <wiretap/wtap_opttypes.h>
|
2014-07-03 08:45:32 +00:00
|
|
|
|
2016-03-21 22:51:12 +00:00
|
|
|
#ifdef HAVE_PLUGINS
|
|
|
|
#include <wsutil/plugins.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-08 19:45:19 +00:00
|
|
|
#include <wsutil/report_message.h>
|
2016-03-21 22:51:12 +00:00
|
|
|
|
2017-04-20 07:19:01 +00:00
|
|
|
#include "ui/failure_message.h"
|
|
|
|
|
2017-02-04 15:26:34 +00:00
|
|
|
#define INVALID_OPTION 1
|
|
|
|
#define OPEN_ERROR 2
|
|
|
|
#define OUTPUT_FILE_ERROR 1
|
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
/* Show command-line usage */
|
2014-06-20 18:48:27 +00:00
|
|
|
static void
|
2014-07-03 08:45:32 +00:00
|
|
|
print_usage(FILE *output)
|
2012-09-20 10:28:05 +00:00
|
|
|
{
|
2013-12-31 16:23:00 +00:00
|
|
|
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 display this help and exit.\n");
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember where this frame was in the file */
|
|
|
|
typedef struct FrameRecord_t {
|
2013-11-09 10:38:02 +00:00
|
|
|
gint64 offset;
|
|
|
|
guint num;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2015-09-28 13:11:44 +00:00
|
|
|
nstime_t frame_time;
|
2012-09-20 10:28:05 +00:00
|
|
|
} FrameRecord_t;
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/* Debugging only */
|
|
|
|
|
|
|
|
/* Enable this symbol to see debug output */
|
2012-09-20 10:36:28 +00:00
|
|
|
/* #define REORDER_DEBUG */
|
2012-09-20 10:28:05 +00:00
|
|
|
|
|
|
|
#ifdef REORDER_DEBUG
|
|
|
|
#define DEBUG_PRINT printf
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT(...)
|
|
|
|
#endif
|
|
|
|
/**************************************************/
|
|
|
|
|
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
static void
|
2014-12-01 00:30:19 +00:00
|
|
|
frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
|
2017-04-20 07:19:01 +00:00
|
|
|
struct wtap_pkthdr *phdr, Buffer *buf, const char *infile,
|
|
|
|
const char *outfile)
|
2012-09-20 10:28:05 +00:00
|
|
|
{
|
|
|
|
int err;
|
2013-07-26 21:49:42 +00:00
|
|
|
gchar *err_info;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-02-07 19:14:52 +00:00
|
|
|
DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
|
2014-01-02 20:47:21 +00:00
|
|
|
frame->offset);
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-02-07 19:14:52 +00:00
|
|
|
|
2014-12-01 00:30:19 +00:00
|
|
|
/* Re-read the frame from the stored location */
|
|
|
|
if (!wtap_seek_read(wth, frame->offset, phdr, buf, &err, &err_info)) {
|
2013-07-26 21:49:42 +00:00
|
|
|
if (err != 0) {
|
|
|
|
/* Print a message noting that the read failed somewhere along the line. */
|
|
|
|
fprintf(stderr,
|
2017-04-20 07:19:01 +00:00
|
|
|
"reordercap: An error occurred while re-reading \"%s\".\n",
|
|
|
|
infile);
|
|
|
|
cfile_read_failure_message("reordercap", infile, err, err_info);
|
2013-07-26 21:49:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 10:28:05 +00:00
|
|
|
|
|
|
|
/* Copy, and set length and timestamp from item. */
|
2014-12-01 00:30:19 +00:00
|
|
|
/* TODO: remove when wtap_seek_read() fills in phdr,
|
|
|
|
including time stamps, for all file types */
|
2015-09-28 13:11:44 +00:00
|
|
|
phdr->ts = frame->frame_time;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
|
|
|
/* Dump frame to outfile */
|
2014-12-18 00:02:50 +00:00
|
|
|
if (!wtap_dump(pdh, phdr, ws_buffer_start_ptr(buf), &err, &err_info)) {
|
2017-04-20 17:14:50 +00:00
|
|
|
cfile_write_failure_message("reordercap", infile, outfile, err,
|
|
|
|
err_info, frame->num,
|
|
|
|
wtap_file_type_subtype(wth));
|
2012-09-20 10:28:05 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-10-04 18:24:21 +00:00
|
|
|
}
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
/* Comparing timestamps between 2 frames.
|
2014-05-23 23:40:56 +00:00
|
|
|
negative if (t1 < t2)
|
|
|
|
zero if (t1 == t2)
|
|
|
|
positive if (t1 > t2)
|
2012-10-04 18:24:21 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
frames_compare(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
2014-01-15 08:36:09 +00:00
|
|
|
const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
|
|
|
|
const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2015-09-28 13:11:44 +00:00
|
|
|
const nstime_t *time1 = &frame1->frame_time;
|
|
|
|
const nstime_t *time2 = &frame2->frame_time;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-05-23 23:40:56 +00:00
|
|
|
return nstime_cmp(time1, time2);
|
2012-10-04 18:24:21 +00:00
|
|
|
}
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2016-03-21 22:51:12 +00:00
|
|
|
/*
|
2017-04-08 19:45:19 +00:00
|
|
|
* General errors and warnings are reported with an console message
|
|
|
|
* in reordercap.
|
2016-03-21 22:51:12 +00:00
|
|
|
*/
|
|
|
|
static void
|
2017-04-08 19:45:19 +00:00
|
|
|
failure_warning_message(const char *msg_format, va_list ap)
|
2016-03-21 22:51:12 +00:00
|
|
|
{
|
2016-09-05 23:26:29 +00:00
|
|
|
fprintf(stderr, "reordercap: ");
|
|
|
|
vfprintf(stderr, msg_format, ap);
|
|
|
|
fprintf(stderr, "\n");
|
2016-03-21 22:51:12 +00:00
|
|
|
}
|
2017-09-11 15:45:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Report additional information for an error in command-line arguments.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
failure_message_cont(const char *msg_format, va_list ap)
|
|
|
|
{
|
|
|
|
vfprintf(stderr, msg_format, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2016-03-21 22:51:12 +00:00
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
/********************************************************************/
|
|
|
|
/* Main function. */
|
|
|
|
/********************************************************************/
|
2014-06-20 18:48:27 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2012-09-20 10:28:05 +00:00
|
|
|
{
|
2014-07-03 08:45:32 +00:00
|
|
|
GString *comp_info_str;
|
|
|
|
GString *runtime_info_str;
|
2016-12-05 04:25:51 +00:00
|
|
|
char *init_progfile_dir_error;
|
2014-05-09 05:18:49 +00:00
|
|
|
wtap *wth = NULL;
|
|
|
|
wtap_dumper *pdh = NULL;
|
2014-12-01 00:30:19 +00:00
|
|
|
struct wtap_pkthdr dump_phdr;
|
2013-06-16 00:20:00 +00:00
|
|
|
Buffer buf;
|
2012-09-20 10:28:05 +00:00
|
|
|
int err;
|
|
|
|
gchar *err_info;
|
|
|
|
gint64 data_offset;
|
|
|
|
const struct wtap_pkthdr *phdr;
|
2012-10-04 18:24:21 +00:00
|
|
|
guint wrong_order_count = 0;
|
|
|
|
gboolean write_output_regardless = TRUE;
|
|
|
|
guint i;
|
2016-05-31 03:42:41 +00:00
|
|
|
GArray *shb_hdrs = NULL;
|
2015-07-20 14:35:06 +00:00
|
|
|
wtapng_iface_descriptions_t *idb_inf = NULL;
|
2016-06-01 14:11:46 +00:00
|
|
|
GArray *nrb_hdrs = NULL;
|
2017-02-04 15:26:34 +00:00
|
|
|
int ret = EXIT_SUCCESS;
|
2012-10-04 18:24:21 +00:00
|
|
|
|
|
|
|
GPtrArray *frames;
|
|
|
|
FrameRecord_t *prevFrame = NULL;
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2012-10-03 18:03:01 +00:00
|
|
|
int opt;
|
2014-07-03 04:50:54 +00:00
|
|
|
static const struct option long_options[] = {
|
2015-11-21 23:48:27 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
2014-07-03 04:50:54 +00:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
2012-10-03 18:03:01 +00:00
|
|
|
int file_count;
|
2012-09-20 10:28:05 +00:00
|
|
|
char *infile;
|
2015-11-10 01:21:46 +00:00
|
|
|
const char *outfile;
|
2012-10-03 18:03:01 +00:00
|
|
|
|
2017-09-11 15:45:52 +00:00
|
|
|
cmdarg_err_init(failure_warning_message, failure_message_cont);
|
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
/* Get the compile-time version information string */
|
2016-04-01 00:16:15 +00:00
|
|
|
comp_info_str = get_compiled_version_info(NULL, NULL);
|
2014-07-03 08:45:32 +00:00
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
/* Get the run-time version information string */
|
2016-04-01 00:16:15 +00:00
|
|
|
runtime_info_str = get_runtime_version_info(NULL);
|
2014-07-03 08:45:32 +00:00
|
|
|
|
|
|
|
/* Add it to the information to be reported on a crash. */
|
|
|
|
ws_add_crash_info("Reordercap (Wireshark) %s\n"
|
|
|
|
"\n"
|
|
|
|
"%s"
|
|
|
|
"\n"
|
|
|
|
"%s",
|
|
|
|
get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
|
2016-09-10 11:03:38 +00:00
|
|
|
g_string_free(comp_info_str, TRUE);
|
|
|
|
g_string_free(runtime_info_str, TRUE);
|
2014-07-03 08:45:32 +00:00
|
|
|
|
2016-12-05 04:25:51 +00:00
|
|
|
/*
|
|
|
|
* Get credential information for later use.
|
|
|
|
*/
|
|
|
|
init_process_policies();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to get the pathname of the directory containing the
|
|
|
|
* executable file.
|
|
|
|
*/
|
|
|
|
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
|
|
|
if (init_progfile_dir_error != NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
|
|
|
|
init_progfile_dir_error);
|
|
|
|
g_free(init_progfile_dir_error);
|
|
|
|
}
|
2016-12-04 21:42:07 +00:00
|
|
|
|
2016-12-05 04:25:51 +00:00
|
|
|
wtap_init();
|
2016-03-21 22:51:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PLUGINS
|
|
|
|
/* Register wiretap plugins */
|
2017-04-08 19:45:19 +00:00
|
|
|
init_report_message(failure_warning_message, failure_warning_message,
|
|
|
|
NULL, NULL, NULL);
|
2016-03-21 22:51:12 +00:00
|
|
|
|
2016-12-05 04:25:51 +00:00
|
|
|
/* Scan for plugins. This does *not* call their registration routines;
|
|
|
|
that's done later.
|
2016-09-05 23:26:29 +00:00
|
|
|
|
2016-12-05 04:25:51 +00:00
|
|
|
Don't report failures to load plugins because most (non-wiretap)
|
|
|
|
plugins *should* fail to load (because we're not linked against
|
|
|
|
libwireshark and dissector plugins need libwireshark). */
|
|
|
|
scan_plugins(DONT_REPORT_LOAD_FAILURE);
|
2016-03-21 22:51:12 +00:00
|
|
|
|
2016-12-05 04:25:51 +00:00
|
|
|
/* Register all libwiretap plugin modules. */
|
|
|
|
register_all_wiretap_modules();
|
2016-03-21 22:51:12 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-03 18:03:01 +00:00
|
|
|
/* Process the options first */
|
2014-07-03 04:50:54 +00:00
|
|
|
while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) {
|
2012-10-03 18:03:01 +00:00
|
|
|
switch (opt) {
|
2012-10-04 18:24:21 +00:00
|
|
|
case 'n':
|
|
|
|
write_output_regardless = FALSE;
|
2012-10-03 18:03:01 +00:00
|
|
|
break;
|
2013-12-31 16:23:00 +00:00
|
|
|
case 'h':
|
2014-07-03 08:45:32 +00:00
|
|
|
printf("Reordercap (Wireshark) %s\n"
|
|
|
|
"Reorder timestamps of input file frames into output file.\n"
|
2015-11-11 17:19:50 +00:00
|
|
|
"See https://www.wireshark.org for more information.\n",
|
2014-07-03 08:45:32 +00:00
|
|
|
get_ws_vcs_version_info());
|
|
|
|
print_usage(stdout);
|
2017-02-04 15:26:34 +00:00
|
|
|
goto clean_exit;
|
2014-06-20 18:48:27 +00:00
|
|
|
case 'v':
|
2016-09-10 11:03:38 +00:00
|
|
|
comp_info_str = get_compiled_version_info(NULL, NULL);
|
|
|
|
runtime_info_str = get_runtime_version_info(NULL);
|
2014-12-23 21:58:38 +00:00
|
|
|
show_version("Reordercap (Wireshark)", comp_info_str, runtime_info_str);
|
2014-07-03 08:45:32 +00:00
|
|
|
g_string_free(comp_info_str, TRUE);
|
|
|
|
g_string_free(runtime_info_str, TRUE);
|
2017-02-04 15:26:34 +00:00
|
|
|
goto clean_exit;
|
2012-10-03 18:03:01 +00:00
|
|
|
case '?':
|
2014-07-03 08:45:32 +00:00
|
|
|
print_usage(stderr);
|
2017-02-04 15:26:34 +00:00
|
|
|
ret = INVALID_OPTION;
|
|
|
|
goto clean_exit;
|
2012-10-03 18:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
/* Remaining args are file names */
|
2012-10-03 18:03:01 +00:00
|
|
|
file_count = argc - optind;
|
|
|
|
if (file_count == 2) {
|
2013-12-31 16:23:00 +00:00
|
|
|
infile = argv[optind];
|
2012-10-03 18:03:01 +00:00
|
|
|
outfile = argv[optind+1];
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-07-03 08:45:32 +00:00
|
|
|
print_usage(stderr);
|
2017-02-04 15:26:34 +00:00
|
|
|
ret = INVALID_OPTION;
|
|
|
|
goto clean_exit;
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open infile */
|
2014-03-20 16:57:29 +00:00
|
|
|
/* TODO: if reordercap is ever changed to give the user a choice of which
|
|
|
|
open_routine reader to use, then the following needs to change. */
|
2014-01-18 14:20:02 +00:00
|
|
|
wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
|
2012-09-20 10:28:05 +00:00
|
|
|
if (wth == NULL) {
|
2017-04-20 18:23:51 +00:00
|
|
|
cfile_open_failure_message("reordercap", infile, err, err_info);
|
2017-02-04 15:26:34 +00:00
|
|
|
ret = OPEN_ERROR;
|
|
|
|
goto clean_exit;
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
2015-11-16 21:02:43 +00:00
|
|
|
DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth));
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2016-05-31 03:42:41 +00:00
|
|
|
shb_hdrs = wtap_file_get_shb_for_new_file(wth);
|
2013-06-19 19:31:06 +00:00
|
|
|
idb_inf = wtap_file_get_idb_info(wth);
|
2016-06-01 14:11:46 +00:00
|
|
|
nrb_hdrs = wtap_file_get_nrb_for_new_file(wth);
|
2013-06-19 19:31:06 +00:00
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
/* Open outfile (same filetype/encap as input file) */
|
2015-11-10 01:21:46 +00:00
|
|
|
if (strcmp(outfile, "-") == 0) {
|
2015-11-10 10:01:43 +00:00
|
|
|
pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth),
|
2017-03-27 14:11:41 +00:00
|
|
|
wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
|
2015-11-10 01:21:46 +00:00
|
|
|
} else {
|
|
|
|
pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
|
2017-03-27 14:11:41 +00:00
|
|
|
wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err);
|
2015-11-10 01:21:46 +00:00
|
|
|
}
|
2013-06-19 19:31:06 +00:00
|
|
|
g_free(idb_inf);
|
2015-07-20 14:35:06 +00:00
|
|
|
idb_inf = NULL;
|
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
if (pdh == NULL) {
|
2017-04-20 18:23:51 +00:00
|
|
|
cfile_dump_open_failure_message("reordercap", outfile, err,
|
|
|
|
wtap_file_type_subtype(wth));
|
2016-07-14 23:01:57 +00:00
|
|
|
wtap_block_array_free(shb_hdrs);
|
|
|
|
wtap_block_array_free(nrb_hdrs);
|
2017-02-04 15:26:34 +00:00
|
|
|
ret = OUTPUT_FILE_ERROR;
|
|
|
|
goto clean_exit;
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
/* Allocate the array of frame pointers. */
|
|
|
|
frames = g_ptr_array_new();
|
2012-09-20 10:28:05 +00:00
|
|
|
|
|
|
|
/* Read each frame from infile */
|
2014-05-23 10:50:02 +00:00
|
|
|
while (wtap_read(wth, &err, &err_info, &data_offset)) {
|
|
|
|
FrameRecord_t *newFrameRecord;
|
2012-10-04 18:24:21 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
phdr = wtap_phdr(wth);
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
newFrameRecord = g_slice_new(FrameRecord_t);
|
|
|
|
newFrameRecord->num = frames->len + 1;
|
|
|
|
newFrameRecord->offset = data_offset;
|
2014-05-23 23:40:56 +00:00
|
|
|
if (phdr->presence_flags & WTAP_HAS_TS) {
|
2015-09-28 13:11:44 +00:00
|
|
|
newFrameRecord->frame_time = phdr->ts;
|
2014-05-23 23:40:56 +00:00
|
|
|
} else {
|
2015-09-28 13:11:44 +00:00
|
|
|
nstime_set_unset(&newFrameRecord->frame_time);
|
2014-05-23 23:40:56 +00:00
|
|
|
}
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
|
|
|
|
wrong_order_count++;
|
2014-05-23 03:01:31 +00:00
|
|
|
}
|
2014-05-23 10:50:02 +00:00
|
|
|
|
|
|
|
g_ptr_array_add(frames, newFrameRecord);
|
|
|
|
prevFrame = newFrameRecord;
|
2012-10-04 18:24:21 +00:00
|
|
|
}
|
2013-07-26 21:49:42 +00:00
|
|
|
if (err != 0) {
|
|
|
|
/* Print a message noting that the read failed somewhere along the line. */
|
2017-04-20 07:19:01 +00:00
|
|
|
cfile_read_failure_message("reordercap", infile, err, err_info);
|
2013-07-26 21:49:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
printf("%u frames, %u out of order\n", frames->len, wrong_order_count);
|
|
|
|
|
|
|
|
/* Sort the frames */
|
|
|
|
if (wrong_order_count > 0) {
|
|
|
|
g_ptr_array_sort(frames, frames_compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out each sorted frame in turn */
|
2014-12-01 00:30:19 +00:00
|
|
|
wtap_phdr_init(&dump_phdr);
|
2014-08-02 11:00:48 +00:00
|
|
|
ws_buffer_init(&buf, 1500);
|
2012-10-04 18:24:21 +00:00
|
|
|
for (i = 0; i < frames->len; i++) {
|
2013-03-20 01:18:10 +00:00
|
|
|
FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
/* Avoid writing if already sorted and configured to */
|
|
|
|
if (write_output_regardless || (wrong_order_count > 0)) {
|
2017-04-20 07:19:01 +00:00
|
|
|
frame_write(frame, wth, pdh, &dump_phdr, &buf, infile, outfile);
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
2012-10-04 18:24:21 +00:00
|
|
|
g_slice_free(FrameRecord_t, frame);
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
2014-12-01 00:30:19 +00:00
|
|
|
wtap_phdr_cleanup(&dump_phdr);
|
2014-08-02 11:00:48 +00:00
|
|
|
ws_buffer_free(&buf);
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
if (!write_output_regardless && (wrong_order_count == 0)) {
|
2017-02-19 18:58:47 +00:00
|
|
|
printf("Not writing output file because input file is already in order.\n");
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 18:24:21 +00:00
|
|
|
/* Free the whole array */
|
|
|
|
g_ptr_array_free(frames, TRUE);
|
|
|
|
|
2012-09-20 10:28:05 +00:00
|
|
|
/* Close outfile */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_close(pdh, &err)) {
|
2017-04-20 07:19:01 +00:00
|
|
|
cfile_close_failure_message(outfile, err);
|
2016-07-14 23:01:57 +00:00
|
|
|
wtap_block_array_free(shb_hdrs);
|
|
|
|
wtap_block_array_free(nrb_hdrs);
|
2017-02-04 15:26:34 +00:00
|
|
|
ret = OUTPUT_FILE_ERROR;
|
|
|
|
goto clean_exit;
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
2016-07-14 23:01:57 +00:00
|
|
|
wtap_block_array_free(shb_hdrs);
|
|
|
|
wtap_block_array_free(nrb_hdrs);
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2016-09-10 11:03:38 +00:00
|
|
|
/* Finally, close infile and release resources. */
|
|
|
|
wtap_close(wth);
|
2012-09-20 10:28:05 +00:00
|
|
|
|
2017-02-04 15:26:34 +00:00
|
|
|
clean_exit:
|
|
|
|
wtap_cleanup();
|
2017-02-20 14:36:54 +00:00
|
|
|
free_progdirs();
|
|
|
|
#ifdef HAVE_PLUGINS
|
|
|
|
plugins_cleanup();
|
|
|
|
#endif
|
2017-02-04 15:26:34 +00:00
|
|
|
return ret;
|
2012-09-20 10:28:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-31 16:23:00 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://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:
|
|
|
|
*/
|