- Always use <openssl/include.h> instead of just <include.h>
[tinc] / src / protocol.c
index de93251..ef60180 100644 (file)
@@ -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.93 2001/06/08 18:02:10 guus Exp $
+    $Id: protocol.c,v 1.28.4.98 2001/07/04 08:41:36 guus Exp $
 */
 
 #include "config.h"
 
 #include <netinet/in.h>
 
-#ifdef HAVE_OPENSSL_SHA_H
-# include <openssl/sha.h>
-#else
-# include <sha.h>
-#endif
-
-#ifdef HAVE_OPENSSL_RAND_H
-# include <openssl/rand.h>
-#else
-# include <rand.h>
-#endif
+#include <openssl/sha.h>
+#include <openssl/rand.h>
+#include <openssl/evp.h>
 
-#ifdef HAVE_OPENSSL_EVP_H
-# include <openssl/evp.h>
-#else
-# include <evp.h>
+#ifndef HAVE_RAND_PSEUDO_BYTES
+#define RAND_pseudo_bytes RAND_bytes
 #endif
 
-
 #include "conf.h"
 #include "net.h"
 #include "netutl.h"
@@ -227,22 +216,22 @@ cp
       return -1;
     }
 
-  /* First check if the host we connected to is already in our
+  /* First check if the host is already in our
      connection list. If so, we are probably making a loop, which
      is not desirable.
    */
 
-  if(cl->status.outgoing)
+  if((old = lookup_id(cl->name)))
     {
-      if((old = lookup_id(cl->name)))
+      if(debug_lvl >= DEBUG_CONNECTIONS)
+        syslog(LOG_NOTICE, _("%s (%s) is already in our connection list"), cl->name, cl->hostname);
+      if(cl->status.outgoing)
         {
-          if(debug_lvl >= DEBUG_CONNECTIONS)
-            syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->name, cl->hostname);
           cl->status.outgoing = 0;
           old->status.outgoing = 1;
-          terminate_connection(cl);
-          return 0;
         }
+      terminate_connection(cl);
+      return 0;
     }
     
   /* Now we can add the name to the id tree */
@@ -253,8 +242,14 @@ cp
   
   node = avl_unlink(connection_tree, cl);
   cl->port = port;
-  avl_insert_node(connection_tree, node);
-
+  if(!avl_insert_node(connection_tree, node))
+    {
+      old = avl_search_node(connection_tree, node)->data;
+      syslog(LOG_ERR, _("%s is listening on %s:%hd, which is already in use by %s!"),
+             cl->name, cl->hostname, cl->port, old->name);
+      return -1;
+    }
+    
   /* Read in the public key, so that we can send a metakey */
 
   if(read_rsa_public_key(cl))
@@ -1060,7 +1055,7 @@ int send_ping(connection_t *cl)
 cp
   cl->status.pinged = 1;
   cl->last_ping_time = time(NULL);
-  RAND_bytes(salt, SALTLEN);
+  RAND_pseudo_bytes(salt, SALTLEN);
   bin2hex(salt, salt, SALTLEN);
   salt[SALTLEN*2] = '\0';
 cp
@@ -1077,7 +1072,7 @@ int send_pong(connection_t *cl)
 {
   char salt[SALTLEN*2+1];
 cp
-  RAND_bytes(salt, SALTLEN);
+  RAND_pseudo_bytes(salt, SALTLEN);
   bin2hex(salt, salt, SALTLEN);
   salt[SALTLEN*2] = '\0';
 cp
@@ -1103,17 +1098,16 @@ cp
      This reduces unnecessary key_changed broadcasts.
   */
 
-  if(mykeyused)
+  if(from==myself && !mykeyused)
+    return 0;
+
+  for(node = connection_tree->head; node; node = node->next)
     {
-      for(node = connection_tree->head; node; node = node->next)
-        {
-          p = (connection_t *)node->data;
-          if(p != cl && p->status.meta && p->status.active)
-            if(!(p->options & OPTION_INDIRECT) || from == myself)
-              send_request(p, "%d %s", KEY_CHANGED, from->name);
-        }
-    mykeyused = 0;
-  }
+      p = (connection_t *)node->data;
+      if(p != cl && p->status.meta && p->status.active)
+        if(!(p->options & OPTION_INDIRECT) || from == myself)
+          send_request(p, "%d %s", KEY_CHANGED, from->name);
+    }
 cp
   return 0;
 }