]> git.meshlink.io Git - meshlink/blob - src/meshlink.h
Add meshlink_set_node_channel_timeout().
[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_COUNT
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 struct meshlink_handle 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 struct meshlink_handle *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 struct meshlink_handle 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 struct meshlink_handle *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.
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.
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.
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.
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 struct meshlink_handle 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 struct meshlink_handle *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.
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.
287  *  @param devclass The device class which will be used in the mesh.
288  *
289  *  @return         A pointer to a struct meshlink_handle 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 struct meshlink_handle *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  *  \memberof meshlink_handle
301  *  @param mesh     A handle which represents an instance of MeshLink.
302  *
303  *  @param submesh  Name of the new Sub-Mesh to create.
304  *
305  *  @return         A pointer to a struct meshlink_submesh which represents this instance of SubMesh, or NULL in case of an error.
306  *                  The pointer is valid until meshlink_close() is called.
307  */
308 struct meshlink_submesh *meshlink_submesh_open(struct meshlink_handle *mesh, const char *submesh);
309
310 /// Start MeshLink.
311 /** This function causes MeshLink to open network sockets, make outgoing connections, and
312  *  create a new thread, which will handle all network I/O.
313  *
314  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
315  *
316  *  \memberof meshlink_handle
317  *  @param mesh     A handle which represents an instance of MeshLink.
318  *
319  *  @return         This function will return true if MeshLink has successfully started, false otherwise.
320  */
321 extern bool meshlink_start(struct meshlink_handle *mesh);
322
323 /// Stop MeshLink.
324 /** This function causes MeshLink to disconnect from all other nodes,
325  *  close all sockets, and shut down its own thread.
326  *
327  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
328  *  Channels that are still open will remain valid, but any communication via channels will stop as well.
329  *
330  *  \memberof meshlink_handle
331  *  @param mesh     A handle which represents an instance of MeshLink.
332  */
333 extern void meshlink_stop(struct meshlink_handle *mesh);
334
335 /// Close the MeshLink handle.
336 /** This function calls meshlink_stop() if necessary,
337  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
338  *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
339  *
340  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
341  *  If called at a proper time with a valid handle, this function always succeeds.
342  *  If called within a callback or with an invalid handle, the result is undefined.
343  *
344  * \memberof meshlink_handle
345  *  @param mesh     A handle which represents an instance of MeshLink.
346  */
347 extern void meshlink_close(struct meshlink_handle *mesh);
348
349 /// Destroy a MeshLink instance.
350 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
351  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
352  *  new instance.
353  *
354  *  @param confbase The directory in which MeshLink stores its configuration files.
355  *                  After the function returns, the application is free to overwrite or free @a confbase.
356  *
357  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
358  */
359 extern bool meshlink_destroy(const char *confbase);
360
361 /// A callback for receiving data from the mesh.
362 /** @param mesh      A handle which represents an instance of MeshLink.
363  *  @param source    A pointer to a struct meshlink_node describing the source of the data.
364  *  @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).
365  *                   The pointer is only valid during the lifetime of the callback.
366  *                   The callback should mempcy() the data if it needs to be available outside the callback.
367  *  @param len       The length of the received data, or 0 in case there is no data.
368  */
369 typedef void (*meshlink_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len);
370
371 /// Set the receive callback.
372 /** This functions sets the callback that is called whenever another node sends data to the local node.
373  *  The callback is run in MeshLink's own thread.
374  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
375  *  to hand the data over to the application's thread.
376  *  The callback should also not block itself and return as quickly as possible.
377  *
378  *  \memberof meshlink_handle
379  *  @param mesh      A handle which represents an instance of MeshLink.
380  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
381  *                   If a NULL pointer is given, the callback will be disabled.
382  */
383 extern void meshlink_set_receive_cb(struct meshlink_handle *mesh, meshlink_receive_cb_t cb);
384
385 /// A callback reporting the meta-connection attempt made by the host node to an another node.
386 /** @param mesh      A handle which represents an instance of MeshLink.
387  *  @param node      A pointer to a struct meshlink_node describing the node to whom meta-connection is being tried.
388  *                   This pointer is valid until meshlink_close() is called.
389  */
390 typedef void (*meshlink_connection_try_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
391
392 /// Set the meta-connection try callback.
393 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
394  *  The callback is run in MeshLink's own thread.
395  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
396  *  to hand the data over to the application's thread.
397  *  The callback should also not block itself and return as quickly as possible.
398  *
399  *  \memberof meshlink_handle
400  *  @param mesh      A handle which represents an instance of MeshLink.
401  *  @param cb        A pointer to the function which will be called when host node attempts to make
402  *                   the connection to another node. If a NULL pointer is given, the callback will be disabled.
403  */
404 extern void meshlink_set_connection_try_cb(struct meshlink_handle *mesh, meshlink_connection_try_cb_t cb);
405
406 /// A callback reporting node status changes.
407 /** @param mesh      A handle which represents an instance of MeshLink.
408  *  @param node       A pointer to a struct meshlink_node describing the node whose status changed.
409  *                    This pointer is valid until meshlink_close() is called.
410  *  @param reachable  True if the node is reachable, false otherwise.
411  */
412 typedef void (*meshlink_node_status_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, bool reachable);
413
414 /// Set the node status callback.
415 /** This functions sets the callback that is called whenever another node's status changed.
416  *  The callback is run in MeshLink's own thread.
417  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
418  *  to hand the data over to the application's thread.
419  *  The callback should also not block itself and return as quickly as possible.
420  *
421  *  \memberof meshlink_handle
422  *  @param mesh      A handle which represents an instance of MeshLink.
423  *  @param cb        A pointer to the function which will be called when another node's status changes.
424  *                   If a NULL pointer is given, the callback will be disabled.
425  */
426 extern void meshlink_set_node_status_cb(struct meshlink_handle *mesh, meshlink_node_status_cb_t cb);
427
428 /// A callback reporting node path MTU changes.
429 /** @param mesh      A handle which represents an instance of MeshLink.
430  *  @param node       A pointer to a struct meshlink_node describing the node whose status changed.
431  *                    This pointer is valid until meshlink_close() is called.
432  *  @param pmtu       The current path MTU to the node, or 0 if UDP communication is not (yet) possible.
433  */
434 typedef void (*meshlink_node_pmtu_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t pmtu);
435
436 /// Set the node extended status callback.
437 /** This functions sets the callback that is called whenever certain connectivity parameters for a node change.
438  *  The callback is run in MeshLink's own thread.
439  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
440  *  to hand the data over to the application's thread.
441  *  The callback should also not block itself and return as quickly as possible.
442  *
443  *  \memberof meshlink_handle
444  *  @param mesh      A handle which represents an instance of MeshLink.
445  *  @param cb        A pointer to the function which will be called when another node's extended status changes.
446  *                   If a NULL pointer is given, the callback will be disabled.
447  */
448 extern void meshlink_set_node_pmtu_cb(struct meshlink_handle *mesh, meshlink_node_pmtu_cb_t cb);
449
450 /// A callback reporting duplicate node detection.
451 /** @param mesh       A handle which represents an instance of MeshLink.
452  *  @param node       A pointer to a struct meshlink_node describing the node which is duplicate.
453  *                    This pointer is valid until meshlink_close() is called.
454  */
455 typedef void (*meshlink_node_duplicate_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
456
457 /// Set the node duplicate callback.
458 /** This functions sets the callback that is called whenever a duplicate node is detected.
459  *  The callback is run in MeshLink's own thread.
460  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
461  *  to hand the data over to the application's thread.
462  *  The callback should also not block itself and return as quickly as possible.
463  *
464  *  \memberof meshlink_handle
465  *  @param mesh      A handle which represents an instance of MeshLink.
466  *  @param cb        A pointer to the function which will be called when a duplicate node is detected.
467  *                   If a NULL pointer is given, the callback will be disabled.
468  */
469 extern void meshlink_set_node_duplicate_cb(struct meshlink_handle *mesh, meshlink_node_duplicate_cb_t cb);
470
471 /// Severity of log messages generated by MeshLink.
472 typedef enum {
473         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
474         MESHLINK_INFO,     ///< Informational messages.
475         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
476         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
477         MESHLINK_CRITICAL  ///< Critical errors which cause MeshLink to fail completely.
478 } meshlink_log_level_t;
479
480 /// A callback for receiving log messages generated by MeshLink.
481 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
482  *  @param level     An enum describing the severity level of the message.
483  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
484  *                   This pointer is only valid for the duration of the callback.
485  *                   The application must not free() this pointer.
486  *                   The application should strdup() the text if it has to be available outside the callback.
487  */
488 typedef void (*meshlink_log_cb_t)(struct meshlink_handle *mesh, meshlink_log_level_t level, const char *text);
489
490 /// Set the log callback.
491 /** This functions sets the callback that is called whenever MeshLink has some information to log.
492  *
493  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
494  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
495  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
496  *
497  *  If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
498  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
499  *  to hand the data over to the application's thread.
500  *  The callback should also not block itself and return as quickly as possible.
501  *
502  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
503  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
504  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
505  *
506  *  \memberof meshlink_handle
507  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
508  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
509  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
510  *                   If a NULL pointer is given, the callback will be disabled.
511  */
512 extern void meshlink_set_log_cb(struct meshlink_handle *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
513
514 /// A callback for receiving error conditions encountered by the MeshLink thread.
515 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
516  *  @param errno     The error code describing what kind of error occured.
517  */
518 typedef void (*meshlink_error_cb_t)(struct meshlink_handle *mesh, meshlink_errno_t meshlink_errno);
519
520 /// Set the error callback.
521 /** This functions sets the callback that is called whenever the MeshLink thread encounters a serious error.
522  *
523  *  While most API functions report an error directly to the caller in case something went wrong,
524  *  MeshLink also runs a background thread which can encounter error conditions.
525  *  Most of them will be dealt with automatically, however there can be errors that will prevent MeshLink from
526  *  working correctly. When the callback is called, it means that MeshLink is no longer functioning
527  *  as expected. The application should then present an error message and shut down, or perform any other
528  *  action it deems appropriate.
529  *
530  *  The callback is run in MeshLink's own thread.
531  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
532  *  to hand the data over to the application's thread.
533  *  The callback should also not block itself and return as quickly as possible.
534  *
535  *  Even though the callback signals a serious error inside MeshLink, all open handles are still valid,
536  *  and the application should close handles in exactly the same it would have to do if the callback
537  *  was not called. This must not be done inside the callback itself.
538  *
539  *  \memberof meshlink_handle
540  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
541  *  @param cb        A pointer to the function which will be called when a serious error is encountered.
542  *                   If a NULL pointer is given, the callback will be disabled.
543  */
544 extern void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb);
545
546 /// Send data to another node.
547 /** This functions sends one packet of data to another node in the mesh.
548  *  The packet is sent using UDP semantics, which means that
549  *  the packet is sent as one unit and is received as one unit,
550  *  and that there is no guarantee that the packet will arrive at the destination.
551  *  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.
552  *  The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
553  *  The application should take care of getting an acknowledgement and retransmission if necessary.
554  *
555  *  \memberof meshlink_node
556  *  @param mesh         A handle which represents an instance of MeshLink.
557  *  @param destination  A pointer to a struct meshlink_node describing the destination for the data.
558  *  @param data         A pointer to a buffer containing the data to be sent to the source.
559  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
560  *                      It is valid to specify a NULL pointer, but only if @a len is also 0.
561  *  @param len          The length of the data.
562  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
563  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
564  */
565 extern bool meshlink_send(struct meshlink_handle *mesh, struct meshlink_node *destination, const void *data, size_t len);
566
567 /// Query the maximum packet size that can be sent to a node.
568 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
569  *  The path MTU is a property of the path packets will take to the destination node over the Internet.
570  *  It can be different for different destination nodes.
571  *  and the path MTU can change at any point in time due to changes in the Internet.
572  *  Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
573  *
574  *  \memberof meshlink_node
575  *  @param mesh         A handle which represents an instance of MeshLink.
576  *  @param destination  A pointer to a struct meshlink_node describing the destination for the data.
577  *
578  *  @return             The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
579  *                      or a negative value in case of an error.
580  */
581 extern ssize_t meshlink_get_pmtu(struct meshlink_handle *mesh, struct meshlink_node *destination);
582
583 /// Get a handle for our own node.
584 /** This function returns a handle for the local node.
585  *
586  *  \memberof meshlink_handle
587  *  @param mesh         A handle which represents an instance of MeshLink.
588  *
589  *  @return             A pointer to a struct meshlink_node which represents the local node.
590  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
591  */
592 extern struct meshlink_node *meshlink_get_self(struct meshlink_handle *mesh);
593
594 /// Get a handle for a specific node.
595 /** This function returns a handle for the node with the given name.
596  *
597  *  \memberof meshlink_handle
598  *  @param mesh         A handle which represents an instance of MeshLink.
599  *  @param name         The name of the node for which a handle is requested.
600  *                      After this function returns, the application is free to overwrite or free @a name.
601  *
602  *  @return             A pointer to a struct meshlink_node which represents the requested node,
603  *                      or NULL if the requested node does not exist.
604  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
605  */
606 extern struct meshlink_node *meshlink_get_node(struct meshlink_handle *mesh, const char *name);
607
608 /// Get a handle for a specific submesh.
609 /** This function returns a handle for the submesh with the given name.
610  *
611  *  \memberof meshlink_handle
612  *  @param mesh         A handle which represents an instance of MeshLink.
613  *  @param name         The name of the submesh for which a handle is requested.
614  *                      After this function returns, the application is free to overwrite or free @a name.
615  *
616  *  @return             A pointer to a struct meshlink_submesh which represents the requested submesh,
617  *                      or NULL if the requested submesh does not exist.
618  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
619  */
620 extern struct meshlink_submesh *meshlink_get_submesh(struct meshlink_handle *mesh, const char *name);
621
622 /// Get the fingerprint of a node's public key.
623 /** This function returns a fingerprint of the node's public key.
624  *  It should be treated as an opaque blob.
625  *
626  *  \memberof meshlink_node
627  *  @param mesh         A handle which represents an instance of MeshLink.
628  *  @param node         A pointer to a struct meshlink_node describing the node.
629  *
630  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
631  *                      The application should call free() after it is done using this string.
632  */
633 extern char *meshlink_get_fingerprint(struct meshlink_handle *mesh, struct meshlink_node *node);
634
635 /// Get a list of all nodes.
636 /** This function returns a list with handles for all known nodes.
637  *
638  *  \memberof meshlink_handle
639  *  @param mesh         A handle which represents an instance of MeshLink.
640  *  @param nodes        A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
641  *                      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).
642  *                      The application is allowed to call free() on the array whenever it wishes.
643  *                      The pointers in the array are valid until meshlink_close() is called.
644  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
645  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
646  *                      The contents of this variable will be changed to reflect the new size of the array.
647  *
648  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
649  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
650  *                      If it is a new value, the old value of @a nodes should not be used anymore.
651  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
652  */
653 extern struct meshlink_node **meshlink_get_all_nodes(struct meshlink_handle *mesh, struct meshlink_node **nodes, size_t *nmemb);
654
655 /// Sign data using the local node's MeshLink key.
656 /** This function signs data using the local node's MeshLink key.
657  *  The generated signature can be securely verified by other nodes.
658  *
659  *  \memberof meshlink_handle
660  *  @param mesh         A handle which represents an instance of MeshLink.
661  *  @param data         A pointer to a buffer containing the data to be signed.
662  *  @param len          The length of the data to be signed.
663  *  @param signature    A pointer to a buffer where the signature will be stored.
664  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
665  *                      The signature is a binary blob, and is not nul-terminated.
666  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
667  *
668  *  @return             This function returns true if the signature was correctly generated, false otherwise.
669  */
670 extern bool meshlink_sign(struct meshlink_handle *mesh, const void *data, size_t len, void *signature, size_t *siglen);
671
672 /// Get the list of all nodes by device class.
673 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
674  *
675  *  \memberof meshlink_handle
676  *  @param mesh         A handle which represents an instance of MeshLink.
677  *  @param devclass     Device class of the nodes for which the list has to be obtained.
678  *  @param nodes        A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
679  *                      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).
680  *                      The application is allowed to call free() on the array whenever it wishes.
681  *                      The pointers in the array are valid until meshlink_close() is called.
682  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
683  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
684  *                      The contents of this variable will be changed to reflect the new size of the array.
685  *
686  *  @return             A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
687  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
688  *                      If it is a new value, the old value of @a nodes should not be used anymore.
689  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
690  */
691 extern struct meshlink_node **meshlink_get_all_nodes_by_dev_class(struct meshlink_handle *mesh, dev_class_t devclass, struct meshlink_node **nodes, size_t *nmemb);
692
693 /// Get the list of all nodes by Submesh.
694 /** This function returns a list with handles for all the nodes that matches with the given @a Submesh.
695  *
696  *  \memberof meshlink_submesh
697  *  @param mesh         A handle which represents an instance of MeshLink.
698  *  @param submesh      Submesh handle of the nodes for which the list has to be obtained.
699  *  @param nodes        A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
700  *                      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).
701  *                      The application is allowed to call free() on the array whenever it wishes.
702  *                      The pointers in the array are valid until meshlink_close() is called.
703  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
704  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
705  *                      The contents of this variable will be changed to reflect the new size of the array.
706  *
707  *  @return             A pointer to an array containing pointers to all known nodes of the given Submesh, or NULL in case of an error.
708  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
709  *                      If it is a new value, the old value of @a nodes should not be used anymore.
710  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
711  */
712 extern struct meshlink_node **meshlink_get_all_nodes_by_submesh(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, struct meshlink_node **nodes, size_t *nmemb);
713
714 /// Get the node's device class.
715 /** This function returns the device class of the given node.
716  *
717  *  \memberof meshlink_node
718  *  @param mesh          A handle which represents an instance of MeshLink.
719  *  @param node         A pointer to a struct meshlink_node describing the node.
720  *
721  *  @return              This function returns the device class of the @a node, or -1 in case of an error.
722  */
723 extern dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node);
724
725 /// Get the node's submesh handle.
726 /** This function returns the submesh handle of the given node.
727  *
728  *  \memberof meshlink_node
729  *  @param mesh          A handle which represents an instance of MeshLink.
730  *  @param node          A pointer to a struct meshlink_node describing the node.
731  *
732  *  @return              This function returns the submesh handle of the @a node, or NULL in case of an error.
733  */
734 extern struct meshlink_submesh *meshlink_get_node_submesh(struct meshlink_handle *mesh, struct meshlink_node *node);
735
736 /// Verify the signature generated by another node of a piece of data.
737 /** This function verifies the signature that another node generated for a piece of data.
738  *
739  *  \memberof meshlink_node
740  *  @param mesh         A handle which represents an instance of MeshLink.
741  *  @param source       A pointer to a struct meshlink_node describing the source of the signature.
742  *  @param data         A pointer to a buffer containing the data to be verified.
743  *  @param len          The length of the data to be verified.
744  *  @param signature    A pointer to a buffer where the signature is stored.
745  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
746  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
747  *
748  *  @return             This function returns true if the signature is valid, false otherwise.
749  */
750 extern bool meshlink_verify(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len, const void *signature, size_t siglen);
751
752 /// Set the canonical Address for a node.
753 /** This function sets the canonical Address for a node.
754  *  This address is stored permanently until it is changed by another call to this function,
755  *  unlike other addresses associated with a node,
756  *  such as those added with meshlink_hint_address() or addresses discovered at runtime.
757  *
758  *  If a canonical Address is set for the local node,
759  *  it will be used for the hostname part of generated invitation URLs.
760  *
761  *  \memberof meshlink_node
762  *  @param mesh         A handle which represents an instance of MeshLink.
763  *  @param node         A pointer to a struct meshlink_node describing the node.
764  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
765  *  @param port         A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
766  *                      If it is NULL, the listening port's number will be used.
767  *
768  *  @return             This function returns true if the address was added, false otherwise.
769  */
770 extern bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port);
771
772 /// Add an Address for the local node.
773 /** This function adds an Address for the local node, which will be used for invitation URLs.
774  *
775  *  \memberof meshlink_handle
776  *  @param mesh         A handle which represents an instance of MeshLink.
777  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
778  *
779  *  @return             This function returns true if the address was added, false otherwise.
780  */
781 extern bool meshlink_add_address(struct meshlink_handle *mesh, const char *address);
782
783 /// Try to discover the external address for the local node.
784 /** This function performs tries to discover the local node's external address
785  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
786  *  the FQDN associated with the address will be returned.
787  *
788  *  Please note that this is function only returns a single address,
789  *  even if the local node might have more than one external address.
790  *  In that case, there is no control over which address will be selected.
791  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
792  *  there is no guarantee that the external address will be valid for an extended period of time.
793  *
794  *  This function is blocking. It can take several seconds before it returns.
795  *  There is no guarantee it will be able to resolve the external address.
796  *  Failures might be because by temporary network outages.
797  *
798  *  \memberof meshlink_handle
799  *  @param mesh         A handle which represents an instance of MeshLink.
800  *
801  *  @return             This function returns a pointer to a C string containing the discovered external address,
802  *                      or NULL if there was an error looking up the address.
803  *                      After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
804  */
805 extern char *meshlink_get_external_address(struct meshlink_handle *mesh);
806
807 /// Try to discover the external address for the local node.
808 /** This function performs tries to discover the local node's external address
809  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
810  *  the FQDN associated with the address will be returned.
811  *
812  *  Please note that this is function only returns a single address,
813  *  even if the local node might have more than one external address.
814  *  In that case, there is no control over which address will be selected.
815  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
816  *  there is no guarantee that the external address will be valid for an extended period of time.
817  *
818  *  This function is blocking. It can take several seconds before it returns.
819  *  There is no guarantee it will be able to resolve the external address.
820  *  Failures might be because by temporary network outages.
821  *
822  *  \memberof meshlink_handle
823  *  @param mesh            A handle which represents an instance of MeshLink.
824  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
825  *                         this might return the external address for any working address family.
826  *
827  *  @return                This function returns a pointer to a C string containing the discovered external address,
828  *                         or NULL if there was an error looking up the address.
829  *                         After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
830  */
831 extern char *meshlink_get_external_address_for_family(struct meshlink_handle *mesh, int address_family);
832
833 /// Try to discover the local address for the local node.
834 /** This function performs tries to discover the address of the local interface used for outgoing connection.
835  *
836  *  Please note that this is function only returns a single address,
837  *  even if the interface might have more than one address.
838  *  In that case, there is no control over which address will be selected.
839  *  Also note that if you have a dynamic IP address,
840  *  there is no guarantee that the local address will be valid for an extended period of time.
841  *
842  *  This function will fail if it couldn't find a local address for the given address family.
843  *  If hostname resolving is requested, this function may block for a few seconds.
844  *
845  *  \memberof meshlink_handle
846  *  @param mesh            A handle which represents an instance of MeshLink.
847  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
848  *                         this might return the local address for any working address family.
849  *
850  *  @return                This function returns a pointer to a C string containing the discovered local address,
851  *                         or NULL if there was an error looking up the address.
852  *                         After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
853  */
854 extern char *meshlink_get_local_address_for_family(struct meshlink_handle *mesh, int address_family);
855
856 /// Try to discover the external address for the local node, and add it to its list of addresses.
857 /** This function is equivalent to:
858  *
859  *    meshlink_add_address(mesh, meshlink_get_external_address(mesh));
860  *
861  *  Read the description of meshlink_get_external_address() for the limitations of this function.
862  *
863  *  \memberof meshlink_handle
864  *  @param mesh         A handle which represents an instance of MeshLink.
865  *
866  *  @return             This function returns true if the address was added, false otherwise.
867  */
868 extern bool meshlink_add_external_address(struct meshlink_handle *mesh);
869
870 /// Get the network port used by the local node.
871 /** This function returns the network port that the local node is listening on.
872  *
873  *  \memberof meshlink_handle
874  *  @param mesh          A handle which represents an instance of MeshLink.
875  *
876  *  @return              This function returns the port number, or -1 in case of an error.
877  */
878 extern int meshlink_get_port(struct meshlink_handle *mesh);
879
880 /// Set the network port used by the local node.
881 /** This function sets the network port that the local node is listening on.
882  *  It may only be called when the mesh is not running.
883  *  If unsure, call meshlink_stop() before calling this function.
884  *  Also note that if your node is already part of a mesh with other nodes,
885  *  that the other nodes may no longer be able to initiate connections to the local node,
886  *  since they will try to connect to the previously configured port.
887  *
888  *  \memberof meshlink_handle
889  *  @param mesh          A handle which represents an instance of MeshLink.
890  *  @param port          The port number to listen on. This must be between 0 and 65535.
891  *                       If the port is set to 0, then MeshLink will listen on a port
892  *                       that is randomly assigned by the operating system every time meshlink_open() is called.
893  *
894  *  @return              This function returns true if the port was successfully changed
895  *                       to the desired port, false otherwise. If it returns false, there
896  *                       is no guarantee that MeshLink is listening on the old port.
897  */
898
899 extern bool meshlink_set_port(struct meshlink_handle *mesh, int port);
900
901 /// Set the timeout for invitations.
902 /** This function sets the timeout for invitations.
903  *  Note that timeouts are only checked at the time a node tries to join using an invitation.
904  *  The default timeout for invitations is 1 week.
905  *
906  *  \memberof meshlink_handle
907  *  @param mesh         A handle which represents an instance of MeshLink.
908  *  @param timeout      The timeout for invitations in seconds.
909  */
910 extern void meshlink_set_invitation_timeout(struct meshlink_handle *mesh, int timeout);
911
912 /// Invite another node into the mesh.
913 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
914  *  The generated invitation is a string containing a URL.
915  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
916  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
917  *
918  *  \memberof meshlink_handle
919  *  @param mesh         A handle which represents an instance of MeshLink.
920  *  @param submesh      A handle which represents an instance of SubMesh.
921  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
922  *                      After this function returns, the application is free to overwrite or free @a name.
923  *  @param flags        A bitwise-or'd combination of flags that controls how the URL is generated.
924  *
925  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
926  *                      The application should call free() after it has finished using the URL.
927  */
928 extern char *meshlink_invite_ex(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name, uint32_t flags);
929
930 /// Invite another node into the mesh.
931 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
932  *  The generated invitation is a string containing a URL.
933  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
934  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
935  *
936  *  Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
937  *
938  *  \memberof meshlink_handle
939  *  @param mesh         A handle which represents an instance of MeshLink.
940  *  @param submesh      A handle which represents an instance of SubMesh.
941  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
942  *                      After this function returns, the application is free to overwrite or free @a name.
943  *
944  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
945  *                      The application should call free() after it has finished using the URL.
946  */
947 extern char *meshlink_invite(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name);
948
949 /// Use an invitation to join a mesh.
950 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
951  *  An invitation can only be used if the local node has never connected to other nodes before.
952  *  After a successfully accepted invitation, the name of the local node may have changed.
953  *
954  *  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.
955  *
956  *  This function is blocking. It can take several seconds before it returns.
957  *  There is no guarantee it will perform a successful join.
958  *  Failures might be caused by temporary network outages, or by the invitation having expired.
959  *
960  *  \memberof meshlink_handle
961  *  @param mesh         A handle which represents an instance of MeshLink.
962  *  @param invitation   A nul-terminated C string containing the invitation URL.
963  *                      After this function returns, the application is free to overwrite or free @a invitation.
964  *
965  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
966  */
967 extern bool meshlink_join(struct meshlink_handle *mesh, const char *invitation);
968
969 /// Export the local node's key and addresses.
970 /** This function generates a string that contains the local node's public key and one or more IP addresses.
971  *  The application can pass it in some way to another node, which can then import it,
972  *  granting the local node access to the other node's mesh.
973  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
974  *
975  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
976  *
977  *  \memberof meshlink_handle
978  *  @param mesh         A handle which represents an instance of MeshLink.
979  *
980  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
981  *                      The application should call free() after it has finished using this string.
982  */
983 extern char *meshlink_export(struct meshlink_handle *mesh);
984
985 /// Import another node's key and addresses.
986 /** This function accepts a string containing the exported public key and addresses of another node.
987  *  By importing this data, the local node grants the other node access to its mesh.
988  *  The application should make sure that the data it imports is really coming from the node it wants to import,
989  *
990  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
991  *
992  *  \memberof meshlink_handle
993  *  @param mesh         A handle which represents an instance of MeshLink.
994  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
995  *                      After this function returns, the application is free to overwrite or free @a data.
996  *
997  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
998  */
999 extern bool meshlink_import(struct meshlink_handle *mesh, const char *data);
1000
1001 /// Blacklist a node from the mesh.
1002 /** This function causes the local node to blacklist another node.
1003  *  The local node will drop any existing connections to that node,
1004  *  and will not send data to it nor accept any data received from it any more.
1005  *
1006  *  \memberof meshlink_node
1007  *  @param mesh         A handle which represents an instance of MeshLink.
1008  *  @param node         A pointer to a struct meshlink_node describing the node to be blacklisted.
1009  */
1010 extern void meshlink_blacklist(struct meshlink_handle *mesh, struct meshlink_node *node);
1011
1012 /// Whitelist a node on the mesh.
1013 /** This function causes the local node to whitelist a previously blacklisted node.
1014  *  The local node will allow connections to and from that node,
1015  *  and will send data to it and accept any data received from it.
1016  *
1017  *  \memberof meshlink_node
1018  *  @param mesh         A handle which represents an instance of MeshLink.
1019  *  @param node         A pointer to a struct meshlink_node describing the node to be whitelisted.
1020  */
1021 extern void meshlink_whitelist(struct meshlink_handle *mesh, struct meshlink_node *node);
1022
1023 /// Set whether new nodes are blacklisted by default.
1024 /** This function sets the blacklist behaviour for newly discovered nodes.
1025  *  If set to true, new nodes will be automatically blacklisted.
1026  *  If set to false, which is the default, new nodes are automatically whitelisted.
1027  *  The whitelist/blacklist status of a node may be changed afterwards with the
1028  *  meshlink_whitelist() and meshlink_blacklist() functions.
1029  *
1030  *  \memberof meshlink_handle
1031  *  @param mesh         A handle which represents an instance of MeshLink.
1032  *  @param blacklist    True if new nodes are to be blacklisted, false if whitelisted.
1033  */
1034 extern void meshlink_set_default_blacklist(struct meshlink_handle *mesh, bool blacklist);
1035
1036 /// A callback for accepting incoming channels.
1037 /** This function is called whenever a remote node wants to open a channel to the local node.
1038  *  The application then has to decide whether to accept or reject this channel.
1039  *
1040  *  The callback is run in MeshLink's own thread.
1041  *  It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
1042  *  to hand any data over to the application's thread.
1043  *
1044  *  @param mesh         A handle which represents an instance of MeshLink.
1045  *  @param channel      A handle for the incoming channel.
1046  *                      If the application accepts the incoming channel by returning true,
1047  *                      then this handle is valid until meshlink_channel_close() is called on it.
1048  *                      If the application rejects the incoming channel by returning false,
1049  *                      then this handle is invalid after the callback returns
1050  *                      (the callback does not need to call meshlink_channel_close() itself in this case).
1051  *  @param port         The port number the peer wishes to connect to.
1052  *  @param data         A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
1053  *                      The pointer is only valid during the lifetime of the callback.
1054  *                      The callback should mempcy() the data if it needs to be available outside the callback.
1055  *  @param len          The length of the data, or 0 in case no data has been received yet. (Not yet used.)
1056  *
1057  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
1058  *                      If returning false, the channel is invalid and may not be used anymore.
1059  */
1060 typedef bool (*meshlink_channel_accept_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, uint16_t port, const void *data, size_t len);
1061
1062 /// A callback for receiving data from a channel.
1063 /** This function is called whenever data is received from a remote node on a channel.
1064  *
1065  *  This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
1066  *  In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
1067  *  In any case, the @a channel handle will still be valid until the application calls meshlink_close().
1068  *
1069  *  @param mesh         A handle which represents an instance of MeshLink.
1070  *  @param channel      A handle for the channel.
1071  *  @param data         A pointer to a buffer containing data sent by the source, or NULL in case of an error.
1072  *                      The pointer is only valid during the lifetime of the callback.
1073  *                      The callback should mempcy() the data if it needs to be available outside the callback.
1074  *  @param len          The length of the data, or 0 in case of an error.
1075  */
1076 typedef void (*meshlink_channel_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len);
1077
1078 /// A callback informing the application when data can be sent on a channel.
1079 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
1080  *
1081  *  @param mesh         A handle which represents an instance of MeshLink.
1082  *  @param channel      A handle for the channel.
1083  *  @param len          The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
1084  *                      or 0 in case of an error.
1085  */
1086 typedef void (*meshlink_channel_poll_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t len);
1087
1088 /// Set the accept callback.
1089 /** This functions sets the callback that is called whenever another node sends data to the local node.
1090  *  The callback is run in MeshLink's own thread.
1091  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1092  *  to hand the data over to the application's thread.
1093  *  The callback should also not block itself and return as quickly as possible.
1094  *
1095  *  If no accept callback is set, incoming channels are rejected.
1096  *
1097  *  \memberof meshlink_handle
1098  *  @param mesh      A handle which represents an instance of MeshLink.
1099  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1100  *                   If a NULL pointer is given, the callback will be disabled.
1101  */
1102 extern void meshlink_set_channel_accept_cb(struct meshlink_handle *mesh, meshlink_channel_accept_cb_t cb);
1103
1104 /// Set the receive callback.
1105 /** This functions sets the callback that is called whenever another node sends data to the local node.
1106  *  The callback is run in MeshLink's own thread.
1107  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1108  *  to hand the data over to the application's thread.
1109  *  The callback should also not block itself and return as quickly as possible.
1110  *
1111  *  \memberof meshlink_channel
1112  *  @param mesh      A handle which represents an instance of MeshLink.
1113  *  @param channel   A handle for the channel.
1114  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1115  *                   If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
1116  */
1117 extern void meshlink_set_channel_receive_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_receive_cb_t cb);
1118
1119 /// Set the poll callback.
1120 /** This functions sets the callback that is called whenever data can be sent to another node.
1121  *  The callback is run in MeshLink's own thread.
1122  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1123  *  to pass data to or from the application's thread.
1124  *  The callback should also not block itself and return as quickly as possible.
1125  *
1126  *  \memberof meshlink_channel
1127  *  @param mesh      A handle which represents an instance of MeshLink.
1128  *  @param channel   A handle for the channel.
1129  *  @param cb        A pointer to the function which will be called when data can be sent to another node.
1130  *                   If a NULL pointer is given, the callback will be disabled.
1131  */
1132 extern void meshlink_set_channel_poll_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_poll_cb_t cb);
1133
1134 /// Set the send buffer size of a channel.
1135 /** This function sets the desired size of the send buffer.
1136  *  The default size is 128 kB.
1137  *
1138  *  \memberof meshlink_channel
1139  *  @param mesh      A handle which represents an instance of MeshLink.
1140  *  @param channel   A handle for the channel.
1141  *  @param size      The desired size for the send buffer.
1142  *                   If a NULL pointer is given, the callback will be disabled.
1143  */
1144 extern void meshlink_set_channel_sndbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1145
1146 /// Set the receive buffer size of a channel.
1147 /** This function sets the desired size of the receive buffer.
1148  *  The default size is 128 kB.
1149  *
1150  *  \memberof meshlink_channel
1151  *  @param mesh      A handle which represents an instance of MeshLink.
1152  *  @param channel   A handle for the channel.
1153  *  @param size      The desired size for the send buffer.
1154  *                   If a NULL pointer is given, the callback will be disabled.
1155  */
1156 extern void meshlink_set_channel_rcvbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1157
1158 /// Open a reliable stream channel to another node.
1159 /** This function is called whenever a remote node wants to open a channel to the local node.
1160  *  The application then has to decide whether to accept or reject this channel.
1161  *
1162  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1163  *  When the application does no longer need to use this channel, it must call meshlink_close()
1164  *  to free its resources.
1165  *
1166  *  \memberof meshlink_node
1167  *  @param mesh         A handle which represents an instance of MeshLink.
1168  *  @param node         The node to which this channel is being initiated.
1169  *  @param port         The port number the peer wishes to connect to.
1170  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1171  *                      The pointer may be NULL, in which case incoming data is ignored.
1172  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1173  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1174  *  @param len          The length of the data, or 0 if there is no data to send.
1175  *  @param flags        A bitwise-or'd combination of flags that set the semantics for this channel.
1176  *
1177  *  @return             A handle for the channel, or NULL in case of an error.
1178  *                      The handle is valid until meshlink_channel_close() is called.
1179  */
1180 extern struct meshlink_channel *meshlink_channel_open_ex(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags);
1181
1182 /// Open a reliable stream channel to another node.
1183 /** This function is called whenever a remote node wants to open a channel to the local node.
1184  *  The application then has to decide whether to accept or reject this channel.
1185  *
1186  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1187  *  When the application does no longer need to use this channel, it must call meshlink_close()
1188  *  to free its resources.
1189  *
1190  *  Calling this function is equivalent to calling meshlink_channel_open_ex()
1191  *  with the flags set to MESHLINK_CHANNEL_TCP.
1192  *
1193  *  \memberof meshlink_node
1194  *  @param mesh         A handle which represents an instance of MeshLink.
1195  *  @param node         The node to which this channel is being initiated.
1196  *  @param port         The port number the peer wishes to connect to.
1197  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1198  *                      The pointer may be NULL, in which case incoming data is ignored.
1199  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1200  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1201  *  @param len          The length of the data, or 0 if there is no data to send.
1202  *
1203  *  @return             A handle for the channel, or NULL in case of an error.
1204  *                      The handle is valid until meshlink_channel_close() is called.
1205  */
1206 extern struct meshlink_channel *meshlink_channel_open(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len);
1207
1208 /// Partially close a reliable stream channel.
1209 /** This shuts down the read or write side of a channel, or both, without closing the handle.
1210  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
1211  *  but still allows waiting for incoming data from the remote node.
1212  *
1213  *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
1214  *
1215  *  \memberof meshlink_channel
1216  *  @param mesh         A handle which represents an instance of MeshLink.
1217  *  @param channel      A handle for the channel.
1218  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
1219  */
1220 extern void meshlink_channel_shutdown(struct meshlink_handle *mesh, struct meshlink_channel *channel, int direction);
1221
1222 /// Close a reliable stream channel.
1223 /** This informs the remote node that the local node has finished sending all data on the channel.
1224  *  It also causes the local node to stop accepting incoming data from the remote node.
1225  *  It will free the struct meshlink_channel and all associated resources.
1226  *  Afterwards, the channel handle is invalid and must not be used any more.
1227  *
1228  *  It is allowed to call this function at any time on a valid handle, even inside callback functions.
1229  *  If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1230  *
1231  *  \memberof meshlink_channel
1232  *  @param mesh         A handle which represents an instance of MeshLink.
1233  *  @param channel      A handle for the channel.
1234  */
1235 extern void meshlink_channel_close(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1236
1237 /// Transmit data on a channel
1238 /** This queues data to send to the remote node.
1239  *
1240  *  \memberof meshlink_channel
1241  *  @param mesh         A handle which represents an instance of MeshLink.
1242  *  @param channel      A handle for the channel.
1243  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1244  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1245  *  @param len          The length of the data, or 0 if there is no data to send.
1246  *
1247  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1248  *                      If MESHLINK_CHANNEL_NO_PARTIAL is set, then the result will either be len,
1249  *                      0 if the buffer is currently too full, or -1 if len is too big even for an empty buffer.
1250  */
1251 extern ssize_t meshlink_channel_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len);
1252
1253 /// A callback for cleaning up buffers submitted for asynchronous I/O.
1254 /** This callbacks signals that MeshLink has finished using this buffer.
1255  *  The ownership of the buffer is now back into the application's hands.
1256  *
1257  *  @param mesh      A handle which represents an instance of MeshLink.
1258  *  @param channel   A handle for the channel which used this buffer.
1259  *  @param data      A pointer to a buffer containing the enqueued data.
1260  *  @param len       The length of the buffer.
1261  *  @param priv      A private pointer which was set by the application when submitting the buffer.
1262  */
1263 typedef void (*meshlink_aio_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, void *priv);
1264
1265 /// A callback for asynchronous I/O to and from filedescriptors.
1266 /** This callbacks signals that MeshLink has finished using this filedescriptor.
1267  *
1268  *  @param mesh      A handle which represents an instance of MeshLink.
1269  *  @param channel   A handle for the channel which used this filedescriptor.
1270  *  @param fd        The filedescriptor that was used.
1271  *  @param len       The length of the data that was successfully sent or received.
1272  *  @param priv      A private pointer which was set by the application when submitting the buffer.
1273  */
1274 typedef void (*meshlink_aio_fd_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, void *priv);
1275
1276 /// Transmit data on a channel asynchronously
1277 /** This registers a buffer that will be used to send data to the remote node.
1278  *  Multiple buffers can be registered, in which case data will be sent in the order the buffers were registered.
1279  *  While there are still buffers with unsent data, the poll callback will not be called.
1280  *
1281  *  \memberof meshlink_channel
1282  *  @param mesh         A handle which represents an instance of MeshLink.
1283  *  @param channel      A handle for the channel.
1284  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1285  *                      After meshlink_channel_aio_send() returns, the buffer may not be modified or freed by the application
1286  *                      until the callback routine is called.
1287  *  @param len          The length of the data, or 0 if there is no data to send.
1288  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the buffer.
1289  *  @param priv         A private pointer which is passed unchanged to the callback.
1290  *
1291  *  @return             True if the buffer was enqueued, false otherwise.
1292  */
1293 extern bool meshlink_channel_aio_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv);
1294
1295 /// Transmit data on a channel asynchronously from a filedescriptor
1296 /** This will read up to the specified length number of bytes from the given filedescriptor, and send it over the channel.
1297  *  The callback may be returned early if there is an error reading from the filedescriptor.
1298  *  While there is still with unsent data, the poll callback will not be called.
1299  *
1300  *  \memberof meshlink_channel
1301  *  @param mesh         A handle which represents an instance of MeshLink.
1302  *  @param channel      A handle for the channel.
1303  *  @param fd           A file descriptor from which data will be read.
1304  *  @param len          The length of the data, or 0 if there is no data to send.
1305  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1306  *  @param priv         A private pointer which is passed unchanged to the callback.
1307  *
1308  *  @return             True if the buffer was enqueued, false otherwise.
1309  */
1310 extern bool meshlink_channel_aio_fd_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv);
1311
1312 /// Receive data on a channel asynchronously
1313 /** This registers a buffer that will be filled with incoming channel data.
1314  *  Multiple buffers can be registered, in which case data will be received in the order the buffers were registered.
1315  *  While there are still buffers that have not been filled, the receive callback will not be called.
1316  *
1317  *  \memberof meshlink_channel
1318  *  @param mesh         A handle which represents an instance of MeshLink.
1319  *  @param channel      A handle for the channel.
1320  *  @param data         A pointer to a buffer that will be filled with incoming data.
1321  *                      After meshlink_channel_aio_receive() returns, the buffer may not be modified or freed by the application
1322  *                      until the callback routine is called.
1323  *  @param len          The length of the data.
1324  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the buffer.
1325  *  @param priv         A private pointer which is passed unchanged to the callback.
1326  *
1327  *  @return             True if the buffer was enqueued, false otherwise.
1328  */
1329 extern bool meshlink_channel_aio_receive(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv);
1330
1331 /// Receive data on a channel asynchronously and send it to a filedescriptor
1332 /** This will read up to the specified length number of bytes from the channel, and send it to the filedescriptor.
1333  *  The callback may be returned early if there is an error writing to the filedescriptor.
1334  *  While there is still unread data, the receive callback will not be called.
1335  *
1336  *  \memberof meshlink_channel
1337  *  @param mesh         A handle which represents an instance of MeshLink.
1338  *  @param channel      A handle for the channel.
1339  *  @param fd           A file descriptor to which data will be written.
1340  *  @param len          The length of the data.
1341  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1342  *  @param priv         A private pointer which was set by the application when submitting the buffer.
1343  *
1344  *  @return             True if the buffer was enqueued, false otherwise.
1345  */
1346 extern bool meshlink_channel_aio_fd_receive(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv);
1347
1348 /// Get channel flags.
1349 /** This returns the flags used when opening this channel.
1350  *
1351  *  \memberof meshlink_channel
1352  *  @param mesh         A handle which represents an instance of MeshLink.
1353  *  @param channel      A handle for the channel.
1354  *
1355  *  @return             The flags set for this channel.
1356  */
1357 extern uint32_t meshlink_channel_get_flags(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1358
1359 /// Get the amount of bytes in the send buffer.
1360 /** This returns the amount of bytes in the send buffer.
1361  *  These bytes have not been received by the peer yet.
1362  *
1363  *  \memberof meshlink_channel
1364  *  @param mesh         A handle which represents an instance of MeshLink.
1365  *  @param channel      A handle for the channel.
1366  *
1367  *  @return             The amount of un-ACKed bytes in the send buffer.
1368  */
1369 extern size_t meshlink_channel_get_sendq(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1370
1371 /// Get the amount of bytes in the receive buffer.
1372 /** This returns the amount of bytes in the receive buffer.
1373  *  These bytes have not been processed by the application yet.
1374  *
1375  *  \memberof meshlink_channel
1376  *  @param mesh         A handle which represents an instance of MeshLink.
1377  *  @param channel      A handle for the channel.
1378  *
1379  *  @return             The amount of bytes in the receive buffer.
1380  */
1381 extern size_t meshlink_channel_get_recvq(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1382
1383 /// Set the connection timeout used for channels to the given node.
1384 /** This sets the timeout after which unresponsive channels will be reported as closed.
1385  *  The timeout is set for all current and future channels to the given node.
1386  *
1387  *  \memberof meshlink_node
1388  *  @param mesh         A handle which represents an instance of MeshLink.
1389  *  @param channel      A handle for the channel.
1390  *  @param timeout      The timeout in seconds after which unresponsive channels will be reported as closed.
1391  *                      The default is 60 seconds.
1392  */
1393 extern void meshlink_set_node_channel_timeout(struct meshlink_handle *mesh, struct meshlink_node *node, int timeout);
1394
1395 /// Hint that a hostname may be found at an address
1396 /** This function indicates to meshlink that the given hostname is likely found
1397  *  at the given IP address and port.
1398  *
1399  *  \memberof meshlink_node
1400  *  @param mesh     A handle which represents an instance of MeshLink.
1401  *  @param node     A pointer to a struct meshlink_node describing the node to add the address hint for.
1402  *  @param addr     The IP address and port which should be tried for the
1403  *                  given hostname. The caller is free to overwrite or free
1404  *                  this memory once meshlink returns.
1405  */
1406 extern void meshlink_hint_address(struct meshlink_handle *mesh, struct meshlink_node *node, const struct sockaddr *addr);
1407
1408 /// Enable or disable zeroconf discovery of local peers
1409 /** This controls whether zeroconf discovery using the Catta library will be
1410  *  enabled to search for peers on the local network. By default, it is enabled.
1411  *
1412  *  \memberof meshlink_handle
1413  *  @param mesh    A handle which represents an instance of MeshLink.
1414  *  @param enable  Set to true to enable discovery, false to disable.
1415  */
1416 extern void meshlink_enable_discovery(struct meshlink_handle *mesh, bool enable);
1417
1418 /// Performs key rotation for an encrypted storage
1419 /** This rotates the (master) key for an encrypted storage and discards the old key
1420  *  if the call succeeded. This is an atomic call.
1421  *
1422  *  \memberof meshlink_handle
1423  *  @param mesh     A handle which represents an instance of MeshLink.
1424  *  @param key      A pointer to the new key used to encrypt storage.
1425  *  @param keylen   The length of the new key in bytes.
1426  *
1427  *  @return         This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
1428  */
1429 extern bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const void *key, size_t keylen);
1430
1431 /// Set device class timeouts
1432 /** This sets the ping interval and timeout for a given device class.
1433  *
1434  *  \memberof meshlink_handle
1435  *  @param mesh          A handle which represents an instance of MeshLink.
1436  *  @param devclass      The device class to update
1437  *  @param pinginterval  The interval between keepalive packets, in seconds. The default is 60.
1438  *  @param pingtimeout   The required time within which a peer should respond, in seconds. The default is 5.
1439  *                       The timeout must be smaller than the interval.
1440  */
1441 extern void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout);
1442
1443 #ifdef __cplusplus
1444 }
1445 #endif
1446
1447 #endif