libdht
node.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 naturalpolice
3  * SPDX-License-Identifier: MIT
4  *
5  * Licensed under the MIT License (see LICENSE).
6  */
7 
15 #ifndef DHT_NODE_H_
16 #define DHT_NODE_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 #ifdef _WIN32
24 typedef int socklen_t;
25 #include <winsock2.h>
26 #include <ws2ipdef.h>
27 #else
28 #include <sys/socket.h>
29 #endif
30 
31 #include "bencode.h"
32 
37  FIND_NODE,
38  GET_PEERS,
39  GET,
40 };
41 
49 struct search_node {
50  unsigned char id[20];
51  struct sockaddr_storage addr;
52  socklen_t addrlen;
53  struct timeval reply_time;
54  struct timeval next_query;
55  int queried;
56  unsigned char *token;
57  size_t token_len;
58  struct search_node *next;
59  struct sockaddr_storage *peers;
60  struct bvalue *v;
61  int seq;
62  size_t peer_count;
63  int error;
64  unsigned char k[32];
65  unsigned char sig[64];
66 };
67 
68 struct dht_node;
69 
81 typedef void (*search_complete_t)(struct dht_node *n,
82  const struct search_node *nodes,
83  void *opaque);
84 
96 typedef void (*node_output_t)(const unsigned char *data, size_t len,
97  const struct sockaddr *dest, socklen_t addrlen,
98  void *opaque);
99 
111 typedef void (*bootstrap_status_t)(int ready, void *opaque);
112 
117  unsigned char ip[18];
118  size_t len;
119  unsigned int count;
121 };
122 
126 struct ip_counter {
127  unsigned int total;
128  struct timeval heat_start;
130 };
131 
132 struct bucket;
133 struct search;
134 struct peer_list;
135 struct put_item;
136 
140 struct dht_node {
141  unsigned char id[20];
143  void *opaque;
144  struct bucket *buckets;
145  struct {
146  struct search *first;
147  struct search **tail;
148  } searches;
149  uint16_t tid;
152  unsigned char secret[16];
153  struct peer_list *peer_storage;
154  struct put_item *put_storage;
155  struct search *bootstrap;
158 };
159 
165 typedef struct search *dht_search_t;
166 
191 int dht_node_init(struct dht_node *n, const unsigned char *id,
192  node_output_t output, void *opaque);
193 
203 int dht_node_start(struct dht_node *n);
204 
218 void dht_node_input(struct dht_node *n, const unsigned char *data, size_t len,
219  const struct sockaddr *src, socklen_t addrlen);
220 
233 void dht_node_ping(struct dht_node *n, struct sockaddr *dest,
234  socklen_t addrlen);
235 
245 void dht_node_timeout(struct dht_node *n, struct timeval *tv);
246 
257 void dht_node_work(struct dht_node *n);
258 
267 void dht_node_cleanup(struct dht_node *n);
268 
294 int dht_node_search(struct dht_node *n, const unsigned char id[20],
295  int search_type,
296  search_complete_t callback, void *opaque,
297  dht_search_t *handle);
298 
309 void dht_node_cancel(struct dht_node *n, dht_search_t handle);
310 
318 void dht_node_dump_buckets(struct dht_node *n);
319 
340 void dht_node_announce(struct dht_node *n, const unsigned char *info_hash,
341  const struct search_node *nodes,
342  int implied_port, int port);
343 
357 void dht_node_put_immutable(struct dht_node *n,
358  const struct search_node *nodes,
359  const struct bvalue *val);
360 
379 void dht_node_put_mutable(struct dht_node *n,
380  const struct search_node *nodes,
381  const unsigned char k[32],
382  const unsigned char signature[64],
383  const unsigned char *salt, size_t salt_len,
384  int seq, const struct bvalue *val);
385 
398 struct bvalue *dht_node_save(const struct dht_node *n);
399 
410 int dht_node_restore(const struct bvalue *dict, struct dht_node *n);
411 
425  bootstrap_status_t callback,
426  void *opaque);
427 
428 #ifdef __cplusplus
429 }
430 #endif
431 
432 #endif /* DHT_NODE_H_ */
External IP counter entry.
Definition: node.h:116
struct sockaddr_storage addr
Node address.
Definition: node.h:51
unsigned int count
Number of times seen.
Definition: node.h:119
struct peer_list * peer_storage
Peer list storage.
Definition: node.h:153
struct ip_counter_entry * entries
External IP address entries.
Definition: node.h:129
void(* node_output_t)(const unsigned char *data, size_t len, const struct sockaddr *dest, socklen_t addrlen, void *opaque)
Node output callback.
Definition: node.h:96
struct ip_counter_entry * next
Next entry.
Definition: node.h:120
DHT node object.
Definition: node.h:140
Search node.
Definition: node.h:49
unsigned int total
Total external IP address count.
Definition: node.h:127
struct bucket * buckets
Bucket list.
Definition: node.h:144
void * bootstrap_priv
Bootstrap callback user data.
Definition: node.h:157
void dht_node_dump_buckets(struct dht_node *n)
Dump the node&#39;s routing table.
Provides routines for parsing and formating bencoded data.
void dht_node_ping(struct dht_node *n, struct sockaddr *dest, socklen_t addrlen)
Ping remote node.
unsigned char sig[64]
signature of stored value
Definition: node.h:65
void(* search_complete_t)(struct dht_node *n, const struct search_node *nodes, void *opaque)
Search complete callback.
Definition: node.h:81
size_t peer_count
Number of entries in peers array.
Definition: node.h:62
size_t token_len
Length of token string.
Definition: node.h:57
int dht_node_restore(const struct bvalue *dict, struct dht_node *n)
Restore node state.
void(* bootstrap_status_t)(int ready, void *opaque)
Bootstrap status notification callback.
Definition: node.h:111
int seq
v&#39;s sequence number
Definition: node.h:61
bootstrap_status_t bootstrap_cb
Bootstrap status callback.
Definition: node.h:156
uint16_t tid
Transaction ID generation counter.
Definition: node.h:149
struct search_node * next
Next node in the list.
Definition: node.h:58
bencoding value.
Definition: bencode.h:32
int dht_node_start(struct dht_node *n)
Start DHT node.
void dht_node_announce(struct dht_node *n, const unsigned char *info_hash, const struct search_node *nodes, int implied_port, int port)
Send announce queries for an infohash.
struct search * dht_search_t
Search handle.
Definition: node.h:165
int error
Error code if node query failed.
Definition: node.h:63
void dht_node_put_mutable(struct dht_node *n, const struct search_node *nodes, const unsigned char k[32], const unsigned char signature[64], const unsigned char *salt, size_t salt_len, int seq, const struct bvalue *val)
Store mutable data in the DHT.
void dht_node_set_bootstrap_callback(struct dht_node *n, bootstrap_status_t callback, void *opaque)
Set bootstrap status notification callback.
struct timeval next_query
When to send next query.
Definition: node.h:54
int dht_node_search(struct dht_node *n, const unsigned char id[20], int search_type, search_complete_t callback, void *opaque, dht_search_t *handle)
Start a search on the DHT.
struct sockaddr_storage * peers
Array of peer addresses.
Definition: node.h:59
External IP counter.
Definition: node.h:126
node_output_t output
Datagram output function.
Definition: node.h:142
socklen_t addrlen
Length of addr field.
Definition: node.h:52
struct put_item * put_storage
Put data storage.
Definition: node.h:154
void dht_node_cancel(struct dht_node *n, dht_search_t handle)
Cancel a pending DHT search.
unsigned char k[32]
ed25519 public key of stored value
Definition: node.h:64
void dht_node_timeout(struct dht_node *n, struct timeval *tv)
Get node timeout.
struct bvalue * dht_node_save(const struct dht_node *n)
Save node state.
int dht_node_init(struct dht_node *n, const unsigned char *id, node_output_t output, void *opaque)
Initialize DHT node.
void dht_node_work(struct dht_node *n)
Service the node.
void dht_node_input(struct dht_node *n, const unsigned char *data, size_t len, const struct sockaddr *src, socklen_t addrlen)
Input a received UDP datagram.
size_t len
Length of ip field.
Definition: node.h:118
void dht_node_put_immutable(struct dht_node *n, const struct search_node *nodes, const struct bvalue *val)
Store immutable data in the DHT.
struct bvalue * v
value received in reply to get query
Definition: node.h:60
struct timeval reply_time
Query reply time.
Definition: node.h:53
struct search * bootstrap
Bootstrap search handle.
Definition: node.h:155
dht_search_type
Type of DHT search.
Definition: node.h:36
void * opaque
Output callback user data.
Definition: node.h:143
unsigned char * token
Storage token.
Definition: node.h:56
int queried
Number of queries sent with no reply.
Definition: node.h:55
void dht_node_cleanup(struct dht_node *n)
Cleanup DHT node.