This commit is contained in:
Ryan Tharp 2018-08-01 02:29:16 +00:00
commit 0b32bad7b9
66 changed files with 947 additions and 1052 deletions

2
.gitignore vendored
View File

@ -35,3 +35,5 @@ testnet_tmp
*.pid
vsproject/
daemon.ini

View File

@ -63,6 +63,7 @@
"future": "cpp",
"map": "cpp",
"vector": "cpp",
"new": "cpp"
"new": "cpp",
"shared_mutex": "cpp"
}
}

View File

@ -26,7 +26,11 @@ endif()
include(CheckIncludeFileCXX)
CHECK_INCLUDE_FILE_CXX(filesystem.h HAVE_STD_FS)
if(ANDROID)
set(THREAD_LIB "-pthread")
else()
set(THREAD_LIB pthread)
endif()
if(STATIC_LINK)
set(STATIC_LINK_LIBS c)
@ -207,6 +211,7 @@ set(LIB_SRC
llarp/crypto_async.cpp
llarp/crypto_libsodium.cpp
llarp/dht.cpp
llarp/dns.cpp
llarp/dnsc.cpp
llarp/dnsd.cpp
llarp/encode.cpp
@ -275,6 +280,8 @@ set(TEST_SRC
test/encrypted_frame_unittest.cpp
test/hiddenservice_unittest.cpp
)
set(TEST_EXE testAll)
set(GTEST_DIR test/gtest)
@ -284,9 +291,15 @@ set(CLIENT_SRC
client/main.cpp
)
#set(LIB llarp)
#set(SHARED_LIB ${LIB})
#set(STATIC_LIB ${LIB}-static)
set(ALL_SRC ${CLIENT_SRC} daemon/rcutil.cpp ${EXE_SRC} ${DNS_SRC} ${LIB_PLATFORM_SRC} ${LIB_SRC} ${TEST_SRC} ${CPP_BACKPORT_SRC})
foreach(F ${ALL_SRC})
set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS -DLOG_TAG=\\\"${F}\\\")
endforeach(F)
set(LIB lokinet)
set(SHARED_LIB ${LIB})
set(STATIC_LIB ${LIB}-static)
# TODO: exclude this from includes and expose stuff properly for rcutil
include_directories(llarp)
@ -301,7 +314,6 @@ if(SHADOW)
target_link_libraries(shadow-plugin-${SHARED_LIB} ${LIBS})
install(TARGETS shadow-plugin-${SHARED_LIB} DESTINATION plugins)
else()
add_executable(rcutil daemon/rcutil.cpp)
add_executable(${EXE} ${EXE_SRC})
add_executable(${CLIENT_EXE} ${CLIENT_SRC})
@ -357,46 +369,27 @@ else()
endif(MINGW)
endif(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
if (MINGW)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static pthread ws2_32 stdc++fs)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static ${THREAD_LIB} ws2_32 stdc++fs)
elseif(WIN32)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static pthread ws2_32)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static ${THREAD_LIB} ws2_32)
endif(MINGW)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static pthread)
target_link_libraries(dns ${STATIC_LIB} llarpplatform-static ${THREAD_LIB})
endif(NOT WITH_SHARED)
endif(WITH_STATIC)
if(WITH_SHARED)
add_library(${SHARED_LIB} SHARED ${LIB_SRC})
if(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
add_library(backport SHARED ${CPP_BACKPORT_SRC})
set(LIB_SRC ${LIB_SRC} ${CPP_BACKPORT_SRC})
endif(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
add_library(llarpplatform SHARED ${LIB_PLATFORM_SRC})
if(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
target_link_libraries(${SHARED_LIB} ${LIBS} backport llarpplatform)
if (MINGW)
target_link_libraries(${SHARED_LIB} ${LIBS} backport llarpplatform ws2_32 stdc++fs)
elseif(WIN32)
target_link_libraries(${SHARED_LIB} ${LIBS} backport llarpplatform ws2_32)
endif(MINGW)
else()
target_link_libraries(${SHARED_LIB} ${LIBS} llarpplatform)
if (MINGW)
target_link_libraries(${SHARED_LIB} ${LIBS} llarpplatform ws2_32 stdc++fs)
elseif(WIN32)
target_link_libraries(${SHARED_LIB} ${LIBS} llarpplatform ws2_32)
endif(MINGW)
endif(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
if(NOT WITH_STATIC)
if(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
target_link_libraries(${EXE} ${SHARED_LIB} backport llarpplatform)
target_link_libraries(${CLIENT_EXE} ${SHARED_LIB} backport llarpplatform)
target_link_libraries(rcutil ${SHARED_LIB} backport llarpplatform)
else()
target_link_libraries(${EXE} ${SHARED_LIB} llarpplatform)
target_link_libraries(${CLIENT_EXE} ${SHARED_LIB} llarpplatform)
target_link_libraries(rcutil ${SHARED_LIB} llarpplatform)
endif(COMPILER_SUPPORTS_CXX11 AND NOT COMPILER_SUPPORTS_CXX17 OR ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD" OR NOT HAVE_STD_FS)
target_link_libraries(dns ${SHARED_LIB} llarpplatform pthread)
endif(NOT WITH_STATIC)
add_library(${SHARED_LIB} SHARED ${LIB_SRC} ${LIB_PLATFORM_SRC})
if (MINGW)
set(${LIBS} ${LIBS} ws2_32 stdc++fs)
elseif(WIN32)
set(${LIBS} ${LIBS} ws2_32)
endif(MINGW)
target_link_libraries(${SHARED_LIB} ${LIBS} ${THREAD_LIB})
target_link_libraries(${EXE} ${SHARED_LIB})
target_link_libraries(rcutil ${SHARED_LIB})
target_link_libraries(dns ${SHARED_LIB} ${THREAD_LIB})
endif(WITH_SHARED)
endif(SHADOW)

View File

@ -86,6 +86,12 @@ testnet-build: testnet-configure
$(TESTNET_EXE): testnet-build
cp -f $(REPO)/llarpd $(TESTNET_EXE)
shared-configure: clean
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DWITH_SHARED=ON
shared: shared-configure
ninja
testnet: $(TESTNET_EXE)
mkdir -p $(TESTNET_ROOT)
python3 contrib/testnet/genconf.py --bin=$(TESTNET_EXE) --svc=$(TESTNET_SERVERS) --clients=$(TESTNET_CLIENTS) --dir=$(TESTNET_ROOT) --out $(TESTNET_CONF)

View File

@ -5,13 +5,22 @@ from ctypes import *
import signal
import time
import threading
import os
lib_file = os.path.join(os.path.realpath('.'), 'liblokinet.so')
class LLARP(threading.Thread):
class LokiNET(threading.Thread):
lib = None
ctx = None
def load(self, lib, conf):
self.lib = CDLL(lib)
self.lib.llarp_ensure_config(conf)
self.ctx = self.lib.llarp_main_init(conf)
return self.ctx != 0
def signal(self, sig):
if self.ctx and self.lib:
self.lib.llarp_main_signal(self.ctx, int(sig))
@ -20,21 +29,21 @@ class LLARP(threading.Thread):
code = self.lib.llarp_main_run(self.ctx)
print("llarp_main_run exited with status {}".format(code))
def close(self):
if self.lib and self.ctx:
self.lib.llarp_main_free(self.ctx)
def main():
llarp = LLARP()
llarp.lib = CDLL("./libllarp.so")
llarp.ctx = llarp.lib.llarp_main_init(b'daemon.ini')
if llarp.ctx:
llarp.start()
loki = LokiNET()
if loki.load(lib_file, b'daemon.ini'):
loki.start()
try:
while True:
print("busy loop")
time.sleep(1)
except KeyboardInterrupt:
llarp.signal(signal.SIGINT)
finally:
llarp.lib.llarp_main_free(llarp.ctx)
loki.close()
return

View File

@ -1,8 +0,0 @@
#!/usr/bin/env bash
root=$(dirname $(realpath -L $0))
if [ ! -d v ] ; then
echo "setting up wizard for the first time..."
python3 -m venv v && v/bin/pip install -r "$root/requirements.txt" &> /dev/null || echo "failed"
fi
v/bin/python "$root/lokinet.py" $@

View File

@ -1,50 +0,0 @@
#!/usr/bin/env python3
from configparser import ConfigParser as Config
import netifaces
import ipaddress
import os
def yield_public_addresses():
for ifname in netifaces.interfaces():
addrs = netifaces.ifaddresses(ifname)
if netifaces.AF_INET in addrs:
for addr in addrs[netifaces.AF_INET]:
ip = addr['addr']
if not ipaddress.ip_address(ip).is_private:
yield ifname, ip
def genconf(rootdir):
conf = Config()
conf['router'] = {
'threads': '2',
'net-threads': '1',
'contact-file': os.path.join(rootdir, 'self.signed'),
'transport-privkey': os.path.join(rootdir, 'transport.key'),
'identity-privkey': os.path.join(rootdir, 'identity.key')
}
conf['netdb'] = {
'dir': os.path.join(rootdir, 'netdb')
}
conf['bind'] = {}
found = False
for ifname, ip in yield_public_addresses():
conf['bind'][ifname] = '1090'
print("using public address {}".format(ip))
break
else:
print("This machine has no public network addresses")
return conf
def main(args):
fname = 'daemon.ini'
if len(args) == 1:
fname = args[0]
conf = genconf(os.path.realpath('.'))
if conf:
with open(fname, 'w') as f:
conf.write(f)
print("wrote config to {}".format(fname))
if __name__ == '__main__':
import sys
main(sys.argv[1:])

View File

@ -1 +0,0 @@
netifaces==0.10.7

View File

@ -1,12 +0,0 @@
[router]
threads = 2
net-threads = 1
contact-file = /home/jeff/git/llarp/self.signed
transport-privkey = /home/jeff/git/llarp/transport.key
identity-privkey = /home/jeff/git/llarp/identity.key
[netdb]
dir = /home/jeff/git/llarp/netdb
[bind]

View File

@ -3,12 +3,12 @@
#include <stdio.h> /* fprintf, printf */
#include <unistd.h>
#include <llarp.h>
#include <llarp/logic.h>
#include "dnsd.hpp"
#include "ev.hpp"
#include "llarp/net.hpp"
#include "logger.hpp"
#include "net.hpp"
#include <llarp.h>
#include <thread> // for multithreaded version
#include <vector>
@ -18,7 +18,7 @@
#endif
struct llarp_main *ctx = 0;
bool done = false;
bool done = false;
void
handle_signal(int sig)
@ -39,14 +39,15 @@ hookChecker(std::string name, struct dnsd_context *context)
#define SERVER "8.8.8.8"
#define PORT 53
struct dns_relay_config {
struct dns_relay_config
{
std::string upstream_host;
uint16_t upstream_port;
};
void
dns_iter_config(llarp_config_iterator *itr, const char *section,
const char *key, const char *val)
const char *key, const char *val)
{
dns_relay_config *config = (dns_relay_config *)itr->user;
if(!strcmp(section, "dns"))
@ -54,12 +55,14 @@ dns_iter_config(llarp_config_iterator *itr, const char *section,
if(!strcmp(key, "upstream-server"))
{
config->upstream_host = strdup(val);
llarp::LogDebug("Config file setting dns server to ", config->upstream_host);
llarp::LogDebug("Config file setting dns server to ",
config->upstream_host);
}
if(!strcmp(key, "upstream-port"))
{
config->upstream_port = atoi(val);
llarp::LogDebug("Config file setting dns server port to ", config->upstream_port);
llarp::LogDebug("Config file setting dns server port to ",
config->upstream_port);
}
}
}
@ -69,15 +72,15 @@ main(int argc, char *argv[])
{
int code = 1;
llarp::LogInfo("Starting up server");
const char *conffname = handleBaseCmdLineArgs(argc, argv);
dns_relay_config dnsr_config;
dnsr_config.upstream_host = "8.8.8.8";
dnsr_config.upstream_port = 53;
llarp_config *config_reader;
llarp_new_config(&config_reader);
//ctx = llarp_main_init(conffname, multiThreaded);
// ctx = llarp_main_init(conffname, multiThreaded);
if(llarp_load_config(config_reader, conffname))
{
llarp_free_config(&config_reader);
@ -103,7 +106,9 @@ main(int argc, char *argv[])
// configure main netloop
struct dnsd_context dnsd;
if(!llarp_dnsd_init(&dnsd, netloop, "*", 1053, (const char *)dnsr_config.upstream_host.c_str(), dnsr_config.upstream_port))
if(!llarp_dnsd_init(&dnsd, netloop, "*", 1053,
(const char *)dnsr_config.upstream_host.c_str(),
dnsr_config.upstream_port))
{
// llarp::LogError("failed to initialize dns subsystem");
llarp::LogError("Couldnt init dns daemon");
@ -112,41 +117,11 @@ main(int argc, char *argv[])
// Configure intercept
dnsd.intercept = &hookChecker;
// singlethreaded
if(0)
{
llarp::LogInfo("singlethread start");
worker = llarp_init_same_process_threadpool();
logic = llarp_init_single_process_logic(worker);
llarp_ev_loop_run_single_process(netloop, worker, logic);
llarp::LogInfo("singlethread end");
}
else
{
uint num_llarpworkers = 2;
uint num_nethreads = 8;
llarp::LogInfo("multithreaded start with ", num_llarpworkers,
" llarp-workers and ", num_nethreads, " networkers");
// create workers
worker = llarp_init_threadpool(num_llarpworkers, "llarp-worker");
logic = llarp_init_logic();
auto netio = netloop;
std::vector< std::thread > netio_threads;
while(num_nethreads--)
{
netio_threads.emplace_back([netio]() { llarp_ev_loop_run(netio); });
#if(__APPLE__ && __MACH__)
#elif(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
pthread_set_name_np(netio_threads.back().native_handle(),
"llarp-netio");
#else
pthread_setname_np(netio_threads.back().native_handle(), "llarp-netio");
#endif
}
llarp_logic_mainloop(logic);
llarp::LogInfo("multithreaded end");
}
llarp::LogInfo("singlethread start");
worker = llarp_init_same_process_threadpool();
logic = llarp_init_single_process_logic(worker);
llarp_ev_loop_run_single_process(netloop, worker, logic);
llarp::LogInfo("singlethread end");
llarp_ev_loop_free(&netloop);
}
else

View File

@ -22,52 +22,9 @@ main(int argc, char *argv[])
multiThreaded = false;
}
const char *conffname = handleBaseCmdLineArgs(argc, argv);
/*
const char *conffname = "daemon.ini";
int c;
while(1)
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
if(strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5)) == 0)
{
cSetLogLevel(eLogDebug);
}
else if(strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogInfo);
}
else if(strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogWarn);
}
else if(strncmp(optarg, "error", MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
abort();
}
}
*/
if(!llarp_ensure_config(conffname))
return 1;
ctx = llarp_main_init(conffname, multiThreaded);
int code = 1;

View File

@ -10,7 +10,7 @@
#include "buffer.hpp"
#include "crypto.hpp"
#include "fs.hpp"
#include "net.hpp"
#include "llarp/net.hpp"
#include "router.hpp"
struct llarp_main *ctx = 0;

View File

@ -349,7 +349,7 @@ otherwise transmit a LRUM to the next hop
p: p,
v: 0,
x: x1,
y: y
y: y ^ HS(k)
}
link relay downstream message (LRDM)
@ -377,7 +377,7 @@ x1 = SE(k, y, x)
p: p,
v: 0,
x: x1,
y: y
y: y ^ HS(k)
}
link immediate dht message (LIDM):

View File

@ -68,6 +68,23 @@ namespace llarp
return memcmp(l, other.l, sz) < 0;
}
AlignedBuffer
operator^(const AlignedBuffer& other) const
{
AlignedBuffer< sz > ret;
for(size_t idx = 0; idx < sz / 8; ++idx)
ret.l[idx] = l[idx] ^ other.l[idx];
return ret;
}
AlignedBuffer&
operator^=(const AlignedBuffer& other)
{
for(size_t idx = 0; idx < sz / 8; ++idx)
l[idx] ^= other.l[idx];
return *this;
}
size_t
size() const
{
@ -90,9 +107,13 @@ namespace llarp
bool
IsZero() const
{
AlignedBuffer< sz > b;
b.Zero();
return memcmp(l, b.l, sz) == 0;
size_t idx = sz / 8;
while(idx)
{
if(l[idx--])
return false;
}
return true;
}
void
@ -156,7 +177,7 @@ namespace llarp
return false;
if(strbuf.sz != sz)
{
llarp::LogError("bdecode buffer size missmatch ", strbuf.sz, "!=", sz);
llarp::LogErrorTag("AlignedBuffer::BDecode", "bdecode buffer size missmatch ", strbuf.sz, "!=", sz);
return false;
}
memcpy(b, strbuf.base, sz);

View File

@ -17,21 +17,9 @@
bool
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz);
bool
bencode_write_int(llarp_buffer_t* buff, int i);
bool
bencode_write_uint16(llarp_buffer_t* buff, uint16_t i);
bool
bencode_write_int64(llarp_buffer_t* buff, int64_t i);
bool
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i);
bool
bencode_write_sizeint(llarp_buffer_t* buff, size_t i);
bool
bencode_start_list(llarp_buffer_t* buff);

View File

@ -45,7 +45,8 @@ namespace llarp
{
if(!item.BDecode(buf))
{
llarp::LogWarn("failed to decode key ", k);
llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k, " for entry in dict");
return false;
}
read = true;
@ -62,7 +63,7 @@ namespace llarp
{
if(!bencode_read_integer(buf, &i))
{
llarp::LogWarn("failed to decode key ", k);
llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k, " for integer in dict");
return false;
}
read = true;

View File

@ -99,8 +99,6 @@ namespace llarp
{
// drop
nextTickInterval += initialIntervalMs / std::sqrt(++dropNum);
llarp::LogWarn("CoDel queue ", m_name, " drop ", nextTickInterval,
" ms next interval lowest=", lowest);
delete item;
m_Queue.pop();
break;

View File

@ -1,41 +1,54 @@
#ifndef LLARP_CONFIG_H_
#define LLARP_CONFIG_H_
/**
* config.h
*
* library configuration utilties
*/
struct llarp_config;
/// allocate config
void
llarp_new_config(struct llarp_config **conf);
/// deallocate config
void
llarp_free_config(struct llarp_config **conf);
/// @brief return -1 on fail otherwiwse 0
int
llarp_load_config(struct llarp_config *conf, const char *fname);
/// config iterator configuration
struct llarp_config_iterator
#ifdef __cplusplus
extern "C"
{
/// a customizable pointer to pass data to iteration functor
void *user;
/// set by llarp_config_iter
struct llarp_config *conf;
/// visit (self, section, key, value)
void (*visit)(struct llarp_config_iterator *, const char *, const char *,
const char *);
};
/// iterator over "conf" and call visit functor defined in "iter"
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter);
#endif
/**
* config.h
*
* library configuration utilties
*/
struct llarp_config;
/// allocate config
void
llarp_new_config(struct llarp_config **conf);
/// deallocate config
void
llarp_free_config(struct llarp_config **conf);
/// @brief return -1 on fail otherwiwse 0
int
llarp_load_config(struct llarp_config *conf, const char *fname);
/// config iterator configuration
struct llarp_config_iterator
{
/// a customizable pointer to pass data to iteration functor
void *user;
/// set by llarp_config_iter
struct llarp_config *conf;
/// visit (self, section, key, value)
void (*visit)(struct llarp_config_iterator *, const char *, const char *,
const char *);
};
/// iterator over "conf" and call visit functor defined in "iter"
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter);
/// ensure configuration exists
/// populate with defaults if it does not exist
/// return if this succeeded
bool
llarp_ensure_config(const char *fname);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -32,15 +32,7 @@ extern "C"
// bottom line is we can't use udp->user
// so we'll need to track all incoming and outgoing requests
struct dns_tracker
{
// uint c_responses;
uint c_requests;
std::map< uint, dnsc_answer_request * > client_request;
// FIXME: support multiple dns server contexts
dnsd_context *dnsd;
// std::map< uint, dnsd_question_request * > daemon_request;
};
struct dns_tracker;
// should we pass by llarp::Addr
// not as long as we're supporting raw

View File

@ -1,5 +1,4 @@
#ifndef LLARP_DTLS_H_
#define LLARP_DTLS_H_
#endif

View File

@ -33,7 +33,7 @@ llarp_ev_loop_free(struct llarp_ev_loop **ev);
/// run main loop
int
llarp_ev_loop_run(struct llarp_ev_loop *ev);
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic);
void
llarp_ev_loop_run_single_process(struct llarp_ev_loop *ev,

View File

@ -10,12 +10,12 @@
struct llarp_link
{
/*
typedef std::mutex mtx_t;
typedef std::unique_lock< mtx_t > lock_t;
/*
*/
typedef llarp::util::DummyMutex mtx_t;
typedef llarp::util::DummyLock lock_t;
*/
llarp_router *router;
llarp_crypto *crypto;
@ -38,7 +38,7 @@ struct llarp_link
const char *m_name;
typedef std::unordered_map< llarp::Addr, llarp_link_session *,
llarp::addrhash >
llarp::Addr::Hash >
LinkMap_t;
LinkMap_t m_sessions;
@ -52,7 +52,7 @@ struct llarp_link
std::atomic< bool > pumpingLogic;
typedef std::unordered_map< llarp::Addr, llarp_link_session *,
llarp::addrhash >
llarp::Addr::Hash >
PendingSessionMap_t;
PendingSessionMap_t m_PendingSessions;
mtx_t m_PendingSessions_Mutex;

View File

@ -6,10 +6,10 @@
#include "llarp/buffer.h"
#include "llarp/crypto.hpp"
#include "llarp/crypto_async.h"
#include "llarp/net.hpp"
#include "llarp/router_contact.h"
#include "llarp/time.h"
#include "llarp/types.h"
#include "net.hpp"
struct llarp_udp_io;
struct llarp_async_iwp;
@ -116,8 +116,6 @@ struct llarp_link_session
llarp_link_establish_job *establish_job = nullptr;
/// cached timestamp for frame creation
llarp_time_t now;
llarp_time_t createdAt = 0;
llarp_time_t lastKeepalive = 0;
uint32_t establish_job_id = 0;

View File

@ -97,9 +97,16 @@ namespace llarp
}
} // namespace llarp
#define LogDebug(x, ...) _Log(llarp::eLogDebug, __FILE__, x, ##__VA_ARGS__)
#define LogInfo(x, ...) _Log(llarp::eLogInfo, __FILE__, x, ##__VA_ARGS__)
#define LogWarn(x, ...) _Log(llarp::eLogWarn, __FILE__, x, ##__VA_ARGS__)
#define LogError(x, ...) _Log(llarp::eLogError, __FILE__, x, ##__VA_ARGS__)
#define LogDebug(x, ...) _Log(llarp::eLogDebug, LOG_TAG, x, ##__VA_ARGS__)
#define LogInfo(x, ...) _Log(llarp::eLogInfo, LOG_TAG, x, ##__VA_ARGS__)
#define LogWarn(x, ...) _Log(llarp::eLogWarn, LOG_TAG, x, ##__VA_ARGS__)
#define LogError(x, ...) _Log(llarp::eLogError, LOG_TAG, x, ##__VA_ARGS__)
#define LogDebugTag(tag, x, ...) _Log(llarp::eLogDebug, tag, x, ##__VA_ARGS__)
#define LogInfoTag(tag, x, ...) _Log(llarp::eLogInfo, tag, x, ##__VA_ARGS__)
#define LogWarnTag(tag, x, ...) _Log(llarp::eLogWarn, tag, x, ##__VA_ARGS__)
#define LogErrorTag(tag, x, ...) _Log(llarp::eLogError, tag, x, ##__VA_ARGS__)
#endif

View File

@ -6,7 +6,6 @@
*/
#include <llarp/messages/dht_immediate.hpp>
#include <llarp/messages/discard.hpp>
#include <llarp/messages/link_intro.hpp>
#include <llarp/messages/relay.hpp>
#include <llarp/messages/relay_ack.hpp>

View File

@ -1,80 +0,0 @@
#ifndef LLARP_MESSAGES_DISCARD_HPP
#define LLARP_MESSAGES_DISCARD_HPP
#include <llarp/link_message.hpp>
namespace llarp
{
const std::size_t MAX_DISCARD_SIZE = 10000;
/// a dummy link message that is discarded
struct DiscardMessage : public ILinkMessage
{
byte_t pad[MAX_DISCARD_SIZE];
size_t sz = 0;
DiscardMessage(const RouterID& id) : ILinkMessage(id)
{
}
DiscardMessage(std::size_t padding) : ILinkMessage()
{
sz = padding;
memset(pad, 'z', sz);
}
virtual bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
{
llarp_buffer_t strbuf;
if(llarp_buffer_eq(key, "v"))
{
if(!bencode_read_integer(buf, &version))
return false;
return version == LLARP_PROTO_VERSION;
}
if(llarp_buffer_eq(key, "z"))
{
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz > MAX_DISCARD_SIZE)
return false;
sz = strbuf.sz;
memcpy(pad, strbuf.base, sz);
return true;
}
return false;
}
virtual bool
BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!bencode_write_bytestring(buf, "a", 1))
return false;
if(!bencode_write_bytestring(buf, "z", 1))
return false;
if(!bencode_write_version_entry(buf))
return false;
if(!bencode_write_bytestring(buf, "z", 1))
return false;
if(!bencode_write_bytestring(buf, pad, sz))
return false;
return bencode_end(buf);
}
virtual bool
HandleMessage(llarp_router* router) const
{
(void)router;
llarp::LogInfo("got discard message of size ", sz, " bytes");
return true;
}
};
} // namespace llarp
#endif

