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>
31
39
40struct lws_dht_ctx;
41
42extern const struct lws_protocols lws_dht_protocol;
43
51typedef struct lws_dht_hash {
52 uint8_t type; /* LWS_DHT_HASH_TYPE_... */
53 uint8_t len;
54 uint8_t id[];
56
57enum {
59 LWS_DHT_HASH_TYPE_SHA1 = 0x11, /* 20 bytes */
60 LWS_DHT_HASH_TYPE_SHA256 = 0x12, /* 32 bytes */
61 LWS_DHT_HASH_TYPE_SHA512 = 0x13, /* 64 bytes */
62 LWS_DHT_HASH_TYPE_BLAKE3 = 0x1e, /* 32 bytes */
63};
64
77lws_dht_hash_create(int type, int len, const uint8_t *data);
78
88
98typedef void
99lws_dht_callback_t(void *closure, int event, const lws_dht_hash_t *info_hash, const void *data, size_t data_len);
100
109typedef int
110lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen);
111
124typedef void
125lws_dht_hash_cb_t(void *hash_return, int hash_size,
126 const void *v1, int len1,
127 const void *v2, int len2,
128 const void *v3, int len3);
129
138typedef void
139lws_dht_capture_announce_cb_t(struct lws_dht_ctx *ctx, lws_dht_hash_t *hash,
140 const struct sockaddr *fromaddr,
141 unsigned short prt);
142
143/* The maximum number of peers we store for a given hash. */
144#define DHT_MAX_PEERS 2048
145
146/* The maximum number of hashes we're willing to track. */
147#define DHT_MAX_HASHES 16384
148
149/* The maximum number of searches we keep data about. */
150#define DHT_MAX_SEARCHES 1024
151
152/* The time after which we consider a search to be expirable. */
153#define DHT_SEARCH_EXPIRE_TIME (62 * 60)
154
167
168
201
209LWS_VISIBLE LWS_EXTERN struct lws_dht_ctx *
211
218lws_dht_destroy(struct lws_dht_ctx **pctx);
219
231lws_dht_insert_node(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id,
232 struct sockaddr *sa, size_t salen);
233
244lws_dht_ping_node(struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen);
245
269lws_dht_search(struct lws_dht_ctx *ctx, const lws_dht_hash_t *id, int port, int af,
270 lws_dht_callback_t *callback, void *closure);
271
285lws_dht_nodes(struct lws_dht_ctx *ctx, int af, int *good_return, int *dubious_return,
286 int *cached_return, int *incoming_return);
287
294lws_dht_dump_tables(struct lws_dht_ctx *ctx);
295
308lws_dht_get_nodes(struct lws_dht_ctx *ctx, struct sockaddr_in *sin, int *num,
309 struct sockaddr_in6 *sin6, int *num6);
310
321lws_dht_get_external_addr(struct lws_dht_ctx *ctx, struct sockaddr_storage *ss, size_t *sslen);
322
323#endif /* __LWS_DHT_H__ */
324
uint8_t legacy
Definition lws-dht.h:194
uint8_t type
Definition lws-dht.h:52
lws_dht_hash_cb_t * hash_cb
Definition lws-dht.h:198
uint8_t len
Definition lws-dht.h:53
uint8_t aux
Definition lws-dht.h:195
const lws_dht_hash_t * id
Definition lws-dht.h:190
lws_dht_blacklist_cb_t * blacklist_cb
Definition lws-dht.h:197
void * closure
Definition lws-dht.h:189
const char * iface
Definition lws-dht.h:196
lws_dht_callback_t * cb
Definition lws-dht.h:188
lws_dht_capture_announce_cb_t * capture_announce_cb
Definition lws-dht.h:199
uint8_t ipv6
Definition lws-dht.h:193
struct lws_vhost * vhost
Definition lws-dht.h:187
const char * v
Definition lws-dht.h:191
LWS_VISIBLE LWS_EXTERN void lws_dht_hash_destroy(lws_dht_hash_t **p)
int lws_dht_blacklist_cb_t(const struct sockaddr *sa, size_t salen)
Definition lws-dht.h:110
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)
const struct lws_protocols lws_dht_protocol
struct lws_dht_info lws_dht_info_t
LWS_VISIBLE LWS_EXTERN void lws_dht_destroy(struct lws_dht_ctx **pctx)
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_ping_node(struct lws_dht_ctx *ctx, struct sockaddr *sa, size_t salen)
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)
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:139
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:158
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:125
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)
void lws_dht_callback_t(void *closure, int event, const lws_dht_hash_t *info_hash, const void *data, size_t data_len)
Definition lws-dht.h:99
@ LWS_DHT_EVENT_VALUES6
Definition lws-dht.h:161
@ LWS_DHT_EVENT_NONE
Definition lws-dht.h:159
@ LWS_DHT_EVENT_SEARCH_DONE
Definition lws-dht.h:162
@ LWS_DHT_EVENT_VALUES
Definition lws-dht.h:160
@ LWS_DHT_EVENT_EXTERNAL_ADDR6
Definition lws-dht.h:165
@ LWS_DHT_EVENT_EXTERNAL_ADDR
Definition lws-dht.h:164
@ LWS_DHT_EVENT_SEARCH_DONE6
Definition lws-dht.h:163
@ LWS_DHT_HASH_TYPE_SHA512
Definition lws-dht.h:61
@ LWS_DHT_HASH_TYPE_SHA1
Definition lws-dht.h:59
@ LWS_DHT_HASH_TYPE_SHA256
Definition lws-dht.h:60
@ LWS_DHT_HASH_TYPE_BLAKE3
Definition lws-dht.h:62
@ LWS_DHT_HASH_TYPE_UNKNOWN
Definition lws-dht.h:58
#define LWS_EXTERN
#define LWS_VISIBLE