diff options
author | AndersBroman <anders.broman@ericsson.com> | 2017-12-11 13:56:12 +0100 |
---|---|---|
committer | Anders Broman <a.broman58@gmail.com> | 2017-12-11 13:35:24 +0000 |
commit | c9546dfceb778dcabe7a830272add62e6ae1403e (patch) | |
tree | 2be7ab9143e7bf08f7ac7b95e7ac514756fc1870 | |
parent | 84f905786ed2b2aabaec9a0c1045daeda66358ba (diff) |
[capture_info] Move capture_info_new_file() to capture.c
Change-Id: I9137af6461921d7553a8968743f61f2265424061
Reviewed-on: https://code.wireshark.org/review/24767
Petri-Dish: Anders Broman <a.broman58@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
-rw-r--r-- | capture_info.c | 126 | ||||
-rw-r--r-- | capture_info.h | 4 | ||||
-rw-r--r-- | ui/capture.c | 115 |
3 files changed, 114 insertions, 131 deletions
diff --git a/capture_info.c b/capture_info.c index b08f696d1a..d0fed6b778 100644 --- a/capture_info.c +++ b/capture_info.c @@ -21,132 +21,6 @@ #include <epan/capture_dissectors.h> -#include <wsutil/filesystem.h> - - -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 Wireshark 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\" contains record data that Wireshark doesn't support.\n" - "(%s)", err_info != NULL ? err_info : "no information supplied"); - 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_UNWRITABLE_FILE_TYPE: - /* Seen only when opening a capture file for writing. */ - errmsg = "Wireshark doesn't support writing capture files in that format."; - break; - - case WTAP_ERR_UNWRITABLE_ENCAP: - /* Seen only when opening a capture file for writing. */ - errmsg = "Wireshark can't save this capture in that format."; - break; - - case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: - if (for_writing) - errmsg = "Wireshark can't save this capture in that format."; - else - errmsg = "The file \"%s\" is a capture for a network type that Wireshark 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 != NULL ? err_info : "no information supplied"); - 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 != NULL ? err_info : "no information supplied"); - 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; -} - -/* 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; -} - static void capture_info_packet(info_data_t* cap_info, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header) { diff --git a/capture_info.h b/capture_info.h index aa5953595c..c01d1f8a7a 100644 --- a/capture_info.h +++ b/capture_info.h @@ -49,10 +49,6 @@ typedef struct _info_data { capture_info ui; /* user interface data */ } info_data_t; - -/* new file arrived - (eventually close old wtap), open wtap */ -extern gboolean capture_info_new_file(const char *new_filename, info_data_t* cap_info); - /* new packets arrived - read from wtap, count */ extern void capture_info_new_packets(int to_read, info_data_t* cap_info); diff --git a/ui/capture.c b/ui/capture.c index 4ec0da4c37..64c04c08bd 100644 --- a/ui/capture.c +++ b/ui/capture.c @@ -39,6 +39,7 @@ #include "wsutil/file_util.h" #include "wsutil/str_util.h" +#include <wsutil/filesystem.h> #include "log.h" typedef struct if_stat_cache_item_s { @@ -287,6 +288,106 @@ capture_input_read_all(capture_session *cap_session, gboolean is_tempfile, return TRUE; } +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 Wireshark 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\" contains record data that Wireshark doesn't support.\n" + "(%s)", err_info != NULL ? err_info : "no information supplied"); + 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_UNWRITABLE_FILE_TYPE: + /* Seen only when opening a capture file for writing. */ + errmsg = "Wireshark doesn't support writing capture files in that format."; + break; + + case WTAP_ERR_UNWRITABLE_ENCAP: + /* Seen only when opening a capture file for writing. */ + errmsg = "Wireshark can't save this capture in that format."; + break; + + case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: + if (for_writing) + errmsg = "Wireshark can't save this capture in that format."; + else + errmsg = "The file \"%s\" is a capture for a network type that Wireshark 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 != NULL ? err_info : "no information supplied"); + 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 != NULL ? err_info : "no information supplied"); + 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; +} /* capture child tells us we have a new (or the first) capture file */ gboolean @@ -295,6 +396,8 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file) capture_options *capture_opts = cap_session->capture_opts; gboolean is_tempfile; int err; + gchar *err_info; + gchar *err_msg; if(cap_session->state == CAPTURE_PREPARING) { g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started"); @@ -346,8 +449,18 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file) } if(capture_opts->show_info) { - if (!capture_info_new_file(new_file, cap_session->cap_data_info)) + if (cap_session->cap_data_info->wtap != NULL) { + wtap_close(cap_session->cap_data_info->wtap); + } + + cap_session->cap_data_info->wtap = wtap_open_offline(new_file, WTAP_TYPE_AUTO, &err, &err_info, FALSE); + if (!cap_session->cap_data_info->wtap) { + err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN), + new_file); + g_warning("capture_info_new_file: %d (%s)", err, err_msg); + g_free(err_msg); return FALSE; + } } if(capture_opts->real_time_mode) { |