Fix Windows device asynchronous write behavior.
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 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 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "logger.h"
26 #include "meta.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool tunnelserver = false;
32 bool strictsubnets = false;
33 bool experimental = true;
34
35 /* Jumptable for the request handlers */
36
37 static bool (*request_handlers[])(connection_t *, const char *) = {
38                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
39                 status_h, error_h, termreq_h,
40                 ping_h, pong_h,
41                 add_subnet_h, del_subnet_h,
42                 add_edge_h, del_edge_h,
43                 key_changed_h, req_key_h, ans_key_h, tcppacket_h, control_h,
44 };
45
46 /* Request names */
47
48 static char (*request_name[]) = {
49                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
50                 "STATUS", "ERROR", "TERMREQ",
51                 "PING", "PONG",
52                 "ADD_SUBNET", "DEL_SUBNET",
53                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
54 };
55
56 static splay_tree_t *past_request_tree;
57
58 /* Generic request routines - takes care of logging and error
59    detection as well */
60
61 bool send_request(connection_t *c, const char *format, ...) {
62         va_list args;
63         char request[MAXBUFSIZE];
64         int len;
65
66         /* Use vsnprintf instead of vxasprintf: faster, no memory
67            fragmentation, cleanup is automatic, and there is a limit on the
68            input buffer anyway */
69
70         va_start(args, format);
71         len = vsnprintf(request, MAXBUFSIZE, format, args);
72         va_end(args);
73
74         if(len < 0 || len > MAXBUFSIZE - 1) {
75                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
76                            c->name, c->hostname);
77                 return false;
78         }
79
80         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);
81
82         request[len++] = '\n';
83
84         if(c == everyone) {
85                 broadcast_meta(NULL, request, len);
86                 return true;
87         } else
88                 return send_meta(c, request, len);
89 }
90
91 void forward_request(connection_t *from, const char *request) {
92         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
93
94         // Create a temporary newline-terminated copy of the request
95         int len = strlen(request);
96         char tmp[len + 1];
97         memcpy(tmp, request, len);
98         tmp[len] = '\n';
99         broadcast_meta(from, tmp, sizeof tmp);
100 }
101
102 bool receive_request(connection_t *c, const char *request) {
103         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
104                 if(!request[0] || request[0] == '\r')
105                         return true;
106                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
107                         if(!strncmp(request + 9, "200", 3)) {
108                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
109                                 return true;
110                         } else {
111                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
112                                 return false;
113                         }
114                 }
115         }
116
117         int reqno = atoi(request);
118
119         if(reqno || *request == '0') {
120                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
121                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
122                         return false;
123                 } else {
124                         logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
125                 }
126
127                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
128                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
129                         return false;
130                 }
131
132                 if(!request_handlers[reqno](c, request)) {
133                         /* Something went wrong. Probably scriptkiddies. Terminate. */
134
135                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
136                         return false;
137                 }
138         } else {
139                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
140                 return false;
141         }
142
143         return true;
144 }
145
146 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
147         return strcmp(a->request, b->request);
148 }
149
150 static void free_past_request(past_request_t *r) {
151         if(r->request)
152                 free((char *)r->request);
153
154         free(r);
155 }
156
157 static timeout_t past_request_timeout;
158
159 static void age_past_requests(void *data) {
160         int left = 0, deleted = 0;
161
162         for splay_each(past_request_t, p, past_request_tree) {
163                 if(p->firstseen + pinginterval <= now.tv_sec)
164                         splay_delete_node(past_request_tree, node), deleted++;
165                 else
166                         left++;
167         }
168
169         if(left || deleted)
170                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
171
172         if(left)
173                 timeout_set(&past_request_timeout, &(struct timeval){10, rand() % 100000});
174 }
175
176 bool seen_request(const char *request) {
177         past_request_t *new, p = {NULL};
178
179         p.request = request;
180
181         if(splay_search(past_request_tree, &p)) {
182                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
183                 return true;
184         } else {
185                 new = xmalloc(sizeof *new);
186                 new->request = xstrdup(request);
187                 new->firstseen = now.tv_sec;
188                 splay_insert(past_request_tree, new);
189                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
190                 return false;
191         }
192 }
193
194 void init_requests(void) {
195         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
196 }
197
198 void exit_requests(void) {
199         splay_delete_tree(past_request_tree);
200
201         timeout_del(&past_request_timeout);
202 }