]> git.meshlink.io Git - meshlink-tiny/blob - src/meshlink-tiny.h
Use a key/value store with configurable storage callbacks.
[meshlink-tiny] / src / meshlink-tiny.h
1 #ifndef MESHLINK_H
2 #define MESHLINK_H
3
4 /*
5     meshlink-tiny.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 /// The length in bytes of a signature made with meshlink_sign()
40 #define MESHLINK_SIGLEN (64ul)
41
42 // The maximum length of fingerprints
43 #define MESHLINK_FINGERPRINTLEN (64ul)
44
45 /// A handle for an instance of MeshLink.
46 typedef struct meshlink_handle meshlink_handle_t;
47
48 /// A handle for a MeshLink node.
49 typedef struct meshlink_node meshlink_node_t;
50
51 /// A struct containing all parameters used for opening a mesh.
52 typedef struct meshlink_open_params meshlink_open_params_t;
53
54 /// Code of most recent error encountered.
55 typedef enum {
56         MESHLINK_OK,           ///< Everything is fine
57         MESHLINK_EINVAL,       ///< Invalid parameter(s) to function call
58         MESHLINK_ENOMEM,       ///< Out of memory
59         MESHLINK_ENOENT,       ///< Node is not known
60         MESHLINK_EEXIST,       ///< Node already exists
61         MESHLINK_EINTERNAL,    ///< MeshLink internal error
62         MESHLINK_ERESOLV,      ///< MeshLink could not resolve a hostname
63         MESHLINK_ESTORAGE,     ///< MeshLink could not load or write data from/to disk
64         MESHLINK_ENETWORK,     ///< MeshLink encountered a network error
65         MESHLINK_EPEER,        ///< A peer caused an error
66         MESHLINK_ENOTSUP,      ///< The operation is not supported in the current configuration of MeshLink
67         MESHLINK_EBUSY,        ///< The MeshLink instance is already in use by another process
68         MESHLINK_EBLACKLISTED  ///< The operation is not allowed because the node is blacklisted
69 } meshlink_errno_t;
70
71 /// Device class
72 typedef enum {
73         DEV_CLASS_BACKBONE = 0,
74         DEV_CLASS_STATIONARY = 1,
75         DEV_CLASS_PORTABLE = 2,
76         DEV_CLASS_UNKNOWN = 3,
77         DEV_CLASS_COUNT
78 } dev_class_t;
79
80 /// Storage policy
81 typedef enum {
82         MESHLINK_STORAGE_ENABLED,    ///< Store all updates.
83         MESHLINK_STORAGE_DISABLED,   ///< Don't store any updates.
84         MESHLINK_STORAGE_KEYS_ONLY   ///< Only store updates when a node's key has changed.
85 } meshlink_storage_policy_t;
86
87 /// Invitation flags
88 static const uint32_t MESHLINK_INVITE_LOCAL = 1;    // Only use local addresses in the URL
89 static const uint32_t MESHLINK_INVITE_PUBLIC = 2;   // Only use public or canonical addresses in the URL
90 static const uint32_t MESHLINK_INVITE_IPV4 = 4;     // Only use IPv4 addresses in the URL
91 static const uint32_t MESHLINK_INVITE_IPV6 = 8;     // Only use IPv6 addresses in the URL
92 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
93
94 /// A variable holding the last encountered error from MeshLink.
95 /** This is a thread local variable that contains the error code of the most recent error
96  *  encountered by a MeshLink API function called in the current thread.
97  *  The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
98  *  if a function returned successfully.
99  */
100 extern __thread meshlink_errno_t meshlink_errno;
101
102 #ifndef MESHLINK_INTERNAL_H
103
104 struct meshlink_handle {
105         const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
106         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
107 };
108
109 struct meshlink_node {
110         const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
111         void *priv;             ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
112 };
113
114 #endif // MESHLINK_INTERNAL_H
115
116 /// Get the text for the given MeshLink error code.
117 /** This function returns a pointer to the string containing the description of the given error code.
118  *
119  *  @param err      An error code returned by MeshLink.
120  *
121  *  @return         A pointer to a string containing the description of the error code.
122  *                  The pointer is to static storage that is valid for the lifetime of the application.
123  *                  This function will always return a valid pointer, even if an invalid error code has been passed.
124  */
125 const char *meshlink_strerror(meshlink_errno_t err) __attribute__((__warn_unused_result__));
126
127 /// Create a new meshlink_open_params_t struct.
128 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
129  *  The resulting struct may be reused for multiple calls to meshlink_open_ex().
130  *  After the last use, the application must free this struct using meshlink_open_params_free().
131  *
132  *  @param confbase The directory in which MeshLink will store its configuration files.
133  *                  After the function returns, the application is free to overwrite or free @a confbase.
134  *  @param name     The name which this instance of the application will use in the mesh.
135  *                  After the function returns, the application is free to overwrite or free @a name.
136  *                  If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
137  *  @param appname  The application name which will be used in the mesh.
138  *                  After the function returns, the application is free to overwrite or free @a name.
139  *  @param devclass The device class which will be used in the mesh.
140  *
141  *  @return         A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
142  *                  The pointer is valid until meshlink_open_params_free() is called.
143  */
144 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__));
145
146 /// Free a meshlink_open_params_t struct.
147 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
148  *
149  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
150  */
151 void meshlink_open_params_free(meshlink_open_params_t *params);
152
153 /// Set the network namespace MeshLink should use.
154 /** This function changes the open parameters to use the given netns filedescriptor.
155  *
156  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
157  *  @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.
158  *
159  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
160  */
161 bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) __attribute__((__warn_unused_result__));
162
163 /// A callback that loads a configuration object.
164 /** @param mesh         A handle which represents an instance of MeshLink.
165  *  @param key          The key under which the configuration object was stored before.
166  *  @param[out] data    A pointer to a pointer to the buffer where the object has to be copied into.
167  *                      No more than len bytes should be written to this buffer.
168  *  @param[in,out] len  A pointer to the size of object.
169  *                      MeshLink sets this to the size of the buffer.
170  *                      The application must write the actual size of the object to it,
171  *                      regardless of whether it is larger or smaller than the buffer.
172  *
173  *  @return             This function must return true if the object was read and copied into the buffer succesfully,
174  *                      false otherwise.
175  *                      If the buffer provided by MeshLink was too small for the whole object, but no other errors
176  *                      occured, true must be returned.
177  */
178 typedef bool (*meshlink_load_cb_t)(struct meshlink_handle *mesh, const char *filename, void *data, size_t *len);
179
180 /// A callback that stores a configuration object.
181 /** @param mesh      A handle which represents an instance of MeshLink.
182  *  @param key       The key under which the object should be stored.
183  *  @param data      A pointer to the buffer holding the data that must be stored.
184  *  @param len       The size of the buffer that must be stored.
185  *
186  *  @return          This function must return true if the file was written succesfully, false otherwise.
187  */
188 typedef bool (*meshlink_store_cb_t)(struct meshlink_handle *mesh, const char *filename, const void *data, size_t len);
189
190 /// A callback that reports the presence of a configuration object.
191 /** @param mesh      A handle which represents an instance of MeshLink.
192  *  @param key       The key the object is stored under.
193  *  @param len       The size of the object if easy to obtain, zero otherwise.
194  *
195  *  @return          The return value. If it is false, the ls callback should immediately stop and return false as well.
196  */
197 typedef bool (*meshlink_ls_entry_cb_t)(struct meshlink_handle *mesh, const char *key, size_t len);
198
199 /// A callback that lists all configuration files.
200 /** @param mesh      A handle which represents an instance of MeshLink.
201  *  @param entry_cb  A callback that must be called once for every configuration file found.
202  *
203  *  @return          This function must return true if all configuration files were listed
204  *                   and all entry callbacks return true as well, false otherwise.
205  */
206 typedef bool (*meshlink_ls_cb_t)(struct meshlink_handle *mesh, meshlink_ls_entry_cb_t entry_cb);
207
208 /// Set the callbacks MeshLink should use for local storage.
209 /** This function allows the application to provide callbacks which handle loading and storing configuration files.
210  *  If storage callbacks are used, the application is in control of how and where configuration files are stored.
211  *  The application should then also ensure that only one MeshLink instance can access the configuration files at a time.
212  *
213  *  The callbacks get a relative filename which may contain directory separators, but there is no requirement that
214  *  the application also stores the files in directories. The only requirement is that a file stored with a given filename
215  *  can be loaded back using the same filename.
216  *
217  *  When storing files, it must ensure stores are done atomically.
218  *
219  *  If a storage key is set, MeshLink will take care of encrypting and decrypting the buffers passed to the storage callbacks.
220  *
221  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
222  *  @param load_cb  A pointer to the function that will be called when MeshLink wants to load a configuration file.
223  *  @param store_cb A pointer to the function that will be called when MeshLink wants to store a configuration file.
224  *  @param ls_cb    A pointer to the function that will be called when MeshLink wants to know which configuration files are available.
225  *
226  *  @return         This function will return true if the callbacks have been successfully updated, false otherwise.
227  */
228 bool meshlink_open_params_set_storage_callbacks(meshlink_open_params_t *params, meshlink_load_cb_t load_cb, meshlink_store_cb_t store_cb, meshlink_ls_cb_t ls_cb) __attribute__((__warn_unused_result__));
229
230 /// Set the encryption key MeshLink should use for local storage.
231 /** This function changes the open parameters to use the given key for encrypting MeshLink's own configuration files.
232  *
233  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
234  *  @param key      A pointer to a key, or NULL in case no encryption should be used.
235  *  @param keylen   The length of the given key, or 0 in case no encryption should be used.
236  *
237  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
238  */
239 bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
240
241 /// Set the encryption key MeshLink should use for local storage.
242 /** This function changes the open parameters to use the given storage policy.
243  *
244  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
245  *  @param policy   The storage policy to use.
246  *
247  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
248  */
249 bool meshlink_open_params_set_storage_policy(meshlink_open_params_t *params, meshlink_storage_policy_t policy) __attribute__((__warn_unused_result__));
250
251 /// Set the filename of the lockfile.
252 /** This function changes the path of the lockfile used to ensure only one instance of MeshLink can be open at the same time.
253  *  If an application changes this, it must always set it to the same location.
254  *
255  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
256  *  @param filename The filename of the lockfile.
257  *
258  *  @return         This function will return true if the open parameters have been successfully updated, false otherwise.
259  */
260 bool meshlink_open_params_set_lock_filename(meshlink_open_params_t *params, const char *filename) __attribute__((__warn_unused_result__));
261
262 /// Open or create a MeshLink instance.
263 /** This function opens or creates a MeshLink instance.
264  *  All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
265  *  which must have been initialized earlier by the application.
266  *
267  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
268  *  When the application does no longer need to use this handle, it must call meshlink_close() to
269  *  free its resources.
270  *
271  *  This function does not start any network I/O yet. The application should
272  *  first set callbacks, and then call meshlink_start().
273  *
274  *  @param params   A pointer to a meshlink_open_params_t which must be filled in by the application.
275  *                  After the function returns, the application is free to reuse or free @a params.
276  *
277  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
278  *                  The pointer is valid until meshlink_close() is called.
279  */
280 struct meshlink_handle *meshlink_open_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
281
282 /// Open or create a MeshLink instance.
283 /** This function opens or creates a MeshLink instance.
284  *  The state is stored in the configuration directory passed in the variable @a confbase.
285  *  If the configuration directory does not exist yet, for example when it is the first time
286  *  this instance is opened, the configuration directory will be automatically created and initialized.
287  *  However, the parent directory should already exist, otherwise an error will be returned.
288  *
289  *  The name given should be a unique identifier for this instance.
290  *
291  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
292  *  When the application does no longer need to use this handle, it must call meshlink_close() to
293  *  free its resources.
294  *
295  *  This function does not start any network I/O yet. The application should
296  *  first set callbacks, and then call meshlink_start().
297  *
298  *  @param confbase The directory in which MeshLink will store its configuration files.
299  *                  After the function returns, the application is free to overwrite or free @a confbase.
300  *  @param name     The name which this instance of the application will use in the mesh.
301  *                  After the function returns, the application is free to overwrite or free @a name.
302  *                  If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
303  *  @param appname  The application name which will be used in the mesh.
304  *                  After the function returns, the application is free to overwrite or free @a name.
305  *  @param devclass The device class which will be used in the mesh.
306  *
307  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
308  *                  The pointer is valid until meshlink_close() is called.
309  */
310 struct meshlink_handle *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
311
312 /// Open or create a MeshLink instance that uses encrypted storage.
313 /** This function opens or creates a MeshLink instance.
314  *  The state is stored in the configuration directory passed in the variable @a confbase.
315  *  If the configuration directory does not exist yet, for example when it is the first time
316  *  this instance is opened, the configuration directory will be automatically created and initialized.
317  *  However, the parent directory should already exist, otherwise an error will be returned.
318  *
319  *  The name given should be a unique identifier for this instance.
320  *
321  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
322  *  When the application does no longer need to use this handle, it must call meshlink_close() to
323  *  free its resources.
324  *
325  *  This function does not start any network I/O yet. The application should
326  *  first set callbacks, and then call meshlink_start().
327  *
328  *  @param confbase The directory in which MeshLink will store its configuration files.
329  *                  After the function returns, the application is free to overwrite or free @a confbase.
330  *  @param name     The name which this instance of the application will use in the mesh.
331  *                  After the function returns, the application is free to overwrite or free @a name.
332  *                  If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
333  *  @param appname  The application name which will be used in the mesh.
334  *                  After the function returns, the application is free to overwrite or free @a name.
335  *  @param devclass The device class which will be used in the mesh.
336  *  @param key      A pointer to a key used to encrypt storage.
337  *  @param keylen   The length of the key in bytes.
338  *
339  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
340  *                  The pointer is valid until meshlink_close() is called.
341  */
342 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__));
343
344 /// Create an ephemeral MeshLink instance that does not store any state.
345 /** This function creates a MeshLink instance.
346  *  No state is ever saved, so once this instance is closed, all its state is gone.
347  *
348  *  The name given should be a unique identifier for this instance.
349  *
350  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
351  *  When the application does no longer need to use this handle, it must call meshlink_close() to
352  *  free its resources.
353  *
354  *  This function does not start any network I/O yet. The application should
355  *  first set callbacks, and then call meshlink_start().
356  *
357  *  @param name     The name which this instance of the application will use in the mesh.
358  *                  After the function returns, the application is free to overwrite or free @a name.
359  *  @param appname  The application name which will be used in the mesh.
360  *                  After the function returns, the application is free to overwrite or free @a name.
361  *  @param devclass The device class which will be used in the mesh.
362  *
363  *  @return         A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
364  *                  The pointer is valid until meshlink_close() is called.
365  */
366 struct meshlink_handle *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
367
368 /// Start MeshLink.
369 /** This function causes MeshLink to open network sockets, make outgoing connections, and
370  *  create a new thread, which will handle all network I/O.
371  *
372  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
373  *
374  *  \memberof meshlink_handle
375  *  @param mesh     A handle which represents an instance of MeshLink.
376  *
377  *  @return         This function will return true if MeshLink has successfully started, false otherwise.
378  */
379 bool meshlink_start(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
380
381 /// Stop MeshLink.
382 /** This function causes MeshLink to disconnect from all other nodes,
383  *  close all sockets, and shut down its own thread.
384  *
385  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
386  *
387  *  \memberof meshlink_handle
388  *  @param mesh     A handle which represents an instance of MeshLink.
389  */
390 void meshlink_stop(struct meshlink_handle *mesh);
391
392 /// Close the MeshLink handle.
393 /** This function calls meshlink_stop() if necessary,
394  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink.
395  *  Afterwards, the handle and any pointers to a struct meshlink_node are invalid.
396  *
397  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
398  *  If called at a proper time with a valid handle, this function always succeeds.
399  *  If called within a callback or with an invalid handle, the result is undefined.
400  *
401  * \memberof meshlink_handle
402  *  @param mesh     A handle which represents an instance of MeshLink.
403  */
404 void meshlink_close(struct meshlink_handle *mesh);
405
406 /// Destroy a MeshLink instance.
407 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
408  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
409  *  new instance.
410  *
411  *  @param confbase The directory in which MeshLink stores its configuration files.
412  *                  After the function returns, the application is free to overwrite or free @a confbase.
413  *
414  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
415  */
416 bool meshlink_destroy(const char *confbase) __attribute__((__warn_unused_result__));
417
418 /// Destroy a MeshLink instance using open parameters.
419 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
420  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
421  *  new instance.
422  *
423  *  This version expects a pointer to meshlink_open_params_t,
424  *  and will use exactly the same settings used for opening a handle to destroy it.
425  *
426  *  @param params   A pointer to a meshlink_open_params_t which must be filled in by the application.
427  *                  After the function returns, the application is free to reuse or free @a params.
428  *
429  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
430  */
431 bool meshlink_destroy_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
432
433 /// A callback for receiving data from the mesh.
434 /** @param mesh      A handle which represents an instance of MeshLink.
435  *  @param source    A pointer to a struct meshlink_node describing the source of the data.
436  *  @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).
437  *                   The pointer is only valid during the lifetime of the callback.
438  *                   The callback should mempcy() the data if it needs to be available outside the callback.
439  *  @param len       The length of the received data, or 0 in case there is no data.
440  */
441 typedef void (*meshlink_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len);
442
443 /// Set the receive callback.
444 /** This functions sets the callback that is called whenever another node sends data to the local 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 another node sends data to the local node.
453  *                   If a NULL pointer is given, the callback will be disabled.
454  */
455 void meshlink_set_receive_cb(struct meshlink_handle *mesh, meshlink_receive_cb_t cb);
456
457 /// A callback reporting the meta-connection attempt made by the host node to an another node.
458 /** @param mesh      A handle which represents an instance of MeshLink.
459  *  @param node      A pointer to a struct meshlink_node describing the node to whom meta-connection is being tried.
460  *                   This pointer is valid until meshlink_close() is called.
461  */
462 typedef void (*meshlink_connection_try_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
463
464 /// Set the meta-connection try callback.
465 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
466  *  The callback is run in MeshLink's own thread.
467  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
468  *  to hand the data over to the application's thread.
469  *  The callback should also not block itself and return as quickly as possible.
470  *
471  *  \memberof meshlink_handle
472  *  @param mesh      A handle which represents an instance of MeshLink.
473  *  @param cb        A pointer to the function which will be called when host node attempts to make
474  *                   the connection to another node. If a NULL pointer is given, the callback will be disabled.
475  */
476 void meshlink_set_connection_try_cb(struct meshlink_handle *mesh, meshlink_connection_try_cb_t cb);
477
478 /// A callback reporting node status changes.
479 /** @param mesh      A handle which represents an instance of MeshLink.
480  *  @param node       A pointer to a struct meshlink_node describing the node whose status changed.
481  *                    This pointer is valid until meshlink_close() is called.
482  *  @param reachable  True if the node is reachable, false otherwise.
483  */
484 typedef void (*meshlink_node_status_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, bool reachable);
485
486 /// Set the node status callback.
487 /** This functions sets the callback that is called whenever another node's status changed.
488  *  The callback is run in MeshLink's own thread.
489  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
490  *  to hand the data over to the application's thread.
491  *  The callback should also not block itself and return as quickly as possible.
492  *
493  *  \memberof meshlink_handle
494  *  @param mesh      A handle which represents an instance of MeshLink.
495  *  @param cb        A pointer to the function which will be called when another node's status changes.
496  *                   If a NULL pointer is given, the callback will be disabled.
497  */
498 void meshlink_set_node_status_cb(struct meshlink_handle *mesh, meshlink_node_status_cb_t cb);
499
500 /// A callback reporting duplicate node detection.
501 /** @param mesh       A handle which represents an instance of MeshLink.
502  *  @param node       A pointer to a struct meshlink_node describing the node which is duplicate.
503  *                    This pointer is valid until meshlink_close() is called.
504  */
505 typedef void (*meshlink_node_duplicate_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
506
507 /// Set the node duplicate callback.
508 /** This functions sets the callback that is called whenever a duplicate node is detected.
509  *  The callback is run in MeshLink's own thread.
510  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
511  *  to hand the data over to the application's thread.
512  *  The callback should also not block itself and return as quickly as possible.
513  *
514  *  \memberof meshlink_handle
515  *  @param mesh      A handle which represents an instance of MeshLink.
516  *  @param cb        A pointer to the function which will be called when a duplicate node is detected.
517  *                   If a NULL pointer is given, the callback will be disabled.
518  */
519 void meshlink_set_node_duplicate_cb(struct meshlink_handle *mesh, meshlink_node_duplicate_cb_t cb);
520
521 /// Severity of log messages generated by MeshLink.
522 typedef enum {
523         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
524         MESHLINK_INFO,     ///< Informational messages.
525         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
526         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
527         MESHLINK_CRITICAL  ///< Critical errors which cause MeshLink to fail completely.
528 } meshlink_log_level_t;
529
530 /// A callback for receiving log messages generated by MeshLink.
531 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
532  *  @param level     An enum describing the severity level of the message.
533  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
534  *                   This pointer is only valid for the duration of the callback.
535  *                   The application must not free() this pointer.
536  *                   The application should strdup() the text if it has to be available outside the callback.
537  */
538 typedef void (*meshlink_log_cb_t)(struct meshlink_handle *mesh, meshlink_log_level_t level, const char *text);
539
540 /// Set the log callback.
541 /** This functions sets the callback that is called whenever MeshLink has some information to log.
542  *
543  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
544  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
545  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
546  *
547  *  If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
548  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
549  *  to hand the data over to the application's thread.
550  *  The callback should also not block itself and return as quickly as possible.
551  *
552  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
553  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
554  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
555  *
556  *  \memberof meshlink_handle
557  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
558  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
559  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
560  *                   If a NULL pointer is given, the callback will be disabled.
561  */
562 void meshlink_set_log_cb(struct meshlink_handle *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
563
564 /// A callback for receiving error conditions encountered by the MeshLink thread.
565 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
566  *  @param errno     The error code describing what kind of error occurred.
567  */
568 typedef void (*meshlink_error_cb_t)(struct meshlink_handle *mesh, meshlink_errno_t meshlink_errno);
569
570 /// Set the error callback.
571 /** This functions sets the callback that is called whenever the MeshLink thread encounters a serious error.
572  *
573  *  While most API functions report an error directly to the caller in case something went wrong,
574  *  MeshLink also runs a background thread which can encounter error conditions.
575  *  Most of them will be dealt with automatically, however there can be errors that will prevent MeshLink from
576  *  working correctly. When the callback is called, it means that MeshLink is no longer functioning
577  *  as expected. The application should then present an error message and shut down, or perform any other
578  *  action it deems appropriate.
579  *
580  *  The callback is run in MeshLink's own thread.
581  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
582  *  to hand the data over to the application's thread.
583  *  The callback should also not block itself and return as quickly as possible.
584  *
585  *  Even though the callback signals a serious error inside MeshLink, all open handles are still valid,
586  *  and the application should close handles in exactly the same it would have to do if the callback
587  *  was not called. This must not be done inside the callback itself.
588  *
589  *  \memberof meshlink_handle
590  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
591  *  @param cb        A pointer to the function which will be called when a serious error is encountered.
592  *                   If a NULL pointer is given, the callback will be disabled.
593  */
594 void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb);
595
596 /// Send data to another node.
597 /** This functions sends one packet of data to another node in the mesh.
598  *  The packet is sent using UDP semantics, which means that
599  *  the packet is sent as one unit and is received as one unit,
600  *  and that there is no guarantee that the packet will arrive at the destination.
601  *  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.
602  *  The application should take care of getting an acknowledgement and retransmission if necessary.
603  *
604  *  \memberof meshlink_node
605  *  @param mesh         A handle which represents an instance of MeshLink.
606  *  @param destination  A pointer to a struct meshlink_node describing the destination for the data.
607  *  @param data         A pointer to a buffer containing the data to be sent to the source.
608  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
609  *                      It is valid to specify a NULL pointer, but only if @a len is also 0.
610  *  @param len          The length of the data.
611  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
612  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
613  */
614 bool meshlink_send(struct meshlink_handle *mesh, struct meshlink_node *destination, const void *data, size_t len) __attribute__((__warn_unused_result__));
615
616 /// Get a handle for our own node.
617 /** This function returns a handle for the local node.
618  *
619  *  \memberof meshlink_handle
620  *  @param mesh         A handle which represents an instance of MeshLink.
621  *
622  *  @return             A pointer to a struct meshlink_node which represents the local node.
623  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
624  */
625 struct meshlink_node *meshlink_get_self(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
626
627 /// Get a handle for a specific node.
628 /** This function returns a handle for the node with the given name.
629  *
630  *  \memberof meshlink_handle
631  *  @param mesh         A handle which represents an instance of MeshLink.
632  *  @param name         The name of the node for which a handle is requested.
633  *                      After this function returns, the application is free to overwrite or free @a name.
634  *
635  *  @return             A pointer to a struct meshlink_node which represents the requested node,
636  *                      or NULL if the requested node does not exist.
637  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
638  */
639 struct meshlink_node *meshlink_get_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
640
641 /// Get the fingerprint of a node's public key.
642 /** This function returns a fingerprint of the node's public key.
643  *  It should be treated as an opaque blob.
644  *
645  *  \memberof meshlink_node
646  *  @param mesh         A handle which represents an instance of MeshLink.
647  *  @param node         A pointer to a struct meshlink_node describing the node.
648  *
649  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
650  *                      The application should call free() after it is done using this string.
651  */
652 char *meshlink_get_fingerprint(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
653
654 /// Get a list of all nodes.
655 /** This function returns a list with handles for all known nodes.
656  *
657  *  \memberof meshlink_handle
658  *  @param mesh         A handle which represents an instance of MeshLink.
659  *  @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.
660  *                      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).
661  *                      The application is allowed to call free() on the array whenever it wishes.
662  *                      The pointers in the array are valid until meshlink_close() is called.
663  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
664  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
665  *                      The contents of this variable will be changed to reflect the new size of the array.
666  *
667  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
668  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
669  *                      If it is a new value, the old value of @a nodes should not be used anymore.
670  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
671  */
672 struct meshlink_node **meshlink_get_all_nodes(struct meshlink_handle *mesh, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
673
674 /// Sign data using the local node's MeshLink key.
675 /** This function signs data using the local node's MeshLink key.
676  *  The generated signature can be securely verified by other nodes.
677  *
678  *  \memberof meshlink_handle
679  *  @param mesh         A handle which represents an instance of MeshLink.
680  *  @param data         A pointer to a buffer containing the data to be signed.
681  *  @param len          The length of the data to be signed.
682  *  @param signature    A pointer to a buffer where the signature will be stored.
683  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
684  *                      The signature is a binary blob, and is not nul-terminated.
685  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
686  *
687  *  @return             This function returns true if the signature was correctly generated, false otherwise.
688  */
689 bool meshlink_sign(struct meshlink_handle *mesh, const void *data, size_t len, void *signature, size_t *siglen) __attribute__((__warn_unused_result__));
690
691 /// Get the list of all nodes by device class.
692 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
693  *
694  *  \memberof meshlink_handle
695  *  @param mesh         A handle which represents an instance of MeshLink.
696  *  @param devclass     Device class of the nodes for which the list has to be obtained.
697  *  @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.
698  *                      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).
699  *                      The application is allowed to call free() on the array whenever it wishes.
700  *                      The pointers in the array are valid until meshlink_close() is called.
701  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
702  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
703  *                      The contents of this variable will be changed to reflect the new size of the array.
704  *
705  *  @return             A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
706  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
707  *                      If it is a new value, the old value of @a nodes should not be used anymore.
708  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
709  */
710 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__));
711
712 /// Get the list of all nodes by time they were last reachable.
713 /** This function returns a list with handles for all the nodes whose last known reachability time overlaps with the given time range.
714  *  If the range includes 0, it will count nodes that were never online.
715  *  If start is bigger than end, the result will be inverted.
716  *
717  *  \memberof meshlink_handle
718  *  @param mesh         A handle which represents an instance of MeshLink.
719  *  @param start        Start time.
720  *  @param end          End time.
721  *  @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.
722  *                      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).
723  *                      The application is allowed to call free() on the array whenever it wishes.
724  *                      The pointers in the array are valid until meshlink_close() is called.
725  *  @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.
726  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
727  *                      The contents of this variable will be changed to reflect the new size of the array.
728  *
729  *  @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.
730  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
731  *                      If it is a new value, the old value of @a nodes should not be used anymore.
732  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
733  */
734 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__));
735
736 /// Get the node's device class.
737 /** This function returns the device class of the given node.
738  *
739  *  \memberof meshlink_node
740  *  @param mesh          A handle which represents an instance of MeshLink.
741  *  @param node          A pointer to a struct meshlink_node describing the node.
742  *
743  *  @return              This function returns the device class of the @a node, or -1 in case of an error.
744  */
745 dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
746
747 /// Verify the signature generated by another node of a piece of data.
748 /** This function verifies the signature that another node generated for a piece of data.
749  *
750  *  \memberof meshlink_node
751  *  @param mesh         A handle which represents an instance of MeshLink.
752  *  @param source       A pointer to a struct meshlink_node describing the source of the signature.
753  *  @param data         A pointer to a buffer containing the data to be verified.
754  *  @param len          The length of the data to be verified.
755  *  @param signature    A pointer to a buffer where the signature is stored.
756  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
757  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
758  *
759  *  @return             This function returns true if the signature is valid, false otherwise.
760  */
761 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__));
762
763 /// Set the canonical Address for a node.
764 /** This function sets the canonical Address for a node.
765  *  This address is stored permanently until it is changed by another call to this function,
766  *  unlike other addresses associated with a node,
767  *  such as those added with meshlink_hint_address() or addresses discovered at runtime.
768  *
769  *  If a canonical Address is set for the local node,
770  *  it will be used for the hostname part of generated invitation URLs.
771  *  If a canonical Address is set for a remote node,
772  *  it is used exclusively for creating outgoing connections to that node.
773  *
774  *  \memberof meshlink_node
775  *  @param mesh         A handle which represents an instance of MeshLink.
776  *  @param node         A pointer to a struct meshlink_node describing the node.
777  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
778  *  @param port         A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
779  *                      If it is NULL, the listening port's number will be used.
780  *
781  *  @return             This function returns true if the address was added, false otherwise.
782  */
783 bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port) __attribute__((__warn_unused_result__));
784
785 /// Clear the canonical Address for a node.
786 /** This function clears the canonical Address for a node.
787  *
788  *  \memberof meshlink_node
789  *  @param mesh         A handle which represents an instance of MeshLink.
790  *  @param node         A pointer to a struct meshlink_node describing the node.
791  *
792  *  @return             This function returns true if the address was removed, false otherwise.
793  */
794 bool meshlink_clear_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
795
796 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
797  *  An invitation can only be used if the local node has never connected to other nodes before.
798  *  After a successfully accepted invitation, the name of the local node may have changed.
799  *
800  *  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.
801  *  It is not valid to call this function when the storage policy set to MESHLINK_STORAGE_DISABLED.
802  *
803  *  This function is blocking. It can take several seconds before it returns.
804  *  There is no guarantee it will perform a successful join.
805  *  Failures might be caused by temporary network outages, or by the invitation having expired.
806  *
807  *  \memberof meshlink_handle
808  *  @param mesh         A handle which represents an instance of MeshLink.
809  *  @param invitation   A nul-terminated C string containing the invitation URL.
810  *                      After this function returns, the application is free to overwrite or free @a invitation.
811  *
812  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
813  */
814 bool meshlink_join(struct meshlink_handle *mesh, const char *invitation) __attribute__((__warn_unused_result__));
815
816 /// Export the local node's key and addresses.
817 /** This function generates a string that contains the local node's public key and one or more IP addresses.
818  *  The application can pass it in some way to another node, which can then import it,
819  *  granting the local node access to the other node's mesh.
820  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
821  *
822  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
823  *
824  *  \memberof meshlink_handle
825  *  @param mesh         A handle which represents an instance of MeshLink.
826  *
827  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
828  *                      The application should call free() after it has finished using this string.
829  */
830 char *meshlink_export(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
831
832 /// Import another node's key and addresses.
833 /** This function accepts a string containing the exported public key and addresses of another node.
834  *  By importing this data, the local node grants the other node access to its mesh.
835  *  The application should make sure that the data it imports is really coming from the node it wants to import,
836  *
837  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
838  *
839  *  \memberof meshlink_handle
840  *  @param mesh         A handle which represents an instance of MeshLink.
841  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
842  *                      After this function returns, the application is free to overwrite or free @a data.
843  *
844  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
845  */
846 bool meshlink_import(struct meshlink_handle *mesh, const char *data) __attribute__((__warn_unused_result__));
847
848 /// Forget any information about a node.
849 /** This function allows the local node to forget any information it has about a node,
850  *  and if possible will remove any data it has stored on disk about the node.
851  *
852  *  After this call returns, the node handle is invalid and may no longer be used, regardless
853  *  of the return value of this call.
854  *
855  *  Note that this function does not prevent MeshLink from actually forgetting about a node,
856  *  or re-learning information about a node at a later point in time. It is merely a hint that
857  *  the application does not care about this node anymore and that any resources kept could be
858  *  cleaned up.
859  *
860  *  \memberof meshlink_node
861  *  @param mesh         A handle which represents an instance of MeshLink.
862  *  @param node         A pointer to a struct meshlink_node describing the node to be forgotten.
863  *
864  *  @return             This function returns true if all currently known data about the node has been forgotten, false otherwise.
865  */
866 bool meshlink_forget_node(struct meshlink_handle *mesh, struct meshlink_node *node);
867
868 /// Hint that a hostname may be found at an address
869 /** This function indicates to meshlink that the given hostname is likely found
870  *  at the given IP address and port.
871  *
872  *  \memberof meshlink_node
873  *  @param mesh     A handle which represents an instance of MeshLink.
874  *  @param node     A pointer to a struct meshlink_node describing the node to add the address hint for.
875  *  @param addr     The IP address and port which should be tried for the
876  *                  given hostname. The caller is free to overwrite or free
877  *                  this memory once meshlink returns.
878  */
879 void meshlink_hint_address(struct meshlink_handle *mesh, struct meshlink_node *node, const struct sockaddr *addr);
880
881 /// Inform MeshLink that the local network configuration might have changed
882 /** This is intended to be used when there is no way for MeshLink to get notifications of local network changes.
883  *  It forces MeshLink to scan all network interfaces for changes in up/down status and new/removed addresses,
884  *  and will immediately check if all connections to other nodes are still alive.
885  *
886  *  \memberof meshlink_handle
887  *  @param mesh    A handle which represents an instance of MeshLink.
888  */
889 void meshlink_hint_network_change(struct meshlink_handle *mesh);
890
891 /// Performs key rotation for an encrypted storage
892 /** This rotates the (master) key for an encrypted storage and discards the old key
893  *  if the call succeeded. This is an atomic call.
894  *
895  *  \memberof meshlink_handle
896  *  @param mesh     A handle which represents an instance of MeshLink.
897  *  @param key      A pointer to the new key used to encrypt storage.
898  *  @param keylen   The length of the new key in bytes.
899  *
900  *  @return         This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
901  */
902 bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
903
904 /// Set device class timeouts
905 /** This sets the ping interval and timeout for a given device class.
906  *
907  *  \memberof meshlink_handle
908  *  @param mesh          A handle which represents an instance of MeshLink.
909  *  @param devclass      The device class to update
910  *  @param pinginterval  The interval between keepalive packets, in seconds. The default is 60.
911  *  @param pingtimeout   The required time within which a peer should respond, in seconds. The default is 5.
912  *                       The timeout must be smaller than the interval.
913  */
914 void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout);
915
916 /// Set device class fast retry period
917 /** This sets the fast retry period for a given device class.
918  *  During this period after the last time the mesh becomes unreachable, connections are tried once a second.
919  *
920  *  \memberof meshlink_handle
921  *  @param mesh               A handle which represents an instance of MeshLink.
922  *  @param devclass           The device class to update
923  *  @param fast_retry_period  The period during which fast connection retries are done. The default is 0.
924  */
925 void meshlink_set_dev_class_fast_retry_period(struct meshlink_handle *mesh, dev_class_t devclass, int fast_retry_period);
926
927 /// Set device class maximum timeout
928 /** This sets the maximum timeout for outgoing connection retries for a given device class.
929  *
930  *  \memberof meshlink_handle
931  *  @param mesh          A handle which represents an instance of MeshLink.
932  *  @param devclass      The device class to update
933  *  @param maxtimeout    The maximum timeout between reconnection attempts, in seconds. The default is 900.
934  */
935 void meshlink_set_dev_class_maxtimeout(struct meshlink_handle *mesh, dev_class_t devclass, int maxtimeout);
936
937 /// Reset all connection timers
938 /** This resets all timers related to connections, causing pending outgoing connections to be retried immediately.
939  * It also sends keepalive packets on all active connections immediately.
940  *
941  *  \memberof meshlink_handle
942  *  @param mesh          A handle which represents an instance of MeshLink.
943  */
944 void meshlink_reset_timers(struct meshlink_handle *mesh);
945
946 /// Set which order invitations are committed
947 /** This determines in which order configuration files are written to disk during an invitation.
948  *  By default, the invitee saves the configuration to disk first, then the inviter.
949  *  By calling this function with @a inviter_commits_first set to true, the order is reversed.
950  *
951  *  \memberof meshlink_handle
952  *  @param mesh               A handle which represents an instance of MeshLink.
953  *  @param inviter_commits_first  If true, then the node that invited a peer will commit data to disk first.
954  */
955 void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first);
956
957 /// Set the scheduling granularity of the application
958 /** This should be set to the effective scheduling granularity for the application.
959  *  This depends on the scheduling granularity of the operating system, the application's
960  *  process priority and whether it is running as realtime or not.
961  *  The default value is 10000 (10 milliseconds).
962  *
963  *  \memberof meshlink_handle
964  *  @param mesh         A handle which represents an instance of MeshLink.
965  *  @param granularity  The scheduling granularity of the application in microseconds.
966  */
967 void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity);
968
969 /// Sets the storage policy used by MeshLink
970 /** This sets the policy MeshLink uses when it has new information about nodes.
971  *  By default, all udpates will be stored to disk (unless an ephemeral instance has been opened).
972  *  Setting the policy to MESHLINK_STORAGE_KEYS_ONLY, only updates that contain new keys for nodes
973  *  are stored.
974  *  By setting the policy to MESHLINK_STORAGE_DISABLED, no updates will be stored.
975  *
976  *  \memberof meshlink_handle
977  *  @param mesh    A handle which represents an instance of MeshLink.
978  *  @param policy  The storage policy to use.
979  */
980 void meshlink_set_storage_policy(struct meshlink_handle *mesh, meshlink_storage_policy_t policy);
981
982 #ifdef __cplusplus
983 }
984 #endif
985
986 #endif