2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _WIN32
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <net/exit_info.hpp>
|
2020-05-06 20:38:44 +00:00
|
|
|
#include <net/net.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.h>
|
2019-02-07 00:22:54 +00:00
|
|
|
#include <util/bits.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
#include <list>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <cstring>
|
2018-05-10 23:32:46 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
namespace llarp
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
|
|
|
ExitInfo::BEncode(llarp_buffer_t* buf) const
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2020-05-15 12:38:04 +00:00
|
|
|
SockAddr exitaddr = ipAddress.createSockAddr();
|
|
|
|
const sockaddr_in6* exitaddr6 = exitaddr;
|
2020-05-11 15:11:44 +00:00
|
|
|
|
2020-05-15 12:38:04 +00:00
|
|
|
SockAddr netmaskaddr = netmask.createSockAddr();
|
|
|
|
const sockaddr_in6* netmaskaddr6 = netmaskaddr;
|
2020-05-06 20:38:44 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
char tmp[128] = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-15 12:38:04 +00:00
|
|
|
if (!inet_ntop(AF_INET6, &exitaddr6->sin6_addr, tmp, sizeof(tmp)))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictString("a", std::string(tmp), buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-15 12:38:04 +00:00
|
|
|
if (!inet_ntop(AF_INET6, &netmaskaddr6->sin6_addr, tmp, sizeof(tmp)))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictString("b", std::string(tmp), buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("k", pubkey, buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("v", version, buf))
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2019-01-17 15:11:17 +00:00
|
|
|
static bool
|
|
|
|
bdecode_ip_string(llarp_buffer_t* buf, in6_addr& ip)
|
|
|
|
{
|
|
|
|
char tmp[128] = {0};
|
|
|
|
llarp_buffer_t strbuf;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2019-01-17 15:11:17 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz >= sizeof(tmp))
|
2019-01-17 15:11:17 +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;
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
2019-02-05 00:41:33 +00:00
|
|
|
ExitInfo::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf))
|
2019-01-17 15:11:17 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("v", version, read, k, buf))
|
2019-01-17 15:11:17 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "a")
|
2020-05-11 15:11:44 +00:00
|
|
|
{
|
|
|
|
in6_addr tmp;
|
2020-05-11 16:46:53 +00:00
|
|
|
if (not bdecode_ip_string(buf, tmp))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SockAddr addr(tmp);
|
|
|
|
ipAddress = IpAddress(addr);
|
2020-05-11 17:42:38 +00:00
|
|
|
return true;
|
2020-05-11 15:11:44 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "b")
|
2020-05-11 15:11:44 +00:00
|
|
|
{
|
2020-05-15 12:38:04 +00:00
|
|
|
in6_addr tmp;
|
|
|
|
if (not bdecode_ip_string(buf, tmp))
|
|
|
|
return false;
|
|
|
|
SockAddr addr(tmp);
|
|
|
|
netmask = IpAddress(addr);
|
|
|
|
return true;
|
2020-05-11 15:11:44 +00:00
|
|
|
}
|
2018-08-31 12:46:54 +00:00
|
|
|
return read;
|
|
|
|
}
|
2018-05-30 20:56:47 +00:00
|
|
|
|
2019-02-07 00:22:54 +00:00
|
|
|
std::ostream&
|
2020-05-17 17:44:00 +00:00
|
|
|
ExitInfo::print(
|
|
|
|
std::ostream& stream, [[maybe_unused]] int level, [[maybe_unused]] int spaces) const
|
2019-02-07 00:22:54 +00:00
|
|
|
{
|
2020-05-11 15:11:44 +00:00
|
|
|
/*
|
2020-05-06 20:38:44 +00:00
|
|
|
// TODO: derive these from ipAdress
|
|
|
|
throw std::runtime_error("FIXME: need in6_addr and netmask from IpAddress");
|
|
|
|
in6_addr address;
|
|
|
|
in6_addr netmask;
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
|
|
|
|
std::ostringstream ss;
|
2019-02-07 00:22:54 +00:00
|
|
|
char tmp[128] = {0};
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (inet_ntop(AF_INET6, (void*)&address, tmp, sizeof(tmp)))
|
2019-02-24 23:46:37 +00:00
|
|
|
ss << tmp;
|
2019-02-07 00:22:54 +00:00
|
|
|
else
|
2019-02-24 23:46:37 +00:00
|
|
|
return stream;
|
|
|
|
ss << std::string("/");
|
2020-05-17 18:00:15 +00:00
|
|
|
#if defined(ANDROID)
|
2020-04-07 18:38:56 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "%zu", llarp::bits::count_array_bits(netmask.s6_addr));
|
2019-02-24 23:46:37 +00:00
|
|
|
ss << tmp;
|
2019-02-07 00:22:54 +00:00
|
|
|
#else
|
2019-02-24 23:46:37 +00:00
|
|
|
ss << std::to_string(llarp::bits::count_array_bits(netmask.s6_addr));
|
2019-02-07 00:22:54 +00:00
|
|
|
#endif
|
2019-02-24 23:46:37 +00:00
|
|
|
printer.printValue(ss.str());
|
2020-05-11 15:11:44 +00:00
|
|
|
*/
|
|
|
|
stream << ipAddress.toString();
|
2019-02-24 23:46:37 +00:00
|
|
|
return stream;
|
2019-02-07 00:22:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
} // namespace llarp
|