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

1
.gitignore vendored

@ -4,6 +4,7 @@
*.a
*.o
*.plist
*.so
llarpd

@ -2,12 +2,14 @@ REPO := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
EXE = $(REPO)/llarpd
STATIC_LIB = $(REPO)/libllarp.a
SHARED_LIB = $(REPO)/libllarp.so
STATIC_SRC_CPP = $(wildcard $(REPO)/llarp/*.cpp)
STATIC_SRC_C = $(wildcard $(REPO)/llarp/*.c)
STATIC_OBJ = $(STATIC_SRC_CPP:.cpp=.cpp.o) $(STATIC_SRC_C:.c=.c.o)
DAEMON_SRC = $(wildcard $(REPO)/daemon/*.c)
DAEMON_OBJ = $(DAEMON_SRC:.c=.c.o)
@ -39,9 +41,10 @@ ifneq ($(GIT_VERSION),"")
VER_FLAGS=-DGIT_REV=\"$(GIT_VERSION)\"
endif
REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c99 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS)
REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS)
REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
REQUIRED_CFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c99 $(CFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
REQUIRED_CXXFLAGS = $(LIBUV_FLAGS) $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
LIB_LDFLAGS = -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
REQUIRED_LDFLAGS = -L$(REPO) -lllarp
all: build
@ -53,20 +56,20 @@ build: $(EXE)
test: $(TEST_OBJ_CPP) $(TEST_OBJ_C)
$(TEST_SRC): $(STATIC_LIB)
$(TEST_SRC): $(SHARED_LIB)
$(TEST_OBJ_CPP): $(TEST_SRC_CPP)
$(CXX) $(REQUIRED_CXXFLAGS) $< -o $<.bin $(STATIC_LIB) $(REQUIRED_LDFLAGS)
$(CXX) $(REQUIRED_CXXFLAGS) $< -o $<.bin $(REQUIRED_LDFLAGS)
mv $<.bin $<.test
$<.test
$(TEST_OBJ_C): $(TEST_SRC_C)
$(CC) $(REQUIRED_CFLAGS) $< -o $<.bin $(STATIC_LIB) $(REQUIRED_LDFLAGS)
$(CC) $(REQUIRED_CFLAGS) $< -o $<.bin $(REQUIRED_LDFLAGS)
mv $<.bin $<.test
$<.test
$(EXE): $(DAEMON_OBJ) $(STATIC_LIB)
$(CXX) $(DAEMON_OBJ) $(STATIC_LIB) $(REQUIRED_LDFLAGS) -o $(EXE)
$(CXX) $(DAEMON_OBJ) $(STATIC_LIB) $(LIB_LDFLAGS) -o $(EXE)
%.cpp.o: %.cpp
$(CXX) $(REQUIRED_CXXFLAGS) -c $< -o $<.o
@ -74,8 +77,11 @@ $(EXE): $(DAEMON_OBJ) $(STATIC_LIB)
%.c.o: %.c
$(CC) $(REQUIRED_CFLAGS) -c $< -o $<.o
$(STATIC_LIB): $(STATIC_OBJ)
$(STATIC_LIB): $(STATIC_OBJ)
$(AR) -r $(STATIC_LIB) $(STATIC_OBJ)
$(SHARED_LIB): $(STATIC_OBJ)
$(CXX) -shared -o $(SHARED_LIB) $(STATIC_OBJ) $(LIB_LDFLAGS)
clean:
$(RM) $(DAEMON_OBJ) $(EXE) $(STATIC_OBJ) $(STATIC_LIB) $(TEST_OBJ)
$(RM) $(DAEMON_OBJ) $(EXE) $(STATIC_OBJ) $(STATIC_LIB) $(TEST_OBJ) $(SHARED_LIB)

@ -1,4 +1,4 @@
[router]
[links]
eth0=ip
lo=ip

@ -38,6 +38,7 @@ int shutdown_llarp(struct llarp_main *m) {
printf(".");
llarp_free_threadpool(&m->tp);
printf(".\n");
return 0;
}
int main(int argc, char *argv[]) {

@ -0,0 +1,16 @@
#ifndef LLARP_DHT_H_
#define LLARP_DHT_H_
#ifdef __cplusplus
extern "C" {
#endif
struct llarp_dht_context;
struct llarp_dht_context * llarp_dht_context_new();
void llarp_dht_context_free(struct llarp_dht_context * dht);
#ifdef __cplusplus
}
#endif
#endif

@ -20,8 +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 {
char *host;
uint16_t port;
struct sockaddr_in6 * addr;
void *user;
void *impl;
void (*recvfrom)(struct llarp_udp_listener *, const struct sockaddr *, char *,

@ -7,22 +7,6 @@
extern "C" {
#endif
// forward declare
struct llarp_msg_muxer;
struct llarp_link_queue;
struct llarp_link_queue *llarp_init_link_queue();
void llarp_free_link_queue(struct llarp_link_queue **queue);
/**
offer a full frame to the inbound frame queue
return true if successfully added
return false if the queue is full
*/
bool llarp_link_offer_frame(struct llarp_link_queue *queue, llarp_buffer_t msg);
/** return true if we have more messages to process */
bool llarp_link_queue_process(struct llarp_link_queue *queue,
struct llarp_msg_muxer *muxer);
#ifdef __cplusplus
}

