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
170lws_dht_send_subscribe(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
171 uint8_t *tid, size_t tid_len, const lws_dht_hash_t *infohash,
172 int want, int confirm);
173
175lws_dht_send_subscribe_confirm(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
176 uint8_t *tid, size_t tid_len, const lws_dht_hash_t *infohash,
177 uint8_t *token, size_t token_len, const uint8_t *sha256, int confirm);
178
180lws_dht_send_ack(struct lws_dht_ctx *ctx, const struct sockaddr *sa, size_t salen,
181 const uint8_t *tid, size_t tid_len);
182
183
184
197lws_dht_msg_parse(const char *in, size_t len, struct lws_dht_msg *out);
198
211lws_dht_msg_gen(char *out, size_t len, const char *verb, const char *hash, unsigned long long offset, unsigned long long len_val);
212
224lws_dht_register_verbs(struct lws_dht_ctx *ctx, const char **verbs, int count, const struct lws_protocols *protocol);
225
236lws_dht_notify_subscribers(struct lws_dht_ctx *ctx, const lws_dht_hash_t *hash, const uint8_t *sha256);
237
246typedef int
247lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen);
248
261typedef void
262lws_dht_hash_cb_t(void *hash_return, int hash_size,
263 const void *v1, int len1,
264 const void *v2, int len2,
265 const void *v3, int len3);
266
275typedef void
276lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash,
277 const struct sockaddr *fromaddr,
278 unsigned short prt);
279
280/* The maximum number of peers we store for a given hash. */
281#define DHT_MAX_PEERS 2048
282
283/* The maximum number of hashes we're willing to track. */
284#define DHT_MAX_HASHES 16384
285
286/* The maximum number of searches we keep data about. */
287#define DHT_MAX_SEARCHES 1024
288
289/* The time after which we consider a search to be expirable. */
290#define DHT_SEARCH_EXPIRE_TIME (62 * 60)
291
309
310
345
353LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx *
355
364lws_dht_get_closure(struct lws_dht_ctx *ctx);
365
374lws_dht_get_myid(struct lws_dht_ctx *ctx);
375
382lws_dht_destroy(struct lws_dht_ctx **pctx);
383
392LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx *
393lws_dht_get_by_name(struct lws_vhost *vhost, const char *name);
394
406lws_dht_insert_node(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id,
407 struct sockaddr *sa, size_t salen);
408
419lws_dht_ping_node(struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen);
420
422lws_dht_send_data(struct lws_dht_ctx *ctx, const struct sockaddr *dest, const void *data, size_t len);
423
425lws_dht_send_data_at(struct lws_dht_ctx *ctx, const struct sockaddr *dest, uint64_t offset, const void *data, size_t len);
426
427LWS_VISIBLE LWS_EXTERN struct lws_transport_sequencer *
428lws_dht_get_ts(struct lws_dht_ctx *ctx, const struct sockaddr *dest, size_t salen, int create);
429
453lws_dht_search(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id, int port, int af,
454 lws_dht_callback_t *callback, void *closure);
455
469lws_dht_nodes(struct lws_dht_ctx *ctx, int af, int *good_return, int *dubious_return,
470 int *cached_return, int *incoming_return);
471
478lws_dht_dump_tables(struct lws_dht_ctx *ctx);
479
492lws_dht_get_nodes(struct lws_dht_ctx *ctx, struct sockaddr_in *sin, int *num,
493 struct sockaddr_in6 *sin6, int *num6);
494
505lws_dht_get_external_addr(struct lws_dht_ctx *ctx, struct sockaddr_storage *ss, size_t *sslen);
506
517lws_dht_get_fallback_node(struct lws_context *cx, const char *custom_path, char *result, size_t result_len);
518
519#endif /* __LWS_DHT_H__ */
520
uint32_t rx_announce_peer
Definition lws-dht.h:140
size_t payload_len
Definition lws-dht.h:110
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:337
uint32_t peer_count
Definition lws-dht.h:145
uint8_t type
Definition lws-dht.h:53
uint32_t tx_ping
Definition lws-dht.h:128
uint32_t tx_pong
Definition lws-dht.h:129
lws_dht_hash_cb_t * hash_cb
Definition lws-dht.h:342
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:340
const void * payload
Definition lws-dht.h:109
uint8_t len
Definition lws-dht.h:54
const char * name
Definition lws-dht.h:334
uint8_t aux
Definition lws-dht.h:338
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:332
uint32_t rx_drops
Definition lws-dht.h:144
lws_dht_blacklist_cb_t * blacklist_cb
Definition lws-dht.h:341
uint32_t rx_pong
Definition lws-dht.h:137
lws_dht_verb_result_t out_precedence
Definition lws-dht.h:166
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
uint32_t rx_get
Definition lws-dht.h:142
void * closure
Definition lws-dht.h:331
uint32_t tx_find_node
Definition lws-dht.h:130
uint32_t tx_announce_peer
Definition lws-dht.h:132
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:339
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:330
char verb[16]
Definition lws-dht.h:105
lws_dht_capture_announce_cb_t * capture_announce_cb
Definition lws-dht.h:343
uint8_t ipv6
Definition lws-dht.h:336
struct lws_vhost * vhost
Definition lws-dht.h:329
const char * v
Definition lws-dht.h:333
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:247
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 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 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)
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:276
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:295
LWS_VISIBLE LWS_EXTERN int lws_dht_notify_subscribers(struct lws_dht_ctx *ctx, const lws_dht_hash_t *hash, const uint8_t *sha256)
LWS_VISIBLE LWS_EXTERN int lws_dht_send_subscribe(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, int want, int confirm)
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:262
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)
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 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_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:298
@ LWS_DHT_EVENT_DATA
Definition lws-dht.h:303
@ LWS_DHT_EVENT_NONE
Definition lws-dht.h:296
@ LWS_DHT_EVENT_NOTIFY
Definition lws-dht.h:306
@ LWS_DHT_EVENT_SEARCH_DONE
Definition lws-dht.h:299
@ LWS_DHT_EVENT_VALUES
Definition lws-dht.h:297
@ LWS_DHT_EVENT_EXTERNAL_ADDR6
Definition lws-dht.h:302
@ LWS_DHT_EVENT_EXTERNAL_ADDR
Definition lws-dht.h:301
@ LWS_DHT_EVENT_SEARCH_DONE6
Definition lws-dht.h:300
@ LWS_DHT_EVENT_WRITE_FAILED
Definition lws-dht.h:305
@ LWS_DHT_EVENT_WRITE_COMPLETED
Definition lws-dht.h:304
@ LWS_DHT_EVENT_TOKEN
Definition lws-dht.h:307
@ 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:61
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_VISIBLE