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