Initial code commit for Toolkit 1.1.
[silc.git] / lib / silcutil / silcnet.c
index da0635952b313189308f881916f83a7aa3165e6b..d778135107a8baed4365faf4e69eedb470e702de 100644 (file)
@@ -4,13 +4,12 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2005 Pekka Riikonen
 
   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
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-  
+  the Free Software Foundation; version 2 of the License.
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -20,7 +19,6 @@
 /* $Id$ */
 
 #include "silcincludes.h"
-#include "silcnet.h"
 
 /* Accepts a connection from a particular socket */
 
@@ -38,7 +36,7 @@ int silc_net_set_socket_opt(int sock, int level, int option, int on)
 
 /* Get socket options */
 
-int silc_net_get_socket_opt(int sock, int level, int option, 
+int silc_net_get_socket_opt(int sock, int level, int option,
                            void *optval, int *opt_len)
 {
   return getsockopt(sock, level, option, optval, opt_len);
@@ -60,7 +58,7 @@ bool silc_net_is_ip4(const char *addr)
 
   if (count != 3)
     return FALSE;
-  
+
   return TRUE;
 }
 
@@ -74,7 +72,7 @@ bool silc_net_is_ip6(const char *addr)
       return FALSE;
     addr++;
   }
-  
+
   return TRUE;
 }
 
@@ -115,14 +113,15 @@ SILC_TASK_CALLBACK(silc_net_resolve_completion)
 static void *silc_net_gethostbyname_thread(void *context)
 {
   SilcNetResolveContext r = (SilcNetResolveContext)context;
+  SilcSchedule schedule = r->schedule;
   char tmp[64];
 
   if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
     r->result = strdup(tmp);
 
-  silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1,
-                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-  silc_schedule_wakeup(r->schedule);
+  silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
+                        SILC_TASK_TIMEOUT);
+  silc_schedule_wakeup(schedule);
   return NULL;
 }
 
@@ -131,20 +130,21 @@ static void *silc_net_gethostbyname_thread(void *context)
 static void *silc_net_gethostbyaddr_thread(void *context)
 {
   SilcNetResolveContext r = (SilcNetResolveContext)context;
+  SilcSchedule schedule = r->schedule;
   char tmp[256];
 
   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
     r->result = strdup(tmp);
 
-  silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1,
-                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-  silc_schedule_wakeup(r->schedule);
+  silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
+                        SILC_TASK_TIMEOUT);
+  silc_schedule_wakeup(schedule);
   return NULL;
 }
 
 /* Resolves IP address for hostname. */
 
-bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, 
+bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
                            SilcUInt32 address_len)
 {
 #ifdef HAVE_IPV6
@@ -201,13 +201,13 @@ bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
   memset(address, 0, address_len);
   strncpy(address, tmp, strlen(tmp));
 #endif
-  
+
   return TRUE;
 }
 
 /* Resolves IP address for hostname async. */
 
