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