]> git.meshlink.io Git - meshlink/blob - src/meshlink_internal.h
Replace rand() by xoshiro256** with per-mesh state.
[meshlink] / src / meshlink_internal.h
1 #ifndef MESHLINK_INTERNAL_H
2 #define MESHLINK_INTERNAL_H
3
4 /*
5     meshlink_internal.h -- Internal parts of the public API.
6     Copyright (C) 2014-2019 Guus Sliepen <guus@meshlink.io>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #ifdef MESHLINK_H
24 #error You must not include both meshlink.h and meshlink_internal.h!
25 #endif
26
27 #include "system.h"
28
29 #include "event.h"
30 #include "hash.h"
31 #include "meshlink.h"
32 #include "meshlink_queue.h"
33 #include "sockaddr.h"
34 #include "sptps.h"
35 #include "xoshiro.h"
36
37 #include <pthread.h>
38
39 #define MAXSOCKETS 4    /* Probably overkill... */
40
41 static const char meshlink_invitation_label[] = "MeshLink invitation";
42 static const char meshlink_tcp_label[] = "MeshLink TCP";
43 static const char meshlink_udp_label[] = "MeshLink UDP";
44
45 #define MESHLINK_CONFIG_VERSION 1
46 #define MESHLINK_INVITATION_VERSION 1
47
48 struct CattaServer;
49 struct CattaSServiceBrowser;
50 struct CattaSimplePoll;
51 struct CattaSEntryGroup;
52
53 typedef struct listen_socket_t {
54         struct io_t tcp;
55         struct io_t udp;
56         sockaddr_t sa;
57         bool bindto;
58 } listen_socket_t;
59
60 typedef enum proxytype_t {
61         PROXY_NONE = 0,
62         PROXY_SOCKS4,
63         PROXY_SOCKS4A,
64         PROXY_SOCKS5,
65         PROXY_HTTP,
66 } proxytype_t;
67
68 struct meshlink_open_params {
69         char *confbase;
70         char *appname;
71         char *name;
72         dev_class_t devclass;
73
74         int netns;
75
76         const void *key;
77         size_t keylen;
78 };
79
80 /// Device class traits
81 typedef struct {
82         int pinginterval;
83         int pingtimeout;
84         unsigned int min_connects;
85         unsigned int max_connects;
86         int edge_weight;
87 } dev_class_traits_t;
88
89 /// A handle for an instance of MeshLink.
90 struct meshlink_handle {
91         // public members
92         char *name;
93         void *priv;
94
95         // private members
96         pthread_mutex_t mesh_mutex;
97         event_loop_t loop;
98         struct node_t *self;
99         meshlink_log_cb_t log_cb;
100         meshlink_log_level_t log_level;
101
102         // The most important network-related members come first
103         int listen_sockets;
104         listen_socket_t listen_socket[MAXSOCKETS];
105
106         meshlink_receive_cb_t receive_cb;
107         meshlink_queue_t outpacketqueue;
108         signal_t datafromapp;
109
110         hash_t *node_udp_cache;
111
112         struct splay_tree_t *nodes;
113         struct splay_tree_t *edges;
114
115         struct list_t *connections;
116         struct list_t *outgoings;
117         struct list_t *submeshes;
118
119         // Meta-connection-related members
120         struct splay_tree_t *past_request_tree;
121         timeout_t past_request_timeout;
122
123         int connection_burst;
124         int contradicting_add_edge;
125         int contradicting_del_edge;
126         int sleeptime;
127         time_t connection_burst_time;
128         time_t last_config_check;
129         time_t last_hard_try;
130         timeout_t pingtimer;
131         timeout_t periodictimer;
132
133         struct connection_t *everyone;
134         uint64_t prng_state[4];
135
136         int next_pit;
137         int pits[10];
138
139         // Infrequently used callbacks
140         meshlink_node_status_cb_t node_status_cb;
141         meshlink_node_pmtu_cb_t node_pmtu_cb;
142         meshlink_channel_accept_cb_t channel_accept_cb;
143         meshlink_node_duplicate_cb_t node_duplicate_cb;
144         meshlink_connection_try_cb_t connection_try_cb;
145
146         // Mesh parameters
147         char *appname;
148         char *myport;
149
150         struct ecdsa *private_key;
151         struct ecdsa *invitation_key;
152
153         dev_class_t devclass;
154
155         int invitation_timeout;
156         int maxtimeout;
157         int udp_choice;
158
159         dev_class_traits_t dev_class_traits[DEV_CLASS_COUNT];
160
161         int netns;
162
163         bool default_blacklist;
164         bool discovery;         // Whether Catta is enabled or not
165
166
167         // Configuration
168         char *confbase;
169         FILE *conffile;
170         void *config_key;
171
172         // Thread management
173         pthread_t thread;
174         pthread_cond_t cond;
175         pthread_mutex_t discovery_mutex;
176         pthread_cond_t discovery_cond;
177         bool threadstarted;
178         bool discovery_threadstarted;
179
180         // Catta
181         pthread_t discovery_thread;
182         struct CattaServer *catta_server;
183         struct CattaSServiceBrowser *catta_browser;
184         struct CattaSimplePoll *catta_poll;
185         struct CattaSEntryGroup *catta_group;
186         char *catta_servicetype;
187         unsigned int catta_interfaces;
188
189         // State used for meshlink_join()
190         int sock;
191         char cookie[18], hash[18];
192         bool success;
193         sptps_t sptps;
194         char *data;
195         size_t thedatalen;
196         size_t blen;
197         char line[4096];
198         char buffer[4096];
199
200         // Proxy configuration, currently not exposed.
201         char *proxyhost;
202         char *proxyport;
203         char *proxyuser;
204         char *proxypass;
205         proxytype_t proxytype;
206 };
207
208 /// A handle for a MeshLink node.
209 struct meshlink_node {
210         const char *name;
211         void *priv;
212 };
213
214 /// A handle for a node Sub-Mesh.
215 struct meshlink_submesh {
216         const char *name;
217         void *priv;
218 };
219
220 /// An AIO buffer.
221 typedef struct meshlink_aio_buffer {
222         const void *data;
223         int fd;
224         size_t len;
225         size_t done;
226         union {
227                 meshlink_aio_cb_t buffer;
228                 meshlink_aio_fd_cb_t fd;
229         } cb;
230         void *priv;
231         struct meshlink_aio_buffer *next;
232 } meshlink_aio_buffer_t;
233
234 /// A channel.
235 struct meshlink_channel {
236         struct node_t *node;
237         void *priv;
238
239         struct utcp_connection *c;
240         meshlink_aio_buffer_t *aio_send;
241         meshlink_aio_buffer_t *aio_receive;
242         meshlink_channel_receive_cb_t receive_cb;
243         meshlink_channel_poll_cb_t poll_cb;
244 };
245
246 /// Header for data packets routed between nodes
247 typedef struct meshlink_packethdr {
248         uint8_t destination[16];
249         uint8_t source[16];
250 } __attribute__((__packed__)) meshlink_packethdr_t;
251
252 extern void meshlink_send_from_queue(event_loop_t *loop, void *mesh);
253 extern void update_node_status(meshlink_handle_t *mesh, struct node_t *n);
254 extern void update_node_pmtu(meshlink_handle_t *mesh, struct node_t *n);
255 extern meshlink_log_level_t global_log_level;
256 extern meshlink_log_cb_t global_log_cb;
257 extern void handle_duplicate_node(meshlink_handle_t *mesh, struct node_t *n);
258 extern void handle_network_change(meshlink_handle_t *mesh, bool online);
259
260 /// Per-instance PRNG
261 static inline int prng(meshlink_handle_t *mesh, uint64_t max) {
262         return xoshiro(mesh->prng_state) % max;
263 }
264
265 /// Fudge value of ~0.1 seconds, in microseconds.
266 static const unsigned int TIMER_FUDGE = 0x20000;
267
268 #endif