]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.h
Fix warnings from Doxygen.
[meshlink] / src / meshlink.h
index ffd05af2275d81f62f75ffb78f4199fabd29a0e4..b54e961e5c9adab0bf6b144e2a527ad34f558511 100644 (file)
 #ifndef MESHLINK_H
 #define MESHLINK_H
 
+#include <stdint.h>
 #include <stdbool.h>
 #include <stddef.h>
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/// The length in bytes of a signature made with meshlink_sign()
+#define MESHLINK_SIGLEN 64
 
 /// A handle for an instance of MeshLink.
 typedef struct meshlink_handle meshlink_handle_t;
@@ -29,23 +38,29 @@ typedef struct meshlink_handle meshlink_handle_t;
 /// A handle for a MeshLink node.
 typedef struct meshlink_node meshlink_node_t;
 
+/// A handle for a MeshLink channel.
+typedef struct meshlink_channel meshlink_channel_t;
+
 /// Code of most recent error encountered.
 typedef enum {
-       MESHLINK_OK,     // Everything is fine
-       MESHLINK_ENOMEM, // Out of memory
-       MESHLINK_ENOENT, // Node is not known
+       MESHLINK_OK,     ///< Everything is fine
+       MESHLINK_ENOMEM, ///< Out of memory
+       MESHLINK_ENOENT, ///< Node is not known
 } meshlink_errno_t;
 
 #ifndef MESHLINK_INTERNAL_H
 
 struct meshlink_handle {
-       meshlink_errno_t errno; /// Code of the last encountered error.
-       const char *errstr;     /// Textual representation of most recent error encountered.
+       meshlink_errno_t meshlink_errno; ///< Code of the last encountered error.
+       const char *errstr;              ///< Textual representation of most recent error encountered.
 };
 
 struct meshlink_node {
-       const char *name; // Textual name of this node.
-       void *priv;       // Private pointer which the application can set at will.
+       const char *name; ///< Textual name of this node.
+       void *priv;       ///< Private pointer which the application can set at will.
+};
+
+struct meshlink_channel {
 };
 
 #endif // MESHLINK_INTERNAL_H
@@ -66,6 +81,9 @@ extern const char *meshlink_strerror(meshlink_errno_t errno);
  *  but it is not a problem if it is run more than once, as long as
  *  the arguments given are the same.
  *
+ *  This function does not start any network I/O yet. The application should
+ *  first set callbacks, and then call meshlink_start().
+ *
  *  @param confbase The directory in which MeshLink will store its configuration files.
  *  @param name     The name which this instance of the application will use in the mesh.
  *
@@ -74,10 +92,10 @@ extern const char *meshlink_strerror(meshlink_errno_t errno);
 extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name);
 
 /// Start MeshLink.
-/** This function causes MeshLink to create a new thread, which will
- *  handle all network I/O.
+/** This function causes MeshLink to open network sockets, make outgoing connections, and
+ *  create a new thread, which will handle all network I/O.
  *
- *  @param confbase The directory in which MeshLink will store its configuration files.
+ *  @param mesh     A handle which represents an instance of MeshLink.
  *
  *  @return         This function will return true if MeshLink has succesfully started its thread, false otherwise.
  */
@@ -85,9 +103,9 @@ extern bool meshlink_start(meshlink_handle_t *mesh);
 
 /// Stop MeshLink.
 /** This function causes MeshLink to disconnect from all other nodes,
- *  and shuts down its own thread.
+ *  close all sockets, and shut down its own thread.
  *
- * @param handle    A handle which represents an instance of MeshLink.
+ *  @param mesh     A handle which represents an instance of MeshLink.
  */
 extern void meshlink_stop(meshlink_handle_t *mesh);
 
@@ -96,12 +114,12 @@ extern void meshlink_stop(meshlink_handle_t *mesh);
  *  and frees all memory allocated by MeshLink.
  *  Afterwards, the handle and any pointers to a struct meshlink_node are invalid.
  *
