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

@ -9,7 +9,6 @@ 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)
@ -41,9 +40,15 @@ ifneq ($(GIT_VERSION),"")
VER_FLAGS=-DGIT_REV=\"$(GIT_VERSION)\"
endif
ifeq ($(JEMALLOC),"1")
MALLOC_LIB=-ljemalloc
else
MALLOC_LIB=
endif
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++14 $(CXXFLAGS) $(DEBUG_FLAGS) $(VER_FLAGS) -Wall -fPIC
LIB_LDFLAGS = -ljemalloc $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
LIB_LDFLAGS = $(MALLOC_LIB) $(SODIUM_LIBS) $(LIBUV_LIBS) -lm -lstdc++
REQUIRED_LDFLAGS = -L$(REPO) -lllarp
TEST_LDFLAGS = $(STATIC_LIB) $(LIB_LDFLAGS)

@ -45,7 +45,7 @@ int main(int argc, char *argv[]) {
struct llarp_main llarp = {NULL, NULL, NULL, NULL};
const char *conffname = "daemon.ini";
if (argc > 1) conffname = argv[1];
llarp_mem_jemalloc();
llarp_mem_stdlib();
llarp_new_config(&llarp.config);
llarp_ev_loop_alloc(&llarp.mainloop);
printf("%s loaded\n", LLARP_VERSION);
@ -55,8 +55,10 @@ int main(int argc, char *argv[]) {
iter.user = &llarp;
iter.visit = iter_main_config;
llarp_config_iter(llarp.config, &iter);
if (!llarp.tp) llarp.tp = llarp_init_threadpool(2);
llarp.router = llarp_init_router(llarp.tp);
if (!llarp_configure_router(llarp.router, llarp.config)) {
printf("Running\n");
llarp_run_router(llarp.router, llarp.mainloop);

@ -13,8 +13,8 @@ extern "C" {
struct llarp_ai {
uint16_t rank;
char dialect[MAX_AI_DIALECT_SIZE + 1];
llarp_pubkey_t enc_key;
uint8_t dialect[MAX_AI_DIALECT_SIZE + 1];
struct in6_addr ip;
uint16_t port;
};

@ -0,0 +1,61 @@
#ifndef LLARP_BENCODE_H
#define LLARP_BENCODE_H
#include <stdbool.h>
#include <stdint.h>
#include <llarp/buffer.h>
#include <llarp/version.h>
#ifdef __cplusplus
extern "C" {
#endif
bool inline bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
{
if(!llarp_buffer_writef(buff, "%ld:", sz)) return false;
return llarp_buffer_write(buff, data, sz);
}
bool inline bencode_write_int(llarp_buffer_t * buff, int i)
{
return llarp_buffer_writef(buff, "i%de", i);
}
bool inline bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
{
return llarp_buffer_writef(buff, "i%de", i);
}
bool inline bencode_write_int64(llarp_buffer_t * buff, int64_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
}
bool inline bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
}
bool inline bencode_start_list(llarp_buffer_t * buff)
{
return llarp_buffer_write(buff, "l", 1);
}
bool inline bencode_start_dict(llarp_buffer_t * buff)
{
return llarp_buffer_write(buff, "d", 1);
}
bool inline bencode_end(llarp_buffer_t * buff)
{
return llarp_buffer_write(buff, "e", 1);
}
bool inline bencode_write_version_entry(llarp_buffer_t * buff)
{
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
}
#ifdef __cplusplus
}
#endif
#endif

@ -3,6 +3,8 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -12,9 +14,28 @@ typedef struct llarp_buffer_t {
char *cur;
} llarp_buffer_t;
size_t llarp_buffer_size_left(llarp_buffer_t *buff);
bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
inline size_t llarp_buffer_size_left(llarp_buffer_t *buff)
{
size_t diff = buff->cur - buff->base;
if ( diff > buff->sz) return 0;
else return buff->sz - diff;
}
inline bool llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz)
{
size_t left = llarp_buffer_size_left(buff);
if (left >= sz)
{
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
return false;
}
bool llarp_buffer_writef(llarp_buffer_t *buff, const char * fmt, ...);
#ifdef __cplusplus
}
#endif

@ -17,6 +17,20 @@ bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buf);
struct llarp_xi_list;
struct llarp_xi_list * llarp_xi_list_new();
void llarp_xi_list_free(struct llarp_xi_list * l);
struct llarp_xi_list_iter
{
void * user;
struct llarp_xi_list * list;
bool (*visit)(struct llarp_xi_list_iter *, struct llarp_xi *);
};
void llarp_xi_list_iterate(struct llarp_xi_list *l,
struct llarp_xi_list_iter *iter);
#ifdef __cplusplus
}
#endif

