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

@ -1,4 +1,5 @@
[router]
threads=1
[links]
lo=ip
lo=1090

@ -12,10 +12,11 @@ struct llarp_main {
void iter_main_config(struct llarp_config_iterator *itr, const char *section,
const char *key, const char *val) {
struct llarp_main *m = (struct llarp_main *)itr->user;
if (!strcmp(section, "threadpool")) {
if (!strcmp(key, "workers")) {
if (!strcmp(section, "router")) {
if (!strcmp(key, "threads")) {
int workers = atoi(val);
if (!m->tp && workers > 0) {
if (workers > 0 && m->tp == NULL) {
printf("%s: %d worker threads\n", section, workers);
m->tp = llarp_init_threadpool(workers);
}
}
@ -26,16 +27,22 @@ int shutdown_llarp(struct llarp_main *m) {
printf("Shutting down .");
llarp_stop_router(m->router);
printf(".");
fflush(stdout);
llarp_ev_loop_stop(m->mainloop);
printf(".");
fflush(stdout);
llarp_threadpool_join(m->tp);
printf(".");
fflush(stdout);
llarp_free_router(&m->router);
printf(".");
fflush(stdout);
llarp_free_config(&m->config);
printf(".");
fflush(stdout);
llarp_ev_loop_free(&m->mainloop);
printf(".");
fflush(stdout);
llarp_free_threadpool(&m->tp);
printf(".\n");
return 0;
@ -59,7 +66,7 @@ int main(int argc, char *argv[]) {
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
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");
llarp_run_router(llarp.router, llarp.mainloop);
llarp_ev_loop_run(llarp.mainloop);

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

@ -4,7 +4,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <llarp/buffer.h>
#include <llarp/version.h>
#include <llarp/proto.h>
#ifdef __cplusplus
extern "C" {

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

@ -1,5 +1,6 @@
#ifndef LLARP_IWP_H_
#define LLARP_IWP_H_
#include <llarp/crypto.h>
#include <llarp/link.h>
#include <llarp/msg_handler.h>
#include <llarp/router_identity.h>
@ -10,10 +11,11 @@ extern "C" {
struct iwp_configure_args
{
llarp_seckey_t seckey;
struct llarp_crypto * crypto;
};
void 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);
#ifdef __cplusplus

@ -29,7 +29,7 @@ struct llarp_link_session_listener {
/** set by try_establish */
struct llarp_link *link;
/** set by try_establish */
struct llarp_rc *rc;
struct llarp_ai *ai;
/** callback to handle result */
void (*result)(struct llarp_link_session_listener *,
struct llarp_link_session *);
@ -37,7 +37,7 @@ struct llarp_link_session_listener {
/** information for establishing an outbound session */
struct llarp_link_establish_job {
struct llarp_rc *rc;
struct llarp_ai *ai;
uint64_t timeout;
};
@ -47,13 +47,26 @@ struct llarp_link_session_iter {
bool (*visit)(struct llarp_link_session_iter *, struct llarp_link_session *);
};
struct llarp_link_ev_listener
{
void * user;
void (*established)(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 (*rx)(struct llarp_ev_listener *, struct llarp_link_session *, size_t);
void (*error)(struct llarp_ev_listener *, struct llarp_link_session *, const char *);
};
struct llarp_link
{
void * impl;
int (*configure_addr)(struct llarp_link *, const char *, int, uint16_t);
int (*start_link)(struct llarp_link *, struct llarp_ev_loop *);
int (*stop_link)(struct llarp_link *);
struct llarp_rc * (*get_our_rc)(struct llarp_link *);
const char * (*name)(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 (*start_link)(struct llarp_link *, struct llarp_ev_loop *);
bool (*stop_link)(struct llarp_link *);
bool (*put_ai)(struct llarp_link *, struct llarp_ai *);
void (*iter_sessions)(struct llarp_link *, struct llarp_link_session_iter);
void (*try_establish)(struct llarp_link *,
struct llarp_link_establish_job,
@ -64,9 +77,13 @@ struct llarp_link
struct llarp_link_session
{
void * impl;
struct llarp_rc * (*get_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 */
ssize_t (*sendto)(struct llarp_link_session *, llarp_buffer_t);
/** return true if this session is timed out */
bool (*timeout)(struct llarp_link_session *);
/** explicit close session */
void (*close)(struct llarp_link_session *);
};

@ -0,0 +1,12 @@
#ifndef LLARP_PROTO_H
#define LLARP_PROTO_H
#ifndef LLARP_PROTO_VERSION
#define LLARP_PROTO_VERSION (0)
#endif
#ifndef LLARP_ETH_PROTO
#define LLARP_ETH_PROTO (0xD1CE)
#endif
#endif

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

@ -14,6 +14,7 @@ extern "C" {
*/
struct llarp_router_ident
{
struct llarp_crypto * crypto;
llarp_seckey_t signkey;
uint64_t updated;
uint16_t version;
@ -21,10 +22,15 @@ struct llarp_router_ident
struct llarp_xi_list * exits;
};
bool llarp_router_ident_bdecode(struct llarp_router_ident *rc, llarp_buffer_t *buf);
bool llarp_router_ident_bencode(struct llarp_router_ident *rc, llarp_buffer_t *buf);
void llarp_router_ident_free(struct llarp_router_ident **rc);
bool llarp_router_ident_gen_rc(struct llarp_router_ident * rc, llarp_buffer_t * buf);
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);
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
}

@ -0,0 +1,19 @@
#ifndef LLARP_STRING_H
#define LLARP_STRING_H
#include <llarp/common.h>
#ifdef __cplusplus
extern "C" {
#endif
size_t INLINE strnlen(const char * str, size_t sz)
{
size_t slen = 0;
while(sz-- && str[slen])
slen ++;
return slen;
}
#ifdef __cplusplus
}
#endif
#endif

@ -1,8 +1,6 @@
#ifndef LLARP_VERSION_H
#define LLARP_VERSION_H
#define LLARP_PROTO_VERSION (0)
#ifndef LLARP_VERSION_MAJ
#define LLARP_VERSION_MAJ "0"
#endif

@ -1,7 +1,7 @@
#include <llarp/address_info.h>
#include <llarp/bencode.h>
#include <llarp/mem.h>
#include <string.h>
#include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {

@ -1,7 +1,7 @@
#include <llarp/exit_info.h>
#include <llarp/bencode.h>
#include <llarp/string.h>
#include <arpa/inet.h>
#include <string.h>
bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {

@ -1,9 +1,11 @@
#include <llarp/exit_route.h>
#include <llarp/bencode.h>
#include <llarp/string.h>
#include <arpa/inet.h>
bool llarp_xr_bencode(struct llarp_xr * xr, llarp_buffer_t * buff)
{
char addr_buff [128] = {0};
const char * addr;

@ -1,39 +1,97 @@
#include <llarp/iwp.h>
#include <cstring>
#include <list>
#include <string>
namespace iwp
{
struct link_impl
{
link_impl(llarp_seckey_t k)
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;
int configure(const char * ifname, int af, uint16_t port)
llarp_msg_muxer * muxer;
std::string linkname;
bool configure(const char * ifname, int af, uint16_t port)
{
// todo: implement
return -1;
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 int configure_addr(struct llarp_link * l, const char * ifname, int af, uint16_t port)
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();
}
}
extern "C" {
void 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)
{
link->impl = new iwp::link_impl(args.seckey);
link->configure_addr = &iwp::configure_addr;
llarp_seckey_t seckey;
args.crypto->keygen(&seckey);
link->impl = new iwp::link_impl(seckey, muxer);
link->name = &iwp::link_name;
link->configure = &iwp::configure;
link->start_link = &iwp::start_link;
link->stop_link = &iwp::stop_link;
link->free = &iwp::free_link;
return true;
}
}

@ -3,6 +3,7 @@
#include <llarp/link.h>
#include <llarp/router.h>
#include <llarp/iwp.h>
#include <llarp/proto.h>
#include "str.hpp"
namespace llarp {
@ -10,7 +11,7 @@ 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() : ready(false) { llarp_msg_muxer_init(&muxer); }
llarp_router::~llarp_router() {}
@ -22,6 +23,13 @@ void llarp_router::AddLink(struct llarp_link *link) {
head->next = new llarp::router_links{link, nullptr};
else
head->link = link;
ready = true;
}
bool llarp_router::Ready()
{
return ready;
}
void llarp_router::ForEachLink(std::function<void(llarp_link *)> visitor) {
@ -42,18 +50,20 @@ struct llarp_router *llarp_init_router(struct llarp_threadpool *tp) {
return router;
}
int llarp_configure_router(struct llarp_router *router,
bool llarp_configure_router(struct llarp_router *router,
struct llarp_config *conf) {
llarp_config_iterator iter;
iter.user = router;
iter.visit = llarp::router_iter_config;
llarp_config_iter(conf, &iter);
return 0;
return router->Ready();
}
void llarp_run_router(struct llarp_router *router, struct llarp_ev_loop *loop) {
router->ForEachLink([loop](llarp_link *link) {
link->start_link(link, loop);
int result = link->start_link(link, loop);
if(result == -1)
printf("link %s failed to start\n", link->name(link));
});
}
@ -74,29 +84,38 @@ void router_iter_config(llarp_config_iterator *iter, const char *section,
const char *key, const char *val) {
llarp_router *self = static_cast<llarp_router *>(iter->user);
if (StrEq(section, "links")) {
if (StrEq(val, "ip")) {
if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>();
iwp_configure_args args;
self->crypto.keygen(&args.seckey);
iwp_configure_args args = {
.crypto = &self->crypto
};
iwp_link_init(link, args, &self->muxer);
if (link->configure_addr(link, key, AF_INET6, 7000))
if (link->configure(link, key, AF_PACKET, LLARP_ETH_PROTO))
{
printf("ethernet link configured on %s\n", key);
self->AddLink(link);
}
else {
link->free(link);
printf("failed to configure %s link for %s\n", val, key);
printf("failed to configure ethernetn link for %s\n", key);
}
} else if (StrEq(val, "eth")) {
struct llarp_link *link = llarp::Alloc<llarp_link>();
iwp_configure_args args;
self->crypto.keygen(&args.seckey);
} else {
struct llarp_link *link = llarp::Alloc<llarp_link>();
uint16_t port = std::atoi(val);
iwp_configure_args args = {
.crypto = &self->crypto
};
iwp_link_init(link, args, &self->muxer);
if (link->configure_addr(link, key, AF_PACKET, 0xD1CE))
if (link->configure(link, key, AF_INET6, port))
{
printf("inet link configured on %s port %d\n", key, port);
self->AddLink(link);
}
else {
link->free(link);
printf("failed to configure %s link for %s\n", val, key);
printf("failed to configure inet link for %s port %d\n", key, port);
}
}
}
}
}
} // namespace llarp

@ -15,6 +15,7 @@ struct router_links {
} // namespace llarp
struct llarp_router {
bool ready;
struct llarp_threadpool *tp;
llarp::router_links links;
llarp_crypto crypto;
@ -35,6 +36,8 @@ struct llarp_router {
void ForEachLink(std::function<void(llarp_link *)> visitor);
void Close();
bool Ready();
};
#endif

Loading…
Cancel
Save