Annotate

util-src/net.c @ 6945:d779c55058c6

util.cache: Small update to prefix private fields with an underscore, add a :count() method (same as util.queue) and add an optional on_evict callback
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Nov 2015 00:07:48 +0000
parent 6789:6b180e77c97a
child 7818:54669df178c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 /* Prosody IM
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 --
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- Copyright (C) 2012 Paul Aurich
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 -- Copyright (C) 2013 Matthew Wild
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- Copyright (C) 2013 Florian Zeitz
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 --
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 #include <stddef.h>
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 #include <string.h>
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 #include <errno.h>
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 #ifndef _WIN32
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
17 #include <sys/ioctl.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
18 #include <sys/types.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
19 #include <sys/socket.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
20 #include <net/if.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
21 #include <ifaddrs.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
22 #include <arpa/inet.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
23 #include <netinet/in.h>
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 #endif
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 #include <lua.h>
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 #include <lauxlib.h>
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
29 #if (LUA_VERSION_NUM == 501)
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
30 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
6413
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
31 #endif
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
32
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 /* Enumerate all locally configured IP addresses */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
35 const char* const type_strings[] = {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 "both",
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 "ipv4",
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 "ipv6",
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 NULL
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 };
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
42 static int lc_local_addresses(lua_State* L) {
5752
729c78581308 util.net: Fix compilation on Windows (outputs an empty list of addresses for now) (tested only in my head)
Matthew Wild <mwild1@gmail.com>
parents: 5750
diff changeset
43 #ifndef _WIN32
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 const long ip4_mask = htonl(0xffff0000);
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
47 struct ifaddrs* addr = NULL, *a;
5810
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
48 #endif
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 int n = 1;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 int type = luaL_checkoption(L, 1, "both", type_strings);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 const char link_local = lua_toboolean(L, 2); /* defaults to 0 (false) */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 const char ipv4 = (type == 0 || type == 1);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 const char ipv6 = (type == 0 || type == 2);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 #ifndef _WIN32
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
56
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
57 if(getifaddrs(&addr) < 0) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 lua_pushnil(L);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno,
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
60 strerror(errno));
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 return 2;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 }
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
63
5752
729c78581308 util.net: Fix compilation on Windows (outputs an empty list of addresses for now) (tested only in my head)
Matthew Wild <mwild1@gmail.com>
parents: 5750
diff changeset
64 #endif
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 lua_newtable(L);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
5752
729c78581308 util.net: Fix compilation on Windows (outputs an empty list of addresses for now) (tested only in my head)
Matthew Wild <mwild1@gmail.com>
parents: 5750
diff changeset
67 #ifndef _WIN32
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
68
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
69 for(a = addr; a; a = a->ifa_next) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 int family;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 char ipaddr[INET6_ADDRSTRLEN];
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
72 const char* tmp = NULL;
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
74 if(a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 continue;
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
76 }
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 family = a->ifa_addr->sa_family;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
80 if(ipv4 && family == AF_INET) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
81 struct sockaddr_in* sa = (struct sockaddr_in*)a->ifa_addr;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
82
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
83 if(!link_local && ((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 continue;
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
85 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
86
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr));
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
88 } else if(ipv6 && family == AF_INET6) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
89 struct sockaddr_in6* sa = (struct sockaddr_in6*)a->ifa_addr;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
90
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
91 if(!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 continue;
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
93 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
94
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
95 if(IN6_IS_ADDR_V4MAPPED(&sa->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&sa->sin6_addr)) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 continue;
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
97 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
98
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 tmp = inet_ntop(family, &sa->sin6_addr, ipaddr, sizeof(ipaddr));
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 }
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
102 if(tmp != NULL) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 lua_pushstring(L, tmp);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 lua_rawseti(L, -2, n++);
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 }
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
106
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 /* TODO: Error reporting? */
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 }
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 freeifaddrs(addr);
5810
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
111 #else
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
112
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
113 if(ipv4) {
5810
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
114 lua_pushstring(L, "0.0.0.0");
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
115 lua_rawseti(L, -2, n++);
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
116 }
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
117
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
118 if(ipv6) {
5810
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
119 lua_pushstring(L, "::");
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
120 lua_rawseti(L, -2, n++);
99ad8d42d0c9 util.net: Fix s2sout on Windows (return 0.0.0.0 and :: instead of an empty list)
Kim Alvefur <zash@zash.se>
parents: 5752
diff changeset
121 }
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
122
5752
729c78581308 util.net: Fix compilation on Windows (outputs an empty list of addresses for now) (tested only in my head)
Matthew Wild <mwild1@gmail.com>
parents: 5750
diff changeset
123 #endif
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return 1;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 }
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
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
Kim Alvefur <zash@zash.se>
parents: 6413
diff changeset
127 int luaopen_util_net(lua_State* L) {
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 luaL_Reg exports[] = {
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 { "local_addresses", lc_local_addresses },
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 { NULL, NULL }
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 };
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
6411
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 5810
diff changeset
133 lua_newtable(L);
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
134 luaL_setfuncs(L, exports, 0);
5699
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 return 1;
e9f5384f5ff1 util.net: Add util.net, containing local_addresses() (removed from LuaSocket 3.0)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 }