diff options
author | Diego Nehab <diego@impa.br> | 2013-05-27 21:05:48 +0800 |
---|---|---|
committer | Diego Nehab <diego@impa.br> | 2013-05-27 21:05:48 +0800 |
commit | 834a3cf520637df0af9967e1f8ad9e40837771cb (patch) | |
tree | 51727c3f53b3e550467ee60eca54a2a202a828f3 /src | |
parent | 5e0b56b8d30c574c6519495ba3fcdd245d54f5a0 (diff) | |
download | luasocket-834a3cf520637df0af9967e1f8ad9e40837771cb.tar.gz luasocket-834a3cf520637df0af9967e1f8ad9e40837771cb.tar.bz2 luasocket-834a3cf520637df0af9967e1f8ad9e40837771cb.zip |
Simplifying getaddrinfo treatment.
Diffstat (limited to 'src')
-rw-r--r-- | src/inet.c | 50 |
1 files changed, 18 insertions, 32 deletions
@@ -79,24 +79,22 @@ static int inet_global_tohostname(lua_State *L) { | |||
79 | } | 79 | } |
80 | 80 | ||
81 | static int inet_global_getnameinfo(lua_State *L) { | 81 | static int inet_global_getnameinfo(lua_State *L) { |
82 | char hbuf[NI_MAXHOST]; | ||
83 | char sbuf[NI_MAXSERV]; | ||
82 | int i, ret; | 84 | int i, ret; |
83 | char host[1024]; | ||
84 | char serv[32]; | ||
85 | struct addrinfo hints; | 85 | struct addrinfo hints; |
86 | struct addrinfo *resolved, *iter; | 86 | struct addrinfo *resolved, *iter; |
87 | const char *node = luaL_optstring(L, 1, NULL); | 87 | const char *host = luaL_optstring(L, 1, NULL); |
88 | const char *service = luaL_optstring(L, 2, NULL); | 88 | const char *serv = luaL_optstring(L, 2, NULL); |
89 | 89 | ||
90 | if (!(node || service)) | 90 | if (!(host || serv)) |
91 | luaL_error(L, "You have to specify a hostname, a service, or both"); | 91 | luaL_error(L, "host and serv cannot be both nil"); |
92 | 92 | ||
93 | memset(&hints, 0, sizeof(hints)); | 93 | memset(&hints, 0, sizeof(hints)); |
94 | hints.ai_socktype = SOCK_STREAM; | 94 | hints.ai_socktype = SOCK_STREAM; |
95 | hints.ai_family = PF_UNSPEC; | 95 | hints.ai_family = PF_UNSPEC; |
96 | 96 | ||
97 | /* getaddrinfo must get a node and a service argument */ | 97 | ret = getaddrinfo(host, serv, &hints, &resolved); |
98 | ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", | ||
99 | &hints, &resolved); | ||
100 | if (ret != 0) { | 98 | if (ret != 0) { |
101 | lua_pushnil(L); | 99 | lua_pushnil(L); |
102 | lua_pushstring(L, socket_gaistrerror(ret)); | 100 | lua_pushstring(L, socket_gaistrerror(ret)); |
@@ -105,19 +103,19 @@ static int inet_global_getnameinfo(lua_State *L) { | |||
105 | 103 | ||
106 | lua_newtable(L); | 104 | lua_newtable(L); |
107 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { | 105 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { |
108 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, | 106 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, |
109 | node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); | 107 | hbuf, host? (socklen_t) sizeof(hbuf): 0, |
110 | 108 | sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0); | |
111 | if (node) { | 109 | if (host) { |
112 | lua_pushnumber(L, i); | 110 | lua_pushnumber(L, i); |
113 | lua_pushstring(L, host); | 111 | lua_pushstring(L, hbuf); |
114 | lua_settable(L, -3); | 112 | lua_settable(L, -3); |
115 | } | 113 | } |
116 | } | 114 | } |
117 | freeaddrinfo(resolved); | 115 | freeaddrinfo(resolved); |
118 | 116 | ||
119 | if (service) { | 117 | if (serv) { |
120 | lua_pushstring(L, serv); | 118 | lua_pushstring(L, sbuf); |
121 | return 2; | 119 | return 2; |
122 | } else { | 120 | } else { |
123 | return 1; | 121 | return 1; |
@@ -176,20 +174,10 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
176 | } | 174 | } |
177 | lua_newtable(L); | 175 | lua_newtable(L); |
178 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 176 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
179 | char hbuf[NI_MAXHOST] | 177 | char hbuf[NI_MAXHOST]; |
180 | #ifndef _WINDOWS | 178 | ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, |
181 | ,sbuf[NI_MAXSERV] | 179 | hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST); |
182 | #endif | 180 | if (ret){ |
183 | ; | ||
184 | ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, | ||
185 | (socklen_t) sizeof(hbuf), | ||
186 | #ifdef _WINDOWS | ||
187 | NULL, 0, | ||
188 | #else | ||
189 | sbuf, 0, | ||
190 | #endif | ||
191 | NI_NUMERICHOST); | ||
192 | if(ret){ | ||
193 | lua_pushnil(L); | 181 | lua_pushnil(L); |
194 | lua_pushstring(L, socket_gaistrerror(ret)); | 182 | lua_pushstring(L, socket_gaistrerror(ret)); |
195 | return 2; | 183 | return 2; |
@@ -218,7 +206,6 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
218 | return 1; | 206 | return 1; |
219 | } | 207 | } |
220 | 208 | ||
221 | |||
222 | /*-------------------------------------------------------------------------*\ | 209 | /*-------------------------------------------------------------------------*\ |
223 | * Gets the host name | 210 | * Gets the host name |
224 | \*-------------------------------------------------------------------------*/ | 211 | \*-------------------------------------------------------------------------*/ |
@@ -237,7 +224,6 @@ static int inet_global_gethostname(lua_State *L) | |||
237 | } | 224 | } |
238 | 225 | ||
239 | 226 | ||
240 | |||
241 | /*=========================================================================*\ | 227 | /*=========================================================================*\ |
242 | * Lua methods | 228 | * Lua methods |
243 | \*=========================================================================*/ | 229 | \*=========================================================================*/ |