aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@impa.br>2013-05-30 16:20:34 +0800
committerDiego Nehab <diego@impa.br>2013-05-30 16:20:34 +0800
commita233e27865d96566a6cb13960d08605ce34d9f0d (patch)
tree6ef3219bec0c29bbb14ce92b5c2f4dc46b42dbc6 /src/inet.c
parent5341131cd07bf4f66ce242980b5f3cfbbf45ea12 (diff)
downloadluasocket-a233e27865d96566a6cb13960d08605ce34d9f0d.tar.gz
luasocket-a233e27865d96566a6cb13960d08605ce34d9f0d.tar.bz2
luasocket-a233e27865d96566a6cb13960d08605ce34d9f0d.zip
Leaving if in src/ but out of build for now.
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c131
1 files changed, 55 insertions, 76 deletions
diff --git a/src/inet.c b/src/inet.c
index 35bc438..50b3e5c 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -3,6 +3,7 @@
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h>
6#include <string.h> 7#include <string.h>
7 8
8#include "lua.h" 9#include "lua.h"
@@ -227,7 +228,6 @@ static int inet_global_gethostname(lua_State *L)
227 } 228 }
228} 229}
229 230
230
231/*=========================================================================*\ 231/*=========================================================================*\
232* Lua methods 232* Lua methods
233\*=========================================================================*/ 233\*=========================================================================*/
@@ -236,44 +236,33 @@ static int inet_global_gethostname(lua_State *L)
236\*-------------------------------------------------------------------------*/ 236\*-------------------------------------------------------------------------*/
237int inet_meth_getpeername(lua_State *L, p_socket ps, int family) 237int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
238{ 238{
239 switch (family) { 239 int err;
240 case PF_INET: { 240 struct sockaddr_storage peer;
241 struct sockaddr_in peer; 241 socklen_t peer_len = sizeof(peer);
242 socklen_t peer_len = sizeof(peer); 242 char name[INET6_ADDRSTRLEN];
243 char name[INET_ADDRSTRLEN]; 243 char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
244 if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { 244 if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) {
245 lua_pushnil(L); 245 lua_pushnil(L);
246 lua_pushstring(L, socket_strerror(errno)); 246 lua_pushstring(L, socket_strerror(errno));
247 return 2; 247 return 2;
248 } else { 248 }
249 inet_ntop(family, &peer.sin_addr, name, sizeof(name)); 249 if ((err = getnameinfo((struct sockaddr *) &peer, peer_len,
250 lua_pushstring(L, name); 250 name, INET6_ADDRSTRLEN,
251 lua_pushnumber(L, ntohs(peer.sin_port)); 251 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV))) {
252 lua_pushliteral(L, "inet"); 252 lua_pushnil(L);
253 return 3; 253 lua_pushstring(L, gai_strerror(err));
254 } 254 return 2;
255 } 255 }
256 case PF_INET6: { 256 lua_pushstring(L, name);
257 struct sockaddr_in6 peer; 257 lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
258 socklen_t peer_len = sizeof(peer); 258 if (family == PF_INET) {
259 char name[INET6_ADDRSTRLEN]; 259 lua_pushliteral(L, "inet");
260 if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { 260 } else if (family == PF_INET6) {
261 lua_pushnil(L); 261 lua_pushliteral(L, "inet6");
262 lua_pushstring(L, socket_strerror(errno)); 262 } else {
263 return 2; 263 lua_pushliteral(L, "uknown family");
264 } else {
265 inet_ntop(family, &peer.sin6_addr, name, sizeof(name));
266 lua_pushstring(L, name);
267 lua_pushnumber(L, ntohs(peer.sin6_port));
268 lua_pushliteral(L, "inet6");
269 return 3;
270 }
271 }
272 default:
273 lua_pushnil(L);
274 lua_pushfstring(L, "unknown family %d", family);
275 return 2;
276 } 264 }
265 return 3;
277} 266}
278 267
279/*-------------------------------------------------------------------------*\ 268/*-------------------------------------------------------------------------*\
@@ -281,44 +270,33 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
281\*-------------------------------------------------------------------------*/ 270\*-------------------------------------------------------------------------*/
282int inet_meth_getsockname(lua_State *L, p_socket ps, int family) 271int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
283{ 272{
284 switch (family) { 273 int err;
285 case PF_INET: { 274 struct sockaddr_storage peer;
286 struct sockaddr_in local; 275 socklen_t peer_len = sizeof(peer);
287 socklen_t local_len = sizeof(local); 276 char name[INET6_ADDRSTRLEN];
288 char name[INET_ADDRSTRLEN]; 277 char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
289 if (getsockname(*ps, (SA *) &local, &local_len) < 0) { 278 if (getsockname(*ps, (SA *) &peer, &peer_len) < 0) {
290 lua_pushnil(L); 279 lua_pushnil(L);
291 lua_pushstring(L, socket_strerror(errno)); 280 lua_pushstring(L, socket_strerror(errno));
292 return 2; 281 return 2;
293 } else { 282 }
294 inet_ntop(family, &local.sin_addr, name, sizeof(name)); 283 if ((err=getnameinfo((struct sockaddr *)&peer, peer_len,
295 lua_pushstring(L, name); 284 name, INET6_ADDRSTRLEN,
296 lua_pushnumber(L, ntohs(local.sin_port)); 285 port, 6, NI_NUMERICHOST | NI_NUMERICSERV))) {
297 lua_pushliteral(L, "inet"); 286 lua_pushnil(L);
298 return 3; 287 lua_pushstring(L, gai_strerror(err));
299 } 288 return 2;
300 } 289 }
301 case PF_INET6: { 290 lua_pushstring(L, name);
302 struct sockaddr_in6 local; 291 lua_pushstring(L, port);
303 socklen_t local_len = sizeof(local); 292 if (family == PF_INET) {
304 char name[INET6_ADDRSTRLEN]; 293 lua_pushliteral(L, "inet");
305 if (getsockname(*ps, (SA *) &local, &local_len) < 0) { 294 } else if (family == PF_INET6) {
306 lua_pushnil(L); 295 lua_pushliteral(L, "inet6");
307 lua_pushstring(L, socket_strerror(errno)); 296 } else {
308 return 2; 297 lua_pushliteral(L, "uknown family");
309 } else {
310 inet_ntop(family, &local.sin6_addr, name, sizeof(name));
311 lua_pushstring(L, name);
312 lua_pushnumber(L, ntohs(local.sin6_port));
313 lua_pushliteral(L, "inet6");
314 return 3;
315 }
316 }
317 default:
318 lua_pushnil(L);
319 lua_pushfstring(L, "unknown family %d", family);
320 return 2;
321 } 298 }
299 return 3;
322} 300}
323 301
324/*=========================================================================*\ 302/*=========================================================================*\
@@ -456,7 +434,8 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
456 } else { 434 } else {
457 len = sizeof(struct sockaddr_in); 435 len = sizeof(struct sockaddr_in);
458 } 436 }
459 return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm)); 437 return socket_strerror(socket_accept(server, client, (SA *) &addr,
438 &len, tm));
460} 439}
461 440
462/*-------------------------------------------------------------------------*\ 441/*-------------------------------------------------------------------------*\