View File

@ -2,6 +2,7 @@
#define LLARP_NET_HPP
#include <llarp/address_info.h>
#include <llarp/net.h>
#include <functional>
#include <iostream>
#include "mem.hpp"
@ -217,25 +218,36 @@ namespace llarp
in_addr_t addr = this->addr4()->s_addr;
unsigned byte = ntohl(addr);
unsigned byte1 = byte >> 24 & 0xff;
unsigned byte2 = byte >> 16 & 0xff;
unsigned byte2 = (0x00ff0000 & byte >> 16);
return (byte1 == 10 || (byte1 == 192 && byte2 == 168)
|| (byte1 == 172 && (byte2 & 0xf0) == 16));
|| (byte1 == 172 && (byte2 >= 16 || byte2 <= 31)));
}
bool
isLoopback()
{
return (ntohl(addr4()->s_addr)) >> 24 == 127;
}
struct Hash
{
std::size_t
operator()(Addr const& a) const noexcept
{
if(a.af() == AF_INET)
{
return a.port() + a.addr4()->s_addr;
}
uint8_t empty[16] = {0};
return (a.af() + memcmp(a.addr6(), empty, 16)) ^ a.port();
}
};
};
struct addrhash
{
std::size_t
operator()(Addr const& a) const noexcept
{
if(a.af() == AF_INET)
{
return a.port() + a.addr4()->s_addr;
}
uint8_t empty[16] = {0};
return (a.af() + memcmp(a.addr6(), empty, 16)) ^ a.port();
}
};
/// get first network interface with public address
bool
GetBestNetIF(std::string& ifname, int af = AF_INET);
} // namespace llarp
#endif

