mirror of https://github.com/oxen-io/lokinet
fix leaks
This commit is contained in:
parent
71c34231b2
commit
d8631bb2b5
|
@ -205,7 +205,7 @@ main(int argc, char *argv[])
|
|||
llarp_crypto crypt;
|
||||
fs::path ident_keyfile = "identity.key";
|
||||
llarp_seckey_t identity;
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), &identity);
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
|
||||
// get identity public key
|
||||
uint8_t *pubkey = llarp_seckey_topublic(identity);
|
||||
llarp_rc_set_pubkey(&tmp, pubkey);
|
||||
|
@ -252,7 +252,7 @@ main(int argc, char *argv[])
|
|||
llarp_crypto crypt;
|
||||
fs::path ident_keyfile = "identity.key";
|
||||
llarp_seckey_t identity;
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), &identity);
|
||||
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
|
||||
// get identity public key
|
||||
uint8_t *pubkey = llarp_seckey_topublic(identity);
|
||||
llarp_rc_set_pubkey(&tmp, pubkey);
|
||||
|
|
|
@ -15,6 +15,9 @@ struct llarp_async_iwp *
|
|||
llarp_async_iwp_new(struct llarp_alloc *mem, struct llarp_crypto *crypto,
|
||||
struct llarp_logic *logic, struct llarp_threadpool *worker);
|
||||
|
||||
void
|
||||
llarp_async_iwp_free(struct llarp_async_iwp *iwp);
|
||||
|
||||
struct iwp_async_keygen;
|
||||
|
||||
typedef void (*iwp_keygen_hook)(struct iwp_async_keygen *);
|
||||
|
|
|
@ -16,10 +16,8 @@ extern "C" {
|
|||
struct llarp_router;
|
||||
|
||||
bool
|
||||
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *path,
|
||||
llarp_seckey_t *identity);
|
||||
bool
|
||||
llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file);
|
||||
llarp_findOrCreateIdentity(struct llarp_crypto *crypto, const char *path,
|
||||
byte_t *secretkey);
|
||||
|
||||
struct llarp_router *
|
||||
llarp_init_router(struct llarp_alloc *mem, struct llarp_threadpool *worker,
|
||||
|
@ -27,27 +25,12 @@ llarp_init_router(struct llarp_alloc *mem, struct llarp_threadpool *worker,
|
|||
void
|
||||
llarp_free_router(struct llarp_router **router);
|
||||
|
||||
void
|
||||
llarp_rc_clear(struct llarp_rc *rc);
|
||||
|
||||
bool
|
||||
llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai);
|
||||
|
||||
bool
|
||||
llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remtoe);
|
||||
|
||||
bool
|
||||
llarp_configure_router(struct llarp_router *router, struct llarp_config *conf);
|
||||
|
||||
void
|
||||
llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem,
|
||||
struct llarp_ai_list *addr);
|
||||
void
|
||||
llarp_rc_set_pubkey(struct llarp_rc *rc, uint8_t *pubkey);
|
||||
|
||||
void
|
||||
llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
|
||||
struct llarp_rc *rc);
|
||||
void
|
||||
llarp_run_router(struct llarp_router *router);
|
||||
|
||||
|
|
|
@ -33,6 +33,25 @@ llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc);
|
|||
void
|
||||
llarp_rc_copy(struct llarp_rc *dst, struct llarp_rc *src);
|
||||
|
||||
void
|
||||
llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem,
|
||||
struct llarp_ai_list *addr);
|
||||
void
|
||||
llarp_rc_set_pubkey(struct llarp_rc *rc, uint8_t *pubkey);
|
||||
|
||||
void
|
||||
llarp_rc_sign(struct llarp_crypto *crypto, const byte_t *seckey,
|
||||
struct llarp_rc *rc);
|
||||
|
||||
void
|
||||
llarp_rc_clear(struct llarp_rc *rc);
|
||||
|
||||
bool
|
||||
llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai);
|
||||
|
||||
bool
|
||||
llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -196,12 +196,7 @@ llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff)
|
|||
struct llarp_ai_list *
|
||||
llarp_ai_list_new(struct llarp_alloc *mem)
|
||||
{
|
||||
void *ptr = mem->alloc(mem, sizeof(struct llarp_ai_list), 8);
|
||||
if(ptr)
|
||||
{
|
||||
return new(ptr) llarp_ai_list(mem);
|
||||
}
|
||||
return nullptr;
|
||||
return new llarp_ai_list(mem);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -209,9 +204,8 @@ llarp_ai_list_free(struct llarp_ai_list *l)
|
|||
{
|
||||
if(l)
|
||||
{
|
||||
struct llarp_alloc *mem = l->mem;
|
||||
l->~llarp_ai_list();
|
||||
mem->free(mem, l);
|
||||
l->list.clear();
|
||||
delete l;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -224,9 +218,7 @@ llarp_ai_copy(struct llarp_ai *dst, struct llarp_ai *src)
|
|||
void
|
||||
llarp_ai_list_pushback(struct llarp_ai_list *l, struct llarp_ai *ai)
|
||||
{
|
||||
llarp_ai a;
|
||||
llarp_ai_copy(&a, ai);
|
||||
l->list.push_back(a);
|
||||
l->list.push_back(*ai);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -233,7 +233,7 @@ struct llarp_async_iwp *
|
|||
llarp_async_iwp_new(struct llarp_alloc *mem, struct llarp_crypto *crypto,
|
||||
struct llarp_logic *logic, struct llarp_threadpool *worker)
|
||||
{
|
||||
struct llarp_async_iwp *iwp = llarp::Alloc< llarp_async_iwp >(mem);
|
||||
llarp_async_iwp *iwp = new llarp_async_iwp;
|
||||
if(iwp)
|
||||
{
|
||||
iwp->mem = mem;
|
||||
|
@ -243,4 +243,10 @@ llarp_async_iwp_new(struct llarp_alloc *mem, struct llarp_crypto *crypto,
|
|||
}
|
||||
return iwp;
|
||||
}
|
||||
|
||||
void
|
||||
llarp_async_iwp_free(struct llarp_async_iwp *iwp)
|
||||
{
|
||||
delete iwp;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ llarp_ev_add_udp(struct llarp_ev_loop *ev, struct llarp_udp_io *udp)
|
|||
}
|
||||
|
||||
int
|
||||
llarp_ev_close_udp_close(struct llarp_udp_io *udp)
|
||||
llarp_ev_close_udp(struct llarp_udp_io *udp)
|
||||
{
|
||||
if(udp->parent->udp_close(udp))
|
||||
return 0;
|
||||
|
|
|
@ -166,10 +166,15 @@ struct llarp_epoll_loop : public llarp_ev_loop
|
|||
bool
|
||||
udp_close(llarp_udp_io* l)
|
||||
{
|
||||
bool ret = false;
|
||||
auto listener = static_cast< llarp::udp_listener* >(l->impl);
|
||||
if(!listener)
|
||||
return false;
|
||||
return close_ev(listener);
|
||||
if(listener)
|
||||
{
|
||||
ret = close_ev(listener);
|
||||
delete listener;
|
||||
l->impl = nullptr;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -403,7 +403,6 @@ namespace iwp
|
|||
|
||||
~session()
|
||||
{
|
||||
printf("~session()\n");
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -676,6 +675,11 @@ namespace iwp
|
|||
iwp = llarp_async_iwp_new(mem, crypto, logic, w);
|
||||
}
|
||||
|
||||
~server()
|
||||
{
|
||||
llarp_async_iwp_free(iwp);
|
||||
}
|
||||
|
||||
session *
|
||||
create_session(const llarp::Addr &src)
|
||||
{
|
||||
|
@ -963,6 +967,7 @@ namespace iwp
|
|||
link_free(struct llarp_link *l)
|
||||
{
|
||||
server *link = static_cast< server * >(l->impl);
|
||||
llarp_ev_close_udp(&link->udp);
|
||||
delete link;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,11 +20,13 @@ namespace llarp
|
|||
|
||||
llarp_router::llarp_router(struct llarp_alloc *m) : ready(false), mem(m)
|
||||
{
|
||||
llarp_rc_clear(&rc);
|
||||
llarp_msg_muxer_init(&muxer);
|
||||
}
|
||||
|
||||
llarp_router::~llarp_router()
|
||||
{
|
||||
llarp_rc_free(&rc);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -80,7 +82,7 @@ llarp_router::try_connect(fs::path rcfile)
|
|||
bool
|
||||
llarp_router::EnsureIdentity()
|
||||
{
|
||||
return llarp_findOrCreateIdentity(&crypto, ident_keyfile.c_str(), &identity);
|
||||
return llarp_findOrCreateIdentity(&crypto, ident_keyfile.c_str(), identity);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -135,12 +137,11 @@ void
|
|||
llarp_router::on_try_connect_result(llarp_link_establish_job *job)
|
||||
{
|
||||
printf("on_try_connect_result\n");
|
||||
llarp_router *self = static_cast< llarp_router * >(job->user);
|
||||
if(job->session)
|
||||
printf("session made\n");
|
||||
else
|
||||
printf("session not made\n");
|
||||
self->mem->free(self->mem, job);
|
||||
delete job;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -150,31 +151,22 @@ llarp_router::Run()
|
|||
llarp::Zero(&rc, sizeof(llarp_rc));
|
||||
// fill our address list
|
||||
rc.addrs = llarp_ai_list_new(mem);
|
||||
llarp_ai addr;
|
||||
for(auto link : links)
|
||||
{
|
||||
llarp_ai addr;
|
||||
link->get_our_address(link, &addr);
|
||||
llarp_ai_list_pushback(rc.addrs, &addr);
|
||||
};
|
||||
// set public key
|
||||
memcpy(rc.pubkey, pubkey(), 32);
|
||||
{
|
||||
// sign router contact
|
||||
byte_t rcbuf[MAX_RC_SIZE];
|
||||
llarp_buffer_t signbuf;
|
||||
llarp::StackBuffer< decltype(rcbuf) >(signbuf, rcbuf);
|
||||
// encode
|
||||
if(!llarp_rc_bencode(&rc, &signbuf))
|
||||
return;
|
||||
llarp_rc_set_pubkey(&rc, pubkey());
|
||||
|
||||
// sign
|
||||
signbuf.sz = signbuf.cur - signbuf.base;
|
||||
printf("sign %ld bytes\n", signbuf.sz);
|
||||
crypto.sign(rc.signature, identity, signbuf);
|
||||
}
|
||||
llarp_rc_sign(&crypto, identity, &rc);
|
||||
|
||||
if(!SaveRC())
|
||||
{
|
||||
printf("failed to save rc\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("saved router contact\n");
|
||||
// start links
|
||||
|
@ -202,9 +194,7 @@ llarp_router::iter_try_connect(llarp_router_link_iter *iter,
|
|||
if(!link)
|
||||
return false;
|
||||
|
||||
auto mem = router->mem;
|
||||
|
||||
llarp_link_establish_job *job = llarp::Alloc< llarp_link_establish_job >(mem);
|
||||
llarp_link_establish_job *job = new llarp_link_establish_job;
|
||||
|
||||
if(!job)
|
||||
return false;
|
||||
|
@ -311,23 +301,23 @@ llarp_rc_set_pubkey(struct llarp_rc *rc, uint8_t *pubkey)
|
|||
|
||||
bool
|
||||
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
|
||||
llarp_seckey_t *identity)
|
||||
byte_t *secretkey)
|
||||
{
|
||||
fs::path path(fpath);
|
||||
std::error_code ec;
|
||||
if(!fs::exists(path, ec))
|
||||
{
|
||||
crypto->keygen(*identity);
|
||||
crypto->keygen(secretkey);
|
||||
std::ofstream f(path, std::ios::binary);
|
||||
if(f.is_open())
|
||||
{
|
||||
f.write((char *)*identity, sizeof(identity));
|
||||
f.write((char *)secretkey, sizeof(llarp_seckey_t));
|
||||
}
|
||||
}
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if(f.is_open())
|
||||
{
|
||||
f.read((char *)*identity, sizeof(identity));
|
||||
f.read((char *)secretkey, sizeof(llarp_seckey_t));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -353,12 +343,12 @@ llarp_rc_write(struct llarp_rc *rc, const char *fpath)
|
|||
}
|
||||
|
||||
void
|
||||
llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
|
||||
struct llarp_rc *rc)
|
||||
llarp_rc_sign(llarp_crypto *crypto, const byte_t *seckey, struct llarp_rc *rc)
|
||||
{
|
||||
// sign router contact
|
||||
byte_t buf[MAX_RC_SIZE];
|
||||
auto signbuf = llarp::StackBuffer< decltype(buf) >(buf);
|
||||
// zero out previous signature
|
||||
llarp::Zero(rc->signature, sizeof(rc->signature));
|
||||
// encode
|
||||
if(llarp_rc_bencode(rc, &signbuf))
|
||||
{
|
||||
|
@ -366,7 +356,7 @@ llarp_rc_sign(llarp_crypto *crypto, llarp_seckey_t *identity,
|
|||
signbuf.sz = signbuf.cur - signbuf.base;
|
||||
printf("router.cpp::llarp_rc_sign - sized [%zu/%zu]\n", signbuf.sz,
|
||||
MAX_RC_SIZE);
|
||||
crypto->sign(rc->signature, *identity, signbuf);
|
||||
crypto->sign(rc->signature, seckey, signbuf);
|
||||
printf("router.cpp::llarp_rc_sign - signed\n");
|
||||
}
|
||||
}
|
||||
|
@ -426,8 +416,8 @@ namespace llarp
|
|||
struct llarp_link *link = nullptr;
|
||||
if(StrEq(section, "iwp-links"))
|
||||
{
|
||||
link = llarp::Alloc< llarp_link >(self->mem);
|
||||
llarp::Zero(link, sizeof(*link));
|
||||
link = new llarp_link;
|
||||
llarp::Zero(link, sizeof(llarp_link));
|
||||
|
||||
llarp_iwp_args args = {
|
||||
.mem = self->mem,
|
||||
|
@ -445,6 +435,7 @@ namespace llarp
|
|||
link->get_our_address(link, &ai);
|
||||
llarp::Addr addr = ai;
|
||||
printf("link %s bound to %s\n", key, addr.to_string().c_str());
|
||||
self->AddLink(link);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue