2016-11-30 03:47:40 +00:00
|
|
|
#ifndef KS_DHT_H
|
|
|
|
#define KS_DHT_H
|
|
|
|
|
|
|
|
#include "ks.h"
|
|
|
|
#include "ks_bencode.h"
|
|
|
|
|
|
|
|
|
|
|
|
KS_BEGIN_EXTERN_C
|
|
|
|
|
|
|
|
|
|
|
|
#define KS_DHT_DEFAULT_PORT 5309
|
|
|
|
#define KS_DHT_RECV_BUFFER_SIZE 0xFFFF
|
|
|
|
|
2016-12-07 16:33:40 +00:00
|
|
|
#define KS_DHT_NODEID_SIZE 20
|
2016-12-02 19:57:45 +00:00
|
|
|
|
|
|
|
#define KS_DHT_MESSAGE_TRANSACTIONID_MAX_SIZE 20
|
|
|
|
#define KS_DHT_MESSAGE_TYPE_MAX_SIZE 20
|
|
|
|
#define KS_DHT_MESSAGE_QUERY_MAX_SIZE 20
|
2016-12-05 20:43:52 +00:00
|
|
|
#define KS_DHT_MESSAGE_ERROR_MAX_SIZE 256
|
2016-12-02 19:57:45 +00:00
|
|
|
|
2016-12-05 20:43:52 +00:00
|
|
|
#define KS_DHT_TRANSACTION_EXPIRATION_DELAY 30
|
2016-12-02 19:57:45 +00:00
|
|
|
|
2016-11-30 03:47:40 +00:00
|
|
|
typedef struct ks_dht2_s ks_dht2_t;
|
2016-12-07 16:33:40 +00:00
|
|
|
//typedef struct ks_dht2_nodeid_s ks_dht2_nodeid_t;
|
|
|
|
//typedef struct ks_dht2_nodeid_raw_s ks_dht2_nodeid_raw_t;
|
|
|
|
typedef uint8_t ks_dht2_nodeid_t[KS_DHT_NODEID_SIZE];
|
|
|
|
typedef struct ks_dht2_node_s ks_dht2_node_t;
|
2016-12-02 19:57:45 +00:00
|
|
|
typedef struct ks_dht2_message_s ks_dht2_message_t;
|
|
|
|
typedef struct ks_dht2_endpoint_s ks_dht2_endpoint_t;
|
|
|
|
typedef struct ks_dht2_transaction_s ks_dht2_transaction_t;
|
|
|
|
|
|
|
|
|
2016-12-06 15:15:12 +00:00
|
|
|
typedef ks_status_t (*ks_dht2_message_callback_t)(ks_dht2_t *dht, ks_dht2_message_t *message);
|
2016-12-02 19:57:45 +00:00
|
|
|
|
2016-12-07 16:33:40 +00:00
|
|
|
struct ks_dht2_node_s {
|
2016-12-02 19:57:45 +00:00
|
|
|
ks_pool_t *pool;
|
2016-12-07 16:33:40 +00:00
|
|
|
ks_dht2_nodeid_t id;
|
|
|
|
ks_sockaddr_t *addr4;
|
|
|
|
ks_sockaddr_t *addr6;
|
|
|
|
ks_size_t addr4_length;
|
|
|
|
ks_size_t addr6_length;
|
2016-12-02 19:57:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ks_dht2_message_s {
|
|
|
|
ks_pool_t *pool;
|
2016-12-06 15:15:12 +00:00
|
|
|
ks_sockaddr_t raddr;
|
2016-12-02 19:57:45 +00:00
|
|
|
struct bencode *data;
|
|
|
|
uint8_t transactionid[KS_DHT_MESSAGE_TRANSACTIONID_MAX_SIZE];
|
|
|
|
ks_size_t transactionid_length;
|
|
|
|
char type[KS_DHT_MESSAGE_TYPE_MAX_SIZE];
|
|
|
|
struct bencode *args;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ks_dht2_endpoint_s {
|
|
|
|
ks_pool_t *pool;
|
|
|
|
ks_sockaddr_t addr;
|
|
|
|
ks_socket_t sock;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ks_dht2_transaction_s {
|
|
|
|
ks_pool_t *pool;
|
2016-12-05 20:43:52 +00:00
|
|
|
ks_sockaddr_t raddr;
|
2016-12-02 22:42:39 +00:00
|
|
|
uint32_t transactionid;
|
2016-12-02 19:57:45 +00:00
|
|
|
ks_dht2_message_callback_t callback;
|
2016-12-05 20:43:52 +00:00
|
|
|
ks_time_t expiration;
|
|
|
|
ks_bool_t finished;
|
2016-12-02 19:57:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-11-30 03:47:40 +00:00
|
|
|
struct ks_dht2_s {
|
|
|
|
ks_pool_t *pool;
|
|
|
|
ks_bool_t pool_alloc;
|
|
|
|
|
2016-12-02 19:57:45 +00:00
|
|
|
ks_bool_t autoroute;
|
|
|
|
ks_port_t autoroute_port;
|
|
|
|
|
2016-11-30 03:47:40 +00:00
|
|
|
ks_dht2_nodeid_t nodeid;
|
|
|
|
|
2016-12-01 21:16:35 +00:00
|
|
|
ks_hash_t *registry_type;
|
|
|
|
ks_hash_t *registry_query;
|
2016-12-05 20:43:52 +00:00
|
|
|
ks_hash_t *registry_error;
|
2016-12-01 04:37:36 +00:00
|
|
|
|
2016-11-30 03:47:40 +00:00
|
|
|
ks_bool_t bind_ipv4;
|
|
|
|
ks_bool_t bind_ipv6;
|
|
|
|
|
|
|
|
ks_dht2_endpoint_t **endpoints;
|
|
|
|
int32_t endpoints_size;
|
|
|
|
ks_hash_t *endpoints_hash;
|
|
|
|
struct pollfd *endpoints_poll;
|
|
|
|
|
2016-12-06 15:15:12 +00:00
|
|
|
ks_q_t *send_q;
|
|
|
|
ks_dht2_message_t *send_q_unsent;
|
2016-11-30 03:47:40 +00:00
|
|
|
uint8_t recv_buffer[KS_DHT_RECV_BUFFER_SIZE];
|
|
|
|
ks_size_t recv_buffer_length;
|
2016-12-01 04:37:36 +00:00
|
|
|
|
2016-12-02 22:42:39 +00:00
|
|
|
uint32_t transactionid_next;
|
2016-12-02 19:57:45 +00:00
|
|
|
ks_hash_t *transactions_hash;
|
|
|
|
};
|
2016-11-30 03:47:40 +00:00
|
|
|
|
2016-12-02 19:57:45 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-11-30 03:47:40 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_alloc(ks_dht2_t **dht, ks_pool_t *pool);
|
2016-11-30 18:35:12 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_prealloc(ks_dht2_t *dht, ks_pool_t *pool);
|
2016-11-30 03:47:40 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_free(ks_dht2_t *dht);
|
|
|
|
|
2016-12-01 22:34:31 +00:00
|
|
|
|
2016-12-07 16:33:40 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_init(ks_dht2_t *dht, const ks_dht2_nodeid_t *nodeid);
|
2016-11-30 03:47:40 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_deinit(ks_dht2_t *dht);
|
|
|
|
|
2016-12-02 19:57:45 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_autoroute(ks_dht2_t *dht, ks_bool_t autoroute, ks_port_t port);
|
2016-11-30 03:47:40 +00:00
|
|
|
|
2016-12-02 19:57:45 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_bind(ks_dht2_t *dht, const ks_sockaddr_t *addr, ks_dht2_endpoint_t **endpoint);
|
2016-12-06 15:15:12 +00:00
|
|
|
KS_DECLARE(void) ks_dht2_pulse(ks_dht2_t *dht, int32_t timeout);
|
2016-11-30 03:47:40 +00:00
|
|
|
|
2016-12-01 04:37:36 +00:00
|
|
|
|
2016-12-02 19:57:45 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_register_type(ks_dht2_t *dht, const char *value, ks_dht2_message_callback_t callback);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_register_query(ks_dht2_t *dht, const char *value, ks_dht2_message_callback_t callback);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_alloc(ks_dht2_message_t **message, ks_pool_t *pool);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_prealloc(ks_dht2_message_t *message, ks_pool_t *pool);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_free(ks_dht2_message_t *message);
|
|
|
|
|
2016-12-06 15:15:12 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_init(ks_dht2_message_t *message, ks_sockaddr_t *raddr, ks_bool_t alloc_data);
|
2016-12-02 19:57:45 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_deinit(ks_dht2_message_t *message);
|
|
|
|
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_parse(ks_dht2_message_t *message, const uint8_t *buffer, ks_size_t buffer_length);
|
|
|
|
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_query(ks_dht2_message_t *message,
|
2016-12-02 22:42:39 +00:00
|
|
|
uint32_t transactionid,
|
2016-12-02 19:57:45 +00:00
|
|
|
const char *query,
|
|
|
|
struct bencode **args);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_response(ks_dht2_message_t *message,
|
|
|
|
uint8_t *transactionid,
|
|
|
|
ks_size_t transactionid_length,
|
|
|
|
struct bencode **args);
|
2016-12-05 20:43:52 +00:00
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_message_error(ks_dht2_message_t *message,
|
|
|
|
uint8_t *transactionid,
|
|
|
|
ks_size_t transactionid_length,
|
|
|
|
struct bencode **args);
|
2016-12-02 19:57:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_transaction_alloc(ks_dht2_transaction_t **transaction, ks_pool_t *pool);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_transaction_prealloc(ks_dht2_transaction_t *trasnaction, ks_pool_t *pool);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_transaction_free(ks_dht2_transaction_t *transaction);
|
|
|
|
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_transaction_init(ks_dht2_transaction_t *transaction,
|
2016-12-05 20:43:52 +00:00
|
|
|
ks_sockaddr_t *raddr,
|
2016-12-02 22:42:39 +00:00
|
|
|
uint32_t transactionid,
|
2016-12-02 19:57:45 +00:00
|
|
|
ks_dht2_message_callback_t callback);
|
|
|
|
KS_DECLARE(ks_status_t) ks_dht2_transaction_deinit(ks_dht2_transaction_t *transaction);
|
|
|
|
|
2016-11-30 03:47:40 +00:00
|
|
|
KS_END_EXTERN_C
|
|
|
|
|
|
|
|
#endif /* KS_DHT_H */
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
|
|
|
*/
|