View File

@ -126,6 +126,7 @@ namespace llarp
TransitHopInfo info;
SharedSecret pathKey;
ShortHash nonceXOR;
llarp_time_t started = 0;
// 10 minutes default
llarp_time_t lifetime = DEFAULT_PATH_LIFETIME;
@ -188,6 +189,8 @@ namespace llarp
SecretKey commkey;
/// shared secret at this hop
SharedSecret shared;
/// hash of shared secret used for nonce mutation
ShortHash nonceXOR;
/// next hop's router id
RouterID upstream;
/// nonce for key exchange

View File

@ -22,9 +22,11 @@ namespace llarp
Tag(const std::string& str) : Tag()
{
#ifndef MIN
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
memcpy(data(), str.c_str(), MIN(16UL, str.size()));
#undef MIN
#endif
}
llarp::dht::Key_t

View File

@ -135,7 +135,7 @@ llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff)
/* rank */
if(!bencode_write_bytestring(buff, "c", 1))
return false;
if(!bencode_write_uint16(buff, ai->rank))
if(!bencode_write_uint64(buff, ai->rank))
return false;
/* dialect */
if(!bencode_write_bytestring(buff, "d", 1))
@ -159,7 +159,7 @@ llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff)
/** port */
if(!bencode_write_bytestring(buff, "p", 1))
return false;
if(!bencode_write_uint16(buff, ai->port))
if(!bencode_write_uint64(buff, ai->port))
return false;
/** version */

View File

@ -8,34 +8,11 @@ bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz)
return llarp_buffer_write(buff, data, sz);
}
bool
bencode_write_int(llarp_buffer_t* buff, int i)
{
return llarp_buffer_writef(buff, "i%de", i);
}
bool
bencode_write_uint16(llarp_buffer_t* buff, uint16_t i)
{
return llarp_buffer_writef(buff, "i%de", i);
}
bool
bencode_write_int64(llarp_buffer_t* buff, int64_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
}
bool
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
}
bool
bencode_write_sizeint(llarp_buffer_t* buff, size_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
return llarp_buffer_writef(buff, "i%lu", i)
&& llarp_buffer_write(buff, "e", 1);
}
bool
@ -81,10 +58,7 @@ bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
buffer->cur++;
numbuf[len] = 0;
auto num = atol(numbuf);
if(num == -1)
return false;
*result = num;
*result = strtoul(numbuf, nullptr, 10);
return true;
}

View File

