5 meshlink.h -- MeshLink API
6 Copyright (C) 2014-2019 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 /// A handle for a MeshLink sub-mesh.
58 typedef struct meshlink_submesh meshlink_submesh_t;
60 /// Code of most recent error encountered.
62 MESHLINK_OK, ///< Everything is fine
63 MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
64 MESHLINK_ENOMEM, ///< Out of memory
65 MESHLINK_ENOENT, ///< Node is not known
66 MESHLINK_EEXIST, ///< Node already exists
67 MESHLINK_EINTERNAL, ///< MeshLink internal error
68 MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
69 MESHLINK_ESTORAGE, ///< MeshLink could not load or write data from/to disk
70 MESHLINK_ENETWORK, ///< MeshLink encountered a network error
71 MESHLINK_EPEER, ///< A peer caused an error
72 MESHLINK_ENOTSUP, ///< The operation is not supported in the current configuration of MeshLink
73 MESHLINK_EBUSY, ///< The MeshLink instance is already in use by another process
74 MESHLINK_EBLACKLISTED ///< The operation is not allowed because the node is blacklisted
79 DEV_CLASS_BACKBONE = 0,
80 DEV_CLASS_STATIONARY = 1,
81 DEV_CLASS_PORTABLE = 2,
82 DEV_CLASS_UNKNOWN = 3,
87 static const uint32_t MESHLINK_INVITE_LOCAL = 1; // Only use local addresses in the URL
88 static const uint32_t MESHLINK_INVITE_PUBLIC = 2; // Only use public or canonical addresses in the URL
89 static const uint32_t MESHLINK_INVITE_IPV4 = 4; // Only use IPv4 addresses in the URL
90 static const uint32_t MESHLINK_INVITE_IPV6 = 8; // Only use IPv6 addresses in the URL
91 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
94 static const uint32_t MESHLINK_CHANNEL_RELIABLE = 1; // Data is retransmitted when packets are lost.
95 static const uint32_t MESHLINK_CHANNEL_ORDERED = 2; // Data is delivered in-order to the application.
96 static const uint32_t MESHLINK_CHANNEL_FRAMED = 4; // Data is delivered in chunks of the same length as data was originally sent.
97 static const uint32_t MESHLINK_CHANNEL_DROP_LATE = 8; // When packets are reordered, late packets are ignored.
98 static const uint32_t MESHLINK_CHANNEL_NO_PARTIAL = 16; // Calls to meshlink_channel_send() will either send all data or nothing.
99 static const uint32_t MESHLINK_CHANNEL_TCP = 3; // Select TCP semantics.
100 static const uint32_t MESHLINK_CHANNEL_UDP = 0; // Select UDP semantics.
102 /// A variable holding the last encountered error from MeshLink.
103 /** This is a thread local variable that contains the error code of the most recent error
104 * encountered by a MeshLink API function called in the current thread.
105 * The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
106 * if a function returned successfully.
108 extern __thread meshlink_errno_t meshlink_errno;
110 #ifndef MESHLINK_INTERNAL_H
112 struct meshlink_handle {
113 const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
114 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
117 struct meshlink_node {
118 const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
119 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
122 struct meshlink_submesh {
123 const char *const name; ///< Textual name of this Sub-Mesh. It is stored in a nul-terminated C string, which is allocated by MeshLink.
124 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
127 struct meshlink_channel {
128 struct meshlink_node *const node; ///< Pointer to the peer of this channel.
129 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
132 #endif // MESHLINK_INTERNAL_H
134 /// Get the text for the given MeshLink error code.
135 /** This function returns a pointer to the string containing the description of the given error code.
137 * @param err An error code returned by MeshLink.
139 * @return A pointer to a string containing the description of the error code.
140 * The pointer is to static storage that is valid for the lifetime of the application.
141 * This function will always return a valid pointer, even if an invalid error code has been passed.
143 const char *meshlink_strerror(meshlink_errno_t err) __attribute__((__warn_unused_result__));
145 /// Create a new meshlink_open_params_t struct.
146 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
147 * The resulting struct may be reused for multiple calls to meshlink_open_ex().
148 * After the last use, the application must free this struct using meshlink_open_params_free().
150 * @param confbase The directory in which MeshLink will store its configuration files.
151 * After the function returns, the application is free to overwrite or free @a confbase.
152 * @param name The name which this instance of the application will use in the mesh.
153 * After the function returns, the application is free to overwrite or free @a name.
154 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
155 * @param appname The application name which will be used in the mesh.
156 * After the function returns, the application is free to overwrite or free @a name.
157 * @param devclass The device class which will be used in the mesh.
159 * @return A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
160 * The pointer is valid until meshlink_open_params_free() is called.
162 meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
164 /// Free a meshlink_open_params_t struct.
165 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
167 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
169 void meshlink_open_params_free(meshlink_open_params_t *params);
171 /// Set the network namespace MeshLink should use.
172 /** This function changes the open parameters to use the given netns filedescriptor.
174 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
175 * @param netns A filedescriptor that must point to a valid network namespace, or -1 to have MeshLink use the same namespace as the calling thread.
177 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
179 bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) __attribute__((__warn_unused_result__));
181 /// Set the encryption key MeshLink should use for local storage.
182 /** This function changes the open parameters to use the given key for encrypting MeshLink's own configuration files.
184 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
185 * @param key A pointer to a key, or NULL in case no encryption should be used.
186 * @param keylen The length of the given key, or 0 in case no encryption should be used.
188 * @return This function will return true if the open parameters have been successfully updated, false otherwise.
190 bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
192 /// Open or create a MeshLink instance.
193 /** This function opens or creates a MeshLink instance.
194 * All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
195 * which must have been initialized earlier by the application.
197 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
198 * When the application does no longer need to use this handle, it must call meshlink_close() to
199 * free its resources.
201 * This function does not start any network I/O yet. The application should
202 * first set callbacks, and then call meshlink_start().
204 * @param params A pointer to a meshlink_open_params_t which must be filled in by the application.
205 * After the function returns, the application is free to reuse or free @a params.
207 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
208 * The pointer is valid until meshlink_close() is called.
210 struct meshlink_handle *meshlink_open_ex(const meshlink_open_params_t *params) __attribute__((__warn_unused_result__));
212 /// Open or create a MeshLink instance.
213 /** This function opens or creates a MeshLink instance.
214 * The state is stored in the configuration directory passed in the variable @a confbase.
215 * If the configuration directory does not exist yet, for example when it is the first time
216 * this instance is opened, the configuration directory will be automatically created and initialized.
217 * However, the parent directory should already exist, otherwise an error will be returned.
219 * The name given should be a unique identifier for this instance.
221 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
222 * When the application does no longer need to use this handle, it must call meshlink_close() to
223 * free its resources.
225 * This function does not start any network I/O yet. The application should
226 * first set callbacks, and then call meshlink_start().
228 * @param confbase The directory in which MeshLink will store its configuration files.
229 * After the function returns, the application is free to overwrite or free @a confbase.
230 * @param name The name which this instance of the application will use in the mesh.
231 * After the function returns, the application is free to overwrite or free @a name.
232 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
233 * @param appname The application name which will be used in the mesh.
234 * After the function returns, the application is free to overwrite or free @a name.
235 * @param devclass The device class which will be used in the mesh.
237 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
238 * The pointer is valid until meshlink_close() is called.
240 struct meshlink_handle *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
242 /// Open or create a MeshLink instance that uses encrypted storage.
243 /** This function opens or creates a MeshLink instance.
244 * The state is stored in the configuration directory passed in the variable @a confbase.
245 * If the configuration directory does not exist yet, for example when it is the first time
246 * this instance is opened, the configuration directory will be automatically created and initialized.
247 * However, the parent directory should already exist, otherwise an error will be returned.
249 * The name given should be a unique identifier for this instance.
251 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
252 * When the application does no longer need to use this handle, it must call meshlink_close() to
253 * free its resources.
255 * This function does not start any network I/O yet. The application should
256 * first set callbacks, and then call meshlink_start().
258 * @param confbase The directory in which MeshLink will store its configuration files.
259 * After the function returns, the application is free to overwrite or free @a confbase.
260 * @param name The name which this instance of the application will use in the mesh.
261 * After the function returns, the application is free to overwrite or free @a name.
262 * If NULL is passed as the name, the name used last time the MeshLink instance was initialized is used.
263 * @param appname The application name which will be used in the mesh.
264 * After the function returns, the application is free to overwrite or free @a name.
265 * @param devclass The device class which will be used in the mesh.
266 * @param key A pointer to a key used to encrypt storage.
267 * @param keylen The length of the key in bytes.
269 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
270 * The pointer is valid until meshlink_close() is called.
272 struct meshlink_handle *meshlink_open_encrypted(const char *confbase, const char *name, const char *appname, dev_class_t devclass, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
274 /// Create an ephemeral MeshLink instance that does not store any state.
275 /** This function creates a MeshLink instance.
276 * No state is ever saved, so once this instance is closed, all its state is gone.
278 * The name given should be a unique identifier for this instance.
280 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
281 * When the application does no longer need to use this handle, it must call meshlink_close() to
282 * free its resources.
284 * This function does not start any network I/O yet. The application should
285 * first set callbacks, and then call meshlink_start().
287 * @param name The name which this instance of the application will use in the mesh.
288 * After the function returns, the application is free to overwrite or free @a name.
289 * @param appname The application name which will be used in the mesh.
290 * After the function returns, the application is free to overwrite or free @a name.
291 * @param devclass The device class which will be used in the mesh.
293 * @return A pointer to a struct meshlink_handle which represents this instance of MeshLink, or NULL in case of an error.
294 * The pointer is valid until meshlink_close() is called.
296 struct meshlink_handle *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) __attribute__((__warn_unused_result__));
299 /** This function causes MeshLink to open a new Sub-Mesh network
300 * create a new thread, which will handle all network I/O.
302 * It is allowed to call this function even if MeshLink is already started, in which case it will return true.
304 * \memberof meshlink_handle
305 * @param mesh A handle which represents an instance of MeshLink.
307 * @param submesh Name of the new Sub-Mesh to create.
309 * @return A pointer to a struct meshlink_submesh which represents this instance of SubMesh, or NULL in case of an error.
310 * The pointer is valid until meshlink_close() is called.
312 struct meshlink_submesh *meshlink_submesh_open(struct meshlink_handle *mesh, const char *submesh) __attribute__((__warn_unused_result__));
315 /** This function causes MeshLink to open network sockets, make outgoing connections, and
316 * create a new thread, which will handle all network I/O.
318 * It is allowed to call this function even if MeshLink is already started, in which case it will return true.
320 * \memberof meshlink_handle
321 * @param mesh A handle which represents an instance of MeshLink.
323 * @return This function will return true if MeshLink has successfully started, false otherwise.
325 bool meshlink_start(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
328 /** This function causes MeshLink to disconnect from all other nodes,
329 * close all sockets, and shut down its own thread.
331 * This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
332 * Channels that are still open will remain valid, but any communication via channels will stop as well.
334 * \memberof meshlink_handle
335 * @param mesh A handle which represents an instance of MeshLink.
337 void meshlink_stop(struct meshlink_handle *mesh);
339 /// Close the MeshLink handle.
340 /** This function calls meshlink_stop() if necessary,
341 * and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
342 * Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
344 * It is allowed to call this function at any time on a valid handle, except inside callback functions.
345 * If called at a proper time with a valid handle, this function always succeeds.
346 * If called within a callback or with an invalid handle, the result is undefined.
348 * \memberof meshlink_handle
349 * @param mesh A handle which represents an instance of MeshLink.
351 void meshlink_close(struct meshlink_handle *mesh);
353 /// Destroy a MeshLink instance.
354 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
355 * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
358 * @param confbase The directory in which MeshLink stores its configuration files.
359 * After the function returns, the application is free to overwrite or free @a confbase.
361 * @return This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
363 bool meshlink_destroy(const char *confbase) __attribute__((__warn_unused_result__));
365 /// A callback for receiving data from the mesh.
366 /** @param mesh A handle which represents an instance of MeshLink.
367 * @param source A pointer to a struct meshlink_node describing the source of the data.
368 * @param data A pointer to a buffer containing the data sent by the source, or NULL in case there is no data (an empty packet was received).
369 * The pointer is only valid during the lifetime of the callback.
370 * The callback should mempcy() the data if it needs to be available outside the callback.
371 * @param len The length of the received data, or 0 in case there is no data.
373 typedef void (*meshlink_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len);
375 /// Set the receive callback.
376 /** This functions sets the callback that is called whenever another node sends data to the local node.
377 * The callback is run in MeshLink's own thread.
378 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
379 * to hand the data over to the application's thread.
380 * The callback should also not block itself and return as quickly as possible.
382 * \memberof meshlink_handle
383 * @param mesh A handle which represents an instance of MeshLink.
384 * @param cb A pointer to the function which will be called when another node sends data to the local node.
385 * If a NULL pointer is given, the callback will be disabled.
387 void meshlink_set_receive_cb(struct meshlink_handle *mesh, meshlink_receive_cb_t cb);
389 /// A callback reporting the meta-connection attempt made by the host node to an another node.
390 /** @param mesh A handle which represents an instance of MeshLink.
391 * @param node A pointer to a struct meshlink_node describing the node to whom meta-connection is being tried.
392 * This pointer is valid until meshlink_close() is called.
394 typedef void (*meshlink_connection_try_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
396 /// Set the meta-connection try callback.
397 /** This functions sets the callback that is called whenever a connection attempt is happened to another node.
398 * The callback is run in MeshLink's own thread.
399 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
400 * to hand the data over to the application's thread.
401 * The callback should also not block itself and return as quickly as possible.
403 * \memberof meshlink_handle
404 * @param mesh A handle which represents an instance of MeshLink.
405 * @param cb A pointer to the function which will be called when host node attempts to make
406 * the connection to another node. If a NULL pointer is given, the callback will be disabled.
408 void meshlink_set_connection_try_cb(struct meshlink_handle *mesh, meshlink_connection_try_cb_t cb);
410 /// A callback reporting node status changes.
411 /** @param mesh A handle which represents an instance of MeshLink.
412 * @param node A pointer to a struct meshlink_node describing the node whose status changed.
413 * This pointer is valid until meshlink_close() is called.
414 * @param reachable True if the node is reachable, false otherwise.
416 typedef void (*meshlink_node_status_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, bool reachable);
418 /// Set the node status callback.
419 /** This functions sets the callback that is called whenever another node's status changed.
420 * The callback is run in MeshLink's own thread.
421 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
422 * to hand the data over to the application's thread.
423 * The callback should also not block itself and return as quickly as possible.
425 * \memberof meshlink_handle
426 * @param mesh A handle which represents an instance of MeshLink.
427 * @param cb A pointer to the function which will be called when another node's status changes.
428 * If a NULL pointer is given, the callback will be disabled.
430 void meshlink_set_node_status_cb(struct meshlink_handle *mesh, meshlink_node_status_cb_t cb);
432 /// A callback reporting node path MTU changes.
433 /** @param mesh A handle which represents an instance of MeshLink.
434 * @param node A pointer to a struct meshlink_node describing the node whose status changed.
435 * This pointer is valid until meshlink_close() is called.
436 * @param pmtu The current path MTU to the node, or 0 if UDP communication is not (yet) possible.
438 typedef void (*meshlink_node_pmtu_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node, uint16_t pmtu);
440 /// Set the node extended status callback.
441 /** This functions sets the callback that is called whenever certain connectivity parameters for a node change.
442 * The callback is run in MeshLink's own thread.
443 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
444 * to hand the data over to the application's thread.
445 * The callback should also not block itself and return as quickly as possible.
447 * \memberof meshlink_handle
448 * @param mesh A handle which represents an instance of MeshLink.
449 * @param cb A pointer to the function which will be called when another node's extended status changes.
450 * If a NULL pointer is given, the callback will be disabled.
452 void meshlink_set_node_pmtu_cb(struct meshlink_handle *mesh, meshlink_node_pmtu_cb_t cb);
454 /// A callback reporting duplicate node detection.
455 /** @param mesh A handle which represents an instance of MeshLink.
456 * @param node A pointer to a struct meshlink_node describing the node which is duplicate.
457 * This pointer is valid until meshlink_close() is called.
459 typedef void (*meshlink_node_duplicate_cb_t)(struct meshlink_handle *mesh, struct meshlink_node *node);
461 /// Set the node duplicate callback.
462 /** This functions sets the callback that is called whenever a duplicate node is detected.
463 * The callback is run in MeshLink's own thread.
464 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
465 * to hand the data over to the application's thread.
466 * The callback should also not block itself and return as quickly as possible.
468 * \memberof meshlink_handle
469 * @param mesh A handle which represents an instance of MeshLink.
470 * @param cb A pointer to the function which will be called when a duplicate node is detected.
471 * If a NULL pointer is given, the callback will be disabled.
473 void meshlink_set_node_duplicate_cb(struct meshlink_handle *mesh, meshlink_node_duplicate_cb_t cb);
475 /// Severity of log messages generated by MeshLink.
477 MESHLINK_DEBUG, ///< Internal debugging messages. Only useful during application development.
478 MESHLINK_INFO, ///< Informational messages.
479 MESHLINK_WARNING, ///< Warnings which might indicate problems, but which are not real errors.
480 MESHLINK_ERROR, ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
481 MESHLINK_CRITICAL ///< Critical errors which cause MeshLink to fail completely.
482 } meshlink_log_level_t;
484 /// A callback for receiving log messages generated by MeshLink.
485 /** @param mesh A handle which represents an instance of MeshLink, or NULL.
486 * @param level An enum describing the severity level of the message.
487 * @param text A pointer to a nul-terminated C string containing the textual log message.
488 * This pointer is only valid for the duration of the callback.
489 * The application must not free() this pointer.
490 * The application should strdup() the text if it has to be available outside the callback.
492 typedef void (*meshlink_log_cb_t)(struct meshlink_handle *mesh, meshlink_log_level_t level, const char *text);
494 /// Set the log callback.
495 /** This functions sets the callback that is called whenever MeshLink has some information to log.
497 * The @a mesh parameter can either be a valid MeshLink handle, or NULL.
498 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
499 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
501 * If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
502 * It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
503 * to hand the data over to the application's thread.
504 * The callback should also not block itself and return as quickly as possible.
506 * The @a mesh parameter can either be a valid MeshLink handle, or NULL.
507 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
508 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
510 * \memberof meshlink_handle
511 * @param mesh A handle which represents an instance of MeshLink, or NULL.
512 * @param level An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
513 * @param cb A pointer to the function which will be called when another node sends data to the local node.
514 * If a NULL pointer is given, the callback will be disabled.
516 void meshlink_set_log_cb(struct meshlink_handle *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
518 /// A callback for receiving error conditions encountered by the MeshLink thread.
519 /** @param mesh A handle which represents an instance of MeshLink, or NULL.
520 * @param errno The error code describing what kind of error occurred.
522 typedef void (*meshlink_error_cb_t)(struct meshlink_handle *mesh, meshlink_errno_t meshlink_errno);
524 /// Set the error callback.
525 /** This functions sets the callback that is called whenever the MeshLink thread encounters a serious error.
527 * While most API functions report an error directly to the caller in case something went wrong,
528 * MeshLink also runs a background thread which can encounter error conditions.
529 * Most of them will be dealt with automatically, however there can be errors that will prevent MeshLink from
530 * working correctly. When the callback is called, it means that MeshLink is no longer functioning
531 * as expected. The application should then present an error message and shut down, or perform any other
532 * action it deems appropriate.
534 * The callback is run in MeshLink's own thread.
535 * It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
536 * to hand the data over to the application's thread.
537 * The callback should also not block itself and return as quickly as possible.
539 * Even though the callback signals a serious error inside MeshLink, all open handles are still valid,
540 * and the application should close handles in exactly the same it would have to do if the callback
541 * was not called. This must not be done inside the callback itself.
543 * \memberof meshlink_handle
544 * @param mesh A handle which represents an instance of MeshLink, or NULL.
545 * @param cb A pointer to the function which will be called when a serious error is encountered.
546 * If a NULL pointer is given, the callback will be disabled.
548 void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb);
550 /// Send data to another node.
551 /** This functions sends one packet of data to another node in the mesh.
552 * The packet is sent using UDP semantics, which means that
553 * the packet is sent as one unit and is received as one unit,
554 * and that there is no guarantee that the packet will arrive at the destination.
555 * Packets that are too big to be sent over the network as one unit might be dropped, and this function may return an error if this situation can be detected beforehand.
556 * The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
557 * The application should take care of getting an acknowledgement and retransmission if necessary.
559 * \memberof meshlink_node
560 * @param mesh A handle which represents an instance of MeshLink.
561 * @param destination A pointer to a struct meshlink_node describing the destination for the data.
562 * @param data A pointer to a buffer containing the data to be sent to the source.
563 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
564 * It is valid to specify a NULL pointer, but only if @a len is also 0.
565 * @param len The length of the data.
566 * @return This function will return true if MeshLink has queued the message for transmission, and false otherwise.
567 * A return value of true does not guarantee that the message will actually arrive at the destination.
569 bool meshlink_send(struct meshlink_handle *mesh, struct meshlink_node *destination, const void *data, size_t len) __attribute__((__warn_unused_result__));
571 /// Query the maximum packet size that can be sent to a node.
572 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
573 * The path MTU is a property of the path packets will take to the destination node over the Internet.
574 * It can be different for different destination nodes.
575 * and the path MTU can change at any point in time due to changes in the Internet.
576 * Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
578 * \memberof meshlink_node
579 * @param mesh A handle which represents an instance of MeshLink.
580 * @param destination A pointer to a struct meshlink_node describing the destination for the data.
582 * @return The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
583 * or a negative value in case of an error.
585 ssize_t meshlink_get_pmtu(struct meshlink_handle *mesh, struct meshlink_node *destination) __attribute__((__warn_unused_result__));
587 /// Get a handle for our own node.
588 /** This function returns a handle for the local node.
590 * \memberof meshlink_handle
591 * @param mesh A handle which represents an instance of MeshLink.
593 * @return A pointer to a struct meshlink_node which represents the local node.
594 * The pointer is guaranteed to be valid until meshlink_close() is called.
596 struct meshlink_node *meshlink_get_self(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
598 /// Get a handle for a specific node.
599 /** This function returns a handle for the node with the given name.
601 * \memberof meshlink_handle
602 * @param mesh A handle which represents an instance of MeshLink.
603 * @param name The name of the node for which a handle is requested.
604 * After this function returns, the application is free to overwrite or free @a name.
606 * @return A pointer to a struct meshlink_node which represents the requested node,
607 * or NULL if the requested node does not exist.
608 * The pointer is guaranteed to be valid until meshlink_close() is called.
610 struct meshlink_node *meshlink_get_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
612 /// Get a handle for a specific submesh.
613 /** This function returns a handle for the submesh with the given name.
615 * \memberof meshlink_handle
616 * @param mesh A handle which represents an instance of MeshLink.
617 * @param name The name of the submesh for which a handle is requested.
618 * After this function returns, the application is free to overwrite or free @a name.
620 * @return A pointer to a struct meshlink_submesh which represents the requested submesh,
621 * or NULL if the requested submesh does not exist.
622 * The pointer is guaranteed to be valid until meshlink_close() is called.
624 struct meshlink_submesh *meshlink_get_submesh(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
626 /// Get the fingerprint of a node's public key.
627 /** This function returns a fingerprint of the node's public key.
628 * It should be treated as an opaque blob.
630 * \memberof meshlink_node
631 * @param mesh A handle which represents an instance of MeshLink.
632 * @param node A pointer to a struct meshlink_node describing the node.
634 * @return A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
635 * The application should call free() after it is done using this string.
637 char *meshlink_get_fingerprint(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
639 /// Get a list of all nodes.
640 /** This function returns a list with handles for all known nodes.
642 * \memberof meshlink_handle
643 * @param mesh A handle which represents an instance of MeshLink.
644 * @param nodes A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
645 * The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
646 * The application is allowed to call free() on the array whenever it wishes.
647 * The pointers in the array are valid until meshlink_close() is called.
648 * @param nmemb A pointer to a variable holding the number of nodes that are stored in the array.
649 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
650 * The contents of this variable will be changed to reflect the new size of the array.
652 * @return A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
653 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
654 * If it is a new value, the old value of @a nodes should not be used anymore.
655 * If the new value is NULL, then the old array will have been freed by MeshLink.
657 struct meshlink_node **meshlink_get_all_nodes(struct meshlink_handle *mesh, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
659 /// Sign data using the local node's MeshLink key.
660 /** This function signs data using the local node's MeshLink key.
661 * The generated signature can be securely verified by other nodes.
663 * \memberof meshlink_handle
664 * @param mesh A handle which represents an instance of MeshLink.
665 * @param data A pointer to a buffer containing the data to be signed.
666 * @param len The length of the data to be signed.
667 * @param signature A pointer to a buffer where the signature will be stored.
668 * The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
669 * The signature is a binary blob, and is not nul-terminated.
670 * @param siglen The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
672 * @return This function returns true if the signature was correctly generated, false otherwise.
674 bool meshlink_sign(struct meshlink_handle *mesh, const void *data, size_t len, void *signature, size_t *siglen) __attribute__((__warn_unused_result__));
676 /// Get the list of all nodes by device class.
677 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
679 * \memberof meshlink_handle
680 * @param mesh A handle which represents an instance of MeshLink.
681 * @param devclass Device class of the nodes for which the list has to be obtained.
682 * @param nodes A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
683 * The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
684 * The application is allowed to call free() on the array whenever it wishes.
685 * The pointers in the array are valid until meshlink_close() is called.
686 * @param nmemb A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
687 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
688 * The contents of this variable will be changed to reflect the new size of the array.
690 * @return A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
691 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
692 * If it is a new value, the old value of @a nodes should not be used anymore.
693 * If the new value is NULL, then the old array will have been freed by MeshLink.
695 struct meshlink_node **meshlink_get_all_nodes_by_dev_class(struct meshlink_handle *mesh, dev_class_t devclass, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
697 /// Get the list of all nodes by Submesh.
698 /** This function returns a list with handles for all the nodes that matches with the given @a Submesh.
700 * \memberof meshlink_submesh
701 * @param mesh A handle which represents an instance of MeshLink.
702 * @param submesh Submesh handle of the nodes for which the list has to be obtained.
703 * @param nodes A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
704 * The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
705 * The application is allowed to call free() on the array whenever it wishes.
706 * The pointers in the array are valid until meshlink_close() is called.
707 * @param nmemb A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
708 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
709 * The contents of this variable will be changed to reflect the new size of the array.
711 * @return A pointer to an array containing pointers to all known nodes of the given Submesh, or NULL in case of an error.
712 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
713 * If it is a new value, the old value of @a nodes should not be used anymore.
714 * If the new value is NULL, then the old array will have been freed by MeshLink.
716 struct meshlink_node **meshlink_get_all_nodes_by_submesh(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
718 /// Get the list of all nodes by time they were last reachable.
719 /** This function returns a list with handles for all the nodes whose last known reachability time overlaps with the given time range.
720 * If the range includes 0, it will count nodes that were never online.
721 * If start is bigger than end, the result will be inverted.
723 * \memberof meshlink_handle
724 * @param mesh A handle which represents an instance of MeshLink.
725 * @param start Start time.
726 * @param end End time.
727 * @param nodes A pointer to a previously allocated array of pointers to struct meshlink_node, or NULL in which case MeshLink will allocate a new array.
728 * The application can supply an array it allocated itself with malloc, or the return value from the previous call to this function (which is the preferred way).
729 * The application is allowed to call free() on the array whenever it wishes.
730 * The pointers in the array are valid until meshlink_close() is called.
731 * @param nmemb A pointer to a variable holding the number of nodes that were reachable within the period given by @a start and @a end.
732 * In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
733 * The contents of this variable will be changed to reflect the new size of the array.
735 * @return A pointer to an array containing pointers to all known nodes that were reachable within the period given by @a start and @a end.
736 * If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
737 * If it is a new value, the old value of @a nodes should not be used anymore.
738 * If the new value is NULL, then the old array will have been freed by MeshLink.
740 struct meshlink_node **meshlink_get_all_nodes_by_last_reachable(struct meshlink_handle *mesh, time_t start, time_t end, struct meshlink_node **nodes, size_t *nmemb) __attribute__((__warn_unused_result__));
742 /// Get the node's device class.
743 /** This function returns the device class of the given node.
745 * \memberof meshlink_node
746 * @param mesh A handle which represents an instance of MeshLink.
747 * @param node A pointer to a struct meshlink_node describing the node.
749 * @return This function returns the device class of the @a node, or -1 in case of an error.
751 dev_class_t meshlink_get_node_dev_class(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
753 /// Get the node's submesh handle.
754 /** This function returns the submesh handle of the given node.
756 * \memberof meshlink_node
757 * @param mesh A handle which represents an instance of MeshLink.
758 * @param node A pointer to a struct meshlink_node describing the node.
760 * @return This function returns the submesh handle of the @a node, or NULL in case of an error.
762 struct meshlink_submesh *meshlink_get_node_submesh(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
764 /// Get a node's reachability status.
765 /** This function returns the current reachability of a given node, and the times of the last state changes.
766 * If a given state change never happened, the time returned will be 0.
768 * \memberof meshlink_node
769 * @param mesh A handle which represents an instance of MeshLink.
770 * @param node A pointer to a struct meshlink_node describing the node.
771 * @param last_reachable A pointer to a time_t variable that will be filled in with the last time the node became reachable.
772 * Pass NULL to not have anything written.
773 * @param last_unreachable A pointer to a time_t variable that will be filled in with the last time the node became unreachable.
774 * Pass NULL to not have anything written.
776 * @return This function returns true if the node is currently reachable, false otherwise.
778 bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlink_node *node, time_t *last_reachable, time_t *last_unreachable);
780 /// Verify the signature generated by another node of a piece of data.
781 /** This function verifies the signature that another node generated for a piece of data.
783 * \memberof meshlink_node
784 * @param mesh A handle which represents an instance of MeshLink.
785 * @param source A pointer to a struct meshlink_node describing the source of the signature.
786 * @param data A pointer to a buffer containing the data to be verified.
787 * @param len The length of the data to be verified.
788 * @param signature A pointer to a buffer where the signature is stored.
789 * @param siglen A pointer to a variable holding the size of the signature buffer.
790 * The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
792 * @return This function returns true if the signature is valid, false otherwise.
794 bool meshlink_verify(struct meshlink_handle *mesh, struct meshlink_node *source, const void *data, size_t len, const void *signature, size_t siglen) __attribute__((__warn_unused_result__));
796 /// Set the canonical Address for a node.
797 /** This function sets the canonical Address for a node.
798 * This address is stored permanently until it is changed by another call to this function,
799 * unlike other addresses associated with a node,
800 * such as those added with meshlink_hint_address() or addresses discovered at runtime.
802 * If a canonical Address is set for the local node,
803 * it will be used for the hostname part of generated invitation URLs.
805 * \memberof meshlink_node
806 * @param mesh A handle which represents an instance of MeshLink.
807 * @param node A pointer to a struct meshlink_node describing the node.
808 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
809 * @param port A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
810 * If it is NULL, the listening port's number will be used.
812 * @return This function returns true if the address was added, false otherwise.
814 bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port) __attribute__((__warn_unused_result__));
816 /// Add an invitation address for the local node.
817 /** This function adds an address for the local node, which will be used only for invitation URLs.
818 * This address is not stored permanently.
819 * Multiple addresses can be added using multiple calls to this function.
821 * \memberof meshlink_handle
822 * @param mesh A handle which represents an instance of MeshLink.
823 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
824 * @param port A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
825 * If it is NULL, the listening port's number will be used.
827 * @return This function returns true if the address was added, false otherwise.
829 bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *address, const char *port) __attribute__((__warn_unused_result__));
831 /// Clears all invitation address for the local node.
832 /** This function removes all addresses added with meshlink_add_invitation_address().
834 * \memberof meshlink_handle
835 * @param mesh A handle which represents an instance of MeshLink.
837 void meshlink_clear_invitation_addresses(struct meshlink_handle *mesh);
839 /// Add an Address for the local node.
840 /** This function adds an Address for the local node, which will be used for invitation URLs.
841 * @deprecated This function is deprecated, use meshlink_set_canonical_address() and/or meshlink_add_invitation_address().
843 * \memberof meshlink_handle
844 * @param mesh A handle which represents an instance of MeshLink.
845 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
847 * @return This function returns true if the address was added, false otherwise.
849 bool meshlink_add_address(struct meshlink_handle *mesh, const char *address) __attribute__((__warn_unused_result__, __deprecated__("use meshlink_set_canonical_address() and/or meshlink_add_invitation_address() instead")));
851 /// Try to discover the external address for the local node.
852 /** This function performs tries to discover the local node's external address
853 * by contacting the meshlink.io server. If a reverse lookup of the address works,
854 * the FQDN associated with the address will be returned.
856 * Please note that this is function only returns a single address,
857 * even if the local node might have more than one external address.
858 * In that case, there is no control over which address will be selected.
859 * Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
860 * there is no guarantee that the external address will be valid for an extended period of time.
862 * This function is blocking. It can take several seconds before it returns.
863 * There is no guarantee it will be able to resolve the external address.
864 * Failures might be because by temporary network outages.
866 * \memberof meshlink_handle
867 * @param mesh A handle which represents an instance of MeshLink.
869 * @return This function returns a pointer to a C string containing the discovered external address,
870 * or NULL if there was an error looking up the address.
871 * After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
873 char *meshlink_get_external_address(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
875 /// Try to discover the external address for the local node.
876 /** This function performs tries to discover the local node's external address
877 * by contacting the meshlink.io server. If a reverse lookup of the address works,
878 * the FQDN associated with the address will be returned.
880 * Please note that this is function only returns a single address,
881 * even if the local node might have more than one external address.
882 * In that case, there is no control over which address will be selected.
883 * Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
884 * there is no guarantee that the external address will be valid for an extended period of time.
886 * This function is blocking. It can take several seconds before it returns.
887 * There is no guarantee it will be able to resolve the external address.
888 * Failures might be because by temporary network outages.
890 * \memberof meshlink_handle
891 * @param mesh A handle which represents an instance of MeshLink.
892 * @param address_family The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
893 * this might return the external address for any working address family.
895 * @return This function returns a pointer to a C string containing the discovered external address,
896 * or NULL if there was an error looking up the address.
897 * After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
899 char *meshlink_get_external_address_for_family(struct meshlink_handle *mesh, int address_family) __attribute__((__warn_unused_result__));
901 /// Try to discover the local address for the local node.
902 /** This function performs tries to discover the address of the local interface used for outgoing connection.
904 * Please note that this is function only returns a single address,
905 * even if the interface might have more than one address.
906 * In that case, there is no control over which address will be selected.
907 * Also note that if you have a dynamic IP address,
908 * there is no guarantee that the local address will be valid for an extended period of time.
910 * This function will fail if it couldn't find a local address for the given address family.
911 * If hostname resolving is requested, this function may block for a few seconds.
913 * \memberof meshlink_handle
914 * @param mesh A handle which represents an instance of MeshLink.
915 * @param address_family The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
916 * this might return the local address for any working address family.
918 * @return This function returns a pointer to a C string containing the discovered local address,
919 * or NULL if there was an error looking up the address.
920 * After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
922 char *meshlink_get_local_address_for_family(struct meshlink_handle *mesh, int address_family) __attribute__((__warn_unused_result__));
924 /// Try to discover the external address for the local node, and add it to its list of addresses.
925 /** This function is equivalent to:
927 * meshlink_set_canonical_address(mesh, meshlink_get_self(mesh), meshlink_get_external_address(mesh), NULL);
929 * Read the description of meshlink_get_external_address() for the limitations of this function.
931 * \memberof meshlink_handle
932 * @param mesh A handle which represents an instance of MeshLink.
934 * @return This function returns true if the address was added, false otherwise.
936 bool meshlink_add_external_address(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
938 /// Get the network port used by the local node.
939 /** This function returns the network port that the local node is listening on.
941 * \memberof meshlink_handle
942 * @param mesh A handle which represents an instance of MeshLink.
944 * @return This function returns the port number, or -1 in case of an error.
946 int meshlink_get_port(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
948 /// Set the network port used by the local node.
949 /** This function sets the network port that the local node is listening on.
950 * It may only be called when the mesh is not running.
951 * If unsure, call meshlink_stop() before calling this function.
952 * Also note that if your node is already part of a mesh with other nodes,
953 * that the other nodes may no longer be able to initiate connections to the local node,
954 * since they will try to connect to the previously configured port.
956 * \memberof meshlink_handle
957 * @param mesh A handle which represents an instance of MeshLink.
958 * @param port The port number to listen on. This must be between 0 and 65535.
959 * If the port is set to 0, then MeshLink will listen on a port
960 * that is randomly assigned by the operating system every time meshlink_open() is called.
962 * @return This function returns true if the port was successfully changed
963 * to the desired port, false otherwise. If it returns false, there
964 * is no guarantee that MeshLink is listening on the old port.
967 bool meshlink_set_port(struct meshlink_handle *mesh, int port) __attribute__((__warn_unused_result__));
969 /// Set the timeout for invitations.
970 /** This function sets the timeout for invitations.
971 * Note that timeouts are only checked at the time a node tries to join using an invitation.
972 * The default timeout for invitations is 1 week.
974 * \memberof meshlink_handle
975 * @param mesh A handle which represents an instance of MeshLink.
976 * @param timeout The timeout for invitations in seconds.
978 void meshlink_set_invitation_timeout(struct meshlink_handle *mesh, int timeout);
980 /// Invite another node into the mesh.
981 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
982 * The generated invitation is a string containing a URL.
983 * This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
984 * The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
986 * \memberof meshlink_handle
987 * @param mesh A handle which represents an instance of MeshLink.
988 * @param submesh A handle which represents an instance of SubMesh.
989 * @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
990 * After this function returns, the application is free to overwrite or free @a name.
991 * @param flags A bitwise-or'd combination of flags that controls how the URL is generated.
993 * @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
994 * The application should call free() after it has finished using the URL.
996 char *meshlink_invite_ex(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name, uint32_t flags) __attribute__((__warn_unused_result__));
998 /// Invite another node into the mesh.
999 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
1000 * The generated invitation is a string containing a URL.
1001 * This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
1002 * The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
1004 * Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
1006 * \memberof meshlink_handle
1007 * @param mesh A handle which represents an instance of MeshLink.
1008 * @param submesh A handle which represents an instance of SubMesh.
1009 * @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
1010 * After this function returns, the application is free to overwrite or free @a name.
1012 * @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
1013 * The application should call free() after it has finished using the URL.
1015 char *meshlink_invite(struct meshlink_handle *mesh, struct meshlink_submesh *submesh, const char *name) __attribute__((__warn_unused_result__));
1017 /// Use an invitation to join a mesh.
1018 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
1019 * An invitation can only be used if the local node has never connected to other nodes before.
1020 * After a successfully accepted invitation, the name of the local node may have changed.
1022 * 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.
1024 * This function is blocking. It can take several seconds before it returns.
1025 * There is no guarantee it will perform a successful join.
1026 * Failures might be caused by temporary network outages, or by the invitation having expired.
1028 * \memberof meshlink_handle
1029 * @param mesh A handle which represents an instance of MeshLink.
1030 * @param invitation A nul-terminated C string containing the invitation URL.
1031 * After this function returns, the application is free to overwrite or free @a invitation.
1033 * @return This function returns true if the local node joined the mesh it was invited to, false otherwise.
1035 bool meshlink_join(struct meshlink_handle *mesh, const char *invitation) __attribute__((__warn_unused_result__));
1037 /// Export the local node's key and addresses.
1038 /** This function generates a string that contains the local node's public key and one or more IP addresses.
1039 * The application can pass it in some way to another node, which can then import it,
1040 * granting the local node access to the other node's mesh.
1041 * The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
1043 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
1045 * \memberof meshlink_handle
1046 * @param mesh A handle which represents an instance of MeshLink.
1048 * @return This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
1049 * The application should call free() after it has finished using this string.
1051 char *meshlink_export(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
1053 /// Import another node's key and addresses.
1054 /** This function accepts a string containing the exported public key and addresses of another node.
1055 * By importing this data, the local node grants the other node access to its mesh.
1056 * The application should make sure that the data it imports is really coming from the node it wants to import,
1058 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
1060 * \memberof meshlink_handle
1061 * @param mesh A handle which represents an instance of MeshLink.
1062 * @param data A nul-terminated C string containing the other node's exported key and addresses.
1063 * After this function returns, the application is free to overwrite or free @a data.
1065 * @return This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
1067 bool meshlink_import(struct meshlink_handle *mesh, const char *data) __attribute__((__warn_unused_result__));
1069 /// Forget any information about a node.
1070 /** This function allows the local node to forget any information it has about a node,
1071 * and if possible will remove any data it has stored on disk about the node.
1073 * Any open channels to this node must be closed before calling this function.
1075 * After this call returns, the node handle is invalid and may no longer be used, regardless
1076 * of the return value of this call.
1078 * Note that this function does not prevent MeshLink from actually forgetting about a node,
1079 * or re-learning information about a node at a later point in time. It is merely a hint that
1080 * the application does not care about this node anymore and that any resources kept could be
1083 * \memberof meshlink_node
1084 * @param mesh A handle which represents an instance of MeshLink.
1085 * @param node A pointer to a struct meshlink_node describing the node to be forgotten.
1087 * @return This function returns true if all currently known data about the node has been forgotten, false otherwise.
1089 bool meshlink_forget_node(struct meshlink_handle *mesh, struct meshlink_node *node);
1091 /// Blacklist a node from the mesh.
1092 /** This function causes the local node to blacklist another node.
1093 * The local node will drop any existing connections to that node,
1094 * and will not send data to it nor accept any data received from it any more.
1096 * \memberof meshlink_node
1097 * @param mesh A handle which represents an instance of MeshLink.
1098 * @param node A pointer to a struct meshlink_node describing the node to be blacklisted.
1100 * @return This function returns true if the node has been blacklisted, false otherwise.
1102 bool meshlink_blacklist(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
1104 /// Blacklist a node from the mesh by name.
1105 /** This function causes the local node to blacklist another node by name.
1106 * The local node will drop any existing connections to that node,
1107 * and will not send data to it nor accept any data received from it any more.
1109 * If no node by the given name is known, it is created.
1111 * \memberof meshlink_node
1112 * @param mesh A handle which represents an instance of MeshLink.
1113 * @param name The name of the node to blacklist.
1115 * @return This function returns true if the node has been blacklisted, false otherwise.
1117 bool meshlink_blacklist_by_name(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
1119 /// Whitelist a node on the mesh.
1120 /** This function causes the local node to whitelist a previously blacklisted node.
1121 * The local node will allow connections to and from that node,
1122 * and will send data to it and accept any data received from it.
1124 * \memberof meshlink_node
1125 * @param mesh A handle which represents an instance of MeshLink.
1126 * @param node A pointer to a struct meshlink_node describing the node to be whitelisted.
1128 * @return This function returns true if the node has been whitelisted, false otherwise.
1130 bool meshlink_whitelist(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__));
1132 /// Whitelist a node on the mesh by name.
1133 /** This function causes the local node to whitelist a node by name.
1134 * The local node will allow connections to and from that node,
1135 * and will send data to it and accept any data received from it.
1137 * If no node by the given name is known, it is created.
1138 * This is useful if new nodes are blacklisted by default.
1140 * \memberof meshlink_node
1141 * @param mesh A handle which represents an instance of MeshLink.
1142 * @param name The name of the node to whitelist.
1144 * @return This function returns true if the node has been whitelisted, false otherwise.
1146 bool meshlink_whitelist_by_name(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
1148 /// Set whether new nodes are blacklisted by default.
1149 /** This function sets the blacklist behaviour for newly discovered nodes.
1150 * If set to true, new nodes will be automatically blacklisted.
1151 * If set to false, which is the default, new nodes are automatically whitelisted.
1152 * The whitelist/blacklist status of a node may be changed afterwards with the
1153 * meshlink_whitelist() and meshlink_blacklist() functions.
1155 * \memberof meshlink_handle
1156 * @param mesh A handle which represents an instance of MeshLink.
1157 * @param blacklist True if new nodes are to be blacklisted, false if whitelisted.
1159 void meshlink_set_default_blacklist(struct meshlink_handle *mesh, bool blacklist);
1161 /// A callback for accepting incoming channels.
1162 /** This function is called whenever a remote node wants to open a channel to the local node.
1163 * The application then has to decide whether to accept or reject this channel.
1165 * The callback is run in MeshLink's own thread.
1166 * It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
1167 * to hand any data over to the application's thread.
1169 * @param mesh A handle which represents an instance of MeshLink.
1170 * @param channel A handle for the incoming channel.
1171 * If the application accepts the incoming channel by returning true,
1172 * then this handle is valid until meshlink_channel_close() is called on it.
1173 * If the application rejects the incoming channel by returning false,
1174 * then this handle is invalid after the callback returns
1175 * (the callback does not need to call meshlink_channel_close() itself in this case).
1176 * @param port The port number the peer wishes to connect to.
1177 * @param data A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
1178 * The pointer is only valid during the lifetime of the callback.
1179 * The callback should mempcy() the data if it needs to be available outside the callback.
1180 * @param len The length of the data, or 0 in case no data has been received yet. (Not yet used.)
1182 * @return This function should return true if the application accepts the incoming channel, false otherwise.
1183 * If returning false, the channel is invalid and may not be used anymore.
1185 typedef bool (*meshlink_channel_accept_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, uint16_t port, const void *data, size_t len);
1187 /// A callback for receiving data from a channel.
1188 /** This function is called whenever data is received from a remote node on a channel.
1190 * This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
1191 * In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
1192 * In any case, the @a channel handle will still be valid until the application calls meshlink_close().
1194 * @param mesh A handle which represents an instance of MeshLink.
1195 * @param channel A handle for the channel.
1196 * @param data A pointer to a buffer containing data sent by the source, or NULL in case of an error.
1197 * The pointer is only valid during the lifetime of the callback.
1198 * The callback should mempcy() the data if it needs to be available outside the callback.
1199 * @param len The length of the data, or 0 in case of an error.
1201 typedef void (*meshlink_channel_receive_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len);
1203 /// A callback informing the application when data can be sent on a channel.
1204 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
1206 * @param mesh A handle which represents an instance of MeshLink.
1207 * @param channel A handle for the channel.
1208 * @param len The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
1209 * or 0 in case of an error.
1211 typedef void (*meshlink_channel_poll_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t len);
1213 /// Set the accept callback.
1214 /** This functions sets the callback that is called whenever another node sends data to the local node.
1215 * The callback is run in MeshLink's own thread.
1216 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1217 * to hand the data over to the application's thread.
1218 * The callback should also not block itself and return as quickly as possible.
1220 * If no accept callback is set, incoming channels are rejected.
1222 * \memberof meshlink_handle
1223 * @param mesh A handle which represents an instance of MeshLink.
1224 * @param cb A pointer to the function which will be called when another node sends data to the local node.
1225 * If a NULL pointer is given, the callback will be disabled.
1227 void meshlink_set_channel_accept_cb(struct meshlink_handle *mesh, meshlink_channel_accept_cb_t cb);
1229 /// Set the receive callback.
1230 /** This functions sets the callback that is called whenever another node sends data to the local node.
1231 * The callback is run in MeshLink's own thread.
1232 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1233 * to hand the data over to the application's thread.
1234 * The callback should also not block itself and return as quickly as possible.
1236 * \memberof meshlink_channel
1237 * @param mesh A handle which represents an instance of MeshLink.
1238 * @param channel A handle for the channel.
1239 * @param cb A pointer to the function which will be called when another node sends data to the local node.
1240 * If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
1242 void meshlink_set_channel_receive_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_receive_cb_t cb);
1244 /// Set the poll callback.
1245 /** This functions sets the callback that is called whenever data can be sent to another node.
1246 * The callback is run in MeshLink's own thread.
1247 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
1248 * to pass data to or from the application's thread.
1249 * The callback should also not block itself and return as quickly as possible.
1251 * \memberof meshlink_channel
1252 * @param mesh A handle which represents an instance of MeshLink.
1253 * @param channel A handle for the channel.
1254 * @param cb A pointer to the function which will be called when data can be sent to another node.
1255 * If a NULL pointer is given, the callback will be disabled.
1257 void meshlink_set_channel_poll_cb(struct meshlink_handle *mesh, struct meshlink_channel *channel, meshlink_channel_poll_cb_t cb);
1259 /// Set the send buffer size of a channel.
1260 /** This function sets the desired size of the send buffer.
1261 * The default size is 128 kB.
1263 * \memberof meshlink_channel
1264 * @param mesh A handle which represents an instance of MeshLink.
1265 * @param channel A handle for the channel.
1266 * @param size The desired size for the send buffer.
1267 * If a NULL pointer is given, the callback will be disabled.
1269 void meshlink_set_channel_sndbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1271 /// Set the receive buffer size of a channel.
1272 /** This function sets the desired size of the receive buffer.
1273 * The default size is 128 kB.
1275 * \memberof meshlink_channel
1276 * @param mesh A handle which represents an instance of MeshLink.
1277 * @param channel A handle for the channel.
1278 * @param size The desired size for the send buffer.
1279 * If a NULL pointer is given, the callback will be disabled.
1281 void meshlink_set_channel_rcvbuf(struct meshlink_handle *mesh, struct meshlink_channel *channel, size_t size);
1283 /// Open a reliable stream channel to another node.
1284 /** This function is called whenever a remote node wants to open a channel to the local node.
1285 * The application then has to decide whether to accept or reject this channel.
1287 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1288 * When the application does no longer need to use this channel, it must call meshlink_close()
1289 * to free its resources.
1291 * \memberof meshlink_node
1292 * @param mesh A handle which represents an instance of MeshLink.
1293 * @param node The node to which this channel is being initiated.
1294 * @param port The port number the peer wishes to connect to.
1295 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
1296 * The pointer may be NULL, in which case incoming data is ignored.
1297 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1298 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
1299 * If len is 0, the data pointer is copied into the channel's priv member.
1300 * @param len The length of the data, or 0 if there is no data to send.
1301 * @param flags A bitwise-or'd combination of flags that set the semantics for this channel.
1303 * @return A handle for the channel, or NULL in case of an error.
1304 * The handle is valid until meshlink_channel_close() is called.
1306 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__));
1308 /// Open a reliable stream channel to another node.
1309 /** This function is called whenever a remote node wants to open a channel to the local node.
1310 * The application then has to decide whether to accept or reject this channel.
1312 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
1313 * When the application does no longer need to use this channel, it must call meshlink_close()
1314 * to free its resources.
1316 * Calling this function is equivalent to calling meshlink_channel_open_ex()
1317 * with the flags set to MESHLINK_CHANNEL_TCP.
1319 * \memberof meshlink_node
1320 * @param mesh A handle which represents an instance of MeshLink.
1321 * @param node The node to which this channel is being initiated.
1322 * @param port The port number the peer wishes to connect to.
1323 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
1324 * The pointer may be NULL, in which case incoming data is ignored.
1325 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
1326 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
1327 * @param len The length of the data, or 0 if there is no data to send.
1328 * If len is 0, the data pointer is copied into the channel's priv member.
1330 * @return A handle for the channel, or NULL in case of an error.
1331 * The handle is valid until meshlink_channel_close() is called.
1333 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__));
1335 /// Partially close a reliable stream channel.
1336 /** This shuts down the read or write side of a channel, or both, without closing the handle.
1337 * It can be used to inform the remote node that the local node has finished sending all data on the channel,
1338 * but still allows waiting for incoming data from the remote node.
1340 * Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
1342 * \memberof meshlink_channel
1343 * @param mesh A handle which represents an instance of MeshLink.
1344 * @param channel A handle for the channel.
1345 * @param direction Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
1347 void meshlink_channel_shutdown(struct meshlink_handle *mesh, struct meshlink_channel *channel, int direction);
1349 /// Close a reliable stream channel.
1350 /** This informs the remote node that the local node has finished sending all data on the channel.
1351 * It also causes the local node to stop accepting incoming data from the remote node.
1352 * It will free the struct meshlink_channel and all associated resources.
1353 * Afterwards, the channel handle is invalid and must not be used any more.
1355 * It is allowed to call this function at any time on a valid handle, even inside callback functions.
1356 * If called with a valid handle, this function always succeeds, otherwise the result is undefined.
1358 * \memberof meshlink_channel
1359 * @param mesh A handle which represents an instance of MeshLink.
1360 * @param channel A handle for the channel.
1362 void meshlink_channel_close(struct meshlink_handle *mesh, struct meshlink_channel *channel);
1364 /// Transmit data on a channel
1365 /** This queues data to send to the remote node.
1367 * \memberof meshlink_channel
1368 * @param mesh A handle which represents an instance of MeshLink.
1369 * @param channel A handle for the channel.
1370 * @param data A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1371 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
1372 * @param len The length of the data, or 0 if there is no data to send.
1374 * @return The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1375 * If MESHLINK_CHANNEL_NO_PARTIAL is set, then the result will either be len,
1376 * 0 if the buffer is currently too full, or -1 if len is too big even for an empty buffer.
1378 ssize_t meshlink_channel_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len) __attribute__((__warn_unused_result__));
1380 /// A callback for cleaning up buffers submitted for asynchronous I/O.
1381 /** This callbacks signals that MeshLink has finished using this buffer.
1382 * The ownership of the buffer is now back into the application's hands.
1384 * @param mesh A handle which represents an instance of MeshLink.
1385 * @param channel A handle for the channel which used this buffer.
1386 * @param data A pointer to a buffer containing the enqueued data.
1387 * @param len The length of the buffer.
1388 * @param priv A private pointer which was set by the application when submitting the buffer.
1390 typedef void (*meshlink_aio_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, void *priv);
1392 /// A callback for asynchronous I/O to and from filedescriptors.
1393 /** This callbacks signals that MeshLink has finished using this filedescriptor.
1395 * @param mesh A handle which represents an instance of MeshLink.
1396 * @param channel A handle for the channel which used this filedescriptor.
1397 * @param fd The filedescriptor that was used.
1398 * @param len The length of the data that was successfully sent or received.
1399 * @param priv A private pointer which was set by the application when submitting the buffer.
1401 typedef void (*meshlink_aio_fd_cb_t)(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, void *priv);
1403 /// Transmit data on a channel asynchronously
1404 /** This registers a buffer that will be used to send data to the remote node.
1405 * Multiple buffers can be registered, in which case data will be sent in the order the buffers were registered.
1406 * While there are still buffers with unsent data, the poll callback will not be called.
1408 * \memberof meshlink_channel
1409 * @param mesh A handle which represents an instance of MeshLink.
1410 * @param channel A handle for the channel.
1411 * @param data A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1412 * After meshlink_channel_aio_send() returns, the buffer may not be modified or freed by the application
1413 * until the callback routine is called.
1414 * @param len The length of the data, or 0 if there is no data to send.
1415 * @param cb A pointer to the function which will be called when MeshLink has finished using the buffer.
1416 * @param priv A private pointer which is passed unchanged to the callback.
1418 * @return True if the buffer was enqueued, false otherwise.
1420 bool meshlink_channel_aio_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv) __attribute__((__warn_unused_result__));
1422 /// Transmit data on a channel asynchronously from a filedescriptor
1423 /** This will read up to the specified length number of bytes from the given filedescriptor, and send it over the channel.
1424 * The callback may be returned early if there is an error reading from the filedescriptor.
1425 * While there is still with unsent data, the poll callback will not be called.
1427 * \memberof meshlink_channel
1428 * @param mesh A handle which represents an instance of MeshLink.
1429 * @param channel A handle for the channel.
1430 * @param fd A file descriptor from which data will be read.
1431 * @param len The length of the data, or 0 if there is no data to send.
1432 * @param cb A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1433 * @param priv A private pointer which is passed unchanged to the callback.
1435 * @return True if the buffer was enqueued, false otherwise.
1437 bool meshlink_channel_aio_fd_send(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv) __attribute__((__warn_unused_result__));
1439 /// Receive data on a channel asynchronously
1440 /** This registers a buffer that will be filled with incoming channel data.
1441 * Multiple buffers can be registered, in which case data will be received in the order the buffers were registered.
1442 * While there are still buffers that have not been filled, the receive callback will not be called.
1444 * \memberof meshlink_channel
1445 * @param mesh A handle which represents an instance of MeshLink.
1446 * @param channel A handle for the channel.
1447 * @param data A pointer to a buffer that will be filled with incoming data.
1448 * After meshlink_channel_aio_receive() returns, the buffer may not be modified or freed by the application
1449 * until the callback routine is called.
1450 * @param len The length of the data.
1451 * @param cb A pointer to the function which will be called when MeshLink has finished using the buffer.
1452 * @param priv A private pointer which is passed unchanged to the callback.
1454 * @return True if the buffer was enqueued, false otherwise.
1456 bool meshlink_channel_aio_receive(struct meshlink_handle *mesh, struct meshlink_channel *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv) __attribute__((__warn_unused_result__));
1458 /// Receive data on a channel asynchronously and send it to a filedescriptor
1459 /** This will read up to the specified length number of bytes from the channel, and send it to the filedescriptor.
1460 * The callback may be returned early if there is an error writing to the filedescriptor.
1461 * While there is still unread data, the receive callback will not be called.
1463 * \memberof meshlink_channel
1464 * @param mesh A handle which represents an instance of MeshLink.
1465 * @param channel A handle for the channel.
1466 * @param fd A file descriptor to which data will be written.
1467 * @param len The length of the data.
1468 * @param cb A pointer to the function which will be called when MeshLink has finished using the filedescriptor.
1469 * @param priv A private pointer which was set by the application when submitting the buffer.
1471 * @return True if the buffer was enqueued, false otherwise.
1473 bool meshlink_channel_aio_fd_receive(struct meshlink_handle *mesh, struct meshlink_channel *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv) __attribute__((__warn_unused_result__));
1475 /// Get channel flags.
1476 /** This returns the flags used when opening this channel.
1478 * \memberof meshlink_channel
1479 * @param mesh A handle which represents an instance of MeshLink.
1480 * @param channel A handle for the channel.
1482 * @return The flags set for this channel.
1484 uint32_t meshlink_channel_get_flags(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1486 /// Get the amount of bytes in the send buffer.
1487 /** This returns the amount of bytes in the send buffer.
1488 * These bytes have not been received by the peer yet.
1490 * \memberof meshlink_channel
1491 * @param mesh A handle which represents an instance of MeshLink.
1492 * @param channel A handle for the channel.
1494 * @return The amount of un-ACKed bytes in the send buffer.
1496 size_t meshlink_channel_get_sendq(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1498 /// Get the amount of bytes in the receive buffer.
1499 /** This returns the amount of bytes in the receive buffer.
1500 * These bytes have not been processed by the application yet.
1502 * \memberof meshlink_channel
1503 * @param mesh A handle which represents an instance of MeshLink.
1504 * @param channel A handle for the channel.
1506 * @return The amount of bytes in the receive buffer.
1508 size_t meshlink_channel_get_recvq(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1510 /// Get the maximum segment size of a channel.
1511 /** This returns the amount of bytes that can be sent at once for channels with UDP semantics.
1513 * \memberof meshlink_channel
1514 * @param mesh A handle which represents an instance of MeshLink.
1515 * @param channel A handle for the channel.
1517 * @return The amount of bytes in the receive buffer.
1519 size_t meshlink_channel_get_mss(struct meshlink_handle *mesh, struct meshlink_channel *channel) __attribute__((__warn_unused_result__));
1521 /// Set the connection timeout used for channels to the given node.
1522 /** This sets the timeout after which unresponsive channels will be reported as closed.
1523 * The timeout is set for all current and future channels to the given node.
1525 * \memberof meshlink_node
1526 * @param mesh A handle which represents an instance of MeshLink.
1527 * @param node A pointer to a struct meshlink_node describing the node to set the channel connection timeout for.
1528 * @param timeout The timeout in seconds after which unresponsive channels will be reported as closed.
1529 * The default is 60 seconds.
1531 void meshlink_set_node_channel_timeout(struct meshlink_handle *mesh, struct meshlink_node *node, int timeout);
1533 /// Hint that a hostname may be found at an address
1534 /** This function indicates to meshlink that the given hostname is likely found
1535 * at the given IP address and port.
1537 * \memberof meshlink_node
1538 * @param mesh A handle which represents an instance of MeshLink.
1539 * @param node A pointer to a struct meshlink_node describing the node to add the address hint for.
1540 * @param addr The IP address and port which should be tried for the
1541 * given hostname. The caller is free to overwrite or free
1542 * this memory once meshlink returns.
1544 void meshlink_hint_address(struct meshlink_handle *mesh, struct meshlink_node *node, const struct sockaddr *addr);
1546 /// Enable or disable zeroconf discovery of local peers
1547 /** This controls whether zeroconf discovery using the Catta library will be
1548 * enabled to search for peers on the local network. By default, it is enabled.
1550 * \memberof meshlink_handle
1551 * @param mesh A handle which represents an instance of MeshLink.
1552 * @param enable Set to true to enable discovery, false to disable.
1554 void meshlink_enable_discovery(struct meshlink_handle *mesh, bool enable);
1556 /// Performs key rotation for an encrypted storage
1557 /** This rotates the (master) key for an encrypted storage and discards the old key
1558 * if the call succeeded. This is an atomic call.
1560 * \memberof meshlink_handle
1561 * @param mesh A handle which represents an instance of MeshLink.
1562 * @param key A pointer to the new key used to encrypt storage.
1563 * @param keylen The length of the new key in bytes.
1565 * @return This function returns true if the key rotation for the encrypted storage succeeds, false otherwise.
1567 bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const void *key, size_t keylen) __attribute__((__warn_unused_result__));
1569 /// Set device class timeouts
1570 /** This sets the ping interval and timeout for a given device class.
1572 * \memberof meshlink_handle
1573 * @param mesh A handle which represents an instance of MeshLink.
1574 * @param devclass The device class to update
1575 * @param pinginterval The interval between keepalive packets, in seconds. The default is 60.
1576 * @param pingtimeout The required time within which a peer should respond, in seconds. The default is 5.
1577 * The timeout must be smaller than the interval.
1579 void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout);
1581 /// Set device class fast retry period
1582 /** This sets the fast retry period for a given device class.
1583 * During this period after the last time the mesh becomes unreachable, connections are tried once a second.
1585 * \memberof meshlink_handle
1586 * @param mesh A handle which represents an instance of MeshLink.
1587 * @param devclass The device class to update
1588 * @param fast_retry_period The period during which fast connection retries are done. The default is 0.
1590 void meshlink_set_dev_class_fast_retry_period(struct meshlink_handle *mesh, dev_class_t devclass, int fast_retry_period);
1592 /// Set device class maximum timeout
1593 /** This sets the maximum timeout for outgoing connection retries for a given device class.
1595 * \memberof meshlink_handle
1596 * @param mesh A handle which represents an instance of MeshLink.
1597 * @param devclass The device class to update
1598 * @param maxtimeout The maximum timeout between reconnection attempts, in seconds. The default is 900.
1600 void meshlink_set_dev_class_maxtimeout(struct meshlink_handle *mesh, dev_class_t devclass, int maxtimeout);
1602 /// Reset all connection timers
1603 /** This resets all timers related to connections, causing pending outgoing connections to be retried immediately.
1604 * It also sends keepalive packets on all active connections immediately.
1606 * \memberof meshlink_handle
1607 * @param mesh A handle which represents an instance of MeshLink.
1609 void meshlink_reset_timers(struct meshlink_handle *mesh);
1611 /// Set which order invitations are committed
1612 /** This determines in which order configuration files are written to disk during an invitation.
1613 * By default, the invitee saves the configuration to disk first, then the inviter.
1614 * By calling this function with @a inviter_commits_first set to true, the order is reversed.
1616 * \memberof meshlink_handle
1617 * @param mesh A handle which represents an instance of MeshLink.
1618 * @param inviter_commits_first If true, then the node that invited a peer will commit data to disk first.
1620 void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first);
1622 /// Set the URL used to discover the host's external address
1623 /** For generating invitation URLs, MeshLink can look up the externally visible address of the local node.
1624 * It does so by querying an external service. By default, this is http://meshlink.io/host.cgi.
1625 * Only URLs starting with http:// are supported.
1627 * \memberof meshlink_handle
1628 * @param mesh A handle which represents an instance of MeshLink.
1629 * @param url The URL to use for external address queries, or NULL to revert back to the default URL.
1631 void meshlink_set_external_address_discovery_url(struct meshlink_handle *mesh, const char *url);
1633 /// Set the scheduling granularity of the application
1634 /** This should be set to the effective scheduling granularity for the application.
1635 * This depends on the scheduling granularity of the operating system, the application's
1636 * process priority and whether it is running as realtime or not.
1637 * The default value is 10000 (10 milliseconds).
1639 * \memberof meshlink_handle
1640 * @param mesh A handle which represents an instance of MeshLink.
1641 * @param granularity The scheduling granularity of the application in microseconds.
1643 void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity);