5 meshlink-tiny.h -- MeshLink API
6 Copyright (C) 2014-2021 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
31 #include <sys/types.h>
32 #include <sys/socket.h>
39 /// The length in bytes of a signature made with meshlink_sign()
40 #define MESHLINK_SIGLEN (64ul)
42 // The maximum length of fingerprints
43 #define MESHLINK_FINGERPRINTLEN (64ul)
45 /// A handle for an instance of MeshLink.
46 typedef struct meshlink_handle meshlink_handle_t;
48 /// A handle for a MeshLink node.
49 typedef struct meshlink_node meshlink_node_t;
51 /// A handle for a MeshLink channel.
52 typedef struct meshlink_channel meshlink_channel_t;
54 /// A struct containing all parameters used for opening a mesh.
55 typedef struct meshlink_open_params meshlink_open_params_t;
57 /// Code of most recent error encountered.
59 MESHLINK_OK, ///< Everything is fine
60 MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
61 MESHLINK_ENOMEM, ///< Out of memory
62 MESHLINK_ENOENT, ///< Node is not known
63 MESHLINK_EEXIST, ///< Node already exists
64 MESHLINK_EINTERNAL, ///< MeshLink internal error
65 MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
66 MESHLINK_ESTORAGE, ///< MeshLink could not load or write data from/to disk
67 MESHLINK_ENETWORK, ///< MeshLink encountered a network error
68 MESHLINK_EPEER, ///< A peer caused an error
69 MESHLINK_ENOTSUP, ///< The operation is not supported in the current configuration of MeshLink
70 MESHLINK_EBUSY, ///< The MeshLink instance is already in use by another process
71 MESHLINK_EBLACKLISTED ///< The operation is not allowed because the node is blacklisted
76 DEV_CLASS_BACKBONE = 0,
77 DEV_CLASS_STATIONARY = 1,
78 DEV_CLASS_PORTABLE = 2,
79 DEV_CLASS_UNKNOWN = 3,
85 MESHLINK_STORAGE_ENABLED, ///< Store all updates.
86 MESHLINK_STORAGE_DISABLED, ///< Don't store any updates.
87 MESHLINK_STORAGE_KEYS_ONLY ///< Only store updates when a node's key has changed.
88 } meshlink_storage_policy_t;
91 static const uint32_t MESHLINK_INVITE_LOCAL = 1; // Only use local addresses in the URL
92 static const uint32_t MESHLINK_INVITE_PUBLIC = 2; // Only use public or canonical addresses in the URL
93 static const uint32_t MESHLINK_INVITE_IPV4 = 4; // Only use IPv4 addresses in the URL
94 static const uint32_t MESHLINK_INVITE_IPV6 = 8; // Only use IPv6 addresses in the URL
95 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
98 static const uint32_t MESHLINK_CHANNEL_RELIABLE = 1; // Data is retransmitted when packets are lost.
99 static const uint32_t MESHLINK_CHANNEL_ORDERED = 2; // Data is delivered in-order to the application.
100 static const uint32_t MESHLINK_CHANNEL_FRAMED = 4; // Data is delivered in chunks of the same length as data was originally sent.
101 static const uint32_t MESHLINK_CHANNEL_DROP_LATE = 8; // When packets are reordered, late packets are ignored.
102 static const uint32_t MESHLINK_CHANNEL_NO_PARTIAL = 16; // Calls to meshlink_channel_send() will either send all data or nothing.
103 static const uint32_t MESHLINK_CHANNEL_TCP = 3; // Select TCP semantics.
104 static const uint32_t MESHLINK_CHANNEL_UDP = 0; // Select UDP semantics.
106 /// A variable holding the last encountered error from MeshLink.
107 /** This is a thread local variable that contains the error code of the most recent error
108 * encountered by a MeshLink API function called in the current thread.
109 * The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
110 * if a function returned successfully.
112 extern __thread meshlink_errno_t meshlink_errno;
114 #ifndef MESHLINK_INTERNAL_H
116 struct meshlink_handle {
117 const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
118 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
121 struct meshlink_node {
122 const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
123 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
126 struct meshlink_channel {
127 struct meshlink_node *const node; ///< Pointer to the peer of this channel.
128 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
131 #endif // MESHLINK_INTERNAL_H
133 /// Get the text for the given MeshLink error code.
134 /** This function returns a pointer to the string containing the description of the given error code.
136 * @param err An error code returned by MeshLink.
138 * @return A pointer to a string containing the description of the error code.
139 * The pointer is to static storage that is valid for the lifetime of the application.
140 * This function will always return a valid pointer, even if an invalid error code has been passed.
142 const char *meshlink_strerror(meshlink_errno_t err) __attribute__((__warn_unused_result__));
144 /// Create a new meshlink_open_params_t struct.
145 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
146 * The resulting struct may be reused for multiple calls to meshlink_open_ex().
147 * After the last use, the application must free this struct using meshlink_open_params_free().
149 * @param confbase The directory in which MeshLink will store its configuration files.
150 * After the function returns, the application is free to overwrite or free @a confbase.
151 * @param name The name which this instance of the application will use in the mesh.
152 * After the function returns, the application is free to overwrite or free @a name.
153 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
154 * @param appname The application name which will be used in the mesh.
155 * After the function returns, the application is free to overwrite or free @a name.
156 * @param devclass The device class which will be used in the mesh.
158 * @return A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
159 * The pointer is valid until meshlink_open_params_free() is called.
161 meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
163 /// Free a meshlink_open_params_t struct.
164 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
166 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
168 void meshlink_open_params_free(meshlink_open_params_t *params);
170 /// Set the network namespace MeshLink should use.
171 /** This function changes the open parameters to use the given netns filedescriptor.
173 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
174 * @param netns A filedescriptor that must point to a valid network namespace, or -1 to have MeshLink use the same namespace as the calling thread.
176 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
178 bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) __attribute__((__warn_unused_result__));
180 /// Set the encryption key MeshLink should use for local storage.
181 /** This function changes the open parameters to use the given key for encrypting MeshLink's own configuration files.
183 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
184 * @param key A pointer to a key, or NULL in case no encryption should be used.
185 * @param keylen The length of the given key, or 0 in case no encryption should be used.
187 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
189 bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
191 /// Set the encryption key MeshLink should use for local storage.
192 /** This function changes the open parameters to use the given storage policy.
194 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
195 * @param policy The storage policy to use.
197 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
199 bool meshlink_open_params_set_storage_policy(meshlink_open_params_t *params, meshlink_storage_policy_t policy) __attribute__((__warn_unused_result__));
201 /// Set the filename of the lockfile.
202 /** This function changes the path of the lockfile used to ensure only one instance of MeshLink can be open at the same time.
203 * If an application changes this, it must always set it to the same location.
205 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
206 * @param filename The filename of the lockfile.
208 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
210 bool meshlink_open_params_set_lock_filename(meshlink_open_params_t *params, const char *filename) __attribute__((__warn_unused_result__));
212 /// Open or create a MeshLink instance.
213 /** This function opens or creates a MeshLink instance.
214 * All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
215 * which must have been initialized earlier by the application.
217 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
218 * When the application does no longer need to use this handle, it must call meshlink_close() to
219 * free its resources.
221 * This function does not start any network I/O yet. The application should
222 * first set callbacks, and then call meshlink_start().
224 * @param params A pointer to a meshlink_open_params_t which must be filled in by the application.
225 * After the function returns, the application is free to reuse or free @a params.
227 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
228 * The pointer is valid until meshlink_close() is called.
230 struct meshlink_handle *meshlink_open_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
232 /// Open or create a MeshLink instance.
233 /** This function opens or creates a MeshLink instance.
234 * The state is stored in the configuration directory passed in the variable @a confbase.
235 * If the configuration directory does not exist yet, for example when it is the first time
236 * this instance is opened, the configuration directory will be automatically created and initialized.
237 * However, the parent directory should already exist, otherwise an error will be returned.
239 * The name given should be a unique identifier for this instance.
241 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
242 * When the application does no longer need to use this handle, it must call meshlink_close() to
243 * free its resources.
245 * This function does not start any network I/O yet. The application should
246 * first set callbacks, and then call meshlink_start().
248 * @param confbase The directory in which MeshLink will store its configuration files.
249 * After the function returns, the application is free to overwrite or free @a confbase.
250 * @param name The name which this instance of the application will use in the mesh.
251 * After the function returns, the application is free to overwrite or free @a name.
252 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
253 * @param appname The application name which will be used in the mesh.
254 * After the function returns, the application is free to overwrite or free @a name.
255 * @param devclass The device class which will be used in the mesh.
257 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
258 * The pointer is valid until meshlink_close() is called.
260 struct meshlink_handle *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
262 /// Open or create a MeshLink instance that uses encrypted storage.
263 /** This function opens or creates a MeshLink instance.
264 * The state is stored in the configuration directory passed in the variable @a confbase.
265 * If the configuration directory does not exist yet, for example when it is the first time
266 * this instance is opened, the configuration directory will be automatically created and initialized.
267 * However, the parent directory should already exist, otherwise an error will be returned.
269 * The name given should be a unique identifier for this instance.
271 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
272 * When the application does no longer need to use this handle, it must call meshlink_close() to
273 * free its resources.
275 * This function does not start any network I/O yet. The application should
276 * first set callbacks, and then call meshlink_start().
278 * @param confbase The directory in which MeshLink will store its configuration files.
279 * After the function returns, the application is free to overwrite or free @a confbase.
280 * @param name The name which this instance of the application will use in the mesh.
281 * After the function returns, the application is free to overwrite or free @a name.
282 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
283 * @param appname The application name which will be used in the mesh.
284 * After the function returns, the application is free to overwrite or free @a name.
285 * @param devclass The device class which will be used in the mesh.
286 * @param key A pointer to a key used to encrypt storage.
287 * @param keylen The length of the key in bytes.
289 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
290 * The pointer is valid until meshlink_close() is called.
292 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__));
294 /// Create an ephemeral MeshLink instance that does not store any state.
295 /** This function creates a MeshLink instance.
296 * No state is ever saved, so once this instance is closed, all its state is gone.
298 * The name given should be a unique identifier for this instance.
300 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
301 * When the application does no longer need to use this handle, it must call meshlink_close() to
302 * free its resources.
304 * This function does not start any network I/O yet. The application should
305 * first set callbacks, and then call meshlink_start().
307 * @param name The name which this instance of the application will use in the mesh.
308 * After the function returns, the application is free to overwrite or free @a name.
309 * @param appname The application name which will be used in the mesh.
310 * After the function returns, the application is free to overwrite or free @a name.
311 * @param devclass The device class which will be used in the mesh.
313 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
314 * The pointer is valid until meshlink_close() is called.
316 struct meshlink_handle *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
319 /** This function causes MeshLink to open network sockets, make outgoing connections, and
320 * create a new thread, which will handle all network I/O.
322 * It is allowed to call this function even if MeshLink is already started, in which case it will return true.
324 * \memberof meshlink_handle
325 * @param mesh A handle which represents an instance of MeshLink.
327 * @return This function will return true if MeshLink has successfully started, false otherwise.
329 bool meshlink_start(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
332 /** This function causes MeshLink to disconnect from all other nodes,
333 * close all sockets, and shut down its own thread.
335 * This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
336 * Channels that are still open will remain valid, but any communication via channels will stop as well.
338 * \memberof meshlink_handle
339 * @param mesh A handle which represents an instance of MeshLink.
341 void meshlink_stop(struct meshlink_handle *mesh);
343 /// Close the MeshLink handle.
344 /** This function calls meshlink_stop() if necessary,
345 * and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
346 * Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
348 * It is allowed to call this function at any time on a valid handle, except inside callback functions.
349 * If called at a proper time with a valid handle, this function always succeeds.
350 * If called within a callback or with an invalid handle, the result is undefined.
352 * \memberof meshlink_handle
353 * @param mesh A handle which represents an instance of MeshLink.
355 void meshlink_close(struct meshlink_handle *mesh);
357 /// Destroy a MeshLink instance.
358 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
359 * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
362 * @param confbase The directory in which MeshLink stores its configuration files.
363 * After the function returns, the application is free to overwrite or free @a confbase.
365 * @return This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
367 bool meshlink_destroy(const char *confbase) __attribute__((__warn_unused_result__));
369 /// Destroy a MeshLink instance using open parameters.
370 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
371 * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
374 * This version expects a pointer to meshlink_open_params_t,
375 * and will use exactly the same settings used for opening a handle to destroy it.
377 * @param params A pointer to a meshlink_open_params_t which must be filled in by the application.
378 * After the function returns, the application is free to reuse or free @a params.
380 * @return This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
382 bool meshlink_destroy_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
384 /// A callback for receiving data from the mesh.
385 /** @param mesh A handle which represents an instance of MeshLink.
386 * @param source A pointer to a struct meshlink_node describing the source of the data.
387 * @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).
388 * The pointer is only valid during the lifetime of the callback.
389 * The callback should mempcy() the data if it needs to be available outside the callback.
390 * @param len The length of the received data, or 0 in case there is no data.
392 typedef void (*meshlink_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len);
394 /// Set the receive callback.
395 /** This functions sets the callback that is called whenever another node sends data to the local node.
396 * The callback is run in MeshLink's own thread.
397 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
398 * to hand the data over to the application's thread.
399 * The callback should also not block itself and return as quickly as possible.
401 * \memberof meshlink_handle
402 * @param mesh A handle which represents an instance of MeshLink.
403 * @param cb A pointer to the function which will be called when another node sends data to the local node.
404 * If a NULL pointer is given, the callback will be disabled.
406 void meshlink_set_receive_cb(struct meshlink_handle *mesh, meshlink_receive_cb_t cb);
408 /// A callback reporting the meta-connection attempt made by the host node to an another node.
409 /** @param mesh A handle which represents an instance of MeshLink.
410 * @param node A pointer to a struct meshlink_node describing the node to whom meta-connection is being tried.
411 * This pointer is valid until meshlink_close() is called.
413 typedef void (*meshlink_connection_try_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
415 /// Set the meta-connection try callback.
416 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
417 * The callback is run in MeshLink's own thread.
418 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
419 * to hand the data over to the application's thread.
420 * The callback should also not block itself and return as quickly as possible.
422 * \memberof meshlink_handle
423 * @param mesh A handle which represents an instance of MeshLink.
424 * @param cb A pointer to the function which will be called when host node attempts to make
425 * the connection to another node. If a NULL pointer is given, the callback will be disabled.
427 void meshlink_set_connection_try_cb(struct meshlink_handle *mesh, meshlink_connection_try_cb_t cb);
429 /// A callback reporting node status changes.
430 /** @param mesh A handle which represents an instance of MeshLink.
431 * @param node A pointer to a struct meshlink_node describing the node whose status changed.
432 * This pointer is valid until meshlink_close() is called.
433 * @param reachable True if the node is reachable, false otherwise.
435 typedef void (*meshlink_node_status_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, bool reachable);
437 /// Set the node status callback.
438 /** This functions sets the callback that is called whenever another node's status changed.
439 * The callback is run in MeshLink's own thread.
440 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
441 * to hand the data over to the application's thread.
442 * The callback should also not block itself and return as quickly as possible.
444 * \memberof meshlink_handle
445 * @param mesh A handle which represents an instance of MeshLink.
446 * @param cb A pointer to the function which will be called when another node's status changes.
447 * If a NULL pointer is given, the callback will be disabled.
449 void meshlink_set_node_status_cb(struct meshlink_handle *mesh, meshlink_node_status_cb_t cb);
451 /// A callback reporting duplicate node detection.
452 /** @param mesh A handle which represents an instance of MeshLink.
453 * @param node A pointer to a struct meshlink_node describing the node which is duplicate.
454 * This pointer is valid until meshlink_close() is called.
456 typedef void (*meshlink_node_duplicate_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
458 /// Set the node duplicate callback.
459 /** This functions sets the callback that is called whenever a duplicate node is detected.
460 * The callback is run in MeshLink's own thread.
461 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
462 * to hand the data over to the application's thread.
463 * The callback should also not block itself and return as quickly as possible.
465 * \memberof meshlink_handle
466 * @param mesh A handle which represents an instance of MeshLink.
467 * @param cb A pointer to the function which will be called when a duplicate node is detected.
468 * If a NULL pointer is given, the callback will be disabled.
470 void meshlink_set_node_duplicate_cb(struct meshlink_handle *mesh, meshlink_node_duplicate_cb_t cb);
472 /// Severity of log messages generated by MeshLink.
474 MESHLINK_DEBUG, ///< Internal debugging messages. Only useful during application development.
475 MESHLINK_INFO, ///< Informational messages.
476 MESHLINK_WARNING, ///< Warnings which might indicate problems, but which are not real errors.
477 MESHLINK_ERROR, ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
478 MESHLINK_CRITICAL ///< Critical errors which cause MeshLink to fail completely.
479 } meshlink_log_level_t;
481 /// A callback for receiving log messages generated by MeshLink.
482 /** @param mesh A handle which represents an instance of MeshLink, or NULL.
483 * @param level An enum describing the severity level of the message.
484 * @param text A pointer to a nul-terminated C string containing the textual log message.
485 * This pointer is only valid for the duration of the callback.
486 * The application must not free() this pointer.
487 * The application should strdup() the text if it has to be available outside the callback.
489 typedef void (*meshlink_log_cb_t)(struct meshlink_handle *mesh, meshlink_log_level_t level, const char *text);
491 /// Set the log callback.
492 /** This functions sets the callback that is called whenever MeshLink has some information to log.
494 * The @a mesh parameter can either be a valid MeshLink handle, or NULL.
495 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
496 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
498 * If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
499 * It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
500 * to hand the data over to the application's thread.
501 * The callback should also not block itself and return as quickly as possible.
503 * The @a mesh parameter can either be a valid MeshLink handle, or NULL.
504 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
505 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
507 * \memberof meshlink_handle
508 * @param mesh A handle which represents an instance of MeshLink, or NULL.
509 * @param level An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
510 * @param cb A pointer to the function which will be called when another node sends data to the local node.
511 * If a NULL pointer is given, the callback will be disabled.
513 void meshlink_set_log_cb(struct meshlink_handle *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
515 /// A callback for receiving error conditions encountered by the MeshLink thread.
516 /** @param mesh A handle which represents an instance of MeshLink, or NULL.
517 * @param errno The error code describing what kind of error occurred.
519 typedef void (*meshlink_error_cb_t)(struct meshlink_handle *mesh, meshlink_errno_t meshlink_errno);
521 /// Set the error callback.
522 /** This functions sets the callback that is called whenever the MeshLink thread encounters a serious error.
524 * While most API functions report an error directly to the caller in case something went wrong,
525 * MeshLink also runs a background thread which can encounter error conditions.
526 * Most of them will be dealt with automatically, however there can be errors that will prevent MeshLink from
527 * working correctly. When the callback is called, it means that MeshLink is no longer functioning
528 * as expected. The application should then present an error message and shut down, or perform any other
529 * action it deems appropriate.
531 * The callback is run in MeshLink's own thread.
532 * It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
533 * to hand the data over to the application's thread.
534 * The callback should also not block itself and return as quickly as possible.
536 * Even though the callback signals a serious error inside MeshLink, all open handles are still valid,
537 * and the application should close handles in exactly the same it would have to do if the callback
538 * was not called. This must not be done inside the callback itself.
540 * \memberof meshlink_handle
541 * @param mesh A handle which represents an instance of MeshLink, or NULL.
542 * @param cb A pointer to the function which will be called when a serious error is encountered.
543 * If a NULL pointer is given, the callback will be disabled.
545 void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb);
547 /// Send data to another node.
548 /** This functions sends one packet of data to another node in the mesh.
549 * The packet is sent using UDP semantics, which means that
550 * the packet is sent as one unit and is received as one unit,
551 * and that there is no guarantee that the packet will arrive at the destination.
552 * Packets that are too big to be sent over the network as one unit might be dropped, and this function may return an error if this situation can be detected beforehand.
553 * The application should take care of getting an acknowledgement and retransmission if necessary.
555 * \memberof meshlink_node
556 * @param mesh A handle which represents an instance of MeshLink.
557 * @param destination A pointer to a struct meshlink_node describing the destination for the data.
558 * @param data A pointer to a buffer containing the data to be sent to the source.
559 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
560 * It is valid to specify a NULL pointer, but only if @a len is also 0.
561 * @param len The length of the data.
562 * @return This function will return true if MeshLink has queued the message for transmission, and false otherwise.
563 * A return value of true does not guarantee that the message will actually arrive at the destination.
565 bool meshlink_send(struct meshlink_handle *mesh, struct meshlink_node *destination, const void *data, size_t len) __attribute__((__warn_unused_result__));
567 /// Get a handle for our own node.
568 /** This function returns a handle for the local node.
570 * \memberof meshlink_handle
571 * @param mesh A handle which represents an instance of MeshLink.
573 * @return A pointer to a struct meshlink_node which represents the local node.
574 * The pointer is guaranteed to be valid until meshlink_close() is called.
576 struct meshlink_node *meshlink_get_self(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
578 /// Get a handle for a specific node.
579 /** This function returns a handle for the node with the given name.
581 * \memberof meshlink_handle
582 * @param mesh A handle which represents an instance of MeshLink.
583 * @param name The name of the node for which a handle is requested.
584 * After this function returns, the application is free to overwrite or free @a name.
586 * @return A pointer to a struct meshlink_node which represents the requested node,
587 * or NULL if the requested node does not exist.
588 * The pointer is guaranteed to be valid until meshlink_close() is called.
590 struct meshlink_node *meshlink_get_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
592 /// Get the fingerprint of a node's public key.
593 /** This function returns a fingerprint of the node's public key.
594 * It should be treated as an opaque blob.
596 * \memberof meshlink_node
597 * @param mesh A handle which represents an instance of MeshLink.
598 * @param node A pointer to a struct meshlink_node describing the node.
600 * @return A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
601 * The application should call free() after it is done using this string.
603 char *meshlink_get_fingerprint(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
605 /// Get a list of all nodes.
606 /** This function returns a list with handles for all known nodes.
608 * \memberof meshlink_handle
609 * @param mesh A handle which represents an instance of MeshLink.
610 * @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.
611 * 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).
612 * The application is allowed to call free() on the array whenever it wishes.
613 * The pointers in the array are valid until meshlink_close() is called.
614 * @param nmemb A pointer to a variable holding the number of nodes that are stored in the array.
615 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
616 * The contents of this variable will be changed to reflect the new size of the array.
618 * @return A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
619 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
620 * If it is a new value, the old value of @a nodes should not be used anymore.
621 * If the new value is NULL, then the old array will have been freed by MeshLink.
623 struct meshlink_node **meshlink_get_all_nodes(struct meshlink_handle *mesh, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
625 /// Sign data using the local node's MeshLink key.
626 /** This function signs data using the local node's MeshLink key.
627 * The generated signature can be securely verified by other nodes.
629 * \memberof meshlink_handle
630 * @param mesh A handle which represents an instance of MeshLink.
631 * @param data A pointer to a buffer containing the data to be signed.
632 * @param len The length of the data to be signed.
633 * @param signature A pointer to a buffer where the signature will be stored.
634 * The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
635 * The signature is a binary blob, and is not nul-terminated.
636 * @param siglen The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
638 * @return This function returns true if the signature was correctly generated, false otherwise.
640 bool meshlink_sign(struct meshlink_handle *mesh, const void *data, size_t len, void *signature, size_t *siglen) __attribute__((__warn_unused_result__));
642 /// Get the list of all nodes by device class.
643 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
645 * \memberof meshlink_handle
646 * @param mesh A handle which represents an instance of MeshLink.
647 * @param devclass Device class of the nodes for which the list has to be obtained.
648 * @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.
649 * 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).
650 * The application is allowed to call free() on the array whenever it wishes.
651 * The pointers in the array are valid until meshlink_close() is called.
652 * @param nmemb A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
653 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
654 * The contents of this variable will be changed to reflect the new size of the array.
656 * @return A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
657 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
658 * If it is a new value, the old value of @a nodes should not be used anymore.
659 * If the new value is NULL, then the old array will have been freed by MeshLink.
661 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__));
663 /// Get the list of all nodes by time they were last reachable.
664 /** This function returns a list with handles for all the nodes whose last known reachability time overlaps with the given time range.
665 * If the range includes 0, it will count nodes that were never online.
666 * If start is bigger than end, the result will be inverted.
668 * \memberof meshlink_handle
669 * @param mesh A handle which represents an instance of MeshLink.
670 * @param start Start time.
671 * @param end End time.
672 * @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.
673 * 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).
674 * The application is allowed to call free() on the array whenever it wishes.
675 * The pointers in the array are valid until meshlink_close() is called.
676 * @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.
677 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
678 * The contents of this variable will be changed to reflect the new size of the array.
680 * @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.
681 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
682 * If it is a new value, the old value of @a nodes should not be used anymore.
683 * If the new value is NULL, then the old array will have been freed by MeshLink.
685 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__));
687 /// Get the node's device class.
688 /** This function returns the device class of the given node.
690 * \memberof meshlink_node
691 * @param mesh A handle which represents an instance of MeshLink.
692 * @param node A pointer to a struct meshlink_node describing the node.
694 * @return This function returns the device class of the @a node, or -1 in case of an error.
696 dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
698 /// Get a node's reachability status.
699 /** This function returns the current reachability of a given node, and the times of the last state changes.
700 * If a given state change never happened, the time returned will be 0.
702 * \memberof meshlink_node
703 * @param mesh A handle which represents an instance of MeshLink.
704 * @param node A pointer to a struct meshlink_node describing the node.
705 * @param last_reachable A pointer to a time_t variable that will be filled in with the last time the node became reachable.
706 * Pass NULL to not have anything written.
707 * @param last_unreachable A pointer to a time_t variable that will be filled in with the last time the node became unreachable.
708 * Pass NULL to not have anything written.
710 * @return This function returns true if the node is currently reachable, false otherwise.
712 bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlink_node *node, time_t *last_reachable, time_t *last_unreachable);
714 /// Verify the signature generated by another node of a piece of data.
715 /** This function verifies the signature that another node generated for a piece of data.
717 * \memberof meshlink_node
718 * @param mesh A handle which represents an instance of MeshLink.
719 * @param source A pointer to a struct meshlink_node describing the source of the signature.
720 * @param data A pointer to a buffer containing the data to be verified.
721 * @param len The length of the data to be verified.
722 * @param signature A pointer to a buffer where the signature is stored.
723 * @param siglen A pointer to a variable holding the size of the signature buffer.
724 * The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
726 * @return This function returns true if the signature is valid, false otherwise.
728 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__));
730 /// Set the canonical Address for a node.
731 /** This function sets the canonical Address for a node.
732 * This address is stored permanently until it is changed by another call to this function,
733 * unlike other addresses associated with a node,
734 * such as those added with meshlink_hint_address() or addresses discovered at runtime.
736 * If a canonical Address is set for the local node,
737 * it will be used for the hostname part of generated invitation URLs.
738 * If a canonical Address is set for a remote node,
739 * it is used exclusively for creating outgoing connections to that node.
741 * \memberof meshlink_node
742 * @param mesh A handle which represents an instance of MeshLink.
743 * @param node A pointer to a struct meshlink_node describing the node.
744 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
745 * @param port A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
746 * If it is NULL, the listening port's number will be used.
748 * @return This function returns true if the address was added, false otherwise.
750 bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port) __attribute__((__warn_unused_result__));
752 /// Clear the canonical Address for a node.
753 /** This function clears the canonical Address for a node.
755 * \memberof meshlink_node
756 * @param mesh A handle which represents an instance of MeshLink.
757 * @param node A pointer to a struct meshlink_node describing the node.
759 * @return This function returns true if the address was removed, false otherwise.
761 bool meshlink_clear_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
763 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
764 * An invitation can only be used if the local node has never connected to other nodes before.
765 * After a successfully accepted invitation, the name of the local node may have changed.
767 * 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.
768 * It is not valid to call this function when the storage policy set to MESHLINK_STORAGE_DISABLED.
770 * This function is blocking. It can take several seconds before it returns.
771 * There is no guarantee it will perform a successful join.
772 * Failures might be caused by temporary network outages, or by the invitation having expired.
774 * \memberof meshlink_handle
775 * @param mesh A handle which represents an instance of MeshLink.
776 * @param invitation A nul-terminated C string containing the invitation URL.
777 * After this function returns, the application is free to overwrite or free @a invitation.
779 * @return This function returns true if the local node joined the mesh it was invited to, false otherwise.
781 bool meshlink_join(struct meshlink_handle *mesh, const char *invitation) __attribute__((__warn_unused_result__));
783 /// Export the local node's key and addresses.
784 /** This function generates a string that contains the local node's public key and one or more IP addresses.
785 * The application can pass it in some way to another node, which can then import it,
786 * granting the local node access to the other node's mesh.
787 * The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
789 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
791 * \memberof meshlink_handle
792 * @param mesh A handle which represents an instance of MeshLink.
794 * @return This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
795 * The application should call free() after it has finished using this string.
797 char *meshlink_export(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
799 /// Import another node's key and addresses.
800 /** This function accepts a string containing the exported public key and addresses of another node.
801 * By importing this data, the local node grants the other node access to its mesh.
802 * The application should make sure that the data it imports is really coming from the node it wants to import,
804 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
806 * \memberof meshlink_handle
807 * @param mesh A handle which represents an instance of MeshLink.
808 * @param data A nul-terminated C string containing the other node's exported key and addresses.
809 * After this function returns, the application is free to overwrite or free @a data.
811 * @return This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
813 bool meshlink_import(struct meshlink_handle *mesh, const char *data) __attribute__((__warn_unused_result__));
815 /// Forget any information about a node.
816 /** This function allows the local node to forget any information it has about a node,
817 * and if possible will remove any data it has stored on disk about the node.
819 * Any open channels to this node must be closed before calling this function.
821 * After this call returns, the node handle is invalid and may no longer be used, regardless
822 * of the return value of this call.
824 * Note that this function does not prevent MeshLink from actually forgetting about a node,
825 * or re-learning information about a node at a later point in time. It is merely a hint that
826 * the application does not care about this node anymore and that any resources kept could be
829 * \memberof meshlink_node
830 * @param mesh A handle which represents an instance of MeshLink.
831 * @param node A pointer to a struct meshlink_node describing the node to be forgotten.
833 * @return This function returns true if all currently known data about the node has been forgotten, false otherwise.
835 bool meshlink_forget_node(struct meshlink_handle *mesh, struct meshlink_node *node);
837 /// A callback for listening for incoming channels.
838 /** This function is called whenever a remote node wants to open a channel to the local node.
839 * This callback should only make a decision whether to accept or reject this channel.
840 * The accept callback should be set to get a handle to the actual channel.
842 * The callback is run in MeshLink's own thread.
843 * It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
844 * to hand any data over to the application's thread.
846 * @param mesh A handle which represents an instance of MeshLink.
847 * @param node A handle for the node that wants to open a channel.
848 * @param port The port number the peer wishes to connect to.
850 * @return This function should return true if the application accepts the incoming channel, false otherwise.
852 typedef bool (*meshlink_channel_listen_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t port);
854 /// A callback for accepting incoming channels.
855 /** This function is called whenever a remote node has opened a channel to the local node.
857 * The callback is run in MeshLink's own thread.
858 * It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
859 * to hand any data over to the application's thread.
861 * @param mesh A handle which represents an instance of MeshLink.
862 * @param channel A handle for the incoming channel.
863 * If the application accepts the incoming channel by returning true,
864 * then this handle is valid until meshlink_channel_close() is called on it.
865 * If the application rejects the incoming channel by returning false,
866 * then this handle is invalid after the callback returns
867 * (the callback does not need to call meshlink_channel_close() itself in this case).
868 * @param port The port number the peer wishes to connect to.
869 * @param data A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
870 * The pointer is only valid during the lifetime of the callback.
871 * The callback should mempcy() the data if it needs to be available outside the callback.
872 * @param len The length of the data, or 0 in case no data has been received yet. (Not yet used.)
874 * @return This function should return true if the application accepts the incoming channel, false otherwise.
875 * If returning false, the channel is invalid and may not be used anymore.
877 typedef bool (*meshlink_channel_accept_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, uint16_t port, const void *data, size_t len);
879 /// A callback for receiving data from a channel.
880 /** This function is called whenever data is received from a remote node on a channel.
882 * This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
883 * In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
884 * In any case, the @a channel handle will still be valid until the application calls meshlink_close().
886 * @param mesh A handle which represents an instance of MeshLink.
887 * @param channel A handle for the channel.
888 * @param data A pointer to a buffer containing data sent by the source, or NULL in case of an error.
889 * The pointer is only valid during the lifetime of the callback.
890 * The callback should mempcy() the data if it needs to be available outside the callback.
891 * @param len The length of the data, or 0 in case of an error.
893 typedef void (*meshlink_channel_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len);
895 /// Set the listen callback.
896 /** This functions sets the callback that is called whenever another node wants to open a channel to the local node.
897 * The callback is run in MeshLink's own thread.
898 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
899 * to hand the data over to the application's thread.
900 * The callback should also not block itself and return as quickly as possible.
902 * If no listen or accept callbacks are set, incoming channels are rejected.
904 * \memberof meshlink_handle
905 * @param mesh A handle which represents an instance of MeshLink.
906 * @param cb A pointer to the function which will be called when another node want to open a channel.
907 * If a NULL pointer is given, the callback will be disabled.
909 void meshlink_set_channel_listen_cb(struct meshlink_handle *mesh, meshlink_channel_listen_cb_t cb);
911 /// Set the accept callback.
912 /** This functions sets the callback that is called whenever a remote node has opened a channel to the local node.
913 * The callback is run in MeshLink's own thread.
914 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
915 * to hand the data over to the application's thread.
916 * The callback should also not block itself and return as quickly as possible.
918 * If no listen or accept callbacks are set, incoming channels are rejected.
920 * \memberof meshlink_handle
921 * @param mesh A handle which represents an instance of MeshLink.
922 * @param cb A pointer to the function which will be called when a new channel has been opened by a remote node.
923 * If a NULL pointer is given, the callback will be disabled.
925 void meshlink_set_channel_accept_cb(struct meshlink_handle *mesh, meshlink_channel_accept_cb_t cb);
927 /// Set the receive callback.
928 /** This functions sets the callback that is called whenever another node sends data to the local node.
929 * The callback is run in MeshLink's own thread.
930 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
931 * to hand the data over to the application's thread.
932 * The callback should also not block itself and return as quickly as possible.
934 * \memberof meshlink_channel
935 * @param mesh A handle which represents an instance of MeshLink.
936 * @param channel A handle for the channel.
937 * @param cb A pointer to the function which will be called when another node sends data to the local node.
938 * If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
940 void meshlink_set_channel_receive_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_receive_cb_t cb);
942 /// Set the flags of a channel.
943 /** This function allows changing some of the channel flags.
944 * Currently only MESHLINK_CHANNEL_NO_PARTIAL and MESHLINK_CHANNEL_DROP_LATE are supported, other flags are ignored.
945 * These flags only affect the local side of the channel with the peer.
946 * The changes take effect immediately.
948 * \memberof meshlink_channel
949 * @param mesh A handle which represents an instance of MeshLink.
950 * @param channel A handle for the channel.
951 * @param flags A bitwise-or'd combination of flags that set the semantics for this channel.
953 void meshlink_set_channel_flags(struct meshlink_handle *mesh, struct meshlink_channel *channel, uint32_t flags);
955 /// Open a reliable stream channel to another node.
956 /** This function is called whenever a remote node wants to open a channel to the local node.
957 * The application then has to decide whether to accept or reject this channel.
959 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
960 * When the application does no longer need to use this channel, it must call meshlink_close()
961 * to free its resources.
963 * \memberof meshlink_node
964 * @param mesh A handle which represents an instance of MeshLink.
965 * @param node The node to which this channel is being initiated.
966 * @param port The port number the peer wishes to connect to.
967 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
968 * The pointer may be NULL, in which case incoming data is ignored.
969 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
970 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
971 * If len is 0, the data pointer is copied into the channel's priv member.
972 * @param len The length of the data, or 0 if there is no data to send.
973 * @param flags A bitwise-or'd combination of flags that set the semantics for this channel.
975 * @return A handle for the channel, or NULL in case of an error.
976 * The handle is valid until meshlink_channel_close() is called.
978 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__));
980 /// Open a reliable stream channel to another node.
981 /** This function is called whenever a remote node wants to open a channel to the local node.
982 * The application then has to decide whether to accept or reject this channel.
984 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
985 * When the application does no longer need to use this channel, it must call meshlink_close()
986 * to free its resources.
988 * Calling this function is equivalent to calling meshlink_channel_open_ex()
989 * with the flags set to MESHLINK_CHANNEL_TCP.
991 * \memberof meshlink_node
992 * @param mesh A handle which represents an instance of MeshLink.
993 * @param node The node to which this channel is being initiated.
994 * @param port The port number the peer wishes to connect to.
995 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
996 * The pointer may be NULL, in which case incoming data is ignored.
997 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
998 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
999 * @param len The length of the data, or 0 if there is no data to send.
1000 * If len is 0, the data pointer is copied into the channel's priv member.
1002 * @return A handle for the channel, or NULL in case of an error.
1003 * The handle is valid until meshlink_channel_close() is called.
1005 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__));
1007 /// Partially close a reliable stream channel.
1008 /** This shuts down the read or write side of a channel, or both, without closing the handle.
1009 * It can be used to inform the remote node that the local node has finished sending all data on the channel,
1010 * but still allows waiting for incoming data from the remote node.
1012 * Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
1014 * \memberof meshlink_channel
1015 * @param mesh A handle which represents an instance of MeshLink.
1016 * @param channel A handle for the channel.
1017 * @param direction Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
1019 void meshlink_channel_shutdown(struct meshlink_handle *mesh, struct meshlink_channel *channel, int direction);
1021 /// Close a reliable stream channel.
1022 /** This informs the remote node that the local node has finished sending all data on the channel.
1023 * It also causes the local node to stop accepting incoming data from the remote node.
1024 * Afterwards, the channel handle is invalid and must not be used any more.
1026 * It is allowed to call this function at any time on a valid handle, even inside callback functions.
1027 * If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1029 * \memberof meshlink_channel
1030 * @param mesh A handle which represents an instance of MeshLink.
1031 * @param channel A handle for the channel.
1033 void meshlink_channel_close(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1035 /// Abort a reliable stream channel.
1036 /** This aborts a channel.
1037 * Data that was in the send and receive buffers is dropped, so potentially there is some data that
1038 * was sent on this channel that will not be received by the peer.
1039 * Afterwards, the channel handle is invalid and must not be used any more.
1041 * It is allowed to call this function at any time on a valid handle, even inside callback functions.
1042 * If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1044 * \memberof meshlink_channel
1045 * @param mesh A handle which represents an instance of MeshLink.
1046 * @param channel A handle for the channel.
1048 void meshlink_channel_abort(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1050 /// Transmit data on a channel
1051 /** This queues data to send to the remote node.
1053 * \memberof meshlink_channel
1054 * @param mesh A handle which represents an instance of MeshLink.
1055 * @param channel A handle for the channel.
1056 * @param data A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1057 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
1058 * @param len The length of the data, or 0 if there is no data to send.
1060 * @return The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1061 * If MESHLINK_CHANNEL_NO_PARTIAL is set, then the result will either be len,
1062 * 0 if the buffer is currently too full, or -1 if len is too big even for an empty buffer.
1064 ssize_t meshlink_channel_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len) __attribute__((__warn_unused_result__));
1066 /// Get channel flags.
1067 /** This returns the flags used when opening this channel.
1069 * \memberof meshlink_channel
1070 * @param mesh A handle which represents an instance of MeshLink.
1071 * @param channel A handle for the channel.
1073 * @return The flags set for this channel.
1075 uint32_t meshlink_channel_get_flags(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1077 /// Get the maximum segment size of a channel.
1078 /** This returns the amount of bytes that can be sent at once for channels with UDP semantics.
1080 * \memberof meshlink_channel
1081 * @param mesh A handle which represents an instance of MeshLink.
1082 * @param channel A handle for the channel.
1084 * @return The amount of bytes in the receive buffer.
1086 size_t meshlink_channel_get_mss(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1088 /// Set the connection timeout used for channels to the given node.
1089 /** This sets the timeout after which unresponsive channels will be reported as closed.
1090 * The timeout is set for all current and future channels to the given node.
1092 * \memberof meshlink_node
1093 * @param mesh A handle which represents an instance of MeshLink.
1094 * @param node A pointer to a struct meshlink_node describing the node to set the channel connection timeout for.
1095 * @param timeout The timeout in seconds after which unresponsive channels will be reported as closed.
1096 * The default is 60 seconds.
1098 void meshlink_set_node_channel_timeout(struct meshlink_handle *mesh, struct meshlink_node *node, int timeout);
1100 /// Hint that a hostname may be found at an address
1101 /** This function indicates to meshlink that the given hostname is likely found
1102 * at the given IP address and port.
1104 * \memberof meshlink_node
1105 * @param mesh A handle which represents an instance of MeshLink.
1106 * @param node A pointer to a struct meshlink_node describing the node to add the address hint for.
1107 * @param addr The IP address and port which should be tried for the
1108 * given hostname. The caller is free to overwrite or free
1109 * this memory once meshlink returns.
1111 void meshlink_hint_address(struct meshlink_handle *mesh, struct meshlink_node *node, const struct sockaddr *addr);
1113 /// Inform MeshLink that the local network configuration might have changed
1114 /** This is intended to be used when there is no way for MeshLink to get notifications of local network changes.
1115 * It forces MeshLink to scan all network interfaces for changes in up/down status and new/removed addresses,
1116 * and will immediately check if all connections to other nodes are still alive.
1118 * \memberof meshlink_handle
1119 * @param mesh A handle which represents an instance of MeshLink.
1121 void meshlink_hint_network_change(struct meshlink_handle *mesh);
1123 /// Performs key rotation for an encrypted storage
1124 /** This rotates the (master) key for an encrypted storage and discards the old key
1125 * if the call succeeded. This is an atomic call.
1127 * \memberof meshlink_handle
1128 * @param mesh A handle which represents an instance of MeshLink.
1129 * @param key A pointer to the new key used to encrypt storage.
1130 * @param keylen The length of the new key in bytes.
1132 * @return This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
1134 bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
1136 /// Set device class timeouts
1137 /** This sets the ping interval and timeout for a given device class.
1139 * \memberof meshlink_handle
1140 * @param mesh A handle which represents an instance of MeshLink.
1141 * @param devclass The device class to update
1142 * @param pinginterval The interval between keepalive packets, in seconds. The default is 60.
1143 * @param pingtimeout The required time within which a peer should respond, in seconds. The default is 5.
1144 * The timeout must be smaller than the interval.
1146 void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout);
1148 /// Set device class fast retry period
1149 /** This sets the fast retry period for a given device class.
1150 * During this period after the last time the mesh becomes unreachable, connections are tried once a second.
1152 * \memberof meshlink_handle
1153 * @param mesh A handle which represents an instance of MeshLink.
1154 * @param devclass The device class to update
1155 * @param fast_retry_period The period during which fast connection retries are done. The default is 0.
1157 void meshlink_set_dev_class_fast_retry_period(struct meshlink_handle *mesh, dev_class_t devclass, int fast_retry_period);
1159 /// Set device class maximum timeout
1160 /** This sets the maximum timeout for outgoing connection retries for a given device class.
1162 * \memberof meshlink_handle
1163 * @param mesh A handle which represents an instance of MeshLink.
1164 * @param devclass The device class to update
1165 * @param maxtimeout The maximum timeout between reconnection attempts, in seconds. The default is 900.
1167 void meshlink_set_dev_class_maxtimeout(struct meshlink_handle *mesh, dev_class_t devclass, int maxtimeout);
1169 /// Reset all connection timers
1170 /** This resets all timers related to connections, causing pending outgoing connections to be retried immediately.
1171 * It also sends keepalive packets on all active connections immediately.
1173 * \memberof meshlink_handle
1174 * @param mesh A handle which represents an instance of MeshLink.
1176 void meshlink_reset_timers(struct meshlink_handle *mesh);
1178 /// Set which order invitations are committed
1179 /** This determines in which order configuration files are written to disk during an invitation.
1180 * By default, the invitee saves the configuration to disk first, then the inviter.
1181 * By calling this function with @a inviter_commits_first set to true, the order is reversed.
1183 * \memberof meshlink_handle
1184 * @param mesh A handle which represents an instance of MeshLink.
1185 * @param inviter_commits_first If true, then the node that invited a peer will commit data to disk first.
1187 void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first);
1189 /// Set the scheduling granularity of the application
1190 /** This should be set to the effective scheduling granularity for the application.
1191 * This depends on the scheduling granularity of the operating system, the application's
1192 * process priority and whether it is running as realtime or not.
1193 * The default value is 10000 (10 milliseconds).
1195 * \memberof meshlink_handle
1196 * @param mesh A handle which represents an instance of MeshLink.
1197 * @param granularity The scheduling granularity of the application in microseconds.
1199 void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity);
1201 /// Sets the storage policy used by MeshLink
1202 /** This sets the policy MeshLink uses when it has new information about nodes.
1203 * By default, all udpates will be stored to disk (unless an ephemeral instance has been opened).
1204 * Setting the policy to MESHLINK_STORAGE_KEYS_ONLY, only updates that contain new keys for nodes
1206 * By setting the policy to MESHLINK_STORAGE_DISABLED, no updates will be stored.
1208 * \memberof meshlink_handle
1209 * @param mesh A handle which represents an instance of MeshLink.
1210 * @param policy The storage policy to use.
1212 void meshlink_set_storage_policy(struct meshlink_handle *mesh, meshlink_storage_policy_t policy);