From 156669c28bc62bfddbd5625c4bb4c1f8da94802b Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Thu, 10 May 2012 14:14:22 -0700 Subject: socket.connect now implemented in the C core This avoid socket.lua duplicating the iteration over the results of getaddrinfo(). Some problems with the C implementation not initializing sockets or the luasocket family have also been fixed, and error reporting made more robust. --- src/tcp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/tcp.c') diff --git a/src/tcp.c b/src/tcp.c index 94148c5..3a7f527 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -18,7 +18,7 @@ \*=========================================================================*/ static int global_create(lua_State *L); static int global_create6(lua_State *L); -static int global_connect6(lua_State *L); +static int global_connect(lua_State *L); static int meth_connect(lua_State *L); static int meth_listen(lua_State *L); static int meth_getfamily(lua_State *L); @@ -89,7 +89,7 @@ static t_opt optset[] = { static luaL_Reg func[] = { {"tcp", global_create}, {"tcp6", global_create6}, - {"connect6", global_connect6}, + {"connect", global_connect}, {NULL, NULL} }; @@ -408,6 +408,7 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, freeaddrinfo(resolved); return err; } + tcp->family = iterator->ai_family; /* all sockets initially non-blocking */ socket_setnonblocking(&tcp->sock); } @@ -424,11 +425,12 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, return err; } -static int global_connect6(lua_State *L) { +static int global_connect(lua_State *L) { const char *remoteaddr = luaL_checkstring(L, 1); const char *remoteserv = luaL_checkstring(L, 2); const char *localaddr = luaL_optstring(L, 3, NULL); const char *localserv = luaL_optstring(L, 4, "0"); + int family = inet_optfamily(L, 5, "unspec"); p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); struct addrinfo bindhints, connecthints; const char *err = NULL; @@ -441,7 +443,7 @@ static int global_connect6(lua_State *L) { /* allow user to pick local address and port */ memset(&bindhints, 0, sizeof(bindhints)); bindhints.ai_socktype = SOCK_STREAM; - bindhints.ai_family = PF_UNSPEC; + bindhints.ai_family = family; bindhints.ai_flags = AI_PASSIVE; if (localaddr) { err = inet_trybind(&tcp->sock, localaddr, localserv, &bindhints); @@ -450,6 +452,7 @@ static int global_connect6(lua_State *L) { lua_pushstring(L, err); return 2; } + tcp->family = bindhints.ai_family; } /* try to connect to remote address and port */ memset(&connecthints, 0, sizeof(connecthints)); -- cgit v1.2.3-55-g6feb