libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
Distributed Hash Table

Data Structures

struct  lws_dht_hash
struct  lws_dht_msg
struct  lws_dht_stats
struct  lws_dht_verb_dispatch_args
struct  lws_dht_consensus_info
struct  lws_dht_error_info
struct  lws_dht_info
struct  lws_dht_node_info

Macros

#define LWS_DHT_SHA1_HASH_LEN   20
#define LWS_DHT_STAT_BUCKETS   48
#define DHT_MAX_PEERS   2048
#define DHT_MAX_HASHES   16384
#define DHT_MAX_SEARCHES   1024
#define DHT_SEARCH_EXPIRE_TIME   (62 * 60)

Typedefs

typedef struct lws_dht_hash lws_dht_hash_t
typedef void lws_dht_callback_t(void *closure, int event, const lws_dht_hash_t *info_hash, const void *data, size_t data_len, const struct sockaddr *from, size_t fromlen)
typedef int lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen)
typedef void lws_dht_hash_cb_t(void *hash_return, int hash_size, const void *v1, int len1, const void *v2, int len2, const void *v3, int len3)
typedef void lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash, const struct sockaddr *fromaddr, unsigned short prt)
typedef struct lws_dht_info lws_dht_info_t
typedef struct lws_dht_node_info lws_dht_node_info_t
typedef int(* lws_dht_node_cb_t) (void *user, const lws_dht_node_info_t *ni)

Enumerations

enum  {
  LWS_DHT_HASH_TYPE_UNKNOWN = 0 , LWS_DHT_HASH_TYPE_SHA1 = 0x11 , LWS_DHT_HASH_TYPE_SHA256 = 0x12 , LWS_DHT_HASH_TYPE_SHA512 = 0x13 ,
  LWS_DHT_HASH_TYPE_BLAKE3 = 0x1e
}
enum  lws_dht_verb_result_t {
  LWS_DHT_VERB_RESULT_PROCEED = 0 , LWS_DHT_VERB_RESULT_DROP_OLDER = 1 , LWS_DHT_VERB_RESULT_REPLACE_OLDER = 2 , LWS_DHT_VERB_RESULT_PENDING_ASYNC = 3 ,
  LWS_DHT_VERB_RESULT_PASS = 4 , LWS_DHT_VERB_RESULT_ERROR = -1
}
enum  lws_dht_event_t {
  LWS_DHT_EVENT_NONE , LWS_DHT_EVENT_VALUES , LWS_DHT_EVENT_VALUES6 , LWS_DHT_EVENT_SEARCH_DONE ,
  LWS_DHT_EVENT_SEARCH_DONE6 , LWS_DHT_EVENT_EXTERNAL_ADDR , LWS_DHT_EVENT_EXTERNAL_ADDR6 , LWS_DHT_EVENT_DATA ,
  LWS_DHT_EVENT_WRITE_COMPLETED , LWS_DHT_EVENT_WRITE_FAILED , LWS_DHT_EVENT_NOTIFY , LWS_DHT_EVENT_TOKEN ,
  LWS_DHT_EVENT_ERROR
}

Functions

