static void exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data) { int import_file_fd; char *tmpname, *capfile_name; int err; /* Choose a random name for the temporary import buffer */ import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_", NULL); capfile_name = g_strdup(tmpname); err = exp_pdu_open(exp_pdu_tap_data, import_file_fd, g_strdup_printf("Dump of PDUs from %s", cfile.filename)); if (err != 0) { open_failure_alert_box(capfile_name ? capfile_name : "temporary file", err, TRUE); goto end; } /* Run the tap */ cf_retap_packets(&cfile); err = exp_pdu_close(exp_pdu_tap_data); if (err!= 0) { write_failure_alert_box(capfile_name, err); } /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */ if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) { open_failure_alert_box(capfile_name, err, FALSE); goto end; } switch (cf_read(&cfile, FALSE)) { case CF_READ_OK: case CF_READ_ERROR: /* Just because we got an error, that doesn't mean we were unable to read any of the file; we handle what we could get from the file. */ break; case CF_READ_ABORTED: /* The user bailed out of re-reading the capture file; the capture file has been closed - just free the capture file name string and return (without changing the last containing directory). */ break; } end: g_free(capfile_name); }
gboolean eo_save_entry(const gchar *save_as_filename, export_object_entry_t *entry, gboolean show_err) { int to_fd; gint64 bytes_left; int bytes_to_write; ssize_t bytes_written; guint8 *ptr; int err; to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644); if(to_fd == -1) { /* An error occurred */ if (show_err) open_failure_alert_box(save_as_filename, errno, TRUE); return FALSE; } /* * The third argument to _write() on Windows is an unsigned int, * so, on Windows, that's the size of the third argument to * ws_write(). * * The third argument to write() on UN*X is a size_t, although * the return value is an ssize_t, so one probably shouldn't * write more than the max value of an ssize_t. * * In either case, there's no guarantee that a gint64 such as * payload_len can be passed to ws_write(), so we write in * chunks of, at most 2^31 bytes. */ ptr = entry->payload_data; bytes_left = entry->payload_len; while (bytes_left != 0) { if (bytes_left > 0x40000000) bytes_to_write = 0x40000000; else bytes_to_write = (int)bytes_left; bytes_written = ws_write(to_fd, ptr, bytes_to_write); if(bytes_written <= 0) { if (bytes_written < 0) err = errno; else err = WTAP_ERR_SHORT_WRITE; if (show_err) write_failure_alert_box(save_as_filename, err); ws_close(to_fd); return FALSE; } bytes_left -= bytes_written; ptr += bytes_written; } if (ws_close(to_fd) < 0) { if (show_err) write_failure_alert_box(save_as_filename, errno); return FALSE; } return TRUE; }
/* save the SSL Session Keys */ static gboolean savesslkeys_save_clicked_cb(char *file, gchar *keylist) { int fd; fd = ws_open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666); if (fd == -1) { open_failure_alert_box(file, errno, TRUE); return FALSE; } /* * Thanks, Microsoft, for not using size_t for the third argument to * _write(). Presumably this string will be <= 4GiB long.... */ if (ws_write(fd, keylist, (unsigned int)strlen(keylist)) < 0) { write_failure_alert_box(file, errno); ws_close(fd); return FALSE; } if (ws_close(fd) < 0) { write_failure_alert_box(file, errno); return FALSE; } g_free(keylist); return TRUE; }
void FollowStreamDialog::saveAs() { QString file_name = QFileDialog::getSaveFileName(this, wsApp->windowTitleString(tr("Save Stream Content As" UTF8_HORIZONTAL_ELLIPSIS))); if (!file_name.isEmpty()) { QTextStream out(&file_); file_.setFileName(file_name); if (!file_.open(QIODevice::WriteOnly)) { open_failure_alert_box(file_name.toUtf8().constData(), errno, TRUE); return; } save_as_ = true; readStream(); if ((show_type_ != SHOW_RAW) && (show_type_ != SHOW_UTF8)) { out << ui->teStreamContent->toPlainText(); } save_as_ = false; file_.close(); } }
/* save rtp dump of stream_fwd */ gboolean rtpstream_save(rtpstream_tapinfo_t *tapinfo, capture_file *cap_file, rtp_stream_info_t* stream, const gchar *filename) { gboolean was_registered; if (!tapinfo) { return FALSE; } was_registered = tapinfo->is_registered; /* open file for saving */ tapinfo->save_file = ws_fopen(filename, "wb"); if (tapinfo->save_file==NULL) { open_failure_alert_box(filename, errno, TRUE); return FALSE; } rtp_write_header(stream, tapinfo->save_file); if (ferror(tapinfo->save_file)) { write_failure_alert_box(filename, errno); fclose(tapinfo->save_file); return FALSE; } if (!tapinfo->is_registered) register_tap_listener_rtp_stream(tapinfo); tapinfo->mode = TAP_SAVE; tapinfo->filter_stream_fwd = stream; cf_retap_packets(cap_file); tapinfo->mode = TAP_ANALYSE; if (!was_registered) remove_tap_listener_rtp_stream(tapinfo); if (ferror(tapinfo->save_file)) { write_failure_alert_box(filename, errno); fclose(tapinfo->save_file); return FALSE; } if (fclose(tapinfo->save_file) == EOF) { write_failure_alert_box(filename, errno); return FALSE; } return TRUE; }
void ImportTextDialog::convertTextFile() { int import_file_fd; char *tmpname; int err; capfile_name_.clear(); /* Choose a random name for the temporary import buffer */ import_file_fd = create_tempfile(&tmpname, "import"); capfile_name_.append(tmpname); import_info_.wdh = wtap_dump_fdopen(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAP, import_info_.encapsulation, import_info_.max_frame_length, FALSE, &err); qDebug() << capfile_name_ << ":" << import_info_.wdh << import_info_.encapsulation << import_info_.max_frame_length; if (import_info_.wdh == NULL) { open_failure_alert_box(capfile_name_.toUtf8().constData(), err, TRUE); fclose(import_info_.import_text_file); setResult(QDialog::Rejected); return; } text_import_setup(&import_info_); text_importin = import_info_.import_text_file; text_importlex(); text_import_cleanup(); if (fclose(import_info_.import_text_file)) { read_failure_alert_box(import_info_.import_text_filename, errno); } if (!wtap_dump_close(import_info_.wdh, &err)) { write_failure_alert_box(capfile_name_.toUtf8().constData(), err); } }
static void exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data) { int import_file_fd; char *tmpname, *capfile_name; int 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; /* Choose a random name for the temporary import buffer */ import_file_fd = create_tempfile(&tmpname, "Wireshark_PDU_"); capfile_name = g_strdup(tmpname); /* 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("Dump of PDUs from %s", cfile.filename); shb_hdr->shb_hardware = NULL; /* UTF-8 string containing the * description of the hardware used to create this section. */ shb_hdr->shb_os = os_info_str->str; /* UTF-8 string containing the name * of the operating system used to create this section. */ g_string_free(os_info_str, FALSE); /* The actual string is not freed */ shb_hdr->shb_user_appl = appname; /* UTF-8 string containing the name * of the application used to create this section. */ /* 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, PDU->Export"); 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); exp_pdu_tap_data->wdh = 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, &err); if (exp_pdu_tap_data->wdh == NULL) { open_failure_alert_box(capfile_name, err, TRUE); goto end; } /* Run the tap */ cf_retap_packets(&cfile); if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) { write_failure_alert_box(capfile_name, err); } remove_tap_listener(exp_pdu_tap_data); /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */ if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) { open_failure_alert_box(capfile_name, err, FALSE); goto end; } switch (cf_read(&cfile, FALSE)) { case CF_READ_OK: case CF_READ_ERROR: /* Just because we got an error, that doesn't mean we were unable to read any of the file; we handle what we could get from the file. */ break; case CF_READ_ABORTED: /* The user bailed out of re-reading the capture file; the capture file has been closed - just free the capture file name string and return (without changing the last containing directory). */ break; } end: g_free(capfile_name); g_free(shb_hdr); g_free(appname); }
/* * Alert box for a failed attempt to open a capture file for reading. * "filename" is the name of the file being opened; "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.. * * 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_open_failure_alert_box(const char *filename, int err, gchar *err_info) { 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_RANDOM_OPEN_PIPE: simple_error_message_box( "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.\n" "To capture from a pipe or FIFO use wireshark -i -", display_basename); break; case WTAP_ERR_FILE_UNKNOWN_FORMAT: simple_error_message_box( "The file \"%s\" isn't a capture file in a format Wireshark understands.", display_basename); break; case WTAP_ERR_UNSUPPORTED: simple_error_message_box( "The file \"%s\" contains record data that Wireshark doesn't support.\n" "(%s)", display_basename, err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: simple_error_message_box( "The file \"%s\" is a capture for a network type that Wireshark doesn't support.", display_basename); break; case WTAP_ERR_BAD_FILE: simple_error_message_box( "The file \"%s\" appears to be damaged or corrupt.\n" "(%s)", display_basename, err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; case WTAP_ERR_CANT_OPEN: simple_error_message_box( "The file \"%s\" could not be opened for some unknown reason.", display_basename); break; case WTAP_ERR_SHORT_READ: simple_error_message_box( "The file \"%s\" appears to have been cut short" " in the middle of a packet or other data.", display_basename); break; case WTAP_ERR_DECOMPRESS: simple_error_message_box( "The file \"%s\" cannot be decompressed; it may be damaged or corrupt.\n" "(%s)", display_basename, err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED: simple_error_message_box( "The file \"%s\" cannot be decompressed; it is compressed in a way that we don't support.\n" "(%s)", display_basename, err_info != NULL ? err_info : "no information supplied"); g_free(err_info); break; default: simple_error_message_box( "The file \"%s\" could not be opened: %s.", display_basename, wtap_strerror(err)); break; } g_free(display_basename); } else { /* OS error. */ open_failure_alert_box(filename, err, FALSE); } }
/* * 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); } }
int ExportDissectionDialog::exec() { #if !defined(Q_OS_WIN) int retval; if (!cap_file_) return QDialog::Rejected; retval = QFileDialog::exec(); if (retval == QDialog::Accepted && selectedFiles().length() > 0) { cf_print_status_t status; QString file_name = selectedFiles()[0]; /* Fill in our print (and export) args */ print_args_.file = file_name.toUtf8().data(); print_args_.format = PR_FMT_TEXT; print_args_.to_file = TRUE; print_args_.cmd = NULL; print_args_.print_summary = TRUE; print_args_.print_dissections = print_dissections_as_displayed; print_args_.print_hex = FALSE; print_args_.print_formfeed = FALSE; switch (export_type_) { case export_type_text: /* Text */ print_args_.print_summary = packet_format_group_box_.summaryEnabled(); print_args_.print_dissections = print_dissections_none; if (packet_format_group_box_.detailsEnabled()) { if (packet_format_group_box_.allCollapsedEnabled()) print_args_.print_dissections = print_dissections_collapsed; else if (packet_format_group_box_.asDisplayedEnabled()) print_args_.print_dissections = print_dissections_as_displayed; else if (packet_format_group_box_.allExpandedEnabled()) print_args_.print_dissections = print_dissections_expanded; } print_args_.print_hex = packet_format_group_box_.bytesEnabled(); print_args_.stream = print_stream_text_new(TRUE, print_args_.file); if (print_args_.stream == NULL) { open_failure_alert_box(print_args_.file, errno, TRUE); return QDialog::Rejected; } status = cf_print_packets(cap_file_, &print_args_); break; case export_type_csv: /* CSV */ status = cf_write_csv_packets(cap_file_, &print_args_); break; case export_type_carrays: /* C Arrays */ status = cf_write_carrays_packets(cap_file_, &print_args_); break; case export_type_psml: /* PSML */ status = cf_write_psml_packets(cap_file_, &print_args_); break; case export_type_pdml: /* PDML */ status = cf_write_pdml_packets(cap_file_, &print_args_); break; default: return QDialog::Rejected; } switch (status) { case CF_PRINT_OK: break; case CF_PRINT_OPEN_ERROR: open_failure_alert_box(print_args_.file, errno, TRUE); break; case CF_PRINT_WRITE_ERROR: write_failure_alert_box(print_args_.file, errno); break; } if (selectedFiles().length() > 0) { gchar *dirname; /* Save the directory name for future file dialogs. */ dirname = get_dirname(print_args_.file); /* Overwrites file_name data */ set_last_open_dir(dirname); } } return retval; #else // Q_OS_WIN win32_export_file(parentWidget()->effectiveWinId(), cap_file_, export_type_); return QDialog::Accepted; #endif // Q_OS_WIN }
static void exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data) { char *tmpname, *capfile_name; int err; /* pcapng defs */ wtap_optionblock_t shb_hdr; wtapng_iface_descriptions_t *idb_inf; wtap_optionblock_t int_data; wtapng_if_descr_mandatory_t *int_data_mand; GString *os_info_str; gchar *opt_comment, *wireshark_ver; /* Create data for SHB */ os_info_str = g_string_new(""); get_os_version_info(os_info_str); shb_hdr = wtap_optionblock_create(WTAP_OPTION_BLOCK_NG_SECTION); /* options */ opt_comment = g_strdup_printf("Dump of PDUs from %s", cfile.filename); wtap_optionblock_set_option_string(shb_hdr, OPT_COMMENT, opt_comment); g_free(opt_comment); /* * UTF-8 string containing the name of the operating system used to create * this section. */ wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_OS, g_string_free(os_info_str, TRUE)); /* * UTF-8 string containing the name of the application used to create * this section. */ wireshark_ver = g_strdup_printf("Wireshark %s", get_ws_vcs_version_info()); wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, wireshark_ver); g_free(wireshark_ver); /* Create fake IDB info */ idb_inf = g_new(wtapng_iface_descriptions_t,1); idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtap_optionblock_t)); /* create the fake interface data */ int_data = wtap_optionblock_create(WTAP_OPTION_BLOCK_IF_DESCR); int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_optionblock_get_mandatory_data(int_data); int_data_mand->wtap_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU; int_data_mand->time_units_per_second = 1000000000; /* default nanosecond resolution */ int_data_mand->link_type = wtap_wtap_encap_to_pcap_encap(WTAP_ENCAP_WIRESHARK_UPPER_PDU); int_data_mand->snap_len = WTAP_MAX_PACKET_SIZE; wtap_optionblock_set_option_string(int_data, OPT_IDB_NAME, "Fake IF, PDU->Export"); wtap_optionblock_set_option_uint8(int_data, OPT_IDB_TSRESOL, 9); g_array_append_val(idb_inf->interface_data, int_data); /* Use a random name for the temporary import buffer */ exp_pdu_tap_data->wdh = wtap_dump_open_tempfile_ng(&tmpname, "Wireshark_PDU_", WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE, FALSE, shb_hdr, idb_inf, NULL, &err); capfile_name = g_strdup(tmpname); if (exp_pdu_tap_data->wdh == NULL) { open_failure_alert_box(capfile_name ? capfile_name : "temporary file", err, TRUE); goto end; } /* Run the tap */ cf_retap_packets(&cfile); if (!wtap_dump_close(exp_pdu_tap_data->wdh, &err)) { write_failure_alert_box(capfile_name, err); } remove_tap_listener(exp_pdu_tap_data); /* XXX: should this use the open_routine type in the cfile instead of WTAP_TYPE_AUTO? */ if (cf_open(&cfile, capfile_name, WTAP_TYPE_AUTO, TRUE /* temporary file */, &err) != CF_OK) { open_failure_alert_box(capfile_name, err, FALSE); goto end; } switch (cf_read(&cfile, FALSE)) { case CF_READ_OK: case CF_READ_ERROR: /* Just because we got an error, that doesn't mean we were unable to read any of the file; we handle what we could get from the file. */ break; case CF_READ_ABORTED: /* The user bailed out of re-reading the capture file; the capture file has been closed - just free the capture file name string and return (without changing the last containing directory). */ break; } end: g_free(capfile_name); wtap_optionblock_free(shb_hdr); wtap_free_idb_info(idb_inf); }