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

@ -62,10 +62,10 @@ int main(int argc, char *argv[]) {
iter.user = &llarp; iter.user = &llarp;
iter.visit = iter_main_config; iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter); llarp_config_iter(llarp.config, &iter);
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2); if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp); llarp.router = llarp_init_router(llarp.tp);
if (llarp_configure_router(llarp.router, llarp.config)) { if (llarp_configure_router(llarp.router, llarp.config)) {
printf("Running\n"); printf("Running\n");
llarp_run_router(llarp.router, llarp.mainloop); llarp_run_router(llarp.router, llarp.mainloop);

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

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

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

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

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

@ -19,19 +19,17 @@ bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buf);
struct llarp_xi_list; struct llarp_xi_list;
struct llarp_xi_list * llarp_xi_list_new(); struct llarp_xi_list *llarp_xi_list_new();
void llarp_xi_list_free(struct llarp_xi_list * l); void llarp_xi_list_free(struct llarp_xi_list *l);
struct llarp_xi_list_iter {
void *user;
struct llarp_xi_list *list;
bool (*visit)(struct llarp_xi_list_iter *, struct llarp_xi *);
};
struct llarp_xi_list_iter void llarp_xi_list_iterate(struct llarp_xi_list *l,
{ struct llarp_xi_list_iter *iter);
void * user;
struct llarp_xi_list * list;
bool (*visit)(struct llarp_xi_list_iter *, struct llarp_xi *);
};
void llarp_xi_list_iterate(struct llarp_xi_list *l,
struct llarp_xi_list_iter *iter);
#ifdef __cplusplus #ifdef __cplusplus
} }

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

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

@ -1,10 +1,10 @@
#ifndef LLARP_LINK_H_ #ifndef LLARP_LINK_H_
#define LLARP_LINK_H_ #define LLARP_LINK_H_
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <llarp/ev.h>
#include <llarp/mem.h> #include <llarp/mem.h>
#include <llarp/msg_handler.h> #include <llarp/msg_handler.h>
#include <llarp/obmd.h> #include <llarp/obmd.h>
#include <llarp/ev.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -12,15 +12,15 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* wire layer transport interface * wire layer transport interface
*/ */
struct llarp_link; 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; struct llarp_link_session;
@ -47,20 +47,21 @@ struct llarp_link_session_iter {
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *); bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
}; };
struct llarp_link_ev_listener struct llarp_link_ev_listener {
{ void *user;
void * user; void (*established)(struct llarp_ev_listener *, struct llarp_link_session *,
void (*established)(struct llarp_ev_listener *, struct llarp_link_session *, bool); bool);
void (*timeout)(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 (*tx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*rx)(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;
void * impl; const char *(*name)(struct llarp_link *);
const char * (*name)(struct llarp_link *);
int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener); int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener);
void (*deregister_listener)(struct llarp_link *, int); void (*deregister_listener)(struct llarp_link *, int);
bool (*configure)(struct llarp_link *, const char *, int, uint16_t); bool (*configure)(struct llarp_link *, const char *, int, uint16_t);
@ -68,17 +69,16 @@ struct llarp_link
bool (*stop_link)(struct llarp_link *); bool (*stop_link)(struct llarp_link *);
bool (*put_ai)(struct llarp_link *, struct llarp_ai *); bool (*put_ai)(struct llarp_link *, struct llarp_ai *);
void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter); void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter);
void (*try_establish)(struct llarp_link *, void (*try_establish)(struct llarp_link *, struct llarp_link_establish_job,
struct llarp_link_establish_job,
struct llarp_link_session_listener); struct llarp_link_session_listener);
void (*free)(struct llarp_link *); void (*free)(struct llarp_link *);
}; };
struct llarp_link_session struct llarp_link_session {
{ void *impl;
void * impl; struct llarp_rc *(*remote_rc)(struct llarp_link_session *);
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); ssize_t (*sendto)(struct llarp_link_session *, llarp_buffer_t);
/** return true if this session is timed out */ /** return true if this session is timed out */
bool (*timeout)(struct llarp_link_session *); bool (*timeout)(struct llarp_link_session *);
@ -86,7 +86,6 @@ struct llarp_link_session
void (*close)(struct llarp_link_session *); void (*close)(struct llarp_link_session *);
}; };
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -16,7 +16,7 @@ struct llarp_router *llarp_init_router(struct llarp_threadpool *tp);
void llarp_free_router(struct llarp_router **router); void llarp_free_router(struct llarp_router **router);
bool llarp_configure_router(struct llarp_router *router, bool llarp_configure_router(struct llarp_router *router,
struct llarp_config *conf); struct llarp_config *conf);
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop); void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop);
void llarp_stop_router(struct llarp_router *router); void llarp_stop_router(struct llarp_router *router);

