From 301cb6d32f5e6c6b8e3a6e3a0dc384eca5c5177e Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 30 Jul 2020 10:36:36 -0400 Subject: [PATCH] fix issue #1320 (allow providing ip/port in bind section of config) --- llarp/config/config.cpp | 26 +++++++++++++++++++------- llarp/net/ip_address.cpp | 12 ++++++++++++ llarp/net/ip_address.hpp | 8 ++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index a5b65caeb..b4603b140 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -345,16 +345,28 @@ namespace llarp LinkInfo info; info.port = 0; info.addressFamily = AF_INET; - info.interface = name; - std::vector splits = split(value, ','); - for (std::string_view str : splits) + if (name == "address") { - int asNum = std::atoi(str.data()); - if (asNum > 0) - info.port = asNum; + const IpAddress addr{value}; + if (not addr.hasPort()) + throw std::invalid_argument("no port provided in link address"); + info.interface = addr.getIpAddr(); + info.port = *addr.getPort(); + } + else + { + info.interface = name; - // otherwise, ignore ("future-proofing") + std::vector splits = split(value, ','); + for (std::string_view str : splits) + { + int asNum = std::atoi(str.data()); + if (asNum > 0) + info.port = asNum; + + // otherwise, ignore ("future-proofing") + } } return info; diff --git a/llarp/net/ip_address.cpp b/llarp/net/ip_address.cpp index a9688a8a4..2143c5bbb 100644 --- a/llarp/net/ip_address.cpp +++ b/llarp/net/ip_address.cpp @@ -137,6 +137,18 @@ namespace llarp return m_ipAddress; // TODO: port } + std::string + IpAddress::getIpAddr() const + { + return m_ipAddress; + } + + bool + IpAddress::hasPort() const + { + return m_port.has_value(); + } + bool IpAddress::operator<(const IpAddress& other) const { diff --git a/llarp/net/ip_address.hpp b/llarp/net/ip_address.hpp index 4b39e4a38..2315dc798 100644 --- a/llarp/net/ip_address.hpp +++ b/llarp/net/ip_address.hpp @@ -69,6 +69,10 @@ namespace llarp std::optional getPort() const; + /// Return true if we have a port set otherwise return false + bool + hasPort() const; + /// Set the port. /// /// @param port @@ -114,6 +118,10 @@ namespace llarp std::string toString() const; + /// get ip address component + std::string + getIpAddr() const; + // TODO: other utility functions left over from Addr which may be useful // IsBogon() const; // isPrivate() const;