@ -1,6 +1,9 @@
#include "config.hpp"
#include <llarp/config.h>
#include <llarp/net.hpp>
#include "fs.hpp"
#include "ini.hpp"
#include "logger.hpp"
#include "mem.hpp"
namespace llarp
@ -38,43 +41,79 @@ namespace llarp
} // namespace llarp
void
llarp_new_config(struct llarp_config **conf)
extern "C"
{
llarp_config *c = new llarp_config;
*conf = c;
}
void
llarp_new_config(struct llarp_config **conf)
{
llarp_config *c = new llarp_config;
*conf = c;
}
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
*conf = nullptr;
}
void
llarp_free_config(struct llarp_config **conf)
{
if(*conf)
delete *conf;
*conf = nullptr;
}
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
return 0;
}
int
llarp_load_config(struct llarp_config *conf, const char *fname)
{
if(!conf->impl.Load(fname))
return -1;
return 0;
}
void
llarp_config_iter(struct llarp_config *conf, struct llarp_config_iterator *iter)
{
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
{"network", conf->impl.network}, {"connect", conf->impl.connect},
{"bind", conf->impl.iwp_links}, {"netdb", conf->impl.netdb},
{"dns", conf->impl.dns}, {"services", conf->impl.services}};
void
llarp_config_iter(struct llarp_config *conf,
struct llarp_config_iterator *iter)
{
iter->conf = conf;
std::map< std::string, llarp::Config::section_t & > sections = {
{"network", conf->impl.network}, {"connect", conf->impl.connect},
{"bind", conf->impl.iwp_links}, {"netdb", conf->impl.netdb},
{"dns", conf->impl.dns}, {"services", conf->impl.services}};
for(const auto item : conf->impl.router)
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
for(const auto item : conf->impl.router)
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
for(const auto section : sections)
for(const auto item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
for(const auto section : sections)
for(const auto item : section.second)
iter->visit(iter, section.first.c_str(), item.first.c_str(),
item.second.c_str());
}
bool
llarp_ensure_config(const char *fname)
{
std::error_code ec;
if(fs::exists(fname, ec))
return true;
if(ec)
{
llarp::LogError(ec);
return false;
}
std::ofstream f(fname);
if(!f.is_open())
{
llarp::LogError("failed to open ", fname, " for writing");
return false;
}
f << "[netdb]" << std::endl;
f << "dir=netdb" << std::endl;
f << "[bind]" << std::endl;
std::string ifname;
if(llarp::GetBestNetIF(ifname, AF_INET))
f << ifname << "=1090" << std::endl;
llarp::LogInfo("Generated new config ", fname);
return true;
}
}

View File

@ -1,17 +1,18 @@
#include <getopt.h>
#include <llarp.h>
#include <llarp/logger.h>
#include <signal.h>
#include <sys/param.h> // for MIN
#include <llarp.hpp>
#include "logger.hpp"
#include "math.h"
#include "router.hpp"
#include <getopt.h>
#include <sys/param.h> // for MIN
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
#endif
namespace llarp
{
Context::~Context()
@ -187,22 +188,8 @@ namespace llarp
}
else
{
auto netio = mainloop;
while(num_nethreads--)
{
netio_threads.emplace_back([netio]() { llarp_ev_loop_run(netio); });
#if(__APPLE__ && __MACH__)
#elif(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
pthread_set_name_np(netio_threads.back().native_handle(),
"llarp-"
"netio");
#else
pthread_setname_np(netio_threads.back().native_handle(), "llarp-netio");
#endif
}
llarp::LogInfo("running mainloop");
llarp_logic_mainloop(logic);
return llarp_ev_loop_run(mainloop, logic);
}
return 0;
}
@ -297,194 +284,199 @@ struct llarp_main
std::unique_ptr< llarp::Context > ctx;
};
struct llarp_main *
llarp_main_init(const char *fname, bool multiProcess)
extern "C"
{
if(!fname)
fname = "daemon.ini";
char *var = getenv("LLARP_DEBUG");
if(var && *var == '1')
struct llarp_main *
llarp_main_init(const char *fname, bool multiProcess)
{
cSetLogLevel(eLogDebug);
}
llarp_main *m = new llarp_main;
m->ctx.reset(new llarp::Context());
m->ctx->singleThreaded = !multiProcess;
if(!m->ctx->LoadConfig(fname))
{
m->ctx->Close();
delete m;
return nullptr;
}
return m;
}
void
llarp_main_signal(struct llarp_main *ptr, int sig)
{
ptr->ctx->HandleSignal(sig);
}
int
llarp_main_setup(struct llarp_main *ptr)
{
return ptr->ctx->Setup();
}
int
llarp_main_run(struct llarp_main *ptr)
{
return ptr->ctx->Run();
}
void
llarp_main_abort(struct llarp_main *ptr)
{
llarp_logic_stop_timer(ptr->ctx->router->logic);
}
int
llarp_main_loadDatabase(struct llarp_main *ptr)
{
return ptr->ctx->LoadDatabase();
}
int
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
{
return ptr->ctx->IterateDatabase(i);
}
bool
llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc)
{
return ptr->ctx->PutDatabase(rc);
}
struct llarp_rc *
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
{
return ptr->ctx->GetDatabase(pk);
}
struct llarp_rc *
llarp_main_getLocalRC(struct llarp_main *ptr)
{
//
/*
llarp_config_iterator iter;
iter.user = this;
iter.visit = &iter_config;
llarp_config_iter(ctx->config, &iter);
*/
llarp_rc *rc = new llarp_rc;
llarp_rc_new(rc);
llarp::LogInfo("Loading ", ptr->ctx->conatctFile);
if(llarp_rc_read(ptr->ctx->conatctFile, rc))
return rc;
else
return nullptr;
}
void
llarp_main_checkOnline(void *u, uint64_t orig, uint64_t left)
{
// llarp::LogInfo("checkOnline - check ", left);
if(left)
return;
struct check_online_request *request =
static_cast< struct check_online_request * >(u);
// llarp::LogDebug("checkOnline - running");
// llarp::LogInfo("checkOnline - DHT nodes ",
// request->ptr->ctx->router->dht->impl.nodes->nodes.size());
request->online = false;
request->nodes = request->ptr->ctx->router->dht->impl.nodes->nodes.size();
if(request->ptr->ctx->router->dht->impl.nodes->nodes.size())
{
// llarp::LogInfo("checkOnline - Going to say we're online");
request->online = true;
}
request->hook(request);
// reschedue our self
llarp_main_queryDHT(request);
}
void
llarp_main_queryDHT_online(struct check_online_request *request)
{
// Info("llarp_main_queryDHT_online: ", request->online ? "online" :
// "offline");
if(request->online && !request->first)
{
request->first = true;
llarp::LogInfo("llarp_main_queryDHT_online - We're online");
llarp::LogInfo("llarp_main_queryDHT_online - Querying DHT");
llarp_dht_lookup_router(request->ptr->ctx->router->dht, request->job);
}
}
void
llarp_main_queryDHT(struct check_online_request *request)
{
// llarp::LogInfo("llarp_main_queryDHT - setting up timer");
request->hook = &llarp_main_queryDHT_online;
llarp_logic_call_later(request->ptr->ctx->router->logic,
{1000, request, &llarp_main_checkOnline});
// llarp_dht_lookup_router(ptr->ctx->router->dht, job);
}
void
llarp_main_free(struct llarp_main *ptr)
{
delete ptr;
}
const char *
handleBaseCmdLineArgs(int argc, char *argv[])
{
const char *conffname = "daemon.ini";
int c;
while(1)
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
if(!fname)
fname = "daemon.ini";
char *var = getenv("LLARP_DEBUG");
if(var && *var == '1')
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
if(strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5)) == 0)
{
cSetLogLevel(eLogDebug);
}
else if(strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogInfo);
}
else if(strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogWarn);
}
else if(strncmp(optarg, "error", MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
abort();
cSetLogLevel(eLogDebug);
}
llarp_main *m = new llarp_main;
m->ctx.reset(new llarp::Context());
m->ctx->singleThreaded = !multiProcess;
if(!m->ctx->LoadConfig(fname))
{
m->ctx->Close();
delete m;
return nullptr;
}
return m;
}
void
llarp_main_signal(struct llarp_main *ptr, int sig)
{
ptr->ctx->HandleSignal(sig);
}
int
llarp_main_setup(struct llarp_main *ptr)
{
return ptr->ctx->Setup();
}
int
llarp_main_run(struct llarp_main *ptr)
{
return ptr->ctx->Run();
}
void
llarp_main_abort(struct llarp_main *ptr)
{
llarp_logic_stop_timer(ptr->ctx->router->logic);
}
int
llarp_main_loadDatabase(struct llarp_main *ptr)
{
return ptr->ctx->LoadDatabase();
}
int
llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i)
{
return ptr->ctx->IterateDatabase(i);
}
bool
llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc)
{
return ptr->ctx->PutDatabase(rc);
}
struct llarp_rc *
llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk)
{
return ptr->ctx->GetDatabase(pk);
}
struct llarp_rc *
llarp_main_getLocalRC(struct llarp_main *ptr)
{
//
/*
llarp_config_iterator iter;
iter.user = this;
iter.visit = &iter_config;
llarp_config_iter(ctx->config, &iter);
*/
llarp_rc *rc = new llarp_rc;
llarp_rc_new(rc);
llarp::LogInfo("Loading ", ptr->ctx->conatctFile);
if(llarp_rc_read(ptr->ctx->conatctFile, rc))
return rc;
else
return nullptr;
}
void
llarp_main_checkOnline(void *u, uint64_t orig, uint64_t left)
{
// llarp::LogInfo("checkOnline - check ", left);
if(left)
return;
struct check_online_request *request =
static_cast< struct check_online_request * >(u);
// llarp::LogDebug("checkOnline - running");
// llarp::LogInfo("checkOnline - DHT nodes ",
// request->ptr->ctx->router->dht->impl.nodes->nodes.size());
request->online = false;
request->nodes = request->ptr->ctx->router->dht->impl.nodes->nodes.size();
if(request->ptr->ctx->router->dht->impl.nodes->nodes.size())
{
// llarp::LogInfo("checkOnline - Going to say we're online");
request->online = true;
}
request->hook(request);
// reschedue our self
llarp_main_queryDHT(request);
}
void
llarp_main_queryDHT_online(struct check_online_request *request)
{
// Info("llarp_main_queryDHT_online: ", request->online ? "online" :
// "offline");
if(request->online && !request->first)
{
request->first = true;
llarp::LogInfo("llarp_main_queryDHT_online - We're online");
llarp::LogInfo("llarp_main_queryDHT_online - Querying DHT");
llarp_dht_lookup_router(request->ptr->ctx->router->dht, request->job);
}
}
return conffname;
void
llarp_main_queryDHT(struct check_online_request *request)
{
// llarp::LogInfo("llarp_main_queryDHT - setting up timer");
request->hook = &llarp_main_queryDHT_online;
llarp_logic_call_later(request->ptr->ctx->router->logic,
{1000, request, &llarp_main_checkOnline});
// llarp_dht_lookup_router(ptr->ctx->router->dht, job);
}
void
llarp_main_free(struct llarp_main *ptr)
{
delete ptr;
}
const char *
handleBaseCmdLineArgs(int argc, char *argv[])
{
const char *conffname = "daemon.ini";
int c;
while(1)
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
if(strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogDebug);
}
else if(strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogInfo);
}
else if(strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogWarn);
}
else if(strncmp(optarg, "error",
MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
break;
}
}
return conffname;
}
}

