Added SILC Thread Queue API
[silc.git] / lib / silcutil / silcnet.c
index a6c0f9271456171013bc36b0aac679bae7a2b03c..edd982a6ccfed5b7e50bc2c42320835c5002b9e2 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2006 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
@@ -41,18 +41,70 @@ SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener,
   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 */
@@ -60,7 +112,10 @@ int silc_net_set_socket_opt(int sock, int level, int option, int on)
 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. */
@@ -138,7 +193,7 @@ 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);
@@ -155,7 +210,7 @@ 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);
@@ -210,6 +265,14 @@ SilcBool silc_net_gethostbyname(const char *name,
   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;
@@ -237,11 +300,16 @@ void silc_net_gethostbyname_async(const char *name,
 {
   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);
 }
@@ -267,7 +335,7 @@ SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
   freeaddrinfo(ai);
 #else
   struct hostent *hp;
-  unsigned char a[16];
+  unsigned char a[4];
 
   if (!silc_net_addr2bin(addr, a, sizeof(a)))
     return FALSE;
@@ -293,10 +361,15 @@ void silc_net_gethostbyaddr_async(const char *addr,
 {
   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);
 }
@@ -536,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 */
@@ -554,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);
 }