aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c132
1 files changed, 56 insertions, 76 deletions
diff --git a/src/inet.c b/src/inet.c
index 35bc438..25482a3 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,34 @@ 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 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 if (err) {
253 return 3; 253 lua_pushnil(L);
254 } 254 lua_pushstring(L, gai_strerror(err));
255 } 255 return 2;
256 case PF_INET6: { 256 }
257 struct sockaddr_in6 peer; 257 lua_pushstring(L, name);
258 socklen_t peer_len = sizeof(peer); 258 lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
259 char name[INET6_ADDRSTRLEN]; 259 if (family == PF_INET) {
260 if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { 260 lua_pushliteral(L, "inet");
261 lua_pushnil(L); 261 } else if (family == PF_INET6) {
262 lua_pushstring(L, socket_strerror(errno)); 262 lua_pushliteral(L, "inet6");
263 return 2; 263 } else {
264 } else { 264 lua_pushliteral(L, "uknown family");
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 } 265 }
266 return 3;
277} 267}
278 268
279/*-------------------------------------------------------------------------*\ 269/*-------------------------------------------------------------------------*\
@@ -281,44 +271,33 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
281\*-------------------------------------------------------------------------*/ 271\*-------------------------------------------------------------------------*/
282int inet_meth_getsockname(lua_State *L, p_socket ps, int family) 272int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
283{ 273{
284 switch (family) { 274 int err;
285 case PF_INET: { 275 struct sockaddr_storage peer;
286 struct sockaddr_in local; 276 socklen_t peer_len = sizeof(peer);
287 socklen_t local_len = sizeof(local); 277 char name[INET6_ADDRSTRLEN];
288 char name[INET_ADDRSTRLEN]; 278 char port[6]; /* 65535 = 5 bytes + 0 to terminate it */
289 if (getsockname(*ps, (SA *) &local, &local_len) < 0) { 279 if (getsockname(*ps, (SA *) &peer, &peer_len) < 0) {
290 lua_pushnil(L); 280 lua_pushnil(L);
291 lua_pushstring(L, socket_strerror(errno)); 281 lua_pushstring(L, socket_strerror(errno));
292 return 2; 282 return 2;
293 } else { 283 }
294 inet_ntop(family, &local.sin_addr, name, sizeof(name)); 284 err=getnameinfo((struct sockaddr *)&peer, peer_len,
295 lua_pushstring(L, name); 285 name, INET6_ADDRSTRLEN, port, 6, NI_NUMERICHOST | NI_NUMERICSERV);
296 lua_pushnumber(L, ntohs(local.sin_port)); 286 if (err) {
297 lua_pushliteral(L, "inet"); 287 lua_pushnil(L);
298 return 3; 288 lua_pushstring(L, gai_strerror(err));
299 } 289 return 2;
300 } 290 }
301 case PF_INET6: { 291 lua_pushstring(L, name);
302 struct sockaddr_in6 local; 292 lua_pushstring(L, port);
303 socklen_t local_len = sizeof(local); 293 if (family == PF_INET) {
304 char name[INET6_ADDRSTRLEN]; 294 lua_pushliteral(L, "inet");
305 if (getsockname(*ps, (SA *) &local, &local_len) < 0) { 295 } else if (family == PF_INET6) {
306 lua_pushnil(L); 296 lua_pushliteral(L, "inet6");
307 lua_pushstring(L, socket_strerror(errno)); 297 } else {
308 return 2; 298 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 } 299 }
300 return 3;
322} 301}
323 302
324/*=========================================================================*\ 303/*=========================================================================*\
@@ -456,7 +435,8 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
456 } else { 435 } else {
457 len = sizeof(struct sockaddr_in); 436 len = sizeof(struct sockaddr_in);
458 } 437 }
459 return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm)); 438 return socket_strerror(socket_accept(server, client, (SA *) &addr,
439 &len, tm));
460} 440}
461 441
462/*-------------------------------------------------------------------------*\ 442/*-------------------------------------------------------------------------*\