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,24 +586,27 @@ 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);
lua_pushnumber(L, sock->sock); if (sock->sock != INVALID_SOCKET) { /* skip closed sockets */
lua_pushvalue(L, -2); lua_pushnumber(L, sock->sock);
lua_settable(L, byfds); lua_pushvalue(L, -2);
if (sock->sock > max) max = sock->sock; lua_settable(L, byfds);
/* a socket can have unread data in our internal buffer. in if (sock->sock > max) max = sock->sock;
* that case, we only call select to find out which of the /* a socket can have unread data in our internal
* other sockets can be written to or read from immediately. */ buffer. in that case, we only call select to find
if (!bf_isempty(sock)) { out which of the other sockets can be written to
ms = 0; or read from immediately. */
lua_pushnumber(L, lua_getn(L, canread) + 1); if (!bf_isempty(sock)) {
lua_pushvalue(L, -2); ms = 0;
lua_settable(L, canread); lua_pushnumber(L, lua_getn(L, canread) + 1);
} else { lua_pushvalue(L, -2);
FD_SET(sock->sock, &readfds); lua_settable(L, canread);
prfds = &readfds; } else {
} FD_SET(sock->sock, &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,14 +616,16 @@ 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);
lua_pushnumber(L, sock->sock); if (sock->sock != INVALID_SOCKET) { /* skip closed sockets */
lua_pushvalue(L, -2); lua_pushnumber(L, sock->sock);
lua_settable(L, byfds); lua_pushvalue(L, -2);
if (sock->sock > max) max = sock->sock; lua_settable(L, byfds);
FD_SET(sock->sock, &writefds); if (sock->sock > max) max = sock->sock;
pwfds = &writefds; FD_SET(sock->sock, &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;