libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-dht.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2011 by Juliusz Chroboczek
3 * Copyright (c) 2026 Andy Green <andy@warmcat.com>
4 * Adaptation for lws, cleaning, modernization
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#if !defined(__LWS_DHT_H__)
26#define __LWS_DHT_H__
27
28#include <stddef.h>
29#include <stdint.h>
30#include <stdio.h>
32
40
41struct lws_dht_ctx;
42
43extern const struct lws_protocols lws_dht_protocol;
44
52typedef struct lws_dht_hash {
53 uint8_t type; /* LWS_DHT_HASH_TYPE_... */
55 uint8_t id[];
57
58enum {
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};
65
66#define LWS_DHT_SHA1_HASH_LEN 20
67
80lws_dht_hash_create(int type, int len, const uint8_t *data);
81
91
101typedef void
102lws_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);
103
105 char verb[16];
107 unsigned long long offset;
108 unsigned long long len;
109 const void *payload;
111};
112
113typedef enum {
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 */
121
122#define LWS_DHT_STAT_BUCKETS 48
123
147
157lws_dht_get_stats(struct lws_vhost *vh, struct lws_dht_stats *current,
158 const struct lws_dht_stats **history, int *head);
159
161 struct lws_dht_ctx *ctx;
162 const struct lws_dht_msg *msg;
163 const struct sockaddr *from;
164 size_t fromlen;
165
167};
168
189lws_dht_send_subscribe(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
190 const lws_dht_hash_t *infohash, int want, int confirm);
191
193lws_dht_send_subscribe_confirm(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
194 uint8_t *tid, size_t tid_len, const lws_dht_hash_t *infohash,
195 uint8_t *token, size_t token_len, const uint8_t *sha256, int confirm);
196
198lws_dht_send_ack(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
199 const uint8_t *tid, size_t tid_len);
200
202lws_dht_send_notify(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
203 const uint8_t *tid, size_t tid_len,
204 const lws_dht_hash_t *infohash, const uint8_t *sha256,
205 const uint8_t *payload, size_t payload_len);
206
207
221lws_dht_msg_parse(const char *in, size_t len, struct lws_dht_msg *out);
222
238lws_dht_valid_domain_name(const char *domain);
239
252lws_dht_msg_gen(char *out, size_t len, const char *verb, const char *hash, unsigned long long offset, unsigned long long len_val);
253
265lws_dht_register_verbs(struct lws_dht_ctx *ctx, const char **verbs, int count, const struct lws_protocols *protocol);
266
277lws_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);
278
287typedef int
288lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen);
289
302typedef void
303lws_dht_hash_cb_t(void *hash_return, int hash_size,
304 const void *v1, int len1,
305 const void *v2, int len2,
306 const void *v3, int len3);
307
316typedef void
317lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash,
318 const struct sockaddr *fromaddr,
319 unsigned short prt);
320
321/* The maximum number of peers we store for a given hash. */
322#define DHT_MAX_PEERS 2048
323
324/* The maximum number of hashes we're willing to track. */
325#define DHT_MAX_HASHES 16384
326
327/*
328 * By default, how many of those one source host (an IPv4 address or an IPv6
329 * /64) may have us start tracking, by announce_peer or subscribe_confirm
330 */
331#define DHT_MAX_HASHES_PER_SOURCE (DHT_MAX_HASHES / 16)
332
333/* The maximum number of searches we keep data about. */
334#define DHT_MAX_SEARCHES 1024
335
336/* The time after which we consider a search to be expirable. */
337#define DHT_SEARCH_EXPIRE_TIME (62 * 60)
338
366
376
387 const uint8_t *tid;
388 size_t tid_len;
389 const char *message;
391 int code;
392};
393
446
454LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx *
456
465lws_dht_get_closure(struct lws_dht_ctx *ctx);
466
475lws_dht_get_myid(struct lws_dht_ctx *ctx);
476
483lws_dht_destroy(struct lws_dht_ctx **pctx);
484
493LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx *
494lws_dht_get_by_name(struct lws_vhost *vhost, const char *name);
495
507lws_dht_insert_node(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id,
508 struct sockaddr *sa, size_t salen);
509
520lws_dht_ping_node(struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen);
521
528lws_dht_test_external_ips(struct lws_dht_ctx *ctx);
529
547
567lws_dht_send_data(struct lws_dht_ctx *ctx, const struct sockaddr *dest, const void *data, size_t len);
568
581lws_dht_send_data_at(struct lws_dht_ctx *ctx, const struct sockaddr *dest, uint64_t offset, const void *data, size_t len);
582
583LWS_VISIBLE LWS_EXTERN struct lws_transport_sequencer *
584lws_dht_get_ts(struct lws_dht_ctx *ctx, const struct sockaddr *dest, size_t salen, int create);
585
610lws_dht_search(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id, int port, int af,
611 lws_dht_callback_t *callback, void *closure);
612
626lws_dht_nodes(struct lws_dht_ctx *ctx, int af, int *good_return, int *dubious_return,
627 int *cached_return, int *incoming_return);
628
655
656typedef int (*lws_dht_node_cb_t)(void *user, const lws_dht_node_info_t *ni);
657
672lws_dht_foreach_node(struct lws_dht_ctx *ctx, int af, lws_dht_node_cb_t cb,
673 void *user);
674
696lws_dht_foreach_search_node(struct lws_dht_ctx *ctx,
697 const lws_dht_hash_t *target, int af,
698 lws_dht_node_cb_t cb, void *user);
699
706lws_dht_dump_tables(struct lws_dht_ctx *ctx);
707
720lws_dht_get_nodes(struct lws_dht_ctx *ctx, struct sockaddr_in *sin, int *num,
721 struct sockaddr_in6 *sin6, int *num6);
722
733lws_dht_get_external_addr(struct lws_dht_ctx *ctx, struct sockaddr_storage *ss, size_t *sslen);
734
754lws_dht_bep42_id(struct lws_context *cx, const struct sockaddr *sa,
755 uint8_t rand, int type, int len);
756
772lws_dht_same_host(const struct sockaddr *a, const struct sockaddr *b);
773
784lws_dht_bep42_check(const lws_dht_hash_t *id, const struct sockaddr *sa);
785
796lws_dht_get_fallback_node(struct lws_context *cx, const char *custom_path, char *result, size_t result_len);
797
798#endif /* __LWS_DHT_H__ */
799
uint32_t rx_announce_peer
Definition lws-dht.h:140
size_t payload_len
Definition lws-dht.h:110
struct sockaddr_storage peer_ss[8]
Definition lws-dht.h:374
struct lws_dht_ctx * ctx
Definition lws-dht.h:161
uint32_t rx_get_peers
Definition lws-dht.h:139
uint8_t legacy
Definition lws-dht.h:435
uint32_t peer_count
Definition lws-dht.h:145
uint8_t type
Definition lws-dht.h:53
unsigned int max_hashes_per_source
Definition lws-dht.h:443
uint32_t tx_ping
Definition lws-dht.h:128
const char * message
Definition lws-dht.h:389
uint32_t tx_pong
Definition lws-dht.h:129
lws_dht_hash_cb_t * hash_cb
Definition lws-dht.h:441
const struct sockaddr * from
Definition lws-dht.h:163
uint32_t tx_put
Definition lws-dht.h:133
const char * fallback_nodes_path
Definition lws-dht.h:439
unsigned int blacklist_secs
Definition lws-dht.h:444
const void * payload
Definition lws-dht.h:109
uint8_t len
Definition lws-dht.h:54
const char * name
Definition lws-dht.h:432
uint8_t aux
Definition lws-dht.h:437
char hash[LWS_GENHASH_LARGEST *2+1]
Definition lws-dht.h:106
uint32_t rx_ping
Definition lws-dht.h:136
const lws_dht_hash_t * id
Definition lws-dht.h:430
uint32_t rx_drops
Definition lws-dht.h:144
uint8_t allow_private_ads
Definition lws-dht.h:436
lws_dht_blacklist_cb_t * blacklist_cb
Definition lws-dht.h:440
uint32_t rx_pong
Definition lws-dht.h:137
lws_dht_verb_result_t out_precedence
Definition lws-dht.h:166
struct sockaddr_storage ss
Definition lws-dht.h:371
unsigned long long len
Definition lws-dht.h:108
uint32_t tx_get_peers
Definition lws-dht.h:131
uint32_t rx_find_node
Definition lws-dht.h:138
uint8_t bep42_ok
Definition lws-dht.h:653
uint32_t rx_get
Definition lws-dht.h:142
void * closure
Definition lws-dht.h:429
uint32_t tx_find_node
Definition lws-dht.h:130
uint32_t tx_announce_peer
Definition lws-dht.h:132
const lws_dht_hash_t * id
Definition lws-dht.h:646
unsigned long long offset
Definition lws-dht.h:107
const struct lws_dht_msg * msg
Definition lws-dht.h:162
const char * iface
Definition lws-dht.h:438
uint32_t tx_get
Definition lws-dht.h:134
uint32_t rx_put
Definition lws-dht.h:141
lws_dht_callback_t * cb
Definition lws-dht.h:428
char verb[16]
Definition lws-dht.h:105
const struct sockaddr * sa
Definition lws-dht.h:647
lws_dht_capture_announce_cb_t * capture_announce_cb
Definition lws-dht.h:442
uint8_t ipv6
Definition lws-dht.h:434
struct lws_vhost * vhost
Definition lws-dht.h:427
const char * v
Definition lws-dht.h:431
const uint8_t * tid
Definition lws-dht.h:387
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_hash_destroy(lws_dht_hash_t **p)
LWS_VISIBLE LWS_EXTERN int lws_dht_get_fallback_node(struct lws_context *cx, const char *custom_path, char *result, size_t result_len)
int lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen)
Definition lws-dht.h:288
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_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_msg_parse(const char *in, size_t len, struct lws_dht_msg *out)
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)
struct lws_dht_node_info lws_dht_node_info_t
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)
const struct lws_protocols lws_dht_protocol
struct lws_dht_info lws_dht_info_t
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_foreach_search_node(struct lws_dht_ctx *ctx, const lws_dht_hash_t *target, int af, lws_dht_node_cb_t cb, void *user)
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 void lws_dht_destroy(struct lws_dht_ctx **pctx)
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)
Definition lws-dht.h:102
LWS_VISIBLE LWS_EXTERN const lws_dht_hash_t * lws_dht_get_myid(struct lws_dht_ctx *ctx)
LWS_VISIBLE LWS_EXTERN lws_dht_hash_t * lws_dht_hash_create(int type, int len, const uint8_t *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)
lws_dht_verb_result_t
Definition lws-dht.h:113
LWS_VISIBLE LWS_EXTERN int lws_dht_ping_node(struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen)
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 void * lws_dht_get_closure(struct lws_dht_ctx *ctx)
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_register_verbs(struct lws_dht_ctx *ctx, const char **verbs, int count, const struct lws_protocols *protocol)
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)
void lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash, const struct sockaddr *fromaddr, unsigned short prt)
Definition lws-dht.h:317
LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx * lws_dht_create(const lws_dht_info_t *info)
lws_dht_event_t
Definition lws-dht.h:342
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)
Definition lws-dht.h:303
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_bep42_check(const lws_dht_hash_t *id, const struct sockaddr *sa)
int(* lws_dht_node_cb_t)(void *user, const lws_dht_node_info_t *ni)
Definition lws-dht.h:656
struct lws_dht_hash lws_dht_hash_t
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_same_host(const struct sockaddr *a, const struct sockaddr *b)
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_valid_domain_name(const char *domain)
lws_dht_send_result
Definition lws-dht.h:534
LWS_VISIBLE LWS_EXTERN void lws_dht_dump_tables(struct lws_dht_ctx *ctx)
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 struct lws_dht_ctx * lws_dht_get_by_name(struct lws_vhost *vhost, const char *name)
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 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 void lws_dht_test_external_ips(struct lws_dht_ctx *ctx)
@ 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_VALUES6
Definition lws-dht.h:345
@ LWS_DHT_EVENT_DATA
Definition lws-dht.h:350
@ LWS_DHT_EVENT_WRITEABLE
Definition lws-dht.h:361
@ LWS_DHT_EVENT_NONE
Definition lws-dht.h:343
@ LWS_DHT_EVENT_NOTIFY
Definition lws-dht.h:353
@ LWS_DHT_EVENT_SEARCH_DONE
Definition lws-dht.h:346
@ LWS_DHT_EVENT_VALUES
Definition lws-dht.h:344
@ LWS_DHT_EVENT_EXTERNAL_ADDR6
Definition lws-dht.h:349
@ LWS_DHT_EVENT_EXTERNAL_ADDR
Definition lws-dht.h:348
@ LWS_DHT_EVENT_SEARCH_DONE6
Definition lws-dht.h:347
@ LWS_DHT_EVENT_WRITE_FAILED
Definition lws-dht.h:352
@ LWS_DHT_EVENT_WRITE_COMPLETED
Definition lws-dht.h:351
@ LWS_DHT_EVENT_ERROR
Definition lws-dht.h:358
@ LWS_DHT_EVENT_TOKEN
Definition lws-dht.h:354
@ LWS_DHT_SEND_REFUSED
Definition lws-dht.h:538
@ LWS_DHT_SEND_QUEUED
Definition lws-dht.h:535
@ LWS_DHT_SEND_BUSY
Definition lws-dht.h:541
@ 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
#define LWS_GENHASH_LARGEST
Definition lws-genhash.h:70
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_VISIBLE