@ -1,24 +1,31 @@
#ifndef LLARP_LINK_H_
#define LLARP_LINK_H_
#include <llarp/crypto.h>
#include <llarp/ibmq.h>
#include <llarp/msg_handler.h>
#include <llarp/mem.h>
#include <llarp/obmd.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct llarp_link;
struct llarp_link *llarp_link_alloc();
struct llarp_link *llarp_link_alloc(struct llarp_msg_muxer * muxer);
void llarp_link_free(struct llarp_link **link);
bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af);
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);
struct llarp_link_queue * llarp_link_frame_queue(struct llarp_link*link);
void llarp_link_stop(struct llarp_link *link);
struct llarp_link_session;

@ -1,33 +1,53 @@
#ifndef LLARP_MSG_HANDLER_H_
#define LLARP_MSG_HANDLER_H_
#include <llarp/buffer.h>
#include <llarp/link.h>
#include <llarp/dht.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* foward declare */
struct llarp_msg_muxer;
struct llarp_link_session;
struct llarp_router;
struct llarp_frame_handler {
struct llarp_obmd *outbound;
struct llarp_ibmq *inbound;
/**
* participating paths
*/
struct llarp_path_context * paths;
/**
* parent muxer
*/
struct llarp_msg_muxer * parent;
/**
handle fully formed frame from link session
*/
bool (*process)(struct llarp_frame_handler *, struct llarp_link_session *,
llarp_buffer_t);
};
struct llarp_msg_handler {
struct llarp_path_context *paths;
struct llarp_dht_context *dht;
bool (*process)(struct llarp_msg_handler *, llarp_buffer_t);
};
struct llarp_msg_muxer {
/** get a message handler for a link level message given msg.a */
struct llarp_frame_handler *(*link_handler_for)(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)(const char *);
struct llarp_msg_handler *(*routing_handler_for)(struct llarp_router *, const char);
};
/** fill function pointers with default values */
void llarp_msg_handler_mux_init(struct llarp_msg_muxer *muxer);
void llarp_msg_muxer_init(struct llarp_msg_muxer *muxer);
#ifdef __cplusplus
}

@ -3,12 +3,20 @@
#include "str.hpp"
namespace llarp {
static void *ai_alloc(size_t sz, size_t align) {}
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,
UStrLen(a.dialect, sizeof(a.dialect))) &&
bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) &&
bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) &&
bencodeEnd(buff);
}
} // namespace llarp
extern "C" {
struct llarp_alloc *llarp_ai_alloc = &llarp_g_mem;
extern "C" {
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
return llarp::BEncode(*ai, buff);
@ -24,15 +32,3 @@ void llarp_ai_list_iterate(struct llarp_ai_list *l,
} while (cur->next);
}
}
namespace llarp {
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,
UStrLen(a.dialect, sizeof(a.dialect))) &&
bencodeDict_Bytes(buff, "i", &a.ip, sizeof(a.ip)) &&
bencodeDict_Int(buff, "p", a.port) && bencodeDict_Int(buff, "v", 0) &&
bencodeEnd(buff);
}
} // namespace llarp

