aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcp.c')
-rw-r--r--src/tcp.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/tcp.c b/src/tcp.c
index afa0477..a67b44a 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -36,6 +36,7 @@ static int meth_dirty(lua_State *L);
36static int opt_tcp_nodelay(lua_State *L); 36static int opt_tcp_nodelay(lua_State *L);
37static int opt_keepalive(lua_State *L); 37static int opt_keepalive(lua_State *L);
38static int opt_linger(lua_State *L); 38static int opt_linger(lua_State *L);
39static int opt_reuseaddr(lua_State *L);
39 40
40/* tcp object methods */ 41/* tcp object methods */
41static luaL_reg tcp[] = { 42static luaL_reg tcp[] = {
@@ -61,6 +62,7 @@ static luaL_reg tcp[] = {
61/* socket option handlers */ 62/* socket option handlers */
62static luaL_reg opt[] = { 63static luaL_reg opt[] = {
63 {"keepalive", opt_keepalive}, 64 {"keepalive", opt_keepalive},
65 {"reuseaddr", opt_reuseaddr},
64 {"tcp-nodelay", opt_tcp_nodelay}, 66 {"tcp-nodelay", opt_tcp_nodelay},
65 {"linger", opt_linger}, 67 {"linger", opt_linger},
66 {NULL, NULL} 68 {NULL, NULL}
@@ -123,7 +125,7 @@ static int meth_setoption(lua_State *L)
123 125
124static int opt_boolean(lua_State *L, int level, int name) 126static int opt_boolean(lua_State *L, int level, int name)
125{ 127{
126 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); 128 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
127 int val = aux_checkboolean(L, 2); 129 int val = aux_checkboolean(L, 2);
128 if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { 130 if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) {
129 lua_pushnil(L); 131 lua_pushnil(L);
@@ -134,16 +136,16 @@ static int opt_boolean(lua_State *L, int level, int name)
134 return 1; 136 return 1;
135} 137}
136 138
139/* enables reuse of local address */
140static int opt_reuseaddr(lua_State *L)
141{
142 return opt_boolean(L, SOL_SOCKET, SO_REUSEADDR);
143}
144
137/* disables the Naggle algorithm */ 145/* disables the Naggle algorithm */
138static int opt_tcp_nodelay(lua_State *L) 146static int opt_tcp_nodelay(lua_State *L)
139{ 147{
140 struct protoent *pe = getprotobyname("TCP"); 148 return opt_boolean(L, IPPROTO_TCP, TCP_NODELAY);
141 if (!pe) {
142 lua_pushnil(L);
143 lua_pushstring(L, "getprotobyname");
144 return 2;
145 }
146 return opt_boolean(L, pe->p_proto, TCP_NODELAY);
147} 149}
148 150
149static int opt_keepalive(lua_State *L) 151static int opt_keepalive(lua_State *L)