aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2012-04-11 13:21:25 -0700
committerSam Roberts <vieuxtech@gmail.com>2012-04-11 13:25:11 -0700
commit2778766d678b147fc079d67dee036346381b4764 (patch)
treeda44507f62fb9c8cd078cf25f6dc24107e56af34 /src/inet.c
parent3a8ba90dfb0c2eb224f317dd692ede426691e72a (diff)
downloadluasocket-2778766d678b147fc079d67dee036346381b4764.tar.gz
luasocket-2778766d678b147fc079d67dee036346381b4764.tar.bz2
luasocket-2778766d678b147fc079d67dee036346381b4764.zip
Preliminary IPv6 support for v2.1
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c52
1 files changed, 49 insertions, 3 deletions
diff --git a/src/inet.c b/src/inet.c
index 862288c..21a97c9 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -2,7 +2,7 @@
2* Internet domain functions 2* Internet domain functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: inet.c,v 1.28 2005/10/07 04:40:59 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8#include <string.h> 8#include <string.h>
@@ -16,14 +16,16 @@
16* Internal function prototypes. 16* Internal function prototypes.
17\*=========================================================================*/ 17\*=========================================================================*/
18static int inet_global_toip(lua_State *L); 18static int inet_global_toip(lua_State *L);
19static int inet_global_toip6(lua_State *L);
19static int inet_global_tohostname(lua_State *L); 20static int inet_global_tohostname(lua_State *L);
20static void inet_pushresolved(lua_State *L, struct hostent *hp); 21static void inet_pushresolved(lua_State *L, struct hostent *hp);
21static int inet_global_gethostname(lua_State *L); 22static int inet_global_gethostname(lua_State *L);
22 23
23/* DNS functions */ 24/* DNS functions */
24static luaL_reg func[] = { 25static luaL_reg func[] = {
25 { "toip", inet_global_toip }, 26 { "toip", inet_global_toip},
26 { "tohostname", inet_global_tohostname }, 27 { "toip6", inet_global_toip6},
28 { "tohostname", inet_global_tohostname},
27 { "gethostname", inet_global_gethostname}, 29 { "gethostname", inet_global_gethostname},
28 { NULL, NULL} 30 { NULL, NULL}
29}; 31};
@@ -95,6 +97,50 @@ static int inet_global_toip(lua_State *L)
95 return 2; 97 return 2;
96} 98}
97 99
100static int inet_global_toip6(lua_State *L)
101{
102 const char *hostname = luaL_checkstring(L, 1);
103 struct addrinfo *iterator = NULL, *resolved = NULL;
104 struct addrinfo hints;
105 int i = 1, ret = 0;
106 memset(&hints, 0, sizeof(hints));
107 hints.ai_socktype = SOCK_STREAM;
108 hints.ai_family = PF_UNSPEC;
109 ret = getaddrinfo(hostname, NULL, &hints, &resolved);
110 if (ret != 0) {
111 lua_pushnil(L);
112 lua_pushstring(L, "getaddrinfo returned error");
113 return 2;
114 }
115 lua_newtable(L);
116 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
117 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
118 getnameinfo(iterator->ai_addr, iterator->ai_addrlen, hbuf, sizeof(hbuf),
119 sbuf, 0, NI_NUMERICHOST);
120 lua_pushnumber(L, i);
121 lua_newtable(L);
122 switch (iterator->ai_family) {
123 case AF_INET:
124 lua_pushliteral(L, "family");
125 lua_pushliteral(L, "inet");
126 lua_settable(L, -3);
127 break;
128 case AF_INET6:
129 lua_pushliteral(L, "family");
130 lua_pushliteral(L, "inet6");
131 lua_settable(L, -3);
132 break;;
133 }
134 lua_pushliteral(L, "addr");
135 lua_pushstring(L, hbuf);
136 lua_settable(L, -3);
137 lua_settable(L, -3);
138 i++;
139 }
140 freeaddrinfo(resolved);
141 return 1;
142}
143
98 144
99/*-------------------------------------------------------------------------*\ 145/*-------------------------------------------------------------------------*\
100* Gets the host name 146* Gets the host name