Don't include netutl.h.
[tinc] / src / net.c
index 1895bc1..0397ba5 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -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: net.c,v 1.35.4.143 2001/10/30 16:34:32 guus Exp $
+    $Id: net.c,v 1.35.4.149 2001/11/16 12:22:02 zarq Exp $
 */
 
 #include "config.h"
@@ -33,6 +33,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <sys/signal.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -62,7 +63,6 @@
 #include "connection.h"
 #include "meta.h"
 #include "net.h"
-#include "netutl.h"
 #include "process.h"
 #include "protocol.h"
 #include "subnet.h"
@@ -250,9 +250,11 @@ int setup_listen_socket(int port)
   int nfd, flags;
   struct sockaddr_in a;
   int option;
-  char *interface;
   char *address;
   ip_mask_t *ipmask;
+#ifdef HAVE_LINUX
+  char *interface;
+#endif
 cp
   if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
     {
@@ -408,14 +410,10 @@ cp
 
   /* Connect */
 
-  a.sin_family = AF_INET;
-  a.sin_port = htons(c->port);
-  a.sin_addr.s_addr = htonl(c->address);
-
-  if(connect(c->socket, (struct sockaddr *)&a, sizeof(a)) == -1)
+  if(connect(c->socket, c->address->ai_addr, c->address->ai_addrlen) == -1)
     {
       close(c->socket);
-      syslog(LOG_ERR, _("%s port %hd: %m"), c->hostname, c->port);
+      syslog(LOG_ERR, _("%s port %s: %m"), c->hostname, c->port);
       return -1;
     }
 
@@ -424,13 +422,13 @@ cp
   if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
     {
       close(c->socket);
-      syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
+      syslog(LOG_ERR, _("fcntl for %s port %s: %m"),
              c->hostname, c->port);
       return -1;
     }
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
-    syslog(LOG_INFO, _("Connected to %s port %hd"),
+    syslog(LOG_INFO, _("Connected to %s port %s"),
          c->hostname, c->port);
 cp
   return 0;
@@ -439,8 +437,21 @@ cp
 int setup_outgoing_connection(char *name)
 {
   connection_t *c;
-  struct hostent *h;
+  node_t *n;
+  struct addrinfo *ai, *aitop, hints;
+  int r, ipv6preferred;
+
 cp
+  n = lookup_node(name);
+  
+  if(n)
+    if(n->connection)
+      {
+        if(debug_lvl >= DEBUG_CONNECTIONS)       
+          syslog(LOG_INFO, _("Already connected to %s"), name);
+        return 0;
+      }
+
   c = new_connection();
   c->name = xstrdup(name);
 
@@ -454,27 +465,37 @@ cp
       return -1;
     }
 
-  if(!get_config_port(lookup_config(c->config_tree, "Port"), &c->port))
+  if(!get_config_string(lookup_config(c->config_tree, "Port"), &c->port))
     {
       syslog(LOG_ERR, _("No port specified for %s"), c->name);
       free_connection(c);
       return -1;
     }
 
-  if(!(h = gethostbyname(c->hostname)))
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_family = AF_INET;
+  if(get_config_bool(lookup_config(c->config_tree, "IPv6Preferred"), &ipv6preferred))
     {
-      syslog(LOG_ERR, _("Error looking up `%s': %m"), c->hostname);
-      free_connection(c);
+      if(ipv6preferred)
+       hints.ai_family = PF_UNSPEC;
+    }
+
+  if((r = getaddrinfo(c->hostname, c->port, &hints, &aitop)) != 0)
+    {
+      syslog(LOG_ERR, _("Looking up %s failed: %s\n"),
+            c->hostname, gai_strerror(r));
       return -1;
     }
 
-  c->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
-  c->hostname = hostlookup(htonl(c->address));
+  for(ai = aitop; ai != NULL; ai = ai->ai_next)
+    {
+      if(setup_outgoing_socket(c) < 0)
+       continue;
+    }
 
-  if(setup_outgoing_socket(c) < 0)
+  if(ai == NULL)
     {
-      syslog(LOG_ERR, _("Could not set up a meta connection to %s (%s)"),
-             c->name, c->hostname);
+      /* No connection alternative succeeded */
       free_connection(c);
       return -1;
     }
@@ -494,7 +515,6 @@ int read_rsa_public_key(connection_t *c)
   FILE *fp;
   char *fname;
   char *key;
-  void *result;
 cp
   if(!c->rsa_key)
     c->rsa_key = RSA_new();
@@ -520,9 +540,9 @@ cp
                     fname);
               return -1;
             }
