lokinet/llarp/address_info.c

104 lines
2.9 KiB
C
Raw Normal View History

2018-04-05 14:43:16 +00:00
#include <arpa/inet.h>
2018-04-04 16:10:27 +00:00
#include <llarp/address_info.h>
2018-04-04 15:19:11 +00:00
#include <llarp/bencode.h>
2018-04-04 16:10:27 +00:00
#include <llarp/mem.h>
2018-04-05 14:23:14 +00:00
#include <llarp/string.h>
2018-01-26 14:17:51 +00:00
2018-05-13 18:07:36 +00:00
bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff)
{
2018-05-16 15:53:28 +00:00
return false;
2018-05-13 18:07:36 +00:00
}
2018-01-29 14:27:24 +00:00
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
2018-04-05 14:43:16 +00:00
char ipbuff[128] = {0};
const char *ipstr;
if (!bencode_start_dict(buff)) return false;
2018-04-04 15:19:11 +00:00
/* rank */
2018-04-05 14:43:16 +00:00
if (!bencode_write_bytestring(buff, "c", 1)) return false;
if (!bencode_write_uint16(buff, ai->rank)) return false;
2018-04-04 15:19:11 +00:00
/* dialect */
2018-04-05 14:43:16 +00:00
if (!bencode_write_bytestring(buff, "d", 1)) return false;
if (!bencode_write_bytestring(buff, ai->dialect,
strnlen(ai->dialect, sizeof(ai->dialect))))
return false;
2018-04-04 15:19:11 +00:00
/* encryption key */
2018-04-05 14:43:16 +00:00
if (!bencode_write_bytestring(buff, "e", 1)) return false;
if (!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t)))
return false;
2018-04-04 15:19:11 +00:00
/** ip */
2018-04-04 16:10:27 +00:00
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
2018-04-05 14:43:16 +00:00
if (!ipstr) return false;
if (!bencode_write_bytestring(buff, "i", 1)) return false;
if (!bencode_write_bytestring(buff, ipstr, strnlen(ipstr, sizeof(ipbuff))))
return false;
2018-04-04 15:19:11 +00:00
/** port */
2018-04-05 14:43:16 +00:00
if (!bencode_write_bytestring(buff, "p", 1)) return false;
if (!bencode_write_uint16(buff, ai->port)) return false;
2018-04-04 15:19:11 +00:00
/** version */
2018-04-05 14:43:16 +00:00
if (!bencode_write_version_entry(buff)) return false;
2018-04-04 15:19:11 +00:00
/** end */
return bencode_end(buff);
2018-01-29 14:27:24 +00:00
}
2018-05-10 23:32:46 +00:00
static bool llarp_ai_list_iter_bencode(struct llarp_ai_list_iter *iter,
struct llarp_ai *ai) {
return llarp_ai_bencode(ai, iter->user);
}
bool llarp_ai_list_bencode(struct llarp_ai_list *l, llarp_buffer_t *buff) {
if (!bencode_start_list(buff)) return false;
struct llarp_ai_list_iter ai_itr = {.user = buff,
.visit = &llarp_ai_list_iter_bencode};
llarp_ai_list_iterate(l, &ai_itr);
return bencode_end(buff);
}
struct llarp_ai_list_node {
2018-04-04 16:10:27 +00:00
struct llarp_ai data;
2018-05-10 23:32:46 +00:00
struct llarp_ai_list_node *next;
};
struct llarp_ai_list {
2018-05-16 18:13:18 +00:00
struct llarp_alloc * mem;
2018-05-10 23:32:46 +00:00
struct llarp_ai_list_node *root;
2018-04-04 16:10:27 +00:00
};
2018-05-16 18:13:18 +00:00
struct llarp_ai_list *llarp_ai_list_new(struct llarp_alloc * mem) {
struct llarp_ai_list *l = mem->alloc(mem, sizeof(struct llarp_ai_list), 8);
2018-05-10 23:32:46 +00:00
if (l) {
2018-05-16 18:13:18 +00:00
l->mem = mem;
2018-05-10 23:32:46 +00:00
l->root = NULL;
}
return l;
2018-04-04 16:10:27 +00:00
}
2018-05-10 23:32:46 +00:00
void llarp_ai_list_free(struct llarp_ai_list **l) {
if (*l) {
2018-05-16 18:13:18 +00:00
struct llarp_alloc * mem = (*l)->mem;
2018-05-10 23:32:46 +00:00
struct llarp_ai_list_node *cur = (*l)->root;
while (cur) {
struct llarp_ai_list_node *tmp = cur->next;
2018-05-16 18:13:18 +00:00
mem->free(mem, cur);
2018-05-10 23:32:46 +00:00
cur = tmp;
}
2018-05-16 18:13:18 +00:00
mem->free(mem, *l);
2018-05-10 23:32:46 +00:00
*l = NULL;
}
2018-04-04 16:10:27 +00:00
}
2018-01-29 14:27:24 +00:00
void llarp_ai_list_iterate(struct llarp_ai_list *l,
struct llarp_ai_list_iter *itr) {
2018-05-10 23:32:46 +00:00
struct llarp_ai_list_node *cur = l->root;
2018-04-04 16:10:27 +00:00
itr->list = l;
2018-05-10 23:32:46 +00:00
while (cur) {
2018-04-04 16:10:27 +00:00
if (!itr->visit(itr, &cur->data)) return;
2018-01-29 14:27:24 +00:00
cur = cur->next;
2018-05-10 23:32:46 +00:00
};
2018-01-29 14:27:24 +00:00
}
2018-05-13 18:07:36 +00:00
bool llarp_ai_list_bdecode(struct llarp_ai_list * l, llarp_buffer_t * buff)
{
return false;
}