Use WSAGetLastError() to determine cause of network errors on Windows.
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2009 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
33 /* Jumptable for the request handlers */
34
35 static bool (*request_handlers[])(connection_t *) = {
36                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
37                 status_h, error_h, termreq_h,
38                 ping_h, pong_h,
39                 add_subnet_h, del_subnet_h,
40                 add_edge_h, del_edge_h,
41                 key_changed_h, req_key_h, ans_key_h, tcppacket_h,
42 };
43
44 /* Request names */
45
46 static char (*request_name[]) = {
47                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
48                 "STATUS", "ERROR", "TERMREQ",
49                 "PING", "PONG",
50                 "ADD_SUBNET", "DEL_SUBNET",
51                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
52 };
53
54 static avl_tree_t *past_request_tree;
55
56 bool check_id(const char *id) {
57         for(; *id; id++)
58                 if(!isalnum(*id) && *id != '_')
59                         return false;
60
61         return true;
62 }
63
64 /* Generic request routines - takes care of logging and error
65    detection as well */
66
67 bool send_request(connection_t *c, const char *format, ...) {
68         va_list args;
69         char buffer[MAXBUFSIZE];
70         int len, request;
71
72         /* Use vsnprintf instead of vxasprintf: faster, no memory
73            fragmentation, cleanup is automatic, and there is a limit on the
74            input buffer anyway */
75
76         va_start(args, format);
77         len = vsnprintf(buffer, MAXBUFSIZE, format, args);
78         va_end(args);
79
80         if(len < 0 || len > MAXBUFSIZE - 1) {
81                 logger(LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
82                            c->name, c->hostname);
83                 return false;
84         }
85
86         ifdebug(PROTOCOL) {
87                 sscanf(buffer, "%d", &request);
88                 ifdebug(META)
89                         logger(LOG_DEBUG, "Sending %s to %s (%s): %s",
90                                    request_name[request], c->name, c->hostname, buffer);
91                 else
92                         logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[request],
93                                    c->name, c->hostname);
94         }
95
96         buffer[len++] = '\n';
97
98         if(c == broadcast) {
99                 broadcast_meta(NULL, buffer, len);
100                 return true;
101         } else
102                 return send_meta(c, buffer, len);
103 }
104
105 void forward_request(connection_t *from) {
106         int request;
107
108         ifdebug(PROTOCOL) {
109                 sscanf(from->buffer, "%d", &request);
110                 ifdebug(META)
111                         logger(LOG_DEBUG, "Forwarding %s from %s (%s): %s",
112                                    request_name[request], from->name, from->hostname,
113                                    from->buffer);
114                 else
115                         logger(LOG_DEBUG, "Forwarding %s from %s (%s)",
116                                    request_name[request], from->name, from->hostname);
117         }
118
119         from->buffer[from->reqlen - 1] = '\n';
120
121         broadcast_meta(from, from->buffer, from->reqlen);
122 }
123
124 bool receive_request(connection_t *c) {
125         int request;
126
127         if(sscanf(c->buffer, "%d", &request) == 1) {
128                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
129                         ifdebug(META)
130                                 logger(LOG_DEBUG, "Unknown request from %s (%s): %s",
131                                            c->name, c->hostname, c->buffer);
132                         else
133                                 logger(LOG_ERR, "Unknown request from %s (%s)",
134                                            c->name, c->hostname);
135
136                         return false;
137                 } else {
138                         ifdebug(PROTOCOL) {
139                                 ifdebug(META)
140                                         logger(LOG_DEBUG, "Got %s from %s (%s): %s",
141                                                    request_name[request], c->name, c->hostname,
142                                                    c->buffer);
143                                 else
144                                         logger(LOG_DEBUG, "Got %s from %s (%s)",
145                                                    request_name[request], c->name, c->hostname);
146                         }
147                 }
148
149                 if((c->allow_request != ALL) && (c->allow_request != request)) {
150                         logger(LOG_ERR, "Unauthorized request from %s (%s)", c->name,
151                                    c->hostname);
152                         return false;
153                 }
154
155                 if(!request_handlers[request](c)) {
156                         /* Something went wrong. Probably scriptkiddies. Terminate. */
157
158                         logger(LOG_ERR, "Error while processing %s from %s (%s)",
159                                    request_name[request], c->name, c->hostname);
160                         return false;
161                 }
162         } else {
163                 logger(LOG_ERR, "Bogus data received from %s (%s)",
164                            c->name, c->hostname);
165                 return false;
166         }
167
168         return true;
169 }
170
171 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
172         return strcmp(a->request, b->request);
173 }
174
175 static void free_past_request(past_request_t *r) {
176         if(r->request)
177                 free(r->request);
178
179         free(r);
180 }
181
182 void init_requests(void) {
183         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
184 }
185
186 void exit_requests(void) {
187         avl_delete_tree(past_request_tree);
188 }
189
190 bool seen_request(char *request) {
191         past_request_t *new, p = {0};
192
193         p.request = request;
194
195         if(avl_search(past_request_tree, &p)) {
196                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
197                 return true;
198         } else {
199                 new = xmalloc(sizeof(*new));
200                 new->request = xstrdup(request);
201                 new->firstseen = now;
202                 avl_insert(past_request_tree, new);
203                 return false;
204         }
205 }
206
207 void age_past_requests(void) {
208         avl_node_t *node, *next;
209         past_request_t *p;
210         int left = 0, deleted = 0;
211
212         for(node = past_request_tree->head; node; node = next) {
213                 next = node->next;
214                 p = node->data;
215
216                 if(p->firstseen + pinginterval < now)
217                         avl_delete_node(past_request_tree, node), deleted++;
218                 else
219                         left++;
220         }
221
222         if(left || deleted)
223                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",
224                            deleted, left);
225 }