]> git.meshlink.io Git - meshlink/blob - src/net.h
Have try_bind() reuse the setup_*_listen_socket() functions.
[meshlink] / src / net.h
1 #ifndef MESHLINK_NET_H
2 #define MESHLINK_NET_H
3
4 /*
5     net.h -- header for net.c
6     Copyright (C) 2014, 2017 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 "event.h"
24 #include "sockaddr.h"
25
26 #ifdef ENABLE_JUMBOGRAMS
27 #define MTU 9018        /* 9000 bytes payload + 14 bytes ethernet header + 4 bytes VLAN tag */
28 #else
29 #define MTU 1518        /* 1500 bytes payload + 14 bytes ethernet header + 4 bytes VLAN tag */
30 #endif
31
32 /* MAXSIZE is the maximum size of an encapsulated packet: MTU + seqno + HMAC + compressor overhead */
33 #define MAXSIZE (MTU + 4 + 32 + MTU/64 + 20)
34
35 /* MAXBUFSIZE is the maximum size of a request: enough for a MAXSIZEd packet or a 8192 bits RSA key */
36 #define MAXBUFSIZE ((MAXSIZE > 2048 ? MAXSIZE : 2048) + 128)
37
38 typedef struct vpn_packet_t {
39         uint16_t probe: 1;
40         int16_t tcp: 1;
41         uint16_t len;           /* the actual number of bytes in the `data' field */
42         uint8_t data[MAXSIZE];
43 } vpn_packet_t;
44
45 /* Packet types when using SPTPS */
46
47 #define PKT_COMPRESSED 1
48 #define PKT_PROBE 4
49
50 typedef enum packet_type_t {
51         PACKET_NORMAL,
52         PACKET_COMPRESSED,
53         PACKET_PROBE
54 } packet_type_t;
55
56 #include "conf.h"
57 #include "list.h"
58
59 typedef struct outgoing_t {
60         struct node_t *node;
61         enum {
62                 OUTGOING_START,
63                 OUTGOING_CANONICAL,
64                 OUTGOING_RECENT,
65                 OUTGOING_KNOWN,
66                 OUTGOING_END,
67                 OUTGOING_NO_KNOWN_ADDRESSES,
68         } state;
69         int timeout;
70         timeout_t ev;
71         struct addrinfo *ai;
72         struct addrinfo *aip;
73 } outgoing_t;
74
75 /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
76 #include "connection.h"
77 #include "node.h"
78
79 extern void init_outgoings(struct meshlink_handle *mesh);
80 extern void exit_outgoings(struct meshlink_handle *mesh);
81
82 extern void retry_outgoing(struct meshlink_handle *mesh, outgoing_t *);
83 extern void handle_incoming_vpn_data(struct event_loop_t *loop, void *, int);
84 extern void finish_connecting(struct meshlink_handle *mesh, struct connection_t *);
85 extern void do_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
86 extern void handle_new_meta_connection(struct event_loop_t *loop, void *, int);
87 extern int setup_tcp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
88 extern int setup_udp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
89 extern bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len);
90 extern bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) __attribute__((__warn_unused_result__));
91 extern void send_packet(struct meshlink_handle *mesh, struct node_t *, struct vpn_packet_t *);
92 extern char *get_name(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
93 extern void load_all_nodes(struct meshlink_handle *mesh);
94 extern bool setup_myself_reloadable(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
95 extern bool setup_network(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
96 extern void reset_outgoing(struct outgoing_t *);
97 extern void setup_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
98 extern void close_network_connections(struct meshlink_handle *mesh);
99 extern void main_loop(struct meshlink_handle *mesh);
100 extern void terminate_connection(struct meshlink_handle *mesh, struct connection_t *, bool);
101 extern bool node_read_public_key(struct meshlink_handle *mesh, struct node_t *) __attribute__((__warn_unused_result__));
102 extern bool node_read_from_config(struct meshlink_handle *mesh, struct node_t *, const config_t *config) __attribute__((__warn_unused_result__));
103 extern bool read_ecdsa_public_key(struct meshlink_handle *mesh, struct connection_t *) __attribute__((__warn_unused_result__));
104 extern bool read_ecdsa_private_key(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
105 extern bool node_write_config(struct meshlink_handle *mesh, struct node_t *) __attribute__((__warn_unused_result__));
106 extern void send_mtu_probe(struct meshlink_handle *mesh, struct node_t *);
107 extern void handle_meta_connection_data(struct meshlink_handle *mesh, struct connection_t *);
108 extern void retry(struct meshlink_handle *mesh);
109 extern int check_port(struct meshlink_handle *mesh);
110
111 #ifndef HAVE_MINGW
112 #define closesocket(s) close(s)
113 #endif
114
115 #endif