- * @param handle    A handle which represents an instance of MeshLink.
+ *  @param mesh     A handle which represents an instance of MeshLink.
  */
 extern void meshlink_close(meshlink_handle_t *mesh);
 
 /// A callback for receiving data from the mesh.
-/** @param handle    A handle which represents an instance of MeshLink.
+/** @param mesh      A handle which represents an instance of MeshLink.
  *  @param source    A pointer to a meshlink_node_t describing the source of the data.
  *  @param data      A pointer to a buffer containing the data sent by the source.
  *  @param len       The length of the received data.
@@ -115,13 +133,13 @@ typedef void (*meshlink_receive_cb_t)(meshlink_handle_t *mesh, meshlink_node_t *
  *  to hand the data over to the application's thread.
  *  The callback should also not block itself and return as quickly as possible.
  *
- *  @param handle    A handle which represents an instance of MeshLink.
+ *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
  */
 extern void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb);
 
 /// A callback reporting node status changes.
-/** @param handle     A handle which represents an instance of MeshLink.
+/** @param mesh       A handle which represents an instance of MeshLink.
  *  @param node       A pointer to a meshlink_node_t describing the node whose status changed.
  *  @param reachable  True if the node is reachable, false otherwise.
  */
@@ -134,22 +152,22 @@ typedef void (*meshlink_node_status_cb_t)(meshlink_handle_t *mesh, meshlink_node
  *  to hand the data over to the application's thread.
  *  The callback should also not block itself and return as quickly as possible.
  *
- *  @param handle    A handle which represents an instance of MeshLink.
+ *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param cb        A pointer to the function which will be called when another node's status changes.
  */
 extern void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb);
 
 /// Severity of log messages generated by MeshLink.
 typedef enum {
-       MESHLINK_DEBUG,    // Internal debugging messages. Only useful during application development.
-       MESHLINK_INFO,     // Informational messages.
-       MESHLINK_WARNING,  // Warnings which might indicate problems, but which are not real errors.
-       MESHLINK_ERROR,    // Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
-       MESHLINK_CRITICAL, // Critical errors which cause MeshLink to fail completely.
+       MESHLINK_DEBUG,    ///< Internal debugging messages. Only useful during application development.
+       MESHLINK_INFO,     ///< Informational messages.
+       MESHLINK_WARNING,  ///< Warnings which might indicate problems, but which are not real errors.
+       MESHLINK_ERROR,    ///< Errors which hamper correct functioning of MeshLink, without causing it to fail completely.
+       MESHLINK_CRITICAL, ///< Critical errors which cause MeshLink to fail completely.
 } meshlink_log_level_t;
 
 /// A callback for receiving log messages generated by MeshLink.
-/** @param handle    A handle which represents an instance of MeshLink.
+/** @param mesh      A handle which represents an instance of MeshLink.
  *  @param level     An enum describing the severity level of the message.
  *  @param text      A pointer to a string containing the textual log message.
  */
@@ -162,7 +180,7 @@ typedef void (*meshlink_log_cb_t)(meshlink_handle_t *mesh, meshlink_log_level_t
  *  to hand the data over to the application's thread.
  *  The callback should also not block itself and return as quickly as possible.
  *
- *  @param handle    A handle which represents an instance of MeshLink.
+ *  @param mesh      A handle which represents an instance of MeshLink.
  *  @param level     An enum describing the minimum severity level. Debugging information with a lower level will not trigger the callback.
  *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
  */
@@ -175,7 +193,7 @@ extern void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t le
  *  and that there is no guarantee that the packet will arrive at the destination.
  *  The application should take care of getting an acknowledgement and retransmission if necessary.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param destination  A pointer to a meshlink_node_t describing the destination for the data.
  *  @param data         A pointer to a buffer containing the data to be sent to the source.
  *  @param len          The length of the data.
@@ -187,7 +205,7 @@ extern bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination,
 /// Get a handle for a specific node.
 /** This function returns a handle for the node with the given name.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param name         The name of the node for which a handle is requested.
  *
  *  @return             A pointer to a meshlink_node_t which represents the requested node,
@@ -198,11 +216,11 @@ extern meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *n
 /// Get a list of all nodes.
 /** This function returns a list with handles for all known nodes.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param nodes        A pointer to an array of pointers to meshlink_node_t, which should be allocated by the application.
  *  @param nmemb        The maximum number of pointers that can be stored in the nodes array.
  *
- *  @param return       The number of known nodes. This can be larger than nmemb, in which case not all nodes were stored in the nodes array.
+ *  @return             The number of known nodes. This can be larger than nmemb, in which case not all nodes were stored in the nodes array.
  */
 extern size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t nmemb);
 
