You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/router_contact.cpp

226 lines
4.8 KiB
C++

6 years ago
#include <llarp/bencode.h>
#include <llarp/router_contact.h>
#include <llarp/version.h>
#include <llarp/crypto.hpp>
#include "buffer.hpp"
#include "logger.hpp"
extern "C" {
void
llarp_rc_free(struct llarp_rc *rc)
{
if(rc->exits)
llarp_xi_list_free(rc->exits);
if(rc->addrs)
llarp_ai_list_free(rc->addrs);
rc->exits = 0;
rc->addrs = 0;
6 years ago
}
6 years ago
struct llarp_rc_decoder
{
struct llarp_rc *rc;
struct llarp_alloc *mem;
6 years ago
};
6 years ago
static bool
llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
6 years ago
{
uint64_t v;
6 years ago
llarp_buffer_t strbuf;
llarp_rc *rc = static_cast< llarp_rc * >(r->user);
6 years ago
if(!key)
return true;
6 years ago
if(llarp_buffer_eq(*key, "a"))
{
if(rc->addrs)
{
llarp_ai_list_free(rc->addrs);
}
rc->addrs = llarp_ai_list_new();
6 years ago
return llarp_ai_list_bdecode(rc->addrs, r->buffer);
}
if(llarp_buffer_eq(*key, "k"))
{
if(!bencode_read_string(r->buffer, &strbuf))
return false;
if(strbuf.sz != PUBKEYSIZE)
return false;
memcpy(rc->pubkey, strbuf.base, PUBKEYSIZE);
return true;
}
if(llarp_buffer_eq(*key, "p"))
6 years ago
{
if(!bencode_read_string(r->buffer, &strbuf))
6 years ago
return false;
if(strbuf.sz != PUBKEYSIZE)
6 years ago
return false;
memcpy(rc->enckey, strbuf.base, PUBKEYSIZE);
6 years ago
return true;
}
6 years ago
if(llarp_buffer_eq(*key, "u"))
{
if(!bencode_read_integer(r->buffer, &rc->last_updated))
return false;
return true;
}
6 years ago
if(llarp_buffer_eq(*key, "v"))
{
if(!bencode_read_integer(r->buffer, &v))
6 years ago
return false;
return v == LLARP_PROTO_VERSION;
}
if(llarp_buffer_eq(*key, "x"))
{
if(rc->exits)
{
llarp_xi_list_free(rc->exits);
}
rc->exits = llarp_xi_list_new();
6 years ago
return llarp_xi_list_bdecode(rc->exits, r->buffer);
}
if(llarp_buffer_eq(*key, "z"))
{
if(!bencode_read_string(r->buffer, &strbuf))
6 years ago
return false;
if(strbuf.sz != SIGSIZE)
6 years ago
return false;
memcpy(rc->signature, strbuf.base, SIGSIZE);
6 years ago
return true;
}
return false;
}
bool
llarp_rc_is_public_router(const struct llarp_rc *const rc)
{
return rc->addrs && llarp_ai_list_size(rc->addrs) > 0;
}
6 years ago
void
llarp_rc_copy(struct llarp_rc *dst, const struct llarp_rc *src)
6 years ago
{
llarp_rc_free(dst);
llarp_rc_clear(dst);
memcpy(dst->pubkey, src->pubkey, PUBKEYSIZE);
memcpy(dst->enckey, src->enckey, PUBKEYSIZE);
memcpy(dst->signature, src->signature, SIGSIZE);
6 years ago
dst->last_updated = src->last_updated;
if(src->addrs)
{
dst->addrs = llarp_ai_list_new();
llarp_ai_list_copy(dst->addrs, src->addrs);
}
if(src->exits)
{
dst->exits = llarp_xi_list_new();
llarp_xi_list_copy(dst->exits, src->exits);
}
}
bool
llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buff)
{
dict_reader r = {buff, rc, &llarp_rc_decode_dict};
return bencode_read_dict(buff, &r);
6 years ago
}
bool
llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
6 years ago
{
// maybe we should copy rc before modifying it
// would that make it more thread safe?
// jeff agrees
bool result = false;
llarp::Signature sig;
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
// copy sig
memcpy(sig, rc->signature, SIGSIZE);
// zero sig
size_t sz = 0;
while(sz < SIGSIZE)
rc->signature[sz++] = 0;
// bencode
if(llarp_rc_bencode(rc, &buf))
{
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
result = crypto->verify(rc->pubkey, buf, sig);
}
else
llarp::Warn("RC encode failed");
// restore sig
memcpy(rc->signature, sig, SIGSIZE);
return result;
6 years ago
}
bool
6 years ago
llarp_rc_bencode(const struct llarp_rc *rc, llarp_buffer_t *buff)
{
6 years ago
/* write dict begin */
if(!bencode_start_dict(buff))
return false;
if(rc->addrs)
{
6 years ago
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "a", 1))
return false;
if(!llarp_ai_list_bencode(rc->addrs, buff))
return false;
6 years ago
}
/* write signing pubkey */
if(!bencode_write_bytestring(buff, "k", 1))
return false;
if(!bencode_write_bytestring(buff, rc->pubkey, PUBKEYSIZE))
6 years ago
return false;
/* write encryption pubkey */
if(!bencode_write_bytestring(buff, "p", 1))
return false;
if(!bencode_write_bytestring(buff, rc->enckey, PUBKEYSIZE))
return false;
/* write last updated */
if(!bencode_write_bytestring(buff, "u", 1))
return false;
if(!bencode_write_uint64(buff, rc->last_updated))
return false;
6 years ago
/* write version */
if(!bencode_write_version_entry(buff))
return false;
6 years ago
if(rc->exits)
{
6 years ago
/* write ai if they exist */
if(!bencode_write_bytestring(buff, "x", 1))
return false;
if(!llarp_xi_list_bencode(rc->exits, buff))
return false;
6 years ago
}
/* write signature */
if(!bencode_write_bytestring(buff, "z", 1))
return false;
if(!bencode_write_bytestring(buff, rc->signature, SIGSIZE))
6 years ago
return false;
return bencode_end(buff);
}
}