lokinet/llarp/exit_route.c

43 lines
1.3 KiB
C
Raw Normal View History

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