@@ -210,27 +228,39 @@ extern size_t meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **
 /** This function signs data using the local node's MeshLink key.
  *  The generated signature can be securely verified by other nodes.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param data         A pointer to a buffer containing the data to be signed.
  *  @param len          The length of the data to be signed.
+ *  @param signature    A pointer to a buffer where the signature will be stored.
+ *  @param siglen       The size of the signature buffer. Will be changed after the call to match the size of the signature itself.
  *
- *  @return             This function returns a pointer to a string containing the signature, or NULL in case of an error. 
- *                      The application should call free() after it has finished using the signature.
+ *  @return             This function returns true if the signature was correctly generated, false otherwise.
  */
-extern char *meshlink_sign(meshlink_handle_t *mesh, const char *data, size_t len);
+extern bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen);
 
 /// Verify the signature generated by another node of a piece of data.
 /** This function verifies the signature that another node generated for a piece of data.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param source       A pointer to a meshlink_node_t describing the source of the signature.
  *  @param data         A pointer to a buffer containing the data to be verified.
  *  @param len          The length of the data to be verified.
  *  @param signature    A pointer to a string containing the signature.
+ *  @param siglen       The size of the signature.
  *
  *  @return             This function returns true if the signature is valid, false otherwise.
  */
-extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len, const char *signature);
+extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen);
+
+/// Add an Address for the local node.
+/** This function adds an Address for the local node, which will be used for invitation URLs.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param address      A string containing the address, which can be either in numeric format or a hostname.
+ *
+ *  @return             This function returns true if the address was added, false otherwise.
+ */
+extern bool meshlink_add_address(meshlink_handle_t *mesh, const char *address);
 
 /// Invite another node into the mesh.
 /** This function generates an invitation that can be used by another node to join the same mesh as the local node.
@@ -238,7 +268,7 @@ extern bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, co
  *  This URL should be passed by the application to the invitee in a way that no eavesdroppers can see the URL.
  *  The URL can only be used once, after the user has joined the mesh the URL is no longer valid.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param name         The name that the invitee will use in the mesh.
  *
  *  @return             This function returns a string that contains the invitation URL.
@@ -251,7 +281,7 @@ extern char *meshlink_invite(meshlink_handle_t *mesh, const char *name);
  *  An invitation can only be used if the local node has never connected to other nodes before.
  *  After a succesfully accepted invitation, the name of the local node may have changed.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param invitation   A string containing the invitation URL.
  *
  *  @return             This function returns true if the local node joined the mesh it was invited to, false otherwise.
@@ -263,7 +293,7 @@ extern bool meshlink_join(meshlink_handle_t *mesh, const char *invitation);
  *  The application can pass it in some way to another node, which can then import it,
  *  granting the local node access to the other node's mesh.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *
  *  @return             This function returns a string that contains the exported key and addresses.
  *                      The application should call free() after it has finished using this string.
@@ -274,7 +304,7 @@ extern char *meshlink_export(meshlink_handle_t *mesh);
 /** This function accepts a string containing the exported public key and addresses of another node.
  *  By importing this data, the local node grants the other node access to its mesh.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param data         A string containing the other node's exported key and addresses.
  *
  *  @return             This function returns true if the data was valid and the other node has been granted access to the mesh, false otherwise.
@@ -286,9 +316,113 @@ extern bool meshlink_import(meshlink_handle_t *mesh, const char *data);
  *  The local node will drop any existing connections to that node,
  *  and will not send data to it nor accept any data received from it any more.
  *
- *  @param handle       A handle which represents an instance of MeshLink.
+ *  @param mesh         A handle which represents an instance of MeshLink.
  *  @param node         A pointer to a meshlink_node_t describing the node to be blacklisted.
  */
 extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node);
 
