]> git.meshlink.io Git - meshlink/blob - src/meshlink_internal.h
Add a public API for the thread-safe message queue.
[meshlink] / src / meshlink_internal.h
1 /*
2     meshlink_internal.h -- Internal parts of the public 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_INTERNAL_H
21 #define MESHLINK_INTERNAL_H
22
23 #include "system.h"
24
25 #include "event.h"
26 #include "hash.h"
27 #include "logger.h"
28 #include "meshlink.h"
29 #include "meshlink_queue.h"
30 #include "sockaddr.h"
31 #include "sptps.h"
32
33 #include <pthread.h>
34
35 #define MAXSOCKETS 8    /* Probably overkill... */
36
37 typedef struct listen_socket_t {
38         struct io_t tcp;
39         struct io_t udp;
40         sockaddr_t sa;
41         bool bindto;
42 } listen_socket_t;
43
44 typedef enum proxytype_t {
45         PROXY_NONE = 0,
46         PROXY_SOCKS4,
47         PROXY_SOCKS4A,
48         PROXY_SOCKS5,
49         PROXY_HTTP,
50         PROXY_EXEC,
51 } proxytype_t;
52
53 typedef struct outpacketqueue {
54         meshlink_node_t *destination;
55         const void *data;
56         unsigned int len;
57 } outpacketqueue_t;
58
59 /// A handle for an instance of MeshLink.
60 struct meshlink_handle {
61         char *name;
62         void *priv;
63
64         char *confbase;
65
66         meshlink_receive_cb_t receive_cb;
67         meshlink_node_status_cb_t node_status_cb;
68         meshlink_log_cb_t log_cb;
69         meshlink_log_level_t log_level;
70
71         pthread_t thread;
72         bool threadstarted;
73         pthread_mutex_t outpacketqueue_mutex;
74         pthread_mutex_t nodes_mutex;
75         event_loop_t loop;
76         listen_socket_t listen_socket[MAXSOCKETS];
77         int listen_sockets;
78         signal_t datafromapp;
79
80         struct node_t *self;
81
82         struct splay_tree_t *config;
83         struct splay_tree_t *edges;
84         struct splay_tree_t *nodes;
85
86         struct list_t *connections;
87         struct list_t *outgoings;
88
89         meshlink_queue_t outpacketqueue;
90
91         struct splay_tree_t *past_request_tree;
92         timeout_t past_request_timeout;
93
94         int contradicting_add_edge;
95         int contradicting_del_edge;
96         int sleeptime;
97         time_t last_config_check;
98         timeout_t pingtimer;
99         timeout_t periodictimer;
100
101         char *myport;
102
103         char *proxyhost;
104         char *proxyport;
105         char *proxyuser;
106         char *proxypass;
107         proxytype_t proxytype;
108
109         bool localdiscovery;
110         sockaddr_t localdiscovery_address;
111
112         hash_t *node_udp_cache;
113         struct connection_t *everyone;
114         struct ecdsa *invitation_key;
115
116         debug_t debug_level;
117         int pinginterval;       /* seconds between pings */
118         int pingtimeout;        /* seconds to wait for response */
119         int maxtimeout;
120
121         int sock;
122         sptps_t sptps;
123         char cookie[18], hash[18];
124         char *data;
125         size_t thedatalen;
126         bool success;
127         char line[4096];
128         char buffer[4096];
129         size_t blen;
130 };
131
132 /// A handle for a MeshLink node.
133 struct meshlink_node {
134         const char *name;
135         void *priv;
136 };
137
138 /// Header for data packets routed between nodes
139 typedef struct meshlink_packethdr {
140         uint8_t destination[16];
141         uint8_t source[16];
142 } __attribute__ ((__packed__)) meshlink_packethdr_t;
143
144 extern void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh);
145
146
147 #endif // MESHLINK_INTERNAL_H