5 meshlink.h -- MeshLink API
6 Copyright (C) 2014-2018 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 coud 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
78 DEV_CLASS_BACKBONE = 0,
79 DEV_CLASS_STATIONARY = 1,
80 DEV_CLASS_PORTABLE = 2,
81 DEV_CLASS_UNKNOWN = 3,
86 static const uint32_t MESHLINK_INVITE_LOCAL = 1; // Only use local addresses in the URL
87 static const uint32_t MESHLINK_INVITE_PUBLIC = 2; // Only use public or canonical addresses in the URL
88 static const uint32_t MESHLINK_INVITE_IPV4 = 4; // Only use IPv4 addresses in the URL
89 static const uint32_t MESHLINK_INVITE_IPV6 = 8; // Only use IPv6 addresses in the URL
90 static const uint32_t MESHLINK_INVITE_NUMERIC = 16; // Don't look up hostnames
93 static const uint32_t MESHLINK_CHANNEL_RELIABLE = 1; // Data is retransmitted when packets are lost.
94 static const uint32_t MESHLINK_CHANNEL_ORDERED = 2; // Data is delivered in-order to the application.
95 static const uint32_t MESHLINK_CHANNEL_FRAMED = 4; // Data is delivered in chunks of the same length as data was originally sent.
96 static const uint32_t MESHLINK_CHANNEL_DROP_LATE = 8; // When packets are reordered, late packets are ignored.
97 static const uint32_t MESHLINK_CHANNEL_TCP = 3; // Select TCP semantics.
98 static const uint32_t MESHLINK_CHANNEL_UDP = 0; // Select UDP semantics.
100 /// A variable holding the last encountered error from MeshLink.
101 /** This is a thread local variable that contains the error code of the most recent error
102 * encountered by a MeshLink API function called in the current thread.
103 * The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
104 * if a function returned succesfully.
106 extern __thread meshlink_errno_t meshlink_errno;
108 #ifndef MESHLINK_INTERNAL_H
110 struct meshlink_handle {
111 const char *const name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink.
112 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
115 struct meshlink_node {
116 const char *const name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
117 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
120 struct meshlink_submesh {
121 const char *const name; ///< Textual name of this Sub-Mesh. It is stored in a nul-terminated C string, which is allocated by MeshLink.
122 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
125 struct meshlink_channel {
126 struct meshlink_node *const node; ///< Pointer to the peer of this channel.
127 void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
130 struct meshlink_open_params {
131 /* This is an opaque struct, all parameters must be set using the corresponding meshlink_open_params_*() functions. */
134 #endif // MESHLINK_INTERNAL_H
136 /// Get the text for the given MeshLink error code.
137 /** This function returns a pointer to the string containing the description of the given error code.
139 * @param err An error code returned by MeshLink.
141 * @return A pointer to a string containing the description of the error code.
142 * The pointer is to static storage that is valid for the lifetime of the application.
143 * This function will always return a valid pointer, even if an invalid error code has been passed.
145 extern const char *meshlink_strerror(meshlink_errno_t err);
147 /// Create a new meshlink_open_params_t struct.
148 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
149 * The resulting struct may be reused for multiple calls to meshlink_open_ex().
150 * After the last use, the application must free this struct using meshlink_open_params_free().
152 * @param confbase The directory in which MeshLink will store its configuration files.
153 * After the function returns, the application is free to overwrite or free @a confbase @a.
154 * @param name The name which this instance of the application will use in the mesh.
155 * After the function returns, the application is free to overwrite or free @a name @a.
156 * @param appname The application name which will be used in the mesh.
157 * After the function returns, the application is free to overwrite or free @a name @a.
158 * @param devclass The device class which will be used in the mesh.
160 * @return A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
161 * The pointer is valid until meshlink_open_params_free() is called.
163 extern meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
165 /// Free a meshlink_open_params_t struct.
166 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
168 * @param params A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
170 extern void meshlink_open_params_free(meshlink_open_params_t *params);
172 /// Open or create a MeshLink instance.
173 /** This function opens or creates a MeshLink instance.
174 * All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
175 * which must have been initialized earlier by the application.
177 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
178 * When the application does no longer need to use this handle, it must call meshlink_close() to
179 * free its resources.
181 * This function does not start any network I/O yet. The application should
182 * first set callbacks, and then call meshlink_start().
184 * @param params A pointer to a meshlink_open_params_t which must be filled in by the application.
185 * After the function returns, the application is free to reuse or free @a params @a.
187 * @return A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
188 * The pointer is valid until meshlink_close() is called.
190 extern meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params);
192 /// Open or create a MeshLink instance.
193 /** This function opens or creates a MeshLink instance.
194 * The state is stored in the configuration directory passed in the variable @a confbase @a.
195 * If the configuration directory does not exist yet, for example when it is the first time
196 * this instance is opened, the configuration directory will be automatically created and initialized.
197 * However, the parent directory should already exist, otherwise an error will be returned.
199 * The name given should be a unique identifier for this instance.
201 * This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
202 * When the application does no longer need to use this handle, it must call meshlink_close() to
203 * free its resources.
205 * This function does not start any network I/O yet. The application should
206 * first set callbacks, and then call meshlink_start().
208 * @param confbase The directory in which MeshLink will store its configuration files.
209 * After the function returns, the application is free to overwrite or free @a confbase @a.
210 * @param name The name which this instance of the application will use in the mesh.
211 * After the function returns, the application is free to overwrite or free @a name @a.
212 * @param appname The application name which will be used in the mesh.
213 * After the function returns, the application is free to overwrite or free @a name @a.
214 * @param devclass The device class which will be used in the mesh.
216 * @return A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
217 * The pointer is valid until meshlink_close() is called.
219 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
222 /** This function causes MeshLink to open a new Sub-Mesh network
223 * create a new thread, which will handle all network I/O.
225 * It is allowed to call this function even if MeshLink is already started, in which case it will return true.
227 * @param mesh A handle which represents an instance of MeshLink.
229 * @param submesh Name of the new Sub-Mesh to create.
231 * @return A pointer to a meshlink_submesh_t which represents this instance of SubMesh, or NULL in case of an error.
232 * The pointer is valid until meshlink_close() is called.
234 meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh);
237 /** This function causes MeshLink to open network sockets, make outgoing connections, and
238 * create a new thread, which will handle all network I/O.
240 * It is allowed to call this function even if MeshLink is already started, in which case it will return true.
242 * @param mesh A handle which represents an instance of MeshLink.
244 * @return This function will return true if MeshLink has succesfully started, false otherwise.
246 extern bool meshlink_start(meshlink_handle_t *mesh);
249 /** This function causes MeshLink to disconnect from all other nodes,
250 * close all sockets, and shut down its own thread.
252 * This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
253 * Channels that are still open will remain valid, but any communication via channels will stop as well.
255 * @param mesh A handle which represents an instance of MeshLink.
257 extern void meshlink_stop(meshlink_handle_t *mesh);
259 /// Close the MeshLink handle.
260 /** This function calls meshlink_stop() if necessary,
261 * and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
262 * Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
264 * It is allowed to call this function at any time on a valid handle, except inside callback functions.
265 * If called at a proper time with a valid handle, this function always succeeds.
266 * If called within a callback or with an invalid handle, the result is undefined.
268 * @param mesh A handle which represents an instance of MeshLink.
270 extern void meshlink_close(meshlink_handle_t *mesh);
272 /// Destroy a MeshLink instance.
273 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
274 * does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
277 * @param confbase The directory in which MeshLink stores its configuration files.
278 * After the function returns, the application is free to overwrite or free @a confbase @a.
280 * @return This function will return true if the MeshLink instance was succesfully destroyed, false otherwise.
282 extern bool meshlink_destroy(const char *confbase);
284 /// A callback for receiving data from the mesh.
285 /** @param mesh A handle which represents an instance of MeshLink.
286 * @param source A pointer to a meshlink_node_t describing the source of the data.
287 * @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).
288 * The pointer is only valid during the lifetime of the callback.
289 * The callback should mempcy() the data if it needs to be available outside the callback.
290 * @param len The length of the received data, or 0 in case there is no data.
292 typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len);
294 /// Set the receive callback.
295 /** This functions sets the callback that is called whenever another node sends data to the local node.
296 * The callback is run in MeshLink's own thread.
297 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
298 * to hand the data over to the application's thread.
299 * The callback should also not block itself and return as quickly as possible.
301 * @param mesh A handle which represents an instance of MeshLink.
302 * @param cb A pointer to the function which will be called when another node sends data to the local node.
303 * If a NULL pointer is given, the callback will be disabled.
305 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
307 /// A callback reporting node status changes.
308 /** @param mesh A handle which represents an instance of MeshLink.
309 * @param node A pointer to a meshlink_node_t describing the node whose status changed.
310 * This pointer is valid until meshlink_close() is called.
311 * @param reachable True if the node is reachable, false otherwise.
313 typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
315 /// Set the node status callback.
316 /** This functions sets the callback that is called whenever another node's status changed.
317 * The callback is run in MeshLink's own thread.
318 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
319 * to hand the data over to the application's thread.
320 * The callback should also not block itself and return as quickly as possible.
322 * @param mesh A handle which represents an instance of MeshLink.
323 * @param cb A pointer to the function which will be called when another node's status changes.
324 * If a NULL pointer is given, the callback will be disabled.
326 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
328 /// A callback reporting duplicate node detection.
329 /** @param mesh A handle which represents an instance of MeshLink.
330 * @param node A pointer to a meshlink_node_t describing the node which is duplicate.
331 * This pointer is valid until meshlink_close() is called.
333 typedef void (*meshlink_node_duplicate_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
335 /// Set the node duplicate callback.
336 /** This functions sets the callback that is called whenever a duplicate node is detected.
337 * The callback is run in MeshLink's own thread.
338 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
339 * to hand the data over to the application's thread.
340 * The callback should also not block itself and return as quickly as possible.
342 * @param mesh A handle which represents an instance of MeshLink.
343 * @param cb A pointer to the function which will be called when a duplicate node is detected.
344 * If a NULL pointer is given, the callback will be disabled.
346 extern void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb);
348 /// Severity of log messages generated by MeshLink.
350 MESHLINK_DEBUG, ///< Internal debugging messages. Only useful during application development.
351 MESHLINK_INFO, ///< Informational messages.
352 MESHLINK_WARNING, ///< Warnings which might indicate problems, but which are not real errors.
353 MESHLINK_ERROR, ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
354 MESHLINK_CRITICAL ///< Critical errors which cause MeshLink to fail completely.
355 } meshlink_log_level_t;
357 /// A callback for receiving log messages generated by MeshLink.
358 /** @param mesh A handle which represents an instance of MeshLink, or NULL.
359 * @param level An enum describing the severity level of the message.
360 * @param text A pointer to a nul-terminated C string containing the textual log message.
361 * This pointer is only valid for the duration of the callback.
362 * The application must not free() this pointer.
363 * The application should strdup() the text if it has to be available outside the callback.
365 typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
367 /// Set the log callback.
368 /** This functions sets the callback that is called whenever MeshLink has some information to log.
370 * The @a mesh @a parameter can either be a valid MeshLink handle, or NULL.
371 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
372 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
374 * If @a mesh @a is not NULL, then the callback is run in MeshLink's own thread.
375 * It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
376 * to hand the data over to the application's thread.
377 * The callback should also not block itself and return as quickly as possible.
379 * The @a mesh @a parameter can either be a valid MeshLink handle, or NULL.
380 * In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
381 * Otherwise, it will be called for errors that happen in the context of the given mesh instance.
383 * @param mesh A handle which represents an instance of MeshLink, or NULL.
384 * @param level An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
385 * @param cb A pointer to the function which will be called when another node sends data to the local node.
386 * If a NULL pointer is given, the callback will be disabled.
388 extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
390 /// Send data to another node.
391 /** This functions sends one packet of data to another node in the mesh.
392 * The packet is sent using UDP semantics, which means that
393 * the packet is sent as one unit and is received as one unit,
394 * and that there is no guarantee that the packet will arrive at the destination.
395 * 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.
396 * The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
397 * The application should take care of getting an acknowledgement and retransmission if necessary.
399 * @param mesh A handle which represents an instance of MeshLink.
400 * @param destination A pointer to a meshlink_node_t describing the destination for the data.
401 * @param data A pointer to a buffer containing the data to be sent to the source.
402 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
403 * It is valid to specify a NULL pointer, but only if @a len @a is also 0.
404 * @param len The length of the data.
405 * @return This function will return true if MeshLink has queued the message for transmission, and false otherwise.
406 * A return value of true does not guarantee that the message will actually arrive at the destination.
408 extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len);
410 /// Query the maximum packet size that can be sent to a node.
411 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
412 * The path MTU is a property of the path packets will take to the destination node over the Internet.
413 * It can be different for different destination nodes.
414 * and the path MTU can change at any point in time due to changes in the Internet.
415 * Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
417 * @param mesh A handle which represents an instance of MeshLink.
418 * @param destination A pointer to a meshlink_node_t describing the destination for the data.
420 * @return The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
421 * or a negative value in case of an error.
423 extern ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination);
425 /// Get a handle for our own node.
426 /** This function returns a handle for the local node.
428 * @param mesh A handle which represents an instance of MeshLink.
430 * @return A pointer to a meshlink_node_t which represents the local node.
431 * The pointer is guaranteed to be valid until meshlink_close() is called.
433 extern meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh);
435 /// Get a handle for a specific node.
436 /** This function returns a handle for the node with the given name.
438 * @param mesh A handle which represents an instance of MeshLink.
439 * @param name The name of the node for which a handle is requested.
440 * After this function returns, the application is free to overwrite or free @a name @a.
442 * @return A pointer to a meshlink_node_t which represents the requested node,
443 * or NULL if the requested node does not exist.
444 * The pointer is guaranteed to be valid until meshlink_close() is called.
446 extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
448 /// Get the fingerprint of a node's public key.
449 /** This function returns a fingerprint of the node's public key.
450 * It should be treated as an opaque blob.
452 * @param mesh A handle which represents an instance of MeshLink.
453 * @param node A pointer to a meshlink_node_t describing the node.
455 * @return A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
456 * The application should call free() after it is done using this string.
458 extern char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node);
460 /// Get a list of all nodes.
461 /** This function returns a list with handles for all known nodes.
463 * @param mesh A handle which represents an instance of MeshLink.
464 * @param nodes A pointer to a previously allocated array of pointers to meshlink_node_t, or NULL in which case MeshLink will allocate a new array.
465 * 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).
466 * The application is allowed to call free() on the array whenever it wishes.
467 * The pointers in the array are valid until meshlink_close() is called.
468 * @param nmemb A pointer to a variable holding the number of nodes that are stored in the array.
469 * In case the @a nodes @a argument is not NULL, MeshLink might call realloc() on the array to change its size.
470 * The contents of this variable will be changed to reflect the new size of the array.
472 * @return A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
473 * If the @a nodes @a argument was not NULL, then the return value can either be the same value or a different value.
474 * If it is a new value, the old value of @a nodes @a should not be used anymore.
475 * If the new value is NULL, then the old array will have been freed by MeshLink.
477 extern meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb);
479 /// Sign data using the local node's MeshLink key.
480 /** This function signs data using the local node's MeshLink key.
481 * The generated signature can be securely verified by other nodes.
483 * @param mesh A handle which represents an instance of MeshLink.
484 * @param data A pointer to a buffer containing the data to be signed.
485 * @param len The length of the data to be signed.
486 * @param signature A pointer to a buffer where the signature will be stored.
487 * The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
488 * The signature is a binary blob, and is not nul-terminated.
489 * @param siglen The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
491 * @return This function returns true if the signature was correctly generated, false otherwise.
493 extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen);
495 /// Get the list of all nodes by device class.
496 /** This function returns a list with handles for all the nodes that matches with the given @a devclass @a .
498 * @param mesh A handle which represents an instance of MeshLink.
499 * @param devclass Device class of the nodes for which the list has to be obtained.
500 * @param nodes A pointer to a previously allocated array of pointers to meshlink_node_t, or NULL in which case MeshLink will allocate a new array.
501 * 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).
502 * The application is allowed to call free() on the array whenever it wishes.
503 * The pointers in the array are valid until meshlink_close() is called.
504 * @param nmemb A pointer to a variable holding the number of nodes with the same @a device class @a that are stored in the array.
505 * In case the @a nodes @a argument is not NULL, MeshLink might call realloc() on the array to change its size.
506 * The contents of this variable will be changed to reflect the new size of the array.
508 * @return A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
509 * If the @a nodes @a argument was not NULL, then the return value can either be the same value or a different value.
510 * If it is a new value, the old value of @a nodes @a should not be used anymore.
511 * If the new value is NULL, then the old array will have been freed by MeshLink.
513 extern meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, dev_class_t devclass, meshlink_node_t **nodes, size_t *nmemb);
515 /// Get the node's device class.
516 /** This function returns the device class of the given node.
518 * @param mesh A handle which represents an instance of MeshLink.
519 * @param node A pointer to a meshlink_node_t describing the node.
521 * @return This function returns the device class of the @a node @a , or -1 in case of an error.
523 extern dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node);
525 /// Verify the signature generated by another node of a piece of data.
526 /** This function verifies the signature that another node generated for a piece of data.
528 * @param mesh A handle which represents an instance of MeshLink.
529 * @param source A pointer to a meshlink_node_t describing the source of the signature.
530 * @param data A pointer to a buffer containing the data to be verified.
531 * @param len The length of the data to be verified.
532 * @param signature A pointer to a buffer where the signature is stored.
533 * @param siglen A pointer to a variable holding the size of the signature buffer.
534 * The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
536 * @return This function returns true if the signature is valid, false otherwise.
538 extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen);
540 /// Set the canonical Address for a node.
541 /** This function sets the canonical Address for a node.
542 * This address is stored permanently until it is changed by another call to this function,
543 * unlike other addresses associated with a node,
544 * such as those added with meshlink_hint_address() or addresses discovered at runtime.
546 * If a canonical Address is set for the local node,
547 * it will be used for the hostname part of generated invitation URLs.
549 * @param mesh A handle which represents an instance of MeshLink.
550 * @param node A pointer to a meshlink_node_t describing the node.
551 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
552 * @param port A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
553 * If it is NULL, the listening port's number will be used.
555 * @return This function returns true if the address was added, false otherwise.
557 extern bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node, const char *address, const char *port);
559 /// Add an Address for the local node.
560 /** This function adds an Address for the local node, which will be used for invitation URLs.
562 * @param mesh A handle which represents an instance of MeshLink.
563 * @param address A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
565 * @return This function returns true if the address was added, false otherwise.
567 extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
569 /// Try to discover the external address for the local node.
570 /** This function performs tries to discover the local node's external address
571 * by contacting the meshlink.io server. If a reverse lookup of the address works,
572 * the FQDN associated with the address will be returned.
574 * Please note that this is function only returns a single address,
575 * even if the local node might have more than one external address.
576 * In that case, there is no control over which address will be selected.
577 * Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
578 * there is no guarantee that the external address will be valid for an extended period of time.
580 * This function is blocking. It can take several seconds before it returns.
581 * There is no guarantee it will be able to resolve the external address.
582 * Failures might be because by temporary network outages.
584 * @param mesh A handle which represents an instance of MeshLink.
586 * @return This function returns a pointer to a C string containing the discovered external address,
587 * or NULL if there was an error looking up the address.
588 * After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
590 extern char *meshlink_get_external_address(meshlink_handle_t *mesh);
592 /// Try to discover the external address for the local node.
593 /** This function performs tries to discover the local node's external address
594 * by contacting the meshlink.io server. If a reverse lookup of the address works,
595 * the FQDN associated with the address will be returned.
597 * Please note that this is function only returns a single address,
598 * even if the local node might have more than one external address.
599 * In that case, there is no control over which address will be selected.
600 * Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
601 * there is no guarantee that the external address will be valid for an extended period of time.
603 * This function is blocking. It can take several seconds before it returns.
604 * There is no guarantee it will be able to resolve the external address.
605 * Failures might be because by temporary network outages.
607 * @param mesh A handle which represents an instance of MeshLink.
608 * @param family The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
609 * this might return the external address for any working address family.
611 * @return This function returns a pointer to a C string containing the discovered external address,
612 * or NULL if there was an error looking up the address.
613 * After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
615 extern char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int address_family);
617 /// Try to discover the local address for the local node.
618 /** This function performs tries to discover the address of the local interface used for outgoing connection.
620 * Please note that this is function only returns a single address,
621 * even if the interface might have more than one address.
622 * In that case, there is no control over which address will be selected.
623 * Also note that if you have a dynamic IP address,
624 * there is no guarantee that the local address will be valid for an extended period of time.
626 * This function will fail if it couldn't find a local address for the given address family.
627 * If hostname resolving is requested, this function may block for a few seconds.
629 * @param mesh A handle which represents an instance of MeshLink.
630 * @param family The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
631 * this might return the local address for any working address family.
633 * @return This function returns a pointer to a C string containing the discovered local address,
634 * or NULL if there was an error looking up the address.
635 * After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
637 extern char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int address_family);
639 /// Try to discover the external address for the local node, and add it to its list of addresses.
640 /** This function is equivalent to:
642 * meshlink_add_address(mesh, meshlink_get_external_address(mesh));
644 * Read the description of meshlink_get_external_address() for the limitations of this function.
646 * @param mesh A handle which represents an instance of MeshLink.
648 * @return This function returns true if the address was added, false otherwise.
650 extern bool meshlink_add_external_address(meshlink_handle_t *mesh);
652 /// Get the network port used by the local node.
653 /** This function returns the network port that the local node is listening on.
655 * @param mesh A handle which represents an instance of MeshLink.
657 * @return This function returns the port number, or -1 in case of an error.
659 extern int meshlink_get_port(meshlink_handle_t *mesh);
661 /// Set the network port used by the local node.
662 /** This function sets the network port that the local node is listening on.
663 * It may only be called when the mesh is not running.
664 * If unsure, call meshlink_stop() before calling this function.
665 * Also note that if your node is already part of a mesh with other nodes,
666 * that the other nodes may no longer be able to initiate connections to the local node,
667 * since they will try to connect to the previously configured port.
669 * @param mesh A handle which represents an instance of MeshLink.
670 * @param port The port number to listen on. This must be between 0 and 65535.
671 * If the port is set to 0, then MeshLink will listen on a port
672 * that is randomly assigned by the operating system every time meshlink_open() is called.
674 * @return This function returns true if the port was succesfully changed, false otherwise.
677 extern bool meshlink_set_port(meshlink_handle_t *mesh, int port);
679 /// Set the timeout for invitations.
680 /** This function sets the timeout for invitations.
681 * Note that timeouts are only checked at the time a node tries to join using an invitation.
682 * The default timeout for invitations is 1 week.
684 * @param mesh A handle which represents an instance of MeshLink.
685 * @param timeout The timeout for invitations in seconds.
687 extern void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout);
689 /// Invite another node into the mesh.
690 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
691 * The generated invitation is a string containing a URL.
692 * This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
693 * The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
695 * @param mesh A handle which represents an instance of MeshLink.
696 * @param submesh A handle which represents an instance of SubMesh.
697 * @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
698 * After this function returns, the application is free to overwrite or free @a name @a.
699 * @param flags A bitwise-or'd combination of flags that controls how the URL is generated.
701 * @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
702 * The application should call free() after it has finished using the URL.
704 extern char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags);
706 /// Invite another node into the mesh.
707 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
708 * The generated invitation is a string containing a URL.
709 * This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
710 * The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
712 * Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
714 * @param mesh A handle which represents an instance of MeshLink.
715 * @param submesh A handle which represents an instance of SubMesh.
716 * @param name A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
717 * After this function returns, the application is free to overwrite or free @a name @a.
719 * @return This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
720 * The application should call free() after it has finished using the URL.
722 extern char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name);
724 /// Use an invitation to join a mesh.
725 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
726 * An invitation can only be used if the local node has never connected to other nodes before.
727 * After a succesfully accepted invitation, the name of the local node may have changed.
729 * 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.
731 * This function is blocking. It can take several seconds before it returns.
732 * There is no guarantee it will perform a successful join.
733 * Failures might be caused by temporary network outages, or by the invitation having expired.
735 * @param mesh A handle which represents an instance of MeshLink.
736 * @param invitation A nul-terminated C string containing the invitation URL.
737 * After this function returns, the application is free to overwrite or free @a invitation @a.
739 * @return This function returns true if the local node joined the mesh it was invited to, false otherwise.
741 extern bool meshlink_join(meshlink_handle_t *mesh, const char *invitation);
743 /// Export the local node's key and addresses.
744 /** This function generates a string that contains the local node's public key and one or more IP addresses.
745 * The application can pass it in some way to another node, which can then import it,
746 * granting the local node access to the other node's mesh.
747 * The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
749 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
751 * @param mesh A handle which represents an instance of MeshLink.
753 * @return This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
754 * The application should call free() after it has finished using this string.
756 extern char *meshlink_export(meshlink_handle_t *mesh);
758 /// Import another node's key and addresses.
759 /** This function accepts a string containing the exported public key and addresses of another node.
760 * By importing this data, the local node grants the other node access to its mesh.
761 * The application should make sure that the data it imports is really coming from the node it wants to import,
763 * Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
765 * @param mesh A handle which represents an instance of MeshLink.
766 * @param data A nul-terminated C string containing the other node's exported key and addresses.
767 * After this function returns, the application is free to overwrite or free @a data @a.
769 * @return This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
771 extern bool meshlink_import(meshlink_handle_t *mesh, const char *data);
773 /// Blacklist a node from the mesh.
774 /** This function causes the local node to blacklist another node.
775 * The local node will drop any existing connections to that node,
776 * and will not send data to it nor accept any data received from it any more.
778 * @param mesh A handle which represents an instance of MeshLink.
779 * @param node A pointer to a meshlink_node_t describing the node to be blacklisted.
781 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
783 /// Whitelist a node on the mesh.
784 /** This function causes the local node to whitelist a previously blacklisted node.
785 * The local node will allow connections to and from that node,
786 * and will send data to it and accept any data received from it.
788 * @param mesh A handle which represents an instance of MeshLink.
789 * @param node A pointer to a meshlink_node_t describing the node to be whitelisted.
791 extern void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node);
793 /// Set whether new nodes are blacklisted by default.
794 /** This function sets the blacklist behaviour for newly discovered nodes.
795 * If set to true, new nodes will be automatically blacklisted.
796 * If set to false, which is the default, new nodes are automatically whitelisted.
797 * The whitelist/blacklist status of a node may be changed afterwards with the
798 * meshlink_whitelist() and meshlink_blacklist() functions.
800 * @param mesh A handle which represents an instance of MeshLink.
801 * @param blacklist True if new nodes are to be blacklisted, false if whitelisted.
803 extern void meshlink_set_default_blacklist(meshlink_handle_t *mesh, bool blacklist);
805 /// A callback for accepting incoming channels.
806 /** This function is called whenever a remote node wants to open a channel to the local node.
807 * The application then has to decide whether to accept or reject this channel.
809 * The callback is run in MeshLink's own thread.
810 * It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
811 * to hand any data over to the application's thread.
813 * @param mesh A handle which represents an instance of MeshLink.
814 * @param channel A handle for the incoming channel.
815 * If the application accepts the incoming channel by returning true,
816 * then this handle is valid until meshlink_channel_close() is called on it.
817 * If the application rejects the incoming channel by returning false,
818 * then this handle is invalid after the callback returns
819 * (the callback does not need to call meshlink_channel_close() itself in this case).
820 * @param port The port number the peer wishes to connect to.
821 * @param data A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
822 * The pointer is only valid during the lifetime of the callback.
823 * The callback should mempcy() the data if it needs to be available outside the callback.
824 * @param len The length of the data, or 0 in case no data has been received yet. (Not yet used.)
826 * @return This function should return true if the application accepts the incoming channel, false otherwise.
827 * If returning false, the channel is invalid and may not be used anymore.
829 typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len);
831 /// A callback for receiving data from a channel.
832 /** This function is called whenever data is received from a remote node on a channel.
834 * This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
835 * In both cases, @a data @a will be NULL and @a len @a will be 0, and meshlink_errno will be set.
836 * In any case, the @a channel @a handle will still be valid until the application calls meshlink_close().
838 * @param mesh A handle which represents an instance of MeshLink.
839 * @param channel A handle for the channel.
840 * @param data A pointer to a buffer containing data sent by the source, or NULL in case of an error.
841 * The pointer is only valid during the lifetime of the callback.
842 * The callback should mempcy() the data if it needs to be available outside the callback.
843 * @param len The length of the data, or 0 in case of an error.
845 typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
847 /// A callback informing the application when data can be sent on a channel.
848 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
850 * @param mesh A handle which represents an instance of MeshLink.
851 * @param channel A handle for the channel.
852 * @param len The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
853 * or 0 in case of an error.
855 typedef void (*meshlink_channel_poll_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
857 /// Set the accept callback.
858 /** This functions sets the callback that is called whenever another node sends data to the local node.
859 * The callback is run in MeshLink's own thread.
860 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
861 * to hand the data over to the application's thread.
862 * The callback should also not block itself and return as quickly as possible.
864 * If no accept callback is set, incoming channels are rejected.
866 * @param mesh A handle which represents an instance of MeshLink.
867 * @param cb A pointer to the function which will be called when another node sends data to the local node.
868 * If a NULL pointer is given, the callback will be disabled.
870 extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
872 /// Set the receive callback.
873 /** This functions sets the callback that is called whenever another node sends data to the local node.
874 * The callback is run in MeshLink's own thread.
875 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
876 * to hand the data over to the application's thread.
877 * The callback should also not block itself and return as quickly as possible.
879 * @param mesh A handle which represents an instance of MeshLink.
880 * @param channel A handle for the channel.
881 * @param cb A pointer to the function which will be called when another node sends data to the local node.
882 * If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
884 extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
886 /// Set the poll callback.
887 /** This functions sets the callback that is called whenever data can be sent to another node.
888 * The callback is run in MeshLink's own thread.
889 * It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
890 * to pass data to or from the application's thread.
891 * The callback should also not block itself and return as quickly as possible.
893 * @param mesh A handle which represents an instance of MeshLink.
894 * @param channel A handle for the channel.
895 * @param cb A pointer to the function which will be called when data can be sent to another node.
896 * If a NULL pointer is given, the callback will be disabled.
898 extern void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb);
900 /// Open a reliable stream channel to another node.
901 /** This function is called whenever a remote node wants to open a channel to the local node.
902 * The application then has to decide whether to accept or reject this channel.
904 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
905 * When the application does no longer need to use this channel, it must call meshlink_close()
906 * to free its resources.
908 * @param mesh A handle which represents an instance of MeshLink.
909 * @param node The node to which this channel is being initiated.
910 * @param port The port number the peer wishes to connect to.
911 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
912 * The pointer may be NULL, in which case incoming data is ignored.
913 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
914 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
915 * @param len The length of the data, or 0 if there is no data to send.
916 * @param flags A bitwise-or'd combination of flags that set the semantics for this channel.
918 * @return A handle for the channel, or NULL in case of an error.
919 * The handle is valid until meshlink_channel_close() is called.
921 extern meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags);
923 /// Open a reliable stream channel to another node.
924 /** This function is called whenever a remote node wants to open a channel to the local node.
925 * The application then has to decide whether to accept or reject this channel.
927 * This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
928 * When the application does no longer need to use this channel, it must call meshlink_close()
929 * to free its resources.
931 * Calling this function is equivalent to calling meshlink_channel_open_ex()
932 * with the flags set to MESHLINK_CHANNEL_TCP.
934 * @param mesh A handle which represents an instance of MeshLink.
935 * @param node The node to which this channel is being initiated.
936 * @param port The port number the peer wishes to connect to.
937 * @param cb A pointer to the function which will be called when the remote node sends data to the local node.
938 * The pointer may be NULL, in which case incoming data is ignored.
939 * @param data A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
940 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
941 * @param len The length of the data, or 0 if there is no data to send.
943 * @return A handle for the channel, or NULL in case of an error.
944 * The handle is valid until meshlink_channel_close() is called.
946 extern meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len);
948 /// Partially close a reliable stream channel.
949 /** This shuts down the read or write side of a channel, or both, without closing the handle.
950 * It can be used to inform the remote node that the local node has finished sending all data on the channel,
951 * but still allows waiting for incoming data from the remote node.
953 * Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
955 * @param mesh A handle which represents an instance of MeshLink.
956 * @param channel A handle for the channel.
957 * @param direction Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
959 extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction);
961 /// Close a reliable stream channel.
962 /** This informs the remote node that the local node has finished sending all data on the channel.
963 * It also causes the local node to stop accepting incoming data from the remote node.
964 * It will free the struct meshlink_channel and all associated resources.
965 * Afterwards, the channel handle is invalid and must not be used any more.
967 * It is allowed to call this function at any time on a valid handle, even inside callback functions.
968 * If called with a valid handle, this function always succeeds, otherwise the result is undefined.
970 * @param mesh A handle which represents an instance of MeshLink.
971 * @param channel A handle for the channel.
973 extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel);
975 /// Transmit data on a channel
976 /** This queues data to send to the remote node.
978 * @param mesh A handle which represents an instance of MeshLink.
979 * @param channel A handle for the channel.
980 * @param data A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
981 * After meshlink_send() returns, the application is free to overwrite or free this buffer.
982 * @param len The length of the data, or 0 if there is no data to send.
984 * @return The amount of data that was queued, which can be less than len, or a negative value in case of an error.
986 extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
988 /// Get channel flags.
989 /** This returns the flags used when opening this channel.
991 * @param mesh A handle which represents an instance of MeshLink.
992 * @param channel A handle for the channel.
994 * @return The flags set for this channel.
996 extern uint32_t meshlink_channel_get_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel);
998 /// Hint that a hostname may be found at an address
999 /** This function indicates to meshlink that the given hostname is likely found
1000 * at the given IP address and port.
1002 * @param mesh A handle which represents an instance of MeshLink.
1003 * @param hostname The hostname which can be found at the given address.
1004 * The caller is free to overwrite or free this string
1005 * once meshlink returns.
1006 * @param addr The IP address and port which should be tried for the
1007 * given hostname. The caller is free to overwrite or free
1008 * this memory once meshlink returns.
1010 extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr);
1012 /// Enable or disable zeroconf discovery of local peers
1014 /** This controls whether zeroconf discovery using the Catta library will be
1015 * enabled to search for peers on the local network. By default, it is enabled.
1017 * @param mesh A handle which represents an instance of MeshLink.
1018 * @param enable Set to true to enable discovery, false to disable.
1020 extern void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable);