]> git.meshlink.io Git - meshlink/blob - src/meshlink.h
2877c64e6e1f1a3c4730cb1572566edd8de229e6
[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_EBLACKLISTED, ///< The operation is not allowed because the node is blacklisted
75 } meshlink_errno_t;
76
77 /// Device class
78 typedef enum {
79         DEV_CLASS_BACKBONE = 0,
80         DEV_CLASS_STATIONARY = 1,
81         DEV_CLASS_PORTABLE = 2,
82         DEV_CLASS_UNKNOWN = 3,
83         DEV_CLASS_COUNT
84 } dev_class_t;
85
86 /// Invitation flags
87 static const uint32_t MESHLINK_INVITE_LOCAL = 1;    // Only use local addresses in the URL
88 static const uint32_t MESHLINK_INVITE_PUBLIC = 2;   // Only use public or canonical addresses in the URL
89 static const uint32_t MESHLINK_INVITE_IPV4 = 4;     // Only use IPv4 addresses in the URL
90 static const uint32_t MESHLINK_INVITE_IPV6 = 8;     // Only use IPv6 addresses in the URL
91 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
92
93 /// Channel flags
94 static const uint32_t MESHLINK_CHANNEL_RELIABLE = 1;   // Data is retransmitted when packets are lost.
95 static const uint32_t MESHLINK_CHANNEL_ORDERED = 2;    // Data is delivered in-order to the application.
96 static const uint32_t MESHLINK_CHANNEL_FRAMED = 4;     // Data is delivered in chunks of the same length as data was originally sent.
97 static const uint32_t MESHLINK_CHANNEL_DROP_LATE = 8;  // When packets are reordered, late packets are ignored.
98 static const uint32_t MESHLINK_CHANNEL_NO_PARTIAL = 16; // Calls to meshlink_channel_send() will either send all data or nothing.
99 static const uint32_t MESHLINK_CHANNEL_TCP = 3;        // Select TCP semantics.
100 static const uint32_t MESHLINK_CHANNEL_UDP = 0;        // Select UDP semantics.
101
102 /// A variable holding the last encountered error from MeshLink.
103 /** This is a thread local variable that contains the error code of the most recent error
104  *  encountered by a MeshLink API function called in the current thread.
105  *  The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
106  *  if a function returned successfully.
107  */
108 extern __thread meshlink_errno_t meshlink_errno;
109
110 #ifndef MESHLINK_INTERNAL_H
111
112 struct meshlink_handle {
113         const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
114         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
115 };
116
117 struct meshlink_node {
118         const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
119         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
120 };
121
122 struct meshlink_submesh {
123         const char *const name; ///< Textual name of this Sub-Mesh. It is stored in a nul-terminated C string, which is allocated by MeshLink.
124         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
125 };
126
127 struct meshlink_channel {
128         struct meshlink_node *const node; ///< Pointer to the peer of this channel.
129         void *priv;                       ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
130 };
131
132 #endif // MESHLINK_INTERNAL_H
133
134 /// Get the text for the given MeshLink error code.
135 /** This function returns a pointer to the string containing the description of the given error code.
136  *
137  *  @param err      An error code returned by MeshLink.
138  *
139  *  @return         A pointer to a string containing the description of the error code.
140  *                  The pointer is to static storage that is valid for the lifetime of the application.
141  *                  This function will always return a valid pointer, even if an invalid error code has been passed.
142  */
143 extern const char *meshlink_strerror(meshlink_errno_t err) __attribute__((__warn_unused_result__));
144
145 /// Create a new meshlink_open_params_t struct.
146 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
147  *  The resulting struct may be reused for multiple calls to meshlink_open_ex().
148  *  After the last use, the application must free this struct using meshlink_open_params_free().
149  *
150  *  @param confbase The directory in which MeshLink will store its configuration files.
151  *                  After the function returns, the application is free to overwrite or free @a confbase.
152  *  @param name     The name which this instance of the application will use in the mesh.
153  *                  After the function returns, the application is free to overwrite or free @a name.
154  *  @param appname  The application name which will be used in the mesh.
155  *                  After the function returns, the application is free to overwrite or free @a name.
156  *  @param devclass The device class which will be used in the mesh.
157  *
158  *  @return         A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
159  *                  The pointer is valid until meshlink_open_params_free() is called.
160  */
161 extern meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
162
163 /// Free a meshlink_open_params_t struct.
164 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
165  *
166  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
167  */
168 extern void meshlink_open_params_free(meshlink_open_params_t *params);
169
170 /// Set the network namespace MeshLink should use.
171 /** This function changes the open parameters to use the given netns filedescriptor.
172  *
173  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
174  *  @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.
175  *
176  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
177  */
178 extern bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) __attribute__((__warn_unused_result__));
179
180 /// Set the encryption key MeshLink should use for local storage.
181 /** This function changes the open parameters to use the given key for encrypting MeshLink's own configuration files.
182  *
183  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
184  *  @param key      A pointer to a key, or NULL in case no encryption should be used.
185  *  @param keylen   The length of the given key, or 0 in case no encryption should be used.
186  *
187  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
188  */
189 extern bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
190
191 /// Open or create a MeshLink instance.
192 /** This function opens or creates a MeshLink instance.
193  *  All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
194  *  which must have been initialized earlier by the application.
195  *
196  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
197  *  When the application does no longer need to use this handle, it must call meshlink_close() to
198  *  free its resources.
199  *
200  *  This function does not start any network I/O yet. The application should
201  *  first set callbacks, and then call meshlink_start().
202  *
203  *  @param params   A pointer to a meshlink_open_params_t which must be filled in by the application.
204  *                  After the function returns, the application is free to reuse or free @a params.
205  *
206  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
207  *                  The pointer is valid until meshlink_close() is called.
208  */
209 extern struct meshlink_handle *meshlink_open_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
210
211 /// Open or create a MeshLink instance.
212 /** This function opens or creates a MeshLink instance.
213  *  The state is stored in the configuration directory passed in the variable @a confbase.
214  *  If the configuration directory does not exist yet, for example when it is the first time
215  *  this instance is opened, the configuration directory will be automatically created and initialized.
216  *  However, the parent directory should already exist, otherwise an error will be returned.
217  *
218  *  The name given should be a unique identifier for this instance.
219  *
220  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
221  *  When the application does no longer need to use this handle, it must call meshlink_close() to
222  *  free its resources.
223  *
224  *  This function does not start any network I/O yet. The application should
225  *  first set callbacks, and then call meshlink_start().
226  *
227  *  @param confbase The directory in which MeshLink will store its configuration files.
228  *                  After the function returns, the application is free to overwrite or free @a confbase.
229  *  @param name     The name which this instance of the application will use in the mesh.
230  *                  After the function returns, the application is free to overwrite or free @a name.
231  *  @param appname  The application name which will be used in the mesh.
232  *                  After the function returns, the application is free to overwrite or free @a name.
233  *  @param devclass The device class which will be used in the mesh.
234  *
235  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
236  *                  The pointer is valid until meshlink_close() is called.
237  */
238 extern struct meshlink_handle *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
239
240 /// Open or create a MeshLink instance that uses encrypted storage.
241 /** This function opens or creates a MeshLink instance.
242  *  The state is stored in the configuration directory passed in the variable @a confbase.
243  *  If the configuration directory does not exist yet, for example when it is the first time
244  *  this instance is opened, the configuration directory will be automatically created and initialized.
245  *  However, the parent directory should already exist, otherwise an error will be returned.
246  *
247  *  The name given should be a unique identifier for this instance.
248  *
249  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
250  *  When the application does no longer need to use this handle, it must call meshlink_close() to
251  *  free its resources.
252  *
253  *  This function does not start any network I/O yet. The application should
254  *  first set callbacks, and then call meshlink_start().
255  *
256  *  @param confbase The directory in which MeshLink will store its configuration files.
257  *                  After the function returns, the application is free to overwrite or free @a confbase.
258  *  @param name     The name which this instance of the application will use in the mesh.
259  *                  After the function returns, the application is free to overwrite or free @a name.
260  *  @param appname  The application name which will be used in the mesh.
261  *                  After the function returns, the application is free to overwrite or free @a name.
262  *  @param devclass The device class which will be used in the mesh.
263  *  @param key      A pointer to a key used to encrypt storage.
264  *  @param keylen   The length of the key in bytes.
265  *
266  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
267  *                  The pointer is valid until meshlink_close() is called.
268  */
269 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) __attribute__((__warn_unused_result__));
270
271 /// Create an ephemeral MeshLink instance that does not store any state.
272 /** This function creates a MeshLink instance.
273  *  No state is ever saved, so once this instance is closed, all its state is gone.
274  *
275  *  The name given should be a unique identifier for this instance.
276  *
277  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
278  *  When the application does no longer need to use this handle, it must call meshlink_close() to
279  *  free its resources.
280  *
281  *  This function does not start any network I/O yet. The application should
282  *  first set callbacks, and then call meshlink_start().
283  *
284  *  @param name     The name which this instance of the application will use in the mesh.
285  *                  After the function returns, the application is free to overwrite or free @a name.
286  *  @param appname  The application name which will be used in the mesh.
287  *                  After the function returns, the application is free to overwrite or free @a name.
288  *  @param devclass The device class which will be used in the mesh.
289  *
290  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
291  *                  The pointer is valid until meshlink_close() is called.
292  */
293 extern struct meshlink_handle *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
294
295 /// Create Sub-Mesh.
296 /** This function causes MeshLink to open a new Sub-Mesh network
297  *  create a new thread, which will handle all network I/O.
298  *
299  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
300  *
301  *  \memberof meshlink_handle
302  *  @param mesh     A handle which represents an instance of MeshLink.
303  *
304  *  @param submesh  Name of the new Sub-Mesh to create.
305  *
306  *  @return         A pointer to a struct meshlink_submesh which represents this instance of SubMesh, or NULL in case of an error.
307  *                  The pointer is valid until meshlink_close() is called.
308  */
309 struct meshlink_submesh *meshlink_submesh_open(struct meshlink_handle *mesh, const char *submesh) __attribute__((__warn_unused_result__));
310
311 /// Start MeshLink.
312 /** This function causes MeshLink to open network sockets, make outgoing connections, and
313  *  create a new thread, which will handle all network I/O.
314  *
315  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
316  *
317  *  \memberof meshlink_handle
318  *  @param mesh     A handle which represents an instance of MeshLink.
319  *
320  *  @return         This function will return true if MeshLink has successfully started, false otherwise.
321  */
322 extern bool meshlink_start(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
323
324 /// Stop MeshLink.
325 /** This function causes MeshLink to disconnect from all other nodes,
326  *  close all sockets, and shut down its own thread.
327  *
328  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
329  *  Channels that are still open will remain valid, but any communication via channels will stop as well.
330  *
331  *  \memberof meshlink_handle
332  *  @param mesh     A handle which represents an instance of MeshLink.
333  */
334 extern void meshlink_stop(struct meshlink_handle *mesh);
335
336 /// Close the MeshLink handle.
337 /** This function calls meshlink_stop() if necessary,
338  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
339  *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
340  *
341  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
342  *  If called at a proper time with a valid handle, this function always succeeds.
343  *  If called within a callback or with an invalid handle, the result is undefined.
344  *
345  * \memberof meshlink_handle
346  *  @param mesh     A handle which represents an instance of MeshLink.
347  */
348 extern void meshlink_close(struct meshlink_handle *mesh);
349
350 /// Destroy a MeshLink instance.
351 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
352  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
353  *  new instance.
354  *
355  *  @param confbase The directory in which MeshLink stores its configuration files.
356  *                  After the function returns, the application is free to overwrite or free @a confbase.
357  *
358  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
359  */
360 extern bool meshlink_destroy(const char *confbase) __attribute__((__warn_unused_result__));
361
362 /// A callback for receiving data from the mesh.
363 /** @param mesh      A handle which represents an instance of MeshLink.
364  *  @param source    A pointer to a struct meshlink_node describing the source of the data.
365  *  @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).
366  *                   The pointer is only valid during the lifetime of the callback.
367  *                   The callback should mempcy() the data if it needs to be available outside the callback.
368  *  @param len       The length of the received data, or 0 in case there is no data.
369  */
370 typedef void (*meshlink_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len);
371
372 /// Set the receive callback.
373 /** This functions sets the callback that is called whenever another node sends data to the local node.
374  *  The callback is run in MeshLink's own thread.
375  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
376  *  to hand the data over to the application's thread.
377  *  The callback should also not block itself and return as quickly as possible.
378  *
379  *  \memberof meshlink_handle
380  *  @param mesh      A handle which represents an instance of MeshLink.
381  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
382  *                   If a NULL pointer is given, the callback will be disabled.
383  */
384 extern void meshlink_set_receive_cb(struct meshlink_handle *mesh, meshlink_receive_cb_t cb);
385
386 /// A callback reporting the meta-connection attempt made by the host node to an another node.
387 /** @param mesh      A handle which represents an instance of MeshLink.
388  *  @param node      A pointer to a struct meshlink_node describing the node to whom meta-connection is being tried.
389  *                   This pointer is valid until meshlink_close() is called.
390  */
391 typedef void (*meshlink_connection_try_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
392
393 /// Set the meta-connection try callback.
394 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
395  *  The callback is run in MeshLink's own thread.
396  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
397  *  to hand the data over to the application's thread.
398  *  The callback should also not block itself and return as quickly as possible.
399  *
400  *  \memberof meshlink_handle
401  *  @param mesh      A handle which represents an instance of MeshLink.
402  *  @param cb        A pointer to the function which will be called when host node attempts to make
403  *                   the connection to another node. If a NULL pointer is given, the callback will be disabled.
404  */
405 extern void meshlink_set_connection_try_cb(struct meshlink_handle *mesh, meshlink_connection_try_cb_t cb);
406
407 /// A callback reporting node status changes.
408 /** @param mesh      A handle which represents an instance of MeshLink.
409  *  @param node       A pointer to a struct meshlink_node describing the node whose status changed.
410  *                    This pointer is valid until meshlink_close() is called.
411  *  @param reachable  True if the node is reachable, false otherwise.
412  */
413 typedef void (*meshlink_node_status_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, bool reachable);
414
415 /// Set the node status callback.
416 /** This functions sets the callback that is called whenever another node's status changed.
417  *  The callback is run in MeshLink's own thread.
418  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
419  *  to hand the data over to the application's thread.
420  *  The callback should also not block itself and return as quickly as possible.
421  *
422  *  \memberof meshlink_handle
423  *  @param mesh      A handle which represents an instance of MeshLink.
424  *  @param cb        A pointer to the function which will be called when another node's status changes.
425  *                   If a NULL pointer is given, the callback will be disabled.
426  */
427 extern void meshlink_set_node_status_cb(struct meshlink_handle *mesh, meshlink_node_status_cb_t cb);
428
429 /// A callback reporting node path MTU changes.
430 /** @param mesh      A handle which represents an instance of MeshLink.
431  *  @param node       A pointer to a struct meshlink_node describing the node whose status changed.
432  *                    This pointer is valid until meshlink_close() is called.
433  *  @param pmtu       The current path MTU to the node, or 0 if UDP communication is not (yet) possible.
434  */
435 typedef void (*meshlink_node_pmtu_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t pmtu);
436
437 /// Set the node extended status callback.
438 /** This functions sets the callback that is called whenever certain connectivity parameters for a node change.
439  *  The callback is run in MeshLink's own thread.
440  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
441  *  to hand the data over to the application's thread.
442  *  The callback should also not block itself and return as quickly as possible.
443  *
444  *  \memberof meshlink_handle
445  *  @param mesh      A handle which represents an instance of MeshLink.
446  *  @param cb        A pointer to the function which will be called when another node's extended status changes.
447  *                   If a NULL pointer is given, the callback will be disabled.
448  */
449 extern void meshlink_set_node_pmtu_cb(struct meshlink_handle *mesh, meshlink_node_pmtu_cb_t cb);
450
451 /// A callback reporting duplicate node detection.
452 /** @param mesh       A handle which represents an instance of MeshLink.
453  *  @param node       A pointer to a struct meshlink_node describing the node which is duplicate.
454  *                    This pointer is valid until meshlink_close() is called.
455  */
456 typedef void (*meshlink_node_duplicate_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
457
458 /// Set the node duplicate callback.
459 /** This functions sets the callback that is called whenever a duplicate node is detected.
460  *  The callback is run in MeshLink's own thread.
461  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
462  *  to hand the data over to the application's thread.
463  *  The callback should also not block itself and return as quickly as possible.
464  *
465  *  \memberof meshlink_handle
466  *  @param mesh      A handle which represents an instance of MeshLink.
467  *  @param cb        A pointer to the function which will be called when a duplicate node is detected.
468  *                   If a NULL pointer is given, the callback will be disabled.
469  */
470 extern void meshlink_set_node_duplicate_cb(struct meshlink_handle *mesh, meshlink_node_duplicate_cb_t cb);
471
472 /// Severity of log messages generated by MeshLink.
473 typedef enum {
474         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
475         MESHLINK_INFO,     ///< Informational messages.
476         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
477         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
478         MESHLINK_CRITICAL  ///< Critical errors which cause MeshLink to fail completely.
479 } meshlink_log_level_t;
480
481 /// A callback for receiving log messages generated by MeshLink.
482 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
483  *  @param level     An enum describing the severity level of the message.
484  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
485  *                   This pointer is only valid for the duration of the callback.
486  *                   The application must not free() this pointer.
487  *                   The application should strdup() the text if it has to be available outside the callback.
488  */
489 typedef void (*meshlink_log_cb_t)(struct meshlink_handle *mesh, meshlink_log_level_t level, const char *text);
490
491 /// Set the log callback.
492 /** This functions sets the callback that is called whenever MeshLink has some information to log.
493  *
494  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
495  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
496  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
497  *
498  *  If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
499  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
500  *  to hand the data over to the application's thread.
501  *  The callback should also not block itself and return as quickly as possible.
502  *
503  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
504  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
505  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
506  *
507  *  \memberof meshlink_handle
508  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
509  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
510  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
511  *                   If a NULL pointer is given, the callback will be disabled.
512  */
513 extern void meshlink_set_log_cb(struct meshlink_handle *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
514
515 /// A callback for receiving error conditions encountered by the MeshLink thread.
516 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
517  *  @param errno     The error code describing what kind of error occurred.
518  */
519 typedef void (*meshlink_error_cb_t)(struct meshlink_handle *mesh, meshlink_errno_t meshlink_errno);
520
521 /// Set the error callback.
522 /** This functions sets the callback that is called whenever the MeshLink thread encounters a serious error.
523  *
524  *  While most API functions report an error directly to the caller in case something went wrong,
525  *  MeshLink also runs a background thread which can encounter error conditions.
526  *  Most of them will be dealt with automatically, however there can be errors that will prevent MeshLink from
527  *  working correctly. When the callback is called, it means that MeshLink is no longer functioning
528  *  as expected. The application should then present an error message and shut down, or perform any other
529  *  action it deems appropriate.
530  *
531  *  The callback is run in MeshLink's own thread.
532  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
533  *  to hand the data over to the application's thread.
534  *  The callback should also not block itself and return as quickly as possible.
535  *
536  *  Even though the callback signals a serious error inside MeshLink, all open handles are still valid,
537  *  and the application should close handles in exactly the same it would have to do if the callback
538  *  was not called. This must not be done inside the callback itself.
539  *
540  *  \memberof meshlink_handle
541  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
542  *  @param cb        A pointer to the function which will be called when a serious error is encountered.
543  *                   If a NULL pointer is given, the callback will be disabled.
544  */
545 extern void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb);
546
547 /// Send data to another node.
548 /** This functions sends one packet of data to another node in the mesh.
549  *  The packet is sent using UDP semantics, which means that
550  *  the packet is sent as one unit and is received as one unit,
551  *  and that there is no guarantee that the packet will arrive at the destination.
552  *  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.
553  *  The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
554  *  The application should take care of getting an acknowledgement and retransmission if necessary.
555  *
556  *  \memberof meshlink_node
557  *  @param mesh         A handle which represents an instance of MeshLink.
558  *  @param destination  A pointer to a struct meshlink_node describing the destination for the data.
559  *  @param data         A pointer to a buffer containing the data to be sent to the source.
560  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
561  *                      It is valid to specify a NULL pointer, but only if @a len is also 0.
562  *  @param len          The length of the data.
563  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
564  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
565  */
566 extern bool meshlink_send(struct meshlink_handle *mesh, struct meshlink_node *destination, const void *data, size_t len) __attribute__((__warn_unused_result__));
567
568 /// Query the maximum packet size that can be sent to a node.
569 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
570  *  The path MTU is a property of the path packets will take to the destination node over the Internet.
571  *  It can be different for different destination nodes.
572  *  and the path MTU can change at any point in time due to changes in the Internet.
573  *  Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
574  *
575  *  \memberof meshlink_node
576  *  @param mesh         A handle which represents an instance of MeshLink.
577  *  @param destination  A pointer to a struct meshlink_node describing the destination for the data.
578  *
579  *  @return             The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
580  *                      or a negative value in case of an error.
581  */
582 extern ssize_t meshlink_get_pmtu(struct meshlink_handle *mesh, struct meshlink_node *destination) __attribute__((__warn_unused_result__));
583
584 /// Get a handle for our own node.
585 /** This function returns a handle for the local node.
586  *
587  *  \memberof meshlink_handle
588  *  @param mesh         A handle which represents an instance of MeshLink.
589  *
590  *  @return             A pointer to a struct meshlink_node which represents the local node.
591  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
592  */
593 extern struct meshlink_node *meshlink_get_self(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
594
595 /// Get a handle for a specific node.
596 /** This function returns a handle for the node with the given name.
597  *
598  *  \memberof meshlink_handle
599  *  @param mesh         A handle which represents an instance of MeshLink.
600  *  @param name         The name of the node for which a handle is requested.
601  *                      After this function returns, the application is free to overwrite or free @a name.
602  *
603  *  @return             A pointer to a struct meshlink_node which represents the requested node,
604  *                      or NULL if the requested node does not exist.
605  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
606  */
607 extern struct meshlink_node *meshlink_get_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
608
609 /// Get a handle for a specific submesh.
610 /** This function returns a handle for the submesh with the given name.
611  *
612  *  \memberof meshlink_handle
613  *  @param mesh         A handle which represents an instance of MeshLink.
614  *  @param name         The name of the submesh for which a handle is requested.
615  *                      After this function returns, the application is free to overwrite or free @a name.
616  *
617  *  @return             A pointer to a struct meshlink_submesh which represents the requested submesh,
618  *                      or NULL if the requested submesh does not exist.
619  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
620  */
621 extern struct meshlink_submesh *meshlink_get_submesh(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
622
623 /// Get the fingerprint of a node's public key.
624 /** This function returns a fingerprint of the node's public key.
625  *  It should be treated as an opaque blob.
626  *
627  *  \memberof meshlink_node
628  *  @param mesh         A handle which represents an instance of MeshLink.
629  *  @param node         A pointer to a struct meshlink_node describing the node.
630  *
631  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
632  *                      The application should call free() after it is done using this string.
633  */
634 extern char *meshlink_get_fingerprint(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
635
636 /// Get a list of all nodes.
637 /** This function returns a list with handles for all known nodes.
638  *
639  *  \memberof meshlink_handle
640  *  @param mesh         A handle which represents an instance of MeshLink.
641  *  @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.
642  *                      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).
643  *                      The application is allowed to call free() on the array whenever it wishes.
644  *                      The pointers in the array are valid until meshlink_close() is called.
645  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
646  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
647  *                      The contents of this variable will be changed to reflect the new size of the array.
648  *
649  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
650  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
651  *                      If it is a new value, the old value of @a nodes should not be used anymore.
652  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
653  */
654 extern struct meshlink_node **meshlink_get_all_nodes(struct meshlink_handle *mesh, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
655
656 /// Sign data using the local node's MeshLink key.
657 /** This function signs data using the local node's MeshLink key.
658  *  The generated signature can be securely verified by other nodes.
659  *
660  *  \memberof meshlink_handle
661  *  @param mesh         A handle which represents an instance of MeshLink.
662  *  @param data         A pointer to a buffer containing the data to be signed.
663  *  @param len          The length of the data to be signed.
664  *  @param signature    A pointer to a buffer where the signature will be stored.
665  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
666  *                      The signature is a binary blob, and is not nul-terminated.
667  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
668  *
669  *  @return             This function returns true if the signature was correctly generated, false otherwise.
670  */
671 extern bool meshlink_sign(struct meshlink_handle *mesh, const void *data, size_t len, void *signature, size_t *siglen) __attribute__((__warn_unused_result__));
672
673 /// Get the list of all nodes by device class.
674 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
675  *
676  *  \memberof meshlink_handle
677  *  @param mesh         A handle which represents an instance of MeshLink.
678  *  @param devclass     Device class of the nodes for which the list has to be obtained.
679  *  @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.
680  *                      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).
681  *                      The application is allowed to call free() on the array whenever it wishes.
682  *                      The pointers in the array are valid until meshlink_close() is called.
683  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
684  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
685  *                      The contents of this variable will be changed to reflect the new size of the array.
686  *
687  *  @return             A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
688  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
689  *                      If it is a new value, the old value of @a nodes should not be used anymore.
690  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
691  */
692 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) __attribute__((__warn_unused_result__));
693
694 /// Get the list of all nodes by Submesh.
695 /** This function returns a list with handles for all the nodes that matches with the given @a Submesh.
696  *
697  *  \memberof meshlink_submesh
698  *  @param mesh         A handle which represents an instance of MeshLink.
699  *  @param submesh      Submesh handle of the nodes for which the list has to be obtained.
700  *  @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.
701  *                      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).
702  *                      The application is allowed to call free() on the array whenever it wishes.
703  *                      The pointers in the array are valid until meshlink_close() is called.
704  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
705  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
706  *                      The contents of this variable will be changed to reflect the new size of the array.
707  *
708  *  @return             A pointer to an array containing pointers to all known nodes of the given Submesh, or NULL in case of an error.
709  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
710  *                      If it is a new value, the old value of @a nodes should not be used anymore.
711  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
712  */
713 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) __attribute__((__warn_unused_result__));
714
715 /// Get the list of all nodes by time they were last reachable.
716 /** This function returns a list with handles for all the nodes whose last known reachability time overlaps with the given time range.
717  *  If the range includes 0, it will count nodes that were never online.
718  *  If start is bigger than end, the result will be inverted.
719  *
720  *  \memberof meshlink_handle
721  *  @param mesh         A handle which represents an instance of MeshLink.
722  *  @param start        Start time.
723  *  @param end          End time.
724  *  @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.
725  *                      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).
726  *                      The application is allowed to call free() on the array whenever it wishes.
727  *                      The pointers in the array are valid until meshlink_close() is called.
728  *  @param nmemb        A pointer to a variable holding the number of nodes that were reachable within the period given by @a start and @a end.
729  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
730  *                      The contents of this variable will be changed to reflect the new size of the array.
731  *
732  *  @return             A pointer to an array containing pointers to all known nodes that were reachable within the period given by @a start and @a end.
733  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
734  *                      If it is a new value, the old value of @a nodes should not be used anymore.
735  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
736  */
737 extern struct meshlink_node **meshlink_get_all_nodes_by_last_reachable(struct meshlink_handle *mesh, time_t start, time_t end, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
738
739 /// Get the node's device class.
740 /** This function returns the device class of the given node.
741  *
742  *  \memberof meshlink_node
743  *  @param mesh          A handle which represents an instance of MeshLink.
744  *  @param node         A pointer to a struct meshlink_node describing the node.
745  *
746  *  @return              This function returns the device class of the @a node, or -1 in case of an error.
747  */
748 extern dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
749
750 /// Get the node's submesh handle.
751 /** This function returns the submesh handle of the given node.
752  *
753  *  \memberof meshlink_node
754  *  @param mesh          A handle which represents an instance of MeshLink.
755  *  @param node          A pointer to a struct meshlink_node describing the node.
756  *
757  *  @return              This function returns the submesh handle of the @a node, or NULL in case of an error.
758  */
759 extern struct meshlink_submesh *meshlink_get_node_submesh(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
760
761 /// Get a node's reachability status.
762 /** This function returns the current reachability of a given node, and the times of the last state changes.
763  *  If a given state change never happened, the time returned will be 0.
764  *
765  *  \memberof meshlink_node
766  *  @param mesh              A handle which represents an instance of MeshLink.
767  *  @param node              A pointer to a struct meshlink_node describing the node.
768  *  @param last_reachable    A pointer to a time_t variable that will be filled in with the last time the node became reachable.
769  *                           Pass NULL to not have anything written.
770  *  @param last_unreachable  A pointer to a time_t variable that will be filled in with the last time the node became unreachable.
771  *                           Pass NULL to not have anything written.
772  *
773  *  @return                  This function returns true if the node is currently reachable, false otherwise.
774  */
775 extern bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlink_node *node, time_t *last_reachable, time_t *last_unreachable);
776
777 /// Verify the signature generated by another node of a piece of data.
778 /** This function verifies the signature that another node generated for a piece of data.
779  *
780  *  \memberof meshlink_node
781  *  @param mesh         A handle which represents an instance of MeshLink.
782  *  @param source       A pointer to a struct meshlink_node describing the source of the signature.
783  *  @param data         A pointer to a buffer containing the data to be verified.
784  *  @param len          The length of the data to be verified.
785  *  @param signature    A pointer to a buffer where the signature is stored.
786  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
787  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
788  *
789  *  @return             This function returns true if the signature is valid, false otherwise.
790  */
791 extern bool meshlink_verify(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len, const void *signature, size_t siglen) __attribute__((__warn_unused_result__));
792
793 /// Set the canonical Address for a node.
794 /** This function sets the canonical Address for a node.
795  *  This address is stored permanently until it is changed by another call to this function,
796  *  unlike other addresses associated with a node,
797  *  such as those added with meshlink_hint_address() or addresses discovered at runtime.
798  *
799  *  If a canonical Address is set for the local node,
800  *  it will be used for the hostname part of generated invitation URLs.
801  *
802  *  \memberof meshlink_node
803  *  @param mesh         A handle which represents an instance of MeshLink.
804  *  @param node         A pointer to a struct meshlink_node describing the node.
805  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
806  *  @param port         A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
807  *                      If it is NULL, the listening port's number will be used.
808  *
809  *  @return             This function returns true if the address was added, false otherwise.
810  */
811 extern bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port) __attribute__((__warn_unused_result__));
812
813 /// Add an Address for the local node.
814 /** This function adds an Address for the local node, which will be used for invitation URLs.
815  *
816  *  \memberof meshlink_handle
817  *  @param mesh         A handle which represents an instance of MeshLink.
818  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
819  *
820  *  @return             This function returns true if the address was added, false otherwise.
821  */
822 extern bool meshlink_add_address(struct meshlink_handle *mesh, const char *address) __attribute__((__warn_unused_result__));
823
824 /// Try to discover the external address for the local node.
825 /** This function performs tries to discover the local node's external address
826  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
827  *  the FQDN associated with the address will be returned.
828  *
829  *  Please note that this is function only returns a single address,
830  *  even if the local node might have more than one external address.
831  *  In that case, there is no control over which address will be selected.
832  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
833  *  there is no guarantee that the external address will be valid for an extended period of time.
834  *
835  *  This function is blocking. It can take several seconds before it returns.
836  *  There is no guarantee it will be able to resolve the external address.
837  *  Failures might be because by temporary network outages.
838  *
839  *  \memberof meshlink_handle
840  *  @param mesh         A handle which represents an instance of MeshLink.
841  *
842  *  @return             This function returns a pointer to a C string containing the discovered external address,
843  *                      or NULL if there was an error looking up the address.
844  *                      After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
845  */
846 extern char *meshlink_get_external_address(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
847
848 /// Try to discover the external address for the local node.
849 /** This function performs tries to discover the local node's external address
850  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
851  *  the FQDN associated with the address will be returned.
852  *
853  *  Please note that this is function only returns a single address,
854  *  even if the local node might have more than one external address.
855  *  In that case, there is no control over which address will be selected.
856  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
857  *  there is no guarantee that the external address will be valid for an extended period of time.
858  *
859  *  This function is blocking. It can take several seconds before it returns.
860  *  There is no guarantee it will be able to resolve the external address.
861  *  Failures might be because by temporary network outages.
862  *
863  *  \memberof meshlink_handle
864  *  @param mesh            A handle which represents an instance of MeshLink.
865  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
866  *                         this might return the external address for any working address family.
867  *
868  *  @return                This function returns a pointer to a C string containing the discovered external address,
869  *                         or NULL if there was an error looking up the address.
870  *                         After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
871  */
872 extern char *meshlink_get_external_address_for_family(struct meshlink_handle *mesh, int address_family) __attribute__((__warn_unused_result__));
873
874 /// Try to discover the local address for the local node.
875 /** This function performs tries to discover the address of the local interface used for outgoing connection.
876  *
877  *  Please note that this is function only returns a single address,
878  *  even if the interface might have more than one address.
879  *  In that case, there is no control over which address will be selected.
880  *  Also note that if you have a dynamic IP address,
881  *  there is no guarantee that the local address will be valid for an extended period of time.
882  *
883  *  This function will fail if it couldn't find a local address for the given address family.
884  *  If hostname resolving is requested, this function may block for a few seconds.
885  *
886  *  \memberof meshlink_handle
887  *  @param mesh            A handle which represents an instance of MeshLink.
888  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
889  *                         this might return the local address for any working address family.
890  *
891  *  @return                This function returns a pointer to a C string containing the discovered local address,
892  *                         or NULL if there was an error looking up the address.
893  *                         After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
894  */
895 extern char *meshlink_get_local_address_for_family(struct meshlink_handle *mesh, int address_family) __attribute__((__warn_unused_result__));
896
897 /// Try to discover the external address for the local node, and add it to its list of addresses.
898 /** This function is equivalent to:
899  *
900  *    meshlink_add_address(mesh, meshlink_get_external_address(mesh));
901  *
902  *  Read the description of meshlink_get_external_address() for the limitations of this function.
903  *
904  *  \memberof meshlink_handle
905  *  @param mesh         A handle which represents an instance of MeshLink.
906  *
907  *  @return             This function returns true if the address was added, false otherwise.
908  */
909 extern bool meshlink_add_external_address(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
910
911 /// Get the network port used by the local node.
912 /** This function returns the network port that the local node is listening on.
913  *
914  *  \memberof meshlink_handle
915  *  @param mesh          A handle which represents an instance of MeshLink.
916  *
917  *  @return              This function returns the port number, or -1 in case of an error.
918  */
919 extern int meshlink_get_port(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
920
921 /// Set the network port used by the local node.
922 /** This function sets the network port that the local node is listening on.
923  *  It may only be called when the mesh is not running.
924  *  If unsure, call meshlink_stop() before calling this function.
925  *  Also note that if your node is already part of a mesh with other nodes,
926  *  that the other nodes may no longer be able to initiate connections to the local node,
927  *  since they will try to connect to the previously configured port.
928  *
929  *  \memberof meshlink_handle
930  *  @param mesh          A handle which represents an instance of MeshLink.
931  *  @param port          The port number to listen on. This must be between 0 and 65535.
932  *                       If the port is set to 0, then MeshLink will listen on a port
933  *                       that is randomly assigned by the operating system every time meshlink_open() is called.
934  *
935  *  @return              This function returns true if the port was successfully changed
936  *                       to the desired port, false otherwise. If it returns false, there
937  *                       is no guarantee that MeshLink is listening on the old port.
938  */
939
940 extern bool meshlink_set_port(struct meshlink_handle *mesh, int port) __attribute__((__warn_unused_result__));
941
942 /// Set the timeout for invitations.
943 /** This function sets the timeout for invitations.
944  *  Note that timeouts are only checked at the time a node tries to join using an invitation.
945  *  The default timeout for invitations is 1 week.
946  *
947  *  \memberof meshlink_handle
948  *  @param mesh         A handle which represents an instance of MeshLink.
949  *  @param timeout      The timeout for invitations in seconds.
950  */
951 extern void meshlink_set_invitation_timeout(struct meshlink_handle *mesh, int timeout);
952
953 /// Invite another node into the mesh.
954 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
955  *  The generated invitation is a string containing a URL.
956  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
957  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
958  *
959  *  \memberof meshlink_handle
960  *  @param mesh         A handle which represents an instance of MeshLink.
961  *  @param submesh      A handle which represents an instance of SubMesh.
962  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
963  *                      After this function returns, the application is free to overwrite or free @a name.
964  *  @param flags        A bitwise-or'd combination of flags that controls how the URL is generated.
965  *
966  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
967  *                      The application should call free() after it has finished using the URL.
968  */
969 extern char *meshlink_invite_ex(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name, uint32_t flags) __attribute__((__warn_unused_result__));
970
971 /// Invite another node into the mesh.
972 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
973  *  The generated invitation is a string containing a URL.
974  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
975  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
976  *
977  *  Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
978  *
979  *  \memberof meshlink_handle
980  *  @param mesh         A handle which represents an instance of MeshLink.
981  *  @param submesh      A handle which represents an instance of SubMesh.
982  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
983  *                      After this function returns, the application is free to overwrite or free @a name.
984  *
985  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
986  *                      The application should call free() after it has finished using the URL.
987  */
988 extern char *meshlink_invite(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name) __attribute__((__warn_unused_result__));
989
990 /// Use an invitation to join a mesh.
991 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
992  *  An invitation can only be used if the local node has never connected to other nodes before.
993  *  After a successfully accepted invitation, the name of the local node may have changed.
994  *
995  *  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.
996  *
997  *  This function is blocking. It can take several seconds before it returns.
998  *  There is no guarantee it will perform a successful join.
999  *  Failures might be caused by temporary network outages, or by the invitation having expired.
1000  *
1001  *  \memberof meshlink_handle
1002  *  @param mesh         A handle which represents an instance of MeshLink.
1003  *  @param invitation   A nul-terminated C string containing the invitation URL.
1004  *                      After this function returns, the application is free to overwrite or free @a invitation.
1005  *
1006  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
1007  */
1008 extern bool meshlink_join(struct meshlink_handle *mesh, const char *invitation) __attribute__((__warn_unused_result__));
1009
1010 /// Export the local node's key and addresses.
1011 /** This function generates a string that contains the local node's public key and one or more IP addresses.
1012  *  The application can pass it in some way to another node, which can then import it,
1013  *  granting the local node access to the other node's mesh.
1014  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
1015  *
1016  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
1017  *
1018  *  \memberof meshlink_handle
1019  *  @param mesh         A handle which represents an instance of MeshLink.
1020  *
1021  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
1022  *                      The application should call free() after it has finished using this string.
1023  */
1024 extern char *meshlink_export(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
1025
1026 /// Import another node's key and addresses.
1027 /** This function accepts a string containing the exported public key and addresses of another node.
1028  *  By importing this data, the local node grants the other node access to its mesh.
1029  *  The application should make sure that the data it imports is really coming from the node it wants to import,
1030  *
1031  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
1032  *
1033  *  \memberof meshlink_handle
1034  *  @param mesh         A handle which represents an instance of MeshLink.
1035  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
1036  *                      After this function returns, the application is free to overwrite or free @a data.
1037  *
1038  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
1039  */
1040 extern bool meshlink_import(struct meshlink_handle *mesh, const char *data) __attribute__((__warn_unused_result__));
1041
1042 /// Forget any information about a node.
1043 /** This function allows the local node to forget any information it has about a node,
1044  *  and if possible will remove any data it has stored on disk about the node.
1045  *
1046  *  Any open channels to this node must be closed before calling this function.
1047  *
1048  *  After this call returns, the node handle is invalid and may no longer be used, regardless
1049  *  of the return value of this call.
1050  *
1051  *  Note that this function does not prevent MeshLink from actually forgetting about a node,
1052  *  or re-learning information about a node at a later point in time. It is merely a hint that
1053  *  the application does not care about this node anymore and that any resources kept could be
1054  *  cleaned up.
1055  *
1056  *  \memberof meshlink_node
1057  *  @param mesh         A handle which represents an instance of MeshLink.
1058  *  @param node         A pointer to a struct meshlink_node describing the node to be forgotten.
1059  *
1060  *  @return             This function returns true if all currently known data about the node has been forgotten, false otherwise.
1061  */
1062 extern bool meshlink_forget_node(struct meshlink_handle *mesh, struct meshlink_node *node);
1063
1064 /// Blacklist a node from the mesh.
1065 /** This function causes the local node to blacklist another node.
1066  *  The local node will drop any existing connections to that node,
1067  *  and will not send data to it nor accept any data received from it any more.
1068  *
1069  *  \memberof meshlink_node
1070  *  @param mesh         A handle which represents an instance of MeshLink.
1071  *  @param node         A pointer to a struct meshlink_node describing the node to be blacklisted.
1072  *
1073  *  @return             This function returns true if the node has been blacklisted, false otherwise.
1074  */
1075 extern bool meshlink_blacklist(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
1076
1077 /// Blacklist a node from the mesh by name.
1078 /** This function causes the local node to blacklist another node by name.
1079  *  The local node will drop any existing connections to that node,
1080  *  and will not send data to it nor accept any data received from it any more.
1081  *
1082  *  If no node by the given name is known, it is created.
1083  *
1084  *  \memberof meshlink_node
1085  *  @param mesh         A handle which represents an instance of MeshLink.
1086  *  @param name         The name of the node to blacklist.
1087  *
1088  *  @return             This function returns true if the node has been blacklisted, false otherwise.
1089  */
1090 extern bool meshlink_blacklist_by_name(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
1091
1092 /// Whitelist a node on the mesh.
1093 /** This function causes the local node to whitelist a previously blacklisted node.
1094  *  The local node will allow connections to and from that node,
1095  *  and will send data to it and accept any data received from it.
1096  *
1097  *  \memberof meshlink_node
1098  *  @param mesh         A handle which represents an instance of MeshLink.
1099  *  @param node         A pointer to a struct meshlink_node describing the node to be whitelisted.
1100  *
1101  *  @return             This function returns true if the node has been whitelisted, false otherwise.
1102  */
1103 extern bool meshlink_whitelist(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
1104
1105 /// Whitelist a node on the mesh by name.
1106 /** This function causes the local node to whitelist a node by name.
1107  *  The local node will allow connections to and from that node,
1108  *  and will send data to it and accept any data received from it.
1109  *
1110  *  If no node by the given name is known, it is created.
1111  *  This is useful if new nodes are blacklisted by default.
1112  *
1113  *  \memberof meshlink_node
1114  *  @param mesh         A handle which represents an instance of MeshLink.
1115  *  @param node         A pointer to a struct meshlink_node describing the node to be whitelisted.
1116  *
1117  *  @return             This function returns true if the node has been whitelisted, false otherwise.
1118  */
1119 extern bool meshlink_whitelist_by_name(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
1120
1121 /// Set whether new nodes are blacklisted by default.
1122 /** This function sets the blacklist behaviour for newly discovered nodes.
1123  *  If set to true, new nodes will be automatically blacklisted.
1124  *  If set to false, which is the default, new nodes are automatically whitelisted.
1125  *  The whitelist/blacklist status of a node may be changed afterwards with the
1126  *  meshlink_whitelist() and meshlink_blacklist() functions.
1127  *
1128  *  \memberof meshlink_handle
1129  *  @param mesh         A handle which represents an instance of MeshLink.
1130  *  @param blacklist    True if new nodes are to be blacklisted, false if whitelisted.
1131  */
1132 extern void meshlink_set_default_blacklist(struct meshlink_handle *mesh, bool blacklist);
1133
1134 /// A callback for accepting incoming channels.
1135 /** This function is called whenever a remote node wants to open a channel to the local node.
1136  *  The application then has to decide whether to accept or reject this channel.
1137  *
1138  *  The callback is run in MeshLink's own thread.
1139  *  It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
1140  *  to hand any data over to the application's thread.
1141  *
1142  *  @param mesh         A handle which represents an instance of MeshLink.
1143  *  @param channel      A handle for the incoming channel.
1144  *                      If the application accepts the incoming channel by returning true,
1145  *                      then this handle is valid until meshlink_channel_close() is called on it.
1146  *                      If the application rejects the incoming channel by returning false,
1147  *                      then this handle is invalid after the callback returns
1148  *                      (the callback does not need to call meshlink_channel_close() itself in this case).
1149  *  @param port         The port number the peer wishes to connect to.
1150  *  @param data         A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
1151  *                      The pointer is only valid during the lifetime of the callback.
1152  *                      The callback should mempcy() the data if it needs to be available outside the callback.
1153  *  @param len          The length of the data, or 0 in case no data has been received yet. (Not yet used.)
1154  *
1155  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
1156  *                      If returning false, the channel is invalid and may not be used anymore.
1157  */
1158 typedef bool (*meshlink_channel_accept_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, uint16_t port, const void *data, size_t len);
1159
1160 /// A callback for receiving data from a channel.
1161 /** This function is called whenever data is received from a remote node on a channel.
1162  *
1163  *  This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
1164  *  In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
1165  *  In any case, the @a channel handle will still be valid until the application calls meshlink_close().
1166  *
1167  *  @param mesh         A handle which represents an instance of MeshLink.
1168  *  @param channel      A handle for the channel.
1169  *  @param data         A pointer to a buffer containing data sent by the source, or NULL in case of an error.
1170  *                      The pointer is only valid during the lifetime of the callback.
1171  *                      The callback should mempcy() the data if it needs to be available outside the callback.
1172  *  @param len          The length of the data, or 0 in case of an error.
1173  */
1174 typedef void (*meshlink_channel_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len);
1175
1176 /// A callback informing the application when data can be sent on a channel.
1177 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
1178  *
1179  *  @param mesh         A handle which represents an instance of MeshLink.
1180  *  @param channel      A handle for the channel.
1181  *  @param len          The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
1182  *                      or 0 in case of an error.
1183  */
1184 typedef void (*meshlink_channel_poll_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t len);
1185
1186 /// Set the accept callback.
1187 /** This functions sets the callback that is called whenever another node sends data to the local node.
1188  *  The callback is run in MeshLink's own thread.
1189  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1190  *  to hand the data over to the application's thread.
1191  *  The callback should also not block itself and return as quickly as possible.
1192  *
1193  *  If no accept callback is set, incoming channels are rejected.
1194  *
1195  *  \memberof meshlink_handle
1196  *  @param mesh      A handle which represents an instance of MeshLink.
1197  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1198  *                   If a NULL pointer is given, the callback will be disabled.
1199  */
1200 extern void meshlink_set_channel_accept_cb(struct meshlink_handle *mesh, meshlink_channel_accept_cb_t cb);
1201
1202 /// Set the receive callback.
1203 /** This functions sets the callback that is called whenever another node sends data to the local node.
1204  *  The callback is run in MeshLink's own thread.
1205  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1206  *  to hand the data over to the application's thread.
1207  *  The callback should also not block itself and return as quickly as possible.
1208  *
1209  *  \memberof meshlink_channel
1210  *  @param mesh      A handle which represents an instance of MeshLink.
1211  *  @param channel   A handle for the channel.
1212  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
1213  *                   If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
1214  */
1215 extern void meshlink_set_channel_receive_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_receive_cb_t cb);
1216
1217 /// Set the poll callback.
1218 /** This functions sets the callback that is called whenever data can be sent to another node.
1219  *  The callback is run in MeshLink's own thread.
1220  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1221  *  to pass data to or from the application's thread.
1222  *  The callback should also not block itself and return as quickly as possible.
1223  *
1224  *  \memberof meshlink_channel
1225  *  @param mesh      A handle which represents an instance of MeshLink.
1226  *  @param channel   A handle for the channel.
1227  *  @param cb        A pointer to the function which will be called when data can be sent to another node.
1228  *                   If a NULL pointer is given, the callback will be disabled.
1229  */
1230 extern void meshlink_set_channel_poll_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_poll_cb_t cb);
1231
1232 /// Set the send buffer size of a channel.
1233 /** This function sets the desired size of the send buffer.
1234  *  The default size is 128 kB.
1235  *
1236  *  \memberof meshlink_channel
1237  *  @param mesh      A handle which represents an instance of MeshLink.
1238  *  @param channel   A handle for the channel.
1239  *  @param size      The desired size for the send buffer.
1240  *                   If a NULL pointer is given, the callback will be disabled.
1241  */
1242 extern void meshlink_set_channel_sndbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1243
1244 /// Set the receive buffer size of a channel.
1245 /** This function sets the desired size of the receive buffer.
1246  *  The default size is 128 kB.
1247  *
1248  *  \memberof meshlink_channel
1249  *  @param mesh      A handle which represents an instance of MeshLink.
1250  *  @param channel   A handle for the channel.
1251  *  @param size      The desired size for the send buffer.
1252  *                   If a NULL pointer is given, the callback will be disabled.
1253  */
1254 extern void meshlink_set_channel_rcvbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1255
1256 /// Open a reliable stream channel to another node.
1257 /** This function is called whenever a remote node wants to open a channel to the local node.
1258  *  The application then has to decide whether to accept or reject this channel.
1259  *
1260  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1261  *  When the application does no longer need to use this channel, it must call meshlink_close()
1262  *  to free its resources.
1263  *
1264  *  \memberof meshlink_node
1265  *  @param mesh         A handle which represents an instance of MeshLink.
1266  *  @param node         The node to which this channel is being initiated.
1267  *  @param port         The port number the peer wishes to connect to.
1268  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1269  *                      The pointer may be NULL, in which case incoming data is ignored.
1270  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1271  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1272  *                      If len is 0, the data pointer is copied into the channel's priv member.
1273  *  @param len          The length of the data, or 0 if there is no data to send.
1274  *  @param flags        A bitwise-or'd combination of flags that set the semantics for this channel.
1275  *
1276  *  @return             A handle for the channel, or NULL in case of an error.
1277  *                      The handle is valid until meshlink_channel_close() is called.
1278  */
1279 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) __attribute__((__warn_unused_result__));
1280
1281 /// Open a reliable stream channel to another node.
1282 /** This function is called whenever a remote node wants to open a channel to the local node.
1283  *  The application then has to decide whether to accept or reject this channel.
1284  *
1285  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1286  *  When the application does no longer need to use this channel, it must call meshlink_close()
1287  *  to free its resources.
1288  *
1289  *  Calling this function is equivalent to calling meshlink_channel_open_ex()
1290  *  with the flags set to MESHLINK_CHANNEL_TCP.
1291  *
1292  *  \memberof meshlink_node
1293  *  @param mesh         A handle which represents an instance of MeshLink.
1294  *  @param node         The node to which this channel is being initiated.
1295  *  @param port         The port number the peer wishes to connect to.
1296  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
1297  *                      The pointer may be NULL, in which case incoming data is ignored.
1298  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1299  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1300  *  @param len          The length of the data, or 0 if there is no data to send.
1301  *                      If len is 0, the data pointer is copied into the channel's priv member.
1302  *
1303  *  @return             A handle for the channel, or NULL in case of an error.
1304  *                      The handle is valid until meshlink_channel_close() is called.
1305  */
1306 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) __attribute__((__warn_unused_result__));
1307
1308 /// Partially close a reliable stream channel.
1309 /** This shuts down the read or write side of a channel, or both, without closing the handle.
1310  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
1311  *  but still allows waiting for incoming data from the remote node.
1312  *
1313  *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
1314  *
1315  *  \memberof meshlink_channel
1316  *  @param mesh         A handle which represents an instance of MeshLink.
1317  *  @param channel      A handle for the channel.
1318  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
1319  */
1320 extern void meshlink_channel_shutdown(struct meshlink_handle *mesh, struct meshlink_channel *channel, int direction);
1321
1322 /// Close a reliable stream channel.
1323 /** This informs the remote node that the local node has finished sending all data on the channel.
1324  *  It also causes the local node to stop accepting incoming data from the remote node.
1325  *  It will free the struct meshlink_channel and all associated resources.
1326  *  Afterwards, the channel handle is invalid and must not be used any more.
1327  *
1328  *  It is allowed to call this function at any time on a valid handle, even inside callback functions.
1329  *  If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1330  *
1331  *  \memberof meshlink_channel
1332  *  @param mesh         A handle which represents an instance of MeshLink.
1333  *  @param channel      A handle for the channel.
1334  */
1335 extern void meshlink_channel_close(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1336
1337 /// Transmit data on a channel
1338 /** This queues data to send to the remote node.
1339  *
1340  *  \memberof meshlink_channel
1341  *  @param mesh         A handle which represents an instance of MeshLink.
1342  *  @param channel      A handle for the channel.
1343  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1344  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1345  *  @param len          The length of the data, or 0 if there is no data to send.
1346  *
1347  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1348  *                      If MESHLINK_CHANNEL_NO_PARTIAL is set, then the result will either be len,
1349  *                      0 if the buffer is currently too full, or -1 if len is too big even for an empty buffer.
1350  */
1351 extern ssize_t meshlink_channel_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len) __attribute__((__warn_unused_result__));
1352
1353 /// A callback for cleaning up buffers submitted for asynchronous I/O.
1354 /** This callbacks signals that MeshLink has finished using this buffer.
1355  *  The ownership of the buffer is now back into the application's hands.
1356  *
1357  *  @param mesh      A handle which represents an instance of MeshLink.
1358  *  @param channel   A handle for the channel which used this buffer.
1359  *  @param data      A pointer to a buffer containing the enqueued data.
1360  *  @param len       The length of the buffer.
1361  *  @param priv      A private pointer which was set by the application when submitting the buffer.
1362  */
1363 typedef void (*meshlink_aio_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, void *priv);
1364
1365 /// A callback for asynchronous I/O to and from filedescriptors.
1366 /** This callbacks signals that MeshLink has finished using this filedescriptor.
1367  *
1368  *  @param mesh      A handle which represents an instance of MeshLink.
1369  *  @param channel   A handle for the channel which used this filedescriptor.
1370  *  @param fd        The filedescriptor that was used.
1371  *  @param len       The length of the data that was successfully sent or received.
1372  *  @param priv      A private pointer which was set by the application when submitting the buffer.
1373  */
1374 typedef void (*meshlink_aio_fd_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, void *priv);
1375
1376 /// Transmit data on a channel asynchronously
1377 /** This registers a buffer that will be used to send data to the remote node.
1378  *  Multiple buffers can be registered, in which case data will be sent in the order the buffers were registered.
1379  *  While there are still buffers with unsent data, the poll callback will not be called.
1380  *
1381  *  \memberof meshlink_channel
1382  *  @param mesh         A handle which represents an instance of MeshLink.
1383  *  @param channel      A handle for the channel.
1384  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1385  *                      After meshlink_channel_aio_send() returns, the buffer may not be modified or freed by the application
1386  *                      until the callback routine is called.
1387  *  @param len          The length of the data, or 0 if there is no data to send.
1388  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the buffer.
1389  *  @param priv         A private pointer which is passed unchanged to the callback.
1390  *
1391  *  @return             True if the buffer was enqueued, false otherwise.
1392  */
1393 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) __attribute__((__warn_unused_result__));
1394
1395 /// Transmit data on a channel asynchronously from a filedescriptor
1396 /** This will read up to the specified length number of bytes from the given filedescriptor, and send it over the channel.
1397  *  The callback may be returned early if there is an error reading from the filedescriptor.
1398  *  While there is still with unsent data, the poll callback will not be called.
1399  *
1400  *  \memberof meshlink_channel
1401  *  @param mesh         A handle which represents an instance of MeshLink.
1402  *  @param channel      A handle for the channel.
1403  *  @param fd           A file descriptor from which data will be read.
1404  *  @param len          The length of the data, or 0 if there is no data to send.
1405  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1406  *  @param priv         A private pointer which is passed unchanged to the callback.
1407  *
1408  *  @return             True if the buffer was enqueued, false otherwise.
1409  */
1410 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) __attribute__((__warn_unused_result__));
1411
1412 /// Receive data on a channel asynchronously
1413 /** This registers a buffer that will be filled with incoming channel data.
1414  *  Multiple buffers can be registered, in which case data will be received in the order the buffers were registered.
1415  *  While there are still buffers that have not been filled, the receive callback will not be called.
1416  *
1417  *  \memberof meshlink_channel
1418  *  @param mesh         A handle which represents an instance of MeshLink.
1419  *  @param channel      A handle for the channel.
1420  *  @param data         A pointer to a buffer that will be filled with incoming data.
1421  *                      After meshlink_channel_aio_receive() returns, the buffer may not be modified or freed by the application
1422  *                      until the callback routine is called.
1423  *  @param len          The length of the data.
1424  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the buffer.
1425  *  @param priv         A private pointer which is passed unchanged to the callback.
1426  *
1427  *  @return             True if the buffer was enqueued, false otherwise.
1428  */
1429 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) __attribute__((__warn_unused_result__));
1430
1431 /// Receive data on a channel asynchronously and send it to a filedescriptor
1432 /** This will read up to the specified length number of bytes from the channel, and send it to the filedescriptor.
1433  *  The callback may be returned early if there is an error writing to the filedescriptor.
1434  *  While there is still unread data, the receive callback will not be called.
1435  *
1436  *  \memberof meshlink_channel
1437  *  @param mesh         A handle which represents an instance of MeshLink.
1438  *  @param channel      A handle for the channel.
1439  *  @param fd           A file descriptor to which data will be written.
1440  *  @param len          The length of the data.
1441  *  @param cb           A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1442  *  @param priv         A private pointer which was set by the application when submitting the buffer.
1443  *
1444  *  @return             True if the buffer was enqueued, false otherwise.
1445  */
1446 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) __attribute__((__warn_unused_result__));
1447
1448 /// Get channel flags.
1449 /** This returns the flags used when opening this channel.
1450  *
1451  *  \memberof meshlink_channel
1452  *  @param mesh         A handle which represents an instance of MeshLink.
1453  *  @param channel      A handle for the channel.
1454  *
1455  *  @return             The flags set for this channel.
1456  */
1457 extern uint32_t meshlink_channel_get_flags(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1458
1459 /// Get the amount of bytes in the send buffer.
1460 /** This returns the amount of bytes in the send buffer.
1461  *  These bytes have not been received by the peer yet.
1462  *
1463  *  \memberof meshlink_channel
1464  *  @param mesh         A handle which represents an instance of MeshLink.
1465  *  @param channel      A handle for the channel.
1466  *
1467  *  @return             The amount of un-ACKed bytes in the send buffer.
1468  */
1469 extern size_t meshlink_channel_get_sendq(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1470
1471 /// Get the amount of bytes in the receive buffer.
1472 /** This returns the amount of bytes in the receive buffer.
1473  *  These bytes have not been processed by the application yet.
1474  *
1475  *  \memberof meshlink_channel
1476  *  @param mesh         A handle which represents an instance of MeshLink.
1477  *  @param channel      A handle for the channel.
1478  *
1479  *  @return             The amount of bytes in the receive buffer.
1480  */
1481 extern size_t meshlink_channel_get_recvq(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1482
1483 /// Set the connection timeout used for channels to the given node.
1484 /** This sets the timeout after which unresponsive channels will be reported as closed.
1485  *  The timeout is set for all current and future channels to the given node.
1486  *
1487  *  \memberof meshlink_node
1488  *  @param mesh         A handle which represents an instance of MeshLink.
1489  *  @param channel      A handle for the channel.
1490  *  @param timeout      The timeout in seconds after which unresponsive channels will be reported as closed.
1491  *                      The default is 60 seconds.
1492  */
1493 extern void meshlink_set_node_channel_timeout(struct meshlink_handle *mesh, struct meshlink_node *node, int timeout);
1494
1495 /// Hint that a hostname may be found at an address
1496 /** This function indicates to meshlink that the given hostname is likely found
1497  *  at the given IP address and port.
1498  *
1499  *  \memberof meshlink_node
1500  *  @param mesh     A handle which represents an instance of MeshLink.
1501  *  @param node     A pointer to a struct meshlink_node describing the node to add the address hint for.
1502  *  @param addr     The IP address and port which should be tried for the
1503  *                  given hostname. The caller is free to overwrite or free
1504  *                  this memory once meshlink returns.
1505  */
1506 extern void meshlink_hint_address(struct meshlink_handle *mesh, struct meshlink_node *node, const struct sockaddr *addr);
1507
1508 /// Enable or disable zeroconf discovery of local peers
1509 /** This controls whether zeroconf discovery using the Catta library will be
1510  *  enabled to search for peers on the local network. By default, it is enabled.
1511  *
1512  *  \memberof meshlink_handle
1513  *  @param mesh    A handle which represents an instance of MeshLink.
1514  *  @param enable  Set to true to enable discovery, false to disable.
1515  */
1516 extern void meshlink_enable_discovery(struct meshlink_handle *mesh, bool enable);
1517
1518 /// Performs key rotation for an encrypted storage
1519 /** This rotates the (master) key for an encrypted storage and discards the old key
1520  *  if the call succeeded. This is an atomic call.
1521  *
1522  *  \memberof meshlink_handle
1523  *  @param mesh     A handle which represents an instance of MeshLink.
1524  *  @param key      A pointer to the new key used to encrypt storage.
1525  *  @param keylen   The length of the new key in bytes.
1526  *
1527  *  @return         This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
1528  */
1529 extern bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
1530
1531 /// Set device class timeouts
1532 /** This sets the ping interval and timeout for a given device class.
1533  *
1534  *  \memberof meshlink_handle
1535  *  @param mesh          A handle which represents an instance of MeshLink.
1536  *  @param devclass      The device class to update
1537  *  @param pinginterval  The interval between keepalive packets, in seconds. The default is 60.
1538  *  @param pingtimeout   The required time within which a peer should respond, in seconds. The default is 5.
1539  *                       The timeout must be smaller than the interval.
1540  */
1541 extern void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout);
1542
1543 /// Set device class fast retry period
1544 /** This sets the fast retry period for a given device class.
1545  *  During this period after the last time the mesh becomes unreachable, connections are tried once a second.
1546  *
1547  *  \memberof meshlink_handle
1548  *  @param mesh               A handle which represents an instance of MeshLink.
1549  *  @param devclass           The device class to update
1550  *  @param fast_retry_period  The period during which fast connection retries are done. The default is 0.
1551  */
1552 extern void meshlink_set_dev_class_fast_retry_period(struct meshlink_handle *mesh, dev_class_t devclass, int fast_retry_period);
1553
1554 /// Set which order invitations are committed
1555 /** This determines in which order configuration files are written to disk during an invitation.
1556  *  By default, the invitee saves the configuration to disk first, then the inviter.
1557  *  By calling this function with @a inviter_commits_first set to true, the order is reversed.
1558  *
1559  *  \memberof meshlink_handle
1560  *  @param mesh               A handle which represents an instance of MeshLink.
1561  *  @param inviter_commits_first  If true, then the node that invited a peer will commit data to disk first.
1562  */
1563 extern void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first);
1564
1565 /// Set the URL used to discover the host's external address
1566 /** For generating invitation URLs, MeshLink can look up the externally visible address of the local node.
1567  *  It does so by querying an external service. By default, this is http://meshlink.io/host.cgi.
1568  *  Only URLs starting with http:// are supported.
1569  *
1570  *  \memberof meshlink_handle
1571  *  @param mesh  A handle which represents an instance of MeshLink.
1572  *  @param url   The URL to use for external address queries, or NULL to revert back to the default URL.
1573  */
1574 extern void meshlink_set_external_address_discovery_url(struct meshlink_handle *mesh, const char *url);
1575
1576 #ifdef __cplusplus
1577 }
1578 #endif
1579
1580 #endif