2 protocol.c -- handle the meta-protocol, basic functions
3 Copyright (C) 1999-2001 Ivo Timmermans <ivo@o2w.nl>,
4 2000,2001 Guus Sliepen <guus@sliepen.eu.org>
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.
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.
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.
20 $Id: protocol.c,v 1.28.4.131 2002/09/04 08:02:33 guus Exp $
25 #include <sys/types.h>
40 #include "connection.h"
44 avl_tree_t *past_request_tree;
46 int check_id(char *id)
50 for (i = 0; i < strlen(id); i++)
51 if(!isalnum(id[i]) && id[i] != '_')
57 /* Generic request routines - takes care of logging and error
60 int send_request(connection_t *c, const char *format, ...)
63 char buffer[MAXBUFSIZE];
65 char *name = "everyone";
66 char *hostname = "broadcast";
71 hostname = c->hostname;
74 /* Use vsnprintf instead of vasprintf: faster, no memory
75 fragmentation, cleanup is automatic, and there is a limit on the
76 input buffer anyway */
78 va_start(args, format);
79 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
82 if(len < 0 || len > MAXBUFSIZE-1)
84 syslog(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"), name, hostname);
88 if(debug_lvl >= DEBUG_PROTOCOL)
90 sscanf(buffer, "%d", &request);
91 if(debug_lvl >= DEBUG_META)
92 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], name, hostname, buffer);
94 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], name, hostname);
100 return send_meta(c, buffer, len);
102 return broadcast_meta(NULL, buffer, len);
105 int receive_request(connection_t *c)
109 if(sscanf(c->buffer, "%d", &request) == 1)
111 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
113 if(debug_lvl >= DEBUG_META)
114 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
115 c->name, c->hostname, c->buffer);
117 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
118 c->name, c->hostname);
124 if(debug_lvl >= DEBUG_PROTOCOL)
126 if(debug_lvl >= DEBUG_META)
127 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
128 request_name[request], c->name, c->hostname, c->buffer);
130 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
131 request_name[request], c->name, c->hostname);
135 if((c->allow_request != ALL) && (c->allow_request != request))
137 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
141 if(request_handlers[request](c))
142 /* Something went wrong. Probably scriptkiddies. Terminate. */
144 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
145 request_name[request], c->name, c->hostname);
151 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
152 c->name, c->hostname);
159 int past_request_compare(past_request_t *a, past_request_t *b)
162 return strcmp(a->request, b->request);
165 void free_past_request(past_request_t *r)
174 void init_requests(void)
177 past_request_tree = avl_alloc_tree((avl_compare_t)past_request_compare, (avl_action_t)free_past_request);
181 void exit_requests(void)
184 avl_delete_tree(past_request_tree);
188 int seen_request(char *request)
190 past_request_t p, *new;
194 if(avl_search(past_request_tree, &p))
196 if(debug_lvl >= DEBUG_SCARY_THINGS)
197 syslog(LOG_DEBUG, _("Already seen request"));
202 new = (past_request_t *)xmalloc(sizeof(*new));
203 new->request = xstrdup(request);
204 new->firstseen = now;
205 avl_insert(past_request_tree, new);
211 void age_past_requests(void)
213 avl_node_t *node, *next;
215 int left = 0, deleted = 0;
217 for(node = past_request_tree->head; node; node = next)
220 p = (past_request_t *)node->data;
221 if(p->firstseen + pingtimeout < now)
222 avl_delete_node(past_request_tree, node), deleted++;
227 if(debug_lvl >= DEBUG_SCARY_THINGS && left + deleted)
228 syslog(LOG_DEBUG, _("Aging past requests: deleted %d, left %d\n"), deleted, left);
232 /* Jumptable for the request handlers */
234 int (*request_handlers[])(connection_t*) = {
235 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
236 status_h, error_h, termreq_h,
238 add_subnet_h, del_subnet_h,
239 add_node_h, del_node_h,
240 key_changed_h, req_key_h, ans_key_h,
246 char (*request_name[]) = {
247 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
248 "STATUS", "ERROR", "TERMREQ",
250 "ADD_SUBNET", "DEL_SUBNET",
251 "ADD_NODE", "DEL_NODE",
252 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",