@ -8,7 +8,6 @@ extern "C" {
#endif
struct llarp_rc {
llarp_buffer_t raw;
struct llarp_ai_list *addrs;
llarp_pubkey_t pubkey;
struct llarp_xi_list *exits;

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

@ -1,24 +1,50 @@
#include "address_info.hpp"
#include "bencode.hpp"
#include "str.hpp"
#include <llarp/bencode.h>
#include <cstring>
#include <arpa/inet.h>
namespace llarp {
/*
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, "e", a.enc_key, sizeof(a.enc_key)) &&
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" {
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
return llarp::BEncode(*ai, buff);
if(!bencode_start_dict(buff)) return false;
/* rank */
if(!bencode_write_bytestring(buff, "c", 1)) return false;
if(!bencode_write_uint16(buff, ai->rank)) return false;
/* dialect */
if(!bencode_write_bytestring(buff, "d", 1)) return false;
if(!bencode_write_bytestring(buff, ai->dialect, strnlen(ai->dialect, sizeof(ai->dialect)))) return false;
/* encryption key */
if(!bencode_write_bytestring(buff, "e", 1)) return false;
if(!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t))) return false;
/** ip */
char ipbuff [128] = {0};
const char * ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
if(ipstr == nullptr) return false;
if(!bencode_write_bytestring(buff, "i", 1)) return false;
if(!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff)))) return false;
/** port */
if(!bencode_write_bytestring(buff, "p", 1)) return false;
if(!bencode_write_uint16(buff, ai->port)) return false;
/** version */
if(!bencode_write_version_entry(buff)) return false;
/** end */
return bencode_end(buff);
}
void llarp_ai_list_iterate(struct llarp_ai_list *l,

@ -1,25 +1,18 @@
#include <llarp/buffer.h>
#include <cstring>
#include <stdarg.h>
#include <stdio.h>
extern "C" {
size_t llarp_buffer_size_left(llarp_buffer_t *buff) {
auto diff = buff->cur - buff->base;
if (diff < 0) {
return 0;
} else if (diff > buff->sz)
return 0;
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;
bool llarp_buffer_writef(llarp_buffer_t * buff, const char * fmt, ...)
{
int written;
size_t sz = llarp_buffer_size_left(buff);
va_list args;
va_start(args, fmt);
written = snprintf(buff->cur, sz, fmt, args);
va_end(args);
if (written == -1) return false;
buff->sz += written;
return true;
}
std::memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
}

@ -15,7 +15,7 @@ void std_free(void *ptr) {
} // namespace llarp
extern "C" {
void llarp_mem_std() {
void llarp_mem_stdlib() {
llarp_g_mem.alloc = llarp::std_malloc;
llarp_g_mem.free = llarp::std_free;
}

@ -1,24 +1,74 @@
#include "router_contact.hpp"
#include "address_info.hpp"
#include "bencode.hpp"
#include "exit_info.hpp"
#include <llarp/bencode.h>
#include <llarp/version.h>
namespace llarp {
bool BEncode(const llarp_rc &a, llarp_buffer_t *buff) {
std::list<llarp_ai> addresses = ai_list_to_std(a.addrs);
std::list<llarp_xi> exits = xi_list_to_std(a.exits);
return bencodeDict(buff) && bencodeDictKey(buff, "a") &&
BEncode(addresses, buff) &&
bencodeDict_Bytes(buff, "k", a.pubkey, sizeof(a.pubkey)) &&
bencodeDict_Int(buff, "v", 0) && bencodeDictKey(buff, "x") &&
BEncode(exits, buff) &&
bencodeDict_Bytes(buff, "z", a.signature, sizeof(a.signature)) &&
bencodeEnd(buff);
}
static bool bencode_rc_ai_write(struct llarp_ai_list_iter * i, struct llarp_ai * ai)
{
llarp_buffer_t * buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_ai_bencode(ai, buff);
}
static bool bencode_rc_xi_write(struct llarp_xi_list_iter * i, struct llarp_xi * xi)
{
llarp_buffer_t * buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_xi_bencode(xi, buff);
}
} // namespace llarp
extern "C" {
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
return llarp::BEncode(*rc, buff);
/* write dict begin */
if(!bencode_start_dict(buff))
return false;
if(rc->addrs)
{
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "a", 1))
return false;
if(!bencode_start_list(buff))
return false;
struct llarp_ai_list_iter ai_itr = {
.user = buff,
.list = nullptr,
.visit = &llarp::bencode_rc_ai_write
};
llarp_ai_list_iterate(rc->addrs, &ai_itr);
if(!bencode_end(buff))
return false;
}
/* write pubkey */
if(!bencode_write_bytestring(buff, "k", 1)) return false;
if(!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t))) return false;
/* write version */
if(!bencode_write_version_entry(buff)) return false;
if(rc->exits)
{
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "x", 1))
return false;
if(!bencode_start_list(buff))
return false;
struct llarp_xi_list_iter xi_itr = {
.user = buff,
.list = nullptr,
.visit = &llarp::bencode_rc_xi_write
};
llarp_xi_list_iterate(rc->exits, &xi_itr);
if(!bencode_end(buff))
return false;
}
/* write signature */
if(!bencode_write_bytestring(buff, "z", 1)) return false;
if(!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t))) return false;
return bencode_end(buff);
}
}

Loading…
Cancel
Save