-          result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
+          c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
           fclose(fp);
-          if(!result)
+          if(!c->rsa_key)
             {
               syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
                     fname);
@@ -536,19 +556,16 @@ cp
 
   /* Else, check if a harnessed public key is in the config file */
 
-  result = NULL;
-
   asprintf(&fname, "%s/hosts/%s", confbase, c->name);
   if((fp = fopen(fname, "r")))
     {
-      result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
+      c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
       fclose(fp);
-      free(fname);
     }
 
   free(fname);
 
-  if(result)
+  if(c->rsa_key)
     return 0;
   else
     {
@@ -560,14 +577,11 @@ cp
 int read_rsa_private_key(void)
 {
   FILE *fp;
-  void *result;
   char *fname, *key;
 cp
-  if(!myself->connection->rsa_key)
-    myself->connection->rsa_key = RSA_new();
-
   if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
     {
+      myself->connection->rsa_key = RSA_new();
       BN_hex2bn(&myself->connection->rsa_key->d, key);
       BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
     }
@@ -579,9 +593,9 @@ cp
                 fname);
           return -1;
         }
-      result = PEM_read_RSAPrivateKey(fp, &myself->connection->rsa_key, NULL, NULL);
+      myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
       fclose(fp);
-      if(!result)
+      if(!myself->connection->rsa_key)
         {
           syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %m"),
                 fname);
@@ -807,7 +821,9 @@ cp
 
   terminate_connection(myself->connection, 0);
 
-//  destroy_trees();
+  close(udp_socket);
+  close(tcp_socket);
+
   exit_edges();
   exit_subnets();
   exit_nodes();
@@ -842,13 +858,13 @@ cp
 
   c->address = ntohl(ci.sin_addr.s_addr);
   c->hostname = hostlookup(ci.sin_addr.s_addr);
-  c->port = htons(ci.sin_port);                                /* This one will be overwritten later */
+  c->port = htons(ci.sin_port);
   c->socket = sfd;
   c->last_ping_time = time(NULL);
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_NOTICE, _("Connection from %s port %d"),
-         c->hostname, htons(ci.sin_port));
+         c->hostname, c->port);
 
   c->allow_request = ID;
 cp
@@ -975,7 +991,8 @@ cp
   /* Deactivate */
 
   c->status.active = 0;
-  c->node->connection = NULL;
+  if(c->node)
+    c->node->connection = NULL;
   do_prune = 1;
 cp
 }
@@ -1093,11 +1110,17 @@ cp
   while(cfg)
     {
       get_config_string(cfg, &name);
-      cfg = lookup_config_next(config_tree, cfg);  /* Next time skip to next ConnectTo line */
+
+      if(check_id(name))
+        {
+          syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
+          continue;
+        }
 
       if(setup_outgoing_connection(name))   /* function returns 0 when there are no problems */
         retry = 1;
 
+      cfg = lookup_config_next(config._tree, cfg); /* Next time skip to next ConnectTo line */
     }
 
   get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout);
@@ -1108,7 +1131,7 @@ cp
       if(seconds_till_retry > maxtimeout)    /* Don't wait more than MAXTIMEOUT seconds. */
         seconds_till_retry = maxtimeout;
 
-      syslog(LOG_ERR, _("Failed to setup all outgoing connections, will retry in %d seconds"),
+      syslog(LOG_ERR, _("Failed to setup any outgoing connection, will retry in %d seconds"),
         seconds_till_retry);
   
       /* Randomize timeout to avoid global synchronisation effects */