Apply patch from Scott Lamb adding an output buffer for the TCP sockets.
[tinc] / src / protocol.c
index 47cc948..de7f8b8 100644 (file)
@@ -1,7 +1,7 @@
 /*
     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 <ivo@tinc-vpn.org>,
+                  2000-2005 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
@@ -17,7 +17,7 @@
     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,
@@ -53,21 +55,19 @@ static char (*request_name[]) = {
 
 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];
@@ -86,7 +86,7 @@ int send_request(connection_t *c, const char *format, ...)
        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) {
@@ -101,16 +101,16 @@ int send_request(connection_t *c, const char *format, ...)
 
        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();
 
@@ -127,10 +127,10 @@ int forward_request(connection_t *from)
 
        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;
 
@@ -145,7 +145,7 @@ int receive_request(connection_t *c)
                                logger(LOG_ERR, _("Unknown request from %s (%s)"),
                                           c->name, c->hostname);
 
-                       return -1;
+                       return false;
                } else {
                        ifdebug(PROTOCOL) {
                                ifdebug(META)
@@ -161,26 +161,26 @@ int receive_request(connection_t *c)
                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);
 }
@@ -209,9 +209,9 @@ void exit_requests(void)
        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();
 
@@ -219,13 +219,13 @@ int seen_request(char *request)
 
        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;
        }
 }
 
@@ -239,15 +239,15 @@ void age_past_requests(void)
 
        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);
 }