diff --git a/src/inet.c b/src/inet.c index dfee700..52f7397 100644 --- a/src/inet.c +++ b/src/inet.c @@ -263,7 +263,6 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family) lua_pushliteral(L, "inet6"); return 3; } - return 2; } default: lua_pushnil(L); @@ -423,6 +422,21 @@ const char *inet_tryconnect(p_socket ps, const char *address, return err; } +/*-------------------------------------------------------------------------*\ +* Tries to accept a socket +\*-------------------------------------------------------------------------*/ +const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) +{ + socklen_t len; + t_sockaddr_storage addr; + if (family == PF_INET6) { + len = sizeof(struct sockaddr_in6); + } else { + len = sizeof(struct sockaddr_in); + } + return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm)); +} + /*-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/ diff --git a/src/inet.h b/src/inet.h index 2346734..4678ba6 100644 --- a/src/inet.h +++ b/src/inet.h @@ -30,6 +30,7 @@ const char *inet_tryconnect(p_socket ps, const char *address, const char *inet_trybind(p_socket ps, const char *address, const char *serv, struct addrinfo *bindhints); const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); +const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm); int inet_meth_getpeername(lua_State *L, p_socket ps, int family); int inet_meth_getsockname(lua_State *L, p_socket ps, int family); diff --git a/src/makefile b/src/makefile index 7e5f2e2..42b745e 100644 --- a/src/makefile +++ b/src/makefile @@ -32,7 +32,7 @@ LUAINC_macosx_base?=/opt/local/include LUAINC_macosx?=$(LUAINC_macosx_base)/lua$(LUAV) # FIXME default should this default to fink or to macports? # What happens when more than one Lua version is installed? -LUAPREFIX_macosx?=/opt/local/ +LUAPREFIX_macosx?=/opt/local # LUAINC_linux: # /usr/include/lua$(LUAV) diff --git a/src/tcp.c b/src/tcp.c index 5c85ae0..ea97320 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -186,9 +186,9 @@ static int meth_accept(lua_State *L) p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); p_timeout tm = timeout_markstart(&server->tm); t_socket sock; - int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); + const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm); /* if successful, push client socket */ - if (err == IO_DONE) { + if (err == NULL) { p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); auxiliar_setclass(L, "tcp{client}", -1); /* initialize structure fields */ @@ -203,7 +203,7 @@ static int meth_accept(lua_State *L) return 1; } else { lua_pushnil(L); - lua_pushstring(L, socket_strerror(err)); + lua_pushstring(L, err); return 2; } } diff --git a/src/usocket.c b/src/usocket.c index 7150996..096ecd0 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -181,11 +181,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { * Accept with timeout \*-------------------------------------------------------------------------*/ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { - SA daddr; - socklen_t dlen = sizeof(daddr); if (*ps == SOCKET_INVALID) return IO_CLOSED; - if (!addr) addr = &daddr; - if (!len) len = &dlen; for ( ;; ) { int err; if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; diff --git a/src/usocket.h b/src/usocket.h index 75bfe82..8b3241b 100644 --- a/src/usocket.h +++ b/src/usocket.h @@ -36,6 +36,7 @@ typedef int t_socket; typedef t_socket *p_socket; +typedef struct sockaddr_storage t_sockaddr_storage; #define SOCKET_INVALID (-1) diff --git a/src/wsocket.c b/src/wsocket.c index 36c10ff..5b184cf 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -169,11 +169,7 @@ int socket_listen(p_socket ps, int backlog) { \*-------------------------------------------------------------------------*/ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { - SA daddr; - socklen_t dlen = sizeof(daddr); if (*ps == SOCKET_INVALID) return IO_CLOSED; - if (!addr) addr = &daddr; - if (!len) len = &dlen; for ( ;; ) { int err; /* try to get client socket */ diff --git a/src/wsocket.h b/src/wsocket.h index 8e0f114..0783b00 100644 --- a/src/wsocket.h +++ b/src/wsocket.h @@ -12,6 +12,7 @@ #include typedef int socklen_t; +typedef SOCKADDR_STORAGE t_sockaddr_storage; typedef SOCKET t_socket; typedef t_socket *p_socket;