lokinet/llarp/net.hpp

143 lines
3.0 KiB
C++
Raw Normal View History

2018-05-18 13:17:58 +00:00
#ifndef LLARP_NET_HPP
#define LLARP_NET_HPP
#include <llarp/address_info.h>
2018-05-18 13:17:58 +00:00
#include <llarp/net.h>
#include <string>
2018-05-18 13:17:58 +00:00
bool
operator==(const sockaddr& a, const sockaddr& b);
2018-05-18 13:17:58 +00:00
bool
operator<(const sockaddr_in6& a, const sockaddr_in6& b);
2018-05-18 13:54:15 +00:00
bool
operator<(const in6_addr& a, const in6_addr& b);
2018-05-18 13:54:15 +00:00
namespace llarp
{
struct Addr
{
int af = AF_INET;
in6_addr addr = {};
2018-05-18 13:54:15 +00:00
uint16_t port = 0;
sockaddr _addr = {0, {0}};
~Addr(){};
Addr(){};
Addr(const Addr& other)
{
af = other.af;
2018-05-18 17:10:48 +00:00
port = other.port;
memcpy(addr.s6_addr, other.addr.s6_addr, sizeof(addr.s6_addr));
CopyInto(_addr);
2018-05-18 17:10:48 +00:00
}
Addr(const llarp_ai& other)
{
2018-05-18 17:10:48 +00:00
af = AF_INET6;
memcpy(addr.s6_addr, other.ip.s6_addr, 16);
2018-05-20 17:55:54 +00:00
port = other.port;
CopyInto(_addr);
2018-05-18 17:10:48 +00:00
}
Addr(const sockaddr& other)
{
uint8_t* addrptr = addr.s6_addr;
2018-05-18 13:54:15 +00:00
switch(other.sa_family)
{
case AF_INET:
// SIIT
af = AF_INET;
memcpy(12 + addrptr, &((const sockaddr_in*)(&other))->sin_addr,
sizeof(in_addr));
addrptr[11] = 0xff;
addrptr[10] = 0xff;
port = ntohs(((sockaddr_in*)(&other))->sin_port);
break;
case AF_INET6:
af = AF_INET6;
memcpy(addrptr, &((const sockaddr_in6*)(&other))->sin6_addr,
sizeof(addr.s6_addr));
port = ntohs(((sockaddr_in6*)(&other))->sin6_port);
break;
// TODO : sockaddr_ll
default:
break;
2018-05-18 13:54:15 +00:00
}
CopyInto(_addr);
2018-05-18 17:10:48 +00:00
}
std::string
to_string() const
2018-05-21 14:28:15 +00:00
{
std::string str;
char tmp[128];
socklen_t sz;
const void* ptr = nullptr;
if(af == AF_INET)
{
sz = sizeof(sockaddr_in);
ptr = &addr.s6_addr[12];
}
if(af == AF_INET6)
{
str += "[";
sz = sizeof(sockaddr_in6);
ptr = &addr.s6_addr[0];
}
if(inet_ntop(af, ptr, tmp, sz))
{
str += tmp;
if(af == AF_INET6)
str += "]";
}
return str + ":" + std::to_string(port);
2018-05-21 14:28:15 +00:00
}
operator const sockaddr*() const
2018-05-18 17:10:48 +00:00
{
return &_addr;
2018-05-18 13:54:15 +00:00
}
void
CopyInto(sockaddr& other) const
{
void *dst, *src;
in_port_t* ptr;
size_t slen;
switch(af)
{
case AF_INET:
dst = (void*)&((const sockaddr_in*)&other)->sin_addr;
src = (void*)&addr.s6_addr[12];
ptr = &((sockaddr_in*)&other)->sin_port;
slen = sizeof(in_addr);
break;
case AF_INET6:
dst = (void*)&((const sockaddr_in6*)&other)->sin6_addr;
src = (void*)&addr.s6_addr[0];
ptr = &((sockaddr_in6*)&other)->sin6_port;
slen = sizeof(in6_addr);
break;
default:
return;
}
memcpy(ptr, src, slen);
*ptr = htons(port);
other.sa_family = af;
}
bool
operator<(const Addr& other) const
2018-05-18 13:54:15 +00:00
{
return af < other.af && addr < other.addr && port < other.port;
}
};
}
2018-05-18 13:17:58 +00:00
#endif