/* dht.h - BEP-5 KRPC codec and bounded IPv4 get_peers traversal. */ #ifndef NAUT_DHT_H #define NAUT_DHT_H #include "naut/common.h" #include "naut/tracker.h" #define NAUT_DHT_ID_LEN 20 #define NAUT_DHT_MAX_NODES 256 #define NAUT_DHT_MAX_PEERS 256 typedef struct { uint8_t id[NAUT_DHT_ID_LEN]; uint8_t ip[4]; uint16_t port; } naut_dht_node; typedef enum { NAUT_DHT_RESPONSE, NAUT_DHT_ERROR } naut_dht_message_type; typedef struct { naut_dht_message_type type; uint8_t transaction[8]; size_t transaction_len; uint8_t id[NAUT_DHT_ID_LEN]; bool has_id; uint8_t token[64]; size_t token_len; naut_dht_node *nodes; size_t num_nodes; naut_peer_addr *peers; size_t num_peers; int error_code; } naut_dht_response; naut_err naut_dht_build_ping(const uint8_t *tx, size_t tx_len, const uint8_t id[20], uint8_t **out, size_t *out_len); naut_err naut_dht_build_find_node(const uint8_t *tx, size_t tx_len, const uint8_t id[20], const uint8_t target[20], uint8_t **out, size_t *out_len); naut_err naut_dht_build_get_peers(const uint8_t *tx, size_t tx_len, const uint8_t id[20], const uint8_t info_hash[20], uint8_t **out, size_t *out_len); naut_err naut_dht_build_announce_peer(const uint8_t *tx, size_t tx_len, const uint8_t id[20], const uint8_t info_hash[20], uint16_t port, bool implied_port, const void *token, size_t token_len, uint8_t **out, size_t *out_len); naut_err naut_dht_parse_response(const uint8_t *data, size_t len, naut_dht_response *out); void naut_dht_response_free(naut_dht_response *response); /* Query bootstrap endpoints ("host:port") and iteratively follow returned * compact nodes until peers are found or the bounded traversal is exhausted. */ naut_err naut_dht_get_peers(const char *const *bootstrap, size_t num_bootstrap, const uint8_t info_hash[20], naut_peer_addr **peers, size_t *num_peers); #endif /* NAUT_DHT_H */