lokinet/llarp/exit_route.cpp

59 lines
1.4 KiB
C++
Raw Normal View History

#ifndef _WIN32
2018-04-05 14:43:16 +00:00
#include <arpa/inet.h>
#endif
#include <bencode.h>
#include <exit_route.h>
2018-04-05 14:23:14 +00:00
#include <llarp/string.h>
2018-04-04 16:10:27 +00:00
bool
llarp_xr_bencode(struct llarp_xr* xr, llarp_buffer_t* buff)
{
2018-04-05 14:43:16 +00:00
char addr_buff[128] = {0};
const char* addr;
2018-04-05 14:23:14 +00:00
if(!bencode_start_dict(buff))
return false;
2018-04-04 16:10:27 +00:00
/** gateway */
addr = inet_ntop(AF_INET6, &xr->gateway, addr_buff, sizeof(addr_buff));
if(!addr)
return false;
if(!bencode_write_bytestring(buff, "a", 1))
return false;
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
2018-04-05 14:43:16 +00:00
return false;
2018-04-04 16:10:27 +00:00
/** netmask */
addr = inet_ntop(AF_INET6, &xr->netmask, addr_buff, sizeof(addr_buff));
if(!addr)
return false;
if(!bencode_write_bytestring(buff, "b", 1))
return false;
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
2018-04-05 14:43:16 +00:00
return false;
/** source */
2018-04-04 16:10:27 +00:00
addr = inet_ntop(AF_INET6, &xr->source, addr_buff, sizeof(addr_buff));
if(!addr)
return false;
if(!bencode_write_bytestring(buff, "c", 1))
return false;
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
2018-04-05 14:43:16 +00:00
return false;
2018-04-04 16:10:27 +00:00
/** lifetime */
if(!bencode_write_bytestring(buff, "l", 1))
return false;
if(!bencode_write_uint64(buff, xr->lifetime))
return false;
2018-04-04 16:10:27 +00:00
/** version */
if(!bencode_write_version_entry(buff))
return false;
2018-04-04 16:10:27 +00:00
/* end */
return bencode_end(buff);
}