]> git.meshlink.io Git - meshlink/blob - src/meshlink.h
Ensure we can pass arbitrary binary blobs for keys.
[meshlink] / src / meshlink.h
1 #ifndef MESHLINK_H
2 #define MESHLINK_H
3
4 /*
5     meshlink.h -- MeshLink 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 #include <stdint.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <unistd.h>
27
28 #if defined(_WIN32)
29 #include <winsock2.h>
30 #else
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #endif
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /// The length in bytes of a signature made with meshlink_sign()
40 #define MESHLINK_SIGLEN (64ul)
41
42 // The maximum length of fingerprints
43 #define MESHLINK_FINGERPRINTLEN (64ul)
44
45 /// A handle for an instance of MeshLink.
46 typedef struct meshlink_handle meshlink_handle_t;
47
48 /// A handle for a MeshLink node.
49 typedef struct meshlink_node meshlink_node_t;
50
51 /// A handle for a MeshLink channel.
52 typedef struct meshlink_channel meshlink_channel_t;
53
54 /// A struct containing all parameters used for opening a mesh.
55 typedef struct meshlink_open_params meshlink_open_params_t;
56
57 /// A handle for a MeshLink sub-mesh.
58 typedef struct meshlink_submesh meshlink_submesh_t;
59
60 /// Code of most recent error encountered.
61 typedef enum {
62         MESHLINK_OK,        ///< Everything is fine
63         MESHLINK_EINVAL,    ///< Invalid parameter(s) to function call
64         MESHLINK_ENOMEM,    ///< Out of memory
65         MESHLINK_ENOENT,    ///< Node is not known
66         MESHLINK_EEXIST,    ///< Node already exists
67         MESHLINK_EINTERNAL, ///< MeshLink internal error
68         MESHLINK_ERESOLV,   ///< MeshLink could not resolve a hostname
69         MESHLINK_ESTORAGE,  ///< MeshLink could not load or write data from/to disk
70         MESHLINK_ENETWORK,  ///< MeshLink encountered a network error
71         MESHLINK_EPEER,     ///< A peer caused an error
72         MESHLINK_ENOTSUP,   ///< The operation is not supported in the current configuration of MeshLink
73         MESHLINK_EBUSY      ///< The MeshLink instance is already in use by another process
74 } meshlink_errno_t;
75
76 /// Device class
77 typedef enum {
78         DEV_CLASS_BACKBONE = 0,
79         DEV_CLASS_STATIONARY = 1,
80         DEV_CLASS_PORTABLE = 2,
81         DEV_CLASS_UNKNOWN = 3,
82         _DEV_CLASS_MAX = 3
83 } dev_class_t;
84
85 /// Invitation flags
86 static const uint32_t MESHLINK_INVITE_LOCAL = 1;    // Only use local addresses in the URL
87 static const uint32_t MESHLINK_INVITE_PUBLIC = 2;   // Only use public or canonical addresses in the URL
88 static const uint32_t MESHLINK_INVITE_IPV4 = 4;     // Only use IPv4 addresses in the URL
89 static const uint32_t MESHLINK_INVITE_IPV6 = 8;     // Only use IPv6 addresses in the URL
90 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
91
92 /// Channel flags
93 static const uint32_t MESHLINK_CHANNEL_RELIABLE = 1;   // Data is retransmitted when packets are lost.
94 static const uint32_t MESHLINK_CHANNEL_ORDERED = 2;    // Data is delivered in-order to the application.
95 static const uint32_t MESHLINK_CHANNEL_FRAMED = 4;     // Data is delivered in chunks of the same length as data was originally sent.
96 static const uint32_t MESHLINK_CHANNEL_DROP_LATE = 8;  // When packets are reordered, late packets are ignored.
97 static const uint32_t MESHLINK_CHANNEL_TCP = 3;        // Select TCP semantics.
98 static const uint32_t MESHLINK_CHANNEL_UDP = 0;        // Select UDP semantics.
99
100 /// A variable holding the last encountered error from MeshLink.
101 /** This is a thread local variable that contains the error code of the most recent error
102  *  encountered by a MeshLink API function called in the current thread.
103  *  The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
104  *  if a function returned successfully.
105  */
106 extern __thread meshlink_errno_t meshlink_errno;
107
108 #ifndef MESHLINK_INTERNAL_H
109
110 struct meshlink_handle {
111         const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
112         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
113 };
114
115 struct meshlink_node {
116         const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
117         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
118 };
119
120 struct meshlink_submesh {
121         const char *const name; ///< Textual name of this Sub-Mesh. It is stored in a nul-terminated C string, which is allocated by MeshLink.
122         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
123 };
124
125 struct meshlink_channel {
126         struct meshlink_node *const node; ///< Pointer to the peer of this channel.
127         void *priv;                       ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
128 };
129
130 #endif // MESHLINK_INTERNAL_H
131
132 /// Get the text for the given MeshLink error code.
133 /** This function returns a pointer to the string containing the description of the given error code.
134  *
135  *  @param err      An error code returned by MeshLink.
136  *
137  *  @return         A pointer to a string containing the description of the error code.
138  *                  The pointer is to static storage that is valid for the lifetime of the application.
139  *                  This function will always return a valid pointer, even if an invalid error code has been passed.
140  */
141 extern const char *meshlink_strerror(meshlink_errno_t err);
142
143 /// Create a new meshlink_open_params_t struct.
144 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
145  *  The resulting struct may be reused for multiple calls to meshlink_open_ex().
146  *  After the last use, the application must free this struct using meshlink_open_params_free().
147  *
148  *  @param confbase The directory in which MeshLink will store its configuration files.
149  *                  After the function returns, the application is free to overwrite or free @a confbase.
150  *  @param name     The name which this instance of the application will use in the mesh.
151  *                  After the function returns, the application is free to overwrite or free @a name.
152  *  @param appname  The application name which will be used in the mesh.
153  *                  After the function returns, the application is free to overwrite or free @a name.
154  *  @param devclass The device class which will be used in the mesh.
155  *
156  *  @return         A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
157  *                  The pointer is valid until meshlink_open_params_free() is called.
158  */
159 extern meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
160
161 /// Free a meshlink_open_params_t struct.
162 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
163  *
164  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
165  */
166 extern void meshlink_open_params_free(meshlink_open_params_t *params);
167
168 /// Set the network namespace MeshLink should use.
169 /** This function changes the open parameters to use the given netns filedescriptor.
170  *
171  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
172  *  @param netns    A filedescriptor that must point to a valid network namespace, or -1 to have MeshLink use the same namespace as the calling thread.
173  *
174  *  @return         This function will return true if the open parameters have been succesfully updated, false otherwise.
175  */
176 extern bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns);
177
178 /// Set the encryption key MeshLink should use for local storage.
179 /** This function changes the open parameters to use the given key for encrypting MeshLink's own configuration files.
180  *
181  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
182  *  @param key      A pointer to a key, or NULL in case no encryption should be used.
183  *  @param keylen   The length of the given key, or 0 in case no encryption should be used.
184  *
185  *  @return         This function will return true if the open parameters have been succesfully updated, false otherwise.
186  */
187 extern bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen);
188
189 /// Open or create a MeshLink instance.
190 /** This function opens or creates a MeshLink instance.
191  *  All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
192  *  which must have been initialized earlier by the application.
193  *
194  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
195  *  When the application does no longer need to use this handle, it must call meshlink_close() to
196  *  free its resources.
197  *
198  *  This function does not start any network I/O yet. The application should
199  *  first set callbacks, and then call meshlink_start().
200  *
201  *  @param params   A pointer to a meshlink_open_params_t which must be filled in by the application.
202  *                  After the function returns, the application is free to reuse or free @a params.
203  *
204  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
205  *                  The pointer is valid until meshlink_close() is called.
206  */
207 extern meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params);
208
209 /// Open or create a MeshLink instance.
210 /** This function opens or creates a MeshLink instance.
211  *  The state is stored in the configuration directory passed in the variable @a confbase.
212  *  If the configuration directory does not exist yet, for example when it is the first time
213  *  this instance is opened, the configuration directory will be automatically created and initialized.
214  *  However, the parent directory should already exist, otherwise an error will be returned.
215  *
216  *  The name given should be a unique identifier for this instance.
217  *
218  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
219  *  When the application does no longer need to use this handle, it must call meshlink_close() to
220  *  free its resources.
221  *
222  *  This function does not start any network I/O yet. The application should
223  *  first set callbacks, and then call meshlink_start().
224  *
225  *  @param confbase The directory in which MeshLink will store its configuration files.
226  *                  After the function returns, the application is free to overwrite or free @a confbase.
227  *  @param name     The name which this instance of the application will use in the mesh.
228  *                  After the function returns, the application is free to overwrite or free @a name.
229  *  @param appname  The application name which will be used in the mesh.
230  *                  After the function returns, the application is free to overwrite or free @a name.
231  *  @param devclass The device class which will be used in the mesh.
232  *
233  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
234  *                  The pointer is valid until meshlink_close() is called.
235  */
236 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
237
238 /// Open or create a MeshLink instance that uses encrypted storage.
239 /** This function opens or creates a MeshLink instance.
240  *  The state is stored in the configuration directory passed in the variable @a confbase @a.
241  *  If the configuration directory does not exist yet, for example when it is the first time
242  *  this instance is opened, the configuration directory will be automatically created and initialized.
243  *  However, the parent directory should already exist, otherwise an error will be returned.
244  *
245  *  The name given should be a unique identifier for this instance.
246  *
247  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
248  *  When the application does no longer need to use this handle, it must call meshlink_close() to
249  *  free its resources.
250  *
251  *  This function does not start any network I/O yet. The application should
252  *  first set callbacks, and then call meshlink_start().
253  *
254  *  @param confbase The directory in which MeshLink will store its configuration files.
255  *                  After the function returns, the application is free to overwrite or free @a confbase @a.
256  *  @param name     The name which this instance of the application will use in the mesh.
257  *                  After the function returns, the application is free to overwrite or free @a name @a.
258  *  @param appname  The application name which will be used in the mesh.
259  *                  After the function returns, the application is free to overwrite or free @a name @a.
260  *  @param devclass The device class which will be used in the mesh.
261  *  @param key      A pointer to a key used to encrypt storage.
262  *  @param keylen   The length of the key in bytes.
263  *
264  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
265  *                  The pointer is valid until meshlink_close() is called.
266  */
267 extern meshlink_handle_t *meshlink_open_encrypted(const char *confbase, const char *name, const char *appname, dev_class_t devclass, const void *key, size_t keylen);
268
269 /// Create an ephemeral MeshLink instance that does not store any state.
270 /** This function creates a MeshLink instance.
271  *  No state is ever saved, so once this instance is closed, all its state is gone.
272  *
273  *  The name given should be a unique identifier for this instance.
274  *
275  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
276  *  When the application does no longer need to use this handle, it must call meshlink_close() to
277  *  free its resources.
278  *
279  *  This function does not start any network I/O yet. The application should
280  *  first set callbacks, and then call meshlink_start().
281  *
282  *  @param name     The name which this instance of the application will use in the mesh.
283  *                  After the function returns, the application is free to overwrite or free @a name @a.
284  *  @param appname  The application name which will be used in the mesh.
285  *                  After the function returns, the application is free to overwrite or free @a name @a.
286  *  @param devclass The device class which will be used in the mesh.
287  *
288  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
289  *                  The pointer is valid until meshlink_close() is called.
290  */
291 extern meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass);
292
293 /// Create Sub-Mesh.
294 /** This function causes MeshLink to open a new Sub-Mesh network
295  *  create a new thread, which will handle all network I/O.
296  *
297  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
298  *
299  *  @param mesh     A handle which represents an instance of MeshLink.
300   *
301  *  @param submesh  Name of the new Sub-Mesh to create.
302  *
303  *  @return         A pointer to a meshlink_submesh_t which represents this instance of SubMesh, or NULL in case of an error.
304  *                  The pointer is valid until meshlink_close() is called.
305  */
306 meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t  *mesh, const char *submesh);
307
308 /// Start MeshLink.
309 /** This function causes MeshLink to open network sockets, make outgoing connections, and
310  *  create a new thread, which will handle all network I/O.
311  *
312  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
313  *
314  *  @param mesh     A handle which represents an instance of MeshLink.
315  *
316  *  @return         This function will return true if MeshLink has successfully started, false otherwise.
317  */
318 extern bool meshlink_start(meshlink_handle_t *mesh);
319
320 /// Stop MeshLink.
321 /** This function causes MeshLink to disconnect from all other nodes,
322  *  close all sockets, and shut down its own thread.
323  *
324  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
325  *  Channels that are still open will remain valid, but any communication via channels will stop as well.
326  *
327  *  @param mesh     A handle which represents an instance of MeshLink.
328  */
329 extern void meshlink_stop(meshlink_handle_t *mesh);
330
331 /// Close the MeshLink handle.
332 /** This function calls meshlink_stop() if necessary,
333  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
334  *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
335  *
336  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
337  *  If called at a proper time with a valid handle, this function always succeeds.
338  *  If called within a callback or with an invalid handle, the result is undefined.
339  *
340  *  @param mesh     A handle which represents an instance of MeshLink.
341  */
342 extern void meshlink_close(meshlink_handle_t *mesh);
343
344 /// Destroy a MeshLink instance.
345 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
346  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
347  *  new instance.
348  *
349  *  @param confbase The directory in which MeshLink stores its configuration files.
350  *                  After the function returns, the application is free to overwrite or free @a confbase.
351  *
352  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
353  */
354 extern bool meshlink_destroy(const char *confbase);
355
356 /// A callback for receiving data from the mesh.
357 /** @param mesh      A handle which represents an instance of MeshLink.
358  *  @param source    A pointer to a meshlink_node_t describing the source of the data.
359  *  @param data      A pointer to a buffer containing the data sent by the source, or NULL in case there is no data (an empty packet was received).
360  *                   The pointer is only valid during the lifetime of the callback.
361  *                   The callback should mempcy() the data if it needs to be available outside the callback.
362  *  @param len       The length of the received data, or 0 in case there is no data.
363  */
364 typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len);
365
366 /// Set the receive callback.
367 /** This functions sets the callback that is called whenever another node sends data to the local node.
368  *  The callback is run in MeshLink's own thread.
369  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
370  *  to hand the data over to the application's thread.
371  *  The callback should also not block itself and return as quickly as possible.
372  *
373  *  @param mesh      A handle which represents an instance of MeshLink.
374  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
375  *                   If a NULL pointer is given, the callback will be disabled.
376  */
377 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
378
379 /// A callback reporting the meta-connection attempt made by the host node to an another node.
380 /** @param mesh      A handle which represents an instance of MeshLink.
381  *  @param node      A pointer to a meshlink_node_t describing the node to whom meta-connection is being tried.
382  *                   This pointer is valid until meshlink_close() is called.
383  */
384 typedef void (*meshlink_connection_try_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
385
386 /// Set the meta-connection try callback.
387 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
388  *  The callback is run in MeshLink's own thread.
389  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
390  *  to hand the data over to the application's thread.
391  *  The callback should also not block itself and return as quickly as possible.
392  *
393  *  @param mesh      A handle which represents an instance of MeshLink.
394  *  @param cb        A pointer to the function which will be called when host node attempts to make
395  *                   the connection to another node. If a NULL pointer is given, the callback will be disabled.
396  */
397 extern void meshlink_set_connection_try_cb(meshlink_handle_t *mesh, meshlink_connection_try_cb_t cb);
398
399 /// A callback reporting node status changes.
400 /** @param mesh       A handle which represents an instance of MeshLink.
401  *  @param node       A pointer to a meshlink_node_t describing the node whose status changed.
402  *                    This pointer is valid until meshlink_close() is called.
403  *  @param reachable  True if the node is reachable, false otherwise.
404  */
405 typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
406
407 /// Set the node status callback.
408 /** This functions sets the callback that is called whenever another node's status changed.
409  *  The callback is run in MeshLink's own thread.
410  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
411  *  to hand the data over to the application's thread.
412  *  The callback should also not block itself and return as quickly as possible.
413  *
414  *  @param mesh      A handle which represents an instance of MeshLink.
415  *  @param cb        A pointer to the function which will be called when another node's status changes.
416  *                   If a NULL pointer is given, the callback will be disabled.
417  */
418 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
419
420 /// A callback reporting duplicate node detection.
421 /** @param mesh       A handle which represents an instance of MeshLink.
422  *  @param node       A pointer to a meshlink_node_t describing the node which is duplicate.
423  *                    This pointer is valid until meshlink_close() is called.
424  */
425 typedef void (*meshlink_node_duplicate_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
426
427 /// Set the node duplicate callback.
428 /** This functions sets the callback that is called whenever a duplicate node is detected.
429  *  The callback is run in MeshLink's own thread.
430  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
431  *  to hand the data over to the application's thread.
432  *  The callback should also not block itself and return as quickly as possible.
433  *
434  *  @param mesh      A handle which represents an instance of MeshLink.
435  *  @param cb        A pointer to the function which will be called when a duplicate node is detected.
436  *                   If a NULL pointer is given, the callback will be disabled.
437  */
438 extern void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb);
439
440 /// Severity of log messages generated by MeshLink.
441 typedef enum {
442         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
443         MESHLINK_INFO,     ///< Informational messages.
444         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
445         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
446         MESHLINK_CRITICAL  ///< Critical errors which cause MeshLink to fail completely.
447 } meshlink_log_level_t;
448
449 /// A callback for receiving log messages generated by MeshLink.
450 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
451  *  @param level     An enum describing the severity level of the message.
452  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
453  *                   This pointer is only valid for the duration of the callback.
454  *                   The application must not free() this pointer.
455  *                   The application should strdup() the text if it has to be available outside the callback.
456  */
457 typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
458
459 /// Set the log callback.
460 /** This functions sets the callback that is called whenever MeshLink has some information to log.
461  *
462  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
463  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
464  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
465  *
466  *  If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
467  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
468  *  to hand the data over to the application's thread.
469  *  The callback should also not block itself and return as quickly as possible.
470  *
471  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
472  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
473  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
474  *
475  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
476  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
477  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
478  *                   If a NULL pointer is given, the callback will be disabled.
479  */
480 extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
481
482 /// Send data to another node.
483 /** This functions sends one packet of data to another node in the mesh.
484  *  The packet is sent using UDP semantics, which means that
485  *  the packet is sent as one unit and is received as one unit,
486  *  and that there is no guarantee that the packet will arrive at the destination.
487  *  Packets that are too big to be sent over the network as one unit might be dropped, and this function may return an error if this situation can be detected beforehand.
488  *  The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
489  *  The application should take care of getting an acknowledgement and retransmission if necessary.
490  *
491  *  @param mesh         A handle which represents an instance of MeshLink.
492  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
493  *  @param data         A pointer to a buffer containing the data to be sent to the source.
494  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
495  *                      It is valid to specify a NULL pointer, but only if @a len is also 0.
496  *  @param len          The length of the data.
497  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
498  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
499  */
500 extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len);
501
502 /// Query the maximum packet size that can be sent to a node.
503 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
504  *  The path MTU is a property of the path packets will take to the destination node over the Internet.
505  *  It can be different for different destination nodes.
506  *  and the path MTU can change at any point in time due to changes in the Internet.
507  *  Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
508  *
509  *  @param mesh         A handle which represents an instance of MeshLink.
510  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
511  *
512  *  @return             The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
513  *                      or a negative value in case of an error.
514  */
515 extern ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination);
516
517 /// Get a handle for our own node.
518 /** This function returns a handle for the local node.
519  *
520  *  @param mesh         A handle which represents an instance of MeshLink.
521  *
522  *  @return             A pointer to a meshlink_node_t which represents the local node.
523  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
524  */
525 extern meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh);
526
527 /// Get a handle for a specific node.
528 /** This function returns a handle for the node with the given name.
529  *
530  *  @param mesh         A handle which represents an instance of MeshLink.
531  *  @param name         The name of the node for which a handle is requested.
532  *                      After this function returns, the application is free to overwrite or free @a name.
533  *
534  *  @return             A pointer to a meshlink_node_t which represents the requested node,
535  *                      or NULL if the requested node does not exist.
536  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
537  */
538 extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
539
540 /// Get a handle for a specific submesh.
541 /** This function returns a handle for the submesh with the given name.
542  *
543  *  @param mesh         A handle which represents an instance of MeshLink.
544  *  @param name         The name of the submesh for which a handle is requested.
545  *                      After this function returns, the application is free to overwrite or free @a name @a.
546  *
547  *  @return             A pointer to a meshlink_submesh_t which represents the requested submesh,
548  *                      or NULL if the requested submesh does not exist.
549  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
550  */
551 extern meshlink_submesh_t *meshlink_get_submesh(meshlink_handle_t *mesh, const char *name);
552
553 /// Get the fingerprint of a node's public key.
554 /** This function returns a fingerprint of the node's public key.
555  *  It should be treated as an opaque blob.
556  *
557  *  @param mesh         A handle which represents an instance of MeshLink.
558  *  @param node         A pointer to a meshlink_node_t describing the node.
559  *
560  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
561  *                      The application should call free() after it is done using this string.
562  */
563 extern char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node);
564
565 /// Get a list of all nodes.
566 /** This function returns a list with handles for all known nodes.
567  *
568  *  @param mesh         A handle which represents an instance of MeshLink.
569  *  @param nodes        A pointer to a previously allocated array of pointers to meshlink_node_t, or NULL in which case MeshLink will allocate a new array.
570  *                      The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
571  *                      The application is allowed to call free() on the array whenever it wishes.
572  *                      The pointers in the array are valid until meshlink_close() is called.
573  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
574  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
575  *                      The contents of this variable will be changed to reflect the new size of the array.
576  *
577  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
578  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
579  *                      If it is a new value, the old value of @a nodes should not be used anymore.
580  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
581  */
582 extern meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb);
583
584 /// Sign data using the local node's MeshLink key.
585 /** This function signs data using the local node's MeshLink key.
586  *  The generated signature can be securely verified by other nodes.
587  *
588  *  @param mesh         A handle which represents an instance of MeshLink.
589  *  @param data         A pointer to a buffer containing the data to be signed.
590  *  @param len          The length of the data to be signed.
591  *  @param signature    A pointer to a buffer where the signature will be stored.
592  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
593  *                      The signature is a binary blob, and is not nul-terminated.
594  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
595  *
596  *  @return             This function returns true if the signature was correctly generated, false otherwise.
597  */
598 extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen);
599
600 /// Get the list of all nodes by device class.
601 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
602  *
603  *  @param mesh         A handle which represents an instance of MeshLink.
604  *  @param devclass     Device class of the nodes for which the list has to be obtained.
605  *  @param nodes        A pointer to a previously allocated array of pointers to meshlink_node_t, or NULL in which case MeshLink will allocate a new array.
606  *                      The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
607  *                      The application is allowed to call free() on the array whenever it wishes.
608  *                      The pointers in the array are valid until meshlink_close() is called.
609  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
610  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
611  *                      The contents of this variable will be changed to reflect the new size of the array.
612  *
613  *  @return             A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
614  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
615  *                      If it is a new value, the old value of @a nodes should not be used anymore.
616  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
617  */
618 extern meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, dev_class_t devclass, meshlink_node_t **nodes, size_t *nmemb);
619
620 /// Get the list of all nodes by Submesh.
621 /** This function returns a list with handles for all the nodes that matches with the given @a Submesh.
622  *
623  *  @param mesh         A handle which represents an instance of MeshLink.
624  *  @param submesh      Submesh handle of the nodes for which the list has to be obtained.
625  *  @param nodes        A pointer to a previously allocated array of pointers to meshlink_node_t, or NULL in which case MeshLink will allocate a new array.
626  *                      The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
627  *                      The application is allowed to call free() on the array whenever it wishes.
628  *                      The pointers in the array are valid until meshlink_close() is called.
629  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
630  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
631  *                      The contents of this variable will be changed to reflect the new size of the array.
632  *
633  *  @return             A pointer to an array containing pointers to all known nodes of the given Submesh, or NULL in case of an error.
634  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
635  *                      If it is a new value, the old value of @a nodes should not be used anymore.
636  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
637  */
638 extern meshlink_node_t **meshlink_get_all_nodes_by_submesh(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, meshlink_node_t **nodes, size_t *nmemb);
639
640 /// Get the node's device class.
641 /** This function returns the device class of the given node.
642  *
643  *  @param mesh          A handle which represents an instance of MeshLink.
644  *  @param node         A pointer to a meshlink_node_t describing the node.
645  *
646  *  @return              This function returns the device class of the @a node, or -1 in case of an error.
647  */
648 extern dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node);
649
650 /// Get the node's submesh handle.
651 /** This function returns the submesh handle of the given node.
652  *
653  *  @param mesh          A handle which represents an instance of MeshLink.
654  *  @param node          A pointer to a meshlink_node_t describing the node.
655  *
656  *  @return              This function returns the submesh handle of the @a node, or NULL in case of an error.
657  */
658 extern meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_node_t *node);
659
660 /// Verify the signature generated by another node of a piece of data.
661 /** This function verifies the signature that another node generated for a piece of data.
662  *
663  *  @param mesh         A handle which represents an instance of MeshLink.
664  *  @param source       A pointer to a meshlink_node_t describing the source of the signature.
665  *  @param data         A pointer to a buffer containing the data to be verified.
666  *  @param len          The length of the data to be verified.
667  *  @param signature    A pointer to a buffer where the signature is stored.
668  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
669  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
670  *
671  *  @return             This function returns true if the signature is valid, false otherwise.
672  */
673 extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen);
674
675 /// Set the canonical Address for a node.
676 /** This function sets the canonical Address for a node.
677  *  This address is stored permanently until it is changed by another call to this function,
678  *  unlike other addresses associated with a node,
679  *  such as those added with meshlink_hint_address() or addresses discovered at runtime.
680  *
681  *  If a canonical Address is set for the local node,
682  *  it will be used for the hostname part of generated invitation URLs.
683  *
684  *  @param mesh         A handle which represents an instance of MeshLink.
685  *  @param node         A pointer to a meshlink_node_t describing the node.
686  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
687  *  @param port         A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
688  *                      If it is NULL, the listening port's number will be used.
689  *
690  *  @return             This function returns true if the address was added, false otherwise.
691  */
692 extern bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node, const char *address, const char *port);
693
694 /// Add an Address for the local node.
695 /** This function adds an Address for the local node, which will be used for invitation URLs.
696  *
697  *  @param mesh         A handle which represents an instance of MeshLink.
698  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
699  *
700  *  @return             This function returns true if the address was added, false otherwise.
701  */
702 extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
703
704 /// Try to discover the external address for the local node.
705 /** This function performs tries to discover the local node's external address
706  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
707  *  the FQDN associated with the address will be returned.
708  *
709  *  Please note that this is function only returns a single address,
710  *  even if the local node might have more than one external address.
711  *  In that case, there is no control over which address will be selected.
712  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
713  *  there is no guarantee that the external address will be valid for an extended period of time.
714  *
715  *  This function is blocking. It can take several seconds before it returns.
716  *  There is no guarantee it will be able to resolve the external address.
717  *  Failures might be because by temporary network outages.
718  *
719  *  @param mesh         A handle which represents an instance of MeshLink.
720  *
721  *  @return             This function returns a pointer to a C string containing the discovered external address,
722  *                      or NULL if there was an error looking up the address.
723  *                      After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
724  */
725 extern char *meshlink_get_external_address(meshlink_handle_t *mesh);
726
727 /// Try to discover the external address for the local node.
728 /** This function performs tries to discover the local node's external address
729  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
730  *  the FQDN associated with the address will be returned.
731  *
732  *  Please note that this is function only returns a single address,
733  *  even if the local node might have more than one external address.
734  *  In that case, there is no control over which address will be selected.
735  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
736  *  there is no guarantee that the external address will be valid for an extended period of time.
737  *
738  *  This function is blocking. It can take several seconds before it returns.
739  *  There is no guarantee it will be able to resolve the external address.
740  *  Failures might be because by temporary network outages.
741  *
742  *  @param mesh            A handle which represents an instance of MeshLink.
743  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
744  *                         this might return the external address for any working address family.
745  *
746  *  @return                This function returns a pointer to a C string containing the discovered external address,
747  *                         or NULL if there was an error looking up the address.
748  *                         After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
749  */
750 extern char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int address_family);
751
752 /// Try to discover the local address for the local node.
753 /** This function performs tries to discover the address of the local interface used for outgoing connection.
754  *
755  *  Please note that this is function only returns a single address,
756  *  even if the interface might have more than one address.
757  *  In that case, there is no control over which address will be selected.
758  *  Also note that if you have a dynamic IP address,
759  *  there is no guarantee that the local address will be valid for an extended period of time.
760  *
761  *  This function will fail if it couldn't find a local address for the given address family.
762  *  If hostname resolving is requested, this function may block for a few seconds.
763  *
764  *  @param mesh            A handle which represents an instance of MeshLink.
765  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
766  *                         this might return the local address for any working address family.
767  *
768  *  @return                This function returns a pointer to a C string containing the discovered local address,
769  *                         or NULL if there was an error looking up the address.
770  *                         After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
771  */
772 extern char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int address_family);
773
774 /// Try to discover the external address for the local node, and add it to its list of addresses.
775 /** This function is equivalent to:
776  *
777  *    meshlink_add_address(mesh, meshlink_get_external_address(mesh));
778  *
779  *  Read the description of meshlink_get_external_address() for the limitations of this function.
780  *
781  *  @param mesh         A handle which represents an instance of MeshLink.
782  *
783  *  @return             This function returns true if the address was added, false otherwise.
784  */
785 extern bool meshlink_add_external_address(meshlink_handle_t *mesh);
786
787 /// Get the network port used by the local node.
788 /** This function returns the network port that the local node is listening on.
789  *
790  *  @param mesh          A handle which represents an instance of MeshLink.
791  *
792  *  @return              This function returns the port number, or -1 in case of an error.
793  */
794 extern int meshlink_get_port(meshlink_handle_t *mesh);
795
796 /// Set the network port used by the local node.
797 /** This function sets the network port that the local node is listening on.
798  *  It may only be called when the mesh is not running.
799  *  If unsure, call meshlink_stop() before calling this function.
800  *  Also note that if your node is already part of a mesh with other nodes,
801  *  that the other nodes may no longer be able to initiate connections to the local node,
802  *  since they will try to connect to the previously configured port.
803  *
804  *  @param mesh          A handle which represents an instance of MeshLink.
805  *  @param port          The port number to listen on. This must be between 0 and 65535.
806  *                       If the port is set to 0, then MeshLink will listen on a port
807  *                       that is randomly assigned by the operating system every time meshlink_open() is called.
808  *
809  *  @return              This function returns true if the port was successfully changed
810  *                       to the desired port, false otherwise. If it returns false, there
811  *                       is no guarantee that MeshLink is listening on the old port.
812  */
813
814 extern bool meshlink_set_port(meshlink_handle_t *mesh, int port);
815
816 /// Set the timeout for invitations.
817 /** This function sets the timeout for invitations.
818  *  Note that timeouts are only checked at the time a node tries to join using an invitation.
819  *  The default timeout for invitations is 1 week.
820  *
821  *  @param mesh         A handle which represents an instance of MeshLink.
822  *  @param timeout      The timeout for invitations in seconds.
823  */
824 extern void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout);
825
826 /// Invite another node into the mesh.
827 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
828  *  The generated invitation is a string containing a URL.
829  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
830  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
831  *
832  *  @param mesh         A handle which represents an instance of MeshLink.
833  *  @param submesh      A handle which represents an instance of SubMesh.
834  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
835  *                      After this function returns, the application is free to overwrite or free @a name.
836  *  @param flags        A bitwise-or'd combination of flags that controls how the URL is generated.
837  *
838  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
839  *                      The application should call free() after it has finished using the URL.
840  */
841 extern char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags);
842
843 /// Invite another node into the mesh.
844 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
845  *  The generated invitation is a string containing a URL.
846  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
847  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
848  *
849  *  Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
850  *
851  *  @param mesh         A handle which represents an instance of MeshLink.
852  *  @param submesh      A handle which represents an instance of SubMesh.
853  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
854  *                      After this function returns, the application is free to overwrite or free @a name.
855  *
856  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
857  *                      The application should call free() after it has finished using the URL.
858  */
859 extern char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name);
860
861 /// Use an invitation to join a mesh.
862 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
863  *  An invitation can only be used if the local node has never connected to other nodes before.
864  *  After a successfully accepted invitation, the name of the local node may have changed.
865  *
866  *  This function may only be called on a mesh that has not been started yet and which is not already part of an existing mesh.
867  *
868  *  This function is blocking. It can take several seconds before it returns.
869  *  There is no guarantee it will perform a successful join.
870  *  Failures might be caused by temporary network outages, or by the invitation having expired.
871  *
872  *  @param mesh         A handle which represents an instance of MeshLink.
873  *  @param invitation   A nul-terminated C string containing the invitation URL.
874  *                      After this function returns, the application is free to overwrite or free @a invitation.
875  *
876  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
877  */
878 extern bool meshlink_join(meshlink_handle_t *mesh, const char *invitation);
879
880 /// Export the local node's key and addresses.
881 /** This function generates a string that contains the local node's public key and one or more IP addresses.
882  *  The application can pass it in some way to another node, which can then import it,
883  *  granting the local node access to the other node's mesh.
884  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
885  *
886  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
887  *
888  *  @param mesh         A handle which represents an instance of MeshLink.
889  *
890  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
891  *                      The application should call free() after it has finished using this string.
892  */
893 extern char *meshlink_export(meshlink_handle_t *mesh);
894
895 /// Import another node's key and addresses.
896 /** This function accepts a string containing the exported public key and addresses of another node.
897  *  By importing this data, the local node grants the other node access to its mesh.
898  *  The application should make sure that the data it imports is really coming from the node it wants to import,
899  *
900  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
901  *
902  *  @param mesh         A handle which represents an instance of MeshLink.
903  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
904  *                      After this function returns, the application is free to overwrite or free @a data.
905  *
906  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
907  */
908 extern bool meshlink_import(meshlink_handle_t *mesh, const char *data);
909
910 /// Blacklist a node from the mesh.
911 /** This function causes the local node to blacklist another node.
912  *  The local node will drop any existing connections to that node,
913  *  and will not send data to it nor accept any data received from it any more.
914  *
915  *  @param mesh         A handle which represents an instance of MeshLink.
916  *  @param node         A pointer to a meshlink_node_t describing the node to be blacklisted.
917  */
918 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
919
920 /// Whitelist a node on the mesh.
921 /** This function causes the local node to whitelist a previously blacklisted node.
922  *  The local node will allow connections to and from that node,
923  *  and will send data to it and accept any data received from it.
924  *
925  *  @param mesh         A handle which represents an instance of MeshLink.
926  *  @param node         A pointer to a meshlink_node_t describing the node to be whitelisted.
927  */
928 extern void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node);
929
930 /// Set whether new nodes are blacklisted by default.
931 /** This function sets the blacklist behaviour for newly discovered nodes.
932  *  If set to true, new nodes will be automatically blacklisted.
933  *  If set to false, which is the default, new nodes are automatically whitelisted.
934  *  The whitelist/blacklist status of a node may be changed afterwards with the
935  *  meshlink_whitelist() and meshlink_blacklist() functions.
936  *
937  *  @param mesh         A handle which represents an instance of MeshLink.
938  *  @param blacklist    True if new nodes are to be blacklisted, false if whitelisted.
939  */
940 extern void meshlink_set_default_blacklist(meshlink_handle_t *mesh, bool blacklist);
941
942 /// A callback for accepting incoming channels.
943 /** This function is called whenever a remote node wants to open a channel to the local node.
944  *  The application then has to decide whether to accept or reject this channel.
945  *
946  *  The callback is run in MeshLink's own thread.
947  *  It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
948  *  to hand any data over to the application's thread.
949  *
950  *  @param mesh         A handle which represents an instance of MeshLink.
951  *  @param channel      A handle for the incoming channel.
952  *                      If the application accepts the incoming channel by returning true,
953  *                      then this handle is valid until meshlink_channel_close() is called on it.
954  *                      If the application rejects the incoming channel by returning false,
955  *                      then this handle is invalid after the callback returns
956  *                      (the callback does not need to call meshlink_channel_close() itself in this case).
957  *  @param port         The port number the peer wishes to connect to.
958  *  @param data         A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
959  *                      The pointer is only valid during the lifetime of the callback.
960  *                      The callback should mempcy() the data if it needs to be available outside the callback.
961  *  @param len          The length of the data, or 0 in case no data has been received yet. (Not yet used.)
962  *
963  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
964  *                      If returning false, the channel is invalid and may not be used anymore.
965  */
966 typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len);
967
968 /// A callback for receiving data from a channel.
969 /** This function is called whenever data is received from a remote node on a channel.
970  *
971  *  This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
972  *  In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
973  *  In any case, the @a channel handle will still be valid until the application calls meshlink_close().
974  *
975  *  @param mesh         A handle which represents an instance of MeshLink.
976  *  @param channel      A handle for the channel.
977  *  @param data         A pointer to a buffer containing data sent by the source, or NULL in case of an error.
978  *                      The pointer is only valid during the lifetime of the callback.
979  *                      The callback should mempcy() the data if it needs to be available outside the callback.
980  *  @param len          The length of the data, or 0 in case of an error.
981  */
982 typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
983
984 /// A callback informing the application when data can be sent on a channel.
985 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
986  *
987  *  @param mesh         A handle which represents an instance of MeshLink.
988  *  @param channel      A handle for the channel.
989  *  @param len          The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
990  *                      or 0 in case of an error.
991  */
992 typedef void (*meshlink_channel_poll_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
993
994 /// Set the accept callback.
995 /** This functions sets the callback that is called whenever another node sends data to the local node.
996  *  The callback is run in MeshLink's own thread.
997  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
998  *  to hand the data over to the application's thread.
999  *  The callback should also not block itself and return as quickly as possible.
1000  *
1001  *  If no accept callback is set, incoming channels are rejected.
1002  *
1003  *  @param mesh      A handle which represents an instance of MeshLink.
1004  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1005  *                   If a NULL pointer is given, the callback will be disabled.
1006  */
1007 extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
1008
1009 /// Set the receive callback.
1010 /** This functions sets the callback that is called whenever another node sends data to the local node.
1011  *  The callback is run in MeshLink's own thread.
1012  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1013  *  to hand the data over to the application's thread.
1014  *  The callback should also not block itself and return as quickly as possible.
1015  *
1016  *  @param mesh      A handle which represents an instance of MeshLink.
1017  *  @param channel   A handle for the channel.
1018  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1019  *                   If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
1020  */
1021 extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
1022
1023 /// Set the poll callback.
1024 /** This functions sets the callback that is called whenever data can be sent to another node.
1025  *  The callback is run in MeshLink's own thread.
1026  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1027  *  to pass data to or from the application's thread.
1028  *  The callback should also not block itself and return as quickly as possible.
1029  *
1030  *  @param mesh      A handle which represents an instance of MeshLink.
1031  *  @param channel   A handle for the channel.
1032  *  @param cb        A pointer to the function which will be called when data can be sent to another node.
1033  *                   If a NULL pointer is given, the callback will be disabled.
1034  */
1035 extern void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb);
1036
1037 /// Open a reliable stream channel to another node.
1038 /** This function is called whenever a remote node wants to open a channel to the local node.
1039  *  The application then has to decide whether to accept or reject this channel.
1040  *
1041  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1042  *  When the application does no longer need to use this channel, it must call meshlink_close()
1043  *  to free its resources.
1044  *
1045  *  @param mesh         A handle which represents an instance of MeshLink.
1046  *  @param node         The node to which this channel is being initiated.
1047  *  @param port         The port number the peer wishes to connect to.
1048  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1049  *                      The pointer may be NULL, in which case incoming data is ignored.
1050  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1051  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1052  *  @param len          The length of the data, or 0 if there is no data to send.
1053  *  @param flags        A bitwise-or'd combination of flags that set the semantics for this channel.
1054  *
1055  *  @return             A handle for the channel, or NULL in case of an error.
1056  *                      The handle is valid until meshlink_channel_close() is called.
1057  */
1058 extern meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags);
1059
1060 /// Open a reliable stream channel to another node.
1061 /** This function is called whenever a remote node wants to open a channel to the local node.
1062  *  The application then has to decide whether to accept or reject this channel.
1063  *
1064  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1065  *  When the application does no longer need to use this channel, it must call meshlink_close()
1066  *  to free its resources.
1067  *
1068  *  Calling this function is equivalent to calling meshlink_channel_open_ex()
1069  *  with the flags set to MESHLINK_CHANNEL_TCP.
1070  *
1071  *  @param mesh         A handle which represents an instance of MeshLink.
1072  *  @param node         The node to which this channel is being initiated.
1073  *  @param port         The port number the peer wishes to connect to.
1074  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1075  *                      The pointer may be NULL, in which case incoming data is ignored.
1076  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1077  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1078  *  @param len          The length of the data, or 0 if there is no data to send.
1079  *
1080  *  @return             A handle for the channel, or NULL in case of an error.
1081  *                      The handle is valid until meshlink_channel_close() is called.
1082  */
1083 extern meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len);
1084
1085 /// Partially close a reliable stream channel.
1086 /** This shuts down the read or write side of a channel, or both, without closing the handle.
1087  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
1088  *  but still allows waiting for incoming data from the remote node.
1089  *
1090  *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
1091  *
1092  *  @param mesh         A handle which represents an instance of MeshLink.
1093  *  @param channel      A handle for the channel.
1094  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
1095  */
1096 extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction);
1097
1098 /// Close a reliable stream channel.
1099 /** This informs the remote node that the local node has finished sending all data on the channel.
1100  *  It also causes the local node to stop accepting incoming data from the remote node.
1101  *  It will free the struct meshlink_channel and all associated resources.
1102  *  Afterwards, the channel handle is invalid and must not be used any more.
1103  *
1104  *  It is allowed to call this function at any time on a valid handle, even inside callback functions.
1105  *  If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1106  *
1107  *  @param mesh         A handle which represents an instance of MeshLink.
1108  *  @param channel      A handle for the channel.
1109  */
1110 extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1111
1112 /// Transmit data on a channel
1113 /** This queues data to send to the remote node.
1114  *
1115  *  @param mesh         A handle which represents an instance of MeshLink.
1116  *  @param channel      A handle for the channel.
1117  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1118  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1119  *  @param len          The length of the data, or 0 if there is no data to send.
1120  *
1121  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1122  */
1123 extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
1124
1125 /// Get channel flags.
1126 /** This returns the flags used when opening this channel.
1127  *
1128  *  @param mesh         A handle which represents an instance of MeshLink.
1129  *  @param channel      A handle for the channel.
1130  *
1131  *  @return             The flags set for this channel.
1132  */
1133 extern uint32_t meshlink_channel_get_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1134
1135 /// Get the amount of bytes in the send buffer.
1136 /** This returns the amount of bytes in the send buffer.
1137  *  These bytes have not been received by the peer yet.
1138  *
1139  *  @param mesh         A handle which represents an instance of MeshLink.
1140  *  @param channel      A handle for the channel.
1141  *
1142  *  @return             The amount of un-ACKed bytes in the send buffer.
1143  */
1144 extern size_t meshlink_channel_get_sendq(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1145
1146 /// Get the amount of bytes in the receive buffer.
1147 /** This returns the amount of bytes in the receive buffer.
1148  *  These bytes have not been processed by the application yet.
1149  *
1150  *  @param mesh         A handle which represents an instance of MeshLink.
1151  *  @param channel      A handle for the channel.
1152  *
1153  *  @return             The amount of bytes in the receive buffer.
1154  */
1155 extern size_t meshlink_channel_get_recvq(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1156
1157 /// Hint that a hostname may be found at an address
1158 /** This function indicates to meshlink that the given hostname is likely found
1159  *  at the given IP address and port.
1160  *
1161  *  @param mesh     A handle which represents an instance of MeshLink.
1162  *  @param node     A pointer to a meshlink_node_t describing the node to add the address hint for.
1163  *  @param addr     The IP address and port which should be tried for the
1164  *                  given hostname. The caller is free to overwrite or free
1165  *                  this memory once meshlink returns.
1166  */
1167 extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr);
1168
1169 /// Enable or disable zeroconf discovery of local peers
1170
1171 /** This controls whether zeroconf discovery using the Catta library will be
1172  *  enabled to search for peers on the local network. By default, it is enabled.
1173  *
1174  *  @param mesh    A handle which represents an instance of MeshLink.
1175  *  @param enable  Set to true to enable discovery, false to disable.
1176  */
1177 extern void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable);
1178
1179 /// Performs key rotation for an encrypted storage
1180
1181 /** This rotates the (master) key for an encrypted storage and discards the old key
1182  *  if the call succeeded. This is an atomic call.
1183  *
1184  *  @param mesh     A handle which represents an instance of MeshLink.
1185  *  @param key      A pointer to the new key used to encrypt storage.
1186  *  @param keylen   The length of the new key in bytes.
1187  *
1188  *  @return         This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
1189  */
1190 extern bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, size_t new_keylen);
1191
1192 #ifdef __cplusplus
1193 }
1194 #endif
1195
1196 #endif