LWS_VISIBLE LWS_EXTERN lws_dht_hash_t * lws_dht_hash_create (int type, int len, const uint8_t *data)
LWS_VISIBLE LWS_EXTERN void lws_dht_hash_destroy (lws_dht_hash_t **p)
LWS_VISIBLE LWS_EXTERN int lws_dht_get_stats (struct lws_vhost *vh, struct lws_dht_stats *current, const struct lws_dht_stats **history, int *head)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_subscribe (struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen, const lws_dht_hash_t *infohash, int want, int confirm)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_subscribe_confirm (struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen, uint8_t *tid, size_t tid_len, const lws_dht_hash_t *infohash, uint8_t *token, size_t token_len, const uint8_t *sha256, int confirm)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_ack (struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen, const uint8_t *tid, size_t tid_len)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_notify (struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen, const uint8_t *tid, size_t tid_len, const lws_dht_hash_t *infohash, const uint8_t *sha256, const uint8_t *payload, size_t payload_len)
LWS_VISIBLE LWS_EXTERN int lws_dht_msg_parse (const char *in, size_t len, struct lws_dht_msg *out)
LWS_VISIBLE LWS_EXTERN int lws_dht_valid_domain_name (const char *domain)
LWS_VISIBLE LWS_EXTERN int lws_dht_msg_gen (char *out, size_t len, const char *verb, const char *hash, unsigned long long offset, unsigned long long len_val)
LWS_VISIBLE LWS_EXTERN int lws_dht_register_verbs (struct lws_dht_ctx *ctx, const char **verbs, int count, const struct lws_protocols *protocol)
LWS_VISIBLE LWS_EXTERN int lws_dht_notify_subscribers (struct lws_dht_ctx *ctx, const lws_dht_hash_t *hash, const uint8_t *sha256, const uint8_t *payload, size_t payload_len)
LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx * lws_dht_create (const lws_dht_info_t *info)
LWS_VISIBLE LWS_EXTERN void * lws_dht_get_closure (struct lws_dht_ctx *ctx)
LWS_VISIBLE LWS_EXTERN const lws_dht_hash_t * lws_dht_get_myid (struct lws_dht_ctx *ctx)
LWS_VISIBLE LWS_EXTERN void lws_dht_destroy (struct lws_dht_ctx **pctx)
LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx * lws_dht_get_by_name (struct lws_vhost *vhost, const char *name)
LWS_VISIBLE LWS_EXTERN int lws_dht_insert_node (struct lws_dht_ctx *ctx, const lws_dht_hash_t *id, struct sockaddr *sa, size_t salen)
LWS_VISIBLE LWS_EXTERN int lws_dht_ping_node (struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen)
LWS_VISIBLE LWS_EXTERN void lws_dht_test_external_ips (struct lws_dht_ctx *ctx)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_data (struct lws_dht_ctx *ctx, const struct sockaddr *dest, const void *data, size_t len)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_data_at (struct lws_dht_ctx *ctx, const struct sockaddr *dest, uint64_t offset, const void *data, size_t len)
LWS_VISIBLE LWS_EXTERN struct lws_transport_sequencer * lws_dht_get_ts (struct lws_dht_ctx *ctx, const struct sockaddr *dest, size_t salen, int create)
LWS_VISIBLE LWS_EXTERN int lws_dht_search (struct lws_dht_ctx *ctx, const lws_dht_hash_t *id, int port, int af, lws_dht_callback_t *callback, void *closure)
LWS_VISIBLE LWS_EXTERN int lws_dht_nodes (struct lws_dht_ctx *ctx, int af, int *good_return, int *dubious_return, int *cached_return, int *incoming_return)
LWS_VISIBLE LWS_EXTERN int lws_dht_foreach_node (struct lws_dht_ctx *ctx, int af, lws_dht_node_cb_t cb, void *user)
LWS_VISIBLE LWS_EXTERN void lws_dht_dump_tables (struct lws_dht_ctx *ctx)
LWS_VISIBLE LWS_EXTERN int lws_dht_get_nodes (struct lws_dht_ctx *ctx, struct sockaddr_in *sin, int *num, struct sockaddr_in6 *sin6, int *num6)
LWS_VISIBLE LWS_EXTERN int lws_dht_get_external_addr (struct lws_dht_ctx *ctx, struct sockaddr_storage *ss, size_t *sslen)
LWS_VISIBLE LWS_EXTERN lws_dht_hash_t * lws_dht_bep42_id (struct lws_context *cx, const struct sockaddr *sa, uint8_t rand, int type, int len)
LWS_VISIBLE LWS_EXTERN int lws_dht_bep42_check (const lws_dht_hash_t *id, const struct sockaddr *sa)
LWS_VISIBLE LWS_EXTERN int lws_dht_get_fallback_node (struct lws_context *cx, const char *custom_path, char *result, size_t result_len)

Variables

const struct lws_protocols lws_dht_protocol

Detailed Description

Distributed Hash Table (DHT) API

Lws provides a Mainline DHT implementation that can be used to track external IP addresses and find nodes/peers in a P2P network.


Data Structure Documentation

◆ lws_dht_hash

struct lws_dht_hash

struct lws_dht_hash - DHT hash/ID structure

Parameters
typeLWS_DHT_HASH_TYPE_...
lenlength of the ID in bytes
idthe ID bytes

Definition at line 52 of file lws-dht.h.

Collaboration diagram for lws_dht_hash:
Data Fields
uint8_t type
uint8_t len
uint8_t id[]

◆ lws_dht_msg

struct lws_dht_msg

Definition at line 104 of file lws-dht.h.

Collaboration diagram for lws_dht_msg:
Data Fields
char verb[16]
char hash[LWS_GENHASH_LARGEST *2+1]
unsigned long long offset
unsigned long long len
const void * payload
size_t payload_len

◆ lws_dht_stats

struct lws_dht_stats

struct lws_dht_stats - tracking metrics for DHT operation volumes

Definition at line 127 of file lws-dht.h.

Collaboration diagram for lws_dht_stats:
Data Fields
uint32_t tx_ping
uint32_t tx_pong
uint32_t tx_find_node
uint32_t tx_get_peers
uint32_t tx_announce_peer
uint32_t tx_put
uint32_t tx_get
uint32_t rx_ping
uint32_t rx_pong
uint32_t rx_find_node
uint32_t rx_get_peers
uint32_t rx_announce_peer
uint32_t rx_put
uint32_t rx_get
uint32_t rx_drops
uint32_t peer_count

◆ lws_dht_verb_dispatch_args

struct lws_dht_verb_dispatch_args

Definition at line 160 of file lws-dht.h.

