Some internal functions were not static.

Correct select bug that would crash on closed sockets.
This commit is contained in:
Diego Nehab 2001-04-16 19:56:33 +00:00
parent bd0bf45979
commit 561177a1dd

View File

@ -211,13 +211,13 @@ static char *socket_strerror(void);
static char *connect_strerror(void); static char *connect_strerror(void);
/* socket auxiliary functions */ /* socket auxiliary functions */
const char *tcp_trybind(p_sock sock, const char *address, static const char *tcp_trybind(p_sock sock, const char *address,
unsigned short port, int backlog); unsigned short port, int backlog);
const char *tcp_tryconnect(p_sock sock, const char *address, static const char *tcp_tryconnect(p_sock sock, const char *address,
unsigned short port); unsigned short port);
const char *udp_setpeername(p_sock sock, const char *address, static const char *udp_setpeername(p_sock sock, const char *address,
unsigned short port); unsigned short port);
const char *udp_setsockname(p_sock sock, const char *address, static const char *udp_setsockname(p_sock sock, const char *address,
unsigned short port); unsigned short port);
static void set_reuseaddr(p_sock sock); static void set_reuseaddr(p_sock sock);
static void set_blocking(p_sock sock); static void set_blocking(p_sock sock);
@ -586,15 +586,17 @@ int global_select(lua_State *L)
if (!lua_isnil(L, 1)) { if (!lua_isnil(L, 1)) {
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, 1)) { while (lua_next(L, 1)) {
if (lua_tag(L, -1) == tags->table) { if (lua_tag(L, -1) == tags->table) { /* skip strange fields */
p_sock sock = get_sock(L, -1, tags, NULL); p_sock sock = get_sock(L, -1, tags, NULL);
if (sock->sock != INVALID_SOCKET) { /* skip closed sockets */
lua_pushnumber(L, sock->sock); lua_pushnumber(L, sock->sock);
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_settable(L, byfds); lua_settable(L, byfds);
if (sock->sock > max) max = sock->sock; if (sock->sock > max) max = sock->sock;
/* a socket can have unread data in our internal buffer. in /* a socket can have unread data in our internal
* that case, we only call select to find out which of the buffer. in that case, we only call select to find
* other sockets can be written to or read from immediately. */ out which of the other sockets can be written to
or read from immediately. */
if (!bf_isempty(sock)) { if (!bf_isempty(sock)) {
ms = 0; ms = 0;
lua_pushnumber(L, lua_getn(L, canread) + 1); lua_pushnumber(L, lua_getn(L, canread) + 1);
@ -605,6 +607,7 @@ int global_select(lua_State *L)
prfds = &readfds; prfds = &readfds;
} }
} }
}
/* get rid of lua_next value and expose index */ /* get rid of lua_next value and expose index */
lua_pop(L, 1); lua_pop(L, 1);
} }
@ -613,8 +616,9 @@ int global_select(lua_State *L)
if (!lua_isnil(L, 2)) { if (!lua_isnil(L, 2)) {
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, 2)) { while (lua_next(L, 2)) {
if (lua_tag(L, -1) == tags->table) { if (lua_tag(L, -1) == tags->table) { /* skip strange fields */
p_sock sock = get_sock(L, -1, tags, NULL); p_sock sock = get_sock(L, -1, tags, NULL);
if (sock->sock != INVALID_SOCKET) { /* skip closed sockets */
lua_pushnumber(L, sock->sock); lua_pushnumber(L, sock->sock);
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_settable(L, byfds); lua_settable(L, byfds);
@ -622,6 +626,7 @@ int global_select(lua_State *L)
FD_SET(sock->sock, &writefds); FD_SET(sock->sock, &writefds);
pwfds = &writefds; pwfds = &writefds;
} }
}
/* get rid of lua_next value and expose index */ /* get rid of lua_next value and expose index */
lua_pop(L, 1); lua_pop(L, 1);
} }
@ -995,7 +1000,7 @@ static void handle_sigpipe(void)
* Returns * Returns
* NULL in case of success, error message otherwise * NULL in case of success, error message otherwise
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *tcp_tryconnect(p_sock sock, const char *address, static const char *tcp_tryconnect(p_sock sock, const char *address,
unsigned short port) unsigned short port)
{ {
struct sockaddr_in remote; struct sockaddr_in remote;
@ -1053,7 +1058,7 @@ void set_reuseaddr(p_sock sock)
* Returns * Returns
* NULL in case of success, error message otherwise * NULL in case of success, error message otherwise
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *tcp_trybind(p_sock sock, const char *address, static const char *tcp_trybind(p_sock sock, const char *address,
unsigned short port, int backlog) unsigned short port, int backlog)
{ {
struct sockaddr_in local; struct sockaddr_in local;
@ -1106,7 +1111,7 @@ const char *tcp_trybind(p_sock sock, const char *address,
* Returns * Returns
* NULL in case of success, error message otherwise * NULL in case of success, error message otherwise
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *udp_setsockname(p_sock sock, const char *address, static const char *udp_setsockname(p_sock sock, const char *address,
unsigned short port) unsigned short port)
{ {
struct sockaddr_in local; struct sockaddr_in local;
@ -1151,7 +1156,7 @@ const char *udp_setsockname(p_sock sock, const char *address,
* Returns * Returns
* NULL in case of success, error message otherwise * NULL in case of success, error message otherwise
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *udp_setpeername(p_sock sock, const char *address, static const char *udp_setpeername(p_sock sock, const char *address,
unsigned short port) unsigned short port)
{ {
struct sockaddr_in local; struct sockaddr_in local;