@ -1,36 +1,39 @@
#ifndef LLARP_ROUTER_IDENT_H #ifndef LLARP_ROUTER_IDENT_H
#define LLARP_ROUTER_IDENT_H #define LLARP_ROUTER_IDENT_H
#include <llarp/address_info.h> #include <llarp/address_info.h>
#include <llarp/router_contact.h>
#include <llarp/crypto.h> #include <llarp/crypto.h>
#include <llarp/exit_info.h> #include <llarp/exit_info.h>
#include <llarp/router_contact.h>
#include <stdbool.h> #include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* rotuer identity private info * rotuer identity private info
*/ */
struct llarp_router_ident struct llarp_router_ident {
{ struct llarp_crypto *crypto;
struct llarp_crypto * crypto;
llarp_seckey_t signkey; llarp_seckey_t signkey;
uint64_t updated; uint64_t updated;
uint16_t version; uint16_t version;
struct llarp_ai_list * addrs; struct llarp_ai_list *addrs;
struct llarp_xi_list * exits; 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);
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_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);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -4,15 +4,13 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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;
size_t slen = 0; while (sz-- && str[slen]) slen++;
while(sz-- && str[slen]) return slen;
slen ++; }
return slen;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -1,58 +1,58 @@
#include <arpa/inet.h>
#include <llarp/address_info.h> #include <llarp/address_info.h>
#include <llarp/bencode.h> #include <llarp/bencode.h>
#include <llarp/mem.h> #include <llarp/mem.h>
#include <llarp/string.h> #include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) { bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
char ipbuff [128] = {0}; char ipbuff[128] = {0};
const char * ipstr; const char *ipstr;
if(!bencode_start_dict(buff)) return false; if (!bencode_start_dict(buff)) return false;
/* rank */ /* rank */
if(!bencode_write_bytestring(buff, "c", 1)) return false; if (!bencode_write_bytestring(buff, "c", 1)) return false;
if(!bencode_write_uint16(buff, ai->rank)) return false; if (!bencode_write_uint16(buff, ai->rank)) return false;
/* dialect */ /* dialect */
if(!bencode_write_bytestring(buff, "d", 1)) return false; 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 */ /* encryption key */
if(!bencode_write_bytestring(buff, "e", 1)) return false; 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 */ /** ip */
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff)); ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
if(!ipstr) return false; if (!ipstr) return false;
if(!bencode_write_bytestring(buff, "i", 1)) 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 */ /** port */
if(!bencode_write_bytestring(buff, "p", 1)) return false; if (!bencode_write_bytestring(buff, "p", 1)) return false;
if(!bencode_write_uint16(buff, ai->port)) return false; if (!bencode_write_uint16(buff, ai->port)) return false;
/** version */ /** version */
if(!bencode_write_version_entry(buff)) return false; if (!bencode_write_version_entry(buff)) return false;
/** end */ /** end */
return bencode_end(buff); return bencode_end(buff);
} }
struct llarp_ai_list {
struct llarp_ai_list struct llarp_ai_list *next;
{
struct llarp_ai_list * next;
struct llarp_ai data; 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); 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 * cur = l; struct llarp_ai_list *tmp;
struct llarp_ai_list * tmp;
do { do {
tmp = cur->next; tmp = cur->next;
llarp_g_mem.free(cur); llarp_g_mem.free(cur);
cur = tmp; cur = tmp;
}while(cur->next); } while (cur->next);
} }
void llarp_ai_list_iterate(struct llarp_ai_list *l, void llarp_ai_list_iterate(struct llarp_ai_list *l,

@ -3,16 +3,15 @@
#include <stdio.h> #include <stdio.h>
extern "C" { 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;
int written; size_t sz = llarp_buffer_size_left(buff);
size_t sz = llarp_buffer_size_left(buff); va_list args;
va_list args; va_start(args, fmt);
va_start(args, fmt); written = snprintf(buff->cur, sz, fmt, args);
written = snprintf(buff->cur, sz, fmt, args); va_end(args);
va_end(args); if (written == -1) return false;
if (written == -1) return false; buff->sz += written;
buff->sz += written; return true;
return true; }
}
} }