View File

@ -1,6 +1,7 @@
#include <assert.h>
#include <llarp/crypto.h>
#include <sodium.h>
#include <sodium/crypto_stream_xchacha20.h>
#include <llarp/crypto.hpp>
#include "mem.hpp"

View File

@ -35,6 +35,7 @@ namespace llarp
// bad msg size?
if(strbuf.sz != 1)
return false;
llarp::LogInfo("Handle DHT message ", *strbuf.base);
switch(*strbuf.base)
{
case 'F':

View File

@ -113,7 +113,7 @@ namespace llarp
// iterative or not?
if(!bencode_write_bytestring(buf, "I", 1))
return false;
if(!bencode_write_int(buf, iterative ? 1 : 0))
if(!bencode_write_uint64(buf, iterative ? 1 : 0))
return false;
// key

View File

@ -2,187 +2,190 @@
#include "dnsd.hpp" // for llarp_handle_dnsd_recvfrom, dnsc
#include "logger.hpp"
uint16_t
get16bits(const char *&buffer) throw()
extern "C"
{
uint16_t value = static_cast< unsigned char >(buffer[0]);
value = value << 8;
value += static_cast< unsigned char >(buffer[1]);
buffer += 2;
return value;
}
// uint32_t
uint32_t
get32bits(const char *&buffer) throw()
{
uint32_t value = uint32_t(
(unsigned char)(buffer[0]) << 24 | (unsigned char)(buffer[1]) << 16
| (unsigned char)(buffer[2]) << 8 | (unsigned char)(buffer[3]));
buffer += 4;
return value;
}
dns_msg_header *
decode_hdr(const char *buffer)
{
dns_msg_header *hdr = new dns_msg_header;
hdr->id = get16bits(buffer);
uint fields = get16bits(buffer);
uint8_t lFields = (fields & 0x00FF) >> 0;
uint8_t hFields = (fields & 0xFF00) >> 8;
// hdr->qr = fields & 0x8000;
hdr->qr = (hFields >> 7) & 0x1;
hdr->opcode = fields & 0x7800;
hdr->aa = fields & 0x0400;
hdr->tc = fields & 0x0200;
hdr->rd = fields & 0x0100;
hdr->ra = (lFields >> 7) & 0x1;
// hdr->z = (lFields >> 6) & 0x1;
// hdr->ad = (lFields >> 5) & 0x1;
// hdr->cd = (lFields >> 4) & 0x1;
hdr->rcode = lFields & 0xf;
hdr->qdCount = get16bits(buffer);
hdr->anCount = get16bits(buffer);
hdr->nsCount = get16bits(buffer);
hdr->arCount = get16bits(buffer);
return hdr;
}
dns_msg_question *
decode_question(const char *buffer)
{
dns_msg_question *question = new dns_msg_question;
std::string m_qName = "";
int length = *buffer++;
// llarp::LogInfo("qNamLen", length);
while(length != 0)
uint16_t
get16bits(const char *&buffer) throw()
{
for(int i = 0; i < length; i++)
uint16_t value = static_cast< unsigned char >(buffer[0]);
value = value << 8;
value += static_cast< unsigned char >(buffer[1]);
buffer += 2;
return value;
}
// uint32_t
uint32_t
get32bits(const char *&buffer) throw()
{
uint32_t value = uint32_t(
(unsigned char)(buffer[0]) << 24 | (unsigned char)(buffer[1]) << 16
| (unsigned char)(buffer[2]) << 8 | (unsigned char)(buffer[3]));
buffer += 4;
return value;
}
dns_msg_header *
decode_hdr(const char *buffer)
{
dns_msg_header *hdr = new dns_msg_header;
hdr->id = get16bits(buffer);
uint fields = get16bits(buffer);
uint8_t lFields = (fields & 0x00FF) >> 0;
uint8_t hFields = (fields & 0xFF00) >> 8;
// hdr->qr = fields & 0x8000;
hdr->qr = (hFields >> 7) & 0x1;
hdr->opcode = fields & 0x7800;
hdr->aa = fields & 0x0400;
hdr->tc = fields & 0x0200;
hdr->rd = fields & 0x0100;
hdr->ra = (lFields >> 7) & 0x1;
// hdr->z = (lFields >> 6) & 0x1;
// hdr->ad = (lFields >> 5) & 0x1;
// hdr->cd = (lFields >> 4) & 0x1;
hdr->rcode = lFields & 0xf;
hdr->qdCount = get16bits(buffer);
hdr->anCount = get16bits(buffer);
hdr->nsCount = get16bits(buffer);
hdr->arCount = get16bits(buffer);
return hdr;
}
dns_msg_question *
decode_question(const char *buffer)
{
dns_msg_question *question = new dns_msg_question;
std::string m_qName = "";
int length = *buffer++;
// llarp::LogInfo("qNamLen", length);
while(length != 0)
{
char c = *buffer++;
m_qName.append(1, c);
for(int i = 0; i < length; i++)
{
char c = *buffer++;
m_qName.append(1, c);
}
length = *buffer++;
if(length != 0)
m_qName.append(1, '.');
}
length = *buffer++;
if(length != 0)
m_qName.append(1, '.');
question->name = m_qName;
question->type = get16bits(buffer);
question->qClass = get16bits(buffer);
return question;
}
question->name = m_qName;
question->type = get16bits(buffer);
question->qClass = get16bits(buffer);
return question;
}
dns_msg_answer *
decode_answer(const char *buffer)
{
dns_msg_answer *answer = new dns_msg_answer;
answer->type = get16bits(buffer);
// assert(answer->type < 259);
if(answer->type > 259)
dns_msg_answer *
decode_answer(const char *buffer)
{
llarp::LogWarn("Answer type is off the charts");
}
answer->aClass = get16bits(buffer);
answer->ttl = get32bits(buffer);
answer->rdLen = get16bits(buffer);
if(answer->rdLen == 4)
{
answer->rData = new uint8_t[answer->rdLen];
memcpy(answer->rData, buffer, answer->rdLen);
}
else
{
llarp::LogWarn("Unknown Type ", answer->type);
}
return answer;
}
void
put16bits(char *&buffer, uint16_t value) throw()
{
buffer[0] = (value & 0xFF00) >> 8;
buffer[1] = value & 0xFF;
buffer += 2;
}
void
put32bits(char *&buffer, uint32_t value) throw()
{
buffer[0] = (value & 0xFF000000) >> 24;
buffer[1] = (value & 0x00FF0000) >> 16;
buffer[2] = (value & 0x0000FF00) >> 8;
buffer[3] = (value & 0x000000FF) >> 0;
buffer += 4;
}
void
code_domain(char *&buffer, const std::string &domain) throw()
{
int start(0), end; // indexes
// llarp::LogInfo("domain [", domain, "]");
while((end = domain.find('.', start)) != std::string::npos)
{
*buffer++ = end - start; // label length octet
for(int i = start; i < end; i++)
dns_msg_answer *answer = new dns_msg_answer;
answer->type = get16bits(buffer);
// assert(answer->type < 259);
if(answer->type > 259)
{
*buffer++ = domain[i]; // label octets
llarp::LogWarn("Answer type is off the charts");
}
answer->aClass = get16bits(buffer);
answer->ttl = get32bits(buffer);
answer->rdLen = get16bits(buffer);
if(answer->rdLen == 4)
{
answer->rData = new uint8_t[answer->rdLen];
memcpy(answer->rData, buffer, answer->rdLen);
}
else
{
llarp::LogWarn("Unknown Type ", answer->type);
}
return answer;
}
void
put16bits(char *&buffer, uint16_t value) throw()
{
buffer[0] = (value & 0xFF00) >> 8;
buffer[1] = value & 0xFF;
buffer += 2;
}
void
put32bits(char *&buffer, uint32_t value) throw()
{
buffer[0] = (value & 0xFF000000) >> 24;
buffer[1] = (value & 0x00FF0000) >> 16;
buffer[2] = (value & 0x0000FF00) >> 8;
buffer[3] = (value & 0x000000FF) >> 0;
buffer += 4;
}
void
code_domain(char *&buffer, const std::string &domain) throw()
{
int start(0), end; // indexes
// llarp::LogInfo("domain [", domain, "]");
while((end = domain.find('.', start)) != std::string::npos)
{
*buffer++ = end - start; // label length octet
for(int i = start; i < end; i++)
{
*buffer++ = domain[i]; // label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
}
start = end + 1; // Skip '.'
}
// llarp::LogInfo("start ", start, " domain size ", domain.size());
*buffer++ = domain.size() - start; // last label length octet
for(uint i = start; i < domain.size(); i++)
{
*buffer++ = domain[i]; // last label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
}
start = end + 1; // Skip '.'
*buffer++ = 0;
}
// llarp::LogInfo("start ", start, " domain size ", domain.size());
*buffer++ = domain.size() - start; // last label length octet
for(uint i = start; i < domain.size(); i++)
void
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
*buffer++ = domain[i]; // last label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
// castBuf += 12;
llarp::LogDebug("msg id ", hdr->id);
llarp::LogDebug("msg qr ", (uint8_t)hdr->qr);
if(hdr->qr)
{
llarp::LogDebug("handling as dnsc answer");
llarp_handle_dnsc_recvfrom(udp, saddr, buf, sz);
}
else
{
llarp::LogDebug("handling as dnsd question");
llarp_handle_dnsd_recvfrom(udp, saddr, buf, sz);
}
/*
llarp::LogInfo("msg op ", hdr->opcode);
llarp::LogInfo("msg rc ", hdr->rcode);
for(uint i = 0; i < hdr->qdCount; i++)
{
dns_msg_question *question = decode_question((const char*)castBuf);
llarp::LogInfo("Read a question");
castBuf += question->name.length() + 8;
}
for(uint i = 0; i < hdr->anCount; i++)
{
dns_msg_answer *answer = decode_answer((const char*)castBuf);
llarp::LogInfo("Read an answer");
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
}
*/
}
*buffer++ = 0;
}
void
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
// castBuf += 12;
llarp::LogDebug("msg id ", hdr->id);
llarp::LogDebug("msg qr ", (uint8_t)hdr->qr);
if(hdr->qr)
{
llarp::LogDebug("handling as dnsc answer");
llarp_handle_dnsc_recvfrom(udp, saddr, buf, sz);
}
else
{
llarp::LogDebug("handling as dnsd question");
llarp_handle_dnsd_recvfrom(udp, saddr, buf, sz);
}
/*
llarp::LogInfo("msg op ", hdr->opcode);
llarp::LogInfo("msg rc ", hdr->rcode);
for(uint i = 0; i < hdr->qdCount; i++)
{
dns_msg_question *question = decode_question((const char*)castBuf);
llarp::LogInfo("Read a question");
castBuf += question->name.length() + 8;
}
for(uint i = 0; i < hdr->anCount; i++)
{
dns_msg_answer *answer = decode_answer((const char*)castBuf);
llarp::LogInfo("Read an answer");
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
}
*/
}
}

