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

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
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
struct llarp_ai_list {
struct llarp_ai_list *next;
7 years ago
struct llarp_ai data;
};
7 years ago
struct llarp_ai_list *llarp_ai_list_new() {
7 years ago
return llarp_g_mem.alloc(sizeof(struct llarp_ai_list), 8);
}
7 years ago
void llarp_ai_list_free(struct llarp_ai_list *l) {
struct llarp_ai_list *cur = l;
struct llarp_ai_list *tmp;
7 years ago
do {
tmp = cur->next;
llarp_g_mem.free(cur);
cur = tmp;
7 years ago
} while (cur->next);
7 years ago
}
7 years ago
void llarp_ai_list_iterate(struct llarp_ai_list *l,
struct llarp_ai_list_iter *itr) {
struct llarp_ai_list *cur = l;
7 years ago
itr->list = l;
7 years ago
do {
7 years ago
if (!itr->visit(itr, &cur->data)) return;
7 years ago
cur = cur->next;
} while (cur->next);
}