1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/router_contact.cpp

66 lines
2.1 KiB
C++
Raw Normal View History

2018-01-26 15:17:51 +01:00
#include "router_contact.hpp"
2018-04-04 17:19:11 +02:00
#include <llarp/bencode.h>
#include <llarp/version.h>
2018-04-05 16:43:16 +02:00
#include "address_info.hpp"
#include "exit_info.hpp"
2018-01-26 15:17:51 +01:00
2018-01-29 15:27:24 +01:00
namespace llarp {
2018-04-04 17:19:11 +02:00
2018-04-05 16:43:16 +02:00
static bool bencode_rc_ai_write(struct llarp_ai_list_iter *i,
struct llarp_ai *ai) {
llarp_buffer_t *buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_ai_bencode(ai, buff);
}
2018-04-04 17:19:11 +02:00
2018-04-05 16:43:16 +02:00
static bool bencode_rc_xi_write(struct llarp_xi_list_iter *i,
struct llarp_xi *xi) {
llarp_buffer_t *buff = static_cast<llarp_buffer_t *>(i->user);
return llarp_xi_bencode(xi, buff);
}
2018-02-01 14:21:00 +01:00
} // namespace llarp
2018-02-01 18:06:49 +01:00
extern "C" {
2018-04-08 14:18:16 +02:00
2018-04-30 18:14:20 +02:00
void llarp_rc_free(struct llarp_rc *rc) {
if (rc->exits) llarp_xi_list_free(rc->exits);
if (rc->addrs) llarp_ai_list_free(rc->addrs);
}
2018-02-01 18:06:49 +01:00
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
2018-04-04 17:19:11 +02:00
/* write dict begin */
2018-04-05 16:43:16 +02:00
if (!bencode_start_dict(buff)) return false;
if (rc->addrs) {
2018-04-04 17:19:11 +02:00
/* write ai if they exist */
2018-04-05 16:43:16 +02:00
if (!bencode_write_bytestring(buff, "a", 1)) return false;
if (!bencode_start_list(buff)) return false;
2018-04-04 17:19:11 +02:00
struct llarp_ai_list_iter ai_itr = {
2018-04-05 16:43:16 +02:00
.user = buff, .list = nullptr, .visit = &llarp::bencode_rc_ai_write};
2018-04-04 17:19:11 +02:00
llarp_ai_list_iterate(rc->addrs, &ai_itr);
2018-04-05 16:43:16 +02:00
if (!bencode_end(buff)) return false;
2018-04-04 17:19:11 +02:00
}
/* write pubkey */
2018-04-05 16:43:16 +02:00
if (!bencode_write_bytestring(buff, "k", 1)) return false;
if (!bencode_write_bytestring(buff, rc->pubkey, sizeof(llarp_pubkey_t)))
return false;
2018-04-04 17:19:11 +02:00
/* write version */
2018-04-05 16:43:16 +02:00
if (!bencode_write_version_entry(buff)) return false;
2018-04-04 17:19:11 +02:00
2018-04-05 16:43:16 +02:00
if (rc->exits) {
2018-04-04 17:19:11 +02:00
/* write ai if they exist */
2018-04-05 16:43:16 +02:00
if (!bencode_write_bytestring(buff, "x", 1)) return false;
if (!bencode_start_list(buff)) return false;
2018-04-04 17:19:11 +02:00
struct llarp_xi_list_iter xi_itr = {
2018-04-05 16:43:16 +02:00
.user = buff, .list = nullptr, .visit = &llarp::bencode_rc_xi_write};
2018-04-04 17:19:11 +02:00
llarp_xi_list_iterate(rc->exits, &xi_itr);
2018-04-05 16:43:16 +02:00
if (!bencode_end(buff)) return false;
2018-04-04 17:19:11 +02:00
}
/* write signature */
2018-04-05 16:43:16 +02:00
if (!bencode_write_bytestring(buff, "z", 1)) return false;
if (!bencode_write_bytestring(buff, rc->signature, sizeof(llarp_sig_t)))
return false;
2018-04-04 17:19:11 +02:00
return bencode_end(buff);
2018-02-01 18:06:49 +01:00
}
}