Collaboration diagram for lws_dht_verb_dispatch_args:
Data Fields
struct lws_dht_ctx * ctx
const struct lws_dht_msg * msg
const struct sockaddr * from
size_t fromlen
lws_dht_verb_result_t out_precedence

◆ lws_dht_consensus_info

struct lws_dht_consensus_info

struct lws_dht_consensus_info - Passed as data for LWS_DHT_EVENT_EXTERNAL_ADDR(6)

Definition at line 360 of file lws-dht.h.

Collaboration diagram for lws_dht_consensus_info:
Data Fields
struct sockaddr_storage ss
size_t sslen
int num_peers
struct sockaddr_storage peer_ss[8]

◆ lws_dht_error_info

struct lws_dht_error_info

struct lws_dht_error_info - Passed as data for LWS_DHT_EVENT_ERROR

A peer's "y":"e" reply to something we sent it: the BEP 5 code (eg, 203, protocol error) and the peer's description of what it refused. The tid and message point into the datagram being processed, are not NUL-terminated, and are only valid during the callback. The sender is the callback's from, which is as unauthenticated as any datagram source.

Definition at line 376 of file lws-dht.h.

Collaboration diagram for lws_dht_error_info:
Data Fields
const uint8_t * tid

tid of the query being refused

size_t tid_len
const char * message

the peer's description

size_t message_len
int code

BEP 5 error code

◆ lws_dht_info

struct lws_dht_info

struct lws_dht_info - Initialization parameters for DHT

Parameters
vhostvhost to attach UDP wsi to
cbcallback for DHT events
closureuser-defined closure for cb
idDHT ID (optional, NULL = random)
vversion string (optional, NULL = default)
portUDP port to listen on
ipv6enable IPv6
legacyif set, on wire: no multihash, 20-byte assumed
allow_private_adsif set, peers at private, loopback or link-local addresses are admitted to the routing table and exempt from the BEP42 id check (for a LAN deployment, or tests on loopback); by default such peers are refused, as any node on the public internet should
aux0 (sha1), or MULTIHASH_TYPE_...
ifaceinterface to bind to
blacklist_cb(optional) user blacklist cb
hash_cb(optional) user hash cb
capture_announce_cb(optional) user capture announce cb

Definition at line 405 of file lws-dht.h.

Collaboration diagram for lws_dht_info:
Data Fields
struct lws_vhost * vhost
lws_dht_callback_t * cb
void * closure
const lws_dht_hash_t * id
const char * v
const char * name
int port
uint8_t ipv6:1
uint8_t legacy:1
uint8_t allow_private_ads:1
uint8_t aux
const char * iface
const char * fallback_nodes_path
lws_dht_blacklist_cb_t * blacklist_cb
lws_dht_hash_cb_t * hash_cb
lws_dht_capture_announce_cb_t * capture_announce_cb

◆ lws_dht_node_info

struct lws_dht_node_info

struct lws_dht_node_info - one routing table entry, for lws_dht_foreach_node()

Parameters
idthe node id it uses
sathe endpoint we know it at
salenlength of sa
heardseconds since anything came from it, or -1 if nothing has
repliedseconds since it last answered something we sent it, or -1 if it never has
pingedhow many requests we have sent it since it last answered
goodwhether it counts as good, eg, for lws_dht_get_nodes()
bep42_okwhether its id is bound to its address as BEP42 says (or its address is private)

Everything pointed to is only valid during the callback.

Definition at line 574 of file lws-dht.h.

Collaboration diagram for lws_dht_node_info:
Data Fields
const lws_dht_hash_t * id
const struct sockaddr * sa
size_t salen
long heard
long replied
int pinged
uint8_t good:1
uint8_t bep42_ok:1

Macro Definition Documentation

◆ LWS_DHT_SHA1_HASH_LEN

#define LWS_DHT_SHA1_HASH_LEN   20

#include <lws-dht.h>

Definition at line 66 of file lws-dht.h.

◆ LWS_DHT_STAT_BUCKETS

#define LWS_DHT_STAT_BUCKETS   48

#include <lws-dht.h>

Definition at line 122 of file lws-dht.h.

◆ DHT_MAX_PEERS

#define DHT_MAX_PEERS   2048

#include <lws-dht.h>

Definition at line 322 of file lws-dht.h.

◆ DHT_MAX_HASHES

#define DHT_MAX_HASHES   16384

#include <lws-dht.h>

Definition at line 325 of file lws-dht.h.

◆ DHT_MAX_SEARCHES

#define DHT_MAX_SEARCHES   1024

#include <lws-dht.h>

Definition at line 328 of file lws-dht.h.

◆ DHT_SEARCH_EXPIRE_TIME

#define DHT_SEARCH_EXPIRE_TIME   (62 * 60)

#include <lws-dht.h>

Definition at line 331 of file lws-dht.h.

Typedef Documentation

