2019-01-14 21:46:07 +00:00
|
|
|
#include <net/address_info.hpp>
|
2020-05-08 17:23:21 +00:00
|
|
|
#include <stdexcept>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _WIN32
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.h>
|
|
|
|
#include <util/mem.h>
|
2019-02-24 23:46:37 +00:00
|
|
|
#include <util/printer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstring>
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
namespace llarp
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-09-04 12:41:25 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
operator==(const AddressInfo& lhs, const AddressInfo& rhs)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
|
|
|
// we don't care about rank
|
2020-04-07 18:38:56 +00:00
|
|
|
return lhs.pubkey == rhs.pubkey && lhs.port == rhs.port && lhs.dialect == rhs.dialect
|
|
|
|
&& lhs.ip == rhs.ip;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
operator<(const AddressInfo& lhs, const AddressInfo& rhs)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-12 00:26:37 +00:00
|
|
|
return lhs.rank < rhs.rank || lhs.ip < rhs.ip || lhs.port < rhs.port;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
AddressInfo::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
uint64_t i;
|
|
|
|
char tmp[128] = {0};
|
|
|
|
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
|
|
|
|
// rank
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "c")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_integer(buf, &i))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (i > 65536 || i <= 0)
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
rank = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dialect
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "d")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz > sizeof(tmp))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
memcpy(tmp, strbuf.base, strbuf.sz);
|
|
|
|
tmp[strbuf.sz] = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
dialect = std::string(tmp);
|
2018-08-31 12:46:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryption public key
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "e")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2018-09-09 11:23:21 +00:00
|
|
|
return pubkey.BDecode(buf);
|
2018-08-31 12:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ip address
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "i")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz >= sizeof(tmp))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
memcpy(tmp, strbuf.base, strbuf.sz);
|
|
|
|
tmp[strbuf.sz] = 0;
|
|
|
|
return inet_pton(AF_INET6, tmp, &ip.s6_addr[0]) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// port
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "p")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_integer(buf, &i))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (i > 65536 || i <= 0)
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
port = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// version
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "v")
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_integer(buf, &i))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
return i == LLARP_PROTO_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bad key
|
|
|
|
return false;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
AddressInfo::BEncode(llarp_buffer_t* buff) const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
char ipbuff[128] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
const char* ipstr;
|
|
|
|
if (!bencode_start_dict(buff))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-31 12:46:54 +00:00
|
|
|
/* rank */
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, "c", 1))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64(buff, rank))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-31 12:46:54 +00:00
|
|
|
/* dialect */
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, "d", 1))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, dialect.c_str(), dialect.size()))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-31 12:46:54 +00:00
|
|
|
/* encryption key */
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, "e", 1))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, pubkey.data(), PUBKEYSIZE))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
/** ip */
|
2020-04-07 18:38:56 +00:00
|
|
|
ipstr = inet_ntop(AF_INET6, (void*)&ip, ipbuff, sizeof(ipbuff));
|
|
|
|
if (!ipstr)
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, "i", 1))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff))))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
/** port */
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, "p", 1))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64(buff, port))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-05-27 13:42:55 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
/** version */
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64_entry(buff, "v", 1, LLARP_PROTO_VERSION))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
|
|
|
/** end */
|
|
|
|
return bencode_end(buff);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
IpAddress
|
|
|
|
AddressInfo::toIpAddress() const
|
|
|
|
{
|
2020-05-11 16:14:07 +00:00
|
|
|
SockAddr addr(ip);
|
|
|
|
addr.setPort(port);
|
|
|
|
return IpAddress(addr);
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-22 15:01:05 +00:00
|
|
|
AddressInfo::fromSockAddr(const SockAddr& addr)
|
2020-05-08 17:23:21 +00:00
|
|
|
{
|
2020-05-11 16:14:07 +00:00
|
|
|
const sockaddr_in6* addr6 = addr;
|
|
|
|
memcpy(ip.s6_addr, addr6->sin6_addr.s6_addr, sizeof(ip.s6_addr));
|
2021-02-22 15:01:05 +00:00
|
|
|
port = addr.getPort();
|
2020-05-08 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::ostream&
|
|
|
|
AddressInfo::print(std::ostream& stream, int level, int spaces) const
|
2019-02-24 23:46:37 +00:00
|
|
|
{
|
|
|
|
char tmp[128] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
inet_ntop(AF_INET6, (void*)&ip, tmp, sizeof(tmp));
|
2019-02-24 23:46:37 +00:00
|
|
|
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printAttribute("ip", tmp);
|
|
|
|
printer.printAttribute("port", port);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
2019-08-19 22:25:40 +00:00
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
to_json(nlohmann::json& j, const AddressInfo& a)
|
2019-08-19 22:25:40 +00:00
|
|
|
{
|
|
|
|
char tmp[128] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
inet_ntop(AF_INET6, (void*)&a.ip, tmp, sizeof(tmp));
|
2019-08-19 22:25:40 +00:00
|
|
|
|
2021-03-05 17:31:52 +00:00
|
|
|
j = nlohmann::json{
|
|
|
|
{"rank", a.rank},
|
|
|
|
{"dialect", a.dialect},
|
|
|
|
{"pubkey", a.pubkey.ToString()},
|
|
|
|
{"in6_addr", tmp},
|
|
|
|
{"port", a.port}};
|
2019-08-19 22:25:40 +00:00
|
|
|
}
|
2018-08-31 12:46:54 +00:00
|
|
|
} // namespace llarp
|