lokinet/llarp/net/exit_info.cpp

127 lines
3.0 KiB
C++
Raw Normal View History

#ifndef _WIN32
2018-04-05 14:43:16 +00: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-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-08-31 12:46:54 +00:00
bool
ExitInfo::BEncode(llarp_buffer_t* buf) const
{
2020-05-15 12:38:04 +00:00
SockAddr exitaddr = ipAddress.createSockAddr();
const sockaddr_in6* exitaddr6 = exitaddr;
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};
if (!bencode_start_dict(buf))
return false;
2020-05-15 12:38:04 +00: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 12:38:04 +00: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 12:46:54 +00:00
return false;
2018-08-31 12:46:54 +00:00
return bencode_end(buf);
}
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;
if (!bencode_read_string(buf, &strbuf))
2019-01-17 15:11:17 +00:00
return false;
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;
if (!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf))
2019-01-17 15:11:17 +00:00
return false;
if (!BEncodeMaybeReadDictInt("v", version, read, k, buf))
2019-01-17 15:11:17 +00:00
return false;
if (k == "a")
{
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;
}
if (k == "b")
{
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;
}
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-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
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("/");
#if defined(ANDROID)
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());
*/
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