summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2015-01-29 13:55:18 -0800
committerGerald Combs <gerald@wireshark.org>2015-01-29 22:51:43 +0000
commit67b069e544da2078cf5c9ba0c95ca9af8ba00b03 (patch)
treedda3edf650e2a08f86825bbf3d537a7ab33051f2
parent2edf8bbd33e4820c19a52a435ddb2cc8e3e32fdb (diff)
downloadwireshark-67b069e544da2078cf5c9ba0c95ca9af8ba00b03.tar.gz
Qt: Add address conversion convenience routines
Add address_to_qstring and address_to_display_qstring, which wrap address_to_string and address_to_display respectively and return QStrings. Convert most of the instances in ui/qt to the new routines. Fix a some memory leaks in the process. Change-Id: Icda80bbfe0b2df723d54c8da84355255f819af89 Reviewed-on: https://code.wireshark.org/review/6848 Petri-Dish: Gerald Combs <gerald@wireshark.org> Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org> Reviewed-by: Gerald Combs <gerald@wireshark.org>
-rw-r--r--doc/README.qt6
-rw-r--r--ui/qt/lbm_lbtrm_transport_dialog.cpp8
-rw-r--r--ui/qt/lbm_lbtru_transport_dialog.cpp6
-rw-r--r--ui/qt/lbm_stream_dialog.cpp12
-rw-r--r--ui/qt/lbm_uimflow_dialog.cpp5
-rw-r--r--ui/qt/qt_ui_utils.cpp26
-rw-r--r--ui/qt/qt_ui_utils.h22
-rw-r--r--ui/qt/sctp_assoc_analyse_dialog.cpp11
-rw-r--r--ui/qt/tcp_stream_dialog.cpp8
-rw-r--r--ui/qt/voip_calls_dialog.cpp14
10 files changed, 78 insertions, 40 deletions
diff --git a/doc/README.qt b/doc/README.qt
index 754a8ea60d..02ed43abe6 100644
--- a/doc/README.qt
+++ b/doc/README.qt
@@ -196,11 +196,15 @@ QEvent::LanguageChange.
If you're using GLib string functions or plain old C character array idioms in
Qt-only code you're probably doing something wrong. QStrings are generally
-*much* safer and easier to use. They also make translations eaiser.
+*much* safer and easier to use. They also make translations easier.
If you need to pass strings between Qt and GLib you can use qstring_strdup
gchar_free_to_qstring, which are defined in ui/qt/qt_ui_utils.h.
+If you're calling a function that returns wmem-allocated memory it might make
+more sense to add a wrapper function to qt_ui_utils than to call wmem_free in
+your code.
+
2.1.4 Mixing C and C++
Sometimes we have to call C++ functions from one of Wireshark's C callbacks and
diff --git a/ui/qt/lbm_lbtrm_transport_dialog.cpp b/ui/qt/lbm_lbtrm_transport_dialog.cpp
index 321a52d3f1..c2d7d42f6b 100644
--- a/ui/qt/lbm_lbtrm_transport_dialog.cpp
+++ b/ui/qt/lbm_lbtrm_transport_dialog.cpp
@@ -26,6 +26,7 @@
#include "file.h"
+#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QClipboard>
@@ -36,12 +37,9 @@
#include <epan/packet_info.h>
#include <epan/tap.h>
#include <epan/to_str.h>
-#include <epan/wmem/wmem.h>
#include <epan/dissectors/packet-lbm.h>
#include <wsutil/nstime.h>
-#include <QDebug>
-
namespace
{
static const int Source_AddressTransport_Column = 0;
@@ -1059,7 +1057,7 @@ void LBMLBTRMTransportDialogInfo::processPacket(const packet_info * pinfo, const
{
LBMLBTRMSourceEntry * source = NULL;
LBMLBTRMSourceMapIterator it;
- QString src_address = QString(address_to_str(pinfo->pool, &(pinfo->src)));
+ QString src_address = address_to_qstring(&(pinfo->src));
it = m_sources.find(src_address);
if (m_sources.end() == it)
@@ -1086,7 +1084,7 @@ void LBMLBTRMTransportDialogInfo::processPacket(const packet_info * pinfo, const
{
LBMLBTRMReceiverEntry * receiver = NULL;
LBMLBTRMReceiverMapIterator it;
- QString src_address = QString(address_to_str(pinfo->pool, &(pinfo->src)));
+ QString src_address = address_to_qstring(&(pinfo->src));
it = m_receivers.find(src_address);
if (m_receivers.end() == it)
diff --git a/ui/qt/lbm_lbtru_transport_dialog.cpp b/ui/qt/lbm_lbtru_transport_dialog.cpp
index cfdad456b5..7eef151644 100644
--- a/ui/qt/lbm_lbtru_transport_dialog.cpp
+++ b/ui/qt/lbm_lbtru_transport_dialog.cpp
@@ -26,6 +26,7 @@
#include "file.h"
+#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QClipboard>
@@ -36,7 +37,6 @@
#include <epan/packet_info.h>
#include <epan/tap.h>
#include <epan/to_str.h>
-#include <epan/wmem/wmem.h>
#include <epan/dissectors/packet-lbm.h>
#include <wsutil/nstime.h>
@@ -1396,7 +1396,7 @@ void LBMLBTRUTransportDialogInfo::processPacket(const packet_info * pinfo, const
{
LBMLBTRUSourceEntry * source = NULL;
LBMLBTRUSourceMapIterator it;
- QString src_address = QString(address_to_str(pinfo->pool, &(pinfo->src)));
+ QString src_address = address_to_qstring(&(pinfo->src));
it = m_sources.find(src_address);
if (m_sources.end() == it)
@@ -1425,7 +1425,7 @@ void LBMLBTRUTransportDialogInfo::processPacket(const packet_info * pinfo, const
{
LBMLBTRUReceiverEntry * receiver = NULL;
LBMLBTRUReceiverMapIterator it;
- QString src_address = QString(address_to_str(pinfo->pool, &(pinfo->src)));
+ QString src_address = address_to_qstring(&(pinfo->src));
it = m_receivers.find(src_address);
if (m_receivers.end() == it)
diff --git a/ui/qt/lbm_stream_dialog.cpp b/ui/qt/lbm_stream_dialog.cpp
index 657f1b68d1..22f067050a 100644
--- a/ui/qt/lbm_stream_dialog.cpp
+++ b/ui/qt/lbm_stream_dialog.cpp
@@ -28,6 +28,7 @@
#include "file.h"
+#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QClipboard>
@@ -35,7 +36,6 @@
#include <QTreeWidget>
#include <QTreeWidgetItemIterator>
#include <epan/packet_info.h>
-#include <epan/wmem/wmem.h>
#include <epan/to_str.h>
#include <epan/tap.h>
#include <epan/dissectors/packet-lbm.h>
@@ -56,7 +56,7 @@ namespace
class LBMSubstreamEntry
{
public:
- LBMSubstreamEntry(guint64 channel, guint32 substream_id, const packet_info* pinfo, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port);
+ LBMSubstreamEntry(guint64 channel, guint32 substream_id, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port);
~LBMSubstreamEntry(void);
void processPacket(guint32 frame, guint32 bytes);
void setItem(QTreeWidgetItem * item);
@@ -79,7 +79,7 @@ class LBMSubstreamEntry
QTreeWidgetItem * m_item;
};
-LBMSubstreamEntry::LBMSubstreamEntry(guint64 channel, guint32 substream_id, const packet_info* pinfo, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port) :
+LBMSubstreamEntry::LBMSubstreamEntry(guint64 channel, guint32 substream_id, const address * source_address, guint16 source_port, const address * destination_address, guint16 destination_port) :
m_channel(channel),
m_substream_id(substream_id),
m_first_frame((guint32)(~0)),
@@ -89,10 +89,10 @@ LBMSubstreamEntry::LBMSubstreamEntry(guint64 channel, guint32 substream_id, cons
m_item(NULL)
{
m_endpoint_a = QString("%1:%2")
- .arg(address_to_str(pinfo->pool, source_address))
+ .arg(address_to_qstring(source_address))
.arg(source_port);
m_endpoint_b = QString("%1:%2")
- .arg(address_to_str(pinfo->pool, destination_address))
+ .arg(address_to_qstring(destination_address))
.arg(destination_port);
}
@@ -224,7 +224,7 @@ void LBMStreamEntry::processPacket(const packet_info * pinfo, const lbm_uim_stre
{
QTreeWidgetItem * item = NULL;
- substream = new LBMSubstreamEntry(m_channel, stream_info->substream_id, pinfo, &(pinfo->src), pinfo->srcport, &(pinfo->dst), pinfo->destport);
+ substream = new LBMSubstreamEntry(m_channel, stream_info->substream_id, &(pinfo->src), pinfo->srcport, &(pinfo->dst), pinfo->destport);
m_substreams.insert(stream_info->substream_id, substream);
item = new QTreeWidgetItem();
substream->setItem(item);
diff --git a/ui/qt/lbm_uimflow_dialog.cpp b/ui/qt/lbm_uimflow_dialog.cpp
index a6c253bdbf..48f7527f15 100644
--- a/ui/qt/lbm_uimflow_dialog.cpp
+++ b/ui/qt/lbm_uimflow_dialog.cpp
@@ -34,6 +34,7 @@
#include "ui/utf8_entities.h"
+#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QDir>
@@ -505,7 +506,6 @@ void LBMUIMFlowDialog::fillDiagram(void)
{
QCustomPlot * sp = m_ui->sequencePlot;
seq_analysis_info_t new_sa;
- char* addr_str;
new_sa = m_sequence_analysis;
new_sa.items = g_queue_new();
@@ -521,13 +521,12 @@ void LBMUIMFlowDialog::fillDiagram(void)
m_node_label_width = 0;
for (guint i = 0; i < m_sequence_analysis.num_nodes; i++)
{
- addr_str = (char*)address_to_display(NULL, &(m_sequence_analysis.nodes[i]));
+ QString addr_str = address_to_display_qstring(&(m_sequence_analysis.nodes[i]));
int label_w = vfm.width(addr_str);
if (m_node_label_width < label_w)
{
m_node_label_width = label_w;
}
- wmem_free(NULL, addr_str);
}
m_node_label_width = (m_node_label_width * 3 / 4) + m_one_em;
diff --git a/ui/qt/qt_ui_utils.cpp b/ui/qt/qt_ui_utils.cpp
index f4a0621080..7c7b5a1d38 100644
--- a/ui/qt/qt_ui_utils.cpp
+++ b/ui/qt/qt_ui_utils.cpp
@@ -27,6 +27,9 @@
#include <ui/recent.h>
#include <ui/ui_util.h>
+#include <epan/addr_resolv.h>
+#include <epan/to_str.h>
+
#include <wsutil/str_util.h>
#include <QFontDatabase>
@@ -36,7 +39,6 @@ format_size_flags_e operator|(format_size_flags_e lhs, format_size_flags_e rhs)
return (format_size_flags_e) ((int)lhs| (int)rhs);
}
-
/*
* We might want to create our own "wsstring" class with convenience
* methods for handling g_malloc()ed strings, GStrings, and a shortcut
@@ -53,6 +55,28 @@ QString gchar_free_to_qstring(gchar *glib_string) {
return *qt_string;
}
+const QString address_to_qstring(const _address *address)
+{
+ QString address_qstr = QString();
+ if (address) {
+ gchar *address_gchar_p = address_to_str(NULL, address);
+ address_qstr = address_gchar_p;
+ wmem_free(NULL, address_gchar_p);
+ }
+ return address_qstr;
+}
+
+const QString address_to_display_qstring(const _address *address)
+{
+ QString address_qstr = QString();
+ if (address) {
+ const gchar *address_gchar_p = address_to_display(NULL, address);
+ address_qstr = address_gchar_p;
+ wmem_free(NULL, (void *) address_gchar_p);
+ }
+ return address_qstr;
+}
+
void smooth_font_size(QFont &font) {
QFontDatabase fdb;
#if QT_VERSION < QT_VERSION_CHECK(4, 8, 0)
diff --git a/ui/qt/qt_ui_utils.h b/ui/qt/qt_ui_utils.h
index 45bf73afa5..e114f7c2fd 100644
--- a/ui/qt/qt_ui_utils.h
+++ b/ui/qt/qt_ui_utils.h
@@ -25,6 +25,10 @@
// xxx - copied from ui/gtk/gui_utils.h
+/** @file
+ * Utility functions for working with the Wireshark and GLib APIs.
+ */
+
#include <stdio.h>
#include "config.h"
@@ -66,6 +70,8 @@ struct remote_host_t {
gboolean nocap_local;
};
+struct _address;
+
#ifdef __cplusplus
}
#endif /* __cplusplus */
@@ -87,6 +93,22 @@ gchar *qstring_strdup(QString q_string);
*/
QString gchar_free_to_qstring(gchar *glib_string);
+/** Convert an address to a QString using address_to_str().
+ *
+ * @param address A pointer to an address.
+ *
+ * @return A QString representation of the address. May be the null string (QString())
+ */
+const QString address_to_qstring(const struct _address *address);
+
+/** Convert an address to a QString using address_to_display().
+ *
+ * @param address A pointer to an address.
+ *
+ * @return A QString representation of the address. May be the null string (QString())
+ */
+const QString address_to_display_qstring(const struct _address *address);
+
/**
* Round the current size of a font up to its next "smooth" size.
* If a smooth size can't be found the font is left unchanged.
diff --git a/ui/qt/sctp_assoc_analyse_dialog.cpp b/ui/qt/sctp_assoc_analyse_dialog.cpp
index 07097ab052..df851132ca 100644
--- a/ui/qt/sctp_assoc_analyse_dialog.cpp
+++ b/ui/qt/sctp_assoc_analyse_dialog.cpp
@@ -23,12 +23,13 @@
#include "sctp_assoc_analyse_dialog.h"
#include "ui_sctp_assoc_analyse_dialog.h"
+
+#include "qt_ui_utils.h"
#include "sctp_graph_dialog.h"
#include "sctp_graph_arwnd_dialog.h"
#include "sctp_graph_byte_dialog.h"
#include "sctp_chunk_statistics_dialog.h"
-
SCTPAssocAnalyseDialog::SCTPAssocAnalyseDialog(QWidget *parent, sctp_assoc_info_t *assoc, capture_file *cf, SCTPAllAssocsDialog* caller) :
QDialog(parent),
ui(new Ui::SCTPAssocAnalyseDialog),
@@ -131,9 +132,7 @@ void SCTPAssocAnalyseDialog::fillTabs()
store = (address *)(list->data);
if (store->type != AT_NONE) {
if ((store->type == AT_IPv4) || (store->type == AT_IPv6)) {
- char* addr_str = (char*)address_to_str(NULL, store);
- ui->listWidgetEP1->addItem(QString("%1").arg(addr_str));
- wmem_free(NULL, addr_str);
+ ui->listWidgetEP1->addItem(address_to_qstring(store));
}
}
list = g_list_next(list);
@@ -185,9 +184,7 @@ void SCTPAssocAnalyseDialog::fillTabs()
store = (address *)(list->data);
if (store->type != AT_NONE) {
if ((store->type == AT_IPv4) || (store->type == AT_IPv6)) {
- char* addr_str = (char*)address_to_str(NULL, store);
- ui->listWidgetEP2->addItem(QString("%1").arg(addr_str));
- wmem_free(NULL, addr_str);
+ ui->listWidgetEP2->addItem(address_to_qstring(store));
}
}
list = g_list_next(list);
diff --git a/ui/qt/tcp_stream_dialog.cpp b/ui/qt/tcp_stream_dialog.cpp
index 2f66f3f937..85236d8a87 100644
--- a/ui/qt/tcp_stream_dialog.cpp
+++ b/ui/qt/tcp_stream_dialog.cpp
@@ -725,16 +725,12 @@ void TCPStreamDialog::fillWindowScale()
QString TCPStreamDialog::streamDescription()
{
- char* src_addr = (char*)address_to_str(NULL, &graph_.src_address);
- char* dst_addr = (char*)address_to_str(NULL, &graph_.dst_address);
QString description(tr(" for %1:%2 %3 %4:%5")
- .arg(src_addr)
+ .arg(address_to_qstring(&graph_.src_address))
.arg(graph_.src_port)
.arg(UTF8_RIGHTWARDS_ARROW)
- .arg(dst_addr)
+ .arg(address_to_qstring(&graph_.dst_address))
.arg(graph_.dst_port));
- wmem_free(NULL, src_addr);
- wmem_free(NULL, dst_addr);
return description;
}
diff --git a/ui/qt/voip_calls_dialog.cpp b/ui/qt/voip_calls_dialog.cpp
index 6059beb25e..c32acde626 100644
--- a/ui/qt/voip_calls_dialog.cpp
+++ b/ui/qt/voip_calls_dialog.cpp
@@ -29,6 +29,7 @@
#include "ui/utf8_entities.h"
+#include "qt_ui_utils.h"
#include "sequence_dialog.h"
#include "stock_icon.h"
#include "wireshark_application.h"
@@ -74,20 +75,17 @@ public:
if (!call_info) {
return;
}
- char* addr_str;
// XXX Pull digit count from capture file precision
setText(start_time_col_, QString::number(nstime_to_sec(&(call_info->start_rel_ts)), 'f', 6));
setText(stop_time_col_, QString::number(nstime_to_sec(&(call_info->stop_rel_ts)), 'f', 6));
- addr_str = (char*)address_to_display(NULL, &(call_info->initial_speaker));
- setText(initial_speaker_col_, addr_str);
+ setText(initial_speaker_col_, address_to_display_qstring(&(call_info->initial_speaker)));
setText(from_col_, call_info->from_identity);
setText(to_col_, call_info->to_identity);
setText(protocol_col_, ((call_info->protocol == VOIP_COMMON) && call_info->protocol_name) ?
call_info->protocol_name : voip_protocol_name[call_info->protocol]);
setText(packets_col_, QString::number(call_info->npackets));
setText(state_col_, voip_call_state_name[call_info->call_state]);
- wmem_free(NULL, addr_str);
/* Add comments based on the protocol */
QString call_comments;
@@ -352,7 +350,7 @@ void VoipCallsDialog::prepareFilter()
const h323_calls_info_t *h323info;
const h245_address_t *h245_add = NULL;
const gcp_ctx_t* ctx;
- char* addr_str, *guid_str;
+ char *guid_str;
if (filter_length < max_filter_length) {
gtk_editable_insert_text(GTK_EDITABLE(main_display_filter_widget), filter_string_fwd->str, -1, &pos);
@@ -388,6 +386,7 @@ void VoipCallsDialog::prepareFilter()
);
break;
case VOIP_H323:
+ {
h323info = (h323_calls_info_t *)listinfo->prot_info;
guid_str = guid_to_str(NULL, &h323info->guid[0]);
g_string_append_printf(filter_string_fwd,
@@ -401,14 +400,13 @@ void VoipCallsDialog::prepareFilter()
wmem_free(NULL, guid_str);
while (listb) {
h245_add = (h245_address_t *)listb->data;
- addr_str = (char*)address_to_str(NULL, &h245_add->h245_address);
g_string_append_printf(filter_string_fwd,
" || (ip.addr == %s && tcp.port == %d && h245)",
- addr_str, h245_add->h245_port);
+ address_to_qstring(&h245_add->h245_address), h245_add->h245_port);
listb = g_list_next(listb);
- wmem_free(NULL, addr_str);
}
g_string_append_printf(filter_string_fwd, ")");
+ }
break;
case TEL_H248:
ctx = (gcp_ctx_t *)listinfo->prot_info;