Reduce duplication in request handler tables
[tinc] / src / protocol.h
1 #ifndef TINC_PROTOCOL_H
2 #define TINC_PROTOCOL_H
3
4 /*
5     protocol.h -- header for protocol.c
6     Copyright (C) 1999-2005 Ivo Timmermans,
7                   2000-2017 Guus Sliepen <guus@tinc-vpn.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License along
20     with this program; if not, write to the Free Software Foundation, Inc.,
21     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "ecdsa.h"
25 #include "connection.h"
26
27 /* Protocol version. Different major versions are incompatible. */
28
29 #define PROT_MAJOR 17
30 #define PROT_MINOR 7
31
32 STATIC_ASSERT(PROT_MINOR <= 255, "PROT_MINOR must not exceed 255");
33
34 /* Silly Windows */
35
36 #ifdef ERROR
37 #undef ERROR
38 #endif
39
40 /* Request numbers */
41
42 typedef enum request_t {
43         ALL = -1,                                       /* Guardian for allow_request */
44         ID = 0, METAKEY, CHALLENGE, CHAL_REPLY, ACK,
45         STATUS, ERROR, TERMREQ,
46         PING, PONG,
47         ADD_SUBNET, DEL_SUBNET,
48         ADD_EDGE, DEL_EDGE,
49         KEY_CHANGED, REQ_KEY, ANS_KEY,
50         PACKET,
51         /* Tinc 1.1 requests */
52         CONTROL,
53         REQ_PUBKEY, ANS_PUBKEY,
54         SPTPS_PACKET,
55         UDP_INFO, MTU_INFO,
56         LAST                                            /* Guardian for the highest request number */
57 } request_t;
58
59 typedef bool (request_handler_t)(connection_t *c, const char *request);
60
61 typedef struct past_request_t {
62         const char *request;
63         time_t firstseen;
64 } past_request_t;
65
66 typedef struct {
67         request_handler_t *const handler;
68         const char *name;
69 } request_entry_t;
70
71 extern bool tunnelserver;
72 extern bool strictsubnets;
73 extern bool experimental;
74
75 extern int invitation_lifetime;
76 extern ecdsa_t *invitation_key;
77
78 /* Maximum size of strings in a request.
79  * scanf terminates %2048s with a NUL character,
80  * but the NUL character can be written after the 2048th non-NUL character.
81  */
82
83 #define MAX_STRING_SIZE 2049
84 #define MAX_STRING "%2048s"
85
86 #include "edge.h"
87 #include "net.h"
88 #include "node.h"
89 #include "subnet.h"
90
91 /* Basic functions */
92
93 extern bool send_request(struct connection_t *c, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
94 extern void forward_request(struct connection_t *c, const char *request);
95 extern bool receive_request(struct connection_t *c, const char *request);
96
97 extern void exit_requests(void);
98 extern bool seen_request(const char *request);
99
100 extern const request_entry_t *get_request_entry(request_t req);
101
102 /* Requests */
103
104 extern bool send_id(struct connection_t *c);
105 extern bool send_metakey(struct connection_t *c);
106 extern bool send_challenge(struct connection_t *c);
107 extern bool send_chal_reply(struct connection_t *c);
108 extern bool send_ack(struct connection_t *c);
109 extern bool send_termreq(struct connection_t *c);
110 extern bool send_ping(struct connection_t *c);
111 extern bool send_pong(struct connection_t *c);
112 extern bool send_add_subnet(struct connection_t *c, const struct subnet_t *subnet);
113 extern bool send_del_subnet(struct connection_t *c, const struct subnet_t *subnet);
114 extern bool send_add_edge(struct connection_t *c, const struct edge_t *e);
115 extern bool send_del_edge(struct connection_t *c, const struct edge_t *e);
116 extern void send_key_changed(void);
117 extern bool send_req_key(struct node_t *to);
118 extern bool send_ans_key(struct node_t *to);
119 extern bool send_tcppacket(struct connection_t *c, const struct vpn_packet_t *packet);
120 extern bool send_sptps_tcppacket(struct connection_t *c, const void *packet, size_t len);
121 extern bool send_udp_info(struct node_t *from, struct node_t *to);
122 extern bool send_mtu_info(struct node_t *from, struct node_t *to, int mtu);
123
124 /* Request handlers  */
125
126 extern request_handler_t id_h;
127 extern request_handler_t metakey_h;
128 extern request_handler_t challenge_h;
129 extern request_handler_t chal_reply_h;
130 extern request_handler_t ack_h;
131 extern request_handler_t termreq_h;
132 extern request_handler_t ping_h;
133 extern request_handler_t pong_h;
134 extern request_handler_t add_subnet_h;
135 extern request_handler_t del_subnet_h;
136 extern request_handler_t add_edge_h;
137 extern request_handler_t del_edge_h;
138 extern request_handler_t key_changed_h;
139 extern request_handler_t req_key_h;
140 extern request_handler_t ans_key_h;
141 extern request_handler_t tcppacket_h;
142 extern request_handler_t sptps_tcppacket_h;
143 extern request_handler_t control_h;
144 extern request_handler_t udp_info_h;
145 extern request_handler_t mtu_info_h;
146
147 #endif