Merge 0.9->trunk
[prosody.git] / util-src / net.c
1 /* Prosody IM
2 --
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6 -- Copyright (C) 2012 Paul Aurich
7 -- Copyright (C) 2013 Matthew Wild
8 -- Copyright (C) 2013 Florian Zeitz
9 --
10 */
11
12 #include <stddef.h>
13 #include <string.h>
14 #include <errno.h>
15
16 #ifndef _WIN32
17   #include <sys/ioctl.h>
18   #include <sys/types.h>
19   #include <sys/socket.h>
20   #include <net/if.h>
21   #include <ifaddrs.h>
22   #include <arpa/inet.h>
23   #include <netinet/in.h>
24 #endif
25
26 #include <lua.h>
27 #include <lauxlib.h>
28
29 /* Enumerate all locally configured IP addresses */
30
31 const char * const type_strings[] = {
32         "both",
33         "ipv4",
34         "ipv6",
35         NULL
36 };
37
38 static int lc_local_addresses(lua_State *L)
39 {
40         /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
41         const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
42         const long ip4_mask      = htonl(0xffff0000);
43 #ifndef _WIN32
44         struct ifaddrs *addr = NULL, *a;
45         int n = 1;
46 #endif
47         int type = luaL_checkoption(L, 1, "both", type_strings);
48         const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */
49         const char ipv4 = (type == 0 || type == 1);
50         const char ipv6 = (type == 0 || type == 2);
51
52 #ifndef _WIN32
53         if (getifaddrs(&addr) < 0) {
54                 lua_pushnil(L);
55                 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno,
56                 strerror(errno));
57                 return 2;
58         }
59
60         lua_newtable(L);
61
62         for (a = addr; a; a = a->ifa_next) {
63                 int family;
64                 char ipaddr[INET6_ADDRSTRLEN];
65                 const char *tmp = NULL;
66
67                 if (a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK)
68                         continue;
69
70                 family = a->ifa_addr->sa_family;
71
72                 if (ipv4 && family == AF_INET) {
73                         struct sockaddr_in *sa = (struct sockaddr_in *)a->ifa_addr;
74                         if (!link_local &&((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal))
75                                 continue;
76                         tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr));
77                 } else if (ipv6 && family == AF_INET6) {
78                         struct sockaddr_in6 *sa = (struct sockaddr_in6 *)a->ifa_addr;
79                         if (!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr))
80                                 continue;
81                         if (IN6_IS_ADDR_V4MAPPED(&sa->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&sa->sin6_addr))
82                                 continue;
83                         tmp = inet_ntop(family, &sa->sin6_addr, ipaddr, sizeof(ipaddr));
84                 }
85
86                 if (tmp != NULL) {
87                         lua_pushstring(L, tmp);
88                         lua_rawseti(L, -2, n++);
89                 }
90                 /* TODO: Error reporting? */
91         }
92
93         freeifaddrs(addr);
94
95         return 1;
96 #endif
97 }
98
99 int luaopen_util_net(lua_State* L)
100 {
101         luaL_Reg exports[] = {
102                 { "local_addresses", lc_local_addresses },
103                 { NULL, NULL }
104         };
105
106         luaL_register(L, "net",  exports);
107         return 1;
108 }