summaryrefslogtreecommitdiff
path: root/NetworkServerLookup.cpp
diff options
context:
space:
mode:
authorIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
committerIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
commitd63dd8d081a6fcd0715cd13f26df59fb831769d2 (patch)
treeb9c1f6245147a310aac3762630bad389bbb57335 /NetworkServerLookup.cpp
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'NetworkServerLookup.cpp')
-rw-r--r--NetworkServerLookup.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/NetworkServerLookup.cpp b/NetworkServerLookup.cpp
new file mode 100644
index 00000000..3e0ec34a
--- /dev/null
+++ b/NetworkServerLookup.cpp
@@ -0,0 +1,86 @@
+#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;
+ }
+ // drop 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);
+}