Skip to content

Commit 24adc20

Browse files
hoffiepljones
andcommitted
Internal: Refactor NetworkUtil::ParseNetworkAddress
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>
1 parent 7075708 commit 24adc20

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/util.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,31 @@ QSize CMinimumStackedLayout::sizeHint() const
889889
* Other Classes *
890890
\******************************************************************************/
891891
// Network utility functions ---------------------------------------------------
892+
bool NetworkUtil::ParseNetworkAddressString ( QString strAddress, QHostAddress& InetAddr, bool bEnableIPv6 )
893+
{
894+
// try to get host by name, assuming
895+
// that the string contains a valid host name string or IP address
896+
const QHostInfo HostInfo = QHostInfo::fromName ( strAddress );
897+
898+
if ( HostInfo.error() != QHostInfo::NoError )
899+
{
900+
// qInfo() << qUtf8Printable ( QString ( "Invalid hostname" ) );
901+
return false; // invalid address
902+
}
903+
904+
foreach ( const QHostAddress HostAddr, HostInfo.addresses() )
905+
{
906+
// qInfo() << qUtf8Printable ( QString ( "Resolved network address to %1 for proto %2" ) .arg ( HostAddr.toString() ) .arg (
907+
// HostAddr.protocol() ) );
908+
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol || ( bEnableIPv6 && HostAddr.protocol() == QAbstractSocket::IPv6Protocol ) )
909+
{
910+
InetAddr = HostAddr;
911+
return true;
912+
}
913+
}
914+
return false;
915+
}
916+
892917
bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 )
893918
{
894919
QHostAddress InetAddr;
@@ -970,31 +995,7 @@ bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAd
970995
return false; // invalid address
971996
}
972997

973-
// try to get host by name, assuming
974-
// that the string contains a valid host name string
975-
const QHostInfo HostInfo = QHostInfo::fromName ( strAddress );
976-
977-
if ( HostInfo.error() != QHostInfo::NoError )
978-
{
979-
// qInfo() << qUtf8Printable ( QString ( "Invalid hostname" ) );
980-
return false; // invalid address
981-
}
982-
983-
bool bFoundAddr = false;
984-
985-
foreach ( const QHostAddress HostAddr, HostInfo.addresses() )
986-
{
987-
// qInfo() << qUtf8Printable ( QString ( "Resolved network address to %1 for proto %2" ) .arg ( HostAddr.toString() ) .arg (
988-
// HostAddr.protocol() ) );
989-
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol || ( bEnableIPv6 && HostAddr.protocol() == QAbstractSocket::IPv6Protocol ) )
990-
{
991-
InetAddr = HostAddr;
992-
bFoundAddr = true;
993-
break;
994-
}
995-
}
996-
997-
if ( !bFoundAddr )
998+
if ( !ParseNetworkAddressString ( strAddress, InetAddr, bEnableIPv6 ) )
998999
{
9991000
// no valid address found
10001001
// qInfo() << qUtf8Printable ( QString ( "No IP address found for hostname" ) );

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ class CNetworkTransportProps
10381038
class NetworkUtil
10391039
{
10401040
public:
1041+
static bool ParseNetworkAddressString ( QString strAddress, QHostAddress& InetAddr, bool bEnableIPv6 );
10411042
static bool ParseNetworkAddress ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 );
10421043

10431044
static QString FixAddress ( const QString& strAddress );

0 commit comments

Comments
 (0)