aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2012-05-08 11:48:54 -0700
committerDiego Nehab <diego.nehab@gmail.com>2012-05-08 11:48:54 -0700
commitee7c53c3e58350c51e5c622e1b9ff8acf02c0e59 (patch)
tree062c4959756a6a51978a40fee36777be82487419
parent04be61f88df2277fd0d2010b2deb851d9b081923 (diff)
parent0c3e067af17a21338e8aa3240b61d40934373939 (diff)
downloadluasocket-ee7c53c3e58350c51e5c622e1b9ff8acf02c0e59.tar.gz
luasocket-ee7c53c3e58350c51e5c622e1b9ff8acf02c0e59.tar.bz2
luasocket-ee7c53c3e58350c51e5c622e1b9ff8acf02c0e59.zip
Merge pull request #6 from Florob/getnameinfo
Add a getnameinfo() wrapper
-rw-r--r--src/inet.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/inet.c b/src/inet.c
index 5823c36..55e89d7 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -16,6 +16,7 @@
16static int inet_global_toip(lua_State *L); 16static int inet_global_toip(lua_State *L);
17static int inet_global_getaddrinfo(lua_State *L); 17static int inet_global_getaddrinfo(lua_State *L);
18static int inet_global_tohostname(lua_State *L); 18static int inet_global_tohostname(lua_State *L);
19static int inet_global_getnameinfo(lua_State *L);
19static void inet_pushresolved(lua_State *L, struct hostent *hp); 20static void inet_pushresolved(lua_State *L, struct hostent *hp);
20static int inet_global_gethostname(lua_State *L); 21static int inet_global_gethostname(lua_State *L);
21 22
@@ -24,6 +25,7 @@ static luaL_Reg func[] = {
24 { "toip", inet_global_toip}, 25 { "toip", inet_global_toip},
25 { "getaddrinfo", inet_global_getaddrinfo}, 26 { "getaddrinfo", inet_global_getaddrinfo},
26 { "tohostname", inet_global_tohostname}, 27 { "tohostname", inet_global_tohostname},
28 { "getnameinfo", inet_global_getnameinfo},
27 { "gethostname", inet_global_gethostname}, 29 { "gethostname", inet_global_gethostname},
28 { NULL, NULL} 30 { NULL, NULL}
29}; 31};
@@ -76,6 +78,52 @@ static int inet_global_tohostname(lua_State *L) {
76 return 2; 78 return 2;
77} 79}
78 80
81static int inet_global_getnameinfo(lua_State *L) {
82 int i, ret;
83 char host[1024];
84 char serv[32];
85 struct addrinfo hints;
86 struct addrinfo *resolved, *iter;
87 const char *node = luaL_optstring(L, 1, NULL);
88 const char *service = luaL_optstring(L, 2, NULL);
89
90 if (!(node || service))
91 luaL_error(L, "You have to specify a hostname, a service, or both");
92
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_socktype = SOCK_STREAM;
95 hints.ai_family = PF_UNSPEC;
96
97 /* getaddrinfo must get a node and a service argument */
98 ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7",
99 &hints, &resolved);
100 if (ret != 0) {
101 lua_pushnil(L);
102 lua_pushstring(L, socket_gaistrerror(ret));
103 return 2;
104 }
105
106 lua_newtable(L);
107 for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) {
108 getnameinfo(iter->ai_addr, iter->ai_addrlen, host,
109 node ? sizeof(host) : 0, serv, service ? sizeof(serv) : 0, 0);
110
111 if (node) {
112 lua_pushnumber(L, i);
113 lua_pushstring(L, host);
114 lua_settable(L, -3);
115 }
116 }
117 freeaddrinfo(resolved);
118
119 if (service) {
120 lua_pushstring(L, serv);
121 return 2;
122 } else {
123 return 1;
124 }
125}
126
79/*-------------------------------------------------------------------------*\ 127/*-------------------------------------------------------------------------*\
80* Returns all information provided by the resolver given a host name 128* Returns all information provided by the resolver given a host name
81* or ip address 129* or ip address