]> git.meshlink.io Git - meshlink/blob - src/meshlink.h
Removed references to HAVE_STRUCT_SOCKADDR_STORAGE.
[meshlink] / src / meshlink.h
1 /*
2     meshlink.h -- MeshLink API
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MESHLINK_H
21 #define MESHLINK_H
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  (64)
41
42 // The maximum length of fingerprints
43 #define MESHLINK_FINGERPRINTLEN  (64)
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 edge.
52 typedef struct meshlink_edge meshlink_edge_t;
53
54 /// A handle for a MeshLink channel.
55 typedef struct meshlink_channel meshlink_channel_t;
56
57 /// Code of most recent error encountered.
58 typedef enum {
59         MESHLINK_OK,     ///< Everything is fine
60         MESHLINK_EINVAL, ///< Invalid parameter(s) to function call
61         MESHLINK_ENOMEM, ///< Out of memory
62         MESHLINK_ENOENT, ///< Node is not known
63         MESHLINK_EEXIST, ///< Node already exists
64         MESHLINK_EINTERNAL, ///< MeshLink internal error
65         MESHLINK_ERESOLV, ///< MeshLink could not resolve a hostname
66         MESHLINK_ESTORAGE, ///< MeshLink coud not load or write data from/to disk
67         MESHLINK_ENETWORK, ///< MeshLink encountered a network error
68         MESHLINK_EPEER, ///< A peer caused an error
69 } meshlink_errno_t;
70
71 // Device class
72 typedef enum {
73         BACKBONE = 1,
74         STATIONARY = 2,
75         PORTABLE = 3
76 } dclass_t;
77
78 /// A variable holding the last encountered error from MeshLink.
79 /** This is a thread local variable that contains the error code of the most recent error
80  *  encountered by a MeshLink API function called in the current thread.
81  *  The variable is only updated when an error is encountered, and is not reset to MESHLINK_OK
82  *  if a function returned succesfully.
83  */
84 extern __thread meshlink_errno_t meshlink_errno;
85
86 #ifndef MESHLINK_INTERNAL_H
87
88 struct meshlink_handle {
89         const char *name;
90         void *priv;
91 };
92
93 struct meshlink_node {
94         const char *name; ///< Textual name of this node. It is stored in a nul-terminated C string, which is allocated by MeshLink.
95         void *priv;       ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink.
96 };
97
98 struct meshlink_channel {
99 };
100
101 #endif // MESHLINK_INTERNAL_H
102
103 /// An edge in the meshlink network.
104 struct meshlink_edge {
105         struct meshlink_node *from;     ///< Pointer to a node. Node memory is
106                                         //   owned by meshlink and should not be
107                                         //   deallocated. Node contents may be
108                                         //   changed by meshlink.
109         struct meshlink_node *to;       ///< Pointer to a node. Node memory is
110                                         //   owned by meshlink and should not be
111                                         //   deallocated. Node contents may be
112                                         //   changed by meshlink.
113         struct sockaddr_storage address;///< The address information associated
114                                         //   with this edge.
115         uint32_t options;               ///< Edge options. @TODO what are edge options?
116         int weight;                     ///< Weight assigned to this edge.
117 };
118
119 /// Get the text for the given MeshLink error code.
120 /** This function returns a pointer to the string containing the description of the given error code.
121  *
122  *  @param err      An error code returned by MeshLink.
123  *
124  *  @return         A pointer to a string containing the description of the error code.
125  *                  The pointer is to static storage that is valid for the lifetime of the application.
126  *                  This function will always return a valid pointer, even if an invalid error code has been passed.
127  */
128 extern const char *meshlink_strerror(meshlink_errno_t err);
129
130 /// Open or create a MeshLink instance.
131 /** This function opens or creates a MeshLink instance.
132  *  The state is stored in the configuration directory passed in the variable @a confbase @a.
133  *  If the configuration directory does not exist yet, for example when it is the first time
134  *  this instance is opened, the configuration directory will be automatically created and initialized.
135  *  However, the parent directory should already exist, otherwise an error will be returned.
136  *
137  *  The name given should be a unique identifier for this instance.
138  *
139  *  This function returns a pointer to a struct meshlink_handle that will be allocated by MeshLink.
140  *  When the application does no longer need to use this handle, it must call meshlink_close() to
141  *  free its resources.
142  *
143  *  This function does not start any network I/O yet. The application should
144  *  first set callbacks, and then call meshlink_start().
145  *
146  *  @param confbase The directory in which MeshLink will store its configuration files.
147  *                  After the function returns, the application is free to overwrite or free @a confbase @a.
148  *  @param name     The name which this instance of the application will use in the mesh.
149  *                  After the function returns, the application is free to overwrite or free @a name @a.
150  *  @param appname  The application name which will be used in the mesh.
151  *                  After the function returns, the application is free to overwrite or free @a name @a.
152  *  @param dclass   The device class which will be used in the mesh.
153  *
154  *  @return         A pointer to a meshlink_handle_t which represents this instance of MeshLink, or NULL in case of an error.
155  *                  The pointer is valid until meshlink_close() is called.
156  */
157 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dclass_t dclass);
158
159 /// is used by the C++ wrapper to allocate more memory behind the handle
160 extern meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dclass_t dclass, size_t size);
161
162 /// Start MeshLink.
163 /** This function causes MeshLink to open network sockets, make outgoing connections, and
164  *  create a new thread, which will handle all network I/O.
165  *
166  *  It is allowed to call this function even if MeshLink is already started, in which case it will return true.
167  *
168  *  @param mesh     A handle which represents an instance of MeshLink.
169  *
170  *  @return         This function will return true if MeshLink has succesfully started, false otherwise.
171  */
172 extern bool meshlink_start(meshlink_handle_t *mesh);
173
174 /// Stop MeshLink.
175 /** This function causes MeshLink to disconnect from all other nodes,
176  *  close all sockets, and shut down its own thread.
177  *
178  *  This function always succeeds. It is allowed to call meshlink_stop() even if MeshLink is already stopped or has never been started.
179  *
180  *  @param mesh     A handle which represents an instance of MeshLink.
181  */
182 extern void meshlink_stop(meshlink_handle_t *mesh);
183
184 /// Close the MeshLink handle.
185 /** This function calls meshlink_stop() if necessary,
186  *  and frees the struct meshlink_handle and all associacted memory allocated by MeshLink.
187  *  Afterwards, the handle and any pointers to a struct meshlink_node or struct meshlink_channel are invalid.
188  *
189  *  It is allowed to call this function at any time on a valid handle, except inside callback functions.
190  *  If called at a proper time with a valid handle, this function always succeeds.
191  *  If called within a callback or with an invalid handle, the result is undefined.
192  *
193  *  @param mesh     A handle which represents an instance of MeshLink.
194  */
195 extern void meshlink_close(meshlink_handle_t *mesh);
196
197 /// A callback for receiving data from the mesh.
198 /** @param mesh      A handle which represents an instance of MeshLink.
199  *  @param source    A pointer to a meshlink_node_t describing the source of the data.
200  *  @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).
201  *                   The pointer is only valid during the lifetime of the callback.
202  *                   The callback should mempcy() the data if it needs to be available outside the callback.
203  *  @param len       The length of the received data, or 0 in case there is no data.
204  */
205 typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len);
206
207 /// Set the receive callback.
208 /** This functions sets the callback that is called whenever another node sends data to the local node.
209  *  The callback is run in MeshLink's own thread.
210  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
211  *  to hand the data over to the application's thread.
212  *  The callback should also not block itself and return as quickly as possible.
213  *
214  *  @param mesh      A handle which represents an instance of MeshLink.
215  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
216  *                   If a NULL pointer is given, the callback will be disabled.
217  */
218 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
219
220 /// A callback reporting node status changes.
221 /** @param mesh       A handle which represents an instance of MeshLink.
222  *  @param node       A pointer to a meshlink_node_t describing the node whose status changed.
223  *                    This pointer is valid until meshlink_close() is called.
224  *  @param reachable  True if the node is reachable, false otherwise.
225  */
226 typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable);
227
228 /// Set the node status callback.
229 /** This functions sets the callback that is called whenever another node's status changed.
230  *  The callback is run in MeshLink's own thread.
231  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
232  *  to hand the data over to the application's thread.
233  *  The callback should also not block itself and return as quickly as possible.
234  *
235  *  @param mesh      A handle which represents an instance of MeshLink.
236  *  @param cb        A pointer to the function which will be called when another node's status changes.
237  *                   If a NULL pointer is given, the callback will be disabled.
238  */
239 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
240
241 /// Severity of log messages generated by MeshLink.
242 typedef enum {
243         MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
244         MESHLINK_INFO,     ///< Informational messages.
245         MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
246         MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
247         MESHLINK_CRITICAL, ///< Critical errors which cause MeshLink to fail completely.
248 } meshlink_log_level_t;
249
250 /// A callback for receiving log messages generated by MeshLink.
251 /** @param mesh      A handle which represents an instance of MeshLink, or NULL.
252  *  @param level     An enum describing the severity level of the message.
253  *  @param text      A pointer to a nul-terminated C string containing the textual log message.
254  *                   This pointer is only valid for the duration of the callback.
255  *                   The application must not free() this pointer.
256  *                   The application should strdup() the text if it has to be available outside the callback.
257  */
258 typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
259
260 /// Set the log callback.
261 /** This functions sets the callback that is called whenever MeshLink has some information to log.
262  *
263  *  The @a mesh @a parameter can either be a valid MeshLink handle, or NULL.
264  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
265  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
266  *
267  *  If @a mesh @a is not NULL, then the callback is run in MeshLink's own thread.
268  *  It is important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
269  *  to hand the data over to the application's thread.
270  *  The callback should also not block itself and return as quickly as possible.
271  *
272  *  The @a mesh @a parameter can either be a valid MeshLink handle, or NULL.
273  *  In case it is NULL, the callback will be called for errors that happen outside the context of a valid mesh instance.
274  *  Otherwise, it will be called for errors that happen in the context of the given mesh instance.
275  *
276  *  @param mesh      A handle which represents an instance of MeshLink, or NULL.
277  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
278  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
279  *                   If a NULL pointer is given, the callback will be disabled.
280  */
281 extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb);
282
283 /// Send data to another node.
284 /** This functions sends one packet of data to another node in the mesh.
285  *  The packet is sent using UDP semantics, which means that
286  *  the packet is sent as one unit and is received as one unit,
287  *  and that there is no guarantee that the packet will arrive at the destination.
288  *  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.
289  *  The application should not send packets that are larger than the path MTU, which can be queried with meshlink_get_pmtu().
290  *  The application should take care of getting an acknowledgement and retransmission if necessary.
291  *
292  *  @param mesh         A handle which represents an instance of MeshLink.
293  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
294  *  @param data         A pointer to a buffer containing the data to be sent to the source.
295  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
296  *                      It is valid to specify a NULL pointer, but only if @a len @a is also 0.
297  *  @param len          The length of the data.
298  *  @return             This function will return true if MeshLink has queued the message for transmission, and false otherwise.
299  *                      A return value of true does not guarantee that the message will actually arrive at the destination.
300  */
301 extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len);
302
303 /// Query the maximum packet size that can be sent to a node.
304 /** This functions returns the maximum size of packets (path MTU) that can be sent to a specific node with meshlink_send().
305  *  The path MTU is a property of the path packets will take to the destination node over the Internet.
306  *  It can be different for different destination nodes.
307  *  and the path MTU can change at any point in time due to changes in the Internet.
308  *  Therefore, although this should only occur rarely, it can still happen that packets that do not exceed this size get dropped.
309  *
310  *  @param mesh         A handle which represents an instance of MeshLink.
311  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
312  *
313  *  @return             The recommended maximum size of packets that are to be sent to the destination node, 0 if the node is unreachable,
314  *                      or a negative value in case of an error.
315  */
316 extern ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination);
317
318 /// Get a handle for a specific node.
319 /** This function returns a handle for the node with the given name.
320  *
321  *  @param mesh         A handle which represents an instance of MeshLink.
322  *  @param name         The name of the node for which a handle is requested.
323  *                      After this function returns, the application is free to overwrite or free @a name @a.
324  *
325  *  @return             A pointer to a meshlink_node_t which represents the requested node,
326  *                      or NULL if the requested node does not exist.
327  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
328  */
329 extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name);
330
331 /// Get the fingerprint of a node's public key.
332 /** This function returns a fingerprint of the node's public key.
333  *  It should be treated as an opaque blob.
334  *
335  *  @param mesh         A handle which represents an instance of MeshLink.
336  *  @param node         A pointer to a meshlink_node_t describing the node.
337  *
338  *  @return             A nul-terminated C string containing the fingerprint of the node's public key in a printable ASCII format.
339  *                      The application should call free() after it is done using this string.
340  */
341 extern char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node);
342
343 /// Get a list of all nodes.
344 /** This function returns a list with handles for all known nodes.
345  *
346  *  @param mesh         A handle which represents an instance of MeshLink.
347  *  @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.
348  *                      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).
349  *                      The application is allowed to call free() on the array whenever it wishes.
350  *                      The pointers in the array are valid until meshlink_close() is called.
351  *  @param nmemb        A pointer to a variable holding the number of nodes that are stored in the array.
352  *                      In case the @a nodes @a argument is not NULL, MeshLink might call realloc() on the array to change its size.
353  *                      The contents of this variable will be changed to reflect the new size of the array.
354  *
355  *  @return             A pointer to an array containing pointers to all known nodes, or NULL in case of an error.
356  *                      If the @a nodes @a argument was not NULL, then the return value can either be the same value or a different value.
357  *                      If it is a new value, the old value of @a nodes @a should not be used anymore.
358  *                      If the new value is NULL, then the old array will have been freed by MeshLink.
359  */
360 extern meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb);
361
362 /// Sign data using the local node's MeshLink key.
363 /** This function signs data using the local node's MeshLink key.
364  *  The generated signature can be securely verified by other nodes.
365  *
366  *  @param mesh         A handle which represents an instance of MeshLink.
367  *  @param data         A pointer to a buffer containing the data to be signed.
368  *  @param len          The length of the data to be signed.
369  *  @param signature    A pointer to a buffer where the signature will be stored.
370  *                      The buffer must be allocated by the application, and should be at least MESHLINK_SIGLEN bytes big.
371  *                      The signature is a binary blob, and is not nul-terminated.
372  *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
373  *
374  *  @return             This function returns true if the signature was correctly generated, false otherwise.
375  */
376 extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen);
377
378 /// Verify the signature generated by another node of a piece of data.
379 /** This function verifies the signature that another node generated for a piece of data.
380  *
381  *  @param mesh         A handle which represents an instance of MeshLink.
382  *  @param source       A pointer to a meshlink_node_t describing the source of the signature.
383  *  @param data         A pointer to a buffer containing the data to be verified.
384  *  @param len          The length of the data to be verified.
385  *  @param signature    A pointer to a buffer where the signature is stored.
386  *  @param siglen       A pointer to a variable holding the size of the signature buffer.
387  *                      The contents of the variable will be changed by meshlink_sign() to reflect the actual size of the signature.
388  *
389  *  @return             This function returns true if the signature is valid, false otherwise.
390  */
391 extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen);
392
393 /// Add an Address for the local node.
394 /** This function adds an Address for the local node, which will be used for invitation URLs.
395  *
396  *  @param mesh         A handle which represents an instance of MeshLink.
397  *  @param address      A nul-terminated C string containing the address, which can be either in numeric format or a hostname.
398  *
399  *  @return             This function returns true if the address was added, false otherwise.
400  */
401 extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
402
403 /// Invite another node into the mesh.
404 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
405  *  The generated invitation is a string containing a URL.
406  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
407  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
408  *
409  *  @param mesh         A handle which represents an instance of MeshLink.
410  *  @param name         A nul-terminated C string containing the name that the invitee will be allowed to use in the mesh.
411  *                      After this function returns, the application is free to overwrite or free @a name @a.
412  *
413  *  @return             This function returns a nul-terminated C string that contains the invitation URL, or NULL in case of an error.
414  *                      The application should call free() after it has finished using the URL.
415  */
416 extern char *meshlink_invite(meshlink_handle_t *mesh, const char *name);
417
418 /// Use an invitation to join a mesh.
419 /** This function allows the local node to join an existing mesh using an invitation URL generated by another node.
420  *  An invitation can only be used if the local node has never connected to other nodes before.
421  *  After a succesfully accepted invitation, the name of the local node may have changed.
422  *
423  *  @param mesh         A handle which represents an instance of MeshLink.
424  *  @param invitation   A nul-terminated C string containing the invitation URL.
425  *                      After this function returns, the application is free to overwrite or free @a invitation @a.
426  *
427  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
428  */
429 extern bool meshlink_join(meshlink_handle_t *mesh, const char *invitation);
430
431 /// Export the local node's key and addresses.
432 /** This function generates a string that contains the local node's public key and one or more IP addresses.
433  *  The application can pass it in some way to another node, which can then import it,
434  *  granting the local node access to the other node's mesh.
435  *  The exported data does not contain any secret keys, it is therefore safe to transmit this data unencrypted over public networks.
436  *
437  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
438  *
439  *  @param mesh         A handle which represents an instance of MeshLink.
440  *
441  *  @return             This function returns a nul-terminated C string that contains the exported key and addresses, or NULL in case of an error.
442  *                      The application should call free() after it has finished using this string.
443  */
444 extern char *meshlink_export(meshlink_handle_t *mesh);
445
446 /// Import another node's key and addresses.
447 /** This function accepts a string containing the exported public key and addresses of another node.
448  *  By importing this data, the local node grants the other node access to its mesh.
449  *  The application should make sure that the data it imports is really coming from the node it wants to import,
450  *
451  *  Note that to create a working connection between two nodes, both must call meshink_export() and both must meshlink_import() each other's data.
452  *
453  *  @param mesh         A handle which represents an instance of MeshLink.
454  *  @param data         A nul-terminated C string containing the other node's exported key and addresses.
455  *                      After this function returns, the application is free to overwrite or free @a data @a.
456  *
457  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
458  */
459 extern bool meshlink_import(meshlink_handle_t *mesh, const char *data);
460
461 /// Blacklist a node from the mesh.
462 /** This function causes the local node to blacklist another node.
463  *  The local node will drop any existing connections to that node,
464  *  and will not send data to it nor accept any data received from it any more.
465  *
466  *  @param mesh         A handle which represents an instance of MeshLink.
467  *  @param node         A pointer to a meshlink_node_t describing the node to be blacklisted.
468  */
469 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
470
471 /// Whitelist a node on the mesh.
472 /** This function causes the local node to whitelist a previously blacklisted node.
473  *  The local node will allow connections to and from that node,
474  *  and will send data to it and accept any data received from it.
475  *
476  *  @param mesh         A handle which represents an instance of MeshLink.
477  *  @param node         A pointer to a meshlink_node_t describing the node to be blacklisted.
478  */
479 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
480
481 /// A callback for accepting incoming channels.
482 /** This function is called whenever a remote node wants to open a channel to the local node.
483  *  The application then has to decide whether to accept or reject this channel.
484  *
485  *  The callback is run in MeshLink's own thread.
486  *  It is therefore important that the callback return quickly and uses apprioriate methods (queues, pipes, locking, etc.)
487  *  to hand any data over to the application's thread.
488  *
489  *  @param mesh         A handle which represents an instance of MeshLink.
490  *  @param channel      A handle for the incoming channel.
491  *                      If the application accepts the incoming channel by returning true,
492  *                      then this handle is valid until meshlink_channel_close() is called on it.
493  *                      If the application rejects the incoming channel by returning false,
494  *                      then this handle is invalid after the callback returns
495  *                      (the callback does not need to call meshlink_channel_close() itself in this case).
496  *  @param node         The node from which this channel is being initiated.
497  *                      The pointer is guaranteed to be valid until meshlink_close() is called.
498  *  @param port         The port number the peer wishes to connect to.
499  *  @param data         A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.)
500  *                      The pointer is only valid during the lifetime of the callback.
501  *                      The callback should mempcy() the data if it needs to be available outside the callback.
502  *  @param len          The length of the data, or 0 in case no data has been received yet. (Not yet used.)
503  *
504  *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
505  *                      If returning false, the channel is invalid and may not be used anymore.
506  */
507 typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_node_t *node, uint16_t port, const void *data, size_t len);
508
509 /// A callback for receiving data from a channel.
510 /** This function is called whenever data is received from a remote node on a channel.
511  *
512  *  This function is also called in case the channel has been closed by the remote node, or when the channel is terminated abnormally.
513  *  In both cases, @a data @a will be NULL and @a len @a will be 0, and meshlink_errno will be set.
514  *  In any case, the @a channel @a handle will still be valid until the application calls meshlink_close().
515  *
516  *  @param mesh         A handle which represents an instance of MeshLink.
517  *  @param channel      A handle for the channel.
518  *  @param data         A pointer to a buffer containing data sent by the source, or NULL in case of an error.
519  *                      The pointer is only valid during the lifetime of the callback.
520  *                      The callback should mempcy() the data if it needs to be available outside the callback.
521  *  @param len          The length of the data, or 0 in case of an error.
522  */
523 typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
524
525 /// Set the accept callback.
526 /** This functions sets the callback that is called whenever another node sends data to the local node.
527  *  The callback is run in MeshLink's own thread.
528  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
529  *  to hand the data over to the application's thread.
530  *  The callback should also not block itself and return as quickly as possible.
531  *
532  *  If no accept callback is set, incoming channels are rejected.
533  *
534  *  @param mesh      A handle which represents an instance of MeshLink.
535  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
536  *                   If a NULL pointer is given, the callback will be disabled.
537  */
538 extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
539
540 /// Set the receive callback.
541 /** This functions sets the callback that is called whenever another node sends data to the local node.
542  *  The callback is run in MeshLink's own thread.
543  *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
544  *  to hand the data over to the application's thread.
545  *  The callback should also not block itself and return as quickly as possible.
546  *
547  *  @param mesh      A handle which represents an instance of MeshLink.
548  *  @param channel   A handle for the channel.
549  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
550  *                   If a NULL pointer is given, the callback will be disabled and incoming data is ignored.
551  */
552 extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
553
554 /// Open a reliable stream channel to another node.
555 /** This function is called whenever a remote node wants to open a channel to the local node.
556  *  The application then has to decide whether to accept or reject this channel.
557  *
558  *  This function returns a pointer to a struct meshlink_channel that will be allocated by MeshLink.
559  *  When the application does no longer need to use this channel, it must call meshlink_close()
560  *  to free its resources.
561  *
562  *  @param mesh         A handle which represents an instance of MeshLink.
563  *  @param node         The node to which this channel is being initiated.
564  *  @param port         The port number the peer wishes to connect to.
565  *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
566  *                      The pointer may be NULL, in which case incoming data is ignored.
567  *  @param data         A pointer to a buffer containing data to already queue for sending, or NULL if there is no data to send.
568  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
569  *  @param len          The length of the data, or 0 if there is no data to send.
570  *
571  *  @return             A handle for the channel, or NULL in case of an error.
572  *                      The handle is valid until meshlink_channel_close() is called.
573  */
574 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);
575
576 /// Partially close a reliable stream channel.
577 /** This shuts down the read or write side of a channel, or both, without closing the handle.
578  *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
579  *  but still allows waiting for incoming data from the remote node.
580  *
581  *  Shutting down the receive direction is also possible, and is equivalent to setting the receive callback to NULL.
582  *
583  *  @param mesh         A handle which represents an instance of MeshLink.
584  *  @param channel      A handle for the channel.
585  *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR, otherwise this call will not have any affect.
586  */
587 extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction);
588
589 /// Close a reliable stream channel.
590 /** This informs the remote node that the local node has finished sending all data on the channel.
591  *  It also causes the local node to stop accepting incoming data from the remote node.
592  *  It will free the struct meshlink_channel and all associated resources.
593  *  Afterwards, the channel handle is invalid and must not be used any more.
594  *
595  *  It is allowed to call this function at any time on a valid handle, even inside callback functions.
596  *  If called with a valid handle, this function always succeeds, otherwise the result is undefined.
597  *
598  *  @param mesh         A handle which represents an instance of MeshLink.
599  *  @param channel      A handle for the channel.
600  */
601 extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel);
602
603 /// Transmit data on a channel
604 /** This queues data to send to the remote node.
605  *
606  *  @param mesh         A handle which represents an instance of MeshLink.
607  *  @param channel      A handle for the channel.
608  *  @param data         A pointer to a buffer containing data sent by the source, or NULL if there is no data to send.
609  *                      After meshlink_send() returns, the application is free to overwrite or free this buffer.
610  *  @param len          The length of the data, or 0 if there is no data to send.
611  *
612  *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
613  */
614 extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
615
616 /// Hint that a hostname may be found at an address
617 /** This function indicates to meshlink that the given hostname is likely found
618  *  at the given IP address and port.
619  *
620  *  @param mesh         A handle which represents an instance of MeshLink.
621  *  @param hostname     The hostname which can be found at the given address.
622  *                      The caller is free to overwrite or free this string
623  *                      once meshlink returns.
624  *  @param addr         The IP address and port which should be tried for the
625  *                      given hostname. The caller is free to overwrite or free
626  *                      this memory once meshlink returns.
627  */
628 extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr);
629
630 /// Get a list of edges.
631 /** This function returns an array with copies of all known bidirectional edges.
632  *  The edges are copied to capture the mesh state at call time, since edges
633  *  mutate frequently. The nodes pointed to within the meshlink_edge_t type
634  *  are not copies; these are the same pointers that one would get from a call
635  *  to meshlink_get_all_nodes().
636  *
637  *  @param mesh         A handle which represents an instance of MeshLink.
638  *  @param edges        A pointer to a previously allocated array of pointers to
639  *                      meshlink_edge_t, or NULL in which case MeshLink will
640  *                      allocate a new array. The application CANNOT supply an
641  *                      array it allocated itself with malloc, but CAN use
642  *                      the return value from the previous call to this function
643  *                      (which is the preferred way).
644  *                      The pointers in the array are valid until meshlink_close() is called.
645  *  @param nmemb        A pointer to a variable holding the number of nodes that
646  *                      are stored in the array. In case the @a nodes @a
647  *                      argument is not NULL, MeshLink might call realloc()
648  *                      on the array to change its size.
649  *                      The contents of this variable will be changed to reflect
650  *                      the new size of the array.
651  *  
652  *  @return             A pointer to an array containing pointers to all known 
653  *                      edges, or NULL in case of an error.
654  *                      If the @a edges @a argument was not NULL, then the
655  *                      retun value can be either the same value or a different
656  *                      value. If the new values is NULL, then the old array
657  *                      will have been freed by Meshlink.
658  *                      The caller must call free() on each element of this
659  *                      array (but not the contents of said elements),
660  *                      as well as the array itself when it is finished.
661  */
662 extern meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink_edge_t **edges, size_t *nmemb);
663
664 #ifdef __cplusplus
665 }
666 #endif
667
668 #endif // MESHLINK_H