]> git.meshlink.io Git - meshlink/blob - src/meshlink_internal.h
Never automatically try to bind to ports >= 32768.
[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 2
46 #define MESHLINK_INVITATION_VERSION 2
47
48 typedef struct listen_socket_t {
49         struct io_t tcp;
50         struct io_t udp;
51         sockaddr_t sa;
52         sockaddr_t broadcast_sa;
53 } listen_socket_t;
54
55 struct meshlink_open_params {
56         char *confbase;
57         char *lock_filename;
58         char *appname;
59         char *name;
60         dev_class_t devclass;
61
62         int netns;
63
64         const void *key;
65         size_t keylen;
66         meshlink_storage_policy_t storage_policy;
67 };
68
69 /// Device class traits
70 typedef struct {
71         int pinginterval;
72         int pingtimeout;
73         int fast_retry_period;
74         int maxtimeout;
75         unsigned int min_connects;
76         unsigned int max_connects;
77         int edge_weight;
78 } dev_class_traits_t;
79
80 /// A handle for an instance of MeshLink.
81 struct meshlink_handle {
82         // public members
83         char *name;
84         void *priv;
85
86         // private members
87         pthread_mutex_t mutex;
88         event_loop_t loop;
89         struct node_t *self;
90         meshlink_log_cb_t log_cb;
91         meshlink_log_level_t log_level;
92         void *packet;
93
94         // The most important network-related members come first
95         int reachable;
96         int listen_sockets;
97         listen_socket_t listen_socket[MAXSOCKETS];
98
99         meshlink_receive_cb_t receive_cb;
100         meshlink_queue_t outpacketqueue;
101         signal_t datafromapp;
102
103         hash_t *node_udp_cache;
104
105         struct splay_tree_t *nodes;
106         struct splay_tree_t *edges;
107
108         struct list_t *connections;
109         struct list_t *outgoings;
110         struct list_t *submeshes;
111
112         // Meta-connection-related members
113         struct splay_tree_t *past_request_tree;
114         timeout_t past_request_timeout;
115
116         int connection_burst;
117         int contradicting_add_edge;
118         int contradicting_del_edge;
119         int sleeptime;
120         time_t connection_burst_time;
121         time_t last_hard_try;
122         time_t last_unreachable;
123         timeout_t pingtimer;
124         timeout_t periodictimer;
125
126         struct connection_t *everyone;
127         uint64_t prng_state[4];
128         uint32_t session_id;
129
130         int next_pit;
131         int pits[10];
132
133         // Infrequently used callbacks
134         meshlink_node_status_cb_t node_status_cb;
135         meshlink_node_status_cb_t meta_status_cb;
136         meshlink_node_pmtu_cb_t node_pmtu_cb;
137         meshlink_channel_listen_cb_t channel_listen_cb;
138         meshlink_channel_accept_cb_t channel_accept_cb;
139         meshlink_node_duplicate_cb_t node_duplicate_cb;
140         meshlink_connection_try_cb_t connection_try_cb;
141         meshlink_error_cb_t error_cb;
142         meshlink_blacklisted_cb_t blacklisted_cb;
143         meshlink_thread_status_cb_t thread_status_cb;
144
145         // Mesh parameters
146         char *appname;
147         char *myport;
148
149         struct ecdsa *private_key;
150         struct ecdsa *invitation_key;
151
152         dev_class_t devclass;
153
154         int invitation_timeout;
155         int udp_choice;
156
157         dev_class_traits_t dev_class_traits[DEV_CLASS_COUNT];
158
159         int netns;
160
161         bool default_blacklist;
162         bool inviter_commits_first;
163
164         // Configuration
165         char *confbase;
166         FILE *lockfile;
167         void *config_key;
168         char *external_address_url;
169         struct list_t *invitation_addresses;
170         meshlink_storage_policy_t storage_policy;
171
172         // Thread management
173         pthread_t thread;
174         pthread_cond_t cond;
175         bool threadstarted;
176
177         // mDNS discovery
178         struct {
179                 bool enabled;
180                 io_t pfroute_io;
181                 int *ifaces;
182                 struct discovery_address *addresses;
183                 int iface_count;
184                 int address_count;
185                 io_t sockets[2];
186                 time_t last_update;
187 #ifdef __APPLE__
188                 pthread_t thread;
189                 void *runloop;
190 #endif
191         } discovery;
192
193         // ADNS
194         pthread_t adns_thread;
195         pthread_cond_t adns_cond;
196         meshlink_queue_t adns_queue;
197         meshlink_queue_t adns_done_queue;
198         signal_t adns_signal;
199 };
200
201 /// A handle for a MeshLink node.
202 struct meshlink_node {
203         const char *name;
204         void *priv;
205 };
206
207 /// A handle for a node Sub-Mesh.
208 struct meshlink_submesh {
209         const char *name;
210         void *priv;
211 };
212
213 /// An AIO buffer.
214 typedef struct meshlink_aio_buffer {
215         const void *data;
216         int fd;
217         size_t len;
218         size_t done;
219         union {
220                 meshlink_aio_cb_t buffer;
221                 meshlink_aio_fd_cb_t fd;
222         } cb;
223         void *priv;
224         struct meshlink_aio_buffer *next;
225 } meshlink_aio_buffer_t;
226
227 /// A channel.
228 struct meshlink_channel {
229         struct node_t *node;
230         void *priv;
231         bool in_callback;
232
233         struct utcp_connection *c;
234         meshlink_aio_buffer_t *aio_send;
235         meshlink_aio_buffer_t *aio_receive;
236         meshlink_channel_receive_cb_t receive_cb;
237         meshlink_channel_poll_cb_t poll_cb;
238 };
239
240 /// Header for data packets routed between nodes
241 typedef struct meshlink_packethdr {
242         uint8_t destination[16];
243         uint8_t source[16];
244 } __attribute__((__packed__)) meshlink_packethdr_t;
245
246 void meshlink_send_from_queue(event_loop_t *loop, void *mesh);
247 void update_node_status(meshlink_handle_t *mesh, struct node_t *n);
248 void update_node_pmtu(meshlink_handle_t *mesh, struct node_t *n);
249 extern meshlink_log_level_t global_log_level;
250 extern meshlink_log_cb_t global_log_cb;
251 void handle_duplicate_node(meshlink_handle_t *mesh, struct node_t *n);
252 void handle_network_change(meshlink_handle_t *mesh, bool online);
253 void call_error_cb(meshlink_handle_t *mesh, meshlink_errno_t meshlink_errno);
254 void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *node, const void *data, size_t len);
255
256 /// Per-instance PRNG
257 static inline int prng(meshlink_handle_t *mesh, uint64_t max) {
258         return xoshiro(mesh->prng_state) % max;
259 }
260
261 /// Fudge value of ~0.1 seconds, in microseconds.
262 static const unsigned int TIMER_FUDGE = 0x8000000;
263
264 #endif