Software /
code /
prosody
Comparison
util-src/net.c @ 6615:8e4572a642cb
util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 03 Apr 2015 19:52:48 +0200 |
parent | 6413:a552f4170aed |
child | 6789:6b180e77c97a |
comparison
equal
deleted
inserted
replaced
6613:2aae36312eb9 | 6615:8e4572a642cb |
---|---|
12 #include <stddef.h> | 12 #include <stddef.h> |
13 #include <string.h> | 13 #include <string.h> |
14 #include <errno.h> | 14 #include <errno.h> |
15 | 15 |
16 #ifndef _WIN32 | 16 #ifndef _WIN32 |
17 #include <sys/ioctl.h> | 17 #include <sys/ioctl.h> |
18 #include <sys/types.h> | 18 #include <sys/types.h> |
19 #include <sys/socket.h> | 19 #include <sys/socket.h> |
20 #include <net/if.h> | 20 #include <net/if.h> |
21 #include <ifaddrs.h> | 21 #include <ifaddrs.h> |
22 #include <arpa/inet.h> | 22 #include <arpa/inet.h> |
23 #include <netinet/in.h> | 23 #include <netinet/in.h> |
24 #endif | 24 #endif |
25 | 25 |
26 #include <lua.h> | 26 #include <lua.h> |
27 #include <lauxlib.h> | 27 #include <lauxlib.h> |
28 | 28 |
30 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) | 30 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0) |
31 #endif | 31 #endif |
32 | 32 |
33 /* Enumerate all locally configured IP addresses */ | 33 /* Enumerate all locally configured IP addresses */ |
34 | 34 |
35 const char * const type_strings[] = { | 35 const char* const type_strings[] = { |
36 "both", | 36 "both", |
37 "ipv4", | 37 "ipv4", |
38 "ipv6", | 38 "ipv6", |
39 NULL | 39 NULL |
40 }; | 40 }; |
41 | 41 |
42 static int lc_local_addresses(lua_State *L) | 42 static int lc_local_addresses(lua_State* L) { |
43 { | |
44 #ifndef _WIN32 | 43 #ifndef _WIN32 |
45 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ | 44 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ |
46 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ | 45 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ |
47 const long ip4_mask = htonl(0xffff0000); | 46 const long ip4_mask = htonl(0xffff0000); |
48 struct ifaddrs *addr = NULL, *a; | 47 struct ifaddrs* addr = NULL, *a; |
49 #endif | 48 #endif |
50 int n = 1; | 49 int n = 1; |
51 int type = luaL_checkoption(L, 1, "both", type_strings); | 50 int type = luaL_checkoption(L, 1, "both", type_strings); |
52 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */ | 51 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */ |
53 const char ipv4 = (type == 0 || type == 1); | 52 const char ipv4 = (type == 0 || type == 1); |
54 const char ipv6 = (type == 0 || type == 2); | 53 const char ipv6 = (type == 0 || type == 2); |
55 | 54 |
56 #ifndef _WIN32 | 55 #ifndef _WIN32 |
57 if (getifaddrs(&addr) < 0) { | 56 |
57 if(getifaddrs(&addr) < 0) { | |
58 lua_pushnil(L); | 58 lua_pushnil(L); |
59 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno, | 59 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno, |
60 strerror(errno)); | 60 strerror(errno)); |
61 return 2; | 61 return 2; |
62 } | 62 } |
63 | |
63 #endif | 64 #endif |
64 lua_newtable(L); | 65 lua_newtable(L); |
65 | 66 |
66 #ifndef _WIN32 | 67 #ifndef _WIN32 |
67 for (a = addr; a; a = a->ifa_next) { | 68 |
69 for(a = addr; a; a = a->ifa_next) { | |
68 int family; | 70 int family; |
69 char ipaddr[INET6_ADDRSTRLEN]; | 71 char ipaddr[INET6_ADDRSTRLEN]; |
70 const char *tmp = NULL; | 72 const char* tmp = NULL; |
71 | 73 |
72 if (a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) | 74 if(a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) { |
73 continue; | 75 continue; |
76 } | |
74 | 77 |
75 family = a->ifa_addr->sa_family; | 78 family = a->ifa_addr->sa_family; |
76 | 79 |
77 if (ipv4 && family == AF_INET) { | 80 if(ipv4 && family == AF_INET) { |
78 struct sockaddr_in *sa = (struct sockaddr_in *)a->ifa_addr; | 81 struct sockaddr_in* sa = (struct sockaddr_in*)a->ifa_addr; |
79 if (!link_local &&((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) | 82 |
83 if(!link_local && ((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) { | |
80 continue; | 84 continue; |
85 } | |
86 | |
81 tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr)); | 87 tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr)); |
82 } else if (ipv6 && family == AF_INET6) { | 88 } else if(ipv6 && family == AF_INET6) { |
83 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)a->ifa_addr; | 89 struct sockaddr_in6* sa = (struct sockaddr_in6*)a->ifa_addr; |
84 if (!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) | 90 |
91 if(!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { | |
85 continue; | 92 continue; |
86 if (IN6_IS_ADDR_V4MAPPED(&sa->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&sa->sin6_addr)) | 93 } |
94 | |
95 if(IN6_IS_ADDR_V4MAPPED(&sa->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&sa->sin6_addr)) { | |
87 continue; | 96 continue; |
97 } | |
98 | |
88 tmp = inet_ntop(family, &sa->sin6_addr, ipaddr, sizeof(ipaddr)); | 99 tmp = inet_ntop(family, &sa->sin6_addr, ipaddr, sizeof(ipaddr)); |
89 } | 100 } |
90 | 101 |
91 if (tmp != NULL) { | 102 if(tmp != NULL) { |
92 lua_pushstring(L, tmp); | 103 lua_pushstring(L, tmp); |
93 lua_rawseti(L, -2, n++); | 104 lua_rawseti(L, -2, n++); |
94 } | 105 } |
106 | |
95 /* TODO: Error reporting? */ | 107 /* TODO: Error reporting? */ |
96 } | 108 } |
97 | 109 |
98 freeifaddrs(addr); | 110 freeifaddrs(addr); |
99 #else | 111 #else |
100 if (ipv4) { | 112 |
113 if(ipv4) { | |
101 lua_pushstring(L, "0.0.0.0"); | 114 lua_pushstring(L, "0.0.0.0"); |
102 lua_rawseti(L, -2, n++); | 115 lua_rawseti(L, -2, n++); |
103 } | 116 } |
104 if (ipv6) { | 117 |
118 if(ipv6) { | |
105 lua_pushstring(L, "::"); | 119 lua_pushstring(L, "::"); |
106 lua_rawseti(L, -2, n++); | 120 lua_rawseti(L, -2, n++); |
107 } | 121 } |
122 | |
108 #endif | 123 #endif |
109 return 1; | 124 return 1; |
110 } | 125 } |
111 | 126 |
112 int luaopen_util_net(lua_State* L) | 127 int luaopen_util_net(lua_State* L) { |
113 { | |
114 luaL_Reg exports[] = { | 128 luaL_Reg exports[] = { |
115 { "local_addresses", lc_local_addresses }, | 129 { "local_addresses", lc_local_addresses }, |
116 { NULL, NULL } | 130 { NULL, NULL } |
117 }; | 131 }; |
118 | 132 |