Terminate a connection on any error. Furthermore, disallow del_host,
[tinc] / src / protocol.c
index 3b81d7a..2691bb0 100644 (file)
@@ -188,7 +188,7 @@ void send_key_changed_all(void)
   conn_list_t *p;
 cp
   for(p = conn_list; p != NULL; p = p->next)
   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
 }
       send_key_changed(p, myself);
 cp
 }
@@ -332,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)
   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;
       function(p, new);
 cp
   return 0;
@@ -347,7 +347,7 @@ int notify_one(conn_list_t *new)
   conn_list_t *p;
 cp
   for(p = conn_list; p != NULL; p = p->next)
   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;
       send_add_host(new, p);
 cp
   return 0;
@@ -392,8 +392,6 @@ cp
        return -1;
       send_passphrase(cl);
     }
        return -1;
       send_passphrase(cl);
     }
-
-  cl->status.active = 0;
 cp
   return 0;
 }
 cp
   return 0;
 }
@@ -424,6 +422,7 @@ cp
 int public_key_h(conn_list_t *cl)
 {
   char *g_n;
 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)
     {
 cp
   if(sscanf(cl->buffer, "%*d %as", &g_n) != 1)
     {
@@ -449,6 +448,14 @@ cp
   else
     send_ack(cl);
 
   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);
   cl->status.active = 1;
   notify_others(cl, NULL, send_add_host);
   notify_one(cl);
@@ -483,6 +490,7 @@ cp
 int timeout_h(conn_list_t *cl)
 {
 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);
   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);
@@ -495,6 +503,8 @@ int del_host_h(conn_list_t *cl)
   ip_t vpn_ip;
   conn_list_t *fw;
 cp
   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);
   if(sscanf(cl->buffer, "%*d %lx", &vpn_ip) != 1)
     {
        syslog(LOG_ERR, "got bad DEL_HOST request: %s", cl->buffer);
@@ -523,6 +533,7 @@ cp
 int ping_h(conn_list_t *cl)
 {
 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;
   if(debug_lvl > 3)
     syslog(LOG_DEBUG, "responding to ping from " IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
   cl->status.pinged = 0;
@@ -536,6 +547,7 @@ cp
 int pong_h(conn_list_t *cl)
 {
 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;
   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;
@@ -551,6 +563,7 @@ int add_host_h(conn_list_t *cl)
   unsigned short port;
   conn_list_t *ncn, *fw;
 cp
   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);
   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);
@@ -593,6 +606,7 @@ int req_key_h(conn_list_t *cl)
   ip_t from;
   conn_list_t *fw;
 cp
   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);
   if(sscanf(cl->buffer, "%*d %lx %lx", &to, &from) != 2)
     {
        syslog(LOG_ERR, "got bad request: %s", cl->buffer);
@@ -676,6 +690,7 @@ int ans_key_h(conn_list_t *cl)
   char *key;
   conn_list_t *fw, *gk;
 cp
   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);
   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);
@@ -735,6 +750,7 @@ int key_changed_h(conn_list_t *cl)
   ip_t from;
   conn_list_t *ik;
 cp
   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);
   if(sscanf(cl->buffer, "%*d %lx", &from) != 1)
     {
        syslog(LOG_ERR, "got bad ANS_KEY request: %s", cl->buffer);