lokinet/llarp/net/exit_info.cpp

125 lines
2.9 KiB
C++
Raw Normal View History

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