aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tcp.c55
1 files changed, 48 insertions, 7 deletions
diff --git a/src/tcp.c b/src/tcp.c
index 7bf1af5..f001206 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -2,11 +2,7 @@
2* TCP object 2* TCP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10 6
11#include "auxiliar.h" 7#include "auxiliar.h"
12#include "socket.h" 8#include "socket.h"
@@ -14,6 +10,8 @@
14#include "options.h" 10#include "options.h"
15#include "tcp.h" 11#include "tcp.h"
16 12
13#include <string.h>
14
17/*=========================================================================*\ 15/*=========================================================================*\
18* Internal function prototypes 16* Internal function prototypes
19\*=========================================================================*/ 17\*=========================================================================*/
@@ -36,6 +34,7 @@ static int meth_accept(lua_State *L);
36static int meth_close(lua_State *L); 34static int meth_close(lua_State *L);
37static int meth_getoption(lua_State *L); 35static int meth_getoption(lua_State *L);
38static int meth_setoption(lua_State *L); 36static int meth_setoption(lua_State *L);
37static int meth_gettimeout(lua_State *L);
39static int meth_settimeout(lua_State *L); 38static int meth_settimeout(lua_State *L);
40static int meth_getfd(lua_State *L); 39static int meth_getfd(lua_State *L);
41static int meth_setfd(lua_State *L); 40static int meth_setfd(lua_State *L);
@@ -65,26 +64,62 @@ static luaL_Reg tcp_methods[] = {
65 {"setpeername", meth_connect}, 64 {"setpeername", meth_connect},
66 {"setsockname", meth_bind}, 65 {"setsockname", meth_bind},
67 {"settimeout", meth_settimeout}, 66 {"settimeout", meth_settimeout},
67 {"gettimeout", meth_gettimeout},
68 {"shutdown", meth_shutdown}, 68 {"shutdown", meth_shutdown},
69 {NULL, NULL} 69 {NULL, NULL}
70}; 70};
71 71
72/* socket option handlers */ 72/* socket option handlers */
73static t_opt optget[] = { 73static t_opt optget[] = {
74 {"bindtodevice", opt_get_bindtodevice},
74 {"keepalive", opt_get_keepalive}, 75 {"keepalive", opt_get_keepalive},
75 {"reuseaddr", opt_get_reuseaddr}, 76 {"reuseaddr", opt_get_reuseaddr},
77 {"reuseport", opt_get_reuseport},
76 {"tcp-nodelay", opt_get_tcp_nodelay}, 78 {"tcp-nodelay", opt_get_tcp_nodelay},
79#ifdef TCP_KEEPIDLE
80 {"tcp-keepidle", opt_get_tcp_keepidle},
81#endif
82#ifdef TCP_KEEPCNT
83 {"tcp-keepcnt", opt_get_tcp_keepcnt},
84#endif
85#ifdef TCP_KEEPINTVL
86 {"tcp-keepintvl", opt_get_tcp_keepintvl},
87#endif
77 {"linger", opt_get_linger}, 88 {"linger", opt_get_linger},
78 {"error", opt_get_error}, 89 {"error", opt_get_error},
90 {"recv-buffer-size", opt_get_recv_buf_size},
91 {"send-buffer-size", opt_get_send_buf_size},
79 {NULL, NULL} 92 {NULL, NULL}
80}; 93};
81 94
82static t_opt optset[] = { 95static t_opt optset[] = {
96 {"bindtodevice", opt_set_bindtodevice},
83 {"keepalive", opt_set_keepalive}, 97 {"keepalive", opt_set_keepalive},
84 {"reuseaddr", opt_set_reuseaddr}, 98 {"reuseaddr", opt_set_reuseaddr},
99 {"reuseport", opt_set_reuseport},
85 {"tcp-nodelay", opt_set_tcp_nodelay}, 100 {"tcp-nodelay", opt_set_tcp_nodelay},
101#ifdef TCP_KEEPIDLE
102 {"tcp-keepidle", opt_set_tcp_keepidle},
103#endif
104#ifdef TCP_KEEPCNT
105 {"tcp-keepcnt", opt_set_tcp_keepcnt},
106#endif
107#ifdef TCP_KEEPINTVL
108 {"tcp-keepintvl", opt_set_tcp_keepintvl},
109#endif
86 {"ipv6-v6only", opt_set_ip6_v6only}, 110 {"ipv6-v6only", opt_set_ip6_v6only},
87 {"linger", opt_set_linger}, 111 {"linger", opt_set_linger},
112 {"recv-buffer-size", opt_set_recv_buf_size},
113 {"send-buffer-size", opt_set_send_buf_size},
114#ifdef TCP_DEFER_ACCEPT
115 {"tcp-defer-accept", opt_set_tcp_defer_accept},
116#endif
117#ifdef TCP_FASTOPEN
118 {"tcp-fastopen", opt_set_tcp_fastopen},
119#endif
120#ifdef TCP_FASTOPEN_CONNECT
121 {"tcp-fastopen-connect", opt_set_tcp_fastopen_connect},
122#endif
88 {NULL, NULL} 123 {NULL, NULL}
89}; 124};
90 125
@@ -348,6 +383,12 @@ static int meth_settimeout(lua_State *L)
348 return timeout_meth_settimeout(L, &tcp->tm); 383 return timeout_meth_settimeout(L, &tcp->tm);
349} 384}
350 385
386static int meth_gettimeout(lua_State *L)
387{
388 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
389 return timeout_meth_gettimeout(L, &tcp->tm);
390}
391
351/*=========================================================================*\ 392/*=========================================================================*\
352* Library functions 393* Library functions
353\*=========================================================================*/ 394\*=========================================================================*/
@@ -415,7 +456,7 @@ static int global_connect(lua_State *L) {
415 bindhints.ai_family = family; 456 bindhints.ai_family = family;
416 bindhints.ai_flags = AI_PASSIVE; 457 bindhints.ai_flags = AI_PASSIVE;
417 if (localaddr) { 458 if (localaddr) {
418 err = inet_trybind(&tcp->sock, &tcp->family, localaddr, 459 err = inet_trybind(&tcp->sock, &tcp->family, localaddr,
419 localserv, &bindhints); 460 localserv, &bindhints);
420 if (err) { 461 if (err) {
421 lua_pushnil(L); 462 lua_pushnil(L);
@@ -427,7 +468,7 @@ static int global_connect(lua_State *L) {
427 memset(&connecthints, 0, sizeof(connecthints)); 468 memset(&connecthints, 0, sizeof(connecthints));
428 connecthints.ai_socktype = SOCK_STREAM; 469 connecthints.ai_socktype = SOCK_STREAM;
429 /* make sure we try to connect only to the same family */ 470 /* make sure we try to connect only to the same family */
430 connecthints.ai_family = tcp->family; 471 connecthints.ai_family = tcp->family;
431 err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, 472 err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv,
432 &tcp->tm, &connecthints); 473 &tcp->tm, &connecthints);
433 if (err) { 474 if (err) {