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