diff --git a/include/llarp/dht.h b/include/llarp/dht.h index fdb380d34..1ab4d0859 100644 --- a/include/llarp/dht.h +++ b/include/llarp/dht.h @@ -4,11 +4,10 @@ extern "C" { #endif - struct llarp_dht_context; +struct llarp_dht_context; - struct llarp_dht_context * llarp_dht_context_new(); - void llarp_dht_context_free(struct llarp_dht_context * dht); - +struct llarp_dht_context* llarp_dht_context_new(); +void llarp_dht_context_free(struct llarp_dht_context* dht); #ifdef __cplusplus } diff --git a/include/llarp/ev.h b/include/llarp/ev.h index b103db07c..bf06d952b 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -20,7 +20,7 @@ int llarp_ev_loop_run(struct llarp_ev_loop *ev); void llarp_ev_loop_stop(struct llarp_ev_loop *ev); struct llarp_udp_listener { - struct sockaddr_in6 * addr; + struct sockaddr_in6 *addr; void *user; void *impl; void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *, diff --git a/include/llarp/ibfq.h b/include/llarp/ibfq.h index cb2d87aa8..f3abd8823 100644 --- a/include/llarp/ibfq.h +++ b/include/llarp/ibfq.h @@ -7,7 +7,6 @@ extern "C" { #endif - #ifdef __cplusplus } #endif diff --git a/include/llarp/link.h b/include/llarp/link.h index 985dfa638..ffd1da44a 100644 --- a/include/llarp/link.h +++ b/include/llarp/link.h @@ -1,12 +1,12 @@ #ifndef LLARP_LINK_H_ #define LLARP_LINK_H_ #include -#include #include +#include #include -#include #include +#include #ifdef __cplusplus extern "C" { @@ -14,18 +14,19 @@ extern "C" { struct llarp_link; -struct llarp_link *llarp_link_alloc(struct llarp_msg_muxer * muxer); +struct llarp_link *llarp_link_alloc(struct llarp_msg_muxer *muxer); void llarp_link_free(struct llarp_link **link); -bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, int af, uint16_t port); +bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, + int af, uint16_t port); /** get link listener for events */ struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *link); -void llarp_link_start(struct llarp_link * link); +void llarp_link_start(struct llarp_link *link); + +struct llarp_link_queue *llarp_link_frame_queue(struct llarp_link *link); - struct llarp_link_queue * llarp_link_frame_queue(struct llarp_link*link); - void llarp_link_stop(struct llarp_link *link); struct llarp_link_session; diff --git a/include/llarp/msg_handler.h b/include/llarp/msg_handler.h index 9bda61777..0fbc01db1 100644 --- a/include/llarp/msg_handler.h +++ b/include/llarp/msg_handler.h @@ -8,24 +8,22 @@ extern "C" { #endif - /* foward declare */ - struct llarp_msg_muxer; - struct llarp_link_session; - struct llarp_router; - -struct llarp_frame_handler { +/* foward declare */ +struct llarp_msg_muxer; +struct llarp_link_session; +struct llarp_router; +struct llarp_frame_handler { /** * participating paths */ - struct llarp_path_context * paths; - + struct llarp_path_context *paths; /** * parent muxer */ - struct llarp_msg_muxer * parent; - + struct llarp_msg_muxer *parent; + /** handle fully formed frame from link session */ @@ -41,9 +39,11 @@ struct llarp_msg_handler { struct llarp_msg_muxer { /** get a message handler for a link level message given msg.a */ - struct llarp_frame_handler *(*link_handler_for)(struct llarp_router *, const char); + struct llarp_frame_handler *(*link_handler_for)(struct llarp_router *, + const char); /** get a message handler for a routing layer message given msg.A */ - struct llarp_msg_handler *(*routing_handler_for)(struct llarp_router *, const char); + struct llarp_msg_handler *(*routing_handler_for)(struct llarp_router *, + const char); }; /** fill function pointers with default values */ diff --git a/llarp/address_info.cpp b/llarp/address_info.cpp index 21b235e80..9d70f37ba 100644 --- a/llarp/address_info.cpp +++ b/llarp/address_info.cpp @@ -3,8 +3,8 @@ #include "str.hpp" namespace llarp { - template<> - bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) { +template <> +bool BEncode(const llarp_ai &a, llarp_buffer_t *buff) { return bencodeDict(buff) && bencodeDict_Int(buff, "c", a.rank) && bencodeDict_Bytes(buff, "e", a.enc_key, sizeof(a.enc_key)) && bencodeDict_Bytes(buff, "d", a.dialect, @@ -15,7 +15,6 @@ namespace llarp { } } // namespace llarp - extern "C" { bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) { diff --git a/llarp/buffer.cpp b/llarp/buffer.cpp index fd164f319..03611a679 100644 --- a/llarp/buffer.cpp +++ b/llarp/buffer.cpp @@ -13,11 +13,9 @@ size_t llarp_buffer_size_left(llarp_buffer_t *buff) { return buff->sz - diff; } -bool llarp_buffer_write(llarp_buffer_t * buff, const void * data, size_t sz) -{ +bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz) { size_t left = llarp_buffer_size_left(buff); - if (sz > left) - { + if (sz > left) { return false; } std::memcpy(buff->cur, data, sz); diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 05af0f253..86f0ceed5 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -97,7 +97,7 @@ static void udp_close_cb(uv_handle_t *handle) { l->closed(); delete l; } - + } // namespace llarp namespace llarp { @@ -143,13 +143,14 @@ int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev, ret = uv_udp_init(ev->loop(), l->udp()); if (ret == 0) { - ret = uv_udp_bind(l->udp(), (sockaddr*) listener->addr, 0); + ret = uv_udp_bind(l->udp(), (sockaddr *)listener->addr, 0); if (ret == 0) { - char addr [128] = {0}; + char addr[128] = {0}; uv_ip6_name(listener->addr, addr, sizeof(addr)); - printf("bound udp listener at %s port %d\n", addr, ntohs(listener->addr->sin6_port)); - ret = - uv_udp_recv_start(l->udp(), &llarp::udp_alloc_cb, &llarp::udp_recv_cb); + printf("bound udp listener at %s port %d\n", addr, + ntohs(listener->addr->sin6_port)); + ret = uv_udp_recv_start(l->udp(), &llarp::udp_alloc_cb, + &llarp::udp_recv_cb); } } return ret; @@ -161,7 +162,7 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) { llarp::udp_listener *l = static_cast(listener->impl); if (l) { if (!uv_udp_recv_stop(l->udp())) { - uv_close((uv_handle_t*)l->udp(), &llarp::udp_close_cb); + uv_close((uv_handle_t *)l->udp(), &llarp::udp_close_cb); ret = 0; } } diff --git a/llarp/exit_info.cpp b/llarp/exit_info.cpp index 10abd1c36..732bdb521 100644 --- a/llarp/exit_info.cpp +++ b/llarp/exit_info.cpp @@ -2,8 +2,8 @@ #include "bencode.hpp" namespace llarp { - template<> - bool BEncode(const struct llarp_xi &xi, llarp_buffer_t *buff) { +template <> +bool BEncode(const struct llarp_xi &xi, llarp_buffer_t *buff) { return bencodeDict(buff) && bencodeDict_Bytes(buff, "a", &xi.address, sizeof(xi.address)) && bencodeDict_Bytes(buff, "b", &xi.netmask, sizeof(xi.netmask)) && @@ -11,7 +11,6 @@ namespace llarp { } } // namespace llarp - extern "C" { bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) { return llarp::BEncode(*xi, buff); diff --git a/llarp/link.cpp b/llarp/link.cpp index d77e4e8bd..afedad860 100644 --- a/llarp/link.cpp +++ b/llarp/link.cpp @@ -1,9 +1,9 @@ #include "link.hpp" -#include "net.hpp" -#include -#include #include +#include #include +#include +#include "net.hpp" bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, @@ -11,10 +11,7 @@ bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) { addr0.sin6_port < addr1.sin6_port; } - -namespace llarp -{ -} +namespace llarp {} extern "C" { struct llarp_link *llarp_link_alloc() { @@ -30,21 +27,21 @@ struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *l) { return &l->listener; } - bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, int af, uint16_t port) { - af = AF_INET6; - link->af = af; - if(llarp::net::GetIfAddr(ifname, link->af, (sockaddr*)&link->localaddr)) - { - link->localaddr.sin6_family = af; - link->localaddr.sin6_port = htons(port); - link->listener.addr = &link->localaddr; - char buff[128] = {0}; - uv_ip6_name(&link->localaddr, buff, sizeof(buff)); - printf("link %s configured with address %s\n", ifname, buff); - return true; - } - return false; +bool llarp_link_configure_addr(struct llarp_link *link, const char *ifname, + int af, uint16_t port) { + af = AF_INET6; + link->af = af; + if (llarp::net::GetIfAddr(ifname, link->af, (sockaddr *)&link->localaddr)) { + link->localaddr.sin6_family = af; + link->localaddr.sin6_port = htons(port); + link->listener.addr = &link->localaddr; + char buff[128] = {0}; + uv_ip6_name(&link->localaddr, buff, sizeof(buff)); + printf("link %s configured with address %s\n", ifname, buff); + return true; } + return false; +} void llarp_link_stop(struct llarp_link *link) { llarp_ev_close_udp_listener(&link->listener); diff --git a/llarp/link_handlers.hpp b/llarp/link_handlers.hpp index a17351d3c..b0039044b 100644 --- a/llarp/link_handlers.hpp +++ b/llarp/link_handlers.hpp @@ -2,18 +2,23 @@ #define LLARP_LINK_HANDLERS_HPP #include -namespace llarp -{ - namespace frame - { - bool process_intro(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_commit(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_down(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_up(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_accept(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_status(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - bool process_relay_exit(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg); - } -} +namespace llarp { +namespace frame { +bool process_intro(struct llarp_frame_handler* h, struct llarp_link_session* s, + llarp_buffer_t msg); +bool process_relay_commit(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +bool process_relay_down(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +bool process_relay_up(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +bool process_relay_accept(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +bool process_relay_status(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +bool process_relay_exit(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg); +} // namespace frame +} // namespace llarp #endif diff --git a/llarp/link_intro.cpp b/llarp/link_intro.cpp index 487febeb8..7c72f7f5f 100644 --- a/llarp/link_intro.cpp +++ b/llarp/link_intro.cpp @@ -1,12 +1,10 @@ #include "link_handlers.hpp" -namespace llarp -{ - namespace frame - { - bool process_intro(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) - { - return false; - } - } +namespace llarp { +namespace frame { +bool process_intro(struct llarp_frame_handler* h, struct llarp_link_session* s, + llarp_buffer_t msg) { + return false; } +} // namespace frame +} // namespace llarp diff --git a/llarp/link_relay_down.cpp b/llarp/link_relay_down.cpp index 091973305..c7765d17d 100644 --- a/llarp/link_relay_down.cpp +++ b/llarp/link_relay_down.cpp @@ -1,12 +1,10 @@ #include "link_handlers.hpp" -namespace llarp -{ - namespace frame - { - bool process_relay_down(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) - { - return false; - } - } +namespace llarp { +namespace frame { +bool process_relay_down(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg) { + return false; } +} // namespace frame +} // namespace llarp diff --git a/llarp/link_relay_up.cpp b/llarp/link_relay_up.cpp index 4190be3e8..7760cc365 100644 --- a/llarp/link_relay_up.cpp +++ b/llarp/link_relay_up.cpp @@ -1,12 +1,10 @@ #include "link_handlers.hpp" -namespace llarp -{ - namespace frame - { - bool process_relay_up(struct llarp_frame_handler * h, struct llarp_link_session * s, llarp_buffer_t msg) - { - return false; - } - } +namespace llarp { +namespace frame { +bool process_relay_up(struct llarp_frame_handler* h, + struct llarp_link_session* s, llarp_buffer_t msg) { + return false; } +} // namespace frame +} // namespace llarp diff --git a/llarp/muxer.cpp b/llarp/muxer.cpp index 719a965cd..a6546fb32 100644 --- a/llarp/muxer.cpp +++ b/llarp/muxer.cpp @@ -1,53 +1,46 @@ #include -#include "router.hpp" #include "link_handlers.hpp" +#include "router.hpp" -namespace llarp -{ - struct llarp_frame_handler introduce_handler = { +namespace llarp { +struct llarp_frame_handler introduce_handler = { .paths = nullptr, .parent = nullptr, - .process = &llarp::frame::process_intro - }; + .process = &llarp::frame::process_intro}; - struct llarp_frame_handler lrdm_handler = { +struct llarp_frame_handler lrdm_handler = { .paths = nullptr, .parent = nullptr, - .process = &llarp::frame::process_relay_down - }; + .process = &llarp::frame::process_relay_down}; - struct llarp_frame_handler lrum_handler = { +struct llarp_frame_handler lrum_handler = { .paths = nullptr, .parent = nullptr, - .process = &llarp::frame::process_relay_up - }; + .process = &llarp::frame::process_relay_up}; - static struct llarp_frame_handler * find_frame_handler(struct llarp_router * r, const char ch) - { - struct llarp_frame_handler * handler = nullptr; - switch(ch) - { +static struct llarp_frame_handler* find_frame_handler(struct llarp_router* r, + const char ch) { + struct llarp_frame_handler* handler = nullptr; + switch (ch) { case 'i': handler = &introduce_handler; - } - if(handler) - { - handler->paths = r->paths; - handler->parent = &r->muxer; - } - return handler; } - - static struct llarp_msg_handler * find_msg_handler(struct llarp_router * r, const char ch) - { - return nullptr; + if (handler) { + handler->paths = r->paths; + handler->parent = &r->muxer; } + return handler; } +static struct llarp_msg_handler* find_msg_handler(struct llarp_router* r, + const char ch) { + return nullptr; +} +} // namespace llarp + extern "C" { - void llarp_msg_muxer_init(struct llarp_msg_muxer * muxer) - { - muxer->link_handler_for = &llarp::find_frame_handler; - muxer->routing_handler_for = &llarp::find_msg_handler; - } +void llarp_msg_muxer_init(struct llarp_msg_muxer* muxer) { + muxer->link_handler_for = &llarp::find_frame_handler; + muxer->routing_handler_for = &llarp::find_msg_handler; +} } diff --git a/llarp/net.cpp b/llarp/net.cpp index c6cd652d5..c5c090161 100644 --- a/llarp/net.cpp +++ b/llarp/net.cpp @@ -1,36 +1,30 @@ #include "net.hpp" #include "str.hpp" -#include #include +#include + +namespace llarp { +namespace net { +bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr) { + ifaddrs* ifa = nullptr; + bool found = false; + socklen_t sl = sizeof(sockaddr_in6); + if (af == AF_INET) sl = sizeof(sockaddr_in); -namespace llarp -{ - namespace net - { - bool GetIfAddr(const std::string & ifname, int af, sockaddr * addr) - { - ifaddrs * ifa = nullptr; - bool found = false; - socklen_t sl = sizeof(sockaddr_in6); - if(af == AF_INET) - sl = sizeof(sockaddr_in); - - if(getifaddrs(&ifa) == -1) - return false; - ifaddrs * i = ifa; - while(i) - { - if(llarp::StrEq(i->ifa_name, ifname.c_str()) && i->ifa_addr && i->ifa_addr->sa_family == af) - { - memcpy(addr, i->ifa_addr, sl); - found = true; - break; - } - i = i->ifa_next; - } - if(ifa) freeifaddrs(ifa); - return found; + if (getifaddrs(&ifa) == -1) return false; + ifaddrs* i = ifa; + while (i) { + if (llarp::StrEq(i->ifa_name, ifname.c_str()) && i->ifa_addr && + i->ifa_addr->sa_family == af) { + memcpy(addr, i->ifa_addr, sl); + found = true; + break; } + i = i->ifa_next; } + if (ifa) freeifaddrs(ifa); + return found; } +} // namespace net +} // namespace llarp diff --git a/llarp/net.hpp b/llarp/net.hpp index 9bcd3acba..1b619d335 100644 --- a/llarp/net.hpp +++ b/llarp/net.hpp @@ -1,14 +1,12 @@ #ifndef LLARP_NET_HPP #define LLARP_NET_HPP -#include #include +#include -namespace llarp -{ - namespace net - { - bool GetIfAddr(const std::string & ifname, int af, sockaddr * addr); - } +namespace llarp { +namespace net { +bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr); } +} // namespace llarp #endif diff --git a/llarp/router.cpp b/llarp/router.cpp index fbd551970..38f72e741 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -1,7 +1,7 @@ +#include "router.hpp" +#include #include #include -#include -#include "router.hpp" #include "link.hpp" #include "str.hpp" @@ -10,19 +10,14 @@ void router_iter_config(llarp_config_iterator *iter, const char *section, const char *key, const char *val); } // namespace llarp -llarp_router::llarp_router() -{ - llarp_msg_muxer_init(&muxer); -} +llarp_router::llarp_router() { llarp_msg_muxer_init(&muxer); } + +llarp_router::~llarp_router() {} -llarp_router::~llarp_router() -{ -} - void llarp_router::AddLink(struct llarp_link *link) { llarp::router_links *head = &links; while (head->next && head->link) head = head->next; - + if (head->link) head->next = new llarp::router_links{link, nullptr}; else @@ -30,14 +25,14 @@ void llarp_router::AddLink(struct llarp_link *link) { } void llarp_router::ForEachLink(std::function visitor) { - llarp::router_links *cur = &links; - do { - if (cur->link) visitor(cur->link); - cur = cur->next; - } while (cur); - } + llarp::router_links *cur = &links; + do { + if (cur->link) visitor(cur->link); + cur = cur->next; + } while (cur); +} - void llarp_router::Close() { ForEachLink(llarp_link_stop); } +void llarp_router::Close() { ForEachLink(llarp_link_stop); } extern "C" { diff --git a/llarp/router.hpp b/llarp/router.hpp index ba177e403..e00b0c4c0 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -1,43 +1,40 @@ #ifndef LLARP_ROUTER_HPP #define LLARP_ROUTER_HPP -#include #include +#include #include #include "mem.hpp" -namespace llarp -{ - struct router_links - { - llarp_link * link = nullptr; - router_links * next = nullptr; - }; - -} // namespace llarp +namespace llarp { +struct router_links { + llarp_link *link = nullptr; + router_links *next = nullptr; +}; - struct llarp_router { - struct llarp_threadpool *tp; - llarp::router_links links; - llarp_crypto crypto; - llarp_msg_muxer muxer; - llarp_path_context * paths; +} // namespace llarp - static void *operator new(size_t sz) { - return llarp_g_mem.alloc(sz, llarp::alignment()); - } +struct llarp_router { + struct llarp_threadpool *tp; + llarp::router_links links; + llarp_crypto crypto; + llarp_msg_muxer muxer; + llarp_path_context *paths; - static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } + static void *operator new(size_t sz) { + return llarp_g_mem.alloc(sz, llarp::alignment()); + } - llarp_router(); - ~llarp_router(); + static void operator delete(void *ptr) { llarp_g_mem.free(ptr); } - void AddLink(struct llarp_link *link); + llarp_router(); + ~llarp_router(); - void ForEachLink(std::function visitor); + void AddLink(struct llarp_link *link); - void Close(); -}; + void ForEachLink(std::function visitor); + void Close(); +}; #endif diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 488e60b0c..26d2a21d2 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -17,8 +17,6 @@ bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) { } } // namespace llarp - - extern "C" { bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) { return llarp::BEncode(*rc, buff);