2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Socket compatibilization module for Unix
|
|
|
|
* LuaSocket toolkit
|
|
|
|
*
|
2004-01-17 09:02:04 +01:00
|
|
|
* We are now treating EINTRs, but if an interrupt happens in the middle of
|
|
|
|
* a select function call, we don't guarantee values timeouts anymore.
|
|
|
|
* It's not a big deal, since we are not real-time anyways.
|
|
|
|
*
|
|
|
|
* We also exchanged the order of the calls to send/recv and select.
|
|
|
|
* The idea is that the outer loop (whoever is calling sock_send/recv)
|
|
|
|
* will call the function again if we didn't time out, so we can
|
|
|
|
* call write and then select only if it fails. This moves the penalty
|
|
|
|
* to when data is not available, maximizing the bandwidth if data is
|
|
|
|
* always available.
|
|
|
|
*
|
2003-06-26 20:47:49 +02:00
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2003-03-28 22:08:50 +01:00
|
|
|
#include <string.h>
|
2004-01-19 16:54:42 +01:00
|
|
|
#include <signal.h>
|
2002-07-08 22:14:09 +02:00
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
#include "socket.h"
|
2002-07-08 22:14:09 +02:00
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
static const char *sock_createstrerror(int err);
|
|
|
|
static const char *sock_bindstrerror(int err);
|
|
|
|
static const char *sock_connectstrerror(int err);
|
|
|
|
static const char *sock_acceptstrerror(int err);
|
|
|
|
static const char *sock_listenstrerror(int err);
|
2004-01-21 19:40:52 +01:00
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-06-09 20:23:40 +02:00
|
|
|
int sock_open(void)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2004-01-19 16:54:42 +01:00
|
|
|
#if DOESNT_COMPILE_TRY_THIS
|
2003-06-26 20:47:49 +02:00
|
|
|
struct sigaction ignore;
|
|
|
|
memset(&ignore, 0, sizeof(ignore));
|
|
|
|
ignore.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &ignore, NULL);
|
2004-01-19 16:54:42 +01:00
|
|
|
#endif
|
|
|
|
/* instals a handler to ignore sigpipe or it will crash us */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2003-03-21 02:07:23 +01:00
|
|
|
return 1;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2004-05-30 23:36:22 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Close module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
int sock_close(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Close and inutilize socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-05-25 03:54:13 +02:00
|
|
|
void sock_destroy(p_sock ps)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-08-16 02:06:04 +02:00
|
|
|
if (*ps != SOCK_INVALID) {
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setblocking(ps);
|
2003-08-16 02:06:04 +02:00
|
|
|
close(*ps);
|
|
|
|
*ps = SOCK_INVALID;
|
|
|
|
}
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Select with int timeout in ms
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, int timeout)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
return select(n, rfds, wfds, efds, timeout >= 0? &tv: NULL);
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Creates and sets up a socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-21 19:40:52 +01:00
|
|
|
const char *sock_create(p_sock ps, int domain, int type, int protocol)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
|
|
|
t_sock sock = socket(domain, type, protocol);
|
2004-01-24 01:18:19 +01:00
|
|
|
if (sock == SOCK_INVALID) return sock_createstrerror(errno);
|
2003-05-25 03:54:13 +02:00
|
|
|
*ps = sock;
|
2004-01-21 19:40:52 +01:00
|
|
|
return NULL;
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Connects or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-21 19:40:52 +01:00
|
|
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
2004-01-17 09:02:04 +01:00
|
|
|
t_sock sock = *ps;
|
2004-01-21 19:40:52 +01:00
|
|
|
int err;
|
|
|
|
/* don't call on closed socket */
|
|
|
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
|
|
|
/* ask system to connect */
|
2004-06-21 00:19:54 +02:00
|
|
|
do err = connect(sock, addr, addr_len);
|
|
|
|
while (err < 0 && errno == EINTR);
|
2004-01-21 19:40:52 +01:00
|
|
|
/* if no error, we're done */
|
|
|
|
if (err == 0) return NULL;
|
|
|
|
/* make sure the system is trying to connect */
|
2004-01-24 01:18:19 +01:00
|
|
|
if (errno != EINPROGRESS) return sock_connectstrerror(errno);
|
2004-01-21 19:40:52 +01:00
|
|
|
/* wait for a timeout or for the system's answer */
|
|
|
|
for ( ;; ) {
|
|
|
|
fd_set rfds, wfds, efds;
|
2004-01-18 01:04:20 +01:00
|
|
|
FD_ZERO(&rfds); FD_SET(sock, &rfds);
|
|
|
|
FD_ZERO(&wfds); FD_SET(sock, &wfds);
|
|
|
|
FD_ZERO(&efds); FD_SET(sock, &efds);
|
|
|
|
/* we run select to avoid busy waiting */
|
2004-01-24 01:18:19 +01:00
|
|
|
do err = sock_select(sock+1, &rfds, &wfds, &efds, tm_getretry(tm));
|
|
|
|
while (err < 0 && errno == EINTR);
|
2004-01-18 01:04:20 +01:00
|
|
|
/* if selects readable, try reading */
|
|
|
|
if (err > 0) {
|
2004-01-17 09:02:04 +01:00
|
|
|
char dummy;
|
2004-01-21 19:40:52 +01:00
|
|
|
/* recv will set errno to the value a blocking connect would set */
|
2004-06-23 03:08:54 +02:00
|
|
|
if (recv(sock, &dummy, 0, 0) < 0 && errno != EAGAIN)
|
2004-01-24 01:18:19 +01:00
|
|
|
return sock_connectstrerror(errno);
|
2004-01-21 19:40:52 +01:00
|
|
|
else
|
|
|
|
return NULL;
|
2004-01-18 01:04:20 +01:00
|
|
|
/* if no event happened, there was a timeout */
|
2004-01-21 19:40:52 +01:00
|
|
|
} else return io_strerror(IO_TIMEOUT);
|
|
|
|
}
|
|
|
|
return io_strerror(IO_TIMEOUT); /* can't get here */
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Binds or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-21 19:40:52 +01:00
|
|
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
|
|
|
if (bind(*ps, addr, addr_len) < 0) err = sock_bindstrerror(errno);
|
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
const char* sock_listen(p_sock ps, int backlog)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
|
|
|
if (listen(*ps, backlog))
|
|
|
|
err = sock_listenstrerror(errno);
|
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void sock_shutdown(p_sock ps, int how)
|
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
shutdown(*ps, how);
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
2004-01-16 08:06:31 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Accept with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *sock_accept(p_sock ps, p_sock pa, SA *addr,
|
|
|
|
socklen_t *addr_len, p_tm tm)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
|
|
|
t_sock sock = *ps;
|
2003-06-09 20:23:40 +02:00
|
|
|
SA dummy_addr;
|
2004-01-21 21:16:48 +01:00
|
|
|
socklen_t dummy_len = sizeof(dummy_addr);
|
2004-01-24 01:18:19 +01:00
|
|
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
2003-06-09 20:23:40 +02:00
|
|
|
if (!addr) addr = &dummy_addr;
|
|
|
|
if (!addr_len) addr_len = &dummy_len;
|
2004-01-21 19:40:52 +01:00
|
|
|
for (;;) {
|
2004-01-24 01:18:19 +01:00
|
|
|
int err;
|
2004-01-18 01:04:20 +01:00
|
|
|
fd_set fds;
|
2004-02-04 15:29:11 +01:00
|
|
|
/* try to accept */
|
2004-06-21 00:19:54 +02:00
|
|
|
do *pa = accept(sock, addr, addr_len);
|
|
|
|
while (*pa < 0 && errno == EINTR);
|
2004-01-24 01:18:19 +01:00
|
|
|
/* if result is valid, we are done */
|
2004-06-21 08:07:58 +02:00
|
|
|
if (*pa != SOCK_INVALID) return NULL;
|
2004-01-24 01:18:19 +01:00
|
|
|
/* find out if we failed for a fatal reason */
|
2004-06-23 03:08:54 +02:00
|
|
|
if (errno != EAGAIN && errno != ECONNABORTED)
|
2004-01-24 01:18:19 +01:00
|
|
|
return sock_acceptstrerror(errno);
|
2004-02-04 15:29:11 +01:00
|
|
|
/* call select to avoid busy-wait. */
|
2004-01-18 01:04:20 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
do err = sock_select(sock+1, &fds, NULL, NULL, tm_getretry(tm));
|
|
|
|
while (err < 0 && errno == EINTR);
|
|
|
|
if (err == 0) return io_strerror(IO_TIMEOUT);
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
2004-01-24 01:18:19 +01:00
|
|
|
return io_strerror(IO_TIMEOUT); /* can't get here */
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Send with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent,
|
|
|
|
int timeout)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-01-16 08:06:31 +01:00
|
|
|
ssize_t put;
|
|
|
|
/* avoid making system calls on closed sockets */
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-16 08:06:31 +01:00
|
|
|
/* make sure we repeat in case the call was interrupted */
|
2004-01-18 01:04:20 +01:00
|
|
|
do put = send(sock, data, count, 0);
|
|
|
|
while (put < 0 && errno == EINTR);
|
2004-01-16 08:06:31 +01:00
|
|
|
/* deal with failure */
|
|
|
|
if (put <= 0) {
|
2004-06-23 03:08:54 +02:00
|
|
|
int ret;
|
2004-01-18 01:04:20 +01:00
|
|
|
fd_set fds;
|
2004-01-16 08:06:31 +01:00
|
|
|
/* in any case, nothing has been sent */
|
2002-07-08 22:14:09 +02:00
|
|
|
*sent = 0;
|
2004-06-23 03:08:54 +02:00
|
|
|
/* only proceed to select if no error happened */
|
|
|
|
if (errno != EAGAIN) return IO_ERROR;
|
|
|
|
/* optimize for the timeout = 0 case */
|
|
|
|
if (timeout == 0) return IO_TIMEOUT;
|
2004-01-18 01:04:20 +01:00
|
|
|
/* here we know the connection has been closed */
|
|
|
|
if (errno == EPIPE) return IO_CLOSED;
|
2004-01-16 08:06:31 +01:00
|
|
|
/* run select to avoid busy wait */
|
2004-01-18 01:04:20 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-06-23 03:08:54 +02:00
|
|
|
ret = sock_select(sock+1, NULL, &fds, NULL, timeout);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret > 0 || errno == EINTR) return IO_RETRY;
|
|
|
|
else return IO_ERROR;
|
2004-01-17 01:17:46 +01:00
|
|
|
/* here we successfully sent something */
|
2004-01-16 08:06:31 +01:00
|
|
|
} else {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Sendto with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
|
2003-06-09 20:23:40 +02:00
|
|
|
SA *addr, socklen_t addr_len, int timeout)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-01-17 01:17:46 +01:00
|
|
|
ssize_t put;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
do put = sendto(sock, data, count, 0, addr, addr_len);
|
2004-01-18 01:04:20 +01:00
|
|
|
while (put < 0 && errno == EINTR);
|
2004-01-17 01:17:46 +01:00
|
|
|
if (put <= 0) {
|
2004-06-23 03:08:54 +02:00
|
|
|
int ret;
|
2004-01-18 01:04:20 +01:00
|
|
|
fd_set fds;
|
2002-07-08 22:14:09 +02:00
|
|
|
*sent = 0;
|
2004-06-23 03:08:54 +02:00
|
|
|
if (errno != EAGAIN) return IO_ERROR;
|
|
|
|
if (timeout == 0) return IO_TIMEOUT;
|
2004-01-18 01:04:20 +01:00
|
|
|
if (errno == EPIPE) return IO_CLOSED;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-06-23 03:08:54 +02:00
|
|
|
ret = sock_select(sock+1, NULL, &fds, NULL, timeout);
|
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
|
|
|
else if (ret > 0 || errno == EINTR) return IO_RETRY;
|
|
|
|
else return IO_ERROR;
|
2004-01-17 01:17:46 +01:00
|
|
|
} else {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Receive with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, int timeout)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-01-16 08:06:31 +01:00
|
|
|
ssize_t taken;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-16 08:06:31 +01:00
|
|
|
do taken = read(sock, data, count);
|
2004-01-18 01:04:20 +01:00
|
|
|
while (taken < 0 && errno == EINTR);
|
2004-01-16 08:06:31 +01:00
|
|
|
if (taken <= 0) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2002-07-08 22:14:09 +02:00
|
|
|
*got = 0;
|
2004-01-16 08:06:31 +01:00
|
|
|
if (taken == 0) return IO_CLOSED;
|
2004-06-23 03:08:54 +02:00
|
|
|
if (errno != EAGAIN) return IO_ERROR;
|
|
|
|
if (timeout == 0) return IO_TIMEOUT;
|
2004-01-16 08:06:31 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
|
2004-01-18 01:04:20 +01:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-06-23 03:08:54 +02:00
|
|
|
else if (ret > 0 || errno == EINTR) return IO_RETRY;
|
|
|
|
else return IO_ERROR;
|
2004-01-16 08:06:31 +01:00
|
|
|
} else {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Recvfrom with timeout
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
|
2003-06-09 20:23:40 +02:00
|
|
|
SA *addr, socklen_t *addr_len, int timeout)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-01-17 01:17:46 +01:00
|
|
|
ssize_t taken;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-01-17 01:17:46 +01:00
|
|
|
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
|
2004-01-18 01:04:20 +01:00
|
|
|
while (taken < 0 && errno == EINTR);
|
2004-01-17 01:17:46 +01:00
|
|
|
if (taken <= 0) {
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2002-07-08 22:14:09 +02:00
|
|
|
*got = 0;
|
2004-01-17 01:17:46 +01:00
|
|
|
if (taken == 0) return IO_CLOSED;
|
2004-06-23 03:08:54 +02:00
|
|
|
if (errno != EAGAIN) return IO_ERROR;
|
|
|
|
if (timeout == 0) return IO_TIMEOUT;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-01-24 01:18:19 +01:00
|
|
|
ret = sock_select(sock+1, &fds, NULL, NULL, timeout);
|
2004-01-18 01:04:20 +01:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-06-23 03:08:54 +02:00
|
|
|
else if (ret > 0 || errno == EINTR) return IO_RETRY;
|
|
|
|
else return IO_ERROR;
|
2004-01-17 01:17:46 +01:00
|
|
|
} else {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void sock_setblocking(p_sock ps)
|
|
|
|
{
|
|
|
|
int flags = fcntl(*ps, F_GETFL, 0);
|
|
|
|
flags &= (~(O_NONBLOCK));
|
|
|
|
fcntl(*ps, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into non-blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
void sock_setnonblocking(p_sock ps)
|
|
|
|
{
|
|
|
|
int flags = fcntl(*ps, F_GETFL, 0);
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
fcntl(*ps, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Error translation functions
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by gethostbyname */
|
2003-05-25 03:54:13 +02:00
|
|
|
const char *sock_hoststrerror(void)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
|
|
|
switch (h_errno) {
|
|
|
|
case HOST_NOT_FOUND: return "host not found";
|
2004-01-24 01:18:19 +01:00
|
|
|
case NO_ADDRESS: return "valid host but no ip found";
|
2002-07-08 22:14:09 +02:00
|
|
|
case NO_RECOVERY: return "name server error";
|
|
|
|
case TRY_AGAIN: return "name server unavailable, try again later";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by socket */
|
|
|
|
static const char *sock_createstrerror(int err)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
|
|
|
case EPROTONOSUPPORT: return "protocol not supported";
|
2002-07-08 22:14:09 +02:00
|
|
|
case EACCES: return "access denied";
|
2004-01-24 01:18:19 +01:00
|
|
|
case EMFILE: return "process file table is full";
|
|
|
|
case ENFILE: return "kernel file table is full";
|
|
|
|
case EINVAL: return "unknown protocol or family";
|
2002-07-08 22:14:09 +02:00
|
|
|
case ENOBUFS: return "insuffucient buffer space";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by accept */
|
|
|
|
static const char *sock_acceptstrerror(int err)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
2004-06-23 03:08:54 +02:00
|
|
|
case EAGAIN: return io_strerror(IO_RETRY);
|
2002-07-08 22:14:09 +02:00
|
|
|
case EBADF: return "invalid descriptor";
|
2004-01-24 01:18:19 +01:00
|
|
|
case ENOBUFS: case ENOMEM: return "insuffucient buffer space";
|
|
|
|
case ENOTSOCK: return "descriptor not a socket";
|
|
|
|
case EOPNOTSUPP: return "not supported";
|
|
|
|
case EINTR: return "call interrupted";
|
|
|
|
case ECONNABORTED: return "connection aborted";
|
|
|
|
case EINVAL: return "not listening";
|
|
|
|
case EMFILE: return "process file table is full";
|
|
|
|
case ENFILE: return "kernel file table is full";
|
|
|
|
case EFAULT: return "invalid memory address";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* return error messages for the known errors reported by bind */
|
|
|
|
static const char *sock_bindstrerror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case EBADF: return "invalid descriptor";
|
|
|
|
case ENOTSOCK: return "descriptor not a socket";
|
|
|
|
case EADDRNOTAVAIL: return "address unavailable in local host";
|
2002-07-08 22:14:09 +02:00
|
|
|
case EADDRINUSE: return "address already in use";
|
2004-01-24 01:18:19 +01:00
|
|
|
case EINVAL: return "already bound";
|
|
|
|
case EACCES: return "access denied";
|
|
|
|
case EFAULT: return "invalid memory address";
|
2002-07-08 22:14:09 +02:00
|
|
|
case ENOMEM: return "out of memory";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-24 01:18:19 +01:00
|
|
|
/* return error messages for the known errors reported by listen */
|
|
|
|
static const char *sock_listenstrerror(int err)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2004-01-24 01:18:19 +01:00
|
|
|
switch (err) {
|
|
|
|
case EADDRINUSE: return "local address already in use";
|
2002-07-08 22:14:09 +02:00
|
|
|
case EBADF: return "invalid descriptor";
|
2004-01-24 01:18:19 +01:00
|
|
|
case ENOTSOCK: return "descriptor not a socket";
|
|
|
|
case EOPNOTSUPP: return "not supported";
|
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return error messages for the known errors reported by connect */
|
|
|
|
static const char *sock_connectstrerror(int err)
|
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case EBADF: return "invalid descriptor";
|
|
|
|
case EFAULT: return "invalid memory address";
|
|
|
|
case ENOTSOCK: return "descriptor not a socket";
|
|
|
|
case EADDRNOTAVAIL: return "address not available in local host";
|
|
|
|
case EISCONN: return "already connected";
|
2002-07-08 22:14:09 +02:00
|
|
|
case ECONNREFUSED: return "connection refused";
|
2004-01-24 01:18:19 +01:00
|
|
|
case ETIMEDOUT: return io_strerror(IO_TIMEOUT);
|
2002-07-08 22:14:09 +02:00
|
|
|
case ENETUNREACH: return "network is unreachable";
|
2004-01-24 01:18:19 +01:00
|
|
|
case EADDRINUSE: return "local address already in use";
|
|
|
|
case EINPROGRESS: return "would block";
|
|
|
|
case EALREADY: return "connect already in progress";
|
|
|
|
case EAGAIN: return "not enough free ports";
|
|
|
|
case EAFNOSUPPORT: return "address family not supported";
|
|
|
|
case EPERM: return "broadcast not enabled or firewall block";
|
2002-07-08 22:14:09 +02:00
|
|
|
default: return "unknown error";
|
|
|
|
}
|
|
|
|
}
|