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