Terminate a connection on any error. Furthermore, disallow del_host,
[tinc] / src / protocol.c
index c6b6fec..2691bb0 100644 (file)
@@ -1,6 +1,6 @@
 /*
     protocol.c -- handle the meta-protocol
-    Copyright (C) 1999 Ivo Timmermans <zarq@iname.com>
+    Copyright (C) 1999,2000 Ivo Timmermans <zarq@iname.com>
 
     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
@@ -19,6 +19,8 @@
 
 #include "config.h"
 
+#include <sys/types.h>
+
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
 #include "netutl.h"
 #include "protocol.h"
 
-char buffer[MAXBUFSIZE];
+char buffer[MAXBUFSIZE+1];
 int buflen;
 
+/* Outgoing request routines */
+
 int send_ack(conn_list_t *cl)
 {
 cp
   if(debug_lvl > 2)
     syslog(LOG_DEBUG, "Send ACK to %s", cl->hostname);
 
-  buflen = sprintf(buffer, "%d\n", ACK);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", ACK);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -64,11 +68,12 @@ cp
     syslog(LOG_DEBUG, "Send TERMREQ to " IP_ADDR_S,
           IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx\n", TERMREQ, myself->vpn_ip);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", TERMREQ, myself->vpn_ip);
 
-  if((write(cl->meta_socket, buffer, buflen)) < 0)
+  if(write(cl->meta_socket, buffer, buflen) < 0)
     {
-      syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
+      if(debug_lvl > 1)
+       syslog(LOG_ERR, "send failed: %s:%d: %m", __FILE__, __LINE__);
       return -1;
     }
 cp
@@ -82,7 +87,7 @@ cp
     syslog(LOG_DEBUG, "Send TIMEOUT to " IP_ADDR_S,
           IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx\n", PINGTIMEOUT, myself->vpn_ip);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", PINGTIMEOUT, myself->vpn_ip);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -100,7 +105,7 @@ cp
     syslog(LOG_DEBUG, "Sending delete host " IP_ADDR_S " to " IP_ADDR_S,
           IP_ADDR_V(new_host->vpn_ip), IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx\n", DEL_HOST, new_host->vpn_ip);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", DEL_HOST, new_host->vpn_ip);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -117,7 +122,7 @@ cp
   if(debug_lvl > 3)
     syslog(LOG_DEBUG, "pinging " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d\n", PING);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PING);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -131,7 +136,7 @@ cp
 int send_pong(conn_list_t *cl)
 {
 cp
-  buflen = sprintf(buffer, "%d\n", PONG);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PONG);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -149,7 +154,7 @@ cp
     syslog(LOG_DEBUG, "Sending add host to " IP_ADDR_S,
           IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx %lx/%lx:%x\n", ADD_HOST, new_host->real_ip, new_host->vpn_ip, new_host->vpn_mask, new_host->port);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx/%lx:%x\n", ADD_HOST, new_host->real_ip, new_host->vpn_ip, new_host->vpn_mask, new_host->port);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -167,7 +172,7 @@ cp
     syslog(LOG_DEBUG, "Sending KEY_CHANGED to " IP_ADDR_S,
           IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx\n", KEY_CHANGED, src->vpn_ip);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", KEY_CHANGED, src->vpn_ip);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -178,12 +183,12 @@ cp
   return 0;
 }
 
-void send_key_changed2(void)
+void send_key_changed_all(void)
 {
   conn_list_t *p;
 cp
   for(p = conn_list; p != NULL; p = p->next)
-    if(p->status.meta && p->protocol_version > PROT_3)
+    if(p->status.meta && p->status.active)
       send_key_changed(p, myself);
 cp
 }
@@ -195,7 +200,7 @@ cp
     syslog(LOG_DEBUG, "Send BASIC_INFO to " IP_ADDR_S,
           IP_ADDR_V(cl->real_ip));
 
-  buflen = sprintf(buffer, "%d %d %lx/%lx:%x\n", BASIC_INFO, PROT_CURRENT, myself->vpn_ip, myself->vpn_mask, myself->port);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %d %lx/%lx:%x\n", BASIC_INFO, PROT_CURRENT, myself->vpn_ip, myself->vpn_mask, myself->port);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -234,7 +239,7 @@ cp
     syslog(LOG_DEBUG, "Send PUBLIC_KEY %s to " IP_ADDR_S,
           my_public_key_base36, IP_ADDR_V(cl->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %s\n", PUBLIC_KEY, my_public_key_base36);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %s\n", PUBLIC_KEY, my_public_key_base36);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -248,7 +253,7 @@ cp
 int send_calculate(conn_list_t *cl, char *k)
 {
 cp
-  buflen = sprintf(buffer, "%d %s\n", CALCULATE, k);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %s\n", CALCULATE, k);
 
   if((write(cl->meta_socket, buffer, buflen)) < 0)
     {
@@ -275,7 +280,7 @@ cp
     syslog(LOG_DEBUG, "Sending out request for public key to " IP_ADDR_S,
           IP_ADDR_V(fw->nexthop->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx %lx\n", REQ_KEY, to, myself->vpn_ip);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx\n", REQ_KEY, to, myself->vpn_ip);
 
   if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
     {
@@ -305,7 +310,7 @@ cp
     syslog(LOG_DEBUG, "Sending public key to " IP_ADDR_S,
           IP_ADDR_V(fw->nexthop->vpn_ip));
 
-  buflen = sprintf(buffer, "%d %lx %lx %d %s\n", ANS_KEY, to, myself->vpn_ip, my_key_expiry, my_public_key_base36);
+  buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx %d %s\n", ANS_KEY, to, myself->vpn_ip, my_key_expiry, my_public_key_base36);
 
   if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
     {
@@ -327,7 +332,7 @@ int notify_others(conn_list_t *new, conn_list_t *source,
   conn_list_t *p;
 cp
   for(p = conn_list; p != NULL; p = p->next)
-    if(p != new && p != source && p->status.meta)
+    if(p != new && p != source && p->status.meta && p->status.active)
       function(p, new);
 cp
   return 0;
@@ -342,7 +347,7 @@ int notify_one(conn_list_t *new)
   conn_list_t *p;
 cp
   for(p = conn_list; p != NULL; p = p->next)
-    if(p != new && p->protocol_version > PROT_3)
+    if(p != new && p->status.active)
       send_add_host(new, p);
 cp
   return 0;
@@ -387,8 +392,6 @@ cp
        return -1;
       send_passphrase(cl);
     }
-
-  cl->status.active = 0;
 cp
   return 0;
 }
@@ -396,7 +399,8 @@ cp
 int passphrase_h(conn_list_t *cl)
 {
 cp
-  cl->pp=xmalloc(sizeof(*(cl->pp)));
+  cl->pp = xmalloc(sizeof(*(cl->pp)));
+
   if(sscanf(cl->buffer, "%*d %as", &(cl->pp->phrase)) != 1)
     {
       syslog(LOG_ERR, "got bad PASSPHRASE request: %s", cl->buffer);
@@ -418,6 +422,7 @@ cp
 int public_key_h(conn_list_t *cl)
 {
   char *g_n;
+  conn_list_t *old;
 cp
   if(sscanf(cl->buffer, "%*d %as", &g_n) != 1)
     {
@@ -443,6 +448,14 @@ cp
   else
     send_ack(cl);
 
+  /* Okay, before we active the connection, we check if there is another entry
+     in the connection list with the same vpn_ip. If so, it presumably is an
+     old connection that has timed out but we don't know it yet. Because our
+     conn_list entry is not active, lookup_conn will skip ourself. */
+
+  while(old=lookup_conn(cl->vpn_ip)) 
+    terminate_connection(old);
+
   cl->status.active = 1;
   notify_others(cl, NULL, send_add_host);
   notify_one(cl);
@@ -477,6 +490,7 @@ cp
 int timeout_h(conn_list_t *cl)
 {
 cp
+  if(!cl->status.active) return -1;
   syslog(LOG_NOTICE, IP_ADDR_S " says it's gotten a timeout from us", IP_ADDR_V(cl->vpn_ip));
   cl->status.termreq = 1;
   terminate_connection(cl);
@@ -489,6 +503,8 @@ int del_host_h(conn_list_t *cl)
   ip_t vpn_ip;
   conn_list_t *fw;
 cp
+  if(!cl->status.active) return -1;
+
   if(sscanf(cl->buffer, "%*d %lx", &vpn_ip) != 1)
     {
        syslog(LOG_ERR, "got bad DEL_HOST request: %s", cl->buffer);
@@ -517,6 +533,7 @@ cp
 int ping_h(conn_list_t *cl)
 {
 cp
+  if(!cl->status.active) return -1;
   if(debug_lvl > 3)
     syslog(LOG_DEBUG, "responding to ping from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
   cl->status.pinged = 0;
@@ -530,6 +547,7 @@ cp
 int pong_h(conn_list_t *cl)
 {
 cp
+  if(!cl->status.active) return -1;
   if(debug_lvl > 3)
     syslog(LOG_DEBUG, "ok, got pong from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
   cl->status.got_pong = 1;
@@ -545,6 +563,7 @@ int add_host_h(conn_list_t *cl)
   unsigned short port;
   conn_list_t *ncn, *fw;
 cp
+  if(!cl->status.active) return -1;
   if(sscanf(cl->buffer, "%*d %lx %lx/%lx:%hx", &real_ip, &vpn_ip, &vpn_mask, &port) != 4)
     {
        syslog(LOG_ERR, "got bad ADD_HOST request: %s", cl->buffer);
@@ -587,6 +606,7 @@ int req_key_h(conn_list_t *cl)
   ip_t from;
   conn_list_t *fw;
 cp
+  if(!cl->status.active) return -1;
   if(sscanf(cl->buffer, "%*d %lx %lx", &to, &from) != 2)
     {
        syslog(LOG_ERR, "got bad request: %s", cl->buffer);
@@ -670,6 +690,7 @@ int ans_key_h(conn_list_t *cl)
   char *key;
   conn_list_t *fw, *gk;
 cp
+  if(!cl->status.active) return -1;
   if(sscanf(cl->buffer, "%*d %lx %lx %d %as", &to, &from, &expiry, &key) != 4)
     {
        syslog(LOG_ERR, "got bad ANS_KEY request: %s", cl->buffer);
@@ -729,6 +750,7 @@ int key_changed_h(conn_list_t *cl)
   ip_t from;
   conn_list_t *ik;
 cp
+  if(!cl->status.active) return -1;
   if(sscanf(cl->buffer, "%*d %lx", &from) != 1)
     {
        syslog(LOG_ERR, "got bad ANS_KEY request: %s", cl->buffer);