static void list_capture_types(void) { int i; struct string_elem *captypes; GSList *list = NULL; captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES); fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n"); for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) { if (wtap_dump_can_open(i)) { captypes[i].sstr = wtap_file_type_subtype_short_string(i); captypes[i].lstr = wtap_file_type_subtype_string(i); list = g_slist_insert_sorted(list, &captypes[i], string_compare); } } g_slist_foreach(list, string_elem_print, NULL); g_slist_free(list); g_free(captypes); }
QString CaptureFilePropertiesDialog::summaryToHtml() { summary_tally summary; double seconds = 0.0; double disp_seconds = 0.0; double marked_seconds = 0.0; memset(&summary, 0, sizeof(summary_tally)); QString section_tmpl; QString table_begin, table_end; QString table_row_begin, table_ul_row_begin, table_row_end; QString table_vheader_tmpl, table_hheader20_tmpl, table_hheader25_tmpl; QString table_data_tmpl; section_tmpl = "<p><strong>%1</strong></p>\n"; table_begin = "<p><table>\n"; table_end = "</table></p>\n"; table_row_begin = "<tr>\n"; table_ul_row_begin = "<tr style=\"border-bottom: 1px solid gray;\">\n"; table_row_end = "</tr>\n"; table_vheader_tmpl = "<td width=\"20%\">%1:</td>"; // <th align="left"> looked odd table_hheader20_tmpl = "<td width=\"20%\"><u>%1</u></td>"; table_hheader25_tmpl = "<td width=\"25%\"><u>%1</u></td>"; table_data_tmpl = "<td>%1</td>"; if (!file_closed_) { /* initial computations */ summary_fill_in(cap_file_.capFile(), &summary); #ifdef HAVE_LIBPCAP summary_fill_in_capture(cap_file_.capFile(), &global_capture_opts, &summary); #endif } seconds = summary.stop_time - summary.start_time; disp_seconds = summary.filtered_stop - summary.filtered_start; marked_seconds = summary.marked_stop - summary.marked_start; QString summary_str; QTextStream out(&summary_str); QString unknown = tr("Unknown"); // File Section out << section_tmpl.arg(tr("File")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Name")) << table_data_tmpl.arg(summary.filename) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Length")) << table_data_tmpl.arg(file_size_to_qstring(summary.file_length)) << table_row_end; QString format_str = wtap_file_type_subtype_string(summary.file_type); if (summary.iscompressed) { format_str.append(tr(" (gzip compressed)")); } out << table_row_begin << table_vheader_tmpl.arg(tr("Format")) << table_data_tmpl.arg(format_str) << table_row_end; QString encaps_str; if (summary.file_encap_type == WTAP_ENCAP_PER_PACKET) { for (guint i = 0; i < summary.packet_encap_types->len; i++) { encaps_str = QString(wtap_encap_string(g_array_index(summary.packet_encap_types, int, i))); } } else {
/* * Alert box for a failed attempt to write to a capture file. * "in_filename" is the name of the file from which the record being * written came; "out_filename" is the name of the file to which we're * writing; "err" is assumed "err" is assumed to be a UNIX-style errno * or a WTAP_ERR_ value; "err_info" is assumed to be a string giving * further information for some WTAP_ERR_ values; "framenum" is the frame * number of the record on which the error occurred; "file_type_subtype" * is a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file * being written. */ void cfile_write_failure_alert_box(const char *in_filename, const char *out_filename, int err, gchar *err_info, guint32 framenum, int file_type_subtype) { char *in_file_string; char *out_display_basename; if (err < 0) { /* Wiretap error. */ if (in_filename == NULL) in_file_string = g_strdup(""); else in_file_string = g_strdup_printf(" of file \"%s\"", in_filename); switch (err) { case WTAP_ERR_UNWRITABLE_ENCAP: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're writing; note that, and report * the frame number and file type/subtype. */ simple_error_message_box( "Frame %u%s has a network type that can't be saved in a \"%s\" file.", framenum, in_file_string, wtap_file_type_subtype_string(file_type_subtype)); break; case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're writing; note that, and report * the frame number and file type/subtype. */ simple_error_message_box( "Frame %u%s has a network type that differs from the network type of earlier packets, which isn't supported in a \"%s\" file.", framenum, in_file_string, wtap_file_type_subtype_string(file_type_subtype)); break; case WTAP_ERR_PACKET_TOO_LARGE: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're writing; note that, and report * the frame number and file type/subtype. */ simple_error_message_box( "Frame %u%s is larger than Wireshark supports in a \"%s\" file.", framenum, in_file_string, wtap_file_type_subtype_string(file_type_subtype)); break; case WTAP_ERR_UNWRITABLE_REC_TYPE: /* * This is a problem with the particular record we're writing and * the file type and subtype we're writing; note that, and report * the record number and file type/subtype. */ simple_error_message_box( "Record %u%s has a record type that can't be saved in a \"%s\" file.", framenum, in_file_string, wtap_file_type_subtype_string(file_type_subtype)); break; case WTAP_ERR_UNWRITABLE_REC_DATA: /* * This is a problem with the particular record we're writing and * the file type and subtype we're writing; note that, and report * the record number and file type/subtype. */ simple_error_message_box( "Record %u%s has data that can't be saved in a \"%s\" file.\n" "(%s)", framenum, in_file_string, wtap_file_type_subtype_string(file_type_subtype), err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; case WTAP_ERR_SHORT_WRITE: out_display_basename = g_filename_display_basename(out_filename); simple_error_message_box( "A full write couldn't be done to the file \"%s\".", out_display_basename); g_free(out_display_basename); break; default: out_display_basename = g_filename_display_basename(out_filename); simple_error_message_box( "An error occurred while writing to the file \"%s\": %s.", out_display_basename, wtap_strerror(err)); g_free(out_display_basename); break; } g_free(in_file_string); } else { /* OS error. */ write_failure_alert_box(out_filename, err); } }
/* * Alert box for a failed attempt to open a capture file for writing. * "filename" is the name of the file being opened; "err" is assumed * to be a UNIX-style errno or a WTAP_ERR_ value; "file_type_subtype" * is a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file * being opened. * * XXX - add explanatory secondary text for at least some of the errors; * various HIGs suggest that you should, for example, suggest that the * user remove files if the file system is full. Perhaps that's because * they're providing guidelines for people less sophisticated than the * typical Wireshark user is, but.... */ void cfile_dump_open_failure_alert_box(const char *filename, int err, int file_type_subtype) { gchar *display_basename; if (err < 0) { /* Wiretap error. */ display_basename = g_filename_display_basename(filename); switch (err) { case WTAP_ERR_NOT_REGULAR_FILE: simple_error_message_box( "The file \"%s\" is a \"special file\" or socket or other non-regular file.", display_basename); break; case WTAP_ERR_CANT_WRITE_TO_PIPE: simple_error_message_box( "The file \"%s\" is a pipe, and %s capture files can't be " "written to a pipe.", display_basename, wtap_file_type_subtype_string(file_type_subtype)); break; case WTAP_ERR_UNWRITABLE_FILE_TYPE: simple_error_message_box( "Wireshark doesn't support writing capture files in that format."); break; case WTAP_ERR_UNWRITABLE_ENCAP: simple_error_message_box("Wireshark can't save this capture in that format."); break; case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: simple_error_message_box( "Wireshark can't save this capture in that format."); break; case WTAP_ERR_CANT_OPEN: simple_error_message_box( "The file \"%s\" could not be created for some unknown reason.", display_basename); break; case WTAP_ERR_SHORT_WRITE: simple_error_message_box( "A full header couldn't be written to the file \"%s\".", display_basename); break; case WTAP_ERR_COMPRESSION_NOT_SUPPORTED: simple_error_message_box( "This file type cannot be written as a compressed file."); break; default: simple_error_message_box( "The file \"%s\" could not be created: %s.", display_basename, wtap_strerror(err)); break; } g_free(display_basename); } else { /* OS error. */ open_failure_alert_box(filename, err, TRUE); } }
static void print_stats(const gchar *filename, capture_info *cf_info) { const gchar *file_type_string, *file_encap_string; gchar *size_string; /* Build printable strings for various stats */ file_type_string = wtap_file_type_subtype_string(cf_info->file_type); file_encap_string = wtap_encap_string(cf_info->file_encap); if (filename) printf ("File name: %s\n", filename); if (cap_file_type) printf ("File type: %s%s\n", file_type_string, cf_info->iscompressed ? " (gzip compressed)" : ""); if (cap_file_encap) { printf ("File encapsulation: %s\n", file_encap_string); if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) { int i; printf ("Encapsulation in use by packets (# of pkts):\n"); for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) { if (cf_info->encap_counts[i] > 0) printf(" %s (%d)\n", wtap_encap_string(i), cf_info->encap_counts[i]); } } } if (cap_file_more_info) { printf ("File timestamp precision: %s (%d)\n", wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec); } if (cap_snaplen && cf_info->snap_set) printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen); else if (cap_snaplen && !cf_info->snap_set) printf ("Packet size limit: file hdr: (not set)\n"); if (cf_info->snaplen_max_inferred > 0) { if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred) printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred); else printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n", cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred); } if (cap_packet_count) { printf ("Number of packets: "); if (machine_readable) { printf ("%u\n", cf_info->packet_count); } else { size_string = format_size(cf_info->packet_count, format_size_unit_none); printf ("%s\n", size_string); g_free(size_string); } } if (cap_file_size) { printf ("File size: "); if (machine_readable) { printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize); } else { size_string = format_size(cf_info->filesize, format_size_unit_bytes); printf ("%s\n", size_string); g_free(size_string); } } if (cap_data_size) { printf ("Data size: "); if (machine_readable) { printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes); } else { size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes); printf ("%s\n", size_string); g_free(size_string); } } if (cf_info->times_known) { if (cap_duration) /* XXX - shorten to hh:mm:ss */ printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE)); if (cap_start_time) printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info)); if (cap_end_time) printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info)); if (cap_data_rate_byte) { printf("Data byte rate: "); if (machine_readable) { print_value("", 2, " bytes/sec", cf_info->data_rate); } else { size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s); printf ("%s\n", size_string); g_free(size_string); } } if (cap_data_rate_bit) { printf("Data bit rate: "); if (machine_readable) { print_value("", 2, " bits/sec", cf_info->data_rate*8); } else { size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s); printf ("%s\n", size_string); g_free(size_string); } } } if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size); if (cf_info->times_known) { if (cap_packet_rate) { printf("Average packet rate: "); if (machine_readable) { print_value("", 2, " packets/sec", cf_info->packet_rate); } else { size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s); printf ("%s\n", size_string); g_free(size_string); } } } #ifdef HAVE_LIBGCRYPT if (cap_file_hashes) { printf ("SHA1: %s\n", file_sha1); printf ("RIPEMD160: %s\n", file_rmd160); printf ("MD5: %s\n", file_md5); } #endif /* HAVE_LIBGCRYPT */ if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order)); if (cap_comment && cf_info->comment) printf ("Capture comment: %s\n", cf_info->comment); if (cap_file_more_info) { if (cf_info->hardware) printf ("Capture hardware: %s\n", cf_info->hardware); if (cf_info->os) printf ("Capture oper-sys: %s\n", cf_info->os); if (cf_info->usr_appl) printf ("Capture application: %s\n", cf_info->usr_appl); } if (cap_file_idb && cf_info->num_interfaces != 0) { guint i; g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len); printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces); for (i = 0; i < cf_info->idb_info_strings->len; i++) { gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i); printf ("Interface #%u info:\n", i); printf ("%s", s); printf (" Number of packets = %u\n", cf_info->interface_ids[i]); } }
// Copied from capture_file_properties_dialog.cpp QString GsmMapSummaryDialog::summaryToHtml() { summary_tally summary; memset(&summary, 0, sizeof(summary_tally)); QString section_tmpl; QString table_begin, table_end; QString table_row_begin, table_ul_row_begin, table_row_end; QString table_vheader_tmpl; QString table_data_tmpl; section_tmpl = "<p><strong>%1</strong></p>\n"; table_begin = "<p><table>\n"; table_end = "</table></p>\n"; table_row_begin = "<tr>\n"; table_ul_row_begin = "<tr style=\"border-bottom: 1px solid gray;\">\n"; table_row_end = "</tr>\n"; table_vheader_tmpl = "<td width=\"50%\">%1:</td>"; // <th align="left"> looked odd table_data_tmpl = "<td>%1</td>"; if (cap_file_.isValid()) { /* initial computations */ summary_fill_in(cap_file_.capFile(), &summary); #ifdef HAVE_LIBPCAP summary_fill_in_capture(cap_file_.capFile(), &global_capture_opts, &summary); #endif } QString summary_str; QTextStream out(&summary_str); // File Section out << section_tmpl.arg(tr("File")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Name")) << table_data_tmpl.arg(summary.filename) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Length")) << table_data_tmpl.arg(file_size_to_qstring(summary.file_length)) << table_row_end; QString format_str = wtap_file_type_subtype_string(summary.file_type); if (summary.iscompressed) { format_str.append(tr(" (gzip compressed)")); } out << table_row_begin << table_vheader_tmpl.arg(tr("Format")) << table_data_tmpl.arg(format_str) << table_row_end; if (summary.has_snap) { out << table_row_begin << table_vheader_tmpl.arg(tr("Snapshot length")) << table_data_tmpl.arg(summary.snap) << table_row_end; } out << table_end; // Data Section out << section_tmpl.arg(tr("Data")); out << table_begin; if (summary.packet_count_ts == summary.packet_count && summary.packet_count >= 1) { // start time out << table_row_begin << table_vheader_tmpl.arg(tr("First packet")) << table_data_tmpl.arg(time_t_to_qstring((time_t)summary.start_time)) << table_row_end; // stop time out << table_row_begin << table_vheader_tmpl.arg(tr("Last packet")) << table_data_tmpl.arg(time_t_to_qstring((time_t)summary.stop_time)) << table_row_end; // elapsed seconds (capture duration) if (summary.packet_count_ts > 1) { /* elapsed seconds */ QString elapsed_str; unsigned int elapsed_time = (unsigned int)summary.elapsed_time; if (elapsed_time/86400) { elapsed_str = QString("%1 days ").arg(elapsed_time / 86400); } elapsed_str += QString("%1:%2:%3") .arg(elapsed_time % 86400 / 3600, 2, 10, QChar('0')) .arg(elapsed_time % 3600 / 60, 2, 10, QChar('0')) .arg(elapsed_time % 60, 2, 10, QChar('0')); out << table_row_begin << table_vheader_tmpl.arg(tr("Elapsed")) << table_data_tmpl.arg(elapsed_str) << table_row_end; } } // count out << table_row_begin << table_vheader_tmpl.arg(tr("Packets")) << table_data_tmpl.arg(summary.packet_count) << table_row_end; out << table_end; // TRANSLATOR Abbreviation for "not applicable" QString n_a = tr("N/A"); QString invoke_rate_str, result_rate_str, total_rate_str; QString invoke_avg_size_str, result_avg_size_str, total_avg_size_str; // Message averages invoke_rate_str = result_rate_str = total_rate_str = n_a; invoke_avg_size_str = result_avg_size_str = total_avg_size_str = n_a; double seconds = summary.stop_time - summary.start_time; int invoke_count = 0, invoke_bytes = 0; int result_count = 0, result_bytes = 0; for (int i = 0; i < GSM_MAP_MAX_NUM_OPR_CODES; i++) { invoke_count += gsm_map_stat.opr_code[i]; invoke_bytes += gsm_map_stat.size[i]; } for (int i = 0; i < GSM_MAP_MAX_NUM_OPR_CODES; i++) { result_count += gsm_map_stat.opr_code_rr[i]; result_bytes += gsm_map_stat.size_rr[i]; } int total_count = invoke_count + result_count; int total_bytes = invoke_bytes + result_bytes; /* * We must have no un-time-stamped packets (i.e., the number of * time-stamped packets must be the same as the number of packets), * and at least two time-stamped packets, in order for the elapsed * time to be valid. */ if (summary.packet_count_ts > 1 && seconds > 0.0) { /* Total number of invokes per second */ invoke_rate_str = QString("%1").arg(invoke_count / seconds, 1, 'f', 1); result_rate_str = QString("%1").arg(result_count / seconds, 1, 'f', 1); total_rate_str = QString("%1").arg((total_count) / seconds, 1, 'f', 1); } /* Average message sizes */ if (invoke_count > 0) { invoke_avg_size_str = QString("%1").arg((double) invoke_bytes / invoke_count, 1, 'f', 1); } if (result_count > 0) { result_avg_size_str = QString("%1").arg((double) result_bytes / result_count, 1, 'f', 1); } if (total_count > 0) { total_avg_size_str = QString("%1").arg((double) total_bytes / total_count, 1, 'f', 1); } // Invoke Section out << section_tmpl.arg(tr("Invokes")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of Invokes")) << table_data_tmpl.arg(invoke_count) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of Invokes per second")) << table_data_tmpl.arg(invoke_rate_str) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of bytes for Invokes")) << table_data_tmpl.arg(invoke_bytes) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of bytes per Invoke")) << table_data_tmpl.arg(invoke_avg_size_str) << table_row_end; out << table_end; // Return Result Section out << section_tmpl.arg(tr("Return Results")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of Return Results")) << table_data_tmpl.arg(result_count) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of Return Results per second")) << table_data_tmpl.arg(result_rate_str) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of bytes for Return Results")) << table_data_tmpl.arg(result_bytes) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of bytes per Return Result")) << table_data_tmpl.arg(result_avg_size_str) << table_row_end; out << table_end; // Total Section out << section_tmpl.arg(tr("Totals")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of GSM MAP messages")) << table_data_tmpl.arg(total_count) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of GSM MAP messages per second")) << table_data_tmpl.arg(total_rate_str) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Total number of bytes for GSM MAP messages")) << table_data_tmpl.arg(total_bytes) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average number of bytes per GSM MAP message")) << table_data_tmpl.arg(total_avg_size_str) << table_row_end; out << table_end; return summary_str; }
QString Mtp3SummaryDialog::summaryToHtml() { summary_tally summary; memset(&summary, 0, sizeof(summary_tally)); QString section_tmpl; QString table_begin, table_end; QString table_row_begin, table_ul_row_begin, table_row_end; QString table_vheader_tmpl, table_hheader15_tmpl, table_hheader25_tmpl; QString table_data_tmpl; section_tmpl = "<p><strong>%1</strong></p>\n"; table_begin = "<p><table>\n"; table_end = "</table></p>\n"; table_row_begin = "<tr>\n"; table_ul_row_begin = "<tr style=\"border-bottom: 1px solid gray;\">\n"; table_row_end = "</tr>\n"; table_vheader_tmpl = "<td width=\"50%\">%1:</td>"; // <th align="left"> looked odd table_hheader15_tmpl = "<td width=\"15%\"><u>%1</u></td>"; table_hheader25_tmpl = "<td width=\"25%\"><u>%1</u></td>"; table_data_tmpl = "<td>%1</td>"; if (cap_file_.isValid()) { /* initial computations */ summary_fill_in(cap_file_.capFile(), &summary); #ifdef HAVE_LIBPCAP summary_fill_in_capture(cap_file_.capFile(), &global_capture_opts, &summary); #endif } QString summary_str; QTextStream out(&summary_str); // File Section out << section_tmpl.arg(tr("File")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Name")) << table_data_tmpl.arg(summary.filename) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Length")) << table_data_tmpl.arg(file_size_to_qstring(summary.file_length)) << table_row_end; QString format_str = wtap_file_type_subtype_string(summary.file_type); if (summary.iscompressed) { format_str.append(tr(" (gzip compressed)")); } out << table_row_begin << table_vheader_tmpl.arg(tr("Format")) << table_data_tmpl.arg(format_str) << table_row_end; if (summary.has_snap) { out << table_row_begin << table_vheader_tmpl.arg(tr("Snapshot length")) << table_data_tmpl.arg(summary.snap) << table_row_end; } out << table_end; // Data Section out << section_tmpl.arg(tr("Data")); out << table_begin; if (summary.packet_count_ts == summary.packet_count && summary.packet_count >= 1) { // start time out << table_row_begin << table_vheader_tmpl.arg(tr("First packet")) << table_data_tmpl.arg(time_t_to_qstring((time_t)summary.start_time)) << table_row_end; // stop time out << table_row_begin << table_vheader_tmpl.arg(tr("Last packet")) << table_data_tmpl.arg(time_t_to_qstring((time_t)summary.stop_time)) << table_row_end; // elapsed seconds (capture duration) if (summary.packet_count_ts > 1) { /* elapsed seconds */ QString elapsed_str; unsigned int elapsed_time = (unsigned int)summary.elapsed_time; if (elapsed_time/86400) { elapsed_str = QString("%1 days ").arg(elapsed_time / 86400); } elapsed_str += QString("%1:%2:%3") .arg(elapsed_time % 86400 / 3600, 2, 10, QChar('0')) .arg(elapsed_time % 3600 / 60, 2, 10, QChar('0')) .arg(elapsed_time % 60, 2, 10, QChar('0')); out << table_row_begin << table_vheader_tmpl.arg(tr("Elapsed")) << table_data_tmpl.arg(elapsed_str) << table_row_end; } } // count out << table_row_begin << table_vheader_tmpl.arg(tr("Packets")) << table_data_tmpl.arg(summary.packet_count) << table_row_end; out << table_end; // TRANSLATOR Abbreviation for "not applicable" QString n_a = tr("N/A"); int total_msus = 0; int total_bytes = 0; double seconds = summary.stop_time - summary.start_time; // SI Section out << section_tmpl.arg(tr("Service Indicator (SI) Totals")); out << table_begin; out << table_row_begin << table_hheader25_tmpl.arg(tr("SI")) << table_hheader15_tmpl.arg(tr("MSUs")) << table_hheader15_tmpl.arg(tr("MSUs/s")) << table_hheader15_tmpl.arg(tr("Bytes")) << table_hheader15_tmpl.arg(tr("Bytes/MSU")) << table_hheader15_tmpl.arg(tr("Bytes/s")) << table_row_end; for (size_t si_code = 0; si_code < MTP3_NUM_SI_CODE; si_code++) { int si_msus = 0; int si_bytes = 0; QString msus_s_str = n_a; QString bytes_msu_str = n_a; QString bytes_s_str = n_a; for (size_t stat_idx = 0; stat_idx < mtp3_num_used; stat_idx++) { si_msus += mtp3_stat[stat_idx].mtp3_si_code[si_code].num_msus; si_bytes += mtp3_stat[stat_idx].mtp3_si_code[si_code].size; } total_msus += si_msus; total_bytes += si_bytes; if (seconds > 0) { msus_s_str = QString("%1").arg(si_msus / seconds, 1, 'f', 1); bytes_s_str = QString("%1").arg(si_bytes / seconds, 1, 'f', 1); } if (si_msus > 0) { bytes_msu_str = QString("%1").arg((double) si_bytes / si_msus, 1, 'f', 1); } out << table_row_begin << table_data_tmpl.arg(mtp3_service_indicator_code_short_vals[si_code].strptr) << table_data_tmpl.arg(si_msus) << table_data_tmpl.arg(msus_s_str) << table_data_tmpl.arg(si_bytes) << table_data_tmpl.arg(bytes_msu_str) << table_data_tmpl.arg(bytes_s_str) << table_row_end; } out << table_end; // Totals Section QString total_msus_s_str = n_a; QString total_bytes_msu_str = n_a; QString total_bytes_s_str = n_a; if (seconds > 0) { total_msus_s_str = QString("%1").arg(total_msus / seconds, 1, 'f', 1); total_bytes_s_str = QString("%1").arg(total_bytes / seconds, 1, 'f', 1); } if (total_msus > 0) { total_bytes_msu_str = QString("%1").arg((double) total_bytes / total_msus, 1, 'f', 1); } out << section_tmpl.arg(tr("Totals")); out << table_begin; out << table_row_begin << table_vheader_tmpl.arg(tr("Total MSUs")) << table_data_tmpl.arg(total_msus) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("MSUs/s")) << table_data_tmpl.arg(total_msus_s_str) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Total Bytes")) << table_data_tmpl.arg(total_bytes) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average Bytes/MSU")) << table_data_tmpl.arg(total_bytes_msu_str) << table_row_end; out << table_row_begin << table_vheader_tmpl.arg(tr("Average Bytes/s")) << table_data_tmpl.arg(total_bytes_s_str) << table_row_end; out << table_end; return summary_str; }
static const char * cf_open_error_message(int err, gchar *err_info, gboolean for_writing, int file_type) { const char *errmsg; static char errmsg_errno[1024+1]; if (err < 0) { /* Wiretap error. */ switch (err) { case WTAP_ERR_NOT_REGULAR_FILE: errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file."; break; case WTAP_ERR_FILE_UNKNOWN_FORMAT: /* Seen only when opening a capture file for reading. */ errmsg = "The file \"%s\" isn't a capture file in a format TShark understands."; break; case WTAP_ERR_UNSUPPORTED: /* Seen only when opening a capture file for reading. */ g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The file \"%%s\" isn't a capture file in a format TShark understands.\n" "(%s)", err_info); g_free(err_info); errmsg = errmsg_errno; break; case WTAP_ERR_CANT_WRITE_TO_PIPE: /* Seen only when opening a capture file for writing. */ g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The file \"%%s\" is a pipe, and %s capture files can't be " "written to a pipe.", wtap_file_type_subtype_string(file_type)); errmsg = errmsg_errno; break; case WTAP_ERR_UNSUPPORTED_FILE_TYPE: /* Seen only when opening a capture file for writing. */ errmsg = "TShark doesn't support writing capture files in that format."; break; case WTAP_ERR_UNSUPPORTED_ENCAP: if (for_writing) errmsg = "TShark can't save this capture in that format."; else { g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The file \"%%s\" is a capture for a network type that TShark doesn't support.\n" "(%s)", err_info); g_free(err_info); errmsg = errmsg_errno; } break; case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: if (for_writing) errmsg = "TShark can't save this capture in that format."; else errmsg = "The file \"%s\" is a capture for a network type that TShark doesn't support."; break; case WTAP_ERR_BAD_FILE: /* Seen only when opening a capture file for reading. */ g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The file \"%%s\" appears to be damaged or corrupt.\n" "(%s)", err_info); g_free(err_info); errmsg = errmsg_errno; break; case WTAP_ERR_CANT_OPEN: if (for_writing) errmsg = "The file \"%s\" could not be created for some unknown reason."; else errmsg = "The file \"%s\" could not be opened for some unknown reason."; break; case WTAP_ERR_SHORT_READ: errmsg = "The file \"%s\" appears to have been cut short" " in the middle of a packet or other data."; break; case WTAP_ERR_SHORT_WRITE: errmsg = "A full header couldn't be written to the file \"%s\"."; break; case WTAP_ERR_DECOMPRESS: g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The compressed file \"%%s\" appears to be damaged or corrupt.\n" "(%s)", err_info); g_free(err_info); errmsg = errmsg_errno; break; default: g_snprintf(errmsg_errno, sizeof(errmsg_errno), "The file \"%%s\" could not be %s: %s.", for_writing ? "created" : "opened", wtap_strerror(err)); errmsg = errmsg_errno; break; } } else errmsg = file_open_error_message(err, for_writing); return errmsg; }
static gchar* get_write_error_string(const merge_in_file_t *in_file, const int file_type, const gchar* out_filename, const int *err, gchar **err_info) { GString *err_message = g_string_new(""); gchar *display_basename = NULL; int write_err; /* in_file may be NULL */ g_assert(err != NULL); g_assert(err_info != NULL); if (*err_info == NULL) { *err_info = g_strdup("no information supplied"); } write_err = *err; display_basename = g_filename_display_basename(in_file ? in_file->filename : "UNKNOWN"); if (write_err < 0) { switch (write_err) { case WTAP_ERR_UNWRITABLE_ENCAP: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're wwriting; note that, and * report the frame number and file type/subtype. */ g_string_printf(err_message, "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n", in_file ? in_file->packet_num : 0, display_basename, wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_PACKET_TOO_LARGE: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're writing; note that, and report * the frame number and file type/subtype. */ g_string_printf(err_message, "Frame %u of \"%s\" is too large for a \"%s\" file.", in_file ? in_file->packet_num : 0, display_basename, wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_UNWRITABLE_REC_TYPE: /* * This is a problem with the particular record we're writing and * the file type and subtype we're writing; note that, and report * the record number and file type/subtype. */ g_string_printf(err_message, "Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.", in_file ? in_file->packet_num : 0, display_basename, wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_UNWRITABLE_REC_DATA: /* * This is a problem with the particular record we're writing and * the file type and subtype we're writing; note that, and report * the frame number and file type/subtype. */ g_string_printf(err_message, "Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)", in_file ? in_file->packet_num : 0, display_basename, wtap_file_type_subtype_string(file_type), *err_info); break; default: g_string_printf(err_message, "An error occurred while writing to the file \"%s\": %s.", out_filename, wtap_strerror(write_err)); break; } } else { /* OS error. */ g_string_printf(err_message, file_write_error_message(write_err), out_filename); } g_free(display_basename); g_free(*err_info); *err_info = g_string_free(err_message, FALSE); return *err_info; }
int main(int argc, char *argv[]) { int opt; gboolean do_append = FALSE; gboolean verbose = FALSE; int in_file_count = 0; guint snaplen = 0; #ifdef PCAP_NG_DEFAULT int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */ #else int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */ #endif int frame_type = -2; int out_fd; merge_in_file_t *in_files = NULL, *in_file; int i; struct wtap_pkthdr *phdr, snap_phdr; wtap_dumper *pdh; int open_err, read_err = 0, write_err, close_err; gchar *err_info; int err_fileno; char *out_filename = NULL; gboolean got_read_error = FALSE, got_write_error = FALSE; int count; #ifdef _WIN32 arg_list_utf_16to8(argc, argv); create_app_running_mutex(); #endif /* _WIN32 */ /* Process the options first */ while ((opt = getopt(argc, argv, "aF:hs:T:vw:")) != -1) { switch (opt) { case 'a': do_append = !do_append; break; case 'F': file_type = wtap_short_string_to_file_type_subtype(optarg); if (file_type < 0) { fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n", optarg); list_capture_types(); exit(1); } break; case 'h': usage(); exit(0); break; case 's': snaplen = get_positive_int(optarg, "snapshot length"); break; case 'T': frame_type = wtap_short_string_to_encap(optarg); if (frame_type < 0) { fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n", optarg); list_encap_types(); exit(1); } break; case 'v': verbose = TRUE; break; case 'w': out_filename = optarg; break; case '?': /* Bad options if GNU getopt */ switch(optopt) { case'F': list_capture_types(); break; case'T': list_encap_types(); break; default: usage(); } exit(1); break; } } /* check for proper args; at a minimum, must have an output * filename and one input file */ in_file_count = argc - optind; if (!out_filename) { fprintf(stderr, "mergecap: an output filename must be set with -w\n"); fprintf(stderr, " run with -h for help\n"); return 1; } if (in_file_count < 1) { fprintf(stderr, "mergecap: No input files were specified\n"); return 1; } /* open the input files */ if (!merge_open_in_files(in_file_count, &argv[optind], &in_files, &open_err, &err_info, &err_fileno)) { fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno], wtap_strerror(open_err)); switch (open_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; } return 2; } if (verbose) { for (i = 0; i < in_file_count; i++) fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i], wtap_file_type_subtype_string(wtap_file_type_subtype(in_files[i].wth))); } if (snaplen == 0) { /* * Snapshot length not specified - default to the maximum of the * snapshot lengths of the input files. */ snaplen = merge_max_snapshot_length(in_file_count, in_files); } /* set the outfile frame type */ if (frame_type == -2) { /* * Default to the appropriate frame type for the input files. */ frame_type = merge_select_frame_type(in_file_count, in_files); if (verbose) { if (frame_type == WTAP_ENCAP_PER_PACKET) { /* * Find out why we had to choose WTAP_ENCAP_PER_PACKET. */ int first_frame_type, this_frame_type; first_frame_type = wtap_file_encap(in_files[0].wth); for (i = 1; i < in_file_count; i++) { this_frame_type = wtap_file_encap(in_files[i].wth); if (first_frame_type != this_frame_type) { fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n"); fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n"); fprintf(stderr, " %s had type %s (%s)\n", in_files[0].filename, wtap_encap_string(first_frame_type), wtap_encap_short_string(first_frame_type)); fprintf(stderr, " %s had type %s (%s)\n", in_files[i].filename, wtap_encap_string(this_frame_type), wtap_encap_short_string(this_frame_type)); break; } } } fprintf(stderr, "mergecap: selected frame_type %s (%s)\n", wtap_encap_string(frame_type), wtap_encap_short_string(frame_type)); } } /* open the outfile */ if (strncmp(out_filename, "-", 2) == 0) { /* use stdout as the outfile */ out_fd = 1 /*stdout*/; } else { /* open the outfile */ out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); if (out_fd == -1) { fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n", out_filename, g_strerror(errno)); exit(1); } } /* prepare the outfile */ if(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG ){ wtapng_section_t *shb_hdr; GString *comment_gstr; shb_hdr = g_new(wtapng_section_t,1); comment_gstr = g_string_new("File created by merging: \n"); for (i = 0; i < in_file_count; i++) { g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename); } shb_hdr->section_length = -1; /* options */ shb_hdr->opt_comment = comment_gstr->str; /* NULL if not available */ shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */ shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */ shb_hdr->shb_user_appl = "mergecap"; /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */ pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, shb_hdr, NULL /* wtapng_iface_descriptions_t *idb_inf */, &open_err); g_string_free(comment_gstr, TRUE); } else { pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err); } if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename, wtap_strerror(open_err)); exit(1); } /* do the merge (or append) */ count = 1; for (;;) { if (do_append) in_file = merge_append_read_packet(in_file_count, in_files, &read_err, &err_info); else in_file = merge_read_packet(in_file_count, in_files, &read_err, &err_info); if (in_file == NULL) { /* EOF */ break; } if (read_err != 0) { /* I/O error reading from in_file */ got_read_error = TRUE; break; } if (verbose) fprintf(stderr, "Record: %u\n", count++); /* We simply write it, perhaps after truncating it; we could do other * things, like modify it. */ phdr = wtap_phdr(in_file->wth); if (snaplen != 0 && phdr->caplen > snaplen) { snap_phdr = *phdr; snap_phdr.caplen = snaplen; phdr = &snap_phdr; } if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err)) { got_write_error = TRUE; break; } } merge_close_in_files(in_file_count, in_files); if (!got_read_error && !got_write_error) { if (!wtap_dump_close(pdh, &write_err)) got_write_error = TRUE; } else wtap_dump_close(pdh, &close_err); if (got_read_error) { /* * Find the file on which we got the error, and report the error. */ for (i = 0; i < in_file_count; i++) { if (in_files[i].state == GOT_ERROR) { fprintf(stderr, "mergecap: Error reading %s: %s\n", in_files[i].filename, wtap_strerror(read_err)); switch (read_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; } } } } if (got_write_error) { switch (write_err) { case WTAP_ERR_UNSUPPORTED_ENCAP: /* * This is a problem with the particular frame we're writing; * note that, and give the frame number. */ fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.", in_file->packet_num, in_file->filename); break; default: fprintf(stderr, "mergecap: Error writing to outfile: %s\n", wtap_strerror(write_err)); break; } } g_free(in_files); return (!got_read_error && !got_write_error) ? 0 : 2; }
static void print_stats_table(const gchar *filename, capture_info *cf_info) { const gchar *file_type_string, *file_encap_string; time_t start_time_t; time_t stop_time_t; /* Build printable strings for various stats */ file_type_string = wtap_file_type_subtype_string(cf_info->file_type); file_encap_string = wtap_encap_string(cf_info->file_encap); start_time_t = (time_t)cf_info->start_time; stop_time_t = (time_t)cf_info->stop_time; if (filename) { putquote(); printf("%s", filename); putquote(); } if (cap_file_type) { putsep(); putquote(); printf("%s", file_type_string); putquote(); } /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered; * Output a line for each different encap with all fields repeated except * the encapsulation field which has "Per Packet: ..." for each * encapsulation type seen ? */ if (cap_file_encap) { putsep(); putquote(); printf("%s", file_encap_string); putquote(); } if (cap_snaplen) { putsep(); putquote(); if (cf_info->snap_set) printf("%u", cf_info->snaplen); else printf("(not set)"); putquote(); if (cf_info->snaplen_max_inferred > 0) { putsep(); putquote(); printf("%u", cf_info->snaplen_min_inferred); putquote(); putsep(); putquote(); printf("%u", cf_info->snaplen_max_inferred); putquote(); } else { putsep(); putquote(); printf("n/a"); putquote(); putsep(); putquote(); printf("n/a"); putquote(); } } if (cap_packet_count) { putsep(); putquote(); printf("%u", cf_info->packet_count); putquote(); } if (cap_file_size) { putsep(); putquote(); printf("%" G_GINT64_MODIFIER "d", cf_info->filesize); putquote(); } if (cap_data_size) { putsep(); putquote(); printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes); putquote(); } if (cap_duration) { putsep(); putquote(); if (cf_info->times_known) printf("%f", cf_info->duration); else printf("n/a"); putquote(); } if (cap_start_time) { putsep(); putquote(); printf("%s", time_string(start_time_t, cf_info, FALSE)); putquote(); } if (cap_end_time) { putsep(); putquote(); printf("%s", time_string(stop_time_t, cf_info, FALSE)); putquote(); } if (cap_data_rate_byte) { putsep(); putquote(); if (cf_info->times_known) printf("%.2f", cf_info->data_rate); else printf("n/a"); putquote(); } if (cap_data_rate_bit) { putsep(); putquote(); if (cf_info->times_known) printf("%.2f", cf_info->data_rate*8); else printf("n/a"); putquote(); } if (cap_packet_size) { putsep(); putquote(); printf("%.2f", cf_info->packet_size); putquote(); } if (cap_packet_rate) { putsep(); putquote(); if (cf_info->times_known) printf("%.2f", cf_info->packet_rate); else printf("n/a"); putquote(); } #ifdef HAVE_LIBGCRYPT if (cap_file_hashes) { putsep(); putquote(); printf("%s", file_sha1); putquote(); putsep(); putquote(); printf("%s", file_rmd160); putquote(); putsep(); putquote(); printf("%s", file_md5); putquote(); } #endif /* HAVE_LIBGCRYPT */ if (cap_order) { putsep(); putquote(); printf("%s", order_string(cf_info->order)); putquote(); } if (cap_comment) { putsep(); putquote(); printf("%s", cf_info->comment); putquote(); } printf("\n"); }
static void print_stats(const gchar *filename, capture_info *cf_info) { const gchar *file_type_string, *file_encap_string; time_t start_time_t; time_t stop_time_t; gchar *size_string; /* Build printable strings for various stats */ file_type_string = wtap_file_type_subtype_string(cf_info->file_type); file_encap_string = wtap_encap_string(cf_info->file_encap); start_time_t = (time_t)cf_info->start_time; stop_time_t = (time_t)cf_info->stop_time; if (filename) printf ("File name: %s\n", filename); if (cap_file_type) printf ("File type: %s%s\n", file_type_string, cf_info->iscompressed ? " (gzip compressed)" : ""); if (cap_file_encap) printf ("File encapsulation: %s\n", file_encap_string); if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) { int i; for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) { if (cf_info->encap_counts[i] > 0) printf(" %s\n", wtap_encap_string(i)); } } if (cap_snaplen && cf_info->snap_set) printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen); else if (cap_snaplen && !cf_info->snap_set) printf ("Packet size limit: file hdr: (not set)\n"); if (cf_info->snaplen_max_inferred > 0) { if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred) printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred); else printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n", cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred); } if (cap_packet_count) { printf ("Number of packets: "); if (machine_readable) { printf ("%u\n", cf_info->packet_count); } else { size_string = format_size(cf_info->packet_count, format_size_unit_none); printf ("%s\n", size_string); g_free(size_string); } } if (cap_file_size) { printf ("File size: "); if (machine_readable) { printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize); } else { size_string = format_size(cf_info->filesize, format_size_unit_bytes); printf ("%s\n", size_string); g_free(size_string); } } if (cap_data_size) { printf ("Data size: "); if (machine_readable) { printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes); } else { size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes); printf ("%s\n", size_string); g_free(size_string); } } if (cf_info->times_known) { if (cap_duration) /* XXX - shorten to hh:mm:ss */ print_value("Capture duration: ", 0, " seconds", cf_info->duration); if (cap_start_time) printf ("Start time: %s", time_string(start_time_t, cf_info, TRUE)); if (cap_end_time) printf ("End time: %s", time_string(stop_time_t, cf_info, TRUE)); if (cap_data_rate_byte) { printf ("Data byte rate: "); if (machine_readable) { print_value("", 2, " bytes/sec", cf_info->data_rate); } else { size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s); printf ("%s\n", size_string); g_free(size_string); } } if (cap_data_rate_bit) { printf ("Data bit rate: "); if (machine_readable) { print_value("", 2, " bits/sec", cf_info->data_rate*8); } else { size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s); printf ("%s\n", size_string); g_free(size_string); } } } if (cap_packet_size) printf ("Average packet size: %.2f bytes\n", cf_info->packet_size); if (cf_info->times_known) { if (cap_packet_rate) { printf ("Average packet rate: "); if (machine_readable) { print_value("", 2, " packets/sec", cf_info->packet_rate); } else { size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none); printf ("%spackets/sec\n", size_string); g_free(size_string); } } } #ifdef HAVE_LIBGCRYPT if (cap_file_hashes) { printf ("SHA1: %s\n", file_sha1); printf ("RIPEMD160: %s\n", file_rmd160); printf ("MD5: %s\n", file_md5); } #endif /* HAVE_LIBGCRYPT */ if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order)); if (cap_comment && cf_info->comment) printf ("Capture comment: %s\n", cf_info->comment); }
gtk_widget_show(file_fr); file_box = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE); gtk_container_add(GTK_CONTAINER(file_fr), file_box); gtk_widget_show(file_box); /* filename */ g_snprintf(string_buff, SUM_STR_MAX, "Name: %s", ((summary.filename) ? summary.filename : "None")); add_string_to_box(string_buff, file_box); /* length */ g_snprintf(string_buff, SUM_STR_MAX, "Length: %" G_GINT64_MODIFIER "d", summary.file_length); add_string_to_box(string_buff, file_box); /* format */ g_snprintf(string_buff, SUM_STR_MAX, "Format: %s", wtap_file_type_subtype_string(summary.file_type)); add_string_to_box(string_buff, file_box); if (summary.has_snap) { /* snapshot length */ g_snprintf(string_buff, SUM_STR_MAX, "Snapshot length: %u", summary.snap); add_string_to_box(string_buff, file_box); } /* Data frame */ data_fr = gtk_frame_new("Data"); gtk_box_pack_start(GTK_BOX (main_vb), data_fr, TRUE, TRUE, 0); gtk_widget_show(data_fr); data_box = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE); gtk_container_add(GTK_CONTAINER(data_fr), data_box);
int main(int argc, char *argv[]) { GString *comp_info_str; GString *runtime_info_str; int opt; DIAG_OFF(cast-qual) static const struct option long_options[] = { {(char *)"help", no_argument, NULL, 'h'}, {(char *)"version", no_argument, NULL, 'V'}, {0, 0, 0, 0 } }; DIAG_ON(cast-qual) gboolean do_append = FALSE; gboolean verbose = FALSE; int in_file_count = 0; guint snaplen = 0; #ifdef PCAP_NG_DEFAULT int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */ #else int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */ #endif int frame_type = -2; int out_fd; merge_in_file_t *in_files = NULL, *in_file; int i; struct wtap_pkthdr *phdr, snap_phdr; wtap_dumper *pdh; int open_err, read_err = 0, write_err, close_err; gchar *err_info, *write_err_info = NULL; int err_fileno; char *out_filename = NULL; gboolean got_read_error = FALSE, got_write_error = FALSE; int count; cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont); #ifdef _WIN32 arg_list_utf_16to8(argc, argv); create_app_running_mutex(); #endif /* _WIN32 */ /* Get the compile-time version information string */ comp_info_str = get_compiled_version_info(NULL, get_mergecap_compiled_info); /* Get the run-time version information string */ runtime_info_str = get_runtime_version_info(get_mergecap_runtime_info); /* Add it to the information to be reported on a crash. */ ws_add_crash_info("Mergecap (Wireshark) %s\n" "\n" "%s" "\n" "%s", get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str); /* Process the options first */ while ((opt = getopt_long(argc, argv, "aF:hs:T:vVw:", long_options, NULL)) != -1) { switch (opt) { case 'a': do_append = !do_append; break; case 'F': file_type = wtap_short_string_to_file_type_subtype(optarg); if (file_type < 0) { fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n", optarg); list_capture_types(); exit(1); } break; case 'h': printf("Mergecap (Wireshark) %s\n" "Merge two or more capture files into one.\n" "See http://www.wireshark.org for more information.\n", get_ws_vcs_version_info()); print_usage(stdout); exit(0); break; case 's': snaplen = get_positive_int(optarg, "snapshot length"); break; case 'T': frame_type = wtap_short_string_to_encap(optarg); if (frame_type < 0) { fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n", optarg); list_encap_types(); exit(1); } break; case 'v': verbose = TRUE; break; case 'V': show_version("Mergecap (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 'w': out_filename = optarg; break; case '?': /* Bad options if GNU getopt */ switch(optopt) { case'F': list_capture_types(); break; case'T': list_encap_types(); break; default: print_usage(stderr); } exit(1); break; } } /* check for proper args; at a minimum, must have an output * filename and one input file */ in_file_count = argc - optind; if (!out_filename) { fprintf(stderr, "mergecap: an output filename must be set with -w\n"); fprintf(stderr, " run with -h for help\n"); return 1; } if (in_file_count < 1) { fprintf(stderr, "mergecap: No input files were specified\n"); return 1; } /* open the input files */ if (!merge_open_in_files(in_file_count, &argv[optind], &in_files, &open_err, &err_info, &err_fileno)) { fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno], wtap_strerror(open_err)); if (err_info != NULL) { fprintf(stderr, "(%s)\n", err_info); g_free(err_info); } return 2; } if (verbose) { for (i = 0; i < in_file_count; i++) fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i], wtap_file_type_subtype_string(wtap_file_type_subtype(in_files[i].wth))); } if (snaplen == 0) { /* * Snapshot length not specified - default to the maximum of the * snapshot lengths of the input files. */ snaplen = merge_max_snapshot_length(in_file_count, in_files); } /* set the outfile frame type */ if (frame_type == -2) { /* * Default to the appropriate frame type for the input files. */ frame_type = merge_select_frame_type(in_file_count, in_files); if (verbose) { if (frame_type == WTAP_ENCAP_PER_PACKET) { /* * Find out why we had to choose WTAP_ENCAP_PER_PACKET. */ int first_frame_type, this_frame_type; first_frame_type = wtap_file_encap(in_files[0].wth); for (i = 1; i < in_file_count; i++) { this_frame_type = wtap_file_encap(in_files[i].wth); if (first_frame_type != this_frame_type) { fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n"); fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n"); fprintf(stderr, " %s had type %s (%s)\n", in_files[0].filename, wtap_encap_string(first_frame_type), wtap_encap_short_string(first_frame_type)); fprintf(stderr, " %s had type %s (%s)\n", in_files[i].filename, wtap_encap_string(this_frame_type), wtap_encap_short_string(this_frame_type)); break; } } } fprintf(stderr, "mergecap: selected frame_type %s (%s)\n", wtap_encap_string(frame_type), wtap_encap_short_string(frame_type)); } } /* open the outfile */ if (strncmp(out_filename, "-", 2) == 0) { /* use stdout as the outfile */ out_fd = 1 /*stdout*/; } else { /* open the outfile */ out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); if (out_fd == -1) { fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n", out_filename, g_strerror(errno)); exit(1); } } /* prepare the outfile */ if(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG ){ wtapng_section_t *shb_hdr; GString *comment_gstr; shb_hdr = g_new(wtapng_section_t,1); comment_gstr = g_string_new("File created by merging: \n"); for (i = 0; i < in_file_count; i++) { g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename); } shb_hdr->section_length = -1; /* options */ shb_hdr->opt_comment = comment_gstr->str; /* NULL if not available */ shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */ shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */ shb_hdr->shb_user_appl = g_strdup("mergecap"); /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */ pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, shb_hdr, NULL /* wtapng_iface_descriptions_t *idb_inf */, &open_err); g_string_free(comment_gstr, TRUE); } else { pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err); } if (pdh == NULL) { merge_close_in_files(in_file_count, in_files); g_free(in_files); fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename, wtap_strerror(open_err)); exit(1); } /* do the merge (or append) */ count = 1; for (;;) { if (do_append) in_file = merge_append_read_packet(in_file_count, in_files, &read_err, &err_info); else in_file = merge_read_packet(in_file_count, in_files, &read_err, &err_info); if (in_file == NULL) { /* EOF */ break; } if (read_err != 0) { /* I/O error reading from in_file */ got_read_error = TRUE; break; } if (verbose) fprintf(stderr, "Record: %d\n", count++); /* We simply write it, perhaps after truncating it; we could do other * things, like modify it. */ phdr = wtap_phdr(in_file->wth); if (snaplen != 0 && phdr->caplen > snaplen) { snap_phdr = *phdr; snap_phdr.caplen = snaplen; phdr = &snap_phdr; } if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) { got_write_error = TRUE; break; } } merge_close_in_files(in_file_count, in_files); if (!got_write_error) { if (!wtap_dump_close(pdh, &write_err)) got_write_error = TRUE; } else { /* * We already got a write error; no need to report another * write error on close. * * Don't overwrite the earlier write error. */ (void)wtap_dump_close(pdh, &close_err); } if (got_read_error) { /* * Find the file on which we got the error, and report the error. */ for (i = 0; i < in_file_count; i++) { if (in_files[i].state == GOT_ERROR) { fprintf(stderr, "mergecap: Error reading %s: %s\n", in_files[i].filename, wtap_strerror(read_err)); if (err_info != NULL) { fprintf(stderr, "(%s)\n", err_info); g_free(err_info); } } } } if (got_write_error) { switch (write_err) { case WTAP_ERR_UNWRITABLE_ENCAP: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're wwriting; note that, and * report the frame number and file type/subtype. */ fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n", in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN", wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_PACKET_TOO_LARGE: /* * This is a problem with the particular frame we're writing and * the file type and subtype we're wwriting; note that, and * report the frame number and file type/subtype. */ fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file.\n", in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN", wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_UNWRITABLE_REC_TYPE: /* * This is a problem with the particular record we're writing and * the file type and subtype we're wwriting; note that, and * report the record number and file type/subtype. */ fprintf(stderr, "mergecap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n", in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN", wtap_file_type_subtype_string(file_type)); break; case WTAP_ERR_UNWRITABLE_REC_DATA: /* * This is a problem with the particular record we're writing and * the file type and subtype we're wwriting; note that, and * report the record number and file type/subtype. */ fprintf(stderr, "mergecap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n", in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN", wtap_file_type_subtype_string(file_type), write_err_info != NULL ? write_err_info : "no information supplied"); g_free(write_err_info); break; default: fprintf(stderr, "mergecap: Error writing to outfile: %s\n", wtap_strerror(write_err)); break; } } g_free(in_files); return (!got_read_error && !got_write_error) ? 0 : 2; }