@ -1,4 +1,5 @@
#include <llarp/buffer.h>
#include <cstring>
extern "C" {
@ -11,4 +12,16 @@ size_t llarp_buffer_size_left(llarp_buffer_t *buff) {
else
return buff->sz - diff;
}
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)
{
return false;
}
std::memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
}

@ -97,6 +97,7 @@ static void udp_close_cb(uv_handle_t *handle) {
l->closed();
delete l;
}
} // namespace llarp
namespace llarp {
@ -134,8 +135,6 @@ int llarp_ev_loop_run(struct llarp_ev_loop *ev) {
int llarp_ev_add_udp_listener(struct llarp_ev_loop *ev,
struct llarp_udp_listener *listener) {
sockaddr_in6 addr;
uv_ip6_addr(listener->host, listener->port, &addr);
int ret = 0;
llarp::udp_listener *l = new llarp::udp_listener;
listener->impl = l;
@ -144,10 +143,13 @@ 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(), (const sockaddr *)&addr, 0);
ret = uv_udp_bind(l->udp(), (sockaddr*) listener->addr, 0);
if (ret == 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);
uv_udp_recv_start(l->udp(), &llarp::udp_alloc_cb, &llarp::udp_recv_cb);
}
}
return ret;
@ -159,8 +161,7 @@ int llarp_ev_close_udp_listener(struct llarp_udp_listener *listener) {
llarp::udp_listener *l = static_cast<llarp::udp_listener *>(listener->impl);
if (l) {
if (!uv_udp_recv_stop(l->udp())) {
l->closed();
delete l;
uv_close((uv_handle_t*)l->udp(), &llarp::udp_close_cb);
ret = 0;
}
}
@ -190,6 +191,6 @@ bool llarp_ev_call_async(struct llarp_ev_caller *caller, void *user) {
}
void llarp_ev_caller_stop(struct llarp_ev_caller *caller) {
uv_close((uv_handle_t *)&caller->async, llarp::ev_caller_async_closed);
uv_close((uv_handle_t *)&caller->async, &llarp::ev_caller_async_closed);
}
}

@ -1,17 +1,19 @@
#include "exit_info.hpp"
#include "bencode.hpp"
extern "C" {
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
return llarp::BEncode(*xi, buff);
}
}
namespace llarp {
bool BEncode(const 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)) &&
bencodeDict_Int(buff, "v", 0) && bencodeEnd(buff);
}
} // namespace llarp
extern "C" {
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
return llarp::BEncode(*xi, buff);
}
}

@ -1,6 +1,9 @@
#include "link.hpp"
#include "net.hpp"
#include <llarp/time.h>
#include <cstring>
#include <arpa/inet.h>
#include <uv.h>
bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) {
return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr,
@ -8,6 +11,11 @@ bool operator<(const sockaddr_in6 addr0, const sockaddr_in6 addr1) {
addr0.sin6_port < addr1.sin6_port;
}
namespace llarp
{
}
extern "C" {
struct llarp_link *llarp_link_alloc() {
return new llarp_link;
@ -22,9 +30,21 @@ struct llarp_udp_listener *llarp_link_udp_listener(struct llarp_link *l) {
return &l->listener;
}
bool llarp_link_configure(struct llarp_link *link, const char *ifname, int af) {
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);

@ -0,0 +1,19 @@
#ifndef LLARP_LINK_HANDLERS_HPP
#define LLARP_LINK_HANDLERS_HPP
#include <llarp/msg_handler.h>
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);
}
}
#endif

@ -0,0 +1,12 @@
#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;
}
}
}

@ -0,0 +1,12 @@
#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;
}
}
}

