pull/1/head
Jeff Becker 6 years ago
parent 420f49a288
commit f022a5bcdf
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -10,12 +10,14 @@ extern "C" {
struct llarp_dtls_args {
struct llarp_alloc * mem;
char key_file[255];
char cert_file[255];
const char * keyfile;
const char * certfile;
};
void dtls_link_init(struct llarp_link* link, struct llarp_dtls_args args,
struct llarp_msg_muxer* muxer);
#ifdef __cplusplus
}
#endif
#endif

@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef __cplusplus
extern "C" {
@ -20,7 +21,7 @@ int llarp_ev_loop_run(struct llarp_ev_loop *ev);
void llarp_ev_loop_stop(struct llarp_ev_loop *ev);
struct llarp_udp_io {
struct sockaddr_in6 *addr;
struct sockaddr addr;
void *user;
void *impl;
struct llarp_ev_loop * parent;

@ -11,7 +11,6 @@ extern "C" {
struct iwp_configure_args {
struct llarp_alloc * mem;
struct llarp_ev_loop * ev;
struct llarp_crypto* crypto;
const char * keyfile;
};

@ -67,7 +67,7 @@ struct llarp_link {
int (*register_listener)(struct llarp_link *, struct llarp_link_ev_listener);
void (*deregister_listener)(struct llarp_link *, int);
*/
bool (*configure)(struct llarp_link *, const char *, int, uint16_t);
bool (*configure)(struct llarp_link *, struct llarp_ev_loop *, const char *, int, uint16_t);
bool (*start_link)(struct llarp_link *, struct llarp_logic *);
bool (*stop_link)(struct llarp_link *);
void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter*);
@ -81,7 +81,6 @@ struct llarp_link {
/** checks if all members are initialized */
bool llarp_link_initialized(struct llarp_link * link);
struct llarp_link_session {
void *impl;
@ -103,5 +102,4 @@ bool llarp_link_session_initialized(struct llarp_link_session * s);
#ifdef __cplusplus
}
#endif
#endif

@ -4,5 +4,15 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
bool llarp_getifaddr(const char * ifname, int af, struct sockaddr* addr);
#ifdef __cplusplus
}
#endif
#endif

@ -1,18 +1,28 @@
#include <llarp/dtls.h>
#include <llarp/net.h>
struct dtls_link
{
struct llarp_alloc * mem;
struct llarp_logic * logic;
struct llarp_ev_loop * netloop;
struct llarp_msg_muxer * msghandler;
struct llarp_udp_io udp;
char keyfile[255];
char certfile[255];
uint32_t timeout_job_id;
};
static struct dtls_link * dtls_link_alloc(struct llarp_alloc * mem, struct llarp_msg_muxer * muxer, char * keyfile, char * certfile)
static struct dtls_link * dtls_link_alloc(struct llarp_alloc * mem, struct llarp_msg_muxer * muxer, const char * keyfile, const char * certfile)
{
struct dtls_link * link = mem->alloc(mem, sizeof(struct dtls_link), 8);
if(link)
{
link->mem = mem;
link->msghandler = muxer;
strncpy(link->keyfile, keyfile, sizeof(link->keyfile));
strncpy(link->certfile, certfile, sizeof(link->certfile));
}
return link;
}
@ -35,6 +45,15 @@ 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);
}
static bool dtls_link_configure(struct llarp_link * l, struct llarp_ev_loop * netloop, const char * ifname, int af, uint16_t port)
{
struct dtls_link * link = l->impl;
if(!llarp_getifaddr(ifname, af, &link->udp.addr))
return false;
link->netloop = netloop;
return llarp_ev_add_udp(link->netloop, &link->udp) != -1;
}
static bool dtls_link_start(struct llarp_link * l, struct llarp_logic * logic)
{
struct dtls_link * link = l->impl;
@ -96,16 +115,15 @@ static void dtls_link_free(struct llarp_link *l)
mem->free(mem, link);
}
void dtls_link_init(struct llarp_link * link, struct llarp_dtls_args args, struct llarp_msg_muxer * muxer)
{
link->impl = dtls_link_alloc(args.mem, muxer, args.key_file, args.cert_file);
link->impl = dtls_link_alloc(args.mem, muxer, args.keyfile, args.certfile);
link->name = dtls_link_name;
/*
link->register_listener = dtls_link_reg_listener;
link->deregister_listener = dtls_link_dereg_listener;
*/
link->configure = dtls_link_configure;
link->start_link = dtls_link_start;
link->stop_link = dtls_link_stop;
link->iter_sessions = dtls_link_iter_sessions;

@ -101,7 +101,7 @@ struct llarp_epoll_loop : public llarp_ev_loop {
}
bool udp_listen(llarp_udp_io* l) {
int fd = udp_bind((sockaddr*)l->addr);
int fd = udp_bind(&l->addr);
if (fd == -1) return false;
llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
epoll_event ev;

@ -15,18 +15,18 @@ static const char * iwp_link_name()
return "IWP";
}
static bool iwp_link_configure(struct llarp_link * l, const char * ifname, int af, uint16_t port)
static bool iwp_link_configure(struct llarp_link * l, struct llarp_ev_loop * netloop, const char * ifname, int af, uint16_t port)
{
struct iwp_link * link = l->impl;
link->udp.user = link;
return llarp_ev_add_udp(link->netloop, &link->udp) == 0;
link->netloop = netloop;
return llarp_ev_add_udp(link->netloop, &link->udp) != -1;
}
static struct iwp_link * iwp_link_alloc(struct iwp_configure_args * args)
{
struct iwp_link * l = args->mem->alloc(args->mem, sizeof(struct iwp_link), 16);
l->alloc = args->mem;
l->netloop = args->ev;
l->keyfile = args->keyfile;
return l;
}

@ -44,3 +44,14 @@ void llarp_logic_mainloop(struct llarp_logic* logic) {
llarp_timer_run(logic->timer, logic->thread);
llarp_threadpool_wait(logic->thread);
}
uint32_t llarp_logic_call_later(struct llarp_logic* logic, struct llarp_timeout_job job)
{
return llarp_timer_call_later(logic->timer, job);
}
void llarp_logic_cancel_call(struct llarp_logic * logic, uint32_t id)
{
llarp_timer_cancel(logic->timer, id);
}

@ -1,7 +1,6 @@
#ifndef LLARP_MEM_HPP
#define LLARP_MEM_HPP
#include <llarp/mem.h>
#include <cmath>
namespace llarp {
template <typename T>
static constexpr size_t alignment() {

@ -1,12 +1,12 @@
#include "net.hpp"
#include <llarp/net.h>
#include "str.hpp"
#include <arpa/inet.h>
#include <ifaddrs.h>
namespace llarp {
namespace net {
bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr) {
extern "C" {
bool llarp_getifaddr(const char * ifname, int af, struct sockaddr* addr) {
ifaddrs* ifa = nullptr;
bool found = false;
socklen_t sl = sizeof(sockaddr_in6);
@ -15,7 +15,7 @@ bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr) {
if (getifaddrs(&ifa) == -1) return false;
ifaddrs* i = ifa;
while (i) {
if (llarp::StrEq(i->ifa_name, ifname.c_str()) && i->ifa_addr &&
if (llarp::StrEq(i->ifa_name, ifname) && i->ifa_addr &&
i->ifa_addr->sa_family == af) {
memcpy(addr, i->ifa_addr, sl);
found = true;
@ -26,5 +26,5 @@ bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr) {
if (ifa) freeifaddrs(ifa);
return found;
}
} // namespace net
} // namespace llarp
}

@ -1,12 +0,0 @@
#ifndef LLARP_NET_HPP
#define LLARP_NET_HPP
#include <sys/socket.h>
#include <string>
namespace llarp {
namespace net {
bool GetIfAddr(const std::string& ifname, int af, sockaddr* addr);
}
} // namespace llarp
#endif

@ -1,6 +1,6 @@
#include "router.hpp"
#include <llarp/ibfq.h>
#include <llarp/iwp.h>
#include <llarp/dtls.h>
#include <llarp/link.h>
#include <llarp/proto.h>
#include <llarp/router.h>
@ -90,24 +90,26 @@ void llarp_free_router(struct llarp_router **router) {
}
}
namespace llarp {
namespace llarp
{
void router_iter_config(llarp_config_iterator *iter, const char *section,
const char *key, const char *val) {
const char *key, const char *val)
{
llarp_router *self = static_cast<llarp_router *>(iter->user);
if (StrEq(section, "links")) {
iwp_configure_args args = {
.mem = self->mem,
.ev = self->netloop,
.crypto = &self->crypto,
.keyfile=self->transport_keyfile
};
llarp_dtls_args args = {
.mem = self->mem,
.keyfile=self->transport_keyfile,
.certfile=self->transport_certfile,
};
if (StrEq(section, "links"))
{
if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>(self->mem);
iwp_link_init(link, args, &self->muxer);
dtls_link_init(link, args, &self->muxer);
if(llarp_link_initialized(link))
{
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO))
if (link->configure(link, self->netloop, key, AF_PACKET, LLARP_ETH_PROTO))
{
printf("ethernet link configured on %s\n", key);
self->AddLink(link);
@ -119,10 +121,10 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>(self->mem);
uint16_t port = std::atoi(val);
iwp_link_init(link, args, &self->muxer);
dtls_link_init(link, args, &self->muxer);
if(llarp_link_initialized(link))
{
if (link->configure(link, key, AF_INET6, port))
if (link->configure(link, self->netloop, key, AF_INET6, port))
{
printf("inet link configured on %s port %d\n", key, port);
self->AddLink(link);
@ -134,4 +136,5 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
}
}
}
} // namespace llarp

@ -17,6 +17,7 @@ struct router_links {
struct llarp_router {
bool ready;
const char * transport_keyfile = "transport.key";
const char * transport_certfile = "transport.pem";
struct llarp_ev_loop * netloop;
struct llarp_threadpool *tp;
llarp::router_links links;

Loading…
Cancel
Save