aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcp.c')
-rw-r--r--src/tcp.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/tcp.c b/src/tcp.c
index 3146467..872b1c6 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -1,8 +1,6 @@
1/*=========================================================================*\ 1/*=========================================================================*\
2* TCP object 2* TCP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4*
5* RCS ID: $Id: tcp.c,v 1.42 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 4\*=========================================================================*/
7#include <string.h> 5#include <string.h>
8 6
@@ -23,6 +21,7 @@ static int global_create6(lua_State *L);
23static int global_connect6(lua_State *L); 21static int global_connect6(lua_State *L);
24static int meth_connect(lua_State *L); 22static int meth_connect(lua_State *L);
25static int meth_listen(lua_State *L); 23static int meth_listen(lua_State *L);
24static int meth_getfamily(lua_State *L);
26static int meth_bind(lua_State *L); 25static int meth_bind(lua_State *L);
27static int meth_send(lua_State *L); 26static int meth_send(lua_State *L);
28static int meth_getstats(lua_State *L); 27static int meth_getstats(lua_State *L);
@@ -49,6 +48,7 @@ static luaL_Reg tcp_methods[] = {
49 {"close", meth_close}, 48 {"close", meth_close},
50 {"connect", meth_connect}, 49 {"connect", meth_connect},
51 {"dirty", meth_dirty}, 50 {"dirty", meth_dirty},
51 {"getfamily", meth_getfamily},
52 {"getfd", meth_getfd}, 52 {"getfd", meth_getfd},
53 {"getoption", meth_getoption}, 53 {"getoption", meth_getoption},
54 {"getpeername", meth_getpeername}, 54 {"getpeername", meth_getpeername},
@@ -218,7 +218,7 @@ static int meth_bind(lua_State *L)
218 struct addrinfo bindhints; 218 struct addrinfo bindhints;
219 memset(&bindhints, 0, sizeof(bindhints)); 219 memset(&bindhints, 0, sizeof(bindhints));
220 bindhints.ai_socktype = SOCK_STREAM; 220 bindhints.ai_socktype = SOCK_STREAM;
221 bindhints.ai_family = tcp->domain; 221 bindhints.ai_family = tcp->family;
222 bindhints.ai_flags = AI_PASSIVE; 222 bindhints.ai_flags = AI_PASSIVE;
223 err = inet_trybind(&tcp->sock, address, port, &bindhints); 223 err = inet_trybind(&tcp->sock, address, port, &bindhints);
224 if (err) { 224 if (err) {
@@ -243,7 +243,7 @@ static int meth_connect(lua_State *L)
243 memset(&connecthints, 0, sizeof(connecthints)); 243 memset(&connecthints, 0, sizeof(connecthints));
244 connecthints.ai_socktype = SOCK_STREAM; 244 connecthints.ai_socktype = SOCK_STREAM;
245 /* make sure we try to connect only to the same family */ 245 /* make sure we try to connect only to the same family */
246 connecthints.ai_family = tcp->domain; 246 connecthints.ai_family = tcp->family;
247 timeout_markstart(&tcp->tm); 247 timeout_markstart(&tcp->tm);
248 err = inet_tryconnect(&tcp->sock, address, port, 248 err = inet_tryconnect(&tcp->sock, address, port,
249 &tcp->tm, &connecthints); 249 &tcp->tm, &connecthints);
@@ -270,6 +270,21 @@ static int meth_close(lua_State *L)
270} 270}
271 271
272/*-------------------------------------------------------------------------*\ 272/*-------------------------------------------------------------------------*\
273* Returns family as string
274\*-------------------------------------------------------------------------*/
275static int meth_getfamily(lua_State *L)
276{
277 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
278 if (tcp->family == PF_INET6) {
279 lua_pushliteral(L, "inet6");
280 return 1;
281 } else {
282 lua_pushliteral(L, "inet4");
283 return 1;
284 }
285}
286
287/*-------------------------------------------------------------------------*\
273* Puts the sockt in listen mode 288* Puts the sockt in listen mode
274\*-------------------------------------------------------------------------*/ 289\*-------------------------------------------------------------------------*/
275static int meth_listen(lua_State *L) 290static int meth_listen(lua_State *L)
@@ -346,9 +361,9 @@ static int meth_settimeout(lua_State *L)
346/*-------------------------------------------------------------------------*\ 361/*-------------------------------------------------------------------------*\
347* Creates a master tcp object 362* Creates a master tcp object
348\*-------------------------------------------------------------------------*/ 363\*-------------------------------------------------------------------------*/
349static int tcp_create(lua_State *L, int domain) { 364static int tcp_create(lua_State *L, int family) {
350 t_socket sock; 365 t_socket sock;
351 const char *err = inet_trycreate(&sock, domain, SOCK_STREAM); 366 const char *err = inet_trycreate(&sock, family, SOCK_STREAM);
352 /* try to allocate a system socket */ 367 /* try to allocate a system socket */
353 if (!err) { 368 if (!err) {
354 /* allocate tcp object */ 369 /* allocate tcp object */
@@ -357,7 +372,7 @@ static int tcp_create(lua_State *L, int domain) {
357 auxiliar_setclass(L, "tcp{master}", -1); 372 auxiliar_setclass(L, "tcp{master}", -1);
358 /* initialize remaining structure fields */ 373 /* initialize remaining structure fields */
359 socket_setnonblocking(&sock); 374 socket_setnonblocking(&sock);
360 if (domain == PF_INET6) { 375 if (family == PF_INET6) {
361 int yes = 1; 376 int yes = 1;
362 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 377 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
363 (void *)&yes, sizeof(yes)); 378 (void *)&yes, sizeof(yes));
@@ -367,7 +382,7 @@ static int tcp_create(lua_State *L, int domain) {
367 (p_error) socket_ioerror, &tcp->sock); 382 (p_error) socket_ioerror, &tcp->sock);
368 timeout_init(&tcp->tm, -1, -1); 383 timeout_init(&tcp->tm, -1, -1);
369 buffer_init(&tcp->buf, &tcp->io, &tcp->tm); 384 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
370 tcp->domain = domain; 385 tcp->family = family;
371 return 1; 386 return 1;
372 } else { 387 } else {
373 lua_pushnil(L); 388 lua_pushnil(L);