Merge 0.9->trunk
[prosody.git] / util-src / net.c
index 497cd95f831540223d52b7efdb863b01a3563fc5..e307c628146351873175d0dd92eb25d66188f73d 100644 (file)
@@ -20,6 +20,7 @@
   #include <net/if.h>
   #include <ifaddrs.h>
   #include <arpa/inet.h>
+  #include <netinet/in.h>
 #endif
 
 #include <lua.h>
@@ -36,13 +37,13 @@ const char * const type_strings[] = {
 
 static int lc_local_addresses(lua_State *L)
 {
+#ifndef _WIN32
        /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
        const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
        const long ip4_mask      = htonl(0xffff0000);
-#ifndef _WIN32
        struct ifaddrs *addr = NULL, *a;
-       int n = 1;
 #endif
+       int n = 1;
        int type = luaL_checkoption(L, 1, "both", type_strings);
        const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */
        const char ipv4 = (type == 0 || type == 1);
@@ -55,9 +56,10 @@ static int lc_local_addresses(lua_State *L)
                strerror(errno));
                return 2;
        }
-
+#endif
        lua_newtable(L);
 
+#ifndef _WIN32
        for (a = addr; a; a = a->ifa_next) {
                int family;
                char ipaddr[INET6_ADDRSTRLEN];
@@ -90,9 +92,17 @@ static int lc_local_addresses(lua_State *L)
        }
 
        freeifaddrs(addr);
-
-       return 1;
+#else
+       if (ipv4) {
+               lua_pushstring(L, "0.0.0.0");
+               lua_rawseti(L, -2, n++);
+       }
+       if (ipv6) {
+               lua_pushstring(L, "::");
+               lua_rawseti(L, -2, n++);
+       }
 #endif
+       return 1;
 }
 
 int luaopen_util_net(lua_State* L)