diff options
| author | Christoph Berg <christoph.berg@credativ.de> | 2022-08-23 10:23:16 +0200 |
|---|---|---|
| committer | Christoph Berg <christoph.berg@credativ.de> | 2022-08-23 10:23:16 +0200 |
| commit | 2d1068e60bce7ae1fff11623454900e8cee956b1 (patch) | |
| tree | acebe276e30466ef0ab91bc3da5e379bec0d7dc5 /Network/NetworkServerLookup.cpp | |
| parent | 43bebfbad6e7ba026bd3d55720c7d9e12a7ef8ef (diff) | |
New upstream version 2.6.0~rc3+repackupstream/2.6.0_rc3+repack
Diffstat (limited to 'Network/NetworkServerLookup.cpp')
| -rwxr-xr-x | Network/NetworkServerLookup.cpp | 170 |
1 files changed, 85 insertions, 85 deletions
diff --git a/Network/NetworkServerLookup.cpp b/Network/NetworkServerLookup.cpp index 0d7cd4fe..c516822a 100755 --- a/Network/NetworkServerLookup.cpp +++ b/Network/NetworkServerLookup.cpp @@ -1,85 +1,85 @@ -#include "NetworkServerLookup.hpp" - -#include <stdexcept> - -#include <QHostInfo> -#include <QString> - -std::tuple<QHostAddress, quint16> -network_server_lookup (QString query - , quint16 default_service_port - , QHostAddress default_host_address - , QAbstractSocket::NetworkLayerProtocol required_protocol) -{ - query = query.trimmed (); - - QHostAddress host_address {default_host_address}; - quint16 service_port {default_service_port}; - - QString host_name; - if (!query.isEmpty ()) - { - int port_colon_index {-1}; - - if ('[' == query[0]) - { - // assume IPv6 combined address/port syntax [<address>]:<port> - auto close_bracket_index = query.lastIndexOf (']'); - host_name = query.mid (1, close_bracket_index - 1); - port_colon_index = query.indexOf (':', close_bracket_index); - } - else - { - port_colon_index = query.lastIndexOf (':'); - host_name = query.left (port_colon_index); - } - host_name = host_name.trimmed (); - - if (port_colon_index >= 0) - { - bool ok; - service_port = query.mid (port_colon_index + 1).trimmed ().toUShort (&ok); - if (!ok) - { - throw std::runtime_error {"network server lookup error: invalid port"}; - } - } - } - - if (!host_name.isEmpty ()) - { - auto host_info = QHostInfo::fromName (host_name); - if (host_info.addresses ().isEmpty ()) - { - throw std::runtime_error {"network server lookup error: host name lookup failed"}; - } - - bool found {false}; - for (int i {0}; i < host_info.addresses ().size () && !found; ++i) - { - host_address = host_info.addresses ().at (i); - switch (required_protocol) - { - case QAbstractSocket::IPv4Protocol: - case QAbstractSocket::IPv6Protocol: - if (required_protocol != host_address.protocol ()) - { - break; - } - // fall through - case QAbstractSocket::AnyIPProtocol: - found = true; - break; - - default: - throw std::runtime_error {"network server lookup error: invalid required protocol"}; - } - } - if (!found) - { - throw std::runtime_error {"network server lookup error: no suitable host address found"}; - } - } - - return std::make_tuple (host_address, service_port); -} +#include "NetworkServerLookup.hpp"
+
+#include <stdexcept>
+
+#include <QHostInfo>
+#include <QString>
+
+std::tuple<QHostAddress, quint16>
+network_server_lookup (QString query
+ , quint16 default_service_port
+ , QHostAddress default_host_address
+ , QAbstractSocket::NetworkLayerProtocol required_protocol)
+{
+ query = query.trimmed ();
+
+ QHostAddress host_address {default_host_address};
+ quint16 service_port {default_service_port};
+
+ QString host_name;
+ if (!query.isEmpty ())
+ {
+ int port_colon_index {-1};
+
+ if ('[' == query[0])
+ {
+ // assume IPv6 combined address/port syntax [<address>]:<port>
+ auto close_bracket_index = query.lastIndexOf (']');
+ host_name = query.mid (1, close_bracket_index - 1);
+ port_colon_index = query.indexOf (':', close_bracket_index);
+ }
+ else
+ {
+ port_colon_index = query.lastIndexOf (':');
+ host_name = query.left (port_colon_index);
+ }
+ host_name = host_name.trimmed ();
+
+ if (port_colon_index >= 0)
+ {
+ bool ok;
+ service_port = query.mid (port_colon_index + 1).trimmed ().toUShort (&ok);
+ if (!ok)
+ {
+ throw std::runtime_error {"network server lookup error: invalid port"};
+ }
+ }
+ }
+
+ if (!host_name.isEmpty ())
+ {
+ auto host_info = QHostInfo::fromName (host_name);
+ if (host_info.addresses ().isEmpty ())
+ {
+ throw std::runtime_error {"network server lookup error: host name lookup failed"};
+ }
+
+ bool found {false};
+ for (int i {0}; i < host_info.addresses ().size () && !found; ++i)
+ {
+ host_address = host_info.addresses ().at (i);
+ switch (required_protocol)
+ {
+ case QAbstractSocket::IPv4Protocol:
+ case QAbstractSocket::IPv6Protocol:
+ if (required_protocol != host_address.protocol ())
+ {
+ break;
+ }
+ // fall through
+ case QAbstractSocket::AnyIPProtocol:
+ found = true;
+ break;
+
+ default:
+ throw std::runtime_error {"network server lookup error: invalid required protocol"};
+ }
+ }
+ if (!found)
+ {
+ throw std::runtime_error {"network server lookup error: no suitable host address found"};
+ }
+ }
+
+ return std::make_tuple (host_address, service_port);
+}
|
