diff options
| author | Sam Roberts <vieuxtech@gmail.com> | 2012-02-27 13:26:23 -0800 |
|---|---|---|
| committer | Sam Roberts <vieuxtech@gmail.com> | 2012-04-11 13:54:01 -0700 |
| commit | 8bb542baaf30874479b83d37af2fea5fa84d0a8e (patch) | |
| tree | 117d706140ee622565fa1c671c5469cb7f30fdac /src/options.c | |
| parent | 0716cb868e847bb9f66c659f8662d905ba012de8 (diff) | |
| download | luasocket-8bb542baaf30874479b83d37af2fea5fa84d0a8e.tar.gz luasocket-8bb542baaf30874479b83d37af2fea5fa84d0a8e.tar.bz2 luasocket-8bb542baaf30874479b83d37af2fea5fa84d0a8e.zip | |
Support getoption method for tcp objects.
Diffstat (limited to 'src/options.c')
| -rw-r--r-- | src/options.c | 70 |
1 files changed, 70 insertions, 0 deletions
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 @@ | |||
| 18 | \*=========================================================================*/ | 18 | \*=========================================================================*/ |
| 19 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); | 19 | static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); |
| 20 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); | 20 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); |
| 21 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); | ||
| 21 | static int opt_set(lua_State *L, p_socket ps, int level, int name, | 22 | static int opt_set(lua_State *L, p_socket ps, int level, int name, |
| 22 | void *val, int len); | 23 | void *val, int len); |
| 24 | static int opt_get(lua_State *L, p_socket ps, int level, int name, | ||
| 25 | void *val, int* len); | ||
| 23 | 26 | ||
| 24 | /*=========================================================================*\ | 27 | /*=========================================================================*\ |
| 25 | * Exported functions | 28 | * Exported functions |
| @@ -40,23 +43,51 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) | |||
| 40 | return opt->func(L, ps); | 43 | return opt->func(L, ps); |
| 41 | } | 44 | } |
| 42 | 45 | ||
| 46 | int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) | ||
| 47 | { | ||
| 48 | const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ | ||
| 49 | while (opt->name && strcmp(name, opt->name)) | ||
| 50 | opt++; | ||
| 51 | if (!opt->func) { | ||
| 52 | char msg[45]; | ||
| 53 | sprintf(msg, "unsupported option `%.35s'", name); | ||
| 54 | luaL_argerror(L, 2, msg); | ||
| 55 | } | ||
| 56 | return opt->func(L, ps); | ||
| 57 | } | ||
| 58 | |||
| 43 | /* enables reuse of local address */ | 59 | /* enables reuse of local address */ |
| 44 | int opt_reuseaddr(lua_State *L, p_socket ps) | 60 | int opt_reuseaddr(lua_State *L, p_socket ps) |
| 45 | { | 61 | { |
| 46 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); | 62 | return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); |
| 47 | } | 63 | } |
| 48 | 64 | ||
| 65 | int opt_get_reuseaddr(lua_State *L, p_socket ps) | ||
| 66 | { | ||
| 67 | return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); | ||
| 68 | } | ||
| 69 | |||
| 49 | /* disables the Naggle algorithm */ | 70 | /* disables the Naggle algorithm */ |
| 50 | int opt_tcp_nodelay(lua_State *L, p_socket ps) | 71 | int opt_tcp_nodelay(lua_State *L, p_socket ps) |
| 51 | { | 72 | { |
| 52 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); | 73 | return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); |
| 53 | } | 74 | } |
| 54 | 75 | ||
| 76 | int opt_get_tcp_nodelay(lua_State *L, p_socket ps) | ||
| 77 | { | ||
| 78 | return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); | ||
| 79 | } | ||
| 80 | |||
| 55 | int opt_keepalive(lua_State *L, p_socket ps) | 81 | int opt_keepalive(lua_State *L, p_socket ps) |
| 56 | { | 82 | { |
| 57 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); | 83 | return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); |
| 58 | } | 84 | } |
| 59 | 85 | ||
| 86 | int opt_get_keepalive(lua_State *L, p_socket ps) | ||
| 87 | { | ||
| 88 | return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); | ||
| 89 | } | ||
| 90 | |||
| 60 | int opt_dontroute(lua_State *L, p_socket ps) | 91 | int opt_dontroute(lua_State *L, p_socket ps) |
| 61 | { | 92 | { |
| 62 | return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); | 93 | return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); |
| @@ -105,6 +136,21 @@ int opt_ip_drop_membersip(lua_State *L, p_socket ps) | |||
| 105 | return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); | 136 | return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); |
| 106 | } | 137 | } |
| 107 | 138 | ||
| 139 | int opt_get_linger(lua_State *L, p_socket ps) | ||
| 140 | { | ||
| 141 | struct linger li; /* obj, name */ | ||
| 142 | int len = sizeof(li); | ||
| 143 | int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len); | ||
| 144 | if (err) | ||
| 145 | return err; | ||
| 146 | lua_newtable(L); | ||
| 147 | lua_pushboolean(L, li.l_onoff); | ||
| 148 | lua_setfield(L, -2, "on"); | ||
| 149 | lua_pushinteger(L, li.l_linger); | ||
| 150 | lua_setfield(L, -2, "timeout"); | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 108 | /*=========================================================================*\ | 154 | /*=========================================================================*\ |
| 109 | * Auxiliar functions | 155 | * Auxiliar functions |
| 110 | \*=========================================================================*/ | 156 | \*=========================================================================*/ |
| @@ -130,6 +176,19 @@ static int opt_setmembership(lua_State *L, p_socket ps, int level, int name) | |||
| 130 | } | 176 | } |
| 131 | 177 | ||
| 132 | static | 178 | static |
| 179 | int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) | ||
| 180 | { | ||
| 181 | socklen_t socklen = *len; | ||
| 182 | if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) { | ||
| 183 | lua_pushnil(L); | ||
| 184 | lua_pushstring(L, "getsockopt failed"); | ||
| 185 | return 2; | ||
| 186 | } | ||
| 187 | *len = socklen; | ||
| 188 | return 0; | ||
| 189 | } | ||
| 190 | |||
| 191 | static | ||
| 133 | int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) | 192 | int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) |
| 134 | { | 193 | { |
| 135 | if (setsockopt(*ps, level, name, (char *) val, len) < 0) { | 194 | if (setsockopt(*ps, level, name, (char *) val, len) < 0) { |
| @@ -141,6 +200,17 @@ int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) | |||
| 141 | return 1; | 200 | return 1; |
| 142 | } | 201 | } |
| 143 | 202 | ||
| 203 | static int opt_getboolean(lua_State *L, p_socket ps, int level, int name) | ||
| 204 | { | ||
| 205 | int val = 0; | ||
| 206 | int len = sizeof(val); | ||
| 207 | int err = opt_get(L, ps, level, name, (char *) &val, &len); | ||
| 208 | if (err) | ||
| 209 | return err; | ||
| 210 | lua_pushboolean(L, val); | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | |||
| 144 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) | 214 | static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) |
| 145 | { | 215 | { |
| 146 | int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ | 216 | int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ |
