mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-15 13:58:32 +00:00
FS-9775: First round of integration of DHT into libblade, requires ongoing changes to DHT for proper exposure to blade level
This commit is contained in:
1 parent
42bfcc3d46
commit
743a768a3f
17 files changed
+991
-4864
No files matched your search
@@ -0,0 +1,447 @@
|
||||
#ifndef KS_DHT_INT_H
|
||||
#define KS_DHT_INT_H
|
||||
|
||||
#include "ks.h"
|
||||
|
||||
KS_BEGIN_EXTERN_C
|
||||
|
||||
/**
|
||||
* Determines the appropriate endpoint to reach a remote address.
|
||||
* If an endpoint is provided, nothing more needs to be done.
|
||||
* If no endpoint is provided, first it will check for an active endpoint it can route though.
|
||||
* If no active endpoint is available and autorouting is enabled it will attempt to bind a usable endpoint.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param raddr pointer to the remote address
|
||||
* @param endpoint dereferenced in/out pointer to the endpoint, if populated then returns immediately
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, ...
|
||||
* @see ks_ip_route
|
||||
* @see ks_hash_read_unlock
|
||||
* @see ks_addr_set
|
||||
* @see ks_dht_bind
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_autoroute_check(ks_dht_t *dht, const ks_sockaddr_t *raddr, ks_dht_endpoint_t **endpoint);
|
||||
|
||||
/**
|
||||
* Called internally to receive datagrams from endpoints.
|
||||
* Handles datagrams by dispatching each through a threadpool.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param timeout time in ms to wait for an incoming datagram on any endpoint
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_endpoints(ks_dht_t *dht, int32_t timeout);
|
||||
|
||||
/**
|
||||
* Called internally to expire or reannounce storage item data.
|
||||
* Handles reannouncing and purging of expiring storage items.
|
||||
* @param dht pointer to the dht instance
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_storageitems(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
* Called internally to process job state machine.
|
||||
* Handles completing and purging of finished jobs.
|
||||
* @param dht pointer to the dht instance
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_jobs(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
* Called internally to send queued messages.
|
||||
* Handles throttling of message sending to ensure system buffers are not overloaded and messages are not dropped.
|
||||
* @param dht pointer to the dht instance
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_send(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
* Called internally to expire transactions.
|
||||
* Handles purging of expired and finished transactions.
|
||||
* @param dht pointer to the dht instance
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_transactions(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
* Called internally to expire and cycle tokens.
|
||||
* Handles cycling new secret entropy for token generation.
|
||||
* @param dht pointer to the dht instance
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse_tokens(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
* Converts a ks_dht_nodeid_t into it's hex string representation.
|
||||
* @param id pointer to the nodeid
|
||||
* @param buffer pointer to the buffer able to contain at least (KS_DHT_NODEID_SIZE * 2) + 1 characters
|
||||
* @return The pointer to the front of the populated string buffer
|
||||
*/
|
||||
KS_DECLARE(char *) ks_dht_hex(const uint8_t *data, char *buffer, ks_size_t len);
|
||||
|
||||
/**
|
||||
* Compacts address information as per the DHT specifications.
|
||||
* @param address pointer to the address being compacted from
|
||||
* @param buffer pointer to the buffer containing compacted data
|
||||
* @param buffer_length pointer to the buffer length consumed
|
||||
* @param buffer_size max size of the buffer
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_NO_MEM
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_compact_addressinfo(const ks_sockaddr_t *address,
|
||||
uint8_t *buffer,
|
||||
ks_size_t *buffer_length,
|
||||
ks_size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Expands address information as per the DHT specifications.
|
||||
* @param buffer pointer to the buffer containing compacted data
|
||||
* @param buffer_length pointer to the buffer length consumed
|
||||
* @param buffer_size max size of the buffer
|
||||
* @param address pointer to the address being expanded into
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_NO_MEM, ...
|
||||
* @see ks_addr_set_raw
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_expand_addressinfo(const uint8_t *buffer,
|
||||
ks_size_t *buffer_length,
|
||||
ks_size_t buffer_size,
|
||||
ks_sockaddr_t *address);
|
||||
|
||||
/**
|
||||
* Compacts node information as per the DHT specifications.
|
||||
* Compacts address information after the nodeid.
|
||||
* @param nodeid pointer to the nodeid being compacted from
|
||||
* @param address pointer to the address being compacted from
|
||||
* @param buffer pointer to the buffer containing compacted data
|
||||
* @param buffer_length pointer to the buffer length consumed
|
||||
* @param buffer_size max size of the buffer
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_NO_MEM, ...
|
||||
* @see ks_dht_utility_compact_addressinfo
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_compact_nodeinfo(const ks_dht_nodeid_t *nodeid,
|
||||
const ks_sockaddr_t *address,
|
||||
uint8_t *buffer,
|
||||
ks_size_t *buffer_length,
|
||||
ks_size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Expands address information as per the DHT specifications.
|
||||
* Expands compacted address information after the nodeid.
|
||||
* @param buffer pointer to the buffer containing compacted data
|
||||
* @param buffer_length pointer to the buffer length consumed
|
||||
* @param buffer_size max size of the buffer
|
||||
* @param address pointer to the address being expanded into
|
||||
* @param nodeid pointer to the nodeid being expanded into
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_NO_MEM, ...
|
||||
* @see ks_dht_utility_expand_addressinfo
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_expand_nodeinfo(const uint8_t *buffer,
|
||||
ks_size_t *buffer_length,
|
||||
ks_size_t buffer_size,
|
||||
ks_dht_nodeid_t *nodeid,
|
||||
ks_sockaddr_t *address);
|
||||
|
||||
/**
|
||||
* Extracts a ks_dht_nodeid_t from a bencode dictionary given a string key.
|
||||
* @param args pointer to the bencode dictionary
|
||||
* @param key string key in the bencode dictionary to extract the value from
|
||||
* @param nodeid dereferenced out pointer to the nodeid
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_ARG_INVALID
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_extract_nodeid(struct bencode *args, const char *key, ks_dht_nodeid_t **nodeid);
|
||||
|
||||
/**
|
||||
* Extracts a ks_dht_token_t from a bencode dictionary given a string key.
|
||||
* @param args pointer to the bencode dictionary
|
||||
* @param key string key in the bencode dictionary to extract the value from
|
||||
* @param nodeid dereferenced out pointer to the token
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_ARG_INVALID
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_utility_extract_token(struct bencode *args, const char *key, ks_dht_token_t **token);
|
||||
|
||||
/**
|
||||
* Generates an opaque write token based on a shifting secret value, the remote address and target nodeid of interest.
|
||||
* This token ensures that future operations can be verified to the remote peer and target id requested.
|
||||
* @param secret rotating secret portion of the token hash
|
||||
* @param raddr pointer to the remote address used for the ip and port in the token hash
|
||||
* @param target pointer to the nodeid of the target used for the token hash
|
||||
* @param token pointer to the output token being generated
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_FAIL
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_token_generate(uint32_t secret, const ks_sockaddr_t *raddr, ks_dht_nodeid_t *target, ks_dht_token_t *token);
|
||||
|
||||
/**
|
||||
* Verify an opaque write token matches the provided remote address and target nodeid.
|
||||
* Handles checking against the last two secret values for the token hash.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param raddr pointer to the remote address used for the ip and port in the token hash
|
||||
* @param target pointer to the nodeid of the target used for the token hash
|
||||
* @param token pointer to the input token being compared
|
||||
* @return Either KS_TRUE if verification passes, otherwise KS_FALSE
|
||||
*/
|
||||
KS_DECLARE(ks_bool_t) ks_dht_token_verify(ks_dht_t *dht, const ks_sockaddr_t *raddr, ks_dht_nodeid_t *target, ks_dht_token_t *token);
|
||||
|
||||
/**
|
||||
* Encodes a message for transmission as a UDP datagram and sends it.
|
||||
* Uses the internally tracked local endpoint and remote address to route the UDP datagram.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param message pointer to the message being sent
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, ...
|
||||
* @see ks_socket_sendto
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_send(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
|
||||
/**
|
||||
* Sets up the common parts of a query message.
|
||||
* Determines the local endpoint aware of autorouting, assigns the remote address, generates a transaction, and queues a callback.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param job pointer to the job
|
||||
* @param query string value of the query type, for example "ping"
|
||||
* @param callback callback to be called when response to transaction is received
|
||||
* @param transaction dereferenced out pointer to the allocated transaction, may be NULL to ignore output
|
||||
* @param message dereferenced out pointer to the allocated message
|
||||
* @param args dereferenced out pointer to the allocated bencode args, may be NULL to ignore output
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_FAIL, ...
|
||||
* @see ks_dht_autoroute_check
|
||||
* @see ks_dht_transaction_alloc
|
||||
* @see ks_dht_transaction_init
|
||||
* @see ks_dht_message_alloc
|
||||
* @see ks_dht_message_init
|
||||
* @see ks_dht_message_query
|
||||
* @see ks_hash_insert
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_query_setup(ks_dht_t *dht,
|
||||
ks_dht_job_t *job,
|
||||
const char *query,
|
||||
ks_dht_job_callback_t callback,
|
||||
ks_dht_transaction_t **transaction,
|
||||
ks_dht_message_t **message,
|
||||
struct bencode **args);
|
||||
|
||||
/**
|
||||
* Sets up the common parts of a response message.
|
||||
* Determines the local endpoint aware of autorouting, assigns the remote address, and assigns the transaction.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param ep pointer to the endpoint, may be NULL to find an endpoint or autoroute one
|
||||
* @param raddr pointer to the remote address
|
||||
* @param transactionid pointer to the buffer containing the transactionid, may be of variable size depending on the querying node
|
||||
* @param transactionid_length length of the transactionid buffer
|
||||
* @param message dereferenced out pointer to the allocated message
|
||||
* @param args dereferenced out pointer to the allocated bencode args, may be NULL to ignore output
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, ...
|
||||
* @see ks_dht_autoroute_check
|
||||
* @see ks_dht_message_alloc
|
||||
* @see ks_dht_message_init
|
||||
* @see ks_dht_message_response
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_response_setup(ks_dht_t *dht,
|
||||
ks_dht_endpoint_t *ep,
|
||||
const ks_sockaddr_t *raddr,
|
||||
uint8_t *transactionid,
|
||||
ks_size_t transactionid_length,
|
||||
ks_dht_message_t **message,
|
||||
struct bencode **args);
|
||||
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_error(ks_dht_t *dht,
|
||||
ks_dht_endpoint_t *ep,
|
||||
const ks_sockaddr_t *raddr,
|
||||
uint8_t *transactionid,
|
||||
ks_size_t transactionid_length,
|
||||
long long errorcode,
|
||||
const char *errorstr);
|
||||
|
||||
KS_DECLARE(void) ks_dht_pulse_jobs(ks_dht_t *dht);
|
||||
KS_DECLARE(ks_status_t) ks_dht_query_ping(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
KS_DECLARE(ks_status_t) ks_dht_query_findnode(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
KS_DECLARE(ks_status_t) ks_dht_query_get(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
KS_DECLARE(ks_status_t) ks_dht_query_put(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
|
||||
KS_DECLARE(void *)ks_dht_process(ks_thread_t *thread, void *data);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_query(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_response(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_error(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_query_ping(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_response_ping(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_query_findnode(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_response_findnode(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_query_get(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_response_get(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_query_put(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
KS_DECLARE(ks_status_t) ks_dht_process_response_put(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_datagram_create(ks_dht_datagram_t **datagram,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_t *dht,
|
||||
ks_dht_endpoint_t *endpoint,
|
||||
const ks_sockaddr_t *raddr);
|
||||
KS_DECLARE(void) ks_dht_datagram_destroy(ks_dht_datagram_t **datagram);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_job_create(ks_dht_job_t **job,
|
||||
ks_pool_t *pool,
|
||||
const ks_sockaddr_t *raddr,
|
||||
int32_t attempts,
|
||||
void *data);
|
||||
KS_DECLARE(void) ks_dht_job_build_ping(ks_dht_job_t *job, ks_dht_job_callback_t query_callback, ks_dht_job_callback_t finish_callback);
|
||||
KS_DECLARE(void) ks_dht_job_build_findnode(ks_dht_job_t *job,
|
||||
ks_dht_job_callback_t query_callback,
|
||||
ks_dht_job_callback_t finish_callback,
|
||||
ks_dht_nodeid_t *target);
|
||||
KS_DECLARE(void) ks_dht_job_build_get(ks_dht_job_t *job,
|
||||
ks_dht_job_callback_t query_callback,
|
||||
ks_dht_job_callback_t finish_callback,
|
||||
ks_dht_nodeid_t *target,
|
||||
const uint8_t *salt,
|
||||
ks_size_t salt_length);
|
||||
KS_DECLARE(void) ks_dht_job_build_put(ks_dht_job_t *job,
|
||||
ks_dht_job_callback_t query_callback,
|
||||
ks_dht_job_callback_t finish_callback,
|
||||
ks_dht_token_t *token,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
KS_DECLARE(void) ks_dht_job_build_search(ks_dht_job_t *job,
|
||||
ks_dht_job_callback_t query_callback,
|
||||
ks_dht_job_callback_t finish_callback);
|
||||
KS_DECLARE(void) ks_dht_job_build_search_findnode(ks_dht_job_t *job,
|
||||
ks_dht_nodeid_t *target,
|
||||
uint32_t family,
|
||||
ks_dht_job_callback_t query_callback,
|
||||
ks_dht_job_callback_t finish_callback);
|
||||
KS_DECLARE(void) ks_dht_job_destroy(ks_dht_job_t **job);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_endpoint_create(ks_dht_endpoint_t **endpoint,
|
||||
ks_pool_t *pool,
|
||||
const ks_sockaddr_t *addr,
|
||||
ks_socket_t sock);
|
||||
KS_DECLARE(void) ks_dht_endpoint_destroy(ks_dht_endpoint_t **endpoint);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_create(ks_dht_message_t **message,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_endpoint_t *endpoint,
|
||||
const ks_sockaddr_t *raddr,
|
||||
ks_bool_t alloc_data);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_message_destroy(ks_dht_message_t **message);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_message_parse(ks_dht_message_t *message, const uint8_t *buffer, ks_size_t buffer_length);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_search_create(ks_dht_search_t **search,
|
||||
ks_pool_t *pool,
|
||||
ks_dhtrt_routetable_t *table,
|
||||
const ks_dht_nodeid_t *target,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data);
|
||||
KS_DECLARE(void) ks_dht_search_destroy(ks_dht_search_t **search);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_publish_create(ks_dht_publish_t **publish,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
KS_DECLARE(void) ks_dht_publish_destroy(ks_dht_publish_t **publish);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_distribute_create(ks_dht_distribute_t **distribute,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_storageitem_callback_t callback,
|
||||
void *data,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
KS_DECLARE(void) ks_dht_distribute_destroy(ks_dht_distribute_t **distribute);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_target_immutable_internal(struct bencode *value, ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_target_mutable_internal(ks_dht_storageitem_pkey_t *pk, struct bencode *salt, ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_create_immutable_internal(ks_dht_storageitem_t **item,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_nodeid_t *target,
|
||||
struct bencode *v,
|
||||
ks_bool_t clone_v);
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_create_immutable(ks_dht_storageitem_t **item,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_nodeid_t *target,
|
||||
const uint8_t *value,
|
||||
ks_size_t value_length);
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_create_mutable_internal(ks_dht_storageitem_t **item,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_nodeid_t *target,
|
||||
struct bencode *v,
|
||||
ks_bool_t clone_v,
|
||||
ks_dht_storageitem_pkey_t *pk,
|
||||
struct bencode *salt,
|
||||
ks_bool_t clone_salt,
|
||||
int64_t sequence,
|
||||
ks_dht_storageitem_signature_t *signature);
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_create_mutable(ks_dht_storageitem_t **item,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_nodeid_t *target,
|
||||
const uint8_t *value,
|
||||
ks_size_t value_length,
|
||||
ks_dht_storageitem_pkey_t *pk,
|
||||
const uint8_t *salt,
|
||||
ks_size_t salt_length,
|
||||
int64_t sequence,
|
||||
ks_dht_storageitem_signature_t *signature);
|
||||
KS_DECLARE(void) ks_dht_storageitem_update_mutable(ks_dht_storageitem_t *item, struct bencode *v, int64_t sequence, ks_dht_storageitem_signature_t *signature);
|
||||
KS_DECLARE(void) ks_dht_storageitem_destroy(ks_dht_storageitem_t **item);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_transaction_create(ks_dht_transaction_t **transaction,
|
||||
ks_pool_t *pool,
|
||||
ks_dht_job_t *job,
|
||||
uint32_t transactionid,
|
||||
ks_dht_job_callback_t callback);
|
||||
KS_DECLARE(void) ks_dht_transaction_destroy(ks_dht_transaction_t **transaction);
|
||||
|
||||
KS_END_EXTERN_C
|
||||
|
||||
#endif /* KS_DHT_INT_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:
|
||||
*/
|
||||
+565
-78
@@ -1,101 +1,588 @@
|
||||
/*
|
||||
Copyright (c) 2009-2011 by Juliusz Chroboczek
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _KS_DHT_H
|
||||
#define _KS_DHT_H
|
||||
#ifndef KS_DHT_H
|
||||
#define KS_DHT_H
|
||||
|
||||
#include "ks.h"
|
||||
#include "ks_bencode.h"
|
||||
#include "sodium.h"
|
||||
|
||||
KS_BEGIN_EXTERN_C
|
||||
|
||||
typedef enum {
|
||||
KS_DHT_EVENT_NONE = 0,
|
||||
KS_DHT_EVENT_VALUES = 1,
|
||||
KS_DHT_EVENT_VALUES6 = 2,
|
||||
KS_DHT_EVENT_SEARCH_DONE = 3,
|
||||
KS_DHT_EVENT_SEARCH_DONE6 = 4
|
||||
} ks_dht_event_t;
|
||||
|
||||
#define KS_DHT_DEFAULT_PORT 5309
|
||||
|
||||
#define KS_DHT_TPOOL_MIN 2
|
||||
#define KS_DHT_TPOOL_MAX 8
|
||||
#define KS_DHT_TPOOL_STACK (1024 * 256)
|
||||
#define KS_DHT_TPOOL_IDLE 10
|
||||
|
||||
#define KS_DHT_DATAGRAM_BUFFER_SIZE 1000
|
||||
|
||||
//#define KS_DHT_RECV_BUFFER_SIZE 0xFFFF
|
||||
|
||||
#define KS_DHT_NODEID_SIZE 20
|
||||
|
||||
#define KS_DHT_RESPONSE_NODES_MAX_SIZE 8
|
||||
|
||||
#define KS_DHT_MESSAGE_TRANSACTIONID_MAX_SIZE 20
|
||||
#define KS_DHT_MESSAGE_TYPE_MAX_SIZE 20
|
||||
#define KS_DHT_MESSAGE_QUERY_MAX_SIZE 20
|
||||
#define KS_DHT_MESSAGE_ERROR_MAX_SIZE 256
|
||||
|
||||
#define KS_DHT_TRANSACTION_EXPIRATION 10
|
||||
#define KS_DHT_TRANSACTIONS_PULSE 1
|
||||
|
||||
#define KS_DHT_SEARCH_RESULTS_MAX_SIZE 8 // @todo replace with KS_DHTRT_BUCKET_SIZE
|
||||
|
||||
#define KS_DHT_STORAGEITEM_PKEY_SIZE crypto_sign_PUBLICKEYBYTES
|
||||
#define KS_DHT_STORAGEITEM_SKEY_SIZE crypto_sign_SECRETKEYBYTES
|
||||
#define KS_DHT_STORAGEITEM_SALT_MAX_SIZE 64
|
||||
#define KS_DHT_STORAGEITEM_SIGNATURE_SIZE crypto_sign_BYTES
|
||||
#define KS_DHT_STORAGEITEM_EXPIRATION 7200
|
||||
#define KS_DHT_STORAGEITEM_KEEPALIVE 300
|
||||
#define KS_DHT_STORAGEITEMS_PULSE 10
|
||||
|
||||
#define KS_DHT_TOKEN_SIZE SHA_DIGEST_LENGTH
|
||||
#define KS_DHT_TOKEN_EXPIRATION 300
|
||||
#define KS_DHT_TOKENS_PULSE 1
|
||||
|
||||
#define KS_DHTRT_MAXQUERYSIZE 20
|
||||
|
||||
typedef struct ks_dht_s ks_dht_t;
|
||||
typedef struct ks_dht_datagram_s ks_dht_datagram_t;
|
||||
typedef struct ks_dht_job_s ks_dht_job_t;
|
||||
typedef struct ks_dht_nodeid_s ks_dht_nodeid_t;
|
||||
typedef struct ks_dht_token_s ks_dht_token_t;
|
||||
typedef struct ks_dht_storageitem_pkey_s ks_dht_storageitem_pkey_t;
|
||||
typedef struct ks_dht_storageitem_skey_s ks_dht_storageitem_skey_t;
|
||||
typedef struct ks_dht_storageitem_signature_s ks_dht_storageitem_signature_t;
|
||||
typedef struct ks_dht_message_s ks_dht_message_t;
|
||||
typedef struct ks_dht_endpoint_s ks_dht_endpoint_t;
|
||||
typedef struct ks_dht_transaction_s ks_dht_transaction_t;
|
||||
typedef struct ks_dht_search_s ks_dht_search_t;
|
||||
typedef struct ks_dht_publish_s ks_dht_publish_t;
|
||||
typedef struct ks_dht_distribute_s ks_dht_distribute_t;
|
||||
typedef struct ks_dht_node_s ks_dht_node_t;
|
||||
typedef struct ks_dhtrt_routetable_s ks_dhtrt_routetable_t;
|
||||
typedef struct ks_dhtrt_querynodes_s ks_dhtrt_querynodes_t;
|
||||
typedef struct ks_dht_storageitem_s ks_dht_storageitem_t;
|
||||
|
||||
|
||||
typedef enum {
|
||||
DHT_PARAM_AUTOROUTE = 1
|
||||
} ks_dht_param_t;
|
||||
|
||||
typedef enum {
|
||||
KS_DHT_AF_INET4 = (1 << 0),
|
||||
KS_DHT_AF_INET6 = (1 << 1)
|
||||
} ks_dht_af_flag_t;
|
||||
typedef ks_status_t (*ks_dht_job_callback_t)(ks_dht_t *dht, ks_dht_job_t *job);
|
||||
typedef ks_status_t (*ks_dht_message_callback_t)(ks_dht_t *dht, ks_dht_message_t *message);
|
||||
//typedef ks_status_t (*ks_dht_search_callback_t)(ks_dht_t *dht, ks_dht_search_t *search);
|
||||
typedef ks_status_t (*ks_dht_storageitem_callback_t)(ks_dht_t *dht, ks_dht_storageitem_t *item);
|
||||
|
||||
|
||||
typedef void (*dht_callback_t)(void *closure, ks_dht_event_t event, const unsigned char *info_hash, const void *data, size_t data_len);
|
||||
struct ks_dht_datagram_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_t *dht;
|
||||
ks_dht_endpoint_t *endpoint;
|
||||
ks_sockaddr_t raddr;
|
||||
uint8_t buffer[KS_DHT_DATAGRAM_BUFFER_SIZE];
|
||||
ks_size_t buffer_length;
|
||||
};
|
||||
|
||||
typedef struct dht_handle_s dht_handle_t;
|
||||
/**
|
||||
* Note: This must remain a structure for casting from raw data
|
||||
*/
|
||||
struct ks_dht_nodeid_s {
|
||||
uint8_t id[KS_DHT_NODEID_SIZE];
|
||||
};
|
||||
|
||||
KS_DECLARE(int) dht_periodic(dht_handle_t *h, const void *buf, size_t buflen, ks_sockaddr_t *from);
|
||||
KS_DECLARE(ks_status_t) ks_dht_init(dht_handle_t **handle, ks_dht_af_flag_t af_flags, const unsigned char *id, unsigned int port);
|
||||
KS_DECLARE(void) ks_dht_set_param(dht_handle_t *h, ks_dht_param_t param, ks_bool_t val);
|
||||
KS_DECLARE(ks_status_t) ks_dht_add_ip(dht_handle_t *h, char *ip, int port);
|
||||
KS_DECLARE(void) ks_dht_start(dht_handle_t *h);
|
||||
KS_DECLARE(int) dht_insert_node(dht_handle_t *h, const unsigned char *id, ks_sockaddr_t *sa);
|
||||
KS_DECLARE(int) dht_ping_node(dht_handle_t *h, ks_sockaddr_t *sa);
|
||||
KS_DECLARE(int) dht_search(dht_handle_t *h, const unsigned char *id, int port, int af, dht_callback_t callback, void *closure);
|
||||
KS_DECLARE(int) dht_nodes(dht_handle_t *h, int af, int *good_return, int *dubious_return, int *cached_return, int *incoming_return);
|
||||
KS_DECLARE(ks_status_t) ks_dht_one_loop(dht_handle_t *h, int timeout);
|
||||
KS_DECLARE(ks_status_t) ks_dht_get_bind_addrs(dht_handle_t *h, const ks_sockaddr_t ***addrs, ks_size_t *addrlen);
|
||||
KS_DECLARE(void) ks_dht_set_callback(dht_handle_t *h, dht_callback_t callback, void *closure);
|
||||
KS_DECLARE(void) ks_dht_set_port(dht_handle_t *h, unsigned int port);
|
||||
KS_DECLARE(void) dht_dump_tables(dht_handle_t *h, FILE *f);
|
||||
KS_DECLARE(int) dht_get_nodes(dht_handle_t *h, struct sockaddr_in *sin, int *num, struct sockaddr_in6 *sin6, int *num6);
|
||||
KS_DECLARE(int) dht_uninit(dht_handle_t **h);
|
||||
KS_DECLARE(void) ks_dht_set_v(dht_handle_t *h, const unsigned char *v);
|
||||
KS_DECLARE(int) ks_dht_calculate_mutable_storage_target(unsigned char *pk, unsigned char *salt, int salt_length, unsigned char *target, int target_length);
|
||||
KS_DECLARE(int) ks_dht_generate_mutable_storage_args(struct bencode *data, int64_t sequence, int cas,
|
||||
unsigned char *id, int id_len, /* querying nodes id */
|
||||
const unsigned char *sk, const unsigned char *pk,
|
||||
unsigned char *salt, unsigned long long salt_length,
|
||||
unsigned char *token, unsigned long long token_length,
|
||||
unsigned char *signature, unsigned long long *signature_length,
|
||||
struct bencode **arguments);
|
||||
enum ks_afflags_t { ifv4=AF_INET, ifv6=AF_INET6, ifboth=AF_INET+AF_INET6};
|
||||
enum ks_dht_nodetype_t { KS_DHT_REMOTE=0x01,
|
||||
KS_DHT_LOCAL=0x02,
|
||||
KS_DHT_BOTH=KS_DHT_REMOTE+KS_DHT_LOCAL };
|
||||
|
||||
/* This must be provided by the user. */
|
||||
KS_DECLARE(int) dht_blacklisted(const ks_sockaddr_t *sa);
|
||||
KS_DECLARE(void) dht_hash(void *hash_return, int hash_size, const void *v1, int len1, const void *v2, int len2, const void *v3, int len3);
|
||||
KS_DECLARE(int) dht_random_bytes(void *buf, size_t size);
|
||||
enum ks_create_node_flags_t {
|
||||
KS_DHTRT_CREATE_DEFAULT=0,
|
||||
KS_DHTRT_CREATE_PING,
|
||||
KS_DHTRT_CREATE_TOUCH
|
||||
};
|
||||
|
||||
struct ks_dht_node_s {
|
||||
ks_dht_nodeid_t nodeid;
|
||||
ks_sockaddr_t addr;
|
||||
enum ks_dht_nodetype_t type; /* local or remote */
|
||||
ks_dhtrt_routetable_t* table;
|
||||
ks_rwl_t *reflock;
|
||||
};
|
||||
|
||||
KS_DECLARE(int) ks_dht_send_message_mutable(dht_handle_t *h, unsigned char *sk, unsigned char *pk, char **node_id,
|
||||
char *message_id, int sequence, char *message, ks_time_t life);
|
||||
struct ks_dht_token_s {
|
||||
uint8_t token[KS_DHT_TOKEN_SIZE];
|
||||
};
|
||||
|
||||
KS_DECLARE(int) ks_dht_send_message_mutable_cjson(dht_handle_t *h, unsigned char *sk, unsigned char *pk, char **node_id,
|
||||
char *message_id, int sequence, cJSON *message, ks_time_t life);
|
||||
enum ks_dht_job_state_t {
|
||||
KS_DHT_JOB_STATE_QUERYING,
|
||||
KS_DHT_JOB_STATE_RESPONDING,
|
||||
KS_DHT_JOB_STATE_EXPIRING,
|
||||
KS_DHT_JOB_STATE_COMPLETING,
|
||||
};
|
||||
|
||||
typedef void (ks_dht_store_entry_json_cb)(struct dht_handle_s *h, const cJSON *msg, void *obj);
|
||||
KS_DECLARE(void) ks_dht_store_entry_json_cb_set(struct dht_handle_s *h, ks_dht_store_entry_json_cb *store_json_cb, void *arg);
|
||||
enum ks_dht_job_result_t {
|
||||
KS_DHT_JOB_RESULT_SUCCESS = 0,
|
||||
KS_DHT_JOB_RESULT_EXPIRED,
|
||||
KS_DHT_JOB_RESULT_ERROR,
|
||||
KS_DHT_JOB_RESULT_FAILURE,
|
||||
};
|
||||
|
||||
KS_DECLARE(int) ks_dht_api_find_node(dht_handle_t *h, char *node_id_hex, char *target_hex, ks_bool_t ipv6);
|
||||
struct ks_dht_job_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_t *dht;
|
||||
ks_dht_job_t *next;
|
||||
|
||||
enum ks_dht_job_state_t state;
|
||||
enum ks_dht_job_result_t result;
|
||||
|
||||
ks_sockaddr_t raddr; // will obtain local endpoint node id when creating message using raddr
|
||||
int32_t attempts;
|
||||
|
||||
//enum ks_dht_job_type_t type;
|
||||
ks_dht_job_callback_t query_callback;
|
||||
ks_dht_job_callback_t finish_callback;
|
||||
|
||||
void *data;
|
||||
ks_dht_message_t *response;
|
||||
|
||||
// job specific query parameters
|
||||
ks_dht_nodeid_t query_target;
|
||||
struct bencode *query_salt;
|
||||
int64_t query_cas;
|
||||
ks_dht_token_t query_token;
|
||||
ks_dht_storageitem_t *query_storageitem;
|
||||
|
||||
// error response parameters
|
||||
int64_t error_code;
|
||||
struct bencode *error_description;
|
||||
|
||||
// job specific response parameters
|
||||
ks_dht_node_t *response_id;
|
||||
ks_dht_node_t *response_nodes[KS_DHT_RESPONSE_NODES_MAX_SIZE];
|
||||
ks_size_t response_nodes_count;
|
||||
ks_dht_node_t *response_nodes6[KS_DHT_RESPONSE_NODES_MAX_SIZE];
|
||||
ks_size_t response_nodes6_count;
|
||||
|
||||
ks_dht_token_t response_token;
|
||||
int64_t response_seq;
|
||||
ks_bool_t response_hasitem;
|
||||
ks_dht_storageitem_t *response_storageitem;
|
||||
};
|
||||
|
||||
struct ks_dhtrt_routetable_s {
|
||||
void* internal;
|
||||
ks_pool_t* pool;
|
||||
ks_logger_t logger;
|
||||
};
|
||||
|
||||
struct ks_dhtrt_querynodes_s {
|
||||
ks_dht_nodeid_t nodeid; /* in: id to query */
|
||||
enum ks_afflags_t family; /* in: AF_INET or AF_INET6 or both */
|
||||
enum ks_dht_nodetype_t type; /* remote, local, or both */
|
||||
uint8_t max; /* in: maximum to return */
|
||||
uint8_t count; /* out: number returned */
|
||||
ks_dht_node_t* nodes[ KS_DHTRT_MAXQUERYSIZE ]; /* out: array of peers (ks_dht_node_t* nodes[incount]) */
|
||||
};
|
||||
|
||||
struct ks_dht_storageitem_pkey_s {
|
||||
uint8_t key[KS_DHT_STORAGEITEM_PKEY_SIZE];
|
||||
};
|
||||
|
||||
struct ks_dht_storageitem_skey_s {
|
||||
uint8_t key[KS_DHT_STORAGEITEM_SKEY_SIZE];
|
||||
};
|
||||
|
||||
struct ks_dht_storageitem_signature_s {
|
||||
uint8_t sig[KS_DHT_STORAGEITEM_SIGNATURE_SIZE];
|
||||
};
|
||||
|
||||
struct ks_dht_message_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_endpoint_t *endpoint;
|
||||
ks_sockaddr_t raddr;
|
||||
struct bencode *data;
|
||||
uint8_t transactionid[KS_DHT_MESSAGE_TRANSACTIONID_MAX_SIZE];
|
||||
ks_size_t transactionid_length;
|
||||
ks_dht_transaction_t *transaction;
|
||||
char type[KS_DHT_MESSAGE_TYPE_MAX_SIZE];
|
||||
struct bencode *args;
|
||||
ks_dht_nodeid_t args_id;
|
||||
};
|
||||
|
||||
struct ks_dht_endpoint_s {
|
||||
ks_pool_t *pool;
|
||||
ks_sockaddr_t addr;
|
||||
ks_socket_t sock;
|
||||
};
|
||||
|
||||
struct ks_dht_transaction_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_job_t *job;
|
||||
uint32_t transactionid;
|
||||
//ks_dht_nodeid_t target; // @todo look at moving this into job now
|
||||
ks_dht_job_callback_t callback;
|
||||
ks_time_t expiration;
|
||||
ks_bool_t finished;
|
||||
};
|
||||
|
||||
struct ks_dht_search_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dhtrt_routetable_t *table;
|
||||
ks_dht_nodeid_t target;
|
||||
ks_dht_job_callback_t callback;
|
||||
void *data;
|
||||
ks_mutex_t *mutex;
|
||||
ks_hash_t *searched;
|
||||
int32_t searching;
|
||||
ks_dht_node_t *results[KS_DHT_SEARCH_RESULTS_MAX_SIZE];
|
||||
ks_dht_nodeid_t distances[KS_DHT_SEARCH_RESULTS_MAX_SIZE];
|
||||
ks_size_t results_length;
|
||||
};
|
||||
|
||||
struct ks_dht_publish_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_job_callback_t callback;
|
||||
void *data;
|
||||
int64_t cas;
|
||||
ks_dht_storageitem_t *item;
|
||||
};
|
||||
|
||||
struct ks_dht_distribute_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_storageitem_callback_t callback;
|
||||
void *data;
|
||||
ks_mutex_t *mutex;
|
||||
int32_t publishing;
|
||||
int64_t cas;
|
||||
ks_dht_storageitem_t *item;
|
||||
};
|
||||
|
||||
struct ks_dht_storageitem_s {
|
||||
ks_pool_t *pool;
|
||||
ks_dht_nodeid_t id;
|
||||
ks_time_t expiration;
|
||||
ks_time_t keepalive;
|
||||
struct bencode *v;
|
||||
|
||||
ks_mutex_t *mutex;
|
||||
volatile int32_t refc;
|
||||
ks_dht_storageitem_callback_t callback;
|
||||
|
||||
ks_bool_t mutable;
|
||||
ks_dht_storageitem_pkey_t pk;
|
||||
ks_dht_storageitem_skey_t sk;
|
||||
struct bencode *salt;
|
||||
int64_t seq;
|
||||
ks_dht_storageitem_signature_t sig;
|
||||
};
|
||||
|
||||
struct ks_dht_s {
|
||||
ks_pool_t *pool;
|
||||
ks_bool_t pool_alloc;
|
||||
|
||||
ks_thread_pool_t *tpool;
|
||||
ks_bool_t tpool_alloc;
|
||||
|
||||
ks_dht_nodeid_t nodeid;
|
||||
// @todo make sure this node is unlocked, and never gets destroyed, should also never use local nodes in search results as they can be internal
|
||||
// network addresses, not what others have contacted through
|
||||
ks_dht_node_t *node;
|
||||
|
||||
ks_bool_t autoroute;
|
||||
ks_port_t autoroute_port;
|
||||
|
||||
ks_hash_t *registry_type;
|
||||
ks_hash_t *registry_query;
|
||||
ks_hash_t *registry_error;
|
||||
|
||||
ks_dht_endpoint_t **endpoints;
|
||||
int32_t endpoints_length;
|
||||
int32_t endpoints_size;
|
||||
ks_hash_t *endpoints_hash;
|
||||
struct pollfd *endpoints_poll;
|
||||
|
||||
ks_q_t *send_q;
|
||||
ks_dht_message_t *send_q_unsent;
|
||||
uint8_t recv_buffer[KS_DHT_DATAGRAM_BUFFER_SIZE + 1]; // Add 1, if we receive it then overflow error
|
||||
ks_size_t recv_buffer_length;
|
||||
|
||||
ks_mutex_t *jobs_mutex;
|
||||
ks_dht_job_t *jobs_first;
|
||||
ks_dht_job_t *jobs_last;
|
||||
|
||||
ks_time_t transactions_pulse;
|
||||
ks_mutex_t *transactionid_mutex;
|
||||
volatile uint32_t transactionid_next;
|
||||
ks_hash_t *transactions_hash;
|
||||
|
||||
ks_dhtrt_routetable_t *rt_ipv4;
|
||||
ks_dhtrt_routetable_t *rt_ipv6;
|
||||
|
||||
ks_time_t tokens_pulse;
|
||||
volatile uint32_t token_secret_current;
|
||||
volatile uint32_t token_secret_previous;
|
||||
ks_time_t token_secret_expiration;
|
||||
|
||||
ks_time_t storageitems_pulse;
|
||||
ks_hash_t *storageitems_hash;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor function for ks_dht_t.
|
||||
* Will allocate and initialize internal state including registration of message handlers.
|
||||
* @param dht dereferenced out pointer to the allocated dht instance
|
||||
* @param pool pointer to the memory pool used by the dht instance, may be NULL to create a new memory pool internally
|
||||
* @param tpool pointer to a thread pool used by the dht instance, may be NULL to create a new thread pool internally
|
||||
* @param nodeid pointer to the nodeid for this dht instance
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_NO_MEM
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_create(ks_dht_t **dht, ks_pool_t *pool, ks_thread_pool_t *tpool, ks_dht_nodeid_t *nodeid);
|
||||
|
||||
/**
|
||||
* Destructor function for ks_dht_t.
|
||||
* Will deinitialize and deallocate internal state.
|
||||
* @param dht dereferenced in/out pointer to the dht instance, NULL upon return
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_destroy(ks_dht_t **dht);
|
||||
|
||||
/**
|
||||
* Enable or disable (default) autorouting support.
|
||||
* When enabled, autorouting will allow sending to remote addresses on interfaces which are not yet bound.
|
||||
* The address will be bound with the provided autoroute port when this occurs.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param autoroute enable or disable autorouting
|
||||
* @param port when enabling autorouting this port will be used to bind new addresses, may be 0 to use the default DHT port
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_autoroute(ks_dht_t *dht, ks_bool_t autoroute, ks_port_t port);
|
||||
|
||||
/**
|
||||
* Register a callback for a specific message type.
|
||||
* Will overwrite any duplicate handlers.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param value string of the type text under the 'y' key of a message
|
||||
* @param callback the callback to be called when a message matches
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_register_type(ks_dht_t *dht, const char *value, ks_dht_message_callback_t callback);
|
||||
|
||||
/**
|
||||
* Register a callback for a specific message query.
|
||||
* Will overwrite any duplicate handlers.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param value string of the type text under the 'q' key of a message
|
||||
* @param callback the callback to be called when a message matches
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_register_query(ks_dht_t *dht, const char *value, ks_dht_message_callback_t callback);
|
||||
|
||||
/**
|
||||
* Register a callback for a specific message error.
|
||||
* Will overwrite any duplicate handlers.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param value string of the errorcode under the first item of the 'e' key of a message
|
||||
* @param callback the callback to be called when a message matches
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_register_error(ks_dht_t *dht, const char *value, ks_dht_message_callback_t callback);
|
||||
|
||||
/**
|
||||
* Bind a local address and port for receiving UDP datagrams.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param addr pointer to the local address information
|
||||
* @param endpoint dereferenced out pointer to the allocated endpoint, may be NULL to ignore endpoint output
|
||||
* @return The ks_status_t result: KS_STATUS_SUCCESS, KS_STATUS_FAIL, ...
|
||||
* @see ks_socket_option
|
||||
* @see ks_addr_bind
|
||||
* @see ks_dht_endpoint_alloc
|
||||
* @see ks_dht_endpoint_init
|
||||
* @see ks_hash_insert
|
||||
* @see ks_dhtrt_initroute
|
||||
* @see ks_dhtrt_create_node
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_bind(ks_dht_t *dht, const ks_sockaddr_t *addr, ks_dht_endpoint_t **endpoint);
|
||||
|
||||
/**
|
||||
* Pulse the internals of dht.
|
||||
* Handles receiving UDP datagrams, dispatching processing, handles expirations, throttled message sending, route table pulsing, etc.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param timeout timeout value used when polling sockets for new UDP datagrams
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_pulse(ks_dht_t *dht, int32_t timeout);
|
||||
|
||||
|
||||
KS_DECLARE(char *) ks_dht_hex(const uint8_t *data, char *buffer, ks_size_t len);
|
||||
KS_DECLARE(uint8_t *) ks_dht_dehex(uint8_t *data, const char *buffer, ks_size_t len);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_target_immutable(const uint8_t *value, ks_size_t value_length, ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_target_mutable(ks_dht_storageitem_pkey_t *pk, const uint8_t *salt, ks_size_t salt_length, ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitem_signature_generate(ks_dht_storageitem_signature_t *sig,
|
||||
ks_dht_storageitem_skey_t *sk,
|
||||
const uint8_t *salt,
|
||||
ks_size_t salt_length,
|
||||
int64_t sequence,
|
||||
const uint8_t *value,
|
||||
ks_size_t value_length);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitem_reference(ks_dht_storageitem_t *item);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitem_dereference(ks_dht_storageitem_t *item);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitem_callback(ks_dht_storageitem_t *item, ks_dht_storageitem_callback_t callback);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitems_read_lock(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitems_read_unlock(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitems_write_lock(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_storageitems_write_unlock(ks_dht_t *dht);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_dht_storageitem_t *) ks_dht_storageitems_find(ks_dht_t *dht, ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dht_storageitems_insert(ks_dht_t *dht, ks_dht_storageitem_t *item);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_ping(ks_dht_t *dht, const ks_sockaddr_t *raddr, ks_dht_job_callback_t callback, void *data);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_findnode(ks_dht_t *dht,
|
||||
const ks_sockaddr_t *raddr,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
ks_dht_nodeid_t *target);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_get(ks_dht_t *dht,
|
||||
const ks_sockaddr_t *raddr,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
ks_dht_nodeid_t *target,
|
||||
const uint8_t *salt,
|
||||
ks_size_t salt_length);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_put(ks_dht_t *dht,
|
||||
const ks_sockaddr_t *raddr,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
ks_dht_token_t *token,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
|
||||
/**
|
||||
* Create a network search of the closest nodes to a target.
|
||||
* @param dht pointer to the dht instance
|
||||
* @param family either AF_INET or AF_INET6 for the appropriate network to search
|
||||
* @param target pointer to the nodeid for the target to be searched
|
||||
* @param callback an optional callback to add to the search when it is finished
|
||||
* @param search dereferenced out pointer to the allocated search, may be NULL to ignore search output
|
||||
* @see ks_dht_search_create
|
||||
* @see ks_hash_insert
|
||||
* @see ks_dht_findnode
|
||||
*/
|
||||
KS_DECLARE(void) ks_dht_search(ks_dht_t *dht,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
ks_dhtrt_routetable_t *table,
|
||||
ks_dht_nodeid_t *target);
|
||||
|
||||
KS_DECLARE(void) ks_dht_publish(ks_dht_t *dht,
|
||||
const ks_sockaddr_t *raddr,
|
||||
ks_dht_job_callback_t callback,
|
||||
void *data,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
|
||||
KS_DECLARE(void) ks_dht_distribute(ks_dht_t *dht,
|
||||
ks_dht_storageitem_callback_t callback,
|
||||
void *data,
|
||||
ks_dhtrt_routetable_t *table,
|
||||
int64_t cas,
|
||||
ks_dht_storageitem_t *item);
|
||||
|
||||
/**
|
||||
* route table methods
|
||||
*
|
||||
*/
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_initroute(ks_dhtrt_routetable_t **tableP,
|
||||
ks_dht_t *dht,
|
||||
ks_pool_t *pool);
|
||||
KS_DECLARE(void) ks_dhtrt_deinitroute(ks_dhtrt_routetable_t **table);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_create_node(ks_dhtrt_routetable_t* table,
|
||||
ks_dht_nodeid_t nodeid,
|
||||
enum ks_dht_nodetype_t type,
|
||||
char* ip, unsigned short port,
|
||||
enum ks_create_node_flags_t flags,
|
||||
ks_dht_node_t** node);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_delete_node(ks_dhtrt_routetable_t* table, ks_dht_node_t* node);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_touch_node(ks_dhtrt_routetable_t* table, ks_dht_nodeid_t nodeid);
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_expire_node(ks_dhtrt_routetable_t* table, ks_dht_nodeid_t nodeid);
|
||||
|
||||
KS_DECLARE(uint8_t) ks_dhtrt_findclosest_nodes(ks_dhtrt_routetable_t* table, ks_dhtrt_querynodes_t* query);
|
||||
KS_DECLARE(ks_dht_node_t*) ks_dhtrt_find_node(ks_dhtrt_routetable_t* table, ks_dht_nodeid_t id);
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_sharelock_node(ks_dht_node_t* node);
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_release_node(ks_dht_node_t* node);
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_release_querynodes(ks_dhtrt_querynodes_t* query);
|
||||
|
||||
KS_DECLARE(void) ks_dhtrt_process_table(ks_dhtrt_routetable_t* table);
|
||||
|
||||
KS_DECLARE(uint32_t) ks_dhtrt_serialize(ks_dhtrt_routetable_t* table, void** ptr);
|
||||
KS_DECLARE(ks_status_t) ks_dhtrt_deserialize(ks_dhtrt_routetable_t* table, void* ptr);
|
||||
|
||||
/* debugging aids */
|
||||
KS_DECLARE(void) ks_dhtrt_dump(ks_dhtrt_routetable_t* table, int level);
|
||||
|
||||
|
||||
KS_END_EXTERN_C
|
||||
|
||||
#endif /* _KS_DHT_H */
|
||||
#endif /* KS_DHT_H */
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
|
||||
Reference in new issue
Block a user