Sensible defaults for $INTERFACE.
[tinc] / src / netutl.c
index 562b105..14ed5ce 100644 (file)
@@ -1,7 +1,7 @@
 /*
     netutl.c -- some supporting network utility code
-    Copyright (C) 1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>
-                  2000,2001 Guus Sliepen <guus@sliepen.warande.net>
+    Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>
+                  2000-2002 Guus Sliepen <guus@sliepen.warande.net>
 
     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
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: netutl.c,v 1.12.4.19 2001/05/07 19:08:46 guus Exp $
+    $Id: netutl.c,v 1.12.4.23 2002/02/11 10:16:18 guus Exp $
 */
 
 #include "config.h"
 
-#include <arpa/inet.h>
+#include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdio.h>
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <syslog.h>
+#include <arpa/inet.h>
 
 #include <utils.h>
 #include <xalloc.h>
@@ -46,15 +47,11 @@ char *hostlookup(unsigned long addr)
   char *name;
   struct hostent *host = NULL;
   struct in_addr in;
-  config_t const *cfg;
-  int lookup_hostname;
+  int lookup_hostname = 0;
 cp
   in.s_addr = addr;
 
-  lookup_hostname = 0;
-  if((cfg = get_config_val(config, config_hostnames)) != NULL)
-    if(cfg->data.val == stupid_true)
-      lookup_hostname = 1;
+  get_config_bool(lookup_config(config_tree, "Hostnames"), &lookup_hostname);
 
   if(lookup_hostname)
     host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
@@ -72,44 +69,37 @@ cp
 }
 
 /*
-  Turn a string into an IP addy with netmask
+  Turn a string into an IP address
   return NULL on failure
+  Should support IPv6 and other stuff in the future.
 */
-ip_mask_t *strtoip(char *str)
+ipv4_t str2address(char *str)
 {
-  ip_mask_t *ip;
-  int masker;
-  char *q, *p;
+  ipv4_t address;
   struct hostent *h;
 cp
-  p = str;
-  if((q = strchr(p, '/')))
-    {
-      *q = '\0';
-      q++; /* q now points to netmask part, or NULL if no mask */
-    }
-
-  if(!(h = gethostbyname(p)))
+  if(!(h = gethostbyname(str)))
     {
       if(debug_lvl >= DEBUG_ERROR)
-        syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
+        syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), str, strerror(errno));
         
-      return NULL;
+      return 0;
     }
 
-  masker = 0;
-  if(q)
-    {
-      masker = strtol(q, &p, 10);
-      if(q == p || (*p))
-       return NULL;
-    }
-
-  ip = xmalloc(sizeof(*ip));
-  ip->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
-
-  ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
+  address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
 cp
-  return ip;
+  return address;
 }
 
+char *address2str(ipv4_t address)
+{
+  char *str;
+cp
+  asprintf(&str, "%hu.%hu.%hu.%hu",
+          (unsigned short int)((address >> 24) & 255),
+          (unsigned short int)((address >> 16) & 255),
+          (unsigned short int)((address >> 8) & 255),
+          (unsigned short int)(address & 255));
+cp
+  return str;
+}