comparison util-src/net.c @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parents 6eb5d2bb11af
children 1f6f05a98fcd
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
31 #include <lauxlib.h> 31 #include <lauxlib.h>
32 32
33 #if (LUA_VERSION_NUM == 501) 33 #if (LUA_VERSION_NUM == 501)
34 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) 34 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
35 #endif 35 #endif
36 #if (LUA_VERSION_NUM < 504)
37 #define luaL_pushfail lua_pushnil
38 #endif
36 39
37 /* Enumerate all locally configured IP addresses */ 40 /* Enumerate all locally configured IP addresses */
38 41
39 const char *const type_strings[] = { 42 static const char *const type_strings[] = {
40 "both", 43 "both",
41 "ipv4", 44 "ipv4",
42 "ipv6", 45 "ipv6",
43 NULL 46 NULL
44 }; 47 };
45 48
46 static int lc_local_addresses(lua_State *L) { 49 static int lc_local_addresses(lua_State *L) {
47 #ifndef _WIN32 50 #ifndef _WIN32
48 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ 51 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
49 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ 52 const uint32_t ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
50 const long ip4_mask = htonl(0xffff0000); 53 const uint32_t ip4_mask = htonl(0xffff0000);
51 struct ifaddrs *addr = NULL, *a; 54 struct ifaddrs *addr = NULL, *a;
52 #endif 55 #endif
53 int n = 1; 56 int n = 1;
54 int type = luaL_checkoption(L, 1, "both", type_strings); 57 int type = luaL_checkoption(L, 1, "both", type_strings);
55 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */ 58 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */
57 const char ipv6 = (type == 0 || type == 2); 60 const char ipv6 = (type == 0 || type == 2);
58 61
59 #ifndef _WIN32 62 #ifndef _WIN32
60 63
61 if(getifaddrs(&addr) < 0) { 64 if(getifaddrs(&addr) < 0) {
62 lua_pushnil(L); 65 luaL_pushfail(L);
63 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno, 66 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno,
64 strerror(errno)); 67 strerror(errno));
65 return 2; 68 return 2;
66 } 69 }
67 70
139 lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4); 142 lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4);
140 return 1; 143 return 1;
141 144
142 case -1: 145 case -1:
143 errno_ = errno; 146 errno_ = errno;
144 lua_pushnil(L); 147 luaL_pushfail(L);
145 lua_pushstring(L, strerror(errno_)); 148 lua_pushstring(L, strerror(errno_));
146 lua_pushinteger(L, errno_); 149 lua_pushinteger(L, errno_);
147 return 3; 150 return 3;
148 151
149 default: 152 default:
150 case 0: 153 case 0:
151 lua_pushnil(L); 154 luaL_pushfail(L);
152 lua_pushstring(L, strerror(EINVAL)); 155 lua_pushstring(L, strerror(EINVAL));
153 lua_pushinteger(L, EINVAL); 156 lua_pushinteger(L, EINVAL);
154 return 3; 157 return 3;
155 } 158 }
156 159
168 } 171 }
169 else if(l == 4) { 172 else if(l == 4) {
170 family = AF_INET; 173 family = AF_INET;
171 } 174 }
172 else { 175 else {
173 lua_pushnil(L); 176 luaL_pushfail(L);
174 lua_pushstring(L, strerror(EAFNOSUPPORT)); 177 lua_pushstring(L, strerror(EAFNOSUPPORT));
175 lua_pushinteger(L, EAFNOSUPPORT); 178 lua_pushinteger(L, EAFNOSUPPORT);
176 return 3; 179 return 3;
177 } 180 }
178 181
179 if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN)) 182 if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN))
180 { 183 {
181 errno_ = errno; 184 errno_ = errno;
182 lua_pushnil(L); 185 luaL_pushfail(L);
183 lua_pushstring(L, strerror(errno_)); 186 lua_pushstring(L, strerror(errno_));
184 lua_pushinteger(L, errno_); 187 lua_pushinteger(L, errno_);
185 return 3; 188 return 3;
186 } 189 }
187 190