Refactor outgoing connection handling.
[tinc] / src / net.h
1 /*
2     net.h -- header for net.c
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifndef __TINC_NET_H__
22 #define __TINC_NET_H__
23
24 #include "ipv6.h"
25 #include "cipher.h"
26 #include "digest.h"
27
28 #ifdef ENABLE_JUMBOGRAMS
29 #define MTU 9018                                /* 9000 bytes payload + 14 bytes ethernet header + 4 bytes VLAN tag */
30 #else
31 #define MTU 1518                                /* 1500 bytes payload + 14 bytes ethernet header + 4 bytes VLAN tag */
32 #endif
33
34 #define MAXSIZE (MTU + 4 + CIPHER_MAX_BLOCK_SIZE + DIGEST_MAX_SIZE + MTU/64 + 20)       /* MTU + seqno + padding + HMAC + compressor overhead */
35 #define MAXBUFSIZE ((MAXSIZE > 2048 ? MAXSIZE : 2048) + 128)    /* Enough room for a request with a MAXSIZEd packet or a 8192 bits RSA key */
36
37 #define MAXSOCKETS 8                    /* Probably overkill... */
38
39 typedef struct mac_t {
40         uint8_t x[6];
41 } mac_t;
42
43 typedef struct ipv4_t {
44         uint8_t x[4];
45 } ipv4_t;
46
47 typedef struct ipv6_t {
48         uint16_t x[8];
49 } ipv6_t;
50
51 typedef short length_t;
52
53 #define AF_UNKNOWN 255
54
55 struct sockaddr_unknown {
56         uint16_t family;
57         uint16_t pad1;
58         uint32_t pad2;
59         char *address;
60         char *port;
61 };
62
63 typedef union sockaddr_t {
64         struct sockaddr sa;
65         struct sockaddr_in in;
66         struct sockaddr_in6 in6;
67         struct sockaddr_unknown unknown;
68 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE
69         struct sockaddr_storage storage;
70 #endif
71 } sockaddr_t;
72
73 #ifdef SA_LEN
74 #define SALEN(s) SA_LEN(&s)
75 #else
76 #define SALEN(s) (s.sa_family==AF_INET?sizeof(struct sockaddr_in):sizeof(struct sockaddr_in6))
77 #endif
78
79 typedef struct vpn_packet_t {
80         length_t len;                           /* the actual number of bytes in the `data' field */
81         int priority;                           /* priority or TOS */
82         uint32_t seqno;                         /* 32 bits sequence number (network byte order of course) */
83         uint8_t data[MAXSIZE];
84 } vpn_packet_t;
85
86 /* Packet types when using SPTPS */
87
88 #define PKT_COMPRESSED 1
89 #define PKT_MAC 2
90 #define PKT_PROBE 4
91
92 typedef enum packet_type_t {
93         PACKET_NORMAL,
94         PACKET_COMPRESSED,
95         PACKET_PROBE
96 } packet_type_t;
97
98 typedef struct listen_socket_t {
99         struct event ev_tcp;
100         struct event ev_udp;
101         int tcp;
102         int udp;
103         sockaddr_t sa;
104 } listen_socket_t;
105
106 #include "conf.h"
107 #include "list.h"
108
109 typedef struct outgoing_t {
110         char *name;
111         int timeout;
112         splay_tree_t *config_tree;
113         struct config_t *cfg;
114         struct addrinfo *ai;
115         struct addrinfo *aip;
116         struct event ev;
117 } outgoing_t;
118
119 extern list_t *outgoing_list;
120
121 extern int maxoutbufsize;
122 extern int seconds_till_retry;
123 extern int addressfamily;
124 extern unsigned replaywin;
125 extern bool localdiscovery;
126
127 extern listen_socket_t listen_socket[MAXSOCKETS];
128 extern int listen_sockets;
129 extern int keylifetime;
130 extern int udp_rcvbuf;
131 extern int udp_sndbuf;
132 extern bool do_prune;
133 extern char *myport;
134 extern int contradicting_add_edge;
135 extern int contradicting_del_edge;
136 extern time_t last_config_check;
137
138 extern char *proxyhost;
139 extern char *proxyport;
140 extern char *proxyuser;
141 extern char *proxypass;
142 typedef enum proxytype_t {
143         PROXY_NONE = 0,
144         PROXY_SOCKS4,
145         PROXY_SOCKS4A,
146         PROXY_SOCKS5,
147         PROXY_HTTP,
148         PROXY_EXEC,
149 } proxytype_t;
150 extern proxytype_t proxytype;
151
152 extern char *scriptinterpreter;
153 extern char *scriptextension;
154
155 /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
156 #include "connection.h"
157 #include "node.h"
158
159 extern void retry_outgoing(outgoing_t *);
160 extern void handle_incoming_vpn_data(int, short, void *);
161 extern void finish_connecting(struct connection_t *);
162 extern bool do_outgoing_connection(struct outgoing_t *);
163 extern void handle_new_meta_connection(int, short, void *);
164 extern int setup_listen_socket(const sockaddr_t *);
165 extern int setup_vpn_in_socket(const sockaddr_t *);
166 extern bool send_sptps_data(void *handle, uint8_t type, const char *data, size_t len);
167 extern bool receive_sptps_record(void *handle, uint8_t type, const char *data, uint16_t len);
168 extern void send_packet(struct node_t *, vpn_packet_t *);
169 extern void receive_tcppacket(struct connection_t *, const char *, int);
170 extern void broadcast_packet(const struct node_t *, vpn_packet_t *);
171 extern char *get_name(void);
172 extern bool setup_myself_reloadable(void);
173 extern bool setup_network(void);
174 extern void setup_outgoing_connection(struct outgoing_t *);
175 extern void try_outgoing_connections(void);
176 extern void close_network_connections(void);
177 extern int main_loop(void);
178 extern void terminate_connection(struct connection_t *, bool);
179 extern bool node_read_ecdsa_public_key(struct node_t *);
180 extern bool read_ecdsa_public_key(struct connection_t *);
181 extern bool read_rsa_public_key(struct connection_t *);
182 extern void send_mtu_probe(struct node_t *);
183 extern void handle_device_data(int, short, void *);
184 extern void handle_meta_connection_data(int, short, void *);
185 extern void regenerate_key(void);
186 extern void purge(void);
187 extern void retry(void);
188 extern int reload_configuration(void);
189 extern void load_all_subnets(void);
190
191 #ifndef HAVE_MINGW
192 #define closesocket(s) close(s)
193 #else
194 extern CRITICAL_SECTION mutex;
195 #endif
196
197 #endif                                                  /* __TINC_NET_H__ */