View File

@ -1,9 +1,21 @@
#ifndef LIBLLARP_DNS_HPP
#define LIBLLARP_DNS_HPP
#include <llarp/dns.h>
#include <sys/types.h> // for uint & ssize_t
#include <map>
#include <string>
struct dns_tracker
{
// uint c_responses;
uint c_requests;
std::map< uint, dnsc_answer_request * > client_request;
// FIXME: support multiple dns server contexts
dnsd_context *dnsd;
// std::map< uint, dnsd_question_request * > daemon_request;
};
// protocol parsing/writing structures & functions
struct dns_msg_header
{
@ -43,33 +55,35 @@ struct dns_msg_answer
uint8_t *rData;
};
uint16_t
get16bits(const char *&buffer) throw();
extern "C"
{
uint16_t
get16bits(const char *&buffer) throw();
uint32_t
get32bits(const char *&buffer) throw();
uint32_t
get32bits(const char *&buffer) throw();
dns_msg_header *
decode_hdr(const char *buffer);
dns_msg_header *
decode_hdr(const char *buffer);
dns_msg_question *
decode_question(const char *buffer);
dns_msg_question *
decode_question(const char *buffer);
dns_msg_answer *
decode_answer(const char *buffer);
dns_msg_answer *
decode_answer(const char *buffer);
void
put16bits(char *&buffer, uint16_t value) throw();
void
put16bits(char *&buffer, uint16_t value) throw();
void
put32bits(char *&buffer, uint32_t value) throw();
void
put32bits(char *&buffer, uint32_t value) throw();
void
code_domain(char *&buffer, const std::string &domain) throw();
void
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz);
void
code_domain(char *&buffer, const std::string &domain) throw();
void
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz);
}
#endif

View File

@ -15,8 +15,8 @@
#include <cstdio>
#include <llarp/dns.h>
#include "llarp/net.hpp" // for llarp::Addr
#include "logger.hpp"
#include "net.hpp" // for llarp::Addr
// FIXME: make configurable
#define SERVER "8.8.8.8"
@ -115,7 +115,7 @@ raw_resolve_host(const char *url)
dnsQuery.request[11] = 0x00;
*/
//char *word;
// char *word;
unsigned int i;
llarp::LogDebug("Asking DNS server ", SERVER, " about ", url);
// dnsQuery.reqType = 0x01;
@ -148,19 +148,19 @@ raw_resolve_host(const char *url)
int rcode;
socklen_t size;
int ip = 0;
//int length;
// int length;
unsigned char buffer[DNC_BUF_SIZE];
// unsigned char tempBuf[3];
uint16_t QDCOUNT; // No. of items in Question Section
uint16_t ANCOUNT; // No. of items in Answer Section
uint16_t NSCOUNT; // No. of items in Authority Section
uint16_t ARCOUNT; // No. of items in Additional Section
//uint16_t QCLASS; // Specifies the class of the query
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
//uint16_t ACLASS; // Specifies the class of the data in the RDATA field
//uint32_t TTL; // The number of seconds the results can be cached
//uint16_t RDLENGTH; // The length of the RDATA field
//uint16_t MSGID;
uint16_t QDCOUNT; // No. of items in Question Section
uint16_t ANCOUNT; // No. of items in Answer Section
uint16_t NSCOUNT; // No. of items in Authority Section
uint16_t ARCOUNT; // No. of items in Additional Section
// uint16_t QCLASS; // Specifies the class of the query
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
// uint16_t ACLASS; // Specifies the class of the data in the RDATA field
// uint32_t TTL; // The number of seconds the results can be cached
// uint16_t RDLENGTH; // The length of the RDATA field
// uint16_t MSGID;
int sockfd;
@ -307,8 +307,8 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
// llarp::LogInfo("got a response, udp user is ", udp->user);
unsigned char *castBuf = (unsigned char *)buf;
//auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
llarp::LogDebug("Header got client responses for id: ", hdr->id);

View File

@ -2,8 +2,8 @@
#include <llarp/dns.h>
#include <string>
#include "ev.hpp"
#include "llarp/net.hpp"
#include "logger.hpp"
#include "net.hpp"
dns_tracker dns_udp_tracker;

View File

@ -35,9 +35,15 @@ llarp_ev_loop_free(struct llarp_ev_loop **ev)
}
int
llarp_ev_loop_run(struct llarp_ev_loop *ev)
llarp_ev_loop_run(struct llarp_ev_loop *ev, struct llarp_logic *logic)
{
return ev->run();
while(true)
{
if(ev->tick(100) == -1)
break;
llarp_logic_tick(logic);
}
return 0;
}
void

View File

@ -7,8 +7,8 @@
#include <unistd.h>
#include <cstdio>
#include "ev.hpp"
#include "llarp/net.hpp"
#include "logger.hpp"
#include "net.hpp"
namespace llarp
{
@ -130,10 +130,10 @@ struct llarp_epoll_loop : public llarp_ev_loop
}
++idx;
}
for(auto& l : udp_listeners)
if(l->tick)
l->tick(l);
}
for(auto& l : udp_listeners)
if(l->tick)
l->tick(l);
return result;
}
@ -168,11 +168,10 @@ struct llarp_epoll_loop : public llarp_ev_loop
}
++idx;
}
for(auto& l : udp_listeners)
if(l->tick)
l->tick(l);
}
for(auto& l : udp_listeners)
if(l->tick)
l->tick(l);
} while(epollfd != -1);
return result;
}

View File

@ -24,9 +24,9 @@
// original upstream
#include <unistd.h>
#include <cstdio>
#include <llarp/net.hpp>
#include "ev.hpp"
#include "logger.hpp"
#include "net.hpp"
namespace llarp
{

View File

@ -17,6 +17,6 @@ namespace fs = std::experimental::filesystem;
// openbsd needs this
// linux gcc 7.2 needs this
namespace fs = cpp17::filesystem;
#endif // end mingw32
#endif // end mingw32
#endif // end LLARP_FS_HPP
#endif // end LLARP_FS_HPP

View File

@ -145,18 +145,18 @@ frame_state::got_frag(frame_header hdr, size_t sz)
uint64_t msgid;
byte_t fragno;
msgid = bufbe64toh(hdr.data());
memcpy(&fragno, hdr.data() + 8, 1);
msgid = bufbe64toh(hdr.data());
fragno = hdr.data()[8];
auto idItr = rxIDs.find(msgid);
if(idItr == rxIDs.end())
{
push_ackfor(msgid, 0);
push_ackfor(msgid, ~0);
return true;
}
auto itr = rx.find(idItr->second);
if(itr == rx.end())
{
push_ackfor(msgid, 0);
push_ackfor(msgid, ~0);
return true;
}
auto fragsize = itr->second->msginfo.fragsize();
@ -191,9 +191,6 @@ frame_state::push_ackfor(uint64_t id, uint32_t bitmask)
llarp::LogDebug("ACK for msgid=", id, " mask=", bitmask);
auto pkt = new sendbuf_t(12 + 6);
auto body_ptr = init_sendbuf(pkt, eACKS, 12, txflags);
// TODO: this assumes big endian
// memcpy(body_ptr, &id, 8);
// memcpy(body_ptr + 8, &bitmask, 4);
htobe64buf(body_ptr, id);
htobe32buf(body_ptr + 8, bitmask);
sendqueue.Put(pkt);
@ -284,22 +281,31 @@ frame_state::got_acks(frame_header hdr, size_t sz)
return true;
}
auto now = llarp_time_now_ms();
transit_message *msg = itr->second;
msg->ack(bitmask);
if(msg->completed())
if(bitmask == ~(0U))
{
llarp::LogDebug("message transmitted msgid=", msgid);
tx.erase(msgid);
delete msg;
}
else if(msg->should_resend_frags(llarp_time_now_ms()))
else
{
llarp::LogDebug("message ", msgid, " retransmit fragments");
msg->retransmit_frags(sendqueue, txflags);
}
msg->ack(bitmask);
if(msg->completed())
{
llarp::LogDebug("message transmitted msgid=", msgid);
tx.erase(msgid);
delete msg;
}
else if(msg->should_resend_frags(now))
{
llarp::LogDebug("message ", msgid, " retransmit fragments");
msg->retransmit_frags(sendqueue, txflags);
}
}
return true;
}

View File

@ -66,7 +66,6 @@ llarp_link::TickSessions()
if(itr->second->timedout(now))
{
itr->second->done();
delete itr->second;
itr = m_PendingSessions.erase(itr);
}
else
@ -157,8 +156,8 @@ void
llarp_link::put_session(const llarp::Addr& src, llarp_link_session* impl)
{
lock_t lock(m_sessions_Mutex);
m_sessions.insert(std::make_pair(src, impl));
impl->our_router = &router->rc;
m_sessions.insert(std::make_pair(src, impl));
}
void
@ -195,24 +194,13 @@ llarp_link::iterate_sessions(std::function< bool(llarp_link_session*) > visitor)
}
void
llarp_link::handle_logic_pump(void* user)
llarp_link::PumpLogic()
{
llarp_link* self = static_cast< llarp_link* >(user);
auto now = llarp_time_now_ms();
self->iterate_sessions([now](llarp_link_session* s) -> bool {
auto now = llarp_time_now_ms();
iterate_sessions([now](llarp_link_session* s) -> bool {
s->TickLogic(now);
return true;
});
self->pumpingLogic = false;
}
void
llarp_link::PumpLogic()
{
if(pumpingLogic)
return;
pumpingLogic = true;
llarp_logic_queue_job(logic, {this, &handle_logic_pump});
}
void
@ -225,8 +213,8 @@ llarp_link::RemoveSession(llarp_link_session* s)
UnmapAddr(s->addr);
s->done();
m_sessions.erase(itr);
delete s;
}
delete s;
}
uint8_t*