◆ lws_dht_hash_t

typedef struct lws_dht_hash lws_dht_hash_t

#include <lws-dht.h>

struct lws_dht_hash - DHT hash/ID structure

Parameters
typeLWS_DHT_HASH_TYPE_...
lenlength of the ID in bytes
idthe ID bytes

◆ lws_dht_callback_t

typedef void lws_dht_callback_t(void *closure, int event, const lws_dht_hash_t *info_hash, const void *data, size_t data_len, const struct sockaddr *from, size_t fromlen)

#include <lws-dht.h>

lws_dht_callback_t() - DHT event callback

Parameters
closureuser-defined closure pointer
eventLWS_DHT_EVENT_...
info_hashthe hash related to the event
dataevent-specific data
data_lenlength of event-specific data

Definition at line 102 of file lws-dht.h.

◆ lws_dht_blacklist_cb_t

typedef int lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen)

#include <lws-dht.h>

lws_dht_blacklist_cb_t() - DHT blacklist check callback

Parameters
sasockaddr to check
salenlength of sockaddr
Returns
0 if the address is allowed, non-zero if it is blacklisted.

Definition at line 288 of file lws-dht.h.

◆ lws_dht_hash_cb_t

typedef void lws_dht_hash_cb_t(void *hash_return, int hash_size, const void *v1, int len1, const void *v2, int len2, const void *v3, int len3)

#include <lws-dht.h>

lws_dht_hash_cb_t() - Custom hash function for DHT

Parameters
hash_returnwhere to store the hash result
hash_sizesize of the hash result buffer
v1first data chunk
len1length of first chunk
v2second data chunk (optional)
len2length of second chunk
v3third data chunk (optional)
len3length of third chunk

Definition at line 303 of file lws-dht.h.

◆ lws_dht_capture_announce_cb_t

typedef void lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash, const struct sockaddr *fromaddr, unsigned short prt)

#include <lws-dht.h>

lws_dht_capture_announce_cb_t() - Captured announce callback

Parameters
ctxDHT context
hashthe announced hash
fromaddrwhere the announce came from
prtthe port announced

Definition at line 317 of file lws-dht.h.

◆ lws_dht_info_t

typedef struct lws_dht_info lws_dht_info_t

#include <lws-dht.h>

struct lws_dht_info - Initialization parameters for DHT

Parameters
vhostvhost to attach UDP wsi to
cbcallback for DHT events
closureuser-defined closure for cb
idDHT ID (optional, NULL = random)
vversion string (optional, NULL = default)
portUDP port to listen on
ipv6enable IPv6
legacyif set, on wire: no multihash, 20-byte assumed
allow_private_adsif set, peers at private, loopback or link-local addresses are admitted to the routing table and exempt from the BEP42 id check (for a LAN deployment, or tests on loopback); by default such peers are refused, as any node on the public internet should
aux0 (sha1), or MULTIHASH_TYPE_...
ifaceinterface to bind to
blacklist_cb(optional) user blacklist cb
hash_cb(optional) user hash cb
capture_announce_cb(optional) user capture announce cb

◆ lws_dht_node_info_t

#include <lws-dht.h>

struct lws_dht_node_info - one routing table entry, for lws_dht_foreach_node()

Parameters
idthe node id it uses
sathe endpoint we know it at
salenlength of sa
heardseconds since anything came from it, or -1 if nothing has
repliedseconds since it last answered something we sent it, or -1 if it never has
pingedhow many requests we have sent it since it last answered
goodwhether it counts as good, eg, for lws_dht_get_nodes()
bep42_okwhether its id is bound to its address as BEP42 says (or its address is private)

Everything pointed to is only valid during the callback.

◆ lws_dht_node_cb_t

typedef int(* lws_dht_node_cb_t) (void *user, const lws_dht_node_info_t *ni)

#include <lws-dht.h>

Definition at line 585 of file lws-dht.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

#include <lws-dht.h>

Enumerator
LWS_DHT_HASH_TYPE_UNKNOWN 
LWS_DHT_HASH_TYPE_SHA1 
LWS_DHT_HASH_TYPE_SHA256 
LWS_DHT_HASH_TYPE_SHA512 
LWS_DHT_HASH_TYPE_BLAKE3 

Definition at line 58 of file lws-dht.h.

58 {
60 LWS_DHT_HASH_TYPE_SHA1 = 0x11, /* 20 bytes */
61 LWS_DHT_HASH_TYPE_SHA256 = 0x12, /* 32 bytes */
62 LWS_DHT_HASH_TYPE_SHA512 = 0x13, /* 64 bytes */
63 LWS_DHT_HASH_TYPE_BLAKE3 = 0x1e, /* 32 bytes */
64};
@ LWS_DHT_HASH_TYPE_SHA512
Definition lws-dht.h:62
@ LWS_DHT_HASH_TYPE_SHA1
Definition lws-dht.h:60
@ LWS_DHT_HASH_TYPE_SHA256
Definition lws-dht.h:61
@ LWS_DHT_HASH_TYPE_BLAKE3
Definition lws-dht.h:63
@ LWS_DHT_HASH_TYPE_UNKNOWN
Definition lws-dht.h:59

