@@ -914,6 +914,75 @@ bool NetworkUtil::ParseNetworkAddressString ( QString strAddress, QHostAddress&
914914 return false ;
915915}
916916
917+ #ifndef CLIENT_NO_SRV_CONNECT
918+ bool NetworkUtil::ParseNetworkAddressSrv ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 )
919+ {
920+ // init requested host address with invalid address first
921+ HostAddress = CHostAddress ();
922+
923+ QRegularExpression plainHostRegex ( " ^([^\\ [:0-9.][^:]*)$" );
924+ if ( plainHostRegex.match ( strAddress ).capturedStart () != 0 )
925+ {
926+ // not a plain hostname? then don't attempt SRV lookup and fail
927+ // immediately.
928+ return false ;
929+ }
930+
931+ QDnsLookup* dns = new QDnsLookup ();
932+ dns->setType ( QDnsLookup::SRV );
933+ dns->setName ( QString ( " _jamulus._udp.%1" ).arg ( strAddress ) );
934+ dns->lookup ();
935+ // QDnsLookup::lookup() works asynchronously. Therefore, wait for
936+ // it to complete here by resuming the main loop here.
937+ // This is not nice and blocks the UI, but is similar to what
938+ // the regular resolve function does as well.
939+ QTime dieTime = QTime::currentTime ().addMSecs ( DNS_SRV_RESOLVE_TIMEOUT_MS );
940+ while ( QTime::currentTime () < dieTime && !dns->isFinished () )
941+ {
942+ QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
943+ }
944+ QList<QDnsServiceRecord> records = dns->serviceRecords ();
945+ dns->deleteLater ();
946+ if ( records.length () != 1 )
947+ {
948+ return false ;
949+ }
950+ QDnsServiceRecord record = records.first ();
951+ if ( record.target () == " ." || record.target () == " " )
952+ {
953+ // RFC2782 says that "." indicates that the service is not available.
954+ // Qt strips the trailing dot, which is why we check for empty string
955+ // as well. Therefore, the "." part might be redundant, but this would
956+ // need further testing to confirm.
957+ // End processing here (= return true), but pass back an
958+ // invalid HostAddress to let the connect logic fail properly.
959+ HostAddress = CHostAddress ( QHostAddress ( " ." ), 0 );
960+ return true ;
961+ }
962+ qDebug () << qUtf8Printable (
963+ QString ( " resolved %1 to a single SRV record: %2:%3" ).arg ( strAddress ).arg ( record.target () ).arg ( record.port () ) );
964+
965+ QHostAddress InetAddr;
966+ if ( ParseNetworkAddressString ( record.target (), InetAddr, bEnableIPv6 ) )
967+ {
968+ HostAddress = CHostAddress ( InetAddr, record.port () );
969+ return true ;
970+ }
971+ return false ;
972+ }
973+
974+ bool NetworkUtil::ParseNetworkAddressWithSrvDiscovery ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 )
975+ {
976+ // Try SRV-based discovery first:
977+ if ( ParseNetworkAddressSrv ( strAddress, HostAddress, bEnableIPv6 ) )
978+ {
979+ return true ;
980+ }
981+ // Try regular connect via plain IP or host name lookup (A/AAAA):
982+ return ParseNetworkAddress ( strAddress, HostAddress, bEnableIPv6 );
983+ }
984+ #endif
985+
917986bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAddress, bool bEnableIPv6 )
918987{
919988 QHostAddress InetAddr;
0 commit comments