From f5b5d2c3c9b28fc5fcfbc1f93c8d33c5cfed177f Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Wed, 18 May 2022 19:50:14 +0000 Subject: [PATCH] Qt: Cleanup Traffic Table Remote traffic_table_ui.? and move the JSON stuff into endpoint. This is in preparation for larger work on both the conversation table as well as the endpoint table, and to start using Qt code in the UI where it should be used. --- ui/CMakeLists.txt | 1 - ui/qt/conversation_dialog.cpp | 41 ++++++- ui/qt/endpoint_dialog.cpp | 215 ++++++++++++++++++++++++++------- ui/qt/endpoint_dialog.h | 3 + ui/traffic_table_ui.c | 220 ---------------------------------- ui/traffic_table_ui.h | 96 --------------- 6 files changed, 218 insertions(+), 358 deletions(-) delete mode 100644 ui/traffic_table_ui.c delete mode 100644 ui/traffic_table_ui.h diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 22146aa0b2..302ba53e6d 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -52,7 +52,6 @@ set(NONGENERATED_UI_SRC text_import.c text_import_regex.c time_shift.c - traffic_table_ui.c util.c voip_calls.c ) diff --git a/ui/qt/conversation_dialog.cpp b/ui/qt/conversation_dialog.cpp index 0cbc5ce917..eee801c653 100644 --- a/ui/qt/conversation_dialog.cpp +++ b/ui/qt/conversation_dialog.cpp @@ -15,7 +15,6 @@ #include "ui/recent.h" #include "ui/tap-tcp-stream.h" -#include "ui/traffic_table_ui.h" #include "wsutil/str_util.h" @@ -52,6 +51,46 @@ // - The value of 'Rel start' and 'Duration' in "Conversations" no need too precise https://gitlab.com/wireshark/wireshark/-/issues/12803 +typedef enum { + CONV_COLUMN_SRC_ADDR, + CONV_COLUMN_SRC_PORT, + CONV_COLUMN_DST_ADDR, + CONV_COLUMN_DST_PORT, + CONV_COLUMN_PACKETS, + CONV_COLUMN_BYTES, + CONV_COLUMN_PKT_AB, + CONV_COLUMN_BYTES_AB, + CONV_COLUMN_PKT_BA, + CONV_COLUMN_BYTES_BA, + CONV_COLUMN_START, + CONV_COLUMN_DURATION, + CONV_COLUMN_BPS_AB, + CONV_COLUMN_BPS_BA, + CONV_NUM_COLUMNS, + CONV_INDEX_COLUMN = CONV_NUM_COLUMNS +} conversation_column_type_e; + +static char const *conv_column_titles[CONV_NUM_COLUMNS] = { + "Address A", + "Port A", + "Address B", + "Port B", + "Packets", + "Bytes", + "Packets A " UTF8_RIGHTWARDS_ARROW " B", + "Bytes A " UTF8_RIGHTWARDS_ARROW " B", + "Packets B " UTF8_RIGHTWARDS_ARROW " A", + "Bytes B " UTF8_RIGHTWARDS_ARROW " A", + "Rel Start", + "Duration", + "Bits/s A " UTF8_RIGHTWARDS_ARROW " B", + "Bits/s B " UTF8_RIGHTWARDS_ARROW " A" +}; + +static char const *conv_conn_a_title = "Connection A"; +static char const *conv_conn_b_title = "Connection B"; +static char const *conv_abs_start_title = "Abs Start"; + static const QString table_name_ = QObject::tr("Conversation"); ConversationDialog::ConversationDialog(QWidget &parent, CaptureFile &cf, int cli_proto_id, const char *filter) : TrafficTableDialog(parent, cf, filter, table_name_), diff --git a/ui/qt/endpoint_dialog.cpp b/ui/qt/endpoint_dialog.cpp index 39ad834071..c608ae8638 100644 --- a/ui/qt/endpoint_dialog.cpp +++ b/ui/qt/endpoint_dialog.cpp @@ -15,8 +15,8 @@ #include #include "ui/recent.h" -#include "ui/traffic_table_ui.h" +#include "wsutil/filesystem.h" #include "wsutil/file_util.h" #include "wsutil/pint.h" #include "wsutil/str_util.h" @@ -35,6 +35,47 @@ #include #include +#include +#include +#include +#include +#include + +typedef enum +{ + ENDP_COLUMN_ADDR, + ENDP_COLUMN_PORT, + ENDP_COLUMN_PACKETS, + ENDP_COLUMN_BYTES, + ENDP_COLUMN_PKT_AB, + ENDP_COLUMN_BYTES_AB, + ENDP_COLUMN_PKT_BA, + ENDP_COLUMN_BYTES_BA, + ENDP_NUM_COLUMNS, + ENDP_COLUMN_GEO_COUNTRY = ENDP_NUM_COLUMNS, + ENDP_COLUMN_GEO_CITY, + ENDP_COLUMN_GEO_AS_NUM, + ENDP_COLUMN_GEO_AS_ORG, + ENDP_NUM_GEO_COLUMNS +} endpoint_column_type_e; + +static char const *endp_column_titles[ENDP_NUM_GEO_COLUMNS] = { + "Address", + "Port", + "Packets", + "Bytes", + "Tx Packets", + "Tx Bytes", + "Rx Packets", + "Rx Bytes", + "Country", + "City", + "AS Number", + "AS Organization" +}; + +static char const *endp_conn_title = "Connection"; + static const QString table_name_ = QObject::tr("Endpoint"); EndpointDialog::EndpointDialog(QWidget &parent, CaptureFile &cf, int cli_proto_id, const char *filter) : TrafficTableDialog(parent, cf, filter, table_name_) @@ -180,6 +221,137 @@ void EndpointDialog::tabChanged() map_bt_->setEnabled(cur_tree && cur_tree->hasGeoIPData()); } +bool +EndpointDialog::writeEndpointGeoipMap(QFile * fp, bool json_only, hostlist_talker_t *const *hosts) +{ + QTextStream out(fp); + + if (!json_only) { + QFile ipmap(get_datafile_path("ipmap.html")); + + if (!ipmap.open(QIODevice::ReadOnly)) { + QMessageBox::warning(this, tr("Map file error"), tr("Could not open base file %1 for reading: %2") + .arg(get_datafile_path("ipmap.html")) + .arg(g_strerror(errno)) + ); + return false; + } + + /* Copy ipmap.html to map file. */ + QTextStream in(&ipmap); + QString line; + while (in.readLineInto(&line)) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) + out << line << Qt::endl; +#else + out << line << endl; +#endif + } + + out << QString("\n"); + + if (count == 0) { + QMessageBox::warning(this, tr("Map file error"), tr("No endpoints available to map")); + return false; + } + + out.flush(); + + return true; +} + QUrl EndpointDialog::createMap(bool json_only) { EndpointTreeWidget *cur_tree = qobject_cast(trafficTableTabWidget()->currentWidget()); @@ -214,49 +386,12 @@ QUrl EndpointDialog::createMap(bool json_only) return QUrl(); } - // - // XXX - At least with Qt 5.12 retrieving the name only works when - // it has been retrieved at least once when the file is open. - // - QString tempfilename = tf.fileName(); - int fd = tf.handle(); - // - // XXX - QFileDevice.handle() can return -1, but can QTemporaryFile.handle() - // do so if QTemporaryFile.open() has succeeded? - // - if (fd == -1) { - QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file")); + if (!writeEndpointGeoipMap(&tf, json_only, hosts)) { g_free(hosts); - return QUrl(); - } - // duplicate file descriptor as it is not allowed to perform a fclose before closing QFile - int duped_fd = ws_dup(fd); - if (duped_fd == -1) { - QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file")); - g_free(hosts); - return QUrl(); - } - FILE* fp = ws_fdopen(duped_fd, "wb"); - if (fp == NULL) { - QMessageBox::warning(this, tr("Map file error"), tr("Unable to create temporary file")); - g_free(hosts); - ws_close(duped_fd); - return QUrl(); - } - - gchar *err_str; - if (!write_endpoint_geoip_map(fp, json_only, hosts, &err_str)) { - QMessageBox::warning(this, tr("Map file error"), err_str); - g_free(err_str); - g_free(hosts); - fclose(fp); + tf.close(); return QUrl(); } g_free(hosts); - if (fclose(fp) == EOF) { - QMessageBox::warning(this, tr("Map file error"), g_strerror(errno)); - return QUrl(); - } tf.setAutoRemove(false); return QUrl::fromLocalFile(tf.fileName()); diff --git a/ui/qt/endpoint_dialog.h b/ui/qt/endpoint_dialog.h index 3fc5514a42..17666be124 100644 --- a/ui/qt/endpoint_dialog.h +++ b/ui/qt/endpoint_dialog.h @@ -10,6 +10,8 @@ #ifndef ENDPOINT_DIALOG_H #define ENDPOINT_DIALOG_H +#include + #include "traffic_table_dialog.h" class EndpointTreeWidget : public TrafficTableTreeWidget @@ -68,6 +70,7 @@ private: QPushButton *map_bt_; QUrl createMap(bool json_only); + bool writeEndpointGeoipMap(QFile * fp, bool json_only, hostlist_talker_t *const *hosts); #endif bool addTrafficTable(register_ct_t* table); diff --git a/ui/traffic_table_ui.c b/ui/traffic_table_ui.c deleted file mode 100644 index 22fdd011e2..0000000000 --- a/ui/traffic_table_ui.c +++ /dev/null @@ -1,220 +0,0 @@ -/* traffic_table_ui.c - * Helper routines common to conversation/endpoint tables. - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ -#include "config.h" - -#include - -#include "traffic_table_ui.h" -#include - -#ifdef HAVE_MAXMINDDB -#include - -#include "wsutil/filesystem.h" -#include "wsutil/file_util.h" -#include "wsutil/json_dumper.h" -#endif - -const char *conv_column_titles[CONV_NUM_COLUMNS] = { - "Address A", - "Port A", - "Address B", - "Port B", - "Packets", - "Bytes", - "Packets A " UTF8_RIGHTWARDS_ARROW " B", - "Bytes A " UTF8_RIGHTWARDS_ARROW " B", - "Packets B " UTF8_RIGHTWARDS_ARROW " A", - "Bytes B " UTF8_RIGHTWARDS_ARROW " A", - "Rel Start", - "Duration", - "Bits/s A " UTF8_RIGHTWARDS_ARROW " B", - "Bits/s B " UTF8_RIGHTWARDS_ARROW " A" -}; - -const char *conv_conn_a_title = "Connection A"; -const char *conv_conn_b_title = "Connection B"; -const char *conv_abs_start_title = "Abs Start"; - -const char *endp_column_titles[ENDP_NUM_GEO_COLUMNS] = { - "Address", - "Port", - "Packets", - "Bytes", - "Tx Packets", - "Tx Bytes", - "Rx Packets", - "Rx Bytes", - "Country", - "City", - "AS Number", - "AS Organization" -}; - -const char *endp_conn_title = "Connection"; - -#ifdef HAVE_MAXMINDDB -gboolean -write_endpoint_geoip_map(FILE *fp, gboolean json_only, hostlist_talker_t *const *hosts, gchar **err_str) -{ - if (!json_only) { - char *base_html_path = get_datafile_path("ipmap.html"); - FILE *base_html_fp = ws_fopen(base_html_path, "rb"); - if (!base_html_fp) { - *err_str = ws_strdup_printf("Could not open base file %s for reading: %s", - base_html_path, g_strerror(errno)); - g_free(base_html_path); - return FALSE; - } - g_free(base_html_path); - - /* Copy ipmap.html to map file. */ - size_t n; - char buf[4096]; - while ((n = fread(buf, 1, sizeof(buf), base_html_fp)) != 0) { - if (fwrite(buf, 1, n, fp) != n) { - *err_str = ws_strdup_printf("Failed to write to map file: %s", g_strerror(errno)); - fclose(base_html_fp); - return FALSE; - } - } - if (ferror(base_html_fp)) { - *err_str = ws_strdup_printf("Failed to read base file: %s", g_strerror(errno)); - fclose(base_html_fp); - return FALSE; - } - fclose(base_html_fp); - - fputs("\n", fp); - } - - if (count == 0) { - *err_str = g_strdup("No endpoints available to map"); - return FALSE; - } - - return TRUE; -} -#endif diff --git a/ui/traffic_table_ui.h b/ui/traffic_table_ui.h deleted file mode 100644 index 18aa75441e..0000000000 --- a/ui/traffic_table_ui.h +++ /dev/null @@ -1,96 +0,0 @@ -/** @file - * - * Helper routines common to conversation/endpoint tables. - * - * Wireshark - Network traffic analyzer - * By Gerald Combs - * Copyright 1998 Gerald Combs - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -#ifndef __TRAFFIC_TABLE_UI_H__ -#define __TRAFFIC_TABLE_UI_H__ - -#ifdef HAVE_MAXMINDDB -#include - -#include "epan/maxmind_db.h" -#include -#endif - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -/** @file - * Conversation and endpoint lists. - */ - -typedef enum { - CONV_COLUMN_SRC_ADDR, - CONV_COLUMN_SRC_PORT, - CONV_COLUMN_DST_ADDR, - CONV_COLUMN_DST_PORT, - CONV_COLUMN_PACKETS, - CONV_COLUMN_BYTES, - CONV_COLUMN_PKT_AB, - CONV_COLUMN_BYTES_AB, - CONV_COLUMN_PKT_BA, - CONV_COLUMN_BYTES_BA, - CONV_COLUMN_START, - CONV_COLUMN_DURATION, - CONV_COLUMN_BPS_AB, - CONV_COLUMN_BPS_BA, - CONV_NUM_COLUMNS, - CONV_INDEX_COLUMN = CONV_NUM_COLUMNS -} conversation_column_type_e; - -extern const char *conv_column_titles[CONV_NUM_COLUMNS]; -extern const char *conv_conn_a_title; -extern const char *conv_conn_b_title; -extern const char *conv_abs_start_title; - -typedef enum -{ - ENDP_COLUMN_ADDR, - ENDP_COLUMN_PORT, - ENDP_COLUMN_PACKETS, - ENDP_COLUMN_BYTES, - ENDP_COLUMN_PKT_AB, - ENDP_COLUMN_BYTES_AB, - ENDP_COLUMN_PKT_BA, - ENDP_COLUMN_BYTES_BA, - ENDP_NUM_COLUMNS, - ENDP_COLUMN_GEO_COUNTRY = ENDP_NUM_COLUMNS, - ENDP_COLUMN_GEO_CITY, - ENDP_COLUMN_GEO_AS_NUM, - ENDP_COLUMN_GEO_AS_ORG, - ENDP_NUM_GEO_COLUMNS -} endpoint_column_type_e; - -extern const char *endp_column_titles[ENDP_NUM_GEO_COLUMNS]; - -extern const char *endp_conn_title; - -#ifdef HAVE_MAXMINDDB -/** - * Writes an HTML file containing a map showing the geographical locations - * of IPv4 and IPv6 addresses. - * - * @param [in] fp File handle for writing the HTML file. - * @param [in] json_only Write GeoJSON data only. - * @param [in] hosts A NULL-terminated array of 'hostlist_talker_t'. A MMDB - * lookup should have been completed before for these addresses. - * @param [in,out] err_str Set to error string on failure. Error string must - * be g_freed. May be NULL. - * @return Whether the map file was successfully written with non-empty data. - */ -gboolean write_endpoint_geoip_map(FILE *fp, gboolean json_only, hostlist_talker_t *const *hosts, gchar **err_str); -#endif - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* __TRAFFIC_TABLE_UI_H__ */