Added SILC Thread Queue API
[silc.git] / lib / silcutil / silcnet.c
index d083656eba63e379357d64acdc60848ec9aad701..edd982a6ccfed5b7e50bc2c42320835c5002b9e2 100644 (file)
@@ -4,13 +4,12 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2007 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
 */
 /* $Id$ */
 
-#include "silcincludes.h"
-#include "silcnet.h"
+#include "silc.h"
+
+/* Returns bound port from listener */
+
+SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
+                                      SilcUInt32 *port_count)
+{
+  SilcUInt16 *ports;
+  int i;
+
+  ports = silc_calloc(listener->socks_count, sizeof(*ports));
+  if (!ports)
+    return NULL;
+
+  for (i = 0; i < listener->socks_count; i++)
+    ports[i] = silc_net_get_local_port(listener->socks[i]);
+
+  if (port_count)
+    *port_count = listener->socks_count;
+
+  return ports;
+}
+
+/* Return bound IP from listener */
+
+char **silc_net_listener_get_ip(SilcNetListener listener,
+                               SilcUInt32 *ip_count)
+{
+  char **ips = NULL, *ip;
+  int i, k;
+
+  ips = silc_calloc(listener->socks_count, sizeof(*ips));
+  if (!ips)
+    return NULL;
+
+  for (i = 0, k = 0; i < listener->socks_count; i++) {
+    if (silc_net_check_local_by_sock(listener->socks[i], NULL, &ip))
+      ips[k++] = ip;
+  }
+
+  if (ip_count)
+    *ip_count = k;
+
+  return ips;
+}
+
+/* Return bound hostname from listener */
+
+char **silc_net_listener_get_hostname(SilcNetListener listener,
+                                     SilcUInt32 *hostname_count)
+{
+  char **hs = NULL, *h;
+  int i, k;
+
+  hs = silc_calloc(listener->socks_count, sizeof(*hs));
+  if (!hs)
+    return NULL;
+
+  for (i = 0, k = 0; i < listener->socks_count; i++) {
+    if (silc_net_check_local_by_sock(listener->socks[i], &h, NULL))
+      hs[k++] = h;
+  }
+
+  if (hostname_count)
+    *hostname_count = k;
+
+  return hs;
+}
 
 /* Accepts a connection from a particular socket */
 
 int silc_net_accept_connection(int sock)
 {
-  return accept(sock, 0, 0);
+  int ret = accept(sock, 0, 0);
+  if (ret < 0)
+    silc_set_errno_posix(errno);
+  return ret;
 }
 
 /* Sets a option for a socket. */
 
 int silc_net_set_socket_opt(int sock, int level, int option, int on)
 {
-  return setsockopt(sock, level, option, (void *)&on, sizeof(on));
+  int ret = setsockopt(sock, level, option, (void *)&on, sizeof(on));
+  if (ret < 0)
+    silc_set_errno_posix(errno);
+  return ret;
 }
 
 /* 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);
+  int ret = getsockopt(sock, level, option, optval, opt_len);
+  if (ret < 0)
+    silc_set_errno_posix(errno);
+  return ret;
 }
 
 /* Checks whether IP address sent as argument is valid IPv4 address. */
 
