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