+/// A callback for accepting incoming channels.
+/** This function is called whenever a remote node wants to open a channel to the local node.
+ *  The application then has to decide whether to accept or reject this channel.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param channel      A handle for the incoming channel.
+ *  @param node         The node from which this channel is being initiated.
+ *  @param port         The port number the peer wishes to connect to.
+ *  @param data         A pointer to a buffer containing data already received. (Not yet used.)
+ *  @param len          The length of the data. (Not yet used.)
+ *
+ *  @return             This function should return true if the application accepts the incoming channel, false otherwise.
+ *                      If returning false, the channel is invalid and may not be used anymore.
+ */
+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);
+
+/// A callback for receiving data from a channel.
+/** This function is called whenever a remote node wants to open a channel to the local node.
+ *  The application then has to decide whether to accept or reject this channel.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param channel      A handle for the channel.
+ *  @param data         A pointer to a buffer containing data sent by the source.
+ *  @param len          The length of the data.
+ */
+typedef void (*meshlink_channel_receive_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
+
+/// Set the accept callback.
+/** This functions sets the callback that is called whenever another node sends data to the local node.
+ *  The callback is run in MeshLink's own thread.
+ *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
+ *  to hand the data over to the application's thread.
+ *  The callback should also not block itself and return as quickly as possible.
+ *
+ *  @param mesh      A handle which represents an instance of MeshLink.
+ *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ */
+extern void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb);
+
+/// Set the receive callback.
+/** This functions sets the callback that is called whenever another node sends data to the local node.
+ *  The callback is run in MeshLink's own thread.
+ *  It is therefore important that the callback uses apprioriate methods (queues, pipes, locking, etc.)
+ *  to hand the data over to the application's thread.
+ *  The callback should also not block itself and return as quickly as possible.
+ *
+ *  @param mesh      A handle which represents an instance of MeshLink.
+ *  @param channel   A handle for the channel.
+ *  @param cb        A pointer to the function which will be called when another node sends data to the local node.
+ */
+extern void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb);
+
+/// Open a reliable stream channel to another node.
+/** This function is called whenever a remote node wants to open a channel to the local node.
+ *  The application then has to decide whether to accept or reject this channel.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param node         The node to which this channel is being initiated.
+ *  @param port         The port number the peer wishes to connect to.
+ *  @param cb           A pointer to the function which will be called when the remote node sends data to the local node.
+ *  @param data         A pointer to a buffer containing data to already queue for sending.
+ *  @param len          The length of the data.
+ *
+ *  @return             A handle for the channel, or NULL in case of an error.
+ */
+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);
+
+/// Partially close a reliable stream channel.
+/** This shuts down the read or write side of a channel, or both, without closing the handle.
+ *  It can be used to inform the remote node that the local node has finished sending all data on the channel,
+ *  but still allows waiting for incoming data from the remote node.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param channel      A handle for the channel.
+ *  @param direction    Must be one of SHUT_RD, SHUT_WR or SHUT_RDWR.
+ */
+extern void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction);
+
+/// Close a reliable stream channel.
+/** This informs the remote node that the local node has finished sending all data on the channel.
+ *  It also causes the local node to stop accepting incoming data from the remote node.
+ *  Afterwards, the channel handle is invalid and must not be used any more.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param channel      A handle for the channel.
+ */
+extern void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel);
+
+/// Transmit data on a channel
+/** This queues data to send to the remote node.
+ *
+ *  @param mesh         A handle which represents an instance of MeshLink.
+ *  @param channel      A handle for the channel.
+ *  @param data         A pointer to a buffer containing data sent by the source.
+ *  @param len          The length of the data.
+ *
+ *  @return             The amount of data that was queued, which can be less than len, or a negative value in case of an error.
+ */
+extern ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif // MESHLINK_H