Skip to content

Commit

Permalink
Internal: Refactor NetworkUtil::ParseNetworkAddress
Browse files Browse the repository at this point in the history
This moves the host elaborate and reusable host name lookup logic
to its own ParseNetworkAddressName method.

Co-authored-by: Peter L Jones <peter@drealm.info>
  • Loading branch information
hoffie and pljones committed Oct 21, 2022
1 parent 7075708 commit 24adc20
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,31 @@ QSize CMinimumStackedLayout::sizeHint() const
* Other Classes *
\******************************************************************************/
// Network utility functions ---------------------------------------------------
bool NetworkUtil::ParseNetworkAddressString ( QString strAddress, QHostAddress& InetAddr, bool bEnableIPv6 )
{
// try to get host by name, assuming
// that the string contains a valid host name string or IP address
const QHostInfo HostInfo = QHostInfo::fromName ( strAddress );

if ( HostInfo.error() != QHostInfo::NoError )
{
// qInfo() << qUtf8Printable ( QString ( "Invalid hostname" ) );
return false; // invalid address
}

foreach ( const QHostAddress HostAddr, HostInfo.addresses() )
{
// qInfo() << qUtf8Printable ( QString ( "Resolved network address to %1 for proto %2" ) .arg ( HostAddr.toString() ) .arg (
// HostAddr.protocol() ) );
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol || ( bEnableIPv6 && HostAddr.protocol() == QAbstractSocket::IPv6Protocol ) )
{
InetAddr = HostAddr;
return true;
}
}
return false;
}

bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 )
{
QHostAddress InetAddr;
Expand Down Expand Up @@ -970,31 +995,7 @@ bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAd
return false; // invalid address
}

// try to get host by name, assuming
// that the string contains a valid host name string
const QHostInfo HostInfo = QHostInfo::fromName ( strAddress );

if ( HostInfo.error() != QHostInfo::NoError )
{
// qInfo() << qUtf8Printable ( QString ( "Invalid hostname" ) );
return false; // invalid address
}

bool bFoundAddr = false;

foreach ( const QHostAddress HostAddr, HostInfo.addresses() )
{
// qInfo() << qUtf8Printable ( QString ( "Resolved network address to %1 for proto %2" ) .arg ( HostAddr.toString() ) .arg (
// HostAddr.protocol() ) );
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol || ( bEnableIPv6 && HostAddr.protocol() == QAbstractSocket::IPv6Protocol ) )
{
InetAddr = HostAddr;
bFoundAddr = true;
break;
}
}

if ( !bFoundAddr )
if ( !ParseNetworkAddressString ( strAddress, InetAddr, bEnableIPv6 ) )
{
// no valid address found
// qInfo() << qUtf8Printable ( QString ( "No IP address found for hostname" ) );
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ class CNetworkTransportProps
class NetworkUtil
{
public:
static bool ParseNetworkAddressString ( QString strAddress, QHostAddress& InetAddr, bool bEnableIPv6 );
static bool ParseNetworkAddress ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 );

static QString FixAddress ( const QString& strAddress );
Expand Down

0 comments on commit 24adc20

Please sign in to comment.