From 12bde801f6a5d3a192dee29dda1266108aa98d45 Mon Sep 17 00:00:00 2001
From: Sam Roberts
+master:dirty()
+client:dirty()
+server:dirty()
+
+Check the read buffer status. +
+ ++Returns true if there is any data in the read buffer, false otherwise. +
+ ++Note: This is an internal method, any use is unlikely to be portable. +
+ + + +
+master:getfd()
+client:getfd()
+server:getfd()
+
+Returns the underling socket descriptor or handle associated to the object. +
+ ++The descriptor or handle. In case the object has been closed, the return will be -1. +
+ ++Note: This is an internal method, any use is unlikely to be portable. +
+ + + +
+master:setfd(fd)
+client:setfd(fd)
+server:setfd(fd)
+
+Sets the underling socket descriptor or handle associated to the object. The current one is simply replaced, not closed, and no other change to the object state is made. +
+ ++No return value. +
+ ++Note: This is an internal method, any use is unlikely to be portable. +
+-home · -download · +home · +download · installation · introduction · reference @@ -113,8 +113,8 @@ message.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/ftp.html b/doc/ftp.html index 9884f31..1f6335e 100644 --- a/doc/ftp.html +++ b/doc/ftp.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -270,8 +270,8 @@ f, e = ftp.put{
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/http.html b/doc/http.html index 0acac13..a274aef 100644 --- a/doc/http.html +++ b/doc/http.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · introduction · introduction · reference @@ -314,8 +314,8 @@ r, c = http.request {
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/index.html b/doc/index.html index 1605af1..bfeb7cc 100644 --- a/doc/index.html +++ b/doc/index.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -187,7 +187,7 @@ still available for those that have compatibility issues.
-download · +download · installation · introduction · reference diff --git a/doc/installation.html b/doc/installation.html index 0288f4a..bb9a5bb 100644 --- a/doc/installation.html +++ b/doc/installation.html @@ -25,8 +25,8 @@ Installation">
-home · -download · +home · +download · installation · introduction · reference @@ -144,8 +144,8 @@ Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/introduction.html b/doc/introduction.html index eff6367..fd22f48 100644 --- a/doc/introduction.html +++ b/doc/introduction.html @@ -25,8 +25,8 @@ Library, Support">
-home · -download · +home · +download · installation · introduction · reference @@ -314,8 +314,8 @@ and many others are covered by the reference manual.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/ltn12.html b/doc/ltn12.html index 0013950..7e2f49a 100644 --- a/doc/ltn12.html +++ b/doc/ltn12.html @@ -25,8 +25,8 @@ Pump, Support, Library">
-home · -download · +home · +download · installation · introduction · reference @@ -411,8 +411,8 @@ Creates and returns a source that produces the contents of a
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/mime.html b/doc/mime.html index d7faf52..9844744 100644 --- a/doc/mime.html +++ b/doc/mime.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -457,8 +457,8 @@ marker.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/reference.html b/doc/reference.html index edffc40..078d40c 100644 --- a/doc/reference.html +++ b/doc/reference.html @@ -25,8 +25,8 @@ Support, Manual">
-home · -download · +home · +download · installation · introduction · reference @@ -226,8 +226,8 @@ Support, Manual">
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/smtp.html b/doc/smtp.html index 27dd473..980b821 100644 --- a/doc/smtp.html +++ b/doc/smtp.html @@ -25,8 +25,8 @@ Library, Support">
-home · -download · +home · +download · installation · introduction · reference @@ -398,8 +398,8 @@ r, e = smtp.send{
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/socket.html b/doc/socket.html index 4d44f01..8a05885 100644 --- a/doc/socket.html +++ b/doc/socket.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -390,8 +390,8 @@ This constant has a string describing the current LuaSocket version.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/tcp.html b/doc/tcp.html index ab70f04..9583b1f 100644 --- a/doc/tcp.html +++ b/doc/tcp.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -573,8 +573,8 @@ Note: This is an internal method, any use is unlikely to be portable.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/udp.html b/doc/udp.html index 688649d..eca881d 100644 --- a/doc/udp.html +++ b/doc/udp.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -397,8 +397,8 @@ imperative nature obvious.
-home · -download · +home · +download · installation · introduction · reference diff --git a/doc/url.html b/doc/url.html index e87126f..303e05d 100644 --- a/doc/url.html +++ b/doc/url.html @@ -24,8 +24,8 @@
-home · -download · +home · +download · installation · introduction · reference @@ -310,8 +310,8 @@ The function returns the decoded string.
-home ·
-download ·
+home ·
+download ·
installation ·
introduction ·
reference
--
cgit v1.2.3-55-g6feb
From 8bb542baaf30874479b83d37af2fea5fa84d0a8e Mon Sep 17 00:00:00 2001
From: Sam Roberts
-The method returns 1 in case of success, or nil otherwise.
+The method returns 1 in case of success, or nil
+followed by an error message otherwise.
+
+Note: The descriptions above come from the man pages.
+
+client:getoption(option)
+Gets options for the TCP object.
+See setoption for description of the
+option names and values.
+
+Option is a string with the option name.
+
+The method returns the option value in case of success, or
+nil followed by an error message otherwise.
diff --git a/src/options.c b/src/options.c
index a464a4b..2085fdc 100644
--- a/src/options.c
+++ b/src/options.c
@@ -18,8 +18,11 @@
\*=========================================================================*/
static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
+static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
static int opt_set(lua_State *L, p_socket ps, int level, int name,
void *val, int len);
+static int opt_get(lua_State *L, p_socket ps, int level, int name,
+ void *val, int* len);
/*=========================================================================*\
* Exported functions
@@ -40,23 +43,51 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
return opt->func(L, ps);
}
+int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
+{
+ const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
+ while (opt->name && strcmp(name, opt->name))
+ opt++;
+ if (!opt->func) {
+ char msg[45];
+ sprintf(msg, "unsupported option `%.35s'", name);
+ luaL_argerror(L, 2, msg);
+ }
+ return opt->func(L, ps);
+}
+
/* enables reuse of local address */
int opt_reuseaddr(lua_State *L, p_socket ps)
{
return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
}
+int opt_get_reuseaddr(lua_State *L, p_socket ps)
+{
+ return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
+}
+
/* disables the Naggle algorithm */
int opt_tcp_nodelay(lua_State *L, p_socket ps)
{
return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
}
+int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
+{
+ return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
+}
+
int opt_keepalive(lua_State *L, p_socket ps)
{
return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
}
+int opt_get_keepalive(lua_State *L, p_socket ps)
+{
+ return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
+}
+
int opt_dontroute(lua_State *L, p_socket ps)
{
return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
@@ -105,6 +136,21 @@ int opt_ip_drop_membersip(lua_State *L, p_socket ps)
return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
}
+int opt_get_linger(lua_State *L, p_socket ps)
+{
+ struct linger li; /* obj, name */
+ int len = sizeof(li);
+ int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len);
+ if (err)
+ return err;
+ lua_newtable(L);
+ lua_pushboolean(L, li.l_onoff);
+ lua_setfield(L, -2, "on");
+ lua_pushinteger(L, li.l_linger);
+ lua_setfield(L, -2, "timeout");
+ return 1;
+}
+
/*=========================================================================*\
* Auxiliar functions
\*=========================================================================*/
@@ -129,6 +175,19 @@ static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
}
+static
+int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len)
+{
+ socklen_t socklen = *len;
+ if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, "getsockopt failed");
+ return 2;
+ }
+ *len = socklen;
+ return 0;
+}
+
static
int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
{
@@ -141,6 +200,17 @@ int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
return 1;
}
+static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
+{
+ int val = 0;
+ int len = sizeof(val);
+ int err = opt_get(L, ps, level, name, (char *) &val, &len);
+ if (err)
+ return err;
+ lua_pushboolean(L, val);
+ return 1;
+}
+
static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
{
int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
diff --git a/src/options.h b/src/options.h
index 900761e..c9b2e47 100644
--- a/src/options.h
+++ b/src/options.h
@@ -17,10 +17,17 @@
typedef struct t_opt {
const char *name;
int (*func)(lua_State *L, p_socket ps);
+ int (*get)(lua_State *L, p_socket ps);
} t_opt;
typedef t_opt *p_opt;
/* supported options */
+int opt_get_reuseaddr(lua_State *L, p_socket ps);
+int opt_get_tcp_nodelay(lua_State *L, p_socket ps);
+int opt_get_keepalive(lua_State *L, p_socket ps);
+int opt_get_linger(lua_State *L, p_socket ps);
+int opt_get_reuseaddr(lua_State *L, p_socket ps);
+
int opt_dontroute(lua_State *L, p_socket ps);
int opt_broadcast(lua_State *L, p_socket ps);
int opt_reuseaddr(lua_State *L, p_socket ps);
@@ -35,5 +42,6 @@ int opt_ip_drop_membersip(lua_State *L, p_socket ps);
/* invokes the appropriate option handler */
int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
+int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps);
#endif
diff --git a/src/tcp.c b/src/tcp.c
index 539ad5e..4713b23 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -31,6 +31,7 @@ static int meth_shutdown(lua_State *L);
static int meth_receive(lua_State *L);
static int meth_accept(lua_State *L);
static int meth_close(lua_State *L);
+static int meth_getoption(lua_State *L);
static int meth_setoption(lua_State *L);
static int meth_settimeout(lua_State *L);
static int meth_getfd(lua_State *L);
@@ -47,6 +48,7 @@ static luaL_reg tcp[] = {
{"connect", meth_connect},
{"dirty", meth_dirty},
{"getfd", meth_getfd},
+ {"getoption", meth_getoption},
{"getpeername", meth_getpeername},
{"getsockname", meth_getsockname},
{"getstats", meth_getstats},
@@ -64,6 +66,14 @@ static luaL_reg tcp[] = {
};
/* socket option handlers */
+static t_opt optget[] = {
+ {"keepalive", opt_get_keepalive},
+ {"reuseaddr", opt_get_reuseaddr},
+ {"tcp-nodelay", opt_get_tcp_nodelay},
+ {"linger", opt_get_linger},
+ {NULL, NULL}
+};
+
static t_opt opt[] = {
{"keepalive", opt_keepalive},
{"reuseaddr", opt_reuseaddr},
@@ -125,6 +135,12 @@ static int meth_setstats(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Just call option handler
\*-------------------------------------------------------------------------*/
+static int meth_getoption(lua_State *L)
+{
+ p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
+ return opt_meth_getoption(L, optget, &tcp->sock);
+}
+
static int meth_setoption(lua_State *L)
{
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
diff --git a/test/tcp-getoptions b/test/tcp-getoptions
new file mode 100755
index 0000000..f9b3d1b
--- /dev/null
+++ b/test/tcp-getoptions
@@ -0,0 +1,41 @@
+#!/usr/bin/env lua
+
+require"socket"
+
+port = 8765
+
+function options(o)
+ print("options for", o)
+
+ for _, opt in ipairs{"keepalive", "reuseaddr", "tcp-nodelay"} do
+ print("getoption", opt, o:getoption(opt))
+ end
+
+ print("getoption", "linger",
+ "on", o:getoption("linger").on,
+ "timeout", o:getoption("linger").timeout)
+end
+
+local m = socket.tcp()
+
+options(m)
+
+assert(m:bind("*", port))
+assert(m:listen())
+
+options(m)
+
+m:close()
+
+local m = socket.bind("*", port)
+
+options(m)
+
+local c = socket.connect("localhost", port)
+
+options(c)
+
+local s = m:accept()
+
+options(s)
+
--
cgit v1.2.3-55-g6feb
+server:getoption(option)
+
+
+
+
+