-void silc_net_gethostbyname_async(const char *name, 
+void silc_net_gethostbyname_async(const char *name,
                                  bool prefer_ipv6,
                                  SilcSchedule schedule,
                                  SilcNetResolveCallback completion,
@@ -230,11 +230,11 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
 {
 #ifdef HAVE_IPV6
   struct addrinfo req, *ai;
-  
+
   memset(&req, 0, sizeof(req));
   req.ai_socktype = SOCK_STREAM;
   req.ai_flags = AI_CANONNAME;
-  
+
   if (getaddrinfo(addr, NULL, &req, &ai))
     return FALSE;
   if (getnameinfo(ai->ai_addr, ai->ai_addrlen, name, name_len, NULL, 0, 0)) {
@@ -244,8 +244,12 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
   freeaddrinfo(ai);
 #else
   struct hostent *hp;
+  unsigned char a[16];
+
+  if (!silc_net_addr2bin(addr, a, sizeof(a)))
+    return FALSE;
 
-  hp = gethostbyaddr(addr, strlen(addr), AF_INET);
+  hp = gethostbyaddr(a, 4, AF_INET);
   if (!hp)
     return FALSE;
   if (name_len < strlen(hp->h_name))
@@ -253,13 +257,13 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
   memset(name, 0, name_len);
   strncpy(name, hp->h_name, strlen(hp->h_name));
 #endif
-  
+
   return TRUE;
 }
 
 /* Resolves hostname by IP address async. */
 
-void silc_net_gethostbyaddr_async(const char *addr, 
+void silc_net_gethostbyaddr_async(const char *addr,
                                  SilcSchedule schedule,
                                  SilcNetResolveCallback completion,
                                  void *context)
@@ -279,63 +283,76 @@ void silc_net_gethostbyaddr_async(const char *addr,
 
 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
 {
-  struct sockaddr_in remote;
-  struct hostent *dest;
-  char *host_ip = NULL;
-  char host_name[1024];
+  char host[1024];
   int rval, len;
-  int i;
 
-  *hostname = NULL;
+#ifdef HAVE_IPV6
+  struct sockaddr_storage remote;
+  char s[NI_MAXHOST];
+
+  if (hostname)
+    *hostname = NULL;
   *ip = NULL;
 
   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
 
   memset(&remote, 0, sizeof(remote));
+  memset(&s, 0, sizeof(s));
   len = sizeof(remote);
   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
   if (rval < 0)
     return FALSE;
 
-  host_ip = inet_ntoa(remote.sin_addr);
-  if (!host_ip)
+  if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0,
+                 NI_NUMERICHOST))
     return FALSE;
 
-  *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
-  memcpy(*ip, host_ip, strlen(host_ip));
-
-  /* Get host by address */
-  dest = gethostbyaddr((char *)&remote.sin_addr, 
-                      sizeof(struct in_addr), AF_INET);
-  if (!dest)
+  *ip = silc_memdup(s, strlen(s));
+  if (*ip == NULL)
     return FALSE;
+#else
+  struct sockaddr_in remote;
+  char *host_ip;
 
-  /* Get same host by name to see that the remote host really is
-     the who it says it is */
-  memset(host_name, 0, sizeof(host_name));
-  memcpy(host_name, dest->h_name, strlen(dest->h_name));
+  if (hostname)
+    *hostname = NULL;
+  *ip = NULL;
 
-  *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
-  memcpy(*hostname, host_name, strlen(host_name));
-  SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
+  SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
 
-  dest = gethostbyname(host_name);
-  if (!dest)
+  memset(&remote, 0, sizeof(remote));
+  len = sizeof(remote);
+  rval = getpeername(sock, (struct sockaddr *)&remote, &len);
+  if (rval < 0)
     return FALSE;
 
-  /* Find the address from list */
-  for (i = 0; dest->h_addr_list[i]; i++)
-    if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, 
-               sizeof(struct in_addr)))
-      break;
-  if (!dest->h_addr_list[i])
+  host_ip = inet_ntoa(remote.sin_addr);
+  if (!host_ip)
     return FALSE;
 
-  silc_free(*ip);
-  *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
-  memcpy(*ip, host_ip, strlen(host_ip));
-  SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
+  *ip = silc_memdup(host_ip, strlen(host_ip));
+  if (*ip == NULL)
+    return FALSE;
+#endif
+
+  /* Do reverse lookup if we want hostname too. */
+  if (hostname) {
+    /* Get host by address */
+    if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
+      return FALSE;
 
+    *hostname = silc_memdup(host, strlen(host));
+    SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
+
+    /* Reverse */
+    if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
+      return FALSE;
+
+    if (strcmp(*ip, host))
+      return FALSE;
+  }
+
+  SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
   return TRUE;
 }
 
@@ -344,63 +361,76 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
 
 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
 {
-  struct sockaddr_in local;
-  struct hostent *dest;
-  char *host_ip = NULL;
-  char host_name[1024];
+  char host[1024];
   int rval, len;
-  int i;
 
-  *hostname = NULL;
+#ifdef HAVE_IPV6
+  struct sockaddr_storage local;
+  char s[NI_MAXHOST];
+
+  if (hostname)
+    *hostname = NULL;
   *ip = NULL;
 
   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
 
   memset(&local, 0, sizeof(local));
+  memset(&s, 0, sizeof(s));
   len = sizeof(local);
   rval = getsockname(sock, (struct sockaddr *)&local, &len);
   if (rval < 0)
     return FALSE;
 
-  host_ip = inet_ntoa(local.sin_addr);
-  if (!host_ip)
+  if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
+                 NI_NUMERICHOST))
     return FALSE;
 
-  *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
-  memcpy(*ip, host_ip, strlen(host_ip));
-
-  /* Get host by address */
-  dest = gethostbyaddr((char *)&local.sin_addr, 
-                      sizeof(struct in_addr), AF_INET);
-  if (!dest)
+  *ip = silc_memdup(s, strlen(s));
+  if (*ip == NULL)
     return FALSE;
+#else
+  struct sockaddr_in local;
+  char *host_ip;
+
+  if (hostname)
+    *hostname = NULL;
+  *ip = NULL;
 
-  /* Get same host by name to see that the local host really is
-     the who it says it is */
-  memset(host_name, 0, sizeof(host_name));
-  memcpy(host_name, dest->h_name, strlen(dest->h_name));
+  SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
 
-  *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
-  memcpy(*hostname, host_name, strlen(host_name));
-  SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
+  memset(&local, 0, sizeof(local));
+  len = sizeof(local);
+  rval = getsockname(sock, (struct sockaddr *)&local, &len);
+  if (rval < 0)
+    return FALSE;
 
-  dest = gethostbyname(host_name);
-  if (!dest)
+  host_ip = inet_ntoa(local.sin_addr);
+  if (!host_ip)
     return FALSE;
 
-  /* Find the address from list */
-  for (i = 0; dest->h_addr_list[i]; i++)
-    if (!memcmp(dest->h_addr_list[i], &local.sin_addr, 
-              sizeof(struct in_addr)))
-      break;
-  if (!dest->h_addr_list[i])
+  *ip = silc_memdup(host_ip, strlen(host_ip));
+  if (*ip == NULL)
     return FALSE;
+#endif
 
-  silc_free(*ip);
-  *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
-  memcpy(*ip, host_ip, strlen(host_ip));
-  SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
+  /* Do reverse lookup if we want hostname too. */
+  if (hostname) {
+    /* Get host by address */
+    if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
+      return FALSE;
+
+    *hostname = silc_memdup(host, strlen(host));
+    SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
 
+    /* Reverse */
+    if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
+      return FALSE;
+
+    if (strcmp(*ip, host))
+      return FALSE;
+  }
+
+  SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
   return TRUE;
 }
 
@@ -419,9 +449,9 @@ SilcUInt16 silc_net_get_remote_port(int sock)
     return 0;
 
   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
-      NI_NUMERICSERV))
+                 NI_NUMERICSERV))
     return 0;
-  
+
   return atoi(s);
 #else
   struct sockaddr_in remote;
@@ -451,9 +481,9 @@ SilcUInt16 silc_net_get_local_port(int sock)
     return 0;
 
   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
-      NI_NUMERICSERV))
+                 NI_NUMERICSERV))
     return 0;
-  
+
   return atoi(s);
 #else
   struct sockaddr_in local;