diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2012-04-23 00:18:45 +0800 |
---|---|---|
committer | Diego Nehab <diego.nehab@gmail.com> | 2012-04-23 00:18:45 +0800 |
commit | f960b3872a668ed1b53bd50c5b6a708367332f3c (patch) | |
tree | e82a1b113ed40d70afae36d230b6a6048e1d9fab /src/tcp.c | |
parent | f37e0260261f7691246429d227cf7124c291e8b1 (diff) | |
download | luasocket-f960b3872a668ed1b53bd50c5b6a708367332f3c.tar.gz luasocket-f960b3872a668ed1b53bd50c5b6a708367332f3c.tar.bz2 luasocket-f960b3872a668ed1b53bd50c5b6a708367332f3c.zip |
Making progress toward a release
Documented headers.lua
Update copyright date everywhere
Remove RCSID from files
Move version back to 2.1 rather than 2.1.1
Fixed url package to support ipv6 hosts
Changed "domain" to "family" in tcp and udp structures
Implemented getfamily methods
Diffstat (limited to 'src/tcp.c')
-rw-r--r-- | src/tcp.c | 31 |
1 files changed, 23 insertions, 8 deletions
@@ -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); | |||
23 | static int global_connect6(lua_State *L); | 21 | static int global_connect6(lua_State *L); |
24 | static int meth_connect(lua_State *L); | 22 | static int meth_connect(lua_State *L); |
25 | static int meth_listen(lua_State *L); | 23 | static int meth_listen(lua_State *L); |
24 | static int meth_getfamily(lua_State *L); | ||
26 | static int meth_bind(lua_State *L); | 25 | static int meth_bind(lua_State *L); |
27 | static int meth_send(lua_State *L); | 26 | static int meth_send(lua_State *L); |
28 | static int meth_getstats(lua_State *L); | 27 | static 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 | \*-------------------------------------------------------------------------*/ | ||
275 | static 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 | \*-------------------------------------------------------------------------*/ |
275 | static int meth_listen(lua_State *L) | 290 | static 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 | \*-------------------------------------------------------------------------*/ |
349 | static int tcp_create(lua_State *L, int domain) { | 364 | static 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); |