remove valgrind access errors

pull/916/head
Jeff Becker 5 years ago
parent 0ec4e583d4
commit ac686a9329
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -82,7 +82,6 @@ set(LIB_PLATFORM_SRC
net/ip.cpp
net/net.cpp
net/net_addr.cpp
net/net_inaddr.cpp
net/net_int.cpp
# for android shim
${ANDROID_PLATFORM_SRC}

@ -28,14 +28,15 @@ namespace llarp
return pkt;
}
Session::Session(LinkLayer* p, RouterContact rc, AddressInfo ai)
Session::Session(LinkLayer* p, const RouterContact& rc,
const AddressInfo& ai)
: m_State{State::Initial}
, m_Inbound{false}
, m_Parent{p}
, m_Parent(p)
, m_CreatedAt{p->Now()}
, m_RemoteAddr{ai}
, m_ChosenAI{std::move(ai)}
, m_RemoteRC{std::move(rc)}
, m_RemoteAddr(ai)
, m_ChosenAI(ai)
, m_RemoteRC(rc)
{
token.Zero();
GotLIM = util::memFn(&Session::GotOutboundLIM, this);
@ -43,12 +44,12 @@ namespace llarp
llarp_buffer_t(rc.pubkey));
}
Session::Session(LinkLayer* p, Addr from)
Session::Session(LinkLayer* p, const Addr& from)
: m_State{State::Initial}
, m_Inbound{true}
, m_Parent{p}
, m_Parent(p)
, m_CreatedAt{p->Now()}
, m_RemoteAddr{from}
, m_RemoteAddr(from)
{
token.Randomize();
GotLIM = util::memFn(&Session::GotInboundLIM, this);
@ -145,8 +146,10 @@ namespace llarp
Session::EncryptWorker(CryptoQueue_ptr msgs)
{
LogDebug("encrypt worker ", msgs->size(), " messages");
for(auto& pkt : *msgs)
auto itr = msgs->begin();
while(itr != msgs->end())
{
Packet_t pkt = std::move(*itr);
llarp_buffer_t pktbuf(pkt);
const TunnelNonce nonce_ptr{pkt.data() + HMACSIZE};
pktbuf.base += PacketOverhead;
@ -157,6 +160,7 @@ namespace llarp
pktbuf.sz = pkt.size() - HMACSIZE;
CryptoManager::instance()->hmac(pkt.data(), pktbuf, m_SessionKey);
Send_LL(pkt.data(), pkt.size());
++itr;
}
}
@ -359,24 +363,27 @@ namespace llarp
{
TunnelNonce N;
N.Randomize();
ILinkSession::Packet_t req(Introduction::SIZE + PacketOverhead);
const auto pk = m_Parent->GetOurRC().pubkey;
const auto e_pk = m_Parent->RouterEncryptionSecret().toPublic();
auto itr = req.data() + PacketOverhead;
std::copy_n(pk.data(), pk.size(), itr);
itr += pk.size();
std::copy_n(e_pk.data(), e_pk.size(), itr);
itr += e_pk.size();
std::copy_n(N.data(), N.size(), itr);
Signature Z;
llarp_buffer_t signbuf(req.data() + PacketOverhead,
Introduction::SIZE - Signature::SIZE);
m_Parent->Sign(Z, signbuf);
std::copy_n(
Z.data(), Z.size(),
req.data() + PacketOverhead + (Introduction::SIZE - Signature::SIZE));
CryptoManager::instance()->randbytes(req.data() + HMACSIZE, TUNNONCESIZE);
EncryptAndSend(std::move(req));
{
ILinkSession::Packet_t req(Introduction::SIZE + PacketOverhead);
const auto pk = m_Parent->GetOurRC().pubkey;
const auto e_pk = m_Parent->RouterEncryptionSecret().toPublic();
auto itr = req.data() + PacketOverhead;
std::copy_n(pk.data(), pk.size(), itr);
itr += pk.size();
std::copy_n(e_pk.data(), e_pk.size(), itr);
itr += e_pk.size();
std::copy_n(N.data(), N.size(), itr);
Signature Z;
llarp_buffer_t signbuf(req.data() + PacketOverhead,
Introduction::SIZE - Signature::SIZE);
m_Parent->Sign(Z, signbuf);
std::copy_n(Z.data(), Z.size(),
req.data() + PacketOverhead
+ (Introduction::SIZE - Signature::SIZE));
CryptoManager::instance()->randbytes(req.data() + HMACSIZE,
TUNNONCESIZE);
EncryptAndSend(std::move(req));
}
m_State = State::Introduction;
if(not CryptoManager::instance()->transport_dh_client(
m_SessionKey, m_ChosenAI.pubkey,

@ -39,9 +39,10 @@ namespace llarp
static constexpr std::size_t MaxACKSInMACK = 1024 / sizeof(uint64_t);
/// outbound session
Session(LinkLayer* parent, RouterContact rc, AddressInfo ai);
Session(LinkLayer* parent, const RouterContact& rc,
const AddressInfo& ai);
/// inbound session
Session(LinkLayer* parent, Addr from);
Session(LinkLayer* parent, const Addr& from);
~Session() = default;

@ -18,8 +18,8 @@ namespace llarp
{
struct ExitInfo
{
struct in6_addr address;
struct in6_addr netmask;
in6_addr address = {0};
in6_addr netmask = {0};
PubKey pubkey;
uint64_t version = LLARP_PROTO_VERSION;

@ -1078,7 +1078,7 @@ namespace llarp
{
char buf[INET6_ADDRSTRLEN + 1] = {0};
std::string str;
in6_addr inaddr;
in6_addr inaddr = {0};
size_t numset = 0;
absl::uint128 bits = netmask_bits.h;
while(bits)

@ -191,6 +191,5 @@ namespace llarp
} // namespace llarp
#include <net/net_addr.hpp>
#include <net/net_inaddr.hpp>
#endif

@ -16,7 +16,13 @@
namespace llarp
{
Addr::Addr() = default;
Addr::Addr()
{
llarp::Zero(&_addr4, sizeof(_addr4));
_addr4.sin_family = AF_INET;
llarp::Zero(&_addr, sizeof(_addr));
_addr.sin6_family = AF_INET6;
}
Addr::~Addr() = default;
void
@ -53,18 +59,18 @@ namespace llarp
return (const in_addr*)&_addr.sin6_addr.s6_addr[12];
}
Addr::Addr(string_view str)
Addr::Addr(string_view str) : Addr()
{
this->from_char_array(str);
}
Addr::Addr(string_view str, const uint16_t p_port)
Addr::Addr(string_view str, const uint16_t p_port) : Addr()
{
this->from_char_array(str);
this->port(p_port);
}
Addr::Addr(string_view addr_str, string_view port_str)
Addr::Addr(string_view addr_str, string_view port_str) : Addr()
{
this->from_char_array(string_view_string(addr_str).c_str());
this->port(std::strtoul(string_view_string(port_str).c_str(), nullptr, 10));
@ -174,18 +180,20 @@ namespace llarp
Addr::Addr(const uint8_t one, const uint8_t two, const uint8_t three,
const uint8_t four)
: Addr()
{
this->from_4int(one, two, three, four);
}
Addr::Addr(const uint8_t one, const uint8_t two, const uint8_t three,
const uint8_t four, const uint16_t p_port)
: Addr()
{
this->from_4int(one, two, three, four);
this->port(p_port);
}
Addr::Addr(const AddressInfo& other)
Addr::Addr(const AddressInfo& other) : Addr()
{
memcpy(addr6(), other.ip.s6_addr, 16);
_addr.sin6_port = htons(other.port);
@ -200,7 +208,7 @@ namespace llarp
_addr.sin6_family = AF_INET6;
}
Addr::Addr(const sockaddr_in& other)
Addr::Addr(const sockaddr_in& other) : Addr()
{
Zero(&_addr, sizeof(sockaddr_in6));
_addr.sin6_family = AF_INET;
@ -217,7 +225,7 @@ namespace llarp
memcpy(&_addr4.sin_addr.s_addr, addr4(), sizeof(in_addr));
}
Addr::Addr(const sockaddr_in6& other)
Addr::Addr(const sockaddr_in6& other) : Addr()
{
memcpy(addr6(), other.sin6_addr.s6_addr, 16);
_addr.sin6_port = htons(other.sin6_port);
@ -236,7 +244,7 @@ namespace llarp
_addr.sin6_family = AF_INET6;
}
Addr::Addr(const sockaddr& other)
Addr::Addr(const sockaddr& other) : Addr()
{
Zero(&_addr, sizeof(sockaddr_in6));
_addr.sin6_family = other.sa_family;

@ -16,7 +16,7 @@ namespace llarp
{
// network order
sockaddr_in6 _addr;
sockaddr_in _addr4; // why do we even have this? favor cpu over memory
sockaddr_in _addr4;
~Addr();
Addr();

@ -1,228 +0,0 @@
#include <net/net_inaddr.hpp>
std::ostream&
operator<<(std::ostream& out, const llarp::inAddr& a)
{
char tmp[128] = {0};
if(a.isIPv6Mode())
{
out << "[";
}
if(inet_ntop(a.isIPv4Mode() ? AF_INET : AF_INET6, (void*)&a._addr, tmp,
sizeof(tmp)))
{
out << tmp;
if(a.isIPv6Mode())
out << "]";
}
return out;
}
namespace llarp
{
void
inAddr::reset()
{
llarp::Zero(&this->_addr, sizeof(in6_addr));
}
bool
inAddr::from_char_array(const char* str)
{
this->reset();
// maybe refactor the family detection out
struct addrinfo hint, *res = nullptr;
int ret;
memset(&hint, '\0', sizeof hint);
hint.ai_family = PF_UNSPEC;
hint.ai_flags = AI_NUMERICHOST;
ret = getaddrinfo(str, nullptr, &hint, &res);
if(ret)
{
llarp::LogError("failed to determine address family: ", str);
return false;
}
if(res->ai_family != AF_INET && res->ai_family != AF_INET6)
{
llarp::LogError("Address family not supported yet", str);
return false;
}
// convert detected-family (ipv4 or ipv6) str to in6_addr
/*
if (res->ai_family == AF_INET)
{
freeaddrinfo(res);
// get IPv4
struct in_addr addr; // basically a uint32_t network order
if(inet_aton(str, &addr) == 0)
{
llarp::LogError("failed to parse ", str);
return false;
}
nuint32_t result;
result.n = addr.s_addr;
this->fromN32(result);
return true;
}
*/
ret = inet_pton(res->ai_family, str, &this->_addr);
// inet_pton won't set SIIT
// this->hexDebug();
freeaddrinfo(res);
if(ret <= 0)
{
if(ret == 0)
{
llarp::LogWarn("Not in presentation format");
return false;
}
llarp::LogWarn("inet_pton failure");
return false;
}
return true;
}
void
inAddr::fromSIIT()
{
if(ipv6_is_siit(this->_addr))
{
this->_addr.s6_addr[0] = this->_addr.s6_addr[12];
this->_addr.s6_addr[1] = this->_addr.s6_addr[13];
this->_addr.s6_addr[2] = this->_addr.s6_addr[14];
this->_addr.s6_addr[3] = this->_addr.s6_addr[15];
this->setIPv4Mode();
}
}
void
inAddr::toSIIT()
{
if(!ipv6_is_siit(this->_addr))
{
this->_addr.s6_addr[10] = 0xff;
this->_addr.s6_addr[11] = 0xff;
this->_addr.s6_addr[12] = this->_addr.s6_addr[0];
this->_addr.s6_addr[13] = this->_addr.s6_addr[1];
this->_addr.s6_addr[14] = this->_addr.s6_addr[2];
this->_addr.s6_addr[15] = this->_addr.s6_addr[3];
llarp::Zero(&this->_addr, sizeof(in6_addr) - 6);
}
}
inline bool
inAddr::isIPv6Mode() const
{
return !this->isIPv4Mode();
}
bool
inAddr::isIPv4Mode() const
{
return ipv6_is_siit(this->_addr)
|| (this->_addr.s6_addr[4] == 0 && this->_addr.s6_addr[5] == 0
&& this->_addr.s6_addr[6] == 0 && this->_addr.s6_addr[7] == 0
&& this->_addr.s6_addr[8] == 0 && this->_addr.s6_addr[9] == 0
&& this->_addr.s6_addr[10] == 0 && this->_addr.s6_addr[11] == 0
&& this->_addr.s6_addr[12] == 0 && this->_addr.s6_addr[13] == 0
&& this->_addr.s6_addr[14] == 0 && this->_addr.s6_addr[15] == 0);
}
void
inAddr::setIPv4Mode()
{
// keep first 4
// llarp::Zero(&this->_addr + 4, sizeof(in6_addr) - 4);
this->_addr.s6_addr[4] = 0;
this->_addr.s6_addr[5] = 0;
this->_addr.s6_addr[6] = 0;
this->_addr.s6_addr[7] = 0;
this->_addr.s6_addr[8] = 0;
this->_addr.s6_addr[9] = 0;
this->_addr.s6_addr[10] = 0;
this->_addr.s6_addr[11] = 0;
this->_addr.s6_addr[12] = 0;
this->_addr.s6_addr[13] = 0;
this->_addr.s6_addr[14] = 0;
this->_addr.s6_addr[15] = 0;
}
void
inAddr::hexDebug()
{
char hex_buffer[16 * 3 + 1];
hex_buffer[16 * 3] = 0;
for(unsigned int j = 0; j < 16; j++)
sprintf(&hex_buffer[3 * j], "%02X ", this->_addr.s6_addr[j]);
printf("in6_addr: [%s]\n", hex_buffer);
}
//
// IPv4 specific functions
//
in_addr
inAddr::toIAddr()
{
in_addr res;
res.s_addr = toN32().n;
return res;
}
void
inAddr::from4int(const uint8_t one, const uint8_t two, const uint8_t three,
const uint8_t four)
{
this->reset();
this->setIPv4Mode();
// Network byte order
this->_addr.s6_addr[0] = one;
this->_addr.s6_addr[1] = two;
this->_addr.s6_addr[2] = three;
this->_addr.s6_addr[3] = four;
}
void
inAddr::fromN32(nuint32_t in)
{
this->reset();
this->setIPv4Mode();
memcpy(&this->_addr, &in.n, sizeof(uint32_t));
}
void
inAddr::fromH32(huint32_t in)
{
this->fromN32(xhtonl(in));
}
nuint32_t
inAddr::toN32()
{
nuint32_t result;
result.n = 0; // return 0 for IPv6
if(this->isIPv4Mode())
{
memcpy(&result.n, &this->_addr, sizeof(uint32_t));
}
return result;
}
huint32_t
inAddr::toH32()
{
return xntohl(this->toN32());
}
//
// IPv6 specific functions
//
} // namespace llarp

@ -1,81 +0,0 @@
#ifndef LLARP_NET_INADDR_HPP
#define LLARP_NET_INADDR_HPP
#include <net/net.hpp>
namespace llarp
{
/// IPv4 or IPv6 holder
struct inAddr
{
// unsigned char s6_addr[16];
struct in6_addr _addr; // store in network order
/// zero out
void
reset();
/// from char*
bool
from_char_array(const char* str);
/// convert from SIIT to IPv4 Mode
void
fromSIIT();
/// convert from IPv4 Mode to SIIT
void
toSIIT();
/// not IPv4 Mode (an actual IPv6 address)
inline bool
isIPv6Mode() const;
/// IPv4 mode (not SIIT)
bool
isIPv4Mode() const;
/// clear out bytes 5-15 (Last 12 bytes)
/// This is how inet_pton works with IPv4 addresses
void
setIPv4Mode();
/// make debugging/testing easier
void
hexDebug();
//
// IPv4 specific functions
//
/// make ipv4 in_addr struct
in_addr
toIAddr();
/// set an IPv4 addr
void
from4int(const uint8_t one, const uint8_t two, const uint8_t three,
const uint8_t four);
/// set from an net-order uint32_t
void
fromN32(nuint32_t in);
/// set from an host-order uint32_t
void
fromH32(huint32_t in);
/// output as net-order uint32_t
nuint32_t
toN32();
/// output as host-order uint32_t
huint32_t
toH32();
//
// IPv6 specific functions
//
// coming soon
};
} // namespace llarp
#endif

@ -18,7 +18,6 @@ list(APPEND TEST_SRC
exit/test_llarp_exit_context.cpp
link/test_llarp_link.cpp
llarp_test.cpp
net/test_llarp_net_inaddr.cpp
net/test_llarp_net.cpp
routing/llarp_routing_transfer_traffic.cpp
routing/test_llarp_routing_obtainexitmessage.cpp

@ -1,7 +1,6 @@
#include <gtest/gtest.h>
#include <net/net.hpp>
#include <net/net_inaddr.hpp>
#include <net/net_int.hpp>
#include <net/ip.hpp>

@ -1,106 +0,0 @@
#include <gtest/gtest.h>
#include <net/net_inaddr.hpp>
struct TestNetInAddr : public ::testing::Test
{
};
TEST_F(TestNetInAddr, TestinAddrFrom4Int)
{
llarp::inAddr test;
test.from4int(127, 0, 0, 1);
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL)
{
ASSERT_TRUE(false);
}
ASSERT_TRUE(strcmp("127.0.0.1", str) == 0);
}
TEST_F(TestNetInAddr, TestinAddrFromStr)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL)
{
ASSERT_TRUE(false);
}
ASSERT_TRUE(strcmp("127.0.0.1", str) == 0);
}
TEST_F(TestNetInAddr, TestinAddrReset)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
test.reset();
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL)
{
ASSERT_TRUE(false);
}
ASSERT_TRUE(strcmp("0.0.0.0", str) == 0);
}
TEST_F(TestNetInAddr, TestinAddrModeSet)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
// test.hexDebug();
ASSERT_TRUE(test.isIPv4Mode());
// corrupt it
test._addr.s6_addr[10] = 0xfe;
test._addr.s6_addr[11] = 0xfe;
test.setIPv4Mode();
// test.hexDebug();
ASSERT_TRUE(test.isIPv4Mode());
}
TEST_F(TestNetInAddr, TestinAddrSIIT)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
test.toSIIT();
// test.hexDebug();
ASSERT_TRUE(llarp::ipv6_is_siit(test._addr));
test.fromSIIT();
// test.hexDebug();
ASSERT_TRUE(!llarp::ipv6_is_siit(test._addr));
}
TEST_F(TestNetInAddr, TestinAddrN32)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
llarp::nuint32_t netOrder = test.toN32();
llarp::inAddr test2;
test2.fromN32(netOrder);
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL)
{
ASSERT_TRUE(false);
}
// printf("[%s]\n", str);
ASSERT_TRUE(strcmp("127.0.0.1", str) == 0);
}
TEST_F(TestNetInAddr, TestinAddrH32)
{
llarp::inAddr test;
test.from_char_array("127.0.0.1");
llarp::huint32_t netOrder = test.toH32();
llarp::inAddr test2;
test2.fromH32(netOrder);
char str[INET6_ADDRSTRLEN];
if(inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL)
{
ASSERT_TRUE(false);
}
// printf("[%s]\n", str);
ASSERT_TRUE(strcmp("127.0.0.1", str) == 0);
}
Loading…
Cancel
Save