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/address_info.c

131 lines
3.5 KiB
C

7 years ago
#include <arpa/inet.h>
7 years ago
#include <llarp/address_info.h>
7 years ago
#include <llarp/bencode.h>
7 years ago
#include <llarp/mem.h>
7 years ago
#include <llarp/string.h>
7 years ago
6 years ago
bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff)
{
6 years ago
return false;
6 years ago
}
7 years ago
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
7 years ago
char ipbuff[128] = {0};
const char *ipstr;
if (!bencode_start_dict(buff)) return false;
7 years ago
/* rank */
7 years ago
if (!bencode_write_bytestring(buff, "c", 1)) return false;
if (!bencode_write_uint16(buff, ai->rank)) return false;
7 years ago
/* dialect */
7 years ago
if (!bencode_write_bytestring(buff, "d", 1)) return false;
if (!bencode_write_bytestring(buff, ai->dialect,
strnlen(ai->dialect, sizeof(ai->dialect))))
return false;
7 years ago
/* encryption key */
7 years ago
if (!bencode_write_bytestring(buff, "e", 1)) return false;
if (!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t)))
return false;
7 years ago
/** ip */
7 years ago
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
7 years ago
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;
7 years ago
/** port */
7 years ago
if (!bencode_write_bytestring(buff, "p", 1)) return false;
if (!bencode_write_uint16(buff, ai->port)) return false;
7 years ago
/** version */
7 years ago
if (!bencode_write_version_entry(buff)) return false;
7 years ago
/** end */
return bencode_end(buff);
7 years ago
}
7 years ago
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 {
7 years ago
struct llarp_ai data;
7 years ago
struct llarp_ai_list_node *next;
};
struct llarp_ai_list {
6 years ago
struct llarp_alloc * mem;
7 years ago
struct llarp_ai_list_node *root;
7 years ago
};
6 years ago
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);
7 years ago
if (l) {
6 years ago
l->mem = mem;
7 years ago
l->root = NULL;
}
return l;
7 years ago
}
7 years ago
void llarp_ai_list_free(struct llarp_ai_list **l) {
if (*l) {
6 years ago
struct llarp_alloc * mem = (*l)->mem;
7 years ago
struct llarp_ai_list_node *cur = (*l)->root;
while (cur) {
struct llarp_ai_list_node *tmp = cur->next;
6 years ago
mem->free(mem, cur);
7 years ago
cur = tmp;
}
6 years ago
mem->free(mem, *l);
7 years ago
*l = NULL;
}
7 years ago
}
void llarp_ai_copy(struct llarp_ai * dst, struct llarp_ai * src)
{
memcpy(dst, src, sizeof(struct llarp_ai));
}
void llarp_ai_list_pushback(struct llarp_ai_list * l, struct llarp_ai a)
{
struct llarp_ai_list_node *cur = l->root;
if(cur)
{
// go to the end of the list
while(cur->next)
cur = cur->next;
cur->next = l->mem->alloc(l->mem, sizeof(struct llarp_ai_list_node), 16);
cur = cur->next;
}
else
6 years ago
{
l->root = l->mem->alloc(l->mem, sizeof(struct llarp_ai_list_node), 16);
cur = l->root;
}
llarp_ai_copy(&cur->data, &a);
cur->next = 0;
}
7 years ago
void llarp_ai_list_iterate(struct llarp_ai_list *l,
struct llarp_ai_list_iter *itr) {
7 years ago
struct llarp_ai_list_node *cur = l->root;
7 years ago
itr->list = l;
7 years ago
while (cur) {
7 years ago
if (!itr->visit(itr, &cur->data)) return;
7 years ago
cur = cur->next;
7 years ago
};
7 years ago
}
6 years ago
bool llarp_ai_list_bdecode(struct llarp_ai_list * l, llarp_buffer_t * buff)
{
return false;
}