]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink_internal.h
Reorganize structs for better packing and access patterns.
[meshlink] / src / meshlink_internal.h
index c6c99541d66b0d7fa622c097313e1090b19dc09c..c711aa57d8863eb67b2ee82852a39d00dd842319 100644 (file)
@@ -3,7 +3,7 @@
 
 /*
     meshlink_internal.h -- Internal parts of the public API.
-    Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
+    Copyright (C) 2014-2019 Guus Sliepen <guus@meshlink.io>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#ifdef MESHLINK_H
+#error You must not include both meshlink.h and meshlink_internal.h!
+#endif
+
 #include "system.h"
 
 #include "event.h"
 
 #include <pthread.h>
 
-#define MAXSOCKETS 8    /* Probably overkill... */
+#define MAXSOCKETS 4    /* Probably overkill... */
 
 static const char meshlink_invitation_label[] = "MeshLink invitation";
 static const char meshlink_tcp_label[] = "MeshLink TCP";
 static const char meshlink_udp_label[] = "MeshLink UDP";
 
+#define MESHLINK_CONFIG_VERSION 1
+#define MESHLINK_INVITATION_VERSION 1
+
 struct CattaServer;
 struct CattaSServiceBrowser;
 struct CattaSimplePoll;
@@ -57,42 +64,49 @@ typedef enum proxytype_t {
        PROXY_HTTP,
 } proxytype_t;
 
-/// A handle for an instance of MeshLink.
-struct meshlink_handle {
-       char *name;
-       void *priv;
-
+struct meshlink_open_params {
+       char *confbase;
        char *appname;
+       char *name;
        dev_class_t devclass;
 
-       char *confbase;
+       int netns;
 
-       meshlink_receive_cb_t receive_cb;
-       meshlink_node_status_cb_t node_status_cb;
-       meshlink_log_cb_t log_cb;
-       meshlink_log_level_t log_level;
+       const void *key;
+       size_t keylen;
+};
 
-       meshlink_channel_accept_cb_t channel_accept_cb;
+/// A handle for an instance of MeshLink.
+struct meshlink_handle {
+       // public members
+       char *name;
+       void *priv;
 
-       pthread_t thread;
-       bool threadstarted;
+       // private members
        pthread_mutex_t mesh_mutex;
        event_loop_t loop;
-       listen_socket_t listen_socket[MAXSOCKETS];
+       struct node_t *self;
+       meshlink_log_cb_t log_cb;
+       meshlink_log_level_t log_level;
+
+       // The most important network-related members come first
        int listen_sockets;
+       listen_socket_t listen_socket[MAXSOCKETS];
+
+       meshlink_receive_cb_t receive_cb;
+       meshlink_queue_t outpacketqueue;
        signal_t datafromapp;
 
-       struct node_t *self;
+       hash_t *node_udp_cache;
 
-       struct splay_tree_t *config;
-       struct splay_tree_t *edges;
        struct splay_tree_t *nodes;
+       struct splay_tree_t *edges;
 
        struct list_t *connections;
        struct list_t *outgoings;
+       struct list_t *submeshes;
 
-       meshlink_queue_t outpacketqueue;
-
+       // Meta-connection-related members
        struct splay_tree_t *past_request_tree;
        timeout_t past_request_timeout;
 
@@ -103,46 +117,72 @@ struct meshlink_handle {
        timeout_t pingtimer;
        timeout_t periodictimer;
 
-       char *myport;
-
-       char *proxyhost;
-       char *proxyport;
-       char *proxyuser;
-       char *proxypass;
-       proxytype_t proxytype;
+       struct connection_t *everyone;
 
-       bool discovery;         // Whether Catta is enabled or not
-       bool localdiscovery;
-       sockaddr_t localdiscovery_address;
+       // Infrequently used callbacks
+       meshlink_node_status_cb_t node_status_cb;
+       meshlink_channel_accept_cb_t channel_accept_cb;
+       meshlink_node_duplicate_cb_t node_duplicate_cb;
+       meshlink_connection_try_cb_t connection_try_cb;
 
-       bool default_blacklist;
+       // Mesh parameters
+       char *appname;
+       char *myport;
 
-       hash_t *node_udp_cache;
-       struct connection_t *everyone;
+       struct ecdsa *private_key;
        struct ecdsa *invitation_key;
-       int invitation_timeout;
 
+       int32_t devclass;
+
+       int invitation_timeout;
        int pinginterval;       /* seconds between pings */
        int pingtimeout;        /* seconds to wait for response */
        int maxtimeout;
 
-       int sock;
-       sptps_t sptps;
-       char cookie[18], hash[18];
-       char *data;
-       size_t thedatalen;
-       bool success;
-       char line[4096];
-       char buffer[4096];
-       size_t blen;
+       int netns;
 
-       pthread_t discovery_thread;
+       bool default_blacklist;
+       bool discovery;         // Whether Catta is enabled or not
+
+
+       // Configuration
+       char *confbase;
+       FILE *conffile;
+       void *config_key;
+
+       // Thread management
+       pthread_t thread;
+       bool threadstarted;
        bool discovery_threadstarted;
+
+       // Catta
+       pthread_t discovery_thread;
        struct CattaServer *catta_server;
        struct CattaSServiceBrowser *catta_browser;
        struct CattaSimplePoll *catta_poll;
        struct CattaSEntryGroup *catta_group;
        char *catta_servicetype;
+       unsigned int catta_interfaces;
+
+       // State used for meshlink_join()
+       int sock;
+       char cookie[18], hash[18];
+       bool success;
+       sptps_t sptps;
+       char *data;
+       size_t thedatalen;
+       size_t blen;
+       char line[4096];
+       char buffer[4096];
+
+       // Unused variables
+       char *proxyhost;
+       char *proxyport;
+       char *proxyuser;
+       char *proxypass;
+       proxytype_t proxytype;
+       sockaddr_t localdiscovery_address;
+       bool localdiscovery;
 };
 
 /// A handle for a MeshLink node.
@@ -151,6 +191,12 @@ struct meshlink_node {
        void *priv;
 };
 
+/// A handle for a node Sub-Mesh.
+struct meshlink_submesh {
+       const char *name;
+       void *priv;
+};
+
 /// A channel.
 struct meshlink_channel {
        struct node_t *node;
@@ -172,6 +218,8 @@ extern void update_node_status(meshlink_handle_t *mesh, struct node_t *n);
 extern meshlink_log_level_t global_log_level;
 extern meshlink_log_cb_t global_log_cb;
 extern int check_port(meshlink_handle_t *mesh);
+extern void handle_duplicate_node(meshlink_handle_t *mesh, struct node_t *n);
+extern void handle_network_change(meshlink_handle_t *mesh, bool online);
 
 /// Device class traits
 typedef struct {