diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-06-04 20:44:39 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-06-04 20:44:39 +0000 |
| commit | 791c9f3168fc1bf86eb1c6b70393a5391de56439 (patch) | |
| tree | 73719c5d20a99bea17459adf2212795d02d085d7 /src | |
| parent | f1ae9db45e692d9b143fc0eefb36b9ad2fe32f69 (diff) | |
| download | luasocket-791c9f3168fc1bf86eb1c6b70393a5391de56439.tar.gz luasocket-791c9f3168fc1bf86eb1c6b70393a5391de56439.tar.bz2 luasocket-791c9f3168fc1bf86eb1c6b70393a5391de56439.zip | |
Select was also crashing on non-table parameters.
Changed time to _time and sleep to _sleep to avoid name clashes.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luasocket.c | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/src/luasocket.c b/src/luasocket.c index f144fce..9abfa09 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
| @@ -316,12 +316,14 @@ static int global_udpsocket(lua_State *L) | |||
| 316 | int top = lua_gettop(L); | 316 | int top = lua_gettop(L); |
| 317 | p_sock sock = push_udptable(L, tags); | 317 | p_sock sock = push_udptable(L, tags); |
| 318 | if (!sock) return 2; | 318 | if (!sock) return 2; |
| 319 | if (top >= 1 && lua_istable(L, 1)) { | 319 | if (top >= 1 ) { |
| 320 | lua_pushnil(L); | 320 | if (lua_istable(L, 1)) { |
| 321 | while (lua_next(L, 1)) { | 321 | lua_pushnil(L); |
| 322 | if (!set_option(L, sock)) lua_error(L, "invalid socket option"); | 322 | while (lua_next(L, 1)) { |
| 323 | lua_pop(L, 1); | 323 | if (!set_option(L, sock)) lua_error(L, "error setting option"); |
| 324 | } | 324 | lua_pop(L, 1); |
| 325 | } | ||
| 326 | } else luaL_argerror(L, 1, "invalid options"); | ||
| 325 | } | 327 | } |
| 326 | return 1; | 328 | return 1; |
| 327 | } | 329 | } |
| @@ -590,7 +592,7 @@ int global_select(lua_State *L) | |||
| 590 | /* writable sockets table to be returned */ | 592 | /* writable sockets table to be returned */ |
| 591 | lua_newtable(L); canwrite = lua_gettop(L); | 593 | lua_newtable(L); canwrite = lua_gettop(L); |
| 592 | /* get sockets we will test for readability into fd_set */ | 594 | /* get sockets we will test for readability into fd_set */ |
| 593 | if (!lua_isnil(L, 1)) { | 595 | if (lua_istable(L, 1)) { |
| 594 | lua_pushnil(L); | 596 | lua_pushnil(L); |
| 595 | while (lua_next(L, 1)) { | 597 | while (lua_next(L, 1)) { |
| 596 | if (lua_tag(L, -1) == tags->table) { /* skip strange fields */ | 598 | if (lua_tag(L, -1) == tags->table) { /* skip strange fields */ |
| @@ -620,7 +622,7 @@ int global_select(lua_State *L) | |||
| 620 | } | 622 | } |
| 621 | } | 623 | } |
| 622 | /* get sockets we will test for writability into fd_set */ | 624 | /* get sockets we will test for writability into fd_set */ |
| 623 | if (!lua_isnil(L, 2)) { | 625 | if (lua_istable(L, 2)) { |
| 624 | lua_pushnil(L); | 626 | lua_pushnil(L); |
| 625 | while (lua_next(L, 2)) { | 627 | while (lua_next(L, 2)) { |
| 626 | if (lua_tag(L, -1) == tags->table) { /* skip strange fields */ | 628 | if (lua_tag(L, -1) == tags->table) { /* skip strange fields */ |
| @@ -1070,23 +1072,31 @@ static int set_option(lua_State *L, p_sock sock) | |||
| 1070 | static const char *const optionnames[] = { | 1072 | static const char *const optionnames[] = { |
| 1071 | "SO_KEEPALIVE", "SO_DONTROUTE", "SO_BROADCAST", "SO_LINGER", NULL | 1073 | "SO_KEEPALIVE", "SO_DONTROUTE", "SO_BROADCAST", "SO_LINGER", NULL |
| 1072 | }; | 1074 | }; |
| 1073 | const char *option = lua_tostring(L, -2); | 1075 | const char *option; |
| 1074 | int err; | 1076 | int err; |
| 1077 | if (!lua_isstring(L, -2)) return 0; | ||
| 1078 | option = lua_tostring(L, -2); | ||
| 1075 | switch (luaL_findstring(option, optionnames)) { | 1079 | switch (luaL_findstring(option, optionnames)) { |
| 1076 | case 0: { | 1080 | case 0: { |
| 1077 | int bool = (int) lua_tonumber(L, -1); | 1081 | int bool; |
| 1082 | if (!lua_isnumber(L, -1)) return 0; | ||
| 1083 | bool = (int) lua_tonumber(L, -1); | ||
| 1078 | err = setsockopt(sock->sock, SOL_SOCKET, SO_KEEPALIVE, &bool, | 1084 | err = setsockopt(sock->sock, SOL_SOCKET, SO_KEEPALIVE, &bool, |
| 1079 | sizeof(bool)); | 1085 | sizeof(bool)); |
| 1080 | return err >= 0; | 1086 | return err >= 0; |
| 1081 | } | 1087 | } |
| 1082 | case 1: { | 1088 | case 1: { |
| 1083 | int bool = (int) lua_tonumber(L, -1); | 1089 | int bool; |
| 1090 | if (!lua_isnumber(L, -1)) return 0; | ||
| 1091 | bool = (int) lua_tonumber(L, -1); | ||
| 1084 | err = setsockopt(sock->sock, SOL_SOCKET, SO_DONTROUTE, &bool, | 1092 | err = setsockopt(sock->sock, SOL_SOCKET, SO_DONTROUTE, &bool, |
| 1085 | sizeof(bool)); | 1093 | sizeof(bool)); |
| 1086 | return err >= 0; | 1094 | return err >= 0; |
| 1087 | } | 1095 | } |
| 1088 | case 2: { | 1096 | case 2: { |
| 1089 | int bool = (int) lua_tonumber(L, -1); | 1097 | int bool; |
| 1098 | if (!lua_isnumber(L, -1)) return 0; | ||
| 1099 | bool = (int) lua_tonumber(L, -1); | ||
| 1090 | err = setsockopt(sock->sock, SOL_SOCKET, SO_BROADCAST, &bool, | 1100 | err = setsockopt(sock->sock, SOL_SOCKET, SO_BROADCAST, &bool, |
| 1091 | sizeof(bool)); | 1101 | sizeof(bool)); |
| 1092 | return err >= 0; | 1102 | return err >= 0; |
| @@ -1096,10 +1106,12 @@ static int set_option(lua_State *L, p_sock sock) | |||
| 1096 | if (!lua_istable(L, -1)) return 0; | 1106 | if (!lua_istable(L, -1)) return 0; |
| 1097 | lua_pushstring(L, "l_onoff"); | 1107 | lua_pushstring(L, "l_onoff"); |
| 1098 | lua_gettable(L, -2); | 1108 | lua_gettable(L, -2); |
| 1109 | if (!lua_isnumber(L, -1)) return 0; | ||
| 1099 | linger.l_onoff = lua_tonumber(L, -1); | 1110 | linger.l_onoff = lua_tonumber(L, -1); |
| 1100 | lua_pop(L, 1); | 1111 | lua_pop(L, 1); |
| 1101 | lua_pushstring(L, "l_linger"); | 1112 | lua_pushstring(L, "l_linger"); |
| 1102 | lua_gettable(L, -2); | 1113 | lua_gettable(L, -2); |
| 1114 | if (!lua_isnumber(L, -1)) return 0; | ||
| 1103 | linger.l_linger = lua_tonumber(L, -1); | 1115 | linger.l_linger = lua_tonumber(L, -1); |
| 1104 | lua_pop(L, 1); | 1116 | lua_pop(L, 1); |
| 1105 | err = setsockopt(sock->sock, SOL_SOCKET, SO_LINGER, &linger, | 1117 | err = setsockopt(sock->sock, SOL_SOCKET, SO_LINGER, &linger, |
| @@ -1678,8 +1690,8 @@ void lua_socketlibopen(lua_State *L) | |||
| 1678 | #endif | 1690 | #endif |
| 1679 | #ifdef _DEBUG | 1691 | #ifdef _DEBUG |
| 1680 | /* test support functions */ | 1692 | /* test support functions */ |
| 1681 | lua_pushcfunction(L, global_sleep); lua_setglobal(L, "sleep"); | 1693 | lua_pushcfunction(L, global_sleep); lua_setglobal(L, "_sleep"); |
| 1682 | lua_pushcfunction(L, global_time); lua_setglobal(L, "time"); | 1694 | lua_pushcfunction(L, global_time); lua_setglobal(L, "_time"); |
| 1683 | #endif | 1695 | #endif |
| 1684 | #ifndef LUASOCKET_NOGLOBALS | 1696 | #ifndef LUASOCKET_NOGLOBALS |
| 1685 | { | 1697 | { |
