Less invasive fix for Addr

pull/1246/head
Stephen Shelton 4 years ago committed by Jeff Becker
parent d3b248e004
commit 42a7bcd434
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -214,7 +214,7 @@ namespace llarp
auto parseAddr = [](std::string input) {
Addr addr;
bool success = addr.from_char_array(input);
bool success = addr.FromString(input);
if (not success)
throw std::invalid_argument(stringify(input, " is not a valid address"));

@ -28,10 +28,7 @@ namespace llarp
void
Addr::port(uint16_t port)
{
if (af() == AF_INET)
{
_addr4.sin_port = htons(port);
}
_addr4.sin_port = htons(port);
_addr.sin6_port = htons(port);
}
@ -61,7 +58,7 @@ namespace llarp
Addr::Addr(std::string_view str) : Addr()
{
this->from_char_array(str);
this->FromString(str);
}
Addr::Addr(std::string_view str, const uint16_t p_port) : Addr(str)
@ -75,17 +72,27 @@ namespace llarp
}
bool
Addr::from_char_array(std::string_view in)
Addr::FromString(std::string_view in)
{
// TODO: this will overwrite port, which may not be specified in the input string
Zero(&_addr, sizeof(sockaddr_in6));
std::string ipPortion;
auto pPosition = in.find(':');
if (pPosition != std::string_view::npos)
{
// parse port
uint16_t port = std::atoi(std::string(in.begin() + pPosition + 1, in.end()).c_str());
LogDebug("Setting port ", std::to_string(port));
const std::string portStr = std::string(in.substr(pPosition + 1));
uint16_t port = std::atoi(portStr.c_str());
this->port(port);
ipPortion = std::string(in.substr(0, pPosition));
}
Zero(&_addr, sizeof(sockaddr_in6));
else
{
ipPortion = std::string(in);
}
struct addrinfo hint, *res = nullptr;
int ret;
@ -123,7 +130,7 @@ namespace llarp
// put it in _addr4
struct in_addr* addr = &_addr4.sin_addr;
if (inet_aton(std::string(in).c_str(), addr) == 0)
if (inet_aton(ipPortion.c_str(), addr) == 0)
{
LogError("failed to parse ", in);
return false;
@ -131,7 +138,6 @@ namespace llarp
_addr.sin6_family = res->ai_family;
_addr4.sin_family = res->ai_family;
_addr4.sin_port = 0; // save a call, 0 is 0 no matter how u arrange it
#if ((__APPLE__ && __MACH__) || __FreeBSD__)
_addr4.sin_len = sizeof(in_addr);
#endif

@ -43,7 +43,7 @@ namespace llarp
addr4() const;
bool
from_char_array(std::string_view str);
FromString(std::string_view str);
bool
from_4int(const uint8_t one, const uint8_t two, const uint8_t three, const uint8_t four);

@ -1,10 +1,13 @@
#include <net/net_addr.hpp>
#include <catch2/catch.hpp>
TEST_CASE("Addr from_char_array", "[addr]")
TEST_CASE("Addr FromString", "[addr]")
{
llarp::Addr addr;
bool success = false;
CHECK_NOTHROW(success = addr.from_char_array("127.0.0.1:53"));
CHECK_NOTHROW(success = addr.FromString("127.0.0.1:53"));
CHECK(success == true);
CHECK(addr.port() == 53);
CHECK(addr.ToString() == "127.0.0.1:53");
}

Loading…
Cancel
Save