@ -0,0 +1,12 @@
#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;
}
}
}

@ -0,0 +1,53 @@
#include <llarp/msg_handler.h>
#include "router.hpp"
#include "link_handlers.hpp"
namespace llarp
{
struct llarp_frame_handler introduce_handler = {
.paths = nullptr,
.parent = nullptr,
.process = &llarp::frame::process_intro
};
struct llarp_frame_handler lrdm_handler = {
.paths = nullptr,
.parent = nullptr,
.process = &llarp::frame::process_relay_down
};
struct llarp_frame_handler lrum_handler = {
.paths = nullptr,
.parent = nullptr,
.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)
{
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;
}
}
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;
}
}

@ -0,0 +1,36 @@
#include "net.hpp"
#include "str.hpp"
#include <ifaddrs.h>
#include <arpa/inet.h>
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;
}
}
}

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

@ -1,41 +1,35 @@
#include <llarp/link.h>
#include <llarp/router.h>
#include <llarp/ibfq.h>
#include "router.hpp"
#include "link.hpp"
#include "mem.hpp"
#include "str.hpp"
namespace llarp {
void router_iter_config(llarp_config_iterator *iter, const char *section,
const char *key, const char *val);
struct router_links {
struct llarp_link *link = nullptr;
struct router_links *next = nullptr;
};
} // namespace llarp
struct llarp_router {
struct llarp_threadpool *tp;
llarp::router_links links;
llarp_crypto crypto;
static void *operator new(size_t sz) {
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
}
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
void AddLink(struct llarp_link *link) {
llarp::router_links *head = &links;
while (head->next && head->link) head = head->next;
llarp_router::llarp_router()
{
llarp_msg_muxer_init(&muxer);
}
if (head->link)
head->next = new llarp::router_links{link, nullptr};
else
head->link = link;
}
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
head->link = link;
}
void ForEachLink(std::function<void(llarp_link *)> visitor) {
void llarp_router::ForEachLink(std::function<void(llarp_link *)> visitor) {
llarp::router_links *cur = &links;
do {
if (cur->link) visitor(cur->link);
@ -43,8 +37,7 @@ struct llarp_router {
} while (cur);
}
void Close() { ForEachLink(llarp_link_stop); }
};
void llarp_router::Close() { ForEachLink(llarp_link_stop); }
extern "C" {
@ -88,8 +81,8 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
llarp_router *self = static_cast<llarp_router *>(iter->user);
if (StrEq(section, "links")) {
if (StrEq(val, "ip")) {
struct llarp_link *link = llarp_link_alloc();
if (llarp_link_configure(link, key, AF_INET6))
struct llarp_link *link = llarp_link_alloc(&self->muxer);
if (llarp_link_configure_addr(link, key, AF_INET6, 7000))
self->AddLink(link);
else {
llarp_link_free(&link);

@ -0,0 +1,43 @@
#ifndef LLARP_ROUTER_HPP
#define LLARP_ROUTER_HPP
#include <llarp/router.h>
#include <llarp/link.h>
#include <functional>
#include "mem.hpp"
namespace llarp
{
struct router_links
{
llarp_link * link = nullptr;
router_links * next = nullptr;
};
} // namespace llarp
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 new(size_t sz) {
return llarp_g_mem.alloc(sz, llarp::alignment<llarp_router>());
}
static void operator delete(void *ptr) { llarp_g_mem.free(ptr); }
llarp_router();
~llarp_router();
void AddLink(struct llarp_link *link);
void ForEachLink(std::function<void(llarp_link *)> visitor);
void Close();
};
#endif

@ -3,12 +3,6 @@
#include "bencode.hpp"
#include "exit_info.hpp"
extern "C" {
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
return llarp::BEncode(*rc, buff);
}
}
namespace llarp {
bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
@ -22,3 +16,11 @@ bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
bencodeEnd(buff);
}
} // namespace llarp
extern "C" {
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
return llarp::BEncode(*rc, buff);
}
}

Loading…
Cancel
Save