2018-04-05 14:43:16 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-04-04 16:10:27 +00:00
|
|
|
#include <llarp/bencode.h>
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <llarp/exit_info.h>
|
2018-05-10 23:32:46 +00:00
|
|
|
#include <llarp/mem.h>
|
2018-04-05 14:23:14 +00:00
|
|
|
#include <llarp/string.h>
|
2018-05-28 13:49:44 +00:00
|
|
|
#include <list>
|
2018-05-10 23:32:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_xi_list
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
std::list< llarp_xi > list;
|
2018-05-10 23:32:46 +00:00
|
|
|
};
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_xi_list *
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_xi_list_new()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
return new llarp_xi_list;
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_xi_list_free(struct llarp_xi_list *l)
|
|
|
|
{
|
|
|
|
if(l)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
delete l;
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
llarp_xi_iter_bencode(struct llarp_xi_list_iter *iter, struct llarp_xi *xi)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
return llarp_xi_bencode(xi, static_cast< llarp_buffer_t * >(iter->user));
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_xi_list_bencode(struct llarp_xi_list *l, llarp_buffer_t *buff)
|
|
|
|
{
|
|
|
|
if(!bencode_start_list(buff))
|
|
|
|
return false;
|
2018-05-28 13:49:44 +00:00
|
|
|
struct llarp_xi_list_iter xi_itr = {buff, nullptr, &llarp_xi_iter_bencode};
|
2018-05-10 23:32:46 +00:00
|
|
|
llarp_xi_list_iterate(l, &xi_itr);
|
|
|
|
return bencode_end(buff);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
llarp_xi_list_iterate(struct llarp_xi_list *l, struct llarp_xi_list_iter *iter)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
iter->list = l;
|
|
|
|
for(auto &item : l->list)
|
|
|
|
if(!iter->visit(iter, &item))
|
2018-05-22 15:54:19 +00:00
|
|
|
return;
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff)
|
|
|
|
{
|
2018-04-04 16:10:27 +00:00
|
|
|
char addr_buff[128] = {0};
|
2018-04-05 14:43:16 +00:00
|
|
|
const char *addr;
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!bencode_start_dict(buff))
|
|
|
|
return false;
|
2018-04-04 16:10:27 +00:00
|
|
|
|
|
|
|
/** address */
|
|
|
|
addr = inet_ntop(AF_INET6, &xi->address, addr_buff, sizeof(addr_buff));
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!addr)
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buff, "a", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
|
2018-04-05 14:43:16 +00:00
|
|
|
return false;
|
2018-04-04 16:10:27 +00:00
|
|
|
|
|
|
|
/** netmask */
|
|
|
|
addr = inet_ntop(AF_INET6, &xi->netmask, addr_buff, sizeof(addr_buff));
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!addr)
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buff, "b", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buff, addr, strnlen(addr, sizeof(addr_buff))))
|
2018-04-05 14:43:16 +00:00
|
|
|
return false;
|
2018-04-04 16:10:27 +00:00
|
|
|
|
|
|
|
/** public key */
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!bencode_write_bytestring(buff, "k", 1))
|
|
|
|
return false;
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!bencode_write_bytestring(buff, xi->pubkey, PUBKEYSIZE))
|
2018-04-05 14:43:16 +00:00
|
|
|
return false;
|
2018-04-04 16:10:27 +00:00
|
|
|
|
|
|
|
/** version */
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!bencode_write_version_entry(buff))
|
|
|
|
return false;
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-04-04 16:10:27 +00:00
|
|
|
return bencode_end(buff);
|
|
|
|
}
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
llarp_xi_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!key)
|
|
|
|
return true;
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_xi *xi = static_cast< llarp_xi * >(r->user);
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_buffer_t strbuf;
|
2018-05-22 18:41:38 +00:00
|
|
|
uint64_t v;
|
2018-05-22 15:54:19 +00:00
|
|
|
char tmp[128] = {0};
|
|
|
|
|
|
|
|
// address
|
|
|
|
if(llarp_buffer_eq(*key, "a"))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
if(strbuf.sz >= sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
memcpy(tmp, strbuf.base, strbuf.sz);
|
|
|
|
return inet_pton(AF_INET6, tmp, xi->address.s6_addr) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(llarp_buffer_eq(*key, "b"))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
if(strbuf.sz >= sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
memcpy(tmp, strbuf.base, strbuf.sz);
|
|
|
|
return inet_pton(AF_INET6, tmp, xi->netmask.s6_addr) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(llarp_buffer_eq(*key, "k"))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!bencode_read_string(r->buffer, &strbuf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-06-12 11:57:14 +00:00
|
|
|
if(strbuf.sz != PUBKEYSIZE)
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-06-12 11:57:14 +00:00
|
|
|
memcpy(xi->pubkey, strbuf.base, PUBKEYSIZE);
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(llarp_buffer_eq(*key, "v"))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!bencode_read_integer(r->buffer, &v))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
return v == LLARP_PROTO_VERSION;
|
|
|
|
}
|
|
|
|
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_xi_bdecode(struct llarp_xi *xi, llarp_buffer_t *buf)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
struct dict_reader r = {buf, xi, &llarp_xi_decode_dict};
|
2018-06-01 17:47:37 +00:00
|
|
|
return bencode_read_dict(buf, &r);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_xi_list_pushback(struct llarp_xi_list *l, struct llarp_xi *xi)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
l->list.emplace_back();
|
|
|
|
llarp_xi_copy(&l->list.back(), xi);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_xi_copy(struct llarp_xi *dst, struct llarp_xi *src)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, sizeof(struct llarp_xi));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
llarp_xi_list_decode_item(struct list_reader *r, bool more)
|
|
|
|
{
|
|
|
|
if(!more)
|
|
|
|
return true;
|
|
|
|
|
2018-05-28 13:49:44 +00:00
|
|
|
llarp_xi_list *l = static_cast< llarp_xi_list * >(r->user);
|
|
|
|
l->list.emplace_back();
|
|
|
|
return llarp_xi_bdecode(&l->list.back(), r->buffer);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 20:56:47 +00:00
|
|
|
void
|
|
|
|
llarp_xi_list_copy(struct llarp_xi_list *dst, struct llarp_xi_list *src)
|
|
|
|
{
|
|
|
|
dst->list.clear();
|
|
|
|
for(auto &itr : src->list)
|
|
|
|
dst->list.emplace_back(itr);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_xi_list_bdecode(struct llarp_xi_list *l, llarp_buffer_t *buff)
|
|
|
|
{
|
2018-05-28 13:49:44 +00:00
|
|
|
list_reader r = {buff, l, &llarp_xi_list_decode_item};
|
2018-06-01 17:47:37 +00:00
|
|
|
return bencode_read_list(buff, &r);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-28 13:49:44 +00:00
|
|
|
}
|