aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/net')
-rw-r--r--src/lib/net/NetworkAddress.cpp88
-rw-r--r--src/lib/net/SecureListenSocket.cpp3
-rw-r--r--src/lib/net/SecureSocket.cpp3
3 files changed, 47 insertions, 47 deletions
diff --git a/src/lib/net/NetworkAddress.cpp b/src/lib/net/NetworkAddress.cpp
index c395ab0..8d60567 100644
--- a/src/lib/net/NetworkAddress.cpp
+++ b/src/lib/net/NetworkAddress.cpp
@@ -28,6 +28,45 @@
// NetworkAddress
//
+static bool parse_address(const std::string& address, std::string& host, int& port)
+{
+ /* Three cases ---
+ * brackets: parse inside for host, check end for port as :INTEGER. DONE
+ * one colon: ipv4 address with port. DONE
+ * otherwise: all host, no port. DONE
+ *
+ * very, very little error checking. depends on address being trimmed before call.
+ *
+ * does not override port with a default value if no port was found in address.
+ */
+
+ if (address[0] == '[') {
+ // bracketed host possibly followed by port as :INTEGER
+ auto endBracket = address.find(']', 1);
+ if (endBracket == std::string::npos)
+ return false;
+ host = address.substr(1, endBracket - 1);
+ if (endBracket + 1 < address.length()) {
+ // port follows (or garbage)
+ if (address[endBracket + 1] != ':')
+ return false;
+ port = std::strtol(&address[endBracket + 2], nullptr, 10);
+ }
+ } else {
+ auto colon = address.find(':');
+ if (colon != std::string::npos && address.find(':', colon + 1) == std::string::npos) {
+ // one single colon, must be ipv4 with port
+ host = address.substr(0, colon);
+ port = std::strtol(&address[colon + 1], nullptr, 10);
+ } else {
+ // no colons (ipv4) or more than one colon (ipv6), both without port
+ host = address;
+ }
+ }
+
+ return true;
+}
+
// name re-resolution adapted from a patch by Brent Priddy.
NetworkAddress::NetworkAddress() :
@@ -62,50 +101,9 @@ NetworkAddress::NetworkAddress(const String& hostname, int port) :
m_hostname(hostname),
m_port(port)
{
- // check for port suffix
- String::size_type i = m_hostname.rfind(':');
- if (i != String::npos && i + 1 < m_hostname.size()) {
- // found a colon. see if it looks like an IPv6 address.
- bool colonNotation = false;
- bool dotNotation = false;
- bool doubleColon = false;
- for (String::size_type j = 0; j < i; ++j) {
- if (m_hostname[j] == ':') {
- colonNotation = true;
- dotNotation = false;
- if (m_hostname[j + 1] == ':') {
- doubleColon = true;
- }
- }
- else if (m_hostname[j] == '.' && colonNotation) {
- dotNotation = true;
- }
- }
-
- // port suffix is ambiguous with IPv6 notation if there's
- // a double colon and the end of the address is not in dot
- // notation. in that case we assume it's not a port suffix.
- // the user can replace the double colon with zeros to
- // disambiguate.
- if ((!doubleColon || dotNotation) && !colonNotation) {
- // parse port from hostname
- char* end;
- const char* chostname = m_hostname.c_str();
- long suffixPort = strtol(chostname + i + 1, &end, 10);
- if (end == chostname + i + 1 || *end != '\0') {
- throw XSocketAddress(XSocketAddress::kBadPort,
- m_hostname, m_port);
- }
-
- // trim port from hostname
- m_hostname.erase(i);
-
- // save port
- m_port = static_cast<int>(suffixPort);
- }
- }
-
- // check port number
+ if (!parse_address(hostname, m_hostname, m_port))
+ throw XSocketAddress(XSocketAddress::kUnknown,
+ m_hostname, m_port);
checkPort();
}
@@ -145,7 +143,7 @@ NetworkAddress::resolve()
// if hostname is empty then use wildcard address otherwise look
// up the name.
if (m_hostname.empty()) {
- m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
+ m_address = ARCH->newAnyAddr(IArchNetwork::kINET6);
}
else {
m_address = ARCH->nameToAddr(m_hostname);
diff --git a/src/lib/net/SecureListenSocket.cpp b/src/lib/net/SecureListenSocket.cpp
index 58ffe09..7af905e 100644
--- a/src/lib/net/SecureListenSocket.cpp
+++ b/src/lib/net/SecureListenSocket.cpp
@@ -22,6 +22,7 @@
#include "net/SocketMultiplexer.h"
#include "net/TSocketMultiplexerMethodJob.h"
#include "arch/XArch.h"
+#include "common/DataDirectories.h"
static const char s_certificateDir[] = { "SSL" };
static const char s_certificateFilename[] = { "Barrier.pem" };
@@ -54,7 +55,7 @@ SecureListenSocket::accept()
}
String certificateFilename = barrier::string::sprintf("%s/%s/%s",
- ARCH->getProfileDirectory().c_str(),
+ DataDirectories::profile().c_str(),
s_certificateDir,
s_certificateFilename);
diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp
index 1fefae0..6670f5f 100644
--- a/src/lib/net/SecureSocket.cpp
+++ b/src/lib/net/SecureSocket.cpp
@@ -23,6 +23,7 @@
#include "mt/Lock.h"
#include "arch/XArch.h"
#include "base/Log.h"
+#include "common/DataDirectories.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
@@ -699,7 +700,7 @@ SecureSocket::verifyCertFingerprint()
String trustedServersFilename;
trustedServersFilename = barrier::string::sprintf(
"%s/%s/%s",
- ARCH->getProfileDirectory().c_str(),
+ DataDirectories::profile().c_str(),
kFingerprintDirName,
kFingerprintTrustedServersFilename);