-bool silc_net_is_ip4(const char *addr)
+SilcBool silc_net_is_ip4(const char *addr)
 {
   int count = 0;
 
   while (*addr) {
-    if (*addr != '.' && !isdigit(*addr))
+    if (*addr != '.' && !isdigit((int)*addr))
       return FALSE;
     if (*addr == '.')
       count++;
@@ -60,27 +134,27 @@ bool silc_net_is_ip4(const char *addr)
 
   if (count != 3)
     return FALSE;
-  
+
   return TRUE;
 }
 
 /* Checks whether IP address sent as argument is valid IPv6 address. */
 
-bool silc_net_is_ip6(const char *addr)
+SilcBool silc_net_is_ip6(const char *addr)
 {
   /* XXX does this work with all kinds of IPv6 addresses? */
   while (*addr) {
-    if (*addr != ':' && !isxdigit(*addr))
+    if (*addr != ':' && !isxdigit((int)*addr))
       return FALSE;
     addr++;
   }
-  
+
   return TRUE;
 }
 
 /* Checks whether IP address sent as argument is valid IP address. */
 
-bool silc_net_is_ip(const char *addr)
+SilcBool silc_net_is_ip(const char *addr)
 {
   if (silc_net_is_ip4(addr))
     return TRUE;
@@ -91,7 +165,7 @@ bool silc_net_is_ip(const char *addr)
 typedef struct {
   SilcNetResolveCallback completion;
   void *context;
-  bool prefer_ipv6;
+  SilcBool prefer_ipv6;
   SilcSchedule schedule;
   char *input;
   char *result;
@@ -119,10 +193,10 @@ static void *silc_net_gethostbyname_thread(void *context)
   char tmp[64];
 
   if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
-    r->result = strdup(tmp);
+    r->result = silc_strdup(tmp);
 
   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
-                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+                        SILC_TASK_TIMEOUT);
   silc_schedule_wakeup(schedule);
   return NULL;
 }
@@ -136,18 +210,19 @@ static void *silc_net_gethostbyaddr_thread(void *context)
   char tmp[256];
 
   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
-    r->result = strdup(tmp);
+    r->result = silc_strdup(tmp);
 
   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
-                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+                        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, 
-                           SilcUInt32 address_len)
+SilcBool silc_net_gethostbyname(const char *name,
+                               SilcBool prefer_ipv6, char *address,
+                               SilcUInt32 address_len)
 {
 #ifdef HAVE_IPV6
   struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL;
@@ -190,6 +265,14 @@ bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
   struct in_addr ip;
   char *tmp;
 
+  if (silc_net_is_ip4(name)) {
+    memset(address, 0, address_len);
+    if (address_len < strlen(name))
+      return FALSE;
+    strncpy(address, name, strlen(name));
+    return TRUE;
+  }
+
   hp = gethostbyname(name);
   if (!hp)
     return FALSE;
@@ -203,40 +286,46 @@ 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, 
-                                 bool prefer_ipv6,
+void silc_net_gethostbyname_async(const char *name,
+                                 SilcBool prefer_ipv6,
                                  SilcSchedule schedule,
                                  SilcNetResolveCallback completion,
                                  void *context)
 {
   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
 
+  if (!schedule) {
+    schedule = silc_schedule_get_global();
+    SILC_VERIFY(schedule);
+  }
+
   r->completion = completion;
   r->context = context;
   r->prefer_ipv6 = prefer_ipv6;
   r->schedule = schedule;
-  r->input = strdup(name);
+  r->input = silc_strdup(name);
 
   silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
 }
 
 /* Resolves hostname by IP address. */
 
-bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
+SilcBool 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)) {
@@ -246,7 +335,7 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
   freeaddrinfo(ai);
 #else
   struct hostent *hp;
-  unsigned char a[16];
+  unsigned char a[4];
 
   if (!silc_net_addr2bin(addr, a, sizeof(a)))
     return FALSE;
@@ -259,31 +348,39 @@ 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)
 {
   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
 
+  if (!schedule) {
+    schedule = silc_schedule_get_global();
+    SILC_VERIFY(schedule);
+  }
+
   r->completion = completion;
   r->context = context;
   r->schedule = schedule;
-  r->input = strdup(addr);
+  r->input = silc_strdup(addr);
 
   silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
 }
 
+#ifndef SILC_SYMBIAN
+
 /* Performs lookups for remote name and IP address. This peforms reverse
    lookup as well to verify that the IP has FQDN. */
 
-bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
+SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname,
+                                    char **ip)
 {
   char host[1024];
   int rval, len;
@@ -361,7 +458,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
 /* Performs lookups for local name and IP address. This peforms reverse
    lookup as well to verify that the IP has FQDN. */
 
-bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
+SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname,
+                                     char **ip)
 {
   char host[1024];
   int rval, len;
@@ -438,7 +536,7 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
 
 /* Return remote port by socket. */
 
-SilcUInt16 silc_net_get_remote_port(int sock)
+SilcUInt16 silc_net_get_remote_port(SilcSocket sock)
 {
 #ifdef HAVE_IPV6
   struct sockaddr_storage remote;
@@ -453,7 +551,7 @@ SilcUInt16 silc_net_get_remote_port(int sock)
   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
                  NI_NUMERICSERV))
     return 0;
-  
+
   return atoi(s);
 #else
   struct sockaddr_in remote;
@@ -470,7 +568,7 @@ SilcUInt16 silc_net_get_remote_port(int sock)
 
 /* Return local port by socket. */
 
-SilcUInt16 silc_net_get_local_port(int sock)
+SilcUInt16 silc_net_get_local_port(SilcSocket sock)
 {
 #ifdef HAVE_IPV6
   struct sockaddr_storage local;
@@ -485,7 +583,7 @@ SilcUInt16 silc_net_get_local_port(int sock)
   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
                  NI_NUMERICSERV))
     return 0;
-  
+
   return atoi(s);
 #else
   struct sockaddr_in local;
@@ -499,6 +597,7 @@ SilcUInt16 silc_net_get_local_port(int sock)
   return ntohs(local.sin_port);
 #endif
 }
+#endif /* !SILC_SYMBIAN */
 
 /* Return name of localhost. */
 
@@ -510,10 +609,10 @@ char *silc_net_localhost(void)
     return NULL;
 
   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
-    return strdup(hostname);
+    return silc_strdup(hostname);
 
   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
-  return strdup(hostname);
+  return silc_strdup(hostname);
 }
 
 /* Returns local IP address */
@@ -528,5 +627,5 @@ char *silc_net_localip(void)
   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
     return NULL;
 
-  return strdup(ip_addr);
+  return silc_strdup(ip_addr);
 }