View File

@ -42,7 +42,6 @@ llarp_link_session::llarp_link_session(llarp_link *l, const byte_t *seckey,
crypto->encryption_keygen(eph_seckey);
llarp_rc_clear(&remote_router);
crypto->randbytes(token, 32);
llarp::LogInfo("session created");
frame.alive();
working.store(false);
createdAt = llarp_time_now_ms();
@ -110,8 +109,8 @@ send_keepalive(void *user)
// send frame after encrypting
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
self->now = llarp_time_now_ms();
self->lastKeepalive = self->now;
self->lastKeepalive = llarp_time_now_ms();
self->encrypt_frame_async_send(buf.base, buf.sz);
self->pump();
self->PumpCryptoOutbound();
@ -216,7 +215,6 @@ handle_generated_session_start(iwp_async_session_start *start)
if(llarp_ev_udp_sendto(link->udp, link->addr, start->buf, start->sz) == -1)
llarp::LogError("sendto failed");
link->EnterState(llarp_link_session::State::eSessionStartSent);
link->serv->remove_intro_from(link->addr);
link->working = false;
}
@ -247,8 +245,6 @@ handle_verify_introack(iwp_async_introack *introack)
{
// invalid signature
llarp::LogError("introack verify failed from ", link->addr);
link->serv->remove_intro_from(link->addr);
link->serv->RemoveSession(link);
return;
}
// cancel resend
@ -286,17 +282,6 @@ handle_establish_timeout(void *user, uint64_t orig, uint64_t left)
void
llarp_link_session::done()
{
auto logic = serv->logic;
if(establish_job_id)
{
llarp_logic_remove_call(logic, establish_job_id);
handle_establish_timeout(this, 0, 0);
}
if(intro_resend_job_id)
{
llarp_logic_remove_call(logic, intro_resend_job_id);
handle_introack_timeout(this, 0, 0);
}
}
void
@ -380,10 +365,8 @@ llarp_link_session::on_intro_ack(const void *buf, size_t sz)
{
// too big?
llarp::LogError("introack too big");
serv->RemoveSession(this);
return;
}
serv->put_intro_from(this);
// copy buffer so we own it
memcpy(workbuf, buf, sz);
// set intro ack parameters
@ -455,10 +438,6 @@ llarp_link_session::Tick(llarp_time_t now)
if(now - lastKeepalive > KEEP_ALIVE_INTERVAL)
send_keepalive(this);
}
if(state == eEstablished)
{
this->now = now;
}
return false;
}
@ -727,7 +706,6 @@ llarp_link_session::recv(const void *buf, size_t sz)
// frame_header hdr((byte_t *)buf);
// llarp::LogDebug("recv - message header type ", (int)hdr.msgtype());
now = llarp_time_now_ms();
switch(state)
{
case eInitial:
@ -809,11 +787,7 @@ llarp_link_session::encrypt_frame_async_send(const void *buf, size_t sz)
void
llarp_link_session::pump()
{
// llarp::LogInfo("session pump");
// TODO: in codel the timestamp may cause excssive drop when all the
// packets have a similar timestamp
bool flush = false;
now = llarp_time_now_ms();
llarp_buffer_t buf;
std::queue< sendbuf_t * > q;
frame.sendqueue.Process(q);

View File

@ -66,12 +66,14 @@ transit_message::ack(uint32_t bitmask)
bool
transit_message::should_send_ack(llarp_time_t now) const
{
if(msginfo.numfrags() == 0)
return true;
if(now < started)
return false;
if(msginfo.numfrags() == 0)
return true;
if(status.count() == 0)
return true;
if(now < lastRetransmit)
return false;
return now - lastRetransmit > 200;
}
@ -86,7 +88,7 @@ transit_message::should_resend_xmit(llarp_time_t now) const
bool
transit_message::should_resend_frags(llarp_time_t now) const
{
if(now < started)
if(now < started || now < lastAck)
return false;
return lastAck > 0 && now - lastAck > 500 && !completed();
}

View File

@ -4,6 +4,7 @@
#include "logger.hpp"
#include "router.hpp"
namespace llarp
{
LinkIntroMessage::~LinkIntroMessage()

View File

@ -72,9 +72,6 @@ namespace llarp
case 'c':
handler->msg = new LR_CommitMessage(handler->GetCurrentFrom());
break;
case 'z':
handler->msg = new DiscardMessage(handler->GetCurrentFrom());
break;
default:
return false;
}

View File

@ -8,7 +8,7 @@ llarp_init_logic()
llarp_logic* logic = new llarp_logic;
if(logic)
{
logic->thread = llarp_init_threadpool(1, "llarp-logic");
logic->thread = llarp_init_same_process_threadpool();
logic->timer = llarp_init_timer();
}
return logic;
@ -30,6 +30,7 @@ void
llarp_logic_tick(struct llarp_logic* logic)
{
llarp_timer_tick_all(logic->timer, logic->thread);
llarp_threadpool_tick(logic->thread);
}
void
@ -75,10 +76,8 @@ llarp_logic_mainloop(struct llarp_logic* logic)
void
llarp_logic_queue_job(struct llarp_logic* logic, struct llarp_thread_job job)
{
llarp_thread_job j;
j.user = job.user;
j.work = job.work;
llarp_threadpool_queue_job(logic->thread, j);
if(job.user && job.work)
llarp_threadpool_queue_job(logic->thread, {job.user, job.work});
}
uint32_t

View File

@ -1,4 +1,4 @@
#include "net.hpp"
#include "llarp/net.hpp"
#include "str.hpp"
#ifdef ANDROID
#include "android/ifaddrs.h"
@ -63,21 +63,21 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr)
if(llarp::StrEq(i->ifa_name, ifname) && i->ifa_addr->sa_family == af)
{
// can't do this here
//llarp::Addr a(*i->ifa_addr);
//if(!a.isPrivate())
// llarp::Addr a(*i->ifa_addr);
// if(!a.isPrivate())
//{
// llarp::LogInfo(__FILE__, "found ", ifname, " af: ", af);
memcpy(addr, i->ifa_addr, sl);
if(af == AF_INET6)
{
// set scope id
sockaddr_in6* ip6addr = (sockaddr_in6*)addr;
ip6addr->sin6_scope_id = if_nametoindex(ifname);
ip6addr->sin6_flowinfo = 0;
}
found = true;
break;
// llarp::LogInfo(__FILE__, "found ", ifname, " af: ", af);
memcpy(addr, i->ifa_addr, sl);
if(af == AF_INET6)
{
// set scope id
sockaddr_in6* ip6addr = (sockaddr_in6*)addr;
ip6addr->sin6_scope_id = if_nametoindex(ifname);
ip6addr->sin6_flowinfo = 0;
}
found = true;
break;
}
//}
}
i = i->ifa_next;
@ -86,3 +86,37 @@ llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr)
freeifaddrs(ifa);
return found;
}
namespace llarp
{
bool
GetBestNetIF(std::string& ifname, int af)
{
ifaddrs* ifa = nullptr;
bool found = false;
if(getifaddrs(&ifa) == -1)
return false;
ifaddrs* i = ifa;
while(i)
{
if(i->ifa_addr)
{
if(i->ifa_addr->sa_family == af)
{
llarp::Addr a(*i->ifa_addr);
if(!(a.isPrivate() || a.isLoopback()))
{
ifname = i->ifa_name;
found = true;
break;
}
}
}
i = i->ifa_next;
}
if(ifa)
freeifaddrs(ifa);
return found;
}
} // namespace llarp

View File

@ -450,8 +450,11 @@ llarp_nodedb_select_random_hop(struct llarp_nodedb *n, struct llarp_rc *prev,
}
if(memcmp(prev->pubkey, itr->second.pubkey, PUBKEYSIZE) == 0)
continue;
llarp_rc_copy(result, &itr->second);
return;
if(itr->second.addrs && llarp_ai_list_size(itr->second.addrs))
{
llarp_rc_copy(result, &itr->second);
return;
}
} while(true);
}
else

View File

