aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@impa.br>2012-12-11 16:35:27 -0200
committerDiego Nehab <diego@impa.br>2012-12-11 16:35:27 -0200
commit618ce43ee3950b80aca1fde0a5b12e6e13627f1b (patch)
treefc97079e13e071593086a3d6aa5b090b96c744a3 /src
parent66670c354146feb8c9603f10682fabcba44a05a9 (diff)
downloadluasocket-618ce43ee3950b80aca1fde0a5b12e6e13627f1b.tar.gz
luasocket-618ce43ee3950b80aca1fde0a5b12e6e13627f1b.tar.bz2
luasocket-618ce43ee3950b80aca1fde0a5b12e6e13627f1b.zip
Fix socket_accept usage to depend on family.
Diffstat (limited to 'src')
-rw-r--r--src/inet.c16
-rw-r--r--src/inet.h1
-rw-r--r--src/makefile2
-rw-r--r--src/tcp.c6
-rw-r--r--src/usocket.c4
-rw-r--r--src/usocket.h1
-rw-r--r--src/wsocket.c4
-rw-r--r--src/wsocket.h1
8 files changed, 22 insertions, 13 deletions
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)
263 lua_pushliteral(L, "inet6"); 263 lua_pushliteral(L, "inet6");
264 return 3; 264 return 3;
265 } 265 }
266 return 2;
267 } 266 }
268 default: 267 default:
269 lua_pushnil(L); 268 lua_pushnil(L);
@@ -424,6 +423,21 @@ const char *inet_tryconnect(p_socket ps, const char *address,
424} 423}
425 424
426/*-------------------------------------------------------------------------*\ 425/*-------------------------------------------------------------------------*\
426* Tries to accept a socket
427\*-------------------------------------------------------------------------*/
428const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm)
429{
430 socklen_t len;
431 t_sockaddr_storage addr;
432 if (family == PF_INET6) {
433 len = sizeof(struct sockaddr_in6);
434 } else {
435 len = sizeof(struct sockaddr_in);
436 }
437 return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm));
438}
439
440/*-------------------------------------------------------------------------*\
427* Tries to bind socket to (address, port) 441* Tries to bind socket to (address, port)
428\*-------------------------------------------------------------------------*/ 442\*-------------------------------------------------------------------------*/
429const char *inet_trybind(p_socket ps, const char *address, const char *serv, 443const char *inet_trybind(p_socket ps, const char *address, const char *serv,
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,
30const char *inet_trybind(p_socket ps, const char *address, const char *serv, 30const char *inet_trybind(p_socket ps, const char *address, const char *serv,
31 struct addrinfo *bindhints); 31 struct addrinfo *bindhints);
32const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); 32const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
33const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);
33 34
34int inet_meth_getpeername(lua_State *L, p_socket ps, int family); 35int inet_meth_getpeername(lua_State *L, p_socket ps, int family);
35int inet_meth_getsockname(lua_State *L, p_socket ps, int family); 36int 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
32LUAINC_macosx?=$(LUAINC_macosx_base)/lua$(LUAV) 32LUAINC_macosx?=$(LUAINC_macosx_base)/lua$(LUAV)
33# FIXME default should this default to fink or to macports? 33# FIXME default should this default to fink or to macports?
34# What happens when more than one Lua version is installed? 34# What happens when more than one Lua version is installed?
35LUAPREFIX_macosx?=/opt/local/ 35LUAPREFIX_macosx?=/opt/local
36 36
37# LUAINC_linux: 37# LUAINC_linux:
38# /usr/include/lua$(LUAV) 38# /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)
186 p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); 186 p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1);
187 p_timeout tm = timeout_markstart(&server->tm); 187 p_timeout tm = timeout_markstart(&server->tm);
188 t_socket sock; 188 t_socket sock;
189 int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); 189 const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm);
190 /* if successful, push client socket */ 190 /* if successful, push client socket */
191 if (err == IO_DONE) { 191 if (err == NULL) {
192 p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); 192 p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
193 auxiliar_setclass(L, "tcp{client}", -1); 193 auxiliar_setclass(L, "tcp{client}", -1);
194 /* initialize structure fields */ 194 /* initialize structure fields */
@@ -203,7 +203,7 @@ static int meth_accept(lua_State *L)
203 return 1; 203 return 1;
204 } else { 204 } else {
205 lua_pushnil(L); 205 lua_pushnil(L);
206 lua_pushstring(L, socket_strerror(err)); 206 lua_pushstring(L, err);
207 return 2; 207 return 2;
208 } 208 }
209} 209}
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) {
181* Accept with timeout 181* Accept with timeout
182\*-------------------------------------------------------------------------*/ 182\*-------------------------------------------------------------------------*/
183int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { 183int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
184 SA daddr;
185 socklen_t dlen = sizeof(daddr);
186 if (*ps == SOCKET_INVALID) return IO_CLOSED; 184 if (*ps == SOCKET_INVALID) return IO_CLOSED;
187 if (!addr) addr = &daddr;
188 if (!len) len = &dlen;
189 for ( ;; ) { 185 for ( ;; ) {
190 int err; 186 int err;
191 if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; 187 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 @@
36 36
37typedef int t_socket; 37typedef int t_socket;
38typedef t_socket *p_socket; 38typedef t_socket *p_socket;
39typedef struct sockaddr_storage t_sockaddr_storage;
39 40
40#define SOCKET_INVALID (-1) 41#define SOCKET_INVALID (-1)
41 42
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) {
169\*-------------------------------------------------------------------------*/ 169\*-------------------------------------------------------------------------*/
170int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, 170int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
171 p_timeout tm) { 171 p_timeout tm) {
172 SA daddr;
173 socklen_t dlen = sizeof(daddr);
174 if (*ps == SOCKET_INVALID) return IO_CLOSED; 172 if (*ps == SOCKET_INVALID) return IO_CLOSED;
175 if (!addr) addr = &daddr;
176 if (!len) len = &dlen;
177 for ( ;; ) { 173 for ( ;; ) {
178 int err; 174 int err;
179 /* try to get client socket */ 175 /* 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 @@
12#include <ws2tcpip.h> 12#include <ws2tcpip.h>
13 13
14typedef int socklen_t; 14typedef int socklen_t;
15typedef SOCKADDR_STORAGE t_sockaddr_storage;
15typedef SOCKET t_socket; 16typedef SOCKET t_socket;
16typedef t_socket *p_socket; 17typedef t_socket *p_socket;
17 18