clang format

pull/1/head
Jeff Becker 6 years ago
parent 84ab8a792a
commit 09be72b5b6
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -19,7 +19,6 @@ struct llarp_ai {
uint16_t port;
};
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff);
bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t buff);

@ -1,63 +1,54 @@
#ifndef LLARP_BENCODE_H
#define LLARP_BENCODE_H
#include <llarp/buffer.h>
#include <llarp/common.h>
#include <llarp/proto.h>
#include <stdbool.h>
#include <stdint.h>
#include <llarp/buffer.h>
#include <llarp/proto.h>
#ifdef __cplusplus
extern "C" {
#endif
bool INLINE bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
{
bool INLINE bencode_write_bytestring(llarp_buffer_t* buff, const void* data,
size_t sz) {
if (!llarp_buffer_writef(buff, "%ld:", sz)) return false;
return llarp_buffer_write(buff, data, sz);
}
bool INLINE bencode_write_int(llarp_buffer_t * buff, int i)
{
bool INLINE bencode_write_int(llarp_buffer_t* buff, int i) {
return llarp_buffer_writef(buff, "i%de", i);
}
bool INLINE bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
{
bool INLINE bencode_write_uint16(llarp_buffer_t* buff, uint16_t i) {
return llarp_buffer_writef(buff, "i%de", i);
}
bool INLINE bencode_write_int64(llarp_buffer_t * buff, int64_t i)
{
bool INLINE bencode_write_int64(llarp_buffer_t* buff, int64_t i) {
return llarp_buffer_writef(buff, "i%lde", i);
}
bool INLINE bencode_write_uint64(llarp_buffer_t * buff, uint64_t i)
{
bool INLINE bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) {
return llarp_buffer_writef(buff, "i%lde", i);
}
bool INLINE bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
{
bool INLINE bencode_write_sizeint(llarp_buffer_t* buff, size_t i) {
return llarp_buffer_writef(buff, "i%lde", i);
}
bool INLINE bencode_start_list(llarp_buffer_t * buff)
{
bool INLINE bencode_start_list(llarp_buffer_t* buff) {
return llarp_buffer_write(buff, "l", 1);
}
bool INLINE bencode_start_dict(llarp_buffer_t * buff)
{
bool INLINE bencode_start_dict(llarp_buffer_t* buff) {
return llarp_buffer_write(buff, "d", 1);
}
bool INLINE bencode_end(llarp_buffer_t * buff)
{
bool INLINE bencode_end(llarp_buffer_t* buff) {
return llarp_buffer_write(buff, "e", 1);
}
bool INLINE bencode_write_version_entry(llarp_buffer_t * buff)
{
bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) {
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
}

@ -14,18 +14,18 @@ typedef struct llarp_buffer_t {
char *cur;
} llarp_buffer_t;
size_t INLINE llarp_buffer_size_left(llarp_buffer_t *buff)
{
size_t INLINE llarp_buffer_size_left(llarp_buffer_t *buff) {
size_t diff = buff->cur - buff->base;
if ( diff > buff->sz) return 0;
else return buff->sz - diff;
if (diff > buff->sz)
return 0;
else
return buff->sz - diff;
}
bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz)
{
bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data,
size_t sz) {
size_t left = llarp_buffer_size_left(buff);
if (left >= sz)
{
if (left >= sz) {
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
@ -35,7 +35,6 @@ bool INLINE llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz
bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
#ifdef __cplusplus
}
#endif

@ -5,7 +5,7 @@
#else
#define INLINE inline
#endif
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#endif

@ -1,7 +1,7 @@
#ifndef LLARP_CRYPTO_H_
#define LLARP_CRYPTO_H_
#include <llarp/common.h>
#include <llarp/buffer.h>
#include <llarp/common.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus

@ -22,9 +22,7 @@ struct llarp_xi_list;
struct llarp_xi_list *llarp_xi_list_new();
void llarp_xi_list_free(struct llarp_xi_list *l);
struct llarp_xi_list_iter
{
struct llarp_xi_list_iter {
void *user;
struct llarp_xi_list *list;
bool (*visit)(struct llarp_xi_list_iter *, struct llarp_xi *);

@ -7,8 +7,7 @@
extern "C" {
#endif
struct llarp_xr
{
struct llarp_xr {
struct in6_addr gateway;
struct in6_addr netmask;
struct in6_addr source;
@ -18,7 +17,6 @@ struct llarp_xr
bool llarp_xr_bencode(struct llarp_xr* xr, llarp_buffer_t* buff);
bool llarp_xr_bdecode(struct llarp_xr* xr, llarp_buffer_t* buff);
#ifdef __cplusplus
}
#endif

@ -9,14 +9,12 @@
extern "C" {
#endif
struct iwp_configure_args
{
struct iwp_configure_args {
struct llarp_crypto* crypto;
};
bool iwp_link_init(struct llarp_link * link, struct iwp_configure_args args, struct llarp_msg_muxer * muxer);
bool iwp_link_init(struct llarp_link* link, struct iwp_configure_args args,
struct llarp_msg_muxer* muxer);
#ifdef __cplusplus
}

@ -1,10 +1,10 @@
#ifndef LLARP_LINK_H_
#define LLARP_LINK_H_
#include <llarp/crypto.h>
#include <llarp/ev.h>
#include <llarp/mem.h>
#include <llarp/msg_handler.h>
#include <llarp/obmd.h>
#include <llarp/ev.h>
#include <stdbool.h>
#include <stdint.h>
@ -18,9 +18,9 @@ extern "C" {
*/
struct llarp_link;
/**
* wire layer transport session for point to point communication between us and another
* wire layer transport session for point to point communication between us and
* another
*/
struct llarp_link_session;
@ -47,18 +47,19 @@ struct llarp_link_session_iter {
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
};
struct llarp_link_ev_listener
{
struct llarp_link_ev_listener {
void *user;
void (*established)(struct llarp_ev_listener *, struct llarp_link_session *, bool);
void (*timeout)(struct llarp_ev_listener *, struct llarp_link_session *, bool);
void (*established)(struct llarp_ev_listener *, struct llarp_link_session *,
bool);
void (*timeout)(struct llarp_ev_listener *, struct llarp_link_session *,
bool);
void (*tx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*rx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*error)(struct llarp_ev_listener *, struct llarp_link_session *, const char *);
void (*error)(struct llarp_ev_listener *, struct llarp_link_session *,
const char *);
};
struct llarp_link
{
struct llarp_link {
void *impl;
const char *(*name)(struct llarp_link *);
int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener);
@ -68,17 +69,16 @@ struct llarp_link
bool (*stop_link)(struct llarp_link *);
bool (*put_ai)(struct llarp_link *, struct llarp_ai *);
void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter);
void (*try_establish)(struct llarp_link *,
struct llarp_link_establish_job,
void (*try_establish)(struct llarp_link *, struct llarp_link_establish_job,
struct llarp_link_session_listener);
void (*free)(struct llarp_link *);
};
struct llarp_link_session
{
struct llarp_link_session {
void *impl;
struct llarp_rc *(*remote_rc)(struct llarp_link_session *);
/** send an entire message, splits up into smaller pieces and does encryption */
/** send an entire message, splits up into smaller pieces and does encryption
*/
ssize_t (*sendto)(struct llarp_link_session *, llarp_buffer_t);
/** return true if this session is timed out */
bool (*timeout)(struct llarp_link_session *);
@ -86,7 +86,6 @@ struct llarp_link_session
void (*close)(struct llarp_link_session *);
};
#ifdef __cplusplus
}
#endif

@ -1,9 +1,9 @@
#ifndef LLARP_ROUTER_IDENT_H
#define LLARP_ROUTER_IDENT_H
#include <llarp/address_info.h>
#include <llarp/router_contact.h>
#include <llarp/crypto.h>
#include <llarp/exit_info.h>
#include <llarp/router_contact.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
@ -12,8 +12,7 @@ extern "C" {
/**
* rotuer identity private info
*/
struct llarp_router_ident
{
struct llarp_router_ident {
struct llarp_crypto *crypto;
llarp_seckey_t signkey;
uint64_t updated;
@ -22,15 +21,19 @@ struct llarp_router_ident
struct llarp_xi_list *exits;
};
void llarp_router_ident_new(struct llarp_router_ident ** ri, struct llarp_crypto * crypto);
void llarp_router_ident_new(struct llarp_router_ident **ri,
struct llarp_crypto *crypto);
void llarp_router_ident_append_ai(struct llarp_router_ident * ri, struct llarp_ai * ai);
void llarp_router_ident_append_ai(struct llarp_router_ident *ri,
struct llarp_ai *ai);
bool llarp_router_ident_bdecode(struct llarp_router_ident *ri, llarp_buffer_t *buf);
bool llarp_router_ident_bencode(struct llarp_router_ident *ri, llarp_buffer_t *buf);
bool llarp_router_ident_bdecode(struct llarp_router_ident *ri,
llarp_buffer_t *buf);
bool llarp_router_ident_bencode(struct llarp_router_ident *ri,
llarp_buffer_t *buf);
void llarp_router_ident_free(struct llarp_router_ident **ri);
bool llarp_router_ident_generate_rc(struct llarp_router_ident * ri, struct llarp_rc ** rc);
bool llarp_router_ident_generate_rc(struct llarp_router_ident *ri,
struct llarp_rc **rc);
#ifdef __cplusplus
}

@ -5,11 +5,9 @@
extern "C" {
#endif
size_t INLINE strnlen(const char * str, size_t sz)
{
size_t INLINE strnlen(const char* str, size_t sz) {
size_t slen = 0;
while(sz-- && str[slen])
slen ++;
while (sz-- && str[slen]) slen++;
return slen;
}

@ -1,8 +1,8 @@
#include <arpa/inet.h>
#include <llarp/address_info.h>
#include <llarp/bencode.h>
#include <llarp/mem.h>
#include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
char ipbuff[128] = {0};
@ -13,15 +13,19 @@ bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
if (!bencode_write_uint16(buff, ai->rank)) return false;
/* dialect */
if (!bencode_write_bytestring(buff, "d", 1)) return false;
if(!bencode_write_bytestring(buff, ai->dialect, strnlen(ai->dialect, sizeof(ai->dialect)))) return false;
if (!bencode_write_bytestring(buff, ai->dialect,
strnlen(ai->dialect, sizeof(ai->dialect))))
return false;
/* encryption key */
if (!bencode_write_bytestring(buff, "e", 1)) return false;
if(!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t))) return false;
if (!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t)))
return false;
/** ip */
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
if (!ipstr) return false;
if (!bencode_write_bytestring(buff, "i", 1)) return false;
if(!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff)))) return false;
if (!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff))))
return false;
/** port */
if (!bencode_write_bytestring(buff, "p", 1)) return false;
if (!bencode_write_uint16(buff, ai->port)) return false;
@ -32,20 +36,16 @@ bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
return bencode_end(buff);
}
struct llarp_ai_list
{
struct llarp_ai_list {
struct llarp_ai_list *next;
struct llarp_ai data;
};
struct llarp_ai_list * llarp_ai_list_new()
{
struct llarp_ai_list *llarp_ai_list_new() {
return llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8);
}
void llarp_ai_list_free(struct llarp_ai_list * l)
{
void llarp_ai_list_free(struct llarp_ai_list *l) {
struct llarp_ai_list *cur = l;
struct llarp_ai_list *tmp;
do {

@ -3,8 +3,7 @@
#include <stdio.h>
extern "C" {
bool llarp_buffer_writef(llarp_buffer_t * buff, const char * fmt, ...)
{
bool llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) {
int written;
size_t sz = llarp_buffer_size_left(buff);
va_list args;

@ -36,12 +36,10 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func,
llarp_dh_complete_hook result, void *user) {
struct llarp_dh_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32);
struct llarp_thread_job job = {
.caller = dh->caller,
struct llarp_thread_job job = {.caller = dh->caller,
.data = impl,
.user = impl,
.work = &llarp_crypto_dh_work
};
.work = &llarp_crypto_dh_work};
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
impl->ourkey = dh->ourkey;

@ -1,8 +1,7 @@
#include <llarp/exit_info.h>
#include <arpa/inet.h>
#include <llarp/bencode.h>
#include <llarp/exit_info.h>
#include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
char addr_buff[128] = {0};
@ -13,21 +12,23 @@ bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
addr = inet_ntop(AF_INET6, &xi->address, 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)))) return false;
if (!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
return false;
/** netmask */
addr = inet_ntop(AF_INET6, &xi->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)))) return false;
if (!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
return false;
/** public key */
if (!bencode_write_bytestring(buff, "k", 1)) return false;
if(!bencode_write_bytestring(buff, xi->pubkey, sizeof(llarp_pubkey_t))) return false;
if (!bencode_write_bytestring(buff, xi->pubkey, sizeof(llarp_pubkey_t)))
return false;
/** version */
if (!bencode_write_version_entry(buff)) return false;
return bencode_end(buff);
}

@ -1,11 +1,9 @@
#include <llarp/exit_route.h>
#include <arpa/inet.h>
#include <llarp/bencode.h>
#include <llarp/exit_route.h>
#include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_xr_bencode(struct llarp_xr * xr, llarp_buffer_t * buff)
{
bool llarp_xr_bencode(struct llarp_xr* xr, llarp_buffer_t* buff) {
char addr_buff[128] = {0};
const char* addr;
@ -15,19 +13,22 @@ bool llarp_xr_bencode(struct llarp_xr * xr, llarp_buffer_t * buff)
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)))) return false;
if (!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
return false;
/** 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)))) return false;
if (!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
return false;
/** source */
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)))) return false;
if (!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
return false;
/** lifetime */
if (!bencode_write_bytestring(buff, "l", 1)) return false;

@ -145,7 +145,8 @@ inline void Parser::parse(Level &l) {
size_t n = line_.find('=');
if (n == std::string::npos) err("no '=' found");
auto p = std::make_pair(trim(line_.substr(0, n)),
auto p =
std::make_pair(trim(line_.substr(0, n)),
trim(line_.substr(n + 1, line_.length() - n - 1)));
l.values.push_back(p);
}

@ -3,13 +3,10 @@
#include <list>
#include <string>
namespace iwp
{
namespace iwp {
struct link_impl
{
link_impl(llarp_seckey_t k, llarp_msg_muxer *m) : muxer(m)
{
struct link_impl {
link_impl(llarp_seckey_t k, llarp_msg_muxer *m) : muxer(m) {
memcpy(seckey, k, sizeof(seckey));
}
@ -21,67 +18,57 @@ namespace iwp
std::string linkname;
bool configure(const char * ifname, int af, uint16_t port)
{
bool configure(const char *ifname, int af, uint16_t port) {
// todo: implement
linkname = std::string(ifname) + std::string("+") + std::to_string(port);
return false;
}
const char * name()
{
return linkname.c_str();
}
const char *name() { return linkname.c_str(); }
bool start(llarp_ev_loop * loop)
{
bool start(llarp_ev_loop *loop) {
// todo: implement
return false;
}
bool stop()
{
bool stop() {
// todo: implement
return false;
}
};
static bool configure(struct llarp_link * l, const char * ifname, int af, uint16_t port)
{
static bool configure(struct llarp_link *l, const char *ifname, int af,
uint16_t port) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->configure(ifname, af, port);
}
static bool start_link(struct llarp_link * l, struct llarp_ev_loop * loop)
{
static bool start_link(struct llarp_link *l, struct llarp_ev_loop *loop) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->start(loop);
}
static bool stop_link(struct llarp_link * l)
{
static bool stop_link(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->stop();
}
static void free_link(struct llarp_link * l)
{
static void free_link(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
delete link;
}
static const char * link_name(struct llarp_link * l)
{
static const char *link_name(struct llarp_link *l) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->name();
}
}
} // namespace iwp
extern "C" {
bool iwp_link_init(struct llarp_link * link, struct iwp_configure_args args, struct llarp_msg_muxer * muxer)
{
bool iwp_link_init(struct llarp_link *link, struct iwp_configure_args args,
struct llarp_msg_muxer *muxer) {
llarp_seckey_t seckey;
args.crypto->keygen(&seckey);
link->impl = new iwp::link_impl(seckey, muxer);
@ -93,5 +80,4 @@ bool iwp_link_init(struct llarp_link * link, struct iwp_configure_args args, str
link->free = &iwp::free_link;
return true;
}
}

@ -5,8 +5,7 @@ namespace llarp {
void *std_malloc(size_t sz, size_t align) {
(void)align;
void *ptr = malloc(sz);
if (ptr)
{
if (ptr) {
std::memset(ptr, 0, sz);
return ptr;
}

@ -1,9 +1,9 @@
#include "router.hpp"
#include <llarp/ibfq.h>
#include <llarp/link.h>
#include <llarp/router.h>
#include <llarp/iwp.h>
#include <llarp/link.h>
#include <llarp/proto.h>
#include <llarp/router.h>
#include "str.hpp"
namespace llarp {
@ -27,10 +27,7 @@ void llarp_router::AddLink(struct llarp_link *link) {
ready = true;
}
bool llarp_router::Ready()
{
return ready;
}
bool llarp_router::Ready() { return ready; }
void llarp_router::ForEachLink(std::function<void(llarp_link *)> visitor) {
llarp::router_links *cur = &links;
@ -40,7 +37,9 @@ void llarp_router::ForEachLink(std::function<void(llarp_link *)> visitor) {
} while (cur);
}
void llarp_router::Close() { ForEachLink([](llarp_link * l) { l->stop_link(l); }); }
void llarp_router::Close() {
ForEachLink([](llarp_link *l) { l->stop_link(l); });
}
extern "C" {
struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
@ -62,8 +61,7 @@ bool llarp_configure_router(struct llarp_router *router,
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
router->ForEachLink([loop](llarp_link *link) {
int result = link->start_link(link, loop);
if(result == -1)
printf("link %s failed to start\n", link->name(link));
if (result == -1) printf("link %s failed to start\n", link->name(link));
});
}
@ -86,32 +84,24 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
if (StrEq(section, "links")) {
if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>();
iwp_configure_args args = {
.crypto = &self->crypto
};
iwp_configure_args args = {.crypto = &self->crypto};
iwp_link_init(link, args, &self->muxer);
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO))
{
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO)) {
printf("ethernet link configured on %s\n", key);
self->AddLink(link);
}
else {
} else {
link->free(link);
printf("failed to configure ethernet link for %s\n", key);
}
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>();
uint16_t port = std::atoi(val);
iwp_configure_args args = {
.crypto = &self->crypto
};
iwp_configure_args args = {.crypto = &self->crypto};
iwp_link_init(link, args, &self->muxer);
if (link->configure(link, key, AF_INET6, port))
{
if (link->configure(link, key, AF_INET6, port)) {
printf("inet link configured on %s port %d\n", key, port);
self->AddLink(link);
}
else {
} else {
link->free(link);
printf("failed to configure inet link for %s port %d\n", key, port);
}

@ -1,74 +1,59 @@
#include "router_contact.hpp"
#include "address_info.hpp"
#include "exit_info.hpp"
#include <llarp/bencode.h>
#include <llarp/version.h>
#include "address_info.hpp"
#include "exit_info.hpp"
namespace llarp {
static bool bencode_rc_ai_write(struct llarp_ai_list_iter * i, struct llarp_ai * ai)
{
static bool bencode_rc_ai_write(struct llarp_ai_list_iter *i,
struct llarp_ai *ai) {
llarp_buffer_t *buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_ai_bencode(ai, buff);
}
static bool bencode_rc_xi_write(struct llarp_xi_list_iter * i, struct llarp_xi * xi)
{
static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i,
struct llarp_xi *xi) {
llarp_buffer_t *buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_xi_bencode(xi, buff);
}
} // namespace llarp
extern "C" {
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
/* write dict begin */
if(!bencode_start_dict(buff))
return false;
if(rc->addrs)
{
if (!bencode_start_dict(buff)) return false;
if (rc->addrs) {
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "a", 1))
return false;
if(!bencode_start_list(buff))
return false;
if (!bencode_write_bytestring(buff, "a", 1)) return false;
if (!bencode_start_list(buff)) return false;
struct llarp_ai_list_iter ai_itr = {
.user = buff,
.list = nullptr,
.visit = &llarp::bencode_rc_ai_write
};
.user = buff, .list = nullptr, .visit = &llarp::bencode_rc_ai_write};
llarp_ai_list_iterate(rc->addrs, &ai_itr);
if(!bencode_end(buff))
return false;
if (!bencode_end(buff)) return false;
}
/* write pubkey */
if (!bencode_write_bytestring(buff, "k", 1)) return false;
if(!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t))) return false;
if (!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t)))
return false;
/* write version */
if (!bencode_write_version_entry(buff)) return false;
if(rc->exits)
{
if (rc->exits) {
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "x", 1))
return false;
if(!bencode_start_list(buff))
return false;
if (!bencode_write_bytestring(buff, "x", 1)) return false;
if (!bencode_start_list(buff)) return false;
struct llarp_xi_list_iter xi_itr = {
.user = buff,
.list = nullptr,
.visit = &llarp::bencode_rc_xi_write
};
.user = buff, .list = nullptr, .visit = &llarp::bencode_rc_xi_write};
llarp_xi_list_iterate(rc->exits, &xi_itr);
if(!bencode_end(buff))
return false;
if (!bencode_end(buff)) return false;
}
/* write signature */
if (!bencode_write_bytestring(buff, "z", 1)) return false;
if(!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t))) return false;
if (!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t)))
return false;
return bencode_end(buff);
}
}

Loading…
Cancel
Save