]> git.meshlink.io Git - meshlink/blob - src/meshlink.h
Add functions to get the amount of bytes in chanenl send and receive buffers.
[meshlink] / src / meshlink.h
1 #ifndef MESHLINK_H
2 #define MESHLINK_H
3
4 /*
5     meshlink.h -- MeshLink API
6     Copyright (C) 2014-2019 Guus Sliepen <guus@meshlink.io>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <unistd.h>
27
28 #if defined(_WIN32)
29 #include <winsock2.h>
30 #else
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #endif
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /// The length in bytes of a signature made with meshlink_sign()
40 #define MESHLINK_SIGLEN (64ul)
41
42 // The maximum length of fingerprints
43 #define MESHLINK_FINGERPRINTLEN (64ul)
44
45 /// A handle for an instance of MeshLink.
46 typedef struct meshlink_handle meshlink_handle_t;
47
48 /// A handle for a MeshLink node.
49 typedef struct meshlink_node meshlink_node_t;
50
51 /// A handle for a MeshLink channel.
52 typedef struct meshlink_channel meshlink_channel_t;
53
54 /// A struct containing all parameters used for opening a mesh.
55 typedef struct meshlink_open_params meshlink_open_params_t;
56
57 /// A handle for a MeshLink sub-mesh.
58 typedef struct meshlink_submesh meshlink_submesh_t;
59
60 /// Code of most recent error encountered.
61 typedef enum {
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_errno_t;
75
76 /// Device class
77 typedef enum {
78         DEV_CLASS_BACKBONE = 0,
79         DEV_CLASS_STATIONARY = 1,
80         DEV_CLASS_PORTABLE = 2,
81         DEV_CLASS_UNKNOWN = 3,
82         _DEV_CLASS_MAX = 3
83 } dev_class_t;
84
85 /// Invitation flags
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
91
92 /// Channel flags
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.
99
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 successfully.
105  */
106 extern __thread meshlink_errno_t meshlink_errno;
107
108 #ifndef MESHLINK_INTERNAL_H
109
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.
113 };
114
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.
118 };
119
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.
123 };
124
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.
128 };
129
130 #endif // MESHLINK_INTERNAL_H
131
132 /// Get the text for the given MeshLink error code.
133 /** This function returns a pointer to the string containing the description of the given error code.
134  *
135  *  @param err      An error code returned by MeshLink.
136  *
137  *  @return         A pointer to a string containing the description of the error code.
138  *                  The pointer is to static storage that is valid for the lifetime of the application.
139  *                  This function will always return a valid pointer, even if an invalid error code has been passed.
140  */
141 extern const char *meshlink_strerror(meshlink_errno_t err);
142
143 /// Create a new meshlink_open_params_t struct.
144 /** This function allocates and initializes a new meshlink_open_params_t struct that can be passed to meshlink_open_ex().
145  *  The resulting struct may be reused for multiple calls to meshlink_open_ex().
146  *  After the last use, the application must free this struct using meshlink_open_params_free().
147  *
148  *  @param confbase The directory in which MeshLink will store its configuration files.
149  *                  After the function returns, the application is free to overwrite or free @a confbase.
150  *  @param name     The name which this instance of the application will use in the mesh.
151  *                  After the function returns, the application is free to overwrite or free @a name.
152  *  @param appname  The application name which will be used in the mesh.
153  *                  After the function returns, the application is free to overwrite or free @a name.
154  *  @param devclass The device class which will be used in the mesh.
155  *
156  *  @return         A pointer to a meshlink_open_params_t which can be passed to meshlink_open_ex(), or NULL in case of an error.
157  *                  The pointer is valid until meshlink_open_params_free() is called.
158  */
159 extern meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
160
161 /// Free a meshlink_open_params_t struct.
162 /** This function frees a meshlink_open_params_t struct and all resources associated with it.
163  *
164  *  @param params   A pointer to a meshlink_open_params_t which must have been created earlier with meshlink_open_params_init().
165  */
166 extern void meshlink_open_params_free(meshlink_open_params_t *params);
167
168 /// Open or create a MeshLink instance.
169 /** This function opens or creates a MeshLink instance.
170  *  All parameters needed by MeshLink are passed via a meshlink_open_params_t struct,
171  *  which must have been initialized earlier by the application.
172  *
173  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
174  *  When the application does no longer need to use this handle, it must call meshlink_close() to
175  *  free its resources.
176  *
177  *  This function does not start any network I/O yet. The application should
178  *  first set callbacks, and then call meshlink_start().
179  *
180  *  @param params   A pointer to a meshlink_open_params_t which must be filled in by the application.
181  *                  After the function returns, the application is free to reuse or free @a params.
182  *
183  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
184  *                  The pointer is valid until meshlink_close() is called.
185  */
186 extern meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params);
187
188 /// Open or create a MeshLink instance.
189 /** This function opens or creates a MeshLink instance.
190  *  The state is stored in the configuration directory passed in the variable @a confbase.
191  *  If the configuration directory does not exist yet, for example when it is the first time
192  *  this instance is opened, the configuration directory will be automatically created and initialized.
193  *  However, the parent directory should already exist, otherwise an error will be returned.
194  *
195  *  The name given should be a unique identifier for this instance.
196  *
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.
200  *
201  *  This function does not start any network I/O yet. The application should
202  *  first set callbacks, and then call meshlink_start().
203  *
204  *  @param confbase The directory in which MeshLink will store its configuration files.
205  *                  After the function returns, the application is free to overwrite or free @a confbase.
206  *  @param name     The name which this instance of the application will use in the mesh.
207  *                  After the function returns, the application is free to overwrite or free @a name.
208  *  @param appname  The application name which will be used in the mesh.
209  *                  After the function returns, the application is free to overwrite or free @a name.
210  *  @param devclass The device class which will be used in the mesh.
211  *
212  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
213  *                  The pointer is valid until meshlink_close() is called.
214  */
215 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass);
216
217 /// Create Sub-Mesh.
218 /** This function causes MeshLink to open a new Sub-Mesh network
219  *  create a new thread, which will handle all network I/O.
220  *
221  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
222  *
223  *  @param mesh     A handle which represents an instance of MeshLink.
224   *
225  *  @param submesh  Name of the new Sub-Mesh to create.
226  *
227  *  @return         A pointer to a meshlink_submesh_t which represents this instance of SubMesh, or NULL in case of an error.
228  *                  The pointer is valid until meshlink_close() is called.
229  */
230 meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t  *mesh, const char *submesh);
231
232 /// Start MeshLink.
233 /** This function causes MeshLink to open network sockets, make outgoing connections, and
234  *  create a new thread, which will handle all network I/O.
235  *
236  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
237  *
238  *  @param mesh     A handle which represents an instance of MeshLink.
239  *
240  *  @return         This function will return true if MeshLink has successfully started, false otherwise.
241  */
242 extern bool meshlink_start(meshlink_handle_t *mesh);
243
244 /// Stop MeshLink.
245 /** This function causes MeshLink to disconnect from all other nodes,
246  *  close all sockets, and shut down its own thread.
247  *
248  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
249  *  Channels that are still open will remain valid, but any communication via channels will stop as well.
250  *
251  *  @param mesh     A handle which represents an instance of MeshLink.
252  */
253 extern void meshlink_stop(meshlink_handle_t *mesh);
254
255 /// Close the MeshLink handle.
256 /** This function calls meshlink_stop() if necessary,
257  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink, including all channels.
258  *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
259  *
260  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
261  *  If called at a proper time with a valid handle, this function always succeeds.
262  *  If called within a callback or with an invalid handle, the result is undefined.
263  *
264  *  @param mesh     A handle which represents an instance of MeshLink.
265  */
266 extern void meshlink_close(meshlink_handle_t *mesh);
267
268 /// Destroy a MeshLink instance.
269 /** This function remove all configuration files of a MeshLink instance. It should only be called when the application
270  *  does not have an open handle to this instance. Afterwards, a call to meshlink_open() will create a completely
271  *  new instance.
272  *
273  *  @param confbase The directory in which MeshLink stores its configuration files.
274  *                  After the function returns, the application is free to overwrite or free @a confbase.
275  *
276  *  @return         This function will return true if the MeshLink instance was successfully destroyed, false otherwise.
277  */
278 extern bool meshlink_destroy(const char *confbase);
279
280 /// A callback for receiving data from the mesh.
281 /** @param mesh      A handle which represents an instance of MeshLink.
282  *  @param source    A pointer to a meshlink_node_t describing the source of the data.
283  *  @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).
284  *                   The pointer is only valid during the lifetime of the callback.
285  *                   The callback should mempcy() the data if it needs to be available outside the callback.
286  *  @param len       The length of the received data, or 0 in case there is no data.
287  */
288 typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len);
289
290 /// Set the receive callback.
291 /** This functions sets the callback that is called whenever another node sends data to the local node.
292  *  The callback is run in MeshLink's own thread.
293  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
294  *  to hand the data over to the application's thread.
295  *  The callback should also not block itself and return as quickly as possible.
296  *
297  *  @param mesh      A handle which represents an instance of MeshLink.
298  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
299  *                   If a NULL pointer is given, the callback will be disabled.
300  */
301 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
302
303 /// A callback reporting node status changes.
304 /** @param mesh       A handle which represents an instance of MeshLink.
305  *  @param node       A pointer to a meshlink_node_t describing the node whose status changed.
306  *                    This pointer is valid until meshlink_close() is called.
307  *  @param reachable  True if the node is reachable, false otherwise.
308  */
309 typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
310
311 /// Set the node status callback.
312 /** This functions sets the callback that is called whenever another node's status changed.
313  *  The callback is run in MeshLink's own thread.
314  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
315  *  to hand the data over to the application's thread.
316  *  The callback should also not block itself and return as quickly as possible.
317  *
318  *  @param mesh      A handle which represents an instance of MeshLink.
319  *  @param cb        A pointer to the function which will be called when another node's status changes.
320  *                   If a NULL pointer is given, the callback will be disabled.
321  */
322 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
323
324 /// A callback reporting duplicate node detection.
325 /** @param mesh       A handle which represents an instance of MeshLink.
326  *  @param node       A pointer to a meshlink_node_t describing the node which is duplicate.
327  *                    This pointer is valid until meshlink_close() is called.
328  */
329 typedef void (*meshlink_node_duplicate_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node);
330
331 /// Set the node duplicate callback.
332 /** This functions sets the callback that is called whenever a duplicate node is detected.
333  *  The callback is run in MeshLink's own thread.
334  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
335  *  to hand the data over to the application's thread.
336  *  The callback should also not block itself and return as quickly as possible.
337  *
338  *  @param mesh      A handle which represents an instance of MeshLink.
339  *  @param cb        A pointer to the function which will be called when a duplicate node is detected.
340  *                   If a NULL pointer is given, the callback will be disabled.
341  */
342 extern void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb);
343
344 /// Severity of log messages generated by MeshLink.
345 typedef enum {
346         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
347         MESHLINK_INFO,     ///< Informational messages.
348         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
349         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
350         MESHLINK_CRITICAL  ///< Critical errors which cause MeshLink to fail completely.
351 } meshlink_log_level_t;
352
353 /// A callback for receiving log messages generated by MeshLink.
354 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
355  *  @param level     An enum describing the severity level of the message.
356  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
357  *                   This pointer is only valid for the duration of the callback.
358  *                   The application must not free() this pointer.
359  *                   The application should strdup() the text if it has to be available outside the callback.
360  */
361 typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
362
363 /// Set the log callback.
364 /** This functions sets the callback that is called whenever MeshLink has some information to log.
365  *
366  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
367  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
368  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
369  *
370  *  If @a mesh is not NULL, then the callback is run in MeshLink's own thread.
371  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
372  *  to hand the data over to the application's thread.
373  *  The callback should also not block itself and return as quickly as possible.
374  *
375  *  The @a mesh parameter can either be a valid MeshLink handle, or NULL.
376  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
377  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
378  *
379  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
380  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
381  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
382  *                   If a NULL pointer is given, the callback will be disabled.
383  */
384 extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
385
386 /// Send data to another node.
387 /** This functions sends one packet of data to another node in the mesh.
388  *  The packet is sent using UDP semantics, which means that
389  *  the packet is sent as one unit and is received as one unit,
390  *  and that there is no guarantee that the packet will arrive at the destination.
391  *  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.
392  *  The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
393  *  The application should take care of getting an acknowledgement and retransmission if necessary.
394  *
395  *  @param mesh         A handle which represents an instance of MeshLink.
396  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
397  *  @param data         A pointer to a buffer containing the data to be sent to the source.
398  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
399  *                      It is valid to specify a NULL pointer, but only if @a len is also 0.
400  *  @param len          The length of the data.
401  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
402  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
403  */
404 extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len);
405
406 /// Query the maximum packet size that can be sent to a node.
407 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
408  *  The path MTU is a property of the path packets will take to the destination node over the Internet.
409  *  It can be different for different destination nodes.
410  *  and the path MTU can change at any point in time due to changes in the Internet.
411  *  Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
412  *
413  *  @param mesh         A handle which represents an instance of MeshLink.
414  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
415  *
416  *  @return             The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
417  *                      or a negative value in case of an error.
418  */
419 extern ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination);
420
421 /// Get a handle for our own node.
422 /** This function returns a handle for the local node.
423  *
424  *  @param mesh         A handle which represents an instance of MeshLink.
425  *
426  *  @return             A pointer to a meshlink_node_t which represents the local node.
427  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
428  */
429 extern meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh);
430
431 /// Get a handle for a specific node.
432 /** This function returns a handle for the node with the given name.
433  *
434  *  @param mesh         A handle which represents an instance of MeshLink.
435  *  @param name         The name of the node for which a handle is requested.
436  *                      After this function returns, the application is free to overwrite or free @a name.
437  *
438  *  @return             A pointer to a meshlink_node_t which represents the requested node,
439  *                      or NULL if the requested node does not exist.
440  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
441  */
442 extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
443
444 /// Get the fingerprint of a node's public key.
445 /** This function returns a fingerprint of the node's public key.
446  *  It should be treated as an opaque blob.
447  *
448  *  @param mesh         A handle which represents an instance of MeshLink.
449  *  @param node         A pointer to a meshlink_node_t describing the node.
450  *
451  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
452  *                      The application should call free() after it is done using this string.
453  */
454 extern char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node);
455
456 /// Get a list of all nodes.
457 /** This function returns a list with handles for all known nodes.
458  *
459  *  @param mesh         A handle which represents an instance of MeshLink.
460  *  @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.
461  *                      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).
462  *                      The application is allowed to call free() on the array whenever it wishes.
463  *                      The pointers in the array are valid until meshlink_close() is called.
464  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
465  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
466  *                      The contents of this variable will be changed to reflect the new size of the array.
467  *
468  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
469  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
470  *                      If it is a new value, the old value of @a nodes should not be used anymore.
471  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
472  */
473 extern meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb);
474
475 /// Sign data using the local node's MeshLink key.
476 /** This function signs data using the local node's MeshLink key.
477  *  The generated signature can be securely verified by other nodes.
478  *
479  *  @param mesh         A handle which represents an instance of MeshLink.
480  *  @param data         A pointer to a buffer containing the data to be signed.
481  *  @param len          The length of the data to be signed.
482  *  @param signature    A pointer to a buffer where the signature will be stored.
483  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
484  *                      The signature is a binary blob, and is not nul-terminated.
485  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
486  *
487  *  @return             This function returns true if the signature was correctly generated, false otherwise.
488  */
489 extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen);
490
491 /// Get the list of all nodes by device class.
492 /** This function returns a list with handles for all the nodes that matches with the given @a devclass.
493  *
494  *  @param mesh         A handle which represents an instance of MeshLink.
495  *  @param devclass     Device class of the nodes for which the list has to be obtained.
496  *  @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.
497  *                      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).
498  *                      The application is allowed to call free() on the array whenever it wishes.
499  *                      The pointers in the array are valid until meshlink_close() is called.
500  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
501  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
502  *                      The contents of this variable will be changed to reflect the new size of the array.
503  *
504  *  @return             A pointer to an array containing pointers to all known nodes of the given device class, or NULL in case of an error.
505  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
506  *                      If it is a new value, the old value of @a nodes should not be used anymore.
507  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
508  */
509 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);
510
511 /// Get the list of all nodes by Submesh.
512 /** This function returns a list with handles for all the nodes that matches with the given @a Submesh.
513  *
514  *  @param mesh         A handle which represents an instance of MeshLink.
515  *  @param submesh      Submesh handle of the nodes for which the list has to be obtained.
516  *  @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.
517  *                      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).
518  *                      The application is allowed to call free() on the array whenever it wishes.
519  *                      The pointers in the array are valid until meshlink_close() is called.
520  *  @param nmemb        A pointer to a variable holding the number of nodes with the same @a device class that are stored in the array.
521  *                      In case the @a nodes argument is not NULL, MeshLink might call realloc() on the array to change its size.
522  *                      The contents of this variable will be changed to reflect the new size of the array.
523  *
524  *  @return             A pointer to an array containing pointers to all known nodes of the given Submesh, or NULL in case of an error.
525  *                      If the @a nodes argument was not NULL, then the return value can either be the same value or a different value.
526  *                      If it is a new value, the old value of @a nodes should not be used anymore.
527  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
528  */
529 extern meshlink_node_t **meshlink_get_all_nodes_by_submesh(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, meshlink_node_t **nodes, size_t *nmemb);
530
531 /// Get the node's device class.
532 /** This function returns the device class of the given node.
533  *
534  *  @param mesh          A handle which represents an instance of MeshLink.
535  *  @param node         A pointer to a meshlink_node_t describing the node.
536  *
537  *  @return              This function returns the device class of the @a node, or -1 in case of an error.
538  */
539 extern dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node);
540
541 /// Get the node's submesh handle.
542 /** This function returns the submesh handle of the given node.
543  *
544  *  @param mesh          A handle which represents an instance of MeshLink.
545  *  @param node          A pointer to a meshlink_node_t describing the node.
546  *
547  *  @return              This function returns the submesh handle of the @a node, or NULL in case of an error.
548  */
549 extern meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_node_t *node);
550
551 /// Verify the signature generated by another node of a piece of data.
552 /** This function verifies the signature that another node generated for a piece of data.
553  *
554  *  @param mesh         A handle which represents an instance of MeshLink.
555  *  @param source       A pointer to a meshlink_node_t describing the source of the signature.
556  *  @param data         A pointer to a buffer containing the data to be verified.
557  *  @param len          The length of the data to be verified.
558  *  @param signature    A pointer to a buffer where the signature is stored.
559  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
560  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
561  *
562  *  @return             This function returns true if the signature is valid, false otherwise.
563  */
564 extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen);
565
566 /// Set the canonical Address for a node.
567 /** This function sets the canonical Address for a node.
568  *  This address is stored permanently until it is changed by another call to this function,
569  *  unlike other addresses associated with a node,
570  *  such as those added with meshlink_hint_address() or addresses discovered at runtime.
571  *
572  *  If a canonical Address is set for the local node,
573  *  it will be used for the hostname part of generated invitation URLs.
574  *
575  *  @param mesh         A handle which represents an instance of MeshLink.
576  *  @param node         A pointer to a meshlink_node_t describing the node.
577  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
578  *  @param port         A nul-terminated C string containing the port, which can be either in numeric or symbolic format.
579  *                      If it is NULL, the listening port's number will be used.
580  *
581  *  @return             This function returns true if the address was added, false otherwise.
582  */
583 extern bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node, const char *address, const char *port);
584
585 /// Add an Address for the local node.
586 /** This function adds an Address for the local node, which will be used for invitation URLs.
587  *
588  *  @param mesh         A handle which represents an instance of MeshLink.
589  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
590  *
591  *  @return             This function returns true if the address was added, false otherwise.
592  */
593 extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
594
595 /// Try to discover the external address for the local node.
596 /** This function performs tries to discover the local node's external address
597  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
598  *  the FQDN associated with the address will be returned.
599  *
600  *  Please note that this is function only returns a single address,
601  *  even if the local node might have more than one external address.
602  *  In that case, there is no control over which address will be selected.
603  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
604  *  there is no guarantee that the external address will be valid for an extended period of time.
605  *
606  *  This function is blocking. It can take several seconds before it returns.
607  *  There is no guarantee it will be able to resolve the external address.
608  *  Failures might be because by temporary network outages.
609  *
610  *  @param mesh         A handle which represents an instance of MeshLink.
611  *
612  *  @return             This function returns a pointer to a C string containing the discovered external address,
613  *                      or NULL if there was an error looking up the address.
614  *                      After meshlink_get_external_address() returns, the application is free to overwrite or free this string.
615  */
616 extern char *meshlink_get_external_address(meshlink_handle_t *mesh);
617
618 /// Try to discover the external address for the local node.
619 /** This function performs tries to discover the local node's external address
620  *  by contacting the meshlink.io server. If a reverse lookup of the address works,
621  *  the FQDN associated with the address will be returned.
622  *
623  *  Please note that this is function only returns a single address,
624  *  even if the local node might have more than one external address.
625  *  In that case, there is no control over which address will be selected.
626  *  Also note that if you have a dynamic IP address, or are behind carrier-grade NAT,
627  *  there is no guarantee that the external address will be valid for an extended period of time.
628  *
629  *  This function is blocking. It can take several seconds before it returns.
630  *  There is no guarantee it will be able to resolve the external address.
631  *  Failures might be because by temporary network outages.
632  *
633  *  @param mesh            A handle which represents an instance of MeshLink.
634  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
635  *                         this might return the external address for any working address family.
636  *
637  *  @return                This function returns a pointer to a C string containing the discovered external address,
638  *                         or NULL if there was an error looking up the address.
639  *                         After meshlink_get_external_address_for_family() returns, the application is free to overwrite or free this string.
640  */
641 extern char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int address_family);
642
643 /// Try to discover the local address for the local node.
644 /** This function performs tries to discover the address of the local interface used for outgoing connection.
645  *
646  *  Please note that this is function only returns a single address,
647  *  even if the interface might have more than one address.
648  *  In that case, there is no control over which address will be selected.
649  *  Also note that if you have a dynamic IP address,
650  *  there is no guarantee that the local address will be valid for an extended period of time.
651  *
652  *  This function will fail if it couldn't find a local address for the given address family.
653  *  If hostname resolving is requested, this function may block for a few seconds.
654  *
655  *  @param mesh            A handle which represents an instance of MeshLink.
656  *  @param address_family  The address family to check, for example AF_INET or AF_INET6. If AF_UNSPEC is given,
657  *                         this might return the local address for any working address family.
658  *
659  *  @return                This function returns a pointer to a C string containing the discovered local address,
660  *                         or NULL if there was an error looking up the address.
661  *                         After meshlink_get_local_address_for_family() returns, the application is free to overwrite or free this string.
662  */
663 extern char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int address_family);
664
665 /// Try to discover the external address for the local node, and add it to its list of addresses.
666 /** This function is equivalent to:
667  *
668  *    meshlink_add_address(mesh, meshlink_get_external_address(mesh));
669  *
670  *  Read the description of meshlink_get_external_address() for the limitations of this function.
671  *
672  *  @param mesh         A handle which represents an instance of MeshLink.
673  *
674  *  @return             This function returns true if the address was added, false otherwise.
675  */
676 extern bool meshlink_add_external_address(meshlink_handle_t *mesh);
677
678 /// Get the network port used by the local node.
679 /** This function returns the network port that the local node is listening on.
680  *
681  *  @param mesh          A handle which represents an instance of MeshLink.
682  *
683  *  @return              This function returns the port number, or -1 in case of an error.
684  */
685 extern int meshlink_get_port(meshlink_handle_t *mesh);
686
687 /// Set the network port used by the local node.
688 /** This function sets the network port that the local node is listening on.
689  *  It may only be called when the mesh is not running.
690  *  If unsure, call meshlink_stop() before calling this function.
691  *  Also note that if your node is already part of a mesh with other nodes,
692  *  that the other nodes may no longer be able to initiate connections to the local node,
693  *  since they will try to connect to the previously configured port.
694  *
695  *  @param mesh          A handle which represents an instance of MeshLink.
696  *  @param port          The port number to listen on. This must be between 0 and 65535.
697  *                       If the port is set to 0, then MeshLink will listen on a port
698  *                       that is randomly assigned by the operating system every time meshlink_open() is called.
699  *
700  *  @return              This function returns true if the port was successfully changed, false otherwise.
701  */
702
703 extern bool meshlink_set_port(meshlink_handle_t *mesh, int port);
704
705 /// Set the timeout for invitations.
706 /** This function sets the timeout for invitations.
707  *  Note that timeouts are only checked at the time a node tries to join using an invitation.
708  *  The default timeout for invitations is 1 week.
709  *
710  *  @param mesh         A handle which represents an instance of MeshLink.
711  *  @param timeout      The timeout for invitations in seconds.
712  */
713 extern void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout);
714
715 /// Invite another node into the mesh.
716 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
717  *  The generated invitation is a string containing a URL.
718  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
719  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
720  *
721  *  @param mesh         A handle which represents an instance of MeshLink.
722  *  @param submesh      A handle which represents an instance of SubMesh.
723  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
724  *                      After this function returns, the application is free to overwrite or free @a name.
725  *  @param flags        A bitwise-or'd combination of flags that controls how the URL is generated.
726  *
727  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
728  *                      The application should call free() after it has finished using the URL.
729  */
730 extern char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags);
731
732 /// Invite another node into the mesh.
733 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
734  *  The generated invitation is a string containing a URL.
735  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
736  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
737  *
738  *  Calling this function is equal to callen meshlink_invite_ex() with flags set to 0.
739  *
740  *  @param mesh         A handle which represents an instance of MeshLink.
741  *  @param submesh      A handle which represents an instance of SubMesh.
742  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
743  *                      After this function returns, the application is free to overwrite or free @a name.
744  *
745  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
746  *                      The application should call free() after it has finished using the URL.
747  */
748 extern char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name);
749
750 /// Use an invitation to join a mesh.
751 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
752  *  An invitation can only be used if the local node has never connected to other nodes before.
753  *  After a successfully accepted invitation, the name of the local node may have changed.
754  *
755  *  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.
756  *
757  *  This function is blocking. It can take several seconds before it returns.
758  *  There is no guarantee it will perform a successful join.
759  *  Failures might be caused by temporary network outages, or by the invitation having expired.
760  *
761  *  @param mesh         A handle which represents an instance of MeshLink.
762  *  @param invitation   A nul-terminated C string containing the invitation URL.
763  *                      After this function returns, the application is free to overwrite or free @a invitation.
764  *
765  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
766  */
767 extern bool meshlink_join(meshlink_handle_t *mesh, const char *invitation);
768
769 /// Export the local node's key and addresses.
770 /** This function generates a string that contains the local node's public key and one or more IP addresses.
771  *  The application can pass it in some way to another node, which can then import it,
772  *  granting the local node access to the other node's mesh.
773  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
774  *
775  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
776  *
777  *  @param mesh         A handle which represents an instance of MeshLink.
778  *
779  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
780  *                      The application should call free() after it has finished using this string.
781  */
782 extern char *meshlink_export(meshlink_handle_t *mesh);
783
784 /// Import another node's key and addresses.
785 /** This function accepts a string containing the exported public key and addresses of another node.
786  *  By importing this data, the local node grants the other node access to its mesh.
787  *  The application should make sure that the data it imports is really coming from the node it wants to import,
788  *
789  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
790  *
791  *  @param mesh         A handle which represents an instance of MeshLink.
792  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
793  *                      After this function returns, the application is free to overwrite or free @a data.
794  *
795  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
796  */
797 extern bool meshlink_import(meshlink_handle_t *mesh, const char *data);
798
799 /// Blacklist a node from the mesh.
800 /** This function causes the local node to blacklist another node.
801  *  The local node will drop any existing connections to that node,
802  *  and will not send data to it nor accept any data received from it any more.
803  *
804  *  @param mesh         A handle which represents an instance of MeshLink.
805  *  @param node         A pointer to a meshlink_node_t describing the node to be blacklisted.
806  */
807 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
808
809 /// Whitelist a node on the mesh.
810 /** This function causes the local node to whitelist a previously blacklisted node.
811  *  The local node will allow connections to and from that node,
812  *  and will send data to it and accept any data received from it.
813  *
814  *  @param mesh         A handle which represents an instance of MeshLink.
815  *  @param node         A pointer to a meshlink_node_t describing the node to be whitelisted.
816  */
817 extern void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node);
818
819 /// Set whether new nodes are blacklisted by default.
820 /** This function sets the blacklist behaviour for newly discovered nodes.
821  *  If set to true, new nodes will be automatically blacklisted.
822  *  If set to false, which is the default, new nodes are automatically whitelisted.
823  *  The whitelist/blacklist status of a node may be changed afterwards with the
824  *  meshlink_whitelist() and meshlink_blacklist() functions.
825  *
826  *  @param mesh         A handle which represents an instance of MeshLink.
827  *  @param blacklist    True if new nodes are to be blacklisted, false if whitelisted.
828  */
829 extern void meshlink_set_default_blacklist(meshlink_handle_t *mesh, bool blacklist);
830
831 /// A callback for accepting incoming channels.
832 /** This function is called whenever a remote node wants to open a channel to the local node.
833  *  The application then has to decide whether to accept or reject this channel.
834  *
835  *  The callback is run in MeshLink's own thread.
836  *  It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
837  *  to hand any data over to the application's thread.
838  *
839  *  @param mesh         A handle which represents an instance of MeshLink.
840  *  @param channel      A handle for the incoming channel.
841  *                      If the application accepts the incoming channel by returning true,
842  *                      then this handle is valid until meshlink_channel_close() is called on it.
843  *                      If the application rejects the incoming channel by returning false,
844  *                      then this handle is invalid after the callback returns
845  *                      (the callback does not need to call meshlink_channel_close() itself in this case).
846  *  @param port         The port number the peer wishes to connect to.
847  *  @param data         A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
848  *                      The pointer is only valid during the lifetime of the callback.
849  *                      The callback should mempcy() the data if it needs to be available outside the callback.
850  *  @param len          The length of the data, or 0 in case no data has been received yet. (Not yet used.)
851  *
852  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
853  *                      If returning false, the channel is invalid and may not be used anymore.
854  */
855 typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len);
856
857 /// A callback for receiving data from a channel.
858 /** This function is called whenever data is received from a remote node on a channel.
859  *
860  *  This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
861  *  In both cases, @a data will be NULL and @a len will be 0, and meshlink_errno will be set.
862  *  In any case, the @a channel handle will still be valid until the application calls meshlink_close().
863  *
864  *  @param mesh         A handle which represents an instance of MeshLink.
865  *  @param channel      A handle for the channel.
866  *  @param data         A pointer to a buffer containing data sent by the source, or NULL in case of an error.
867  *                      The pointer is only valid during the lifetime of the callback.
868  *                      The callback should mempcy() the data if it needs to be available outside the callback.
869  *  @param len          The length of the data, or 0 in case of an error.
870  */
871 typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
872
873 /// A callback informing the application when data can be sent on a channel.
874 /** This function is called whenever there is enough free buffer space so a call to meshlink_channel_send() will succeed.
875  *
876  *  @param mesh         A handle which represents an instance of MeshLink.
877  *  @param channel      A handle for the channel.
878  *  @param len          The maximum amount of data that is guaranteed to be accepted by meshlink_channel_send(),
879  *                      or 0 in case of an error.
880  */
881 typedef void (*meshlink_channel_poll_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
882
883 /// Set the accept callback.
884 /** This functions sets the callback that is called whenever another node sends data to the local node.
885  *  The callback is run in MeshLink's own thread.
886  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
887  *  to hand the data over to the application's thread.
888  *  The callback should also not block itself and return as quickly as possible.
889  *
890  *  If no accept callback is set, incoming channels are rejected.
891  *
892  *  @param mesh      A handle which represents an instance of MeshLink.
893  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
894  *                   If a NULL pointer is given, the callback will be disabled.
895  */
896 extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
897
898 /// Set the receive callback.
899 /** This functions sets the callback that is called whenever another node sends data to the local node.
900  *  The callback is run in MeshLink's own thread.
901  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
902  *  to hand the data over to the application's thread.
903  *  The callback should also not block itself and return as quickly as possible.
904  *
905  *  @param mesh      A handle which represents an instance of MeshLink.
906  *  @param channel   A handle for the channel.
907  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
908  *                   If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
909  */
910 extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
911
912 /// Set the poll callback.
913 /** This functions sets the callback that is called whenever data can be sent to another node.
914  *  The callback is run in MeshLink's own thread.
915  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
916  *  to pass data to or from the application's thread.
917  *  The callback should also not block itself and return as quickly as possible.
918  *
919  *  @param mesh      A handle which represents an instance of MeshLink.
920  *  @param channel   A handle for the channel.
921  *  @param cb        A pointer to the function which will be called when data can be sent to another node.
922  *                   If a NULL pointer is given, the callback will be disabled.
923  */
924 extern void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb);
925
926 /// Open a reliable stream channel to another node.
927 /** This function is called whenever a remote node wants to open a channel to the local node.
928  *  The application then has to decide whether to accept or reject this channel.
929  *
930  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
931  *  When the application does no longer need to use this channel, it must call meshlink_close()
932  *  to free its resources.
933  *
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.
942  *  @param flags        A bitwise-or'd combination of flags that set the semantics for this channel.
943  *
944  *  @return             A handle for the channel, or NULL in case of an error.
945  *                      The handle is valid until meshlink_channel_close() is called.
946  */
947 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);
948
949 /// Open a reliable stream channel to another node.
950 /** This function is called whenever a remote node wants to open a channel to the local node.
951  *  The application then has to decide whether to accept or reject this channel.
952  *
953  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
954  *  When the application does no longer need to use this channel, it must call meshlink_close()
955  *  to free its resources.
956  *
957  *  Calling this function is equivalent to calling meshlink_channel_open_ex()
958  *  with the flags set to MESHLINK_CHANNEL_TCP.
959  *
960  *  @param mesh         A handle which represents an instance of MeshLink.
961  *  @param node         The node to which this channel is being initiated.
962  *  @param port         The port number the peer wishes to connect to.
963  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
964  *                      The pointer may be NULL, in which case incoming data is ignored.
965  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
966  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
967  *  @param len          The length of the data, or 0 if there is no data to send.
968  *
969  *  @return             A handle for the channel, or NULL in case of an error.
970  *                      The handle is valid until meshlink_channel_close() is called.
971  */
972 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);
973
974 /// Partially close a reliable stream channel.
975 /** This shuts down the read or write side of a channel, or both, without closing the handle.
976  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
977  *  but still allows waiting for incoming data from the remote node.
978  *
979  *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
980  *
981  *  @param mesh         A handle which represents an instance of MeshLink.
982  *  @param channel      A handle for the channel.
983  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
984  */
985 extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction);
986
987 /// Close a reliable stream channel.
988 /** This informs the remote node that the local node has finished sending all data on the channel.
989  *  It also causes the local node to stop accepting incoming data from the remote node.
990  *  It will free the struct meshlink_channel and all associated resources.
991  *  Afterwards, the channel handle is invalid and must not be used any more.
992  *
993  *  It is allowed to call this function at any time on a valid handle, even inside callback functions.
994  *  If called with a valid handle, this function always succeeds, otherwise the result is undefined.
995  *
996  *  @param mesh         A handle which represents an instance of MeshLink.
997  *  @param channel      A handle for the channel.
998  */
999 extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1000
1001 /// Transmit data on a channel
1002 /** This queues data to send to the remote node.
1003  *
1004  *  @param mesh         A handle which represents an instance of MeshLink.
1005  *  @param channel      A handle for the channel.
1006  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
1007  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
1008  *  @param len          The length of the data, or 0 if there is no data to send.
1009  *
1010  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
1011  */
1012 extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
1013
1014 /// Get channel flags.
1015 /** This returns the flags used when opening this channel.
1016  *
1017  *  @param mesh         A handle which represents an instance of MeshLink.
1018  *  @param channel      A handle for the channel.
1019  *
1020  *  @return             The flags set for this channel.
1021  */
1022 extern uint32_t meshlink_channel_get_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1023
1024 /// Get the amount of bytes in the send buffer.
1025 /** This returns the amount of bytes in the send buffer.
1026  *  These bytes have not been received by the peer yet.
1027  *
1028  *  @param mesh         A handle which represents an instance of MeshLink.
1029  *  @param channel      A handle for the channel.
1030  *
1031  *  @return             The amount of un-ACKed bytes in the send buffer.
1032  */
1033 extern size_t meshlink_channel_get_sendq(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1034
1035 /// Get the amount of bytes in the receive buffer.
1036 /** This returns the amount of bytes in the receive buffer.
1037  *  These bytes have not been processed by the application yet.
1038  *
1039  *  @param mesh         A handle which represents an instance of MeshLink.
1040  *  @param channel      A handle for the channel.
1041  *
1042  *  @return             The amount of bytes in the receive buffer.
1043  */
1044 extern size_t meshlink_channel_get_recvq(meshlink_handle_t *mesh, meshlink_channel_t *channel);
1045
1046 /// Hint that a hostname may be found at an address
1047 /** This function indicates to meshlink that the given hostname is likely found
1048  *  at the given IP address and port.
1049  *
1050  *  @param mesh     A handle which represents an instance of MeshLink.
1051  *  @param node     A pointer to a meshlink_node_t describing the node to add the address hint for.
1052  *  @param addr     The IP address and port which should be tried for the
1053  *                  given hostname. The caller is free to overwrite or free
1054  *                  this memory once meshlink returns.
1055  */
1056 extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr);
1057
1058 /// Enable or disable zeroconf discovery of local peers
1059
1060 /** This controls whether zeroconf discovery using the Catta library will be
1061  *  enabled to search for peers on the local network. By default, it is enabled.
1062  *
1063  *  @param mesh    A handle which represents an instance of MeshLink.
1064  *  @param enable  Set to true to enable discovery, false to disable.
1065  */
1066 extern void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable);
1067
1068 #ifdef __cplusplus
1069 }
1070 #endif
1071
1072 #endif