@ -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) { llarp_dh_complete_hook result, void *user) {
struct llarp_dh_internal *impl = struct llarp_dh_internal *impl =
llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32); llarp_g_mem.alloc(sizeof(struct llarp_dh_internal), 32);
struct llarp_thread_job job = { struct llarp_thread_job job = {.caller = dh->caller,
.caller = dh->caller, .data = impl,
.data = impl, .user = impl,
.user = impl, .work = &llarp_crypto_dh_work};
.work = &llarp_crypto_dh_work
};
memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t)); memcpy(impl->theirkey, theirkey, sizeof(llarp_pubkey_t));
memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t)); memcpy(impl->nounce, nounce, sizeof(llarp_tunnel_nounce_t));
impl->ourkey = dh->ourkey; impl->ourkey = dh->ourkey;

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

@ -1,40 +1,41 @@
#include <llarp/exit_route.h> #include <arpa/inet.h>
#include <llarp/bencode.h> #include <llarp/bencode.h>
#include <llarp/exit_route.h>
#include <llarp/string.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;
char addr_buff [128] = {0}; if (!bencode_start_dict(buff)) return false;
const char * addr;
if(!bencode_start_dict(buff)) return false;
/** gateway */ /** gateway */
addr = inet_ntop(AF_INET6, &xr->gateway, addr_buff, sizeof(addr_buff)); addr = inet_ntop(AF_INET6, &xr->gateway, addr_buff, sizeof(addr_buff));
if(!addr) return false; if (!addr) return false;
if(!bencode_write_bytestring(buff, "a", 1)) 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 */ /** netmask */
addr = inet_ntop(AF_INET6, &xr->netmask, addr_buff, sizeof(addr_buff)); addr = inet_ntop(AF_INET6, &xr->netmask, addr_buff, sizeof(addr_buff));
if(!addr) return false; if (!addr) return false;
if(!bencode_write_bytestring(buff, "b", 1)) 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 */
/** source */
addr = inet_ntop(AF_INET6, &xr->source, addr_buff, sizeof(addr_buff)); addr = inet_ntop(AF_INET6, &xr->source, addr_buff, sizeof(addr_buff));
if(!addr) return false; if (!addr) return false;
if(!bencode_write_bytestring(buff, "c", 1)) 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 */ /** lifetime */
if(!bencode_write_bytestring(buff, "l", 1)) return false; if (!bencode_write_bytestring(buff, "l", 1)) return false;
if(!bencode_write_uint64(buff, xr->lifetime)) return false; if (!bencode_write_uint64(buff, xr->lifetime)) return false;
/** version */ /** version */
if(!bencode_write_version_entry(buff)) return false; if (!bencode_write_version_entry(buff)) return false;
/* end */ /* end */
return bencode_end(buff); return bencode_end(buff);

