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>
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.122 2002/02/10 21:57:54 guus Exp $
25 #include <sys/types.h>
39 #include "connection.h"
43 int check_id(char *id)
47 for (i = 0; i < strlen(id); i++)
48 if(!isalnum(id[i]) && id[i] != '_')
54 /* Generic request routines - takes care of logging and error
57 int send_request(connection_t *c, const char *format, ...)
60 char buffer[MAXBUFSIZE];
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 */
68 va_start(args, format);
69 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
70 request = va_arg(args, int);
73 if(len < 0 || len > MAXBUFSIZE-1)
75 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
79 if(debug_lvl >= DEBUG_PROTOCOL)
81 if(debug_lvl >= DEBUG_META)
82 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
84 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
89 return send_meta(c, buffer, len);
92 int receive_request(connection_t *c)
96 if(sscanf(c->buffer, "%d", &request) == 1)
98 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
100 if(debug_lvl >= DEBUG_META)
101 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
102 c->name, c->hostname, c->buffer);
104 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
105 c->name, c->hostname);
111 if(debug_lvl >= DEBUG_PROTOCOL)
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);
117 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
118 request_name[request], c->name, c->hostname);
122 if((c->allow_request != ALL) && (c->allow_request != request))
124 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
128 if(request_handlers[request](c))
129 /* Something went wrong. Probably scriptkiddies. Terminate. */
131 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
132 request_name[request], c->name, c->hostname);
138 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
139 c->name, c->hostname);
146 /* Jumptable for the request handlers */
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,
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,
161 char (*request_name[]) = {
162 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
163 "STATUS", "ERROR", "TERMREQ",
165 // "ADD_NODE", "DEL_NODE",
166 "ADD_SUBNET", "DEL_SUBNET",
167 "ADD_EDGE", "DEL_EDGE",
168 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",