- Non-blocking connect()s.
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000,2001 Guus Sliepen <guus@sliepen.warande.net>
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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: protocol.c,v 1.28.4.122 2002/02/10 21:57:54 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <errno.h>
33
34 #include <utils.h>
35
36 #include "conf.h"
37 #include "protocol.h"
38 #include "meta.h"
39 #include "connection.h"
40
41 #include "system.h"
42
43 int check_id(char *id)
44 {
45   int i;
46
47   for (i = 0; i < strlen(id); i++)
48     if(!isalnum(id[i]) && id[i] != '_')
49       return -1;
50   
51   return 0;
52 }
53
54 /* Generic request routines - takes care of logging and error
55    detection as well */
56
57 int send_request(connection_t *c, const char *format, ...)
58 {
59   va_list args;
60   char buffer[MAXBUFSIZE];
61   int len, request;
62
63 cp
64   /* Use vsnprintf instead of vasprintf: faster, no memory
65      fragmentation, cleanup is automatic, and there is a limit on the
66      input buffer anyway */
67
68   va_start(args, format);
69   len = vsnprintf(buffer, MAXBUFSIZE, format, args);
70   request = va_arg(args, int);
71   va_end(args);
72
73   if(len < 0 || len > MAXBUFSIZE-1)
74     {
75       syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
76       return -1;
77     }
78
79   if(debug_lvl >= DEBUG_PROTOCOL)
80     {
81       if(debug_lvl >= DEBUG_META)
82         syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
83       else
84         syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
85     }
86
87   buffer[len++] = '\n';
88 cp
89   return send_meta(c, buffer, len);
90 }
91
92 int receive_request(connection_t *c)
93 {
94   int request;
95 cp
96   if(sscanf(c->buffer, "%d", &request) == 1)
97     {
98       if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
99         {
100           if(debug_lvl >= DEBUG_META)
101             syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
102                    c->name, c->hostname, c->buffer);
103           else
104             syslog(LOG_ERR, _("Unknown request from %s (%s)"),
105                    c->name, c->hostname);
106                    
107           return -1;
108         }
109       else
110         {
111           if(debug_lvl >= DEBUG_PROTOCOL)
112             {
113               if(debug_lvl >= DEBUG_META)
114                 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
115                        request_name[request], c->name, c->hostname, c->buffer);
116               else
117                 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
118                        request_name[request], c->name, c->hostname);
119             }
120         }
121
122       if((c->allow_request != ALL) && (c->allow_request != request))
123         {
124           syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
125           return -1;
126         }
127
128       if(request_handlers[request](c))
129         /* Something went wrong. Probably scriptkiddies. Terminate. */
130         {
131           syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
132                  request_name[request], c->name, c->hostname);
133           return -1;
134         }
135     }
136   else
137     {
138       syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
139              c->name, c->hostname);
140       return -1;
141     }
142 cp
143   return 0;
144 }
145
146 /* Jumptable for the request handlers */
147
148 int (*request_handlers[])(connection_t*) = {
149   id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
150   status_h, error_h, termreq_h,
151   ping_h, pong_h,
152 //  add_node_h, del_node_h,
153   add_subnet_h, del_subnet_h,
154   add_edge_h, del_edge_h,
155   key_changed_h, req_key_h, ans_key_h,
156   tcppacket_h,
157 };
158
159 /* Request names */
160
161 char (*request_name[]) = {
162   "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
163   "STATUS", "ERROR", "TERMREQ",
164   "PING", "PONG",
165 //  "ADD_NODE", "DEL_NODE",
166   "ADD_SUBNET", "DEL_SUBNET",
167   "ADD_EDGE", "DEL_EDGE",
168   "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
169   "PACKET",
170 };