/** Open a number of input files to merge. * * @param in_file_count number of entries in in_file_names and in_files * @param in_file_names filenames of the input files * @param in_files input file array to be filled (>= sizeof(merge_in_file_t) * in_file_count) * @param err wiretap error, if failed * @param err_info wiretap error string, if failed * @param err_fileno file on which open failed, if failed * @return TRUE if all files could be opened, FALSE otherwise */ static gboolean merge_open_in_files(guint in_file_count, const char *const *in_file_names, merge_in_file_t **in_files, merge_progress_callback_t* cb, int *err, gchar **err_info, guint *err_fileno) { guint i; guint j; size_t files_size = in_file_count * sizeof(merge_in_file_t); merge_in_file_t *files; gint64 size; files = (merge_in_file_t *)g_malloc0(files_size); *in_files = files; for (i = 0; i < in_file_count; i++) { files[i].filename = in_file_names[i]; files[i].wth = wtap_open_offline(in_file_names[i], WTAP_TYPE_AUTO, err, err_info, FALSE); files[i].data_offset = 0; files[i].state = PACKET_NOT_PRESENT; files[i].packet_num = 0; if (!files[i].wth) { /* Close the files we've already opened. */ for (j = 0; j < i; j++) cleanup_in_file(&files[j]); *err_fileno = i; return FALSE; } size = wtap_file_size(files[i].wth, err); if (size == -1) { for (j = 0; j != G_MAXUINT && j <= i; j++) cleanup_in_file(&files[j]); *err_fileno = i; return FALSE; } files[i].size = size; files[i].idb_index_map = g_array_new(FALSE, FALSE, sizeof(guint)); } if (cb) cb->callback_func(MERGE_EVENT_INPUT_FILES_OPENED, 0, files, in_file_count, cb->data); return TRUE; }
/* new file arrived */ gboolean capture_info_new_file(const char *new_filename) { int err; gchar *err_info; gchar *err_msg; if(info_data.wtap != NULL) { wtap_close(info_data.wtap); } info_data.wtap = wtap_open_offline(new_filename, &err, &err_info, FALSE); if (!info_data.wtap) { err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_PCAP), new_filename); g_warning("capture_info_new_file: %d (%s)", err, err_msg); g_free (err_msg); return FALSE; } else return TRUE; }
/* new file arrived */ gboolean capture_info_new_file(const char *new_filename, info_data_t* cap_info) { int err; gchar *err_info; gchar *err_msg; if(cap_info->wtap != NULL) { wtap_close(cap_info->wtap); } cap_info->wtap = wtap_open_offline(new_filename, WTAP_TYPE_AUTO, &err, &err_info, FALSE); if (!cap_info->wtap) { err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN), new_filename); g_warning("capture_info_new_file: %d (%s)", err, err_msg); g_free (err_msg); return FALSE; } else return TRUE; }
/* * Scan through the arguments and open the input files */ gboolean merge_open_in_files(int in_file_count, const char *const *in_file_names, merge_in_file_t **in_files, int *err, gchar **err_info, int *err_fileno) { gint i; gint j; size_t files_size = in_file_count * sizeof(merge_in_file_t); merge_in_file_t *files; gint64 size; files = (merge_in_file_t *)g_malloc0(files_size); *in_files = files; for (i = 0; i < in_file_count; i++) { files[i].filename = in_file_names[i]; files[i].wth = wtap_open_offline(in_file_names[i], WTAP_TYPE_AUTO, err, err_info, FALSE); files[i].data_offset = 0; files[i].state = PACKET_NOT_PRESENT; files[i].packet_num = 0; if (!files[i].wth) { /* Close the files we've already opened. */ for (j = 0; j < i; j++) cleanup_in_file(&files[j]); *err_fileno = i; return FALSE; } size = wtap_file_size(files[i].wth, err); if (size == -1) { for (j = 0; j + 1 > j && j <= i; j++) cleanup_in_file(&files[j]); *err_fileno = i; return FALSE; } files[i].size = size; files[i].idb_index_map = g_array_new(FALSE, FALSE, sizeof(guint)); } return TRUE; }
/* * Scan through the arguments and open the input files */ gboolean merge_open_in_files(int in_file_count, char *const *in_file_names, merge_in_file_t **in_files, int *err, gchar **err_info, int *err_fileno) { int i, j; size_t files_size = in_file_count * sizeof(merge_in_file_t); merge_in_file_t *files; gint64 size; files = (merge_in_file_t *)g_malloc(files_size); *in_files = files; for (i = 0; i < in_file_count; i++) { files[i].filename = in_file_names[i]; files[i].wth = wtap_open_offline(in_file_names[i], err, err_info, FALSE); files[i].data_offset = 0; files[i].state = PACKET_NOT_PRESENT; files[i].packet_num = 0; if (!files[i].wth) { /* Close the files we've already opened. */ for (j = 0; j < i; j++) wtap_close(files[j].wth); *err_fileno = i; return FALSE; } size = wtap_file_size(files[i].wth, err); if (size == -1) { for (j = 0; j <= i; j++) wtap_close(files[j].wth); *err_fileno = i; return FALSE; } files[i].size = size; } return TRUE; }
bool CaptureFile::open() { //wtap *wth; gchar *err_info; int err; resetState(); wth = wtap_open_offline(filename.c_str(), &err, &err_info, TRUE); if (wth == NULL) { if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ switch (err) { case WTAP_ERR_UNSUPPORTED_ENCAP: { ostringstream s; s << "File " << filename << " has a packet with a network type that is not supported. (" << err_info << ")"; g_free(err_info); errorMessage = s.str(); break; } case WTAP_ERR_CANT_READ: { ostringstream s; s << "An attempt to read from file " << filename << " failed for some unknown reason."; errorMessage = s.str(); break; } case WTAP_ERR_SHORT_READ: { ostringstream s; s << "File " << filename << " appears to have been cut short in the middle of a packet."; errorMessage = s.str(); break; } case WTAP_ERR_BAD_RECORD: { ostringstream s; s << "File " << filename << " appears to be damaged or corrupt."; errorMessage = s.str(); g_free(err_info); break; } default: { ostringstream s; s << "An error occurred while reading" << " file " << filename << ": " << wtap_strerror(err); errorMessage = s.str(); break; } } } return false; } /* Initialize all data structures used for dissection. */ init_dissection(); /* We're about to start reading the file. */ state = FILE_READ_IN_PROGRESS; //wth = wth; f_datalen = 0; cd_t = wtap_file_type(wth); count = 0; displayed_count = 0; marked_count = 0; drops_known = FALSE; drops = 0; snap = wtap_snapshot_length(wth); if (snap == 0) { /* Snapshot length not known. */ has_snap = FALSE; snap = WTAP_MAX_PACKET_SIZE; } else has_snap = TRUE; nstime_set_zero(&elapsed_time); nstime_set_unset(&first_ts); nstime_set_unset(&prev_dis_ts); #if GLIB_CHECK_VERSION(2,10,0) #else /* memory chunks have been deprecated in favor of the slice allocator, * which has been added in 2.10 */ plist_chunk = g_mem_chunk_new("frame_data_chunk", sizeof (frame_data), FRAME_DATA_CHUNK_SIZE * sizeof (frame_data), G_ALLOC_AND_FREE); g_assert(plist_chunk); #endif /* change the time formats now, as we might have a new precision */ //cf_change_time_formats(cf);//FIXME ?? return true; }
cf_status_t cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err) { wtap *wth; gchar *err_info; //char err_msg[2048+1]; dprintf("%s: fname = %s\n", __FUNCTION__, fname); dprintf("%s: is_tempfile = %d err = %p\n", __FUNCTION__, is_tempfile, err); wth = wtap_open_offline(fname, err, &err_info, FALSE); dprintf("wth = %p\n", wth); if (wth == NULL) goto fail; /* The open succeeded. Fill in the information for this file. */ #if WIRESHARK_1_4_0 /* Cleanup all data structures used for dissection. */ cleanup_dissection(); #endif /* Initialize all data structures used for dissection. */ init_dissection(); cf->wth = wth; cf->f_datalen = 0; /* not used, but set it anyway */ /* Set the file name because we need it to set the follow stream filter. XXX - is that still true? We need it for other reasons, though, in any case. */ cf->filename = g_strdup(fname); /* Indicate whether it's a permanent or temporary file. */ cf->is_tempfile = is_tempfile; /* If it's a temporary capture buffer file, mark it as not saved. */ cf->user_saved = !is_tempfile; cf->cd_t = wtap_file_type(cf->wth); cf->count = 0; cf->drops_known = FALSE; cf->drops = 0; cf->snap = wtap_snapshot_length(cf->wth); if (cf->snap == 0) { /* Snapshot length not known. */ cf->has_snap = FALSE; cf->snap = WTAP_MAX_PACKET_SIZE; } else cf->has_snap = TRUE; nstime_set_zero(&cf->elapsed_time); nstime_set_unset(&first_ts); nstime_set_unset(&prev_dis_ts); nstime_set_unset(&prev_cap_ts); dprintf("%s: exiting\n", __FUNCTION__); return CF_OK; fail: g_snprintf(sharktools_errmsg, sizeof(sharktools_errmsg), cf_open_error_message(*err, err_info, cf->cd_t), fname); return CF_ERROR; }
int main(int argc, char *argv[]) { wtap *wth = NULL; wtap_dumper *pdh = NULL; Buffer buf; int err; gchar *err_info; gint64 data_offset; const struct wtap_pkthdr *phdr; guint wrong_order_count = 0; gboolean write_output_regardless = TRUE; guint i; wtapng_section_t *shb_hdr; wtapng_iface_descriptions_t *idb_inf; GPtrArray *frames; FrameRecord_t *prevFrame = NULL; int opt; int file_count; char *infile; char *outfile; /* Process the options first */ while ((opt = getopt(argc, argv, "hn")) != -1) { switch (opt) { case 'n': write_output_regardless = FALSE; break; case 'h': usage(FALSE); exit(0); case '?': usage(TRUE); exit(1); } } /* Remaining args are file names */ file_count = argc - optind; if (file_count == 2) { infile = argv[optind]; outfile = argv[optind+1]; } else { usage(TRUE); exit(1); } init_open_routines(); /* Open infile */ wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE); if (wth == NULL) { fprintf(stderr, "reordercap: Can't open %s: %s\n", infile, wtap_strerror(err)); switch (err) { case WTAP_ERR_UNSUPPORTED: case WTAP_ERR_UNSUPPORTED_ENCAP: case WTAP_ERR_BAD_FILE: fprintf(stderr, "(%s)\n", err_info); g_free(err_info); break; } exit(1); } DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth)); shb_hdr = wtap_file_get_shb_info(wth); idb_inf = wtap_file_get_idb_info(wth); /* Open outfile (same filetype/encap as input file) */ pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth), 65535, FALSE, shb_hdr, idb_inf, &err); g_free(idb_inf); if (pdh == NULL) { fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err)); g_free(shb_hdr); exit(1); } /* Allocate the array of frame pointers. */ frames = g_ptr_array_new(); /* Read each frame from infile */ while (wtap_read(wth, &err, &err_info, &data_offset)) { FrameRecord_t *newFrameRecord; phdr = wtap_phdr(wth); newFrameRecord = g_slice_new(FrameRecord_t); newFrameRecord->num = frames->len + 1; newFrameRecord->offset = data_offset; newFrameRecord->time = phdr->ts; if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) { wrong_order_count++; } g_ptr_array_add(frames, newFrameRecord); prevFrame = newFrameRecord; } if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ fprintf(stderr, "reordercap: An error occurred while reading \"%s\": %s.\n", infile, wtap_strerror(err)); switch (err) { case WTAP_ERR_UNSUPPORTED: case WTAP_ERR_UNSUPPORTED_ENCAP: case WTAP_ERR_BAD_FILE: fprintf(stderr, "(%s)\n", err_info); g_free(err_info); break; } } 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 */ buffer_init(&buf, 1500); for (i = 0; i < frames->len; i++) { FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i]; /* Avoid writing if already sorted and configured to */ if (write_output_regardless || (wrong_order_count > 0)) { frame_write(frame, wth, pdh, &buf, infile); } g_slice_free(FrameRecord_t, frame); } buffer_free(&buf); if (!write_output_regardless && (wrong_order_count == 0)) { printf("Not writing output file because input file is already in order!\n"); } /* Free the whole array */ g_ptr_array_free(frames, TRUE); /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile, wtap_strerror(err)); g_free(shb_hdr); exit(1); } g_free(shb_hdr); /* Finally, close infile */ wtap_fdclose(wth); return 0; }
/* * Opens an .xml file with Trace data formated according to 3GPP TS 32.423 and converts it to * an "Exported PDU type file with the entire xml file as the first "packet" appending the * raw messages as subsequent packages to be dissected by wireshark. */ static wtap_open_return_val create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_423_file_info_t *file_info) { int import_file_fd; wtap_dumper* wdh_exp_pdu; int exp_pdu_file_err; /* pcapng defs */ wtapng_section_t *shb_hdr; wtapng_iface_descriptions_t *idb_inf; wtapng_if_descr_t int_data; GString *os_info_str; char *appname; gint64 file_size; int packet_size; guint8 *packet_buf; int wrt_err; gchar *wrt_err_info; struct wtap_pkthdr phdr; gboolean do_random = FALSE; char *curr_pos, *next_pos; import_file_fd = create_tempfile(&(file_info->tmpname), "Wireshark_PDU_"); /* Now open a file and dump to it */ /* Create data for SHB */ os_info_str = g_string_new(""); get_os_version_info(os_info_str); appname = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info()); shb_hdr = g_new(wtapng_section_t, 1); shb_hdr->section_length = -1; /* options */ shb_hdr->opt_comment = g_strdup_printf("File converted to Exported PDU format during opening"); /* * UTF-8 string containing the description of the hardware used to create * this section. */ shb_hdr->shb_hardware = NULL; /* * UTF-8 string containing the name of the operating system used to create * this section. */ shb_hdr->shb_os = g_string_free(os_info_str, FALSE); /* * UTF-8 string containing the name of the application used to create * this section. */ shb_hdr->shb_user_appl = appname; /* Create fake IDB info */ idb_inf = g_new(wtapng_iface_descriptions_t, 1); idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t)); /* create the fake interface data */ int_data.wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU; int_data.time_units_per_second = 1000000; /* default microsecond resolution */ int_data.link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU); int_data.snap_len = WTAP_MAX_PACKET_SIZE; int_data.if_name = g_strdup("Fake IF"); int_data.opt_comment = NULL; int_data.if_description = NULL; int_data.if_speed = 0; int_data.if_tsresol = 6; int_data.if_filter_str = NULL; int_data.bpf_filter_len = 0; int_data.if_filter_bpf_bytes = NULL; int_data.if_os = NULL; int_data.if_fcslen = -1; int_data.num_stat_entries = 0; /* Number of ISB:s */ int_data.interface_statistics = NULL; g_array_append_val(idb_inf->interface_data, int_data); wdh_exp_pdu = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE, FALSE, shb_hdr, idb_inf, NULL, &exp_pdu_file_err); if (wdh_exp_pdu == NULL) { return WTAP_OPEN_ERROR; } g_free(shb_hdr); g_free(appname); /* OK we've opend a new pcap-ng file and written the headers, time to do the packets, strt by finding the file size */ if ((file_size = wtap_file_size(wth, err)) == -1) return WTAP_OPEN_ERROR; if (file_size > MAX_FILE_SIZE) { /* * Don't blow up trying to allocate space for an * immensely-large file. */ *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf("mime_file: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u", file_size, MAX_FILE_SIZE); return WTAP_OPEN_ERROR; } packet_size = (int)file_size; /* Allocate the packet buffer * (the whole file + Exported PDU tag "protocol" and * the string "xml" + 1 filler to end on 4 byte boundary for the tag * + End of options 4 bytes */ /* XXX add the length of exported bdu tag(s) here */ packet_buf = (guint8 *)g_malloc(packet_size + 12); packet_buf[0] = 0; packet_buf[1] = 12; /* EXP_PDU_TAG_PROTO_NAME */ packet_buf[2] = 0; packet_buf[3] = 4; packet_buf[4] = 0x78; /* "x" */ packet_buf[5] = 0x6d; /* "m" */ packet_buf[6] = 0x6c; /* "l" */ packet_buf[7] = 0; /* End of options */ packet_buf[8] = 0; packet_buf[9] = 0; packet_buf[10] = 0; packet_buf[11] = 0; if (!wtap_read_bytes(wth->fh, packet_buf + 12, packet_size, &wrt_err, &wrt_err_info)){ return WTAP_OPEN_ERROR; } /* Create the packet header */ memset(&phdr, 0, sizeof(struct wtap_pkthdr)); phdr.rec_type = REC_TYPE_PACKET; phdr.presence_flags = 0; /* yes, we have no bananas^Wtime stamp */ phdr.caplen = packet_size + 12; phdr.len = packet_size + 12; phdr.ts.secs = 0; phdr.ts.nsecs = 0; /* XXX: report errors! */ if (!wtap_dump(wdh_exp_pdu, &phdr, packet_buf, &wrt_err, &wrt_err_info)) { switch (wrt_err) { case WTAP_ERR_UNWRITABLE_REC_DATA: g_free(wrt_err_info); break; default: break; } g_free(packet_buf); return WTAP_OPEN_ERROR; } /* Advance *packet_buf to point at the raw file data */ curr_pos = packet_buf + 12; /* Lets add the raw messages as packets after the main "packet" with the whole file */ while ((curr_pos = strstr(curr_pos, "<msg")) != NULL){ wtap_open_return_val temp_val; curr_pos = curr_pos + 4; next_pos = strstr(curr_pos, "</msg>"); if (!next_pos){ /* Somethings wrong, bail out */ break; } next_pos = next_pos + 6; /* Do we have a raw msg?) */ curr_pos = strstr(curr_pos, "<rawMsg"); if (!curr_pos){ /* No rawMsg, continue */ curr_pos = next_pos; continue; } curr_pos = curr_pos + 7; /* Add the raw msg*/ temp_val = write_packet_data(wdh_exp_pdu, &phdr, &wrt_err, &wrt_err_info, curr_pos); if (temp_val != WTAP_OPEN_MINE){ g_free(packet_buf); return temp_val; } curr_pos = next_pos; } /* Close the written file*/ if (!wtap_dump_close(wdh_exp_pdu, err)){ g_free(packet_buf); return WTAP_OPEN_ERROR; } g_free(packet_buf); /* Now open the file for reading */ /* Find out if random read was requested */ if (wth->random_fh){ do_random = TRUE; } file_info->wth_tmp_file = wtap_open_offline(file_info->tmpname, WTAP_TYPE_AUTO, err, err_info, do_random); if (!file_info->wth_tmp_file){ return WTAP_OPEN_ERROR; } return WTAP_OPEN_MINE; }
int main(int argc, char *argv[]) { GString *comp_info_str; GString *runtime_info_str; wtap *wth; int err; gchar *err_info; int i; int opt; int overall_error_status; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; #ifdef HAVE_PLUGINS char *init_progfile_dir_error; #endif /* Set the C-language locale to the native environment. */ setlocale(LC_ALL, ""); /* Get the compile-time version information string */ comp_info_str = get_compiled_version_info(NULL, NULL); /* Get the run-time version information string */ runtime_info_str = get_runtime_version_info(NULL); /* Add it to the information to be reported on a crash. */ ws_add_crash_info("Captype (Wireshark) %s\n" "\n" "%s" "\n" "%s", get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); #ifdef _WIN32 arg_list_utf_16to8(argc, argv); create_app_running_mutex(); #endif /* _WIN32 */ /* * Get credential information for later use. */ init_process_policies(); init_open_routines(); #ifdef HAVE_PLUGINS if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) { g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error); g_free(init_progfile_dir_error); } else { /* Register all the plugin types we have. */ wtap_register_plugin_types(); /* Types known to libwiretap */ init_report_err(failure_message,NULL,NULL,NULL); /* Scan for plugins. This does *not* call their registration routines; that's done later. 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); /* Register all libwiretap plugin modules. */ register_all_wiretap_modules(); } #endif /* Process the options */ while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) { switch (opt) { case 'h': printf("Captype (Wireshark) %s\n" "Print the file types of capture files.\n" "See https://www.wireshark.org for more information.\n", get_ws_vcs_version_info()); print_usage(stdout); exit(0); break; case 'v': comp_info_str = get_compiled_version_info(NULL, NULL); runtime_info_str = get_runtime_version_info(NULL); show_version("Captype (Wireshark)", comp_info_str, runtime_info_str); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); exit(0); break; case '?': /* Bad flag - print usage message */ print_usage(stderr); exit(1); break; } } if (argc < 2) { print_usage(stderr); return 1; } overall_error_status = 0; for (i = 1; i < argc; i++) { wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE); if(wth) { printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth))); wtap_close(wth); } else { if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT) printf("%s: unknown\n", argv[i]); else { fprintf(stderr, "captype: Can't open %s: %s\n", argv[i], wtap_strerror(err)); if (err_info != NULL) { fprintf(stderr, "(%s)\n", err_info); g_free(err_info); } overall_error_status = 1; /* remember that an error has occurred */ } } } return overall_error_status; }
int main(int argc, char *argv[]) { wtap *wth = NULL; wtap_dumper *pdh = NULL; int err; gchar *err_info; gint64 data_offset; const struct wtap_pkthdr *phdr; guint wrong_order_count = 0; gboolean write_output_regardless = TRUE; guint i; GPtrArray *frames; FrameRecord_t *prevFrame = NULL; int opt; int file_count; char *infile; char *outfile; /* Process the options first */ while ((opt = getopt(argc, argv, "n")) != -1) { switch (opt) { case 'n': write_output_regardless = FALSE; break; case '?': usage(); exit(1); } } /* Remaining args are file names */ file_count = argc - optind; if (file_count == 2) { infile = argv[optind]; outfile = argv[optind+1]; } else { usage(); exit(1); } /* Open infile */ wth = wtap_open_offline(infile, &err, &err_info, TRUE); if (wth == NULL) { printf("reorder: Can't open %s: %s\n", infile, wtap_strerror(err)); exit(1); } DEBUG_PRINT("file_type is %u\n", wtap_file_type(wth)); /* Open outfile (same filetype/encap as input file) */ pdh = wtap_dump_open(outfile, wtap_file_type(wth), wtap_file_encap(wth), 65535, FALSE, &err); if (pdh == NULL) { printf("Failed to open output file: (%s) - error %s\n", outfile, wtap_strerror(err)); exit(1); } /* Allocate the array of frame pointers. */ frames = g_ptr_array_new(); /* Read each frame from infile */ while (wtap_read(wth, &err, &err_info, &data_offset)) { FrameRecord_t *newFrameRecord; phdr = wtap_phdr(wth); newFrameRecord = g_slice_new(FrameRecord_t); newFrameRecord->num = frames->len + 1; newFrameRecord->offset = data_offset; newFrameRecord->length = phdr->len; newFrameRecord->time = phdr->ts; if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) { wrong_order_count++; } g_ptr_array_add(frames, newFrameRecord); prevFrame = newFrameRecord; } 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 */ for (i = 0; i < frames->len; i++) { FrameRecord_t *frame = frames->pdata[i]; /* Avoid writing if already sorted and configured to */ if (write_output_regardless || (wrong_order_count > 0)) { frame_write(frame, wth, pdh); } g_slice_free(FrameRecord_t, frame); } if (!write_output_regardless && (wrong_order_count == 0)) { printf("Not writing output file because input file is already in order!\n"); } /* Free the whole array */ g_ptr_array_free(frames, TRUE); /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { printf("Error closing %s: %s\n", outfile, wtap_strerror(err)); exit(1); } /* Finally, close infile */ wtap_fdclose(wth); return 0; }
int main(int argc, char *argv[]) { int ser_fd; GError *err = NULL; int werr; gchar *err_info; gint64 data_offset; wtap *w; GOptionContext *opt_ctxt; gint64 ts_start_us = 0; gint64 time_start_us = 0; opt_ctxt = g_option_context_new ("FILE"); g_option_context_add_main_entries(opt_ctxt, app_options, NULL); if (!g_option_context_parse(opt_ctxt, &argc, &argv, &err)) { g_printerr("Failed to parse options: %s\n", err->message); return EXIT_FAILURE; } g_option_context_free(opt_ctxt); w = wtap_open_offline(argv[1], WTAP_TYPE_AUTO, &werr, &err_info, FALSE); if (!w) { g_printerr("Failed to open packet file: %d\n", werr); return EXIT_FAILURE; } if (pseudo) { ser_fd = open_pseudo_terminal(&err); if (ser_fd < 0) { g_printerr("Failed open pseudo terminal: %s\n", err->message); return EXIT_FAILURE; } } else { ser_fd = profibus_serial_open(device, speed, &err); if (ser_fd < 0) { g_printerr("Failed open serial port: %s\n", err->message); return EXIT_FAILURE; } } while(TRUE) { guint8 *data; struct wtap_pkthdr *hdr; if (!wtap_read(w, &werr, &err_info, &data_offset)) { if (err == 0) break; g_printerr("Failed to read packet file: %d\n", werr); return EXIT_FAILURE; } hdr = wtap_phdr(w); data = wtap_buf_ptr(w); if (timing) { if (time_start_us == 0) { ts_start_us = hdr->ts.secs * 1000000LL + hdr->ts.nsecs / 1000; time_start_us = g_get_monotonic_time(); } else { gint64 ts = hdr->ts.secs * 1000000LL + hdr->ts.nsecs / 1000; gint64 next = time_start_us + (ts - ts_start_us); gint64 now = g_get_monotonic_time(); gint64 delay = next - now; if (delay > 0) { poll(NULL, 0, delay / 1000); } } } if (write(ser_fd, data, hdr->len) != hdr->len) { g_printerr("Failed to write to serial device: %s\n", strerror(errno)); break; } /* g_debug("Write %d", hdr->len); */ } wtap_close(w); g_message("EOF"); close(ser_fd); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { GString *comp_info_str; GString *runtime_info_str; char *init_progfile_dir_error; wtap *wth = NULL; wtap_dumper *pdh = NULL; wtap_rec dump_rec; Buffer buf; int err; gchar *err_info; gint64 data_offset; const wtap_rec *rec; guint wrong_order_count = 0; gboolean write_output_regardless = TRUE; guint i; GArray *shb_hdrs = NULL; wtapng_iface_descriptions_t *idb_inf = NULL; GArray *nrb_hdrs = NULL; int ret = EXIT_SUCCESS; GPtrArray *frames; FrameRecord_t *prevFrame = NULL; int opt; static const struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0 } }; int file_count; char *infile; const char *outfile; cmdarg_err_init(failure_warning_message, failure_message_cont); /* Get the compile-time version information string */ comp_info_str = get_compiled_version_info(NULL, NULL); /* Get the run-time version information string */ runtime_info_str = get_runtime_version_info(NULL); /* 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); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); /* * 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]); 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); } init_report_message(failure_warning_message, failure_warning_message, NULL, NULL, NULL); wtap_init(TRUE); /* Process the options first */ while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) { switch (opt) { case 'n': write_output_regardless = FALSE; break; case 'h': printf("Reordercap (Wireshark) %s\n" "Reorder timestamps of input file frames into output file.\n" "See https://www.wireshark.org for more information.\n", get_ws_vcs_version_info()); print_usage(stdout); goto clean_exit; case 'v': comp_info_str = get_compiled_version_info(NULL, NULL); runtime_info_str = get_runtime_version_info(NULL); show_version("Reordercap (Wireshark)", comp_info_str, runtime_info_str); g_string_free(comp_info_str, TRUE); g_string_free(runtime_info_str, TRUE); goto clean_exit; case '?': print_usage(stderr); ret = INVALID_OPTION; goto clean_exit; } } /* Remaining args are file names */ file_count = argc - optind; if (file_count == 2) { infile = argv[optind]; outfile = argv[optind+1]; } else { print_usage(stderr); ret = INVALID_OPTION; goto clean_exit; } /* Open infile */ /* 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. */ wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE); if (wth == NULL) { cfile_open_failure_message("reordercap", infile, err, err_info); ret = OPEN_ERROR; goto clean_exit; } DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth)); shb_hdrs = wtap_file_get_shb_for_new_file(wth); idb_inf = wtap_file_get_idb_info(wth); nrb_hdrs = wtap_file_get_nrb_for_new_file(wth); /* Open outfile (same filetype/encap as input file) */ if (strcmp(outfile, "-") == 0) { pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth), wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); } else { pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth), wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); } g_free(idb_inf); idb_inf = NULL; if (pdh == NULL) { cfile_dump_open_failure_message("reordercap", outfile, err, wtap_file_type_subtype(wth)); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); ret = OUTPUT_FILE_ERROR; goto clean_exit; } /* Allocate the array of frame pointers. */ frames = g_ptr_array_new(); /* Read each frame from infile */ while (wtap_read(wth, &err, &err_info, &data_offset)) { FrameRecord_t *newFrameRecord; rec = wtap_get_rec(wth); 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; } if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ cfile_read_failure_message("reordercap", infile, err, err_info); } 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 */ wtap_rec_init(&dump_rec); ws_buffer_init(&buf, 1500); for (i = 0; i < frames->len; i++) { FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i]; /* Avoid writing if already sorted and configured to */ if (write_output_regardless || (wrong_order_count > 0)) { frame_write(frame, wth, pdh, &dump_rec, &buf, infile, outfile); } g_slice_free(FrameRecord_t, frame); } wtap_rec_cleanup(&dump_rec); ws_buffer_free(&buf); if (!write_output_regardless && (wrong_order_count == 0)) { printf("Not writing output file because input file is already in order.\n"); } /* Free the whole array */ g_ptr_array_free(frames, TRUE); /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { cfile_close_failure_message(outfile, err); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); ret = OUTPUT_FILE_ERROR; goto clean_exit; } wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); /* Finally, close infile and release resources. */ wtap_close(wth); clean_exit: wtap_cleanup(); free_progdirs(); return ret; }