◆ lws_dht_verb_result_t

#include <lws-dht.h>

Enumerator
LWS_DHT_VERB_RESULT_PROCEED 
LWS_DHT_VERB_RESULT_DROP_OLDER 
LWS_DHT_VERB_RESULT_REPLACE_OLDER 
LWS_DHT_VERB_RESULT_PENDING_ASYNC 
LWS_DHT_VERB_RESULT_PASS 
LWS_DHT_VERB_RESULT_ERROR 

Definition at line 113 of file lws-dht.h.

113 {
115 LWS_DHT_VERB_RESULT_DROP_OLDER = 1, /* The incoming object is older than what we have; reject it. */
116 LWS_DHT_VERB_RESULT_REPLACE_OLDER = 2, /* The incoming object is newer; accept and replace. */
117 LWS_DHT_VERB_RESULT_PENDING_ASYNC = 3, /* Validation is asynchronous; hold off on core DHT actions. */
118 LWS_DHT_VERB_RESULT_PASS = 4, /* Pass to the next registered plugin handler */
lws_dht_verb_result_t
Definition lws-dht.h:113
@ LWS_DHT_VERB_RESULT_DROP_OLDER
Definition lws-dht.h:115
@ LWS_DHT_VERB_RESULT_PROCEED
Definition lws-dht.h:114
@ LWS_DHT_VERB_RESULT_ERROR
Definition lws-dht.h:119
@ LWS_DHT_VERB_RESULT_REPLACE_OLDER
Definition lws-dht.h:116
@ LWS_DHT_VERB_RESULT_PASS
Definition lws-dht.h:118
@ LWS_DHT_VERB_RESULT_PENDING_ASYNC
Definition lws-dht.h:117

◆ lws_dht_event_t

#include <lws-dht.h>

enum lws_dht_event_t - DHT events reported via callback

Enumerator
LWS_DHT_EVENT_NONE 
LWS_DHT_EVENT_VALUES 

Peers for requested hash found

LWS_DHT_EVENT_VALUES6 

IPv6 peers for requested hash found

LWS_DHT_EVENT_SEARCH_DONE 

Search operation completed

LWS_DHT_EVENT_SEARCH_DONE6 

IPv6 search operation completed

LWS_DHT_EVENT_EXTERNAL_ADDR 

External IPv4 address determined

LWS_DHT_EVENT_EXTERNAL_ADDR6 

External IPv6 address determined

LWS_DHT_EVENT_DATA 

Arbitrary data payload received

LWS_DHT_EVENT_WRITE_COMPLETED 

Reliable write successful

LWS_DHT_EVENT_WRITE_FAILED 

Reliable write failed

LWS_DHT_EVENT_NOTIFY 

Notification received

LWS_DHT_EVENT_TOKEN 

Security token received from a peer, in reply to our subscribe or get_peers request to it; info_hash is the hash that request was for

LWS_DHT_EVENT_ERROR 

A peer answered a query of ours with a BEP 5 error reply; data is a struct lws_dht_error_info

Definition at line 336 of file lws-dht.h.

336 {
lws_dht_event_t
Definition lws-dht.h:336
@ LWS_DHT_EVENT_VALUES6
Definition lws-dht.h:339
@ LWS_DHT_EVENT_DATA
Definition lws-dht.h:344
@ LWS_DHT_EVENT_NONE
Definition lws-dht.h:337
@ LWS_DHT_EVENT_NOTIFY
Definition lws-dht.h:347
@ LWS_DHT_EVENT_SEARCH_DONE
Definition lws-dht.h:340
@ LWS_DHT_EVENT_VALUES
Definition lws-dht.h:338
@ LWS_DHT_EVENT_EXTERNAL_ADDR6
Definition lws-dht.h:343
@ LWS_DHT_EVENT_EXTERNAL_ADDR
Definition lws-dht.h:342
@ LWS_DHT_EVENT_SEARCH_DONE6
Definition lws-dht.h:341
@ LWS_DHT_EVENT_WRITE_FAILED
Definition lws-dht.h:346
@ LWS_DHT_EVENT_WRITE_COMPLETED
Definition lws-dht.h:345
@ LWS_DHT_EVENT_ERROR
Definition lws-dht.h:352
@ LWS_DHT_EVENT_TOKEN
Definition lws-dht.h:348

Function Documentation

◆ lws_dht_hash_create()

LWS_VISIBLE LWS_EXTERN lws_dht_hash_t * lws_dht_hash_create ( int type,
int len,
const uint8_t * data )

#include <lws-dht.h>

lws_dht_hash_create() - Create a DHT hash from data

Parameters
typeLWS_DHT_HASH_TYPE_...
lenlength of data
datathe data to hash or use as ID

This creates a new lws_dht_hash_t object on the heap.

Returns
pointer to the new hash, or NULL on failure.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_hash_destroy()

LWS_VISIBLE LWS_EXTERN void lws_dht_hash_destroy ( lws_dht_hash_t ** p)

#include <lws-dht.h>

lws_dht_hash_destroy() - Destroy a DHT hash

Parameters
ppointer to the hash pointer to destroy

Frees the hash and sets the pointer to NULL.

◆ lws_dht_get_stats()

LWS_VISIBLE LWS_EXTERN int lws_dht_get_stats ( struct lws_vhost * vh,
struct lws_dht_stats * current,
const struct lws_dht_stats ** history,
int * head )

#include <lws-dht.h>

lws_dht_get_stats() - Retrieve current and historical DHT metrics

Parameters
vhthe vhost the DHT is running on
currentPointer to store current un-rotated metrics, or NULL
historyPointer to receive the internal history array pointer
headReceives the index of the oldest history frame (next to be overwritten)

◆ lws_dht_send_subscribe()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_subscribe ( struct lws_dht_ctx * ctx,
const struct sockaddr * sa,
size_t salen,
const lws_dht_hash_t * infohash,
int want,
int confirm )

#include <lws-dht.h>

lws_dht_send_subscribe() - ask a node for a token to subscribe to a hash

Parameters
ctxthe DHT
sathe node's address
salenlength of sa
infohashthe hash to subscribe to
want0, or the address families wanted in the reply's nodes
confirmunused

The node answers like a get_peers, with nodes and a token. The library chooses the transaction id itself and remembers it, and infohash, with sa, so it can route that reply: the token surfaces as LWS_DHT_EVENT_TOKEN with infohash as its info_hash, to be returned in lws_dht_send_subscribe_confirm() for that hash. Only the one reply to this request, from sa and within the ping timeout, produces the event; a token in any other reply is not reported. A node can only have a few subscribes awaiting their reply at once: the oldest one gives way.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_send_subscribe_confirm()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_subscribe_confirm ( struct lws_dht_ctx * ctx,
const struct sockaddr * sa,
size_t salen,
uint8_t * tid,
size_t tid_len,
const lws_dht_hash_t * infohash,
uint8_t * token,
size_t token_len,
const uint8_t * sha256,
int confirm )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_send_ack()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_ack ( struct lws_dht_ctx * ctx,
const struct sockaddr * sa,
size_t salen,
const uint8_t * tid,
size_t tid_len )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_send_notify()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_notify ( struct lws_dht_ctx * ctx,
const struct sockaddr * sa,
size_t salen,
const uint8_t * tid,
size_t tid_len,
const lws_dht_hash_t * infohash,
const uint8_t * sha256,
const uint8_t * payload,
size_t payload_len )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_msg_parse()

LWS_VISIBLE LWS_EXTERN int lws_dht_msg_parse ( const char * in,
size_t len,
struct lws_dht_msg * out )

#include <lws-dht.h>

lws_dht_msg_parse() - Parse a raw DHT message

Parameters
inraw message buffer
lenlength of raw message
outstruct to populate with parsed data

Safe parsing of DHT command messages. The hash token must be 2 - 128 lowercase hex chars, otherwise the message is rejected.

Returns
0 on success, non-zero on error

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_valid_domain_name()

LWS_VISIBLE LWS_EXTERN int lws_dht_valid_domain_name ( const char * domain)

#include <lws-dht.h>

lws_dht_valid_domain_name() - Check a domain string is a safe DNS name

Parameters
domainNUL-terminated domain string

F-052: domain strings arriving on DHT NOTIFY datagrams are later composed into filesystem paths (zone cache paths, lws_dir walks), so they must be restricted to presentation-format DNS names: 1 - 253 chars of [A-Za-z0-9._-] split into non-empty labels of at most 63 chars, with an optional single trailing root dot. Anything else (path separators, "..", empty labels, junk bytes) is refused.

Returns
1 if the name is safe, 0 otherwise

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_msg_gen()

LWS_VISIBLE LWS_EXTERN int lws_dht_msg_gen ( char * out,
size_t len,
const char * verb,
const char * hash,
unsigned long long offset,
unsigned long long len_val )

#include <lws-dht.h>

lws_dht_msg_gen() - Generate a raw DHT message

Parameters
outbuffer to write message to
verbe.g. "PUT", "GET", "REPLICATE"
hashthe hex SHA1 associated
offsetthe byte offset
len_valthe length val

Generate a complete DHT payload with a space separated verb schema.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_register_verbs()

LWS_VISIBLE LWS_EXTERN int lws_dht_register_verbs ( struct lws_dht_ctx * ctx,
const char ** verbs,
int count,
const struct lws_protocols * protocol )

#include <lws-dht.h>

lws_dht_register_verbs() - Register custom verb handlers

Parameters
ctxDHT context
verbsarray of verb string names
countnumber of verbs in array
protocolthe unified protocol handler owning these verbs
Returns
0 on success, non-zero on error

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_notify_subscribers()

LWS_VISIBLE LWS_EXTERN int lws_dht_notify_subscribers ( struct lws_dht_ctx * ctx,
const lws_dht_hash_t * hash,
const uint8_t * sha256,
const uint8_t * payload,
size_t payload_len )

#include <lws-dht.h>

lws_dht_notify_subscribers() - Notify subscribers of a hash change

Parameters
ctxDHT context
hashthe hash that changed
sha256the new sha256 of the content
Returns
number of subscribers notified, or negative on error

◆ lws_dht_create()

LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx * lws_dht_create ( const lws_dht_info_t * info)

#include <lws-dht.h>

lws_dht_create() - Create a DHT context

Parameters
infoinitialization parameters
Returns
pointer to DHT context or NULL on failure.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_closure()

LWS_VISIBLE LWS_EXTERN void * lws_dht_get_closure ( struct lws_dht_ctx * ctx)

#include <lws-dht.h>

lws_dht_get_closure() - Get the user closure pointer

Parameters
ctxDHT context
Returns
the closure pointer

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_myid()

LWS_VISIBLE LWS_EXTERN const lws_dht_hash_t * lws_dht_get_myid ( struct lws_dht_ctx * ctx)

#include <lws-dht.h>

lws_dht_get_myid() - Get the local node's DHT ID hash

Parameters
ctxDHT context
Returns
the local node's ID hash

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_destroy()

LWS_VISIBLE LWS_EXTERN void lws_dht_destroy ( struct lws_dht_ctx ** pctx)

#include <lws-dht.h>

lws_dht_destroy() - Destroy a DHT context

Parameters
pctxpointer to the DHT context pointer to destroy

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_by_name()

LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx * lws_dht_get_by_name ( struct lws_vhost * vhost,
const char * name )

#include <lws-dht.h>

lws_dht_get_by_name() - Get a specific DHT context by name

Parameters
vhostvhost the DHT is bound to
namename to match against
Returns
pointer to DHT context or NULL on failure.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_insert_node()

LWS_VISIBLE LWS_EXTERN int lws_dht_insert_node ( struct lws_dht_ctx * ctx,
const lws_dht_hash_t * id,
struct sockaddr * sa,
size_t salen )

#include <lws-dht.h>

lws_dht_insert_node() - Manually insert a node into the DHT

Parameters
ctxDHT context
idID of the node
sasockaddr of the node
salenlength of sockaddr
Returns
1 if successful, 0 if it already exists, or -1 on error.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_ping_node()

LWS_VISIBLE LWS_EXTERN int lws_dht_ping_node ( struct lws_dht_ctx * ctx,
struct sockaddr * sa,
size_t salen )

#include <lws-dht.h>

lws_dht_ping_node() - Ping a node to verify it is alive

Parameters
ctxDHT context
sasockaddr of the node
salenlength of sockaddr
Returns
number of bytes sent on success, or -1 on error.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_test_external_ips()

LWS_VISIBLE LWS_EXTERN void lws_dht_test_external_ips ( struct lws_dht_ctx * ctx)

#include <lws-dht.h>

lws_dht_test_external_ips() - actively trigger IP verification probes to routing table

Parameters
ctxthe dht context

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_send_data()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_data ( struct lws_dht_ctx * ctx,
const struct sockaddr * dest,
const void * data,
size_t len )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_send_data_at()

LWS_VISIBLE LWS_EXTERN int lws_dht_send_data_at ( struct lws_dht_ctx * ctx,
const struct sockaddr * dest,
uint64_t offset,
const void * data,
size_t len )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_ts()

LWS_VISIBLE LWS_EXTERN struct lws_transport_sequencer * lws_dht_get_ts ( struct lws_dht_ctx * ctx,
const struct sockaddr * dest,
size_t salen,
int create )

#include <lws-dht.h>

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_search()

LWS_VISIBLE LWS_EXTERN int lws_dht_search ( struct lws_dht_ctx * ctx,
const lws_dht_hash_t * id,
int port,
int af,
lws_dht_callback_t * callback,
void * closure )

#include <lws-dht.h>

lws_dht_search() - Perform an asynchronous search for a hash

Parameters
ctxDHT context
idhash to search for
portport to search on
afaddress family (AF_INET, AF_INET6, or 0 for both)
callbacksearch completion/result callback
closureclosure for callback

This performs an iterative, asynchronous search for the requested hash. If port is non-zero, it also announces our availability for this hash.

Results (peers/values) are delivered to the callback as they are found via LWS_DHT_EVENT_VALUES or LWS_DHT_EVENT_VALUES6 events.

The callback is also called with event LWS_DHT_EVENT_SEARCH_DONE (or LWS_DHT_EVENT_SEARCH_DONE6) when the search operation has exhausted all potential nodes or reached a timeout.

Returns
1 if search started successfully, 0 if answered locally (callback still called), or -1 on error.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_nodes()

LWS_VISIBLE LWS_EXTERN int lws_dht_nodes ( struct lws_dht_ctx * ctx,
int af,
int * good_return,
int * dubious_return,
int * cached_return,
int * incoming_return )

#include <lws-dht.h>

lws_dht_nodes() - Get statistics about DHT nodes

Parameters
ctxDHT context
afaddress family (AF_INET, AF_INET6)
good_returncount of known good nodes
dubious_returncount of dubious nodes
cached_returncount of cached nodes
incoming_returncount of incoming nodes
Returns
total number of good + dubious nodes.

◆ lws_dht_foreach_node()

LWS_VISIBLE LWS_EXTERN int lws_dht_foreach_node ( struct lws_dht_ctx * ctx,
int af,
lws_dht_node_cb_t cb,
void * user )

#include <lws-dht.h>

lws_dht_foreach_node() - visit each node in a routing table

Parameters
ctxDHT context
afwhich table, AF_INET or AF_INET6
cbcalled with each node, return nonzero to stop
userpassed to cb

Lets the application see, eg, why a node it expects to be good is not. The table must not be changed from cb.

Returns
how many nodes cb was called with.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_dump_tables()

LWS_VISIBLE LWS_EXTERN void lws_dht_dump_tables ( struct lws_dht_ctx * ctx)

#include <lws-dht.h>

lws_dht_dump_tables() - Log the state of DHT routing tables

Parameters
ctxDHT context

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_nodes()

LWS_VISIBLE LWS_EXTERN int lws_dht_get_nodes ( struct lws_dht_ctx * ctx,
struct sockaddr_in * sin,
int * num,
struct sockaddr_in6 * sin6,
int * num6 )

#include <lws-dht.h>

lws_dht_get_nodes() - Get a list of known nodes

Parameters
ctxDHT context
sinbuffer for IPv4 nodes
numin/out count for IPv4 nodes
sin6buffer for IPv6 nodes
num6in/out count for IPv6 nodes
Returns
total number of nodes filled (v4 + v6).

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_external_addr()

LWS_VISIBLE LWS_EXTERN int lws_dht_get_external_addr ( struct lws_dht_ctx * ctx,
struct sockaddr_storage * ss,
size_t * sslen )

#include <lws-dht.h>

lws_dht_get_external_addr() - Get our external IP and port as determined by STUN

Parameters
ctxDHT context
ssbuffer for external address and port
sslenlength of buffer/written address
Returns
0 on success, or -1 if the address and port are not yet determined.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_bep42_id()

LWS_VISIBLE LWS_EXTERN lws_dht_hash_t * lws_dht_bep42_id ( struct lws_context * cx,
const struct sockaddr * sa,
uint8_t rand,
int type,
int len )

#include <lws-dht.h>

lws_dht_bep42_id() - make a node id bound to an address (BEP42)

Parameters
cxthe lws context (its randomness is used)
sathe node's public address (AF_INET or AF_INET6)
randthe random byte that ends the id (BEP42's "r")
typeLWS_DHT_HASH_TYPE_... of the id to make
lenthe id length in bytes for that type (20 for sha1)

BEP42 ties a node id to the node's IP: the first 21 bits are the CRC32C of the masked address with three bits of rand folded in, the last byte is rand, the rest is random. A peer can then check the id it is told against the address it hears it from, which is what stops one host choosing ids to fill a bucket. lws applies the rule to the first three and last byte of an id of any length.

Returns
a new id to free with lws_dht_hash_destroy(), or NULL

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_bep42_check()

LWS_VISIBLE LWS_EXTERN int lws_dht_bep42_check ( const lws_dht_hash_t * id,
const struct sockaddr * sa )

#include <lws-dht.h>

lws_dht_bep42_check() - does a node id conform to BEP42 for its address

Parameters
idthe node id the peer claims
sathe address the claim came from
Returns
1 if the id's first 21 bits are the BEP42 derivation for sa and the id's last byte, 0 if not (or the family is unknown)

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_dht_get_fallback_node()

LWS_VISIBLE LWS_EXTERN int lws_dht_get_fallback_node ( struct lws_context * cx,
const char * custom_path,
char * result,
size_t result_len )

#include <lws-dht.h>

lws_dht_get_fallback_node() - Retrieves a default DHT node from the system installation

Parameters
cxlws_context to seed the randomizer
resultbuffer to write the randomly chosen fallback node IP:port
result_lensize of the output buffer
Returns
0 on success, or -1 if the fallback file could not be read.

Variable Documentation

◆ lws_dht_protocol

const struct lws_protocols lws_dht_protocol
extern

#include <lws-dht.h>