@ -5,6 +5,7 @@
#include "buffer.hpp"
#include "router.hpp"
namespace llarp
{
namespace path
@ -356,9 +357,11 @@ namespace llarp
Path::HandleUpstream(llarp_buffer_t buf, const TunnelNonce& Y,
llarp_router* r)
{
TunnelNonce n = Y;
for(const auto& hop : hops)
{
r->crypto.xchacha20(buf, hop.shared, Y);
r->crypto.xchacha20(buf, hop.shared, n);
n ^= hop.nonceXOR;
}
RelayUpstreamMessage* msg = new RelayUpstreamMessage;
msg->X = buf;
@ -382,9 +385,11 @@ namespace llarp
Path::HandleDownstream(llarp_buffer_t buf, const TunnelNonce& Y,
llarp_router* r)
{
TunnelNonce n = Y;
for(const auto& hop : hops)
{
r->crypto.xchacha20(buf, hop.shared, Y);
n ^= hop.nonceXOR;
r->crypto.xchacha20(buf, hop.shared, n);
}
return HandleRoutingMessage(buf, r);
}

View File

@ -2,6 +2,7 @@
#include <llarp/path.hpp>
#include <llarp/pathbuilder.hpp>
#include "buffer.hpp"
#include "router.hpp"
namespace llarp
@ -50,6 +51,8 @@ namespace llarp
abort();
return;
}
// generate nonceXOR valueself->hop->pathKey
ctx->crypto->shorthash(hop.nonceXOR, llarp::Buffer(hop.shared));
++ctx->idx;
bool isFarthestHop = ctx->idx == ctx->path->hops.size();

View File

@ -246,6 +246,9 @@ namespace llarp
delete self;
return;
}
// generate hash of hop key for nonce mutation
self->context->Crypto()->shorthash(self->hop->nonceXOR,
llarp::Buffer(self->hop->pathKey));
if(self->record.work
&& self->record.work->IsValid(self->context->Crypto()->shorthash))
{

View File

@ -2,15 +2,14 @@
#include <llarp/iwp.h>
#include <llarp/proto.h>
#include <llarp/link_message.hpp>
#include <llarp/messages/discard.hpp>
#include "llarp/iwp/establish_job.hpp"
#include "llarp/iwp/server.hpp"
#include "llarp/iwp/session.hpp"
#include "buffer.hpp"
#include "encode.hpp"
#include "llarp/net.hpp"
#include "logger.hpp"
#include "net.hpp"
#include "str.hpp"
#include <fstream>
@ -385,30 +384,6 @@ llarp_router::Tick()
paths.TickPaths();
}
bool
llarp_router::send_padded_message(llarp_link_session_iter *itr,
llarp_link_session *peer)
{
llarp_router *self = static_cast< llarp_router * >(itr->user);
llarp::RouterID remote;
remote = &peer->get_remote_router()->pubkey[0];
llarp::DiscardMessage msg(2000);
llarp_buffer_t buf =
llarp::StackBuffer< decltype(linkmsg_buffer) >(self->linkmsg_buffer);
if(!msg.BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
for(size_t idx = 0; idx < 5; ++idx)
{
peer->sendto(buf);
}
return true;
}
void
llarp_router::SendTo(llarp::RouterID remote, const llarp::ILinkMessage *msg,
@ -644,11 +619,13 @@ llarp_router::Run()
continue;
}
}
llarp::LogInfo("Loading Addr: ", a, " into our RC");
llarp_ai_list_pushback(rc.addrs, &addr);
else
{
llarp::LogInfo("Loading Addr: ", a, " into our RC");
llarp_ai_list_pushback(rc.addrs, &addr);
}
};
if(this->publicOverride && !publicFound)
if(this->publicOverride)
{
// llarp::LogWarn("Need to load our public IP into RC!");

View File

@ -39,7 +39,6 @@ namespace llarp
if(strbuf.sz != 1)
return false;
self->key = *strbuf.cur;
llarp::LogInfo("handle messgae ", self->key);
switch(self->key)
{
case 'L':

View File

@ -1,5 +1,6 @@
#include <llarp/service.hpp>
#include "buffer.hpp"
#include "fs.hpp"
#include "ini.hpp"
#include "router.hpp"
@ -157,14 +158,32 @@ namespace llarp
Identity::BEncode(llarp_buffer_t* buf) const
{
/// TODO: implement me
return false;
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictEntry("e", enckey, buf))
return false;
if(!BEncodeWriteDictEntry("s", signkey, buf))
return false;
if(!BEncodeWriteDictInt("v", version, buf))
return false;
if(!BEncodeWriteDictEntry("x", vanity, buf))
return false;
return bencode_end(buf);
}
bool
Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
{
/// TODO: implement me
return false;
bool read = false;
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, buf))
return false;
return read;
}
void
@ -175,13 +194,48 @@ namespace llarp
pub.enckey = llarp::seckey_topublic(enckey);
pub.signkey = llarp::seckey_topublic(signkey);
pub.vanity.Zero();
pub.UpdateAddr();
}
bool
Identity::EnsureKeys(const std::string& fname, llarp_crypto* c)
{
// TODO: implement me
return false;
byte_t tmp[256];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
std::error_code ec;
// check for file
if(!fs::exists(fname, ec))
{
if(ec)
{
llarp::LogError(ec);
return false;
}
// regen and encode
RegenerateKeys(c);
if(!BEncode(&buf))
return false;
// rewind
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
// write
std::ofstream f;
f.open(fname, std::ios::binary);
if(!f.is_open())
return false;
f.write((char*)buf.cur, buf.sz);
}
// read file
std::ifstream inf(fname, std::ios::binary);
inf.seekg(0, std::ios::end);
size_t sz = inf.tellg();
inf.seekg(0, std::ios::beg);
if(sz > sizeof(tmp))
return false;
// decode
inf.read((char*)buf.base, sz);
return BDecode(&buf);
}
bool
@ -221,7 +275,6 @@ namespace llarp
bool
Config::Load(const std::string& fname)
{
// TODO: implement me
ini::Parser parser(fname);
for(const auto& sec : parser.top().ordered_sections)
{

View File

@ -48,18 +48,16 @@ namespace llarp
{
llarp::LogError("failed to set ", k, "=", v,
" for hidden service endpoint ", conf.first);
delete service;
return false;
}
}
if(service->Start())
{
llarp::LogInfo("added hidden service endpoint ", conf.first);
llarp::LogInfo("added hidden service endpoint ", service->Name());
m_Endpoints.insert(std::make_pair(conf.first, service));
return true;
}
llarp::LogError("failed to start hidden service endpoint ", conf.first);
delete service;
return false;
}
} // namespace service

View File

@ -63,6 +63,9 @@ namespace llarp
void
Endpoint::Tick(llarp_time_t now)
{
/// reset tx id for publish
if(now - m_LastPublishAttempt >= INTROSET_PUBLISH_RETRY_INTERVAL)
m_CurrentPublishTX = 0;
// publish descriptors
if(ShouldPublishDescriptors(now))
{

View File

@ -40,8 +40,8 @@ namespace llarp
lock, [this] { return this->stop || !this->jobs.empty(); });
if(this->stop && this->jobs.empty())
return;
job = this->jobs.front();
this->jobs.pop_front();
job = this->jobs.top().job;
this->jobs.pop();
}
// do work
job->work(job->user);
@ -81,7 +81,7 @@ namespace llarp
if(stop)
return;
jobs.push_back(new llarp_thread_job(job.user, job.work));
jobs.emplace(ids++, new llarp_thread_job(job.user, job.work));
}
condition.notify_one();
}
@ -93,7 +93,7 @@ struct llarp_threadpool
{
llarp::thread::Pool *impl;
std::queue< llarp_thread_job > jobs;
std::queue< llarp_thread_job * > jobs;
llarp_threadpool(int workers, const char *name)
: impl(new llarp::thread::Pool(workers, name))
@ -160,7 +160,7 @@ llarp_threadpool_queue_job(struct llarp_threadpool *pool,
if(pool->impl)
pool->impl->QueueJob(job);
else
pool->jobs.push(job);
pool->jobs.push(new llarp_thread_job(job));
}
void
@ -169,7 +169,9 @@ llarp_threadpool_tick(struct llarp_threadpool *pool)
while(pool->jobs.size())
{
auto &job = pool->jobs.front();
job.work(job.user);
if(job && job->work && job->user)
job->work(job->user);
delete job;
pool->jobs.pop();
}
}

View File

@ -4,7 +4,7 @@
#include <llarp/threadpool.h>
#include <llarp/threading.hpp>
#include <deque>
#include <queue>
#include <thread>
#include <vector>
@ -27,8 +27,24 @@ namespace llarp
void
Stop();
std::vector< std::thread > threads;
std::deque< llarp_thread_job* > jobs;
struct Job_t
{
uint32_t id;
llarp_thread_job* job;
Job_t(uint32_t jobid, llarp_thread_job* j) : id(jobid), job(j)
{
}
bool
operator<(const Job_t& j) const
{
return id < j.id;
}
};
std::priority_queue< Job_t > jobs;
uint32_t ids = 0;
mtx_t queue_mutex;
std::condition_variable condition;
std::condition_variable done;

View File

@ -3,6 +3,7 @@
#include <atomic>
#include <condition_variable>
#include <list>
#include <queue>
#include <unordered_map>
#include "logger.hpp"
@ -44,10 +45,10 @@ namespace llarp
static_cast< timer* >(user)->exec();
}
void
send_job(llarp_threadpool* pool)
bool
operator<(const timer& other) const
{
llarp_threadpool_queue_job(pool, {this, timer::call});
return (started + timeout) < (other.started + other.timeout);
}
};
}; // namespace llarp
@ -56,6 +57,7 @@ struct llarp_timer_context
{
std::mutex timersMutex;
std::unordered_map< uint32_t, llarp::timer* > timers;
std::priority_queue< llarp::timer* > calling;
std::mutex tickerMutex;
std::condition_variable* ticker = nullptr;
std::chrono::milliseconds nextTickLen = std::chrono::milliseconds(100);
@ -193,21 +195,14 @@ llarp_timer_tick_all(struct llarp_timer_context* t,
{
// timer hit
itr->second->called_at = now;
itr->second->send_job(pool);
++itr;
}
else if(itr->second->done)
{
// remove timer
itr->second->exec();
llarp::timer* timer = itr->second;
itr = t->timers.erase(itr);
delete timer;
continue;
}
else
++itr;
}
else // timer not hit yet
++itr;
++itr;
}
}

View File

@ -66,8 +66,6 @@ namespace llarp
buf.sz = MESSAGE_PAD_SIZE;
}
buf.cur = buf.base;
llarp::LogInfo("Send ", buf.sz,
" bytes routing message from trasnit hop");
return HandleDownstream(buf, N, r);
}
@ -77,8 +75,7 @@ namespace llarp
{
RelayDownstreamMessage* msg = new RelayDownstreamMessage;
msg->pathid = info.rxID;
msg->Y = Y;
msg->Y = Y ^ nonceXOR;
r->crypto.xchacha20(buf, pathKey, Y);
msg->X = buf;
llarp::LogDebug("relay ", msg->X.size(), " bytes downstream from ",
@ -99,7 +96,7 @@ namespace llarp
{
RelayUpstreamMessage* msg = new RelayUpstreamMessage;
msg->pathid = info.txID;
msg->Y = Y;
msg->Y = Y ^ nonceXOR;
msg->X = buf;
llarp::LogDebug("relay ", msg->X.size(), " bytes upstream from ",

View File

@ -7,16 +7,16 @@ And you can read the LLARP protocol specification [here](doc/proto_v0.txt)
## Building
You have 2 ways the build this project
### Recommended Method (for stable builds)
$ sudo apt install build-essential libtool autoconf cmake git python3-venv
$ git clone --recursive https://github.com/majestrate/llarpd-builder
$ cd llarpd-builder
$ sudo apt install build-essential libtool autoconf cmake git
$ git clone --recursive https://github.com/loki-project/lokinet-builder
$ cd lokinet-builder
$ make
### Development build method
## Running
$ ./lokinet
### Development
Please note development builds are likely to be unstable
@ -30,18 +30,4 @@ Build requirements:
Building a debug build:
$ make
## Running
Right now the reference daemon connects to nodes you tell it to and that's it.
If you built using the recommended way just run:
$ ./lokinet
It'll attempt to connect to a test node I run and keep the session alive.
That's it.
If you built using the dev build you are expected to configure the daemon yourself.
$ make