diff --git a/CMakeLists.txt b/CMakeLists.txt index 83116b50a..baecc82f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ find_package(sodium) set(EXE llarpd) set(EXE_SRC daemon/main.c) -set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs jemalloc) +set(LIBS ${sodium_LIBRARY_RELEASE} pthread stdc++fs jemalloc mbedtls) set(LIB llarp) diff --git a/llarp/dtls_link.cpp b/llarp/dtls_link.cpp index 641e848fd..9ccc0a176 100644 --- a/llarp/dtls_link.cpp +++ b/llarp/dtls_link.cpp @@ -1,5 +1,8 @@ #include #include + +#include + #include #include "crypto.hpp" #include "net.hpp" @@ -18,7 +21,13 @@ struct dtls_link char keyfile[255]; char certfile[255]; uint32_t timeout_job_id; - std::map sessions; + std::map sessions; + + void inbound_session(llarp::Addr & src) + { + + } + }; static struct dtls_link * dtls_link_alloc(struct llarp_alloc * mem, struct llarp_msg_muxer * muxer, const char * keyfile, const char * certfile) @@ -55,6 +64,18 @@ static void dtls_link_issue_cleanup_timer(struct dtls_link * link, uint64_t time link->timeout_job_id = llarp_logic_call_later(link->logic, job); } +void dtls_recvfrom(struct llarp_udp_io * udp, const struct sockaddr *saddr, void * buf, ssize_t sz) +{ + dtls_link * link = static_cast(udp->user); + llarp::Addr src = *saddr; + auto itr = link->sessions.find(src); + if (itr == link->sessions.end()) + { + link->inbound_session(src); + } +} + + static bool dtls_link_configure(struct llarp_link * l, struct llarp_ev_loop * netloop, const char * ifname, int af, uint16_t port) { dtls_link * link = static_cast(l->impl); @@ -74,10 +95,12 @@ static bool dtls_link_configure(struct llarp_link * l, struct llarp_ev_loop * ne return false; } link->netloop = netloop; + link->udp.recvfrom = &dtls_recvfrom; + link->udp.user = link; return llarp_ev_add_udp(link->netloop, &link->udp) != -1; } -static bool dtls_link_start(struct llarp_link * l, struct llarp_logic * logic) +bool dtls_link_start(struct llarp_link * l, struct llarp_logic * logic) { dtls_link * link = static_cast(l->impl); link->timeout_job_id = 0; @@ -87,12 +110,12 @@ static bool dtls_link_start(struct llarp_link * l, struct llarp_logic * logic) return true; } -static void dtls_link_cleanup_dead_sessions(struct dtls_link * link) +void dtls_link_cleanup_dead_sessions(struct dtls_link * link) { // TODO: implement } -static void dtls_link_cleanup_timer(void * l, uint64_t orig, uint64_t left) +void dtls_link_cleanup_timer(void * l, uint64_t orig, uint64_t left) { dtls_link * link = static_cast(l); // clear out previous id of job @@ -106,7 +129,7 @@ static void dtls_link_cleanup_timer(void * l, uint64_t orig, uint64_t left) } -static bool dtls_link_stop(struct llarp_link *l) +bool dtls_link_stop(struct llarp_link *l) { dtls_link * link = static_cast(l->impl); if(link->timeout_job_id) @@ -117,7 +140,7 @@ static bool dtls_link_stop(struct llarp_link *l) } -static void dtls_link_iter_sessions(struct llarp_link * l, struct llarp_link_session_iter * iter) +void dtls_link_iter_sessions(struct llarp_link * l, struct llarp_link_session_iter * iter) { dtls_link * link = static_cast(l->impl); iter->link = l; @@ -126,15 +149,15 @@ static void dtls_link_iter_sessions(struct llarp_link * l, struct llarp_link_ses } -static void dtls_link_try_establish(struct llarp_link * link, struct llarp_link_establish_job job, struct llarp_link_session_listener l) +void dtls_link_try_establish(struct llarp_link * link, struct llarp_link_establish_job job, struct llarp_link_session_listener l) { } -static void dtls_link_mark_session_active(struct llarp_link * link, struct llarp_link_session * s) +void dtls_link_mark_session_active(struct llarp_link * link, struct llarp_link_session * s) { } -static struct llarp_link_session * dtls_link_session_for_addr(struct llarp_link * l, const struct sockaddr * saddr) +struct llarp_link_session * dtls_link_session_for_addr(struct llarp_link * l, const struct sockaddr * saddr) { if(saddr) { @@ -147,7 +170,7 @@ static struct llarp_link_session * dtls_link_session_for_addr(struct llarp_link return nullptr; } -static void dtls_link_free(struct llarp_link *l) +void dtls_link_free(struct llarp_link *l) { dtls_link * link = static_cast(l->impl); struct llarp_alloc * mem = link->mem; diff --git a/llarp/net.hpp b/llarp/net.hpp index 260027487..b378379a6 100644 --- a/llarp/net.hpp +++ b/llarp/net.hpp @@ -22,4 +22,52 @@ bool operator == (const sockaddr& a, const sockaddr& b) return a.sa_family == b.sa_family && memcmp(a.sa_data, b.sa_data, sz) == 0; } +bool operator < (const sockaddr_in6 & a, const sockaddr_in6 & b) +{ + return memcmp(&a, &b, sizeof(sockaddr_in6)) < 0; +} + +bool operator < (const in6_addr & a, const in6_addr b) +{ + return memcmp(&a, &b, sizeof(in6_addr)) < 0; +} + +namespace llarp +{ + struct Addr + { + int af = AF_INET; + in6_addr addr = {0}; + uint16_t port = 0; + + Addr() {}; + Addr(const sockaddr & other) { + uint8_t * addrptr = addr.s6_addr; + switch(other.sa_family) + { + case AF_INET: + // SIIT + memcpy(12+addrptr, &((const sockaddr_in*)(&other))->sin_addr, 4); + addrptr[11] = 0xff; + addrptr[10] = 0xff; + port = ((sockaddr_in*)(&other))->sin_port; + break; + case AF_INET6: + memcpy(addrptr, &((const sockaddr_in6*)(&other))->sin6_addr, 16); + port = ((sockaddr_in6*)(&other))->sin6_port; + break; + // TODO : sockaddr_ll + default: + break; + } + } + + bool operator < (const Addr & other) const + { + return af < other.af && addr < other.addr && port < other.port; + } + + }; +} + #endif