/*
protocol.c -- handle the meta-protocol, basic functions
- Copyright (C) 1999-2001 Ivo Timmermans <ivo@o2w.nl>,
- 2000,2001 Guus Sliepen <guus@sliepen.eu.org>
+ Copyright (C) 1999-2005 Ivo Timmermans,
+ 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol.c,v 1.28.4.141 2003/07/17 15:06:26 guus Exp $
+ $Id$
*/
#include "system.h"
#include "utils.h"
#include "xalloc.h"
+bool tunnelserver = false;
+
/* Jumptable for the request handlers */
-static int (*request_handlers[])(connection_t *) = {
+static bool (*request_handlers[])(connection_t *) = {
id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
status_h, error_h, termreq_h,
ping_h, pong_h,
static avl_tree_t *past_request_tree;
-int check_id(char *id)
+bool check_id(const char *id)
{
- int i;
-
- for(i = 0; i < strlen(id); i++)
- if(!isalnum(id[i]) && id[i] != '_')
- return -1;
+ for(; *id; id++)
+ if(!isalnum(*id) && *id != '_')
+ return false;
- return 0;
+ return true;
}
/* Generic request routines - takes care of logging and error
detection as well */
-int send_request(connection_t *c, const char *format, ...)
+bool send_request(connection_t *c, const char *format, ...)
{
va_list args;
char buffer[MAXBUFSIZE];
cp();
- /* Use vsnprintf instead of vasprintf: faster, no memory
+ /* Use vsnprintf instead of vxasprintf: faster, no memory
fragmentation, cleanup is automatic, and there is a limit on the
input buffer anyway */
if(len < 0 || len > MAXBUFSIZE - 1) {
logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
ifdebug(PROTOCOL) {
buffer[len++] = '\n';
- if(c == broadcast)
- return broadcast_meta(NULL, buffer, len);
- else
+ if(c == broadcast) {
+ broadcast_meta(NULL, buffer, len);
+ return true;
+ } else
return send_meta(c, buffer, len);
}
-int forward_request(connection_t *from)
+void forward_request(connection_t *from)
{
int request;
- cp();
cp();
from->buffer[from->reqlen - 1] = '\n';
- return broadcast_meta(from, from->buffer, from->reqlen);
+ broadcast_meta(from, from->buffer, from->reqlen);
}
-int receive_request(connection_t *c)
+bool receive_request(connection_t *c)
{
int request;
logger(LOG_ERR, _("Unknown request from %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
} else {
ifdebug(PROTOCOL) {
ifdebug(META)
if((c->allow_request != ALL) && (c->allow_request != request)) {
logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
c->hostname);
- return -1;
+ return false;
}
- if(request_handlers[request] (c))
+ if(!request_handlers[request](c)) {
/* Something went wrong. Probably scriptkiddies. Terminate. */
- {
+
logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
request_name[request], c->name, c->hostname);
- return -1;
+ return false;
}
} else {
logger(LOG_ERR, _("Bogus data received from %s (%s)"),
c->name, c->hostname);
- return -1;
+ return false;
}
- return 0;
+ return true;
}
-static int past_request_compare(past_request_t *a, past_request_t *b)
+static int past_request_compare(const past_request_t *a, const past_request_t *b)
{
return strcmp(a->request, b->request);
}
avl_delete_tree(past_request_tree);
}
-int seen_request(char *request)
+bool seen_request(char *request)
{
- past_request_t p, *new;
+ past_request_t *new, p = {0};
cp();
if(avl_search(past_request_tree, &p)) {
ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
- return 1;
+ return true;
} else {
- new = (past_request_t *) xmalloc(sizeof(*new));
+ new = xmalloc(sizeof(*new));
new->request = xstrdup(request);
new->firstseen = now;
avl_insert(past_request_tree, new);
- return 0;
+ return false;
}
}
for(node = past_request_tree->head; node; node = next) {
next = node->next;
- p = (past_request_t *) node->data;
+ p = node->data;
- if(p->firstseen + pingtimeout < now)
+ if(p->firstseen + pinginterval < now)
avl_delete_node(past_request_tree, node), deleted++;
else
left++;
}
if(left || deleted)
- ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Aging past requests: deleted %d, left %d\n"),
+ ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Aging past requests: deleted %d, left %d"),
deleted, left);
}