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