From 817e4d950f3689603d211d8a8b4ccb5d72ee40ca Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 22 Feb 2021 18:20:51 -0800 Subject: [PATCH] Qt: Add utility for IP address conversion to internal format --- src/platform/qt/GDBController.cpp | 7 +++---- src/platform/qt/GDBController.h | 2 +- src/platform/qt/GDBWindow.cpp | 23 +++++++---------------- src/platform/qt/utils.cpp | 22 ++++++++++++++++++++++ src/platform/qt/utils.h | 4 ++++ 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/platform/qt/GDBController.cpp b/src/platform/qt/GDBController.cpp index 32edc7d14..86ae0dc45 100644 --- a/src/platform/qt/GDBController.cpp +++ b/src/platform/qt/GDBController.cpp @@ -11,7 +11,7 @@ using namespace QGBA; GDBController::GDBController(QObject* parent) : DebuggerController(&m_gdbStub.d, parent) - , m_bindAddress({ IPV4, 0 }) + , m_bindAddress({ IPV4, {0} }) { GDBStubCreate(&m_gdbStub); } @@ -28,9 +28,8 @@ void GDBController::setPort(ushort port) { m_port = port; } -void GDBController::setBindAddress(uint32_t bindAddress) { - m_bindAddress.version = IPV4; - m_bindAddress.ipv4 = htonl(bindAddress); +void GDBController::setBindAddress(const Address& address) { + m_bindAddress = address; } void GDBController::listen() { diff --git a/src/platform/qt/GDBController.h b/src/platform/qt/GDBController.h index f2380c6af..03619fe87 100644 --- a/src/platform/qt/GDBController.h +++ b/src/platform/qt/GDBController.h @@ -27,7 +27,7 @@ public: public slots: void setPort(ushort port); - void setBindAddress(uint32_t bindAddress); + void setBindAddress(const Address&); void listen(); signals: diff --git a/src/platform/qt/GDBWindow.cpp b/src/platform/qt/GDBWindow.cpp index c32d7a52d..b719c324d 100644 --- a/src/platform/qt/GDBWindow.cpp +++ b/src/platform/qt/GDBWindow.cpp @@ -15,6 +15,7 @@ #include #include "GDBController.h" +#include "utils.h" using namespace QGBA; @@ -77,24 +78,14 @@ void GDBWindow::portChanged(const QString& portString) { } void GDBWindow::bindAddressChanged(const QString& bindAddressString) { - bool ok = false; - QStringList parts = bindAddressString.split('.'); - if (parts.length() != 4) { + QHostAddress qaddress; + Address address; + + if (!qaddress.setAddress(bindAddressString)) { return; } - int i; - uint32_t address = 0; - for (i = 0; i < 4; ++i) { - ushort octet = parts[i].toUShort(&ok); - if (!ok) { - return; - } - if (octet > 0xFF) { - return; - } - address <<= 8; - address += octet; - } + + convertAddress(&qaddress, &address); m_gdbController->setBindAddress(address); } diff --git a/src/platform/qt/utils.cpp b/src/platform/qt/utils.cpp index 31845e140..8b273cb6d 100644 --- a/src/platform/qt/utils.cpp +++ b/src/platform/qt/utils.cpp @@ -22,6 +22,7 @@ QString niceSizeFormat(size_t filesize) { } return unit.arg(size, 0, 'f', int(size * 10) % 10 ? 1 : 0); } + QString nicePlatformFormat(mPlatform platform) { switch (platform) { #ifdef M_CORE_GBA @@ -37,4 +38,25 @@ QString nicePlatformFormat(mPlatform platform) { } } +bool convertAddress(const QHostAddress* input, Address* output) { + if (input->isNull()) { + return false; + } + Q_IPV6ADDR ipv6; + switch (input->protocol()) { + case QAbstractSocket::IPv4Protocol: + output->version = IPV4; + output->ipv4 = input->toIPv4Address(); + break; + case QAbstractSocket::IPv6Protocol: + output->version = IPV6; + ipv6 = input->toIPv6Address(); + memcpy(output->ipv6, &ipv6, 16); + break; + default: + return false; + } + return true; +} + } diff --git a/src/platform/qt/utils.h b/src/platform/qt/utils.h index db0151d67..ca9ef027f 100644 --- a/src/platform/qt/utils.h +++ b/src/platform/qt/utils.h @@ -6,7 +6,9 @@ #pragma once #include +#include +#include #include #include #include @@ -25,6 +27,8 @@ enum class Endian { QString niceSizeFormat(size_t filesize); QString nicePlatformFormat(mPlatform platform); +bool convertAddress(const QHostAddress* input, Address* output); + inline void lockAspectRatio(const QSize& ref, QSize& size) { if (size.width() * ref.height() > size.height() * ref.width()) { size.setWidth(size.height() * ref.width() / ref.height());