]> git.meshlink.io Git - meshlink/blob - src/net.h
Don't use assert() to check the results of pthread_*() calls.
[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 /* Maximum size of SPTPS payload */
27 #ifdef ENABLE_JUMBOGRAMS
28 #define MTU 8951        /* 9000 bytes payload - 28 bytes IP+UDP header - 21 bytes SPTPS header+MAC */
29 #else
30 #define MTU 1451        /* 1500 bytes payload - 28 bytes IP+UDP - 21 bytes SPTPS header+MAC */
31 #endif
32
33 #define MINMTU 527      /* 576 minimum recommended Internet MTU - 28 bytes IP+UDP - 21 bytes SPTPS header+MAC */
34
35 /* MAXSIZE is the maximum size of an encapsulated packet */
36 #define MAXSIZE (MTU + 64)
37
38 /* MAXBUFSIZE is the maximum size of a request: enough for a base64 encoded MAXSIZEd packet plus request header */
39 #define MAXBUFSIZE ((MAXSIZE * 8) / 6 + 128)
40
41 typedef struct vpn_packet_t {
42         uint16_t probe: 1;
43         int16_t tcp: 1;
44         uint16_t len;           /* the actual number of bytes in the `data' field */
45         uint8_t data[MAXSIZE];
46 } vpn_packet_t;
47
48 /* Packet types when using SPTPS */
49
50 #define PKT_COMPRESSED 1
51 #define PKT_PROBE 4
52
53 typedef enum packet_type_t {
54         PACKET_NORMAL,
55         PACKET_COMPRESSED,
56         PACKET_PROBE
57 } packet_type_t;
58
59 #include "conf.h"
60 #include "list.h"
61
62 typedef struct outgoing_t {
63         struct node_t *node;
64         enum {
65                 OUTGOING_START,
66                 OUTGOING_CANONICAL_RESOLVE,
67                 OUTGOING_CANONICAL,
68                 OUTGOING_RECENT,
69                 OUTGOING_KNOWN,
70                 OUTGOING_END,
71                 OUTGOING_NO_KNOWN_ADDRESSES,
72         } state;
73         int timeout;
74         timeout_t ev;
75         struct addrinfo *ai;
76         struct addrinfo *aip;
77 } outgoing_t;
78
79 /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
80 #include "connection.h"
81 #include "node.h"
82
83 void init_outgoings(struct meshlink_handle *mesh);
84 void exit_outgoings(struct meshlink_handle *mesh);
85
86 void retry_outgoing(struct meshlink_handle *mesh, outgoing_t *);
87 void handle_incoming_vpn_data(struct event_loop_t *loop, void *, int);
88 void finish_connecting(struct meshlink_handle *mesh, struct connection_t *);
89 void do_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
90 void handle_new_meta_connection(struct event_loop_t *loop, void *, int);
91 int setup_tcp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
92 int setup_udp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
93 bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len);
94 bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) __attribute__((__warn_unused_result__));
95 void send_packet(struct meshlink_handle *mesh, struct node_t *, struct vpn_packet_t *);
96 char *get_name(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
97 void load_all_nodes(struct meshlink_handle *mesh);
98 bool setup_myself_reloadable(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
99 bool setup_network(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
100 void reset_outgoing(struct outgoing_t *);
101 void setup_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
102 void close_network_connections(struct meshlink_handle *mesh);
103 void main_loop(struct meshlink_handle *mesh);
104 void terminate_connection(struct meshlink_handle *mesh, struct connection_t *, bool);
105 bool node_read_public_key(struct meshlink_handle *mesh, struct node_t *) __attribute__((__warn_unused_result__));
106 bool node_read_from_config(struct meshlink_handle *mesh, struct node_t *, const config_t *config) __attribute__((__warn_unused_result__));
107 bool read_ecdsa_public_key(struct meshlink_handle *mesh, struct connection_t *) __attribute__((__warn_unused_result__));
108 bool read_ecdsa_private_key(struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
109 bool node_write_config(struct meshlink_handle *mesh, struct node_t *) __attribute__((__warn_unused_result__));
110 void send_mtu_probe(struct meshlink_handle *mesh, struct node_t *);
111 void handle_meta_connection_data(struct meshlink_handle *mesh, struct connection_t *);
112 void retry(struct meshlink_handle *mesh);
113 int check_port(struct meshlink_handle *mesh);
114
115 #ifndef HAVE_MINGW
116 #define closesocket(s) close(s)
117 #endif
118
119 #endif