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>
|
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-01-10 19:41:51 +00:00
|
|
|
#include <string.h>
|
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
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
char tmp[128] = {0};
|
|
|
|
if(!bencode_start_dict(buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2019-04-08 17:40:51 +00:00
|
|
|
if(!inet_ntop(AF_INET6, address.s6_addr, tmp, sizeof(tmp)))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!BEncodeWriteDictString("a", std::string(tmp), buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2019-04-08 17:40:51 +00:00
|
|
|
if(!inet_ntop(AF_INET6, netmask.s6_addr, tmp, sizeof(tmp)))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!BEncodeWriteDictString("b", std::string(tmp), buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!BEncodeWriteDictEntry("k", pubkey, buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
|
|
|
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;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(strbuf.sz >= sizeof(tmp))
|
|
|
|
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;
|
2019-01-17 15:11:17 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
|
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(k == "a")
|
2019-01-17 15:11:17 +00:00
|
|
|
return bdecode_ip_string(buf, address);
|
2019-02-17 12:13:34 +00:00
|
|
|
if(k == "b")
|
2019-01-17 15:11:17 +00:00
|
|
|
return bdecode_ip_string(buf, netmask);
|
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&
|
2019-02-24 23:46:37 +00:00
|
|
|
ExitInfo::print(std::ostream& stream, int level, int spaces) const
|
2019-02-07 00:22:54 +00:00
|
|
|
{
|
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)))
|
|
|
|
ss << tmp;
|
2019-02-07 00:22:54 +00:00
|
|
|
else
|
2019-02-24 23:46:37 +00:00
|
|
|
return stream;
|
|
|
|
ss << std::string("/");
|
2019-02-07 00:22:54 +00:00
|
|
|
#if defined(ANDROID) || defined(RPI)
|
|
|
|
snprintf(tmp, sizeof(tmp), "%zu",
|
2019-02-24 23:46:37 +00:00
|
|
|
llarp::bits::count_array_bits(netmask.s6_addr));
|
|
|
|
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());
|
|
|
|
return stream;
|
2019-02-07 00:22:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
} // namespace llarp
|