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

67 lines
2.0 KiB
C

#include <arpa/inet.h>
#include <llarp/address_info.h>
#include <llarp/bencode.h>
#include <llarp/mem.h>
#include <llarp/string.h>
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
char ipbuff[128] = {0};
const char *ipstr;
if (!bencode_start_dict(buff)) return false;
/* rank */
if (!bencode_write_bytestring(buff, "c", 1)) return false;
if (!bencode_write_uint16(buff, ai->rank)) return false;
/* dialect */
if (!bencode_write_bytestring(buff, "d", 1)) return false;
if (!bencode_write_bytestring(buff, ai->dialect,
strnlen(ai->dialect, sizeof(ai->dialect))))
return false;
/* encryption key */
if (!bencode_write_bytestring(buff, "e", 1)) return false;
if (!bencode_write_bytestring(buff, ai->enc_key, sizeof(llarp_pubkey_t)))
return false;
/** ip */
ipstr = inet_ntop(AF_INET6, &ai->ip, ipbuff, sizeof(ipbuff));
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;
/** port */
if (!bencode_write_bytestring(buff, "p", 1)) return false;
if (!bencode_write_uint16(buff, ai->port)) return false;
/** version */
if (!bencode_write_version_entry(buff)) return false;
/** end */
return bencode_end(buff);
}
struct llarp_ai_list {
struct llarp_ai_list *next;
struct llarp_ai data;
};
struct llarp_ai_list *llarp_ai_list_new() {
return llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8);
}
void llarp_ai_list_free(struct llarp_ai_list *l) {
struct llarp_ai_list *cur = l;
struct llarp_ai_list *tmp;
do {
tmp = cur->next;
llarp_g_mem.free(cur);
cur = tmp;
} while (cur->next);
}
void llarp_ai_list_iterate(struct llarp_ai_list *l,
struct llarp_ai_list_iter *itr) {
struct llarp_ai_list *cur = l;
itr->list = l;
do {
if (!itr->visit(itr, &cur->data)) return;
cur = cur->next;
} while (cur->next);
}