@ -50,9 +50,9 @@ struct Level {
size_t depth; size_t depth;
static std::string default_value; static std::string default_value;
const std::string &operator[](const std::string &name) { const std::string &operator[](const std::string &name) {
for (const auto & itr : values) for (const auto &itr : values)
if (itr.first == name) return itr.second; if (itr.first == name) return itr.second;
return default_value; return default_value;
} }
@ -144,9 +144,10 @@ inline void Parser::parse(Level &l) {
} else { } else {
size_t n = line_.find('='); size_t n = line_.find('=');
if (n == std::string::npos) err("no '=' found"); if (n == std::string::npos) err("no '=' found");
auto p = std::make_pair(trim(line_.substr(0, n)), auto p =
trim(line_.substr(n + 1, line_.length() - n - 1))); std::make_pair(trim(line_.substr(0, n)),
trim(line_.substr(n + 1, line_.length() - n - 1)));
l.values.push_back(p); l.values.push_back(p);
} }
} }
@ -159,10 +160,10 @@ inline void Parser::dump(std::ostream &s, const Level &l,
if (!sname.empty()) s << sname; if (!sname.empty()) s << sname;
for (size_t i = 0; i < l.depth; ++i) s << ']'; for (size_t i = 0; i < l.depth; ++i) s << ']';
if (!sname.empty()) s << std::endl; if (!sname.empty()) s << std::endl;
for( const auto & itr : l.values) for (const auto &itr : l.values)
s << itr.first << '=' << itr.second << std::endl; s << itr.first << '=' << itr.second << std::endl;
for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); for (Level::sections_t::const_iterator it = l.ordered_sections.begin();
it != l.ordered_sections.end(); ++it) { it != l.ordered_sections.end(); ++it) {
assert((*it)->second.depth == l.depth + 1); assert((*it)->second.depth == l.depth + 1);

@ -3,85 +3,72 @@
#include <list> #include <list>
#include <string> #include <string>
namespace iwp namespace iwp {
{
struct link_impl
{
link_impl(llarp_seckey_t k, llarp_msg_muxer *m) : muxer(m)
{
memcpy(seckey, k, sizeof(seckey));
}
std::list<llarp_link_ev_listener> link_listeners;
llarp_seckey_t seckey;
llarp_msg_muxer * muxer;
std::string linkname;
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();
}
bool start(llarp_ev_loop * loop)
{
// todo: implement
return false;
}
bool stop()
{
// todo: implement
return false;
}
};
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) struct link_impl {
{ link_impl(llarp_seckey_t k, llarp_msg_muxer *m) : muxer(m) {
link_impl * link = static_cast<link_impl *>(l->impl); memcpy(seckey, k, sizeof(seckey));
return link->start(loop);
} }
static bool stop_link(struct llarp_link * l) std::list<llarp_link_ev_listener> link_listeners;
{
link_impl * link = static_cast<link_impl *>(l->impl); llarp_seckey_t seckey;
return link->stop();
llarp_msg_muxer *muxer;
std::string linkname;
bool configure(const char *ifname, int af, uint16_t port) {
// todo: implement
linkname = std::string(ifname) + std::string("+") + std::to_string(port);
return false;
} }
static void free_link(struct llarp_link * l) const char *name() { return linkname.c_str(); }
{
link_impl * link = static_cast<link_impl *>(l->impl); bool start(llarp_ev_loop *loop) {
delete link; // todo: implement
return false;
} }
static const char * link_name(struct llarp_link * l) bool stop() {
{ // todo: implement
link_impl * link = static_cast<link_impl *>(l->impl); return false;
return link->name();
} }
};
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) {
link_impl *link = static_cast<link_impl *>(l->impl);
return link->start(loop);
}
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) {
link_impl *link = static_cast<link_impl *>(l->impl);
delete link;
} }
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" { 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; llarp_seckey_t seckey;
args.crypto->keygen(&seckey); args.crypto->keygen(&seckey);
link->impl = new iwp::link_impl(seckey, muxer); 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; link->free = &iwp::free_link;
return true; return true;
} }
} }

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

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

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

Loading…
Cancel
Save