mirror of https://github.com/oxen-io/lokinet
Merge branch 'dev' into private-keys-backup-support
commit
93b8832026
@ -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
|
@ -0,0 +1,35 @@
|
||||
#include <util/lokinet_init.h>
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#include <winuser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
Lokinet_INIT(void)
|
||||
{
|
||||
static const char *(CDECL * pwine_get_version)(void);
|
||||
HMODULE hntdll = GetModuleHandle("ntdll.dll");
|
||||
if(hntdll)
|
||||
{
|
||||
pwine_get_version = (void *)GetProcAddress(hntdll, "wine_get_version");
|
||||
if(pwine_get_version)
|
||||
{
|
||||
static const char *text =
|
||||
"dont run lokinet in wine like wtf man we support linux and pretty "
|
||||
"much every flavour of BSD, and even some flavours of unix system "
|
||||
"5.x.\nThis Program Will now crash lmao.";
|
||||
static const char *title = "srsly fam wtf";
|
||||
MessageBoxA(NULL, text, title, MB_ICONHAND);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int
|
||||
Lokinet_INIT(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#ifndef LLARP_UTIL_LOKINET_INIT_H
|
||||
#define LLARP_UTIL_LOKINET_INIT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef Lokinet_INIT
|
||||
#if defined(_WIN32)
|
||||
#define Lokinet_INIT \
|
||||
DieInCaseSomehowThisGetsRunInWineButLikeWTFThatShouldNotHappenButJustInCaseHandleItWithAPopupOrSomeShit
|
||||
#else
|
||||
#define Lokinet_INIT _lokinet_non_shit_platform_INIT
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int
|
||||
Lokinet_INIT(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
@ -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…
Reference in New Issue