updates.
[silc.git] / lib / silcutil / silcnet.c
index ad563705080917ae9b8ca103346e4fb6df26e2ef..0101faae4d344522048cd24f515cf539df633f40 100644 (file)
@@ -43,6 +43,7 @@ int silc_net_get_socket_opt(int sock, int level, int option,
 {
   return getsockopt(sock, level, option, optval, opt_len);
 }
+
 /* Checks whether IP address sent as argument is valid IP address. */
 
 bool silc_net_is_ip(const char *addr)
@@ -214,10 +215,11 @@ uint16 silc_net_get_local_port(int sock)
 
 /* Return name of localhost. */
 
-char *silc_net_localhost()
+char *silc_net_localhost(void)
 {
   char hostname[256];
   struct hostent *dest;
+  char *h;
 
   if (gethostname(hostname, sizeof(hostname)))
     return NULL;
@@ -226,5 +228,34 @@ char *silc_net_localhost()
   if (!dest)
     return strdup(hostname);
 
+  h = strdup(dest->h_name);
+  dest = gethostbyaddr((char *)dest->h_addr_list[0],
+                      sizeof(struct in_addr), AF_INET);
+  if (!dest)
+    return h;
+
+  silc_free(h);
   return strdup(dest->h_name);
 }
+
+/* Returns local IP address */
+
+char *silc_net_localip(void)
+{
+  char hostname[256];
+  struct hostent *dest;
+  struct in_addr ip;
+  char *ips;
+
+  if (gethostname(hostname, sizeof(hostname)))
+    return NULL;
+
+  dest = gethostbyname(hostname);
+  if (!dest)
+    return NULL;
+
+  memcpy(&ip.s_addr, dest->h_addr_list[0], 4);
+  ips = inet_ntoa(ip);
+
+  return strdup(ips);
+}