1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/exit_route.cpp

58 lines
1.4 KiB
C++
Raw Normal View History

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