luasocket/src/usocket.c

352 lines
12 KiB
C
Raw Normal View History

/*=========================================================================*\
* 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.
*
* RCS ID: $Id$
\*=========================================================================*/
2003-03-28 22:08:50 +01:00
#include <string.h>
2002-07-08 22:14:09 +02:00
#include "socket.h"
2002-07-08 22:14:09 +02:00
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
int sock_open(void)
2002-07-08 22:14:09 +02:00
{
/* instals a handler to ignore sigpipe or it will crash us */
struct sigaction ignore;
memset(&ignore, 0, sizeof(ignore));
ignore.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &ignore, NULL);
2003-03-21 02:07:23 +01:00
return 1;
2002-07-08 22:14:09 +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
{
if (*ps != SOCK_INVALID) {
close(*ps);
*ps = SOCK_INVALID;
}
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
int sock_create(p_sock ps, int domain, int type, int protocol)
2003-05-25 03:54:13 +02:00
{
int val = 1;
2003-05-25 03:54:13 +02:00
t_sock sock = socket(domain, type, protocol);
if (sock == SOCK_INVALID) return IO_ERROR;
2003-05-25 03:54:13 +02:00
*ps = sock;
sock_setnonblocking(ps);
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
return IO_DONE;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout)
2003-05-25 03:54:13 +02:00
{
2004-01-17 09:02:04 +01:00
t_sock sock = *ps;
if (sock == SOCK_INVALID) return IO_CLOSED;
/* if connect fails, we have to find out why */
2004-01-17 09:02:04 +01:00
if (connect(sock, addr, addr_len) < 0) {
struct timeval tv;
fd_set rfds, efds, wfds;
2004-01-17 09:02:04 +01:00
int err;
/* make sure the system is trying to connect */
if (errno != EINPROGRESS) return IO_ERROR;
2004-01-17 09:02:04 +01:00
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
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 */
err = select(sock+1, &rfds, &wfds, &efds, timeout >= 0? &tv: NULL);
/* if select was interrupted, ask the user to retry */
if (err < 0 && errno == EINTR) return IO_RETRY;
/* if selects readable, try reading */
if (err > 0) {
2004-01-17 09:02:04 +01:00
char dummy;
/* try reading so that errno is set */
if (recv(sock, &dummy, 0, 0) < 0 && errno != EAGAIN)
return IO_ERROR;
else return IO_DONE;
/* if no event happened, there was a timeout */
} else return IO_TIMEOUT;
/* otherwise connection succeeded */
} else return IO_DONE;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
2003-05-25 03:54:13 +02:00
{
if (bind(*ps, addr, addr_len) < 0) return IO_ERROR;
else return IO_DONE;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2003-05-25 03:54:13 +02:00
void sock_listen(p_sock ps, int backlog)
{
listen(*ps, backlog);
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
void sock_shutdown(p_sock ps, int how)
{
shutdown(*ps, how);
}
/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
int timeout)
2003-05-25 03:54:13 +02:00
{
t_sock sock = *ps;
SA dummy_addr;
socklen_t dummy_len;
2003-11-27 01:30:54 +01:00
if (sock == SOCK_INVALID) return IO_CLOSED;
if (!addr) addr = &dummy_addr;
if (!addr_len) addr_len = &dummy_len;
2003-05-25 03:54:13 +02:00
*pa = accept(sock, addr, addr_len);
if (*pa == SOCK_INVALID) {
struct timeval tv;
fd_set fds;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
/* just call select to avoid busy-wait. doesn't really matter
* what happens. the caller will choose to retry or not */
select(sock+1, &fds, NULL, NULL, timeout >= 0? &tv: NULL);
return IO_RETRY;
} else return IO_DONE;
2002-07-08 22:14:09 +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;
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;
/* make sure we repeat in case the call was interrupted */
do put = send(sock, data, count, 0);
while (put < 0 && errno == EINTR);
/* deal with failure */
if (put <= 0) {
struct timeval tv;
fd_set fds;
/* in any case, nothing has been sent */
2002-07-08 22:14:09 +02:00
*sent = 0;
/* here we know the connection has been closed */
if (errno == EPIPE) return IO_CLOSED;
/* run select to avoid busy wait */
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) {
/* here the call was interrupted. calling again might work */
if (errno == EINTR) return IO_RETRY;
/* here there was no data before timeout */
else return IO_TIMEOUT;
/* here we didn't send anything, but now we can */
} else return IO_DONE;
/* here we successfully sent something */
} else {
*sent = put;
return IO_DONE;
2002-07-08 22:14:09 +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,
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;
ssize_t put;
2003-11-27 01:30:54 +01:00
if (sock == SOCK_INVALID) return IO_CLOSED;
do put = sendto(sock, data, count, 0, addr, addr_len);
while (put < 0 && errno == EINTR);
if (put <= 0) {
struct timeval tv;
fd_set fds;
2002-07-08 22:14:09 +02:00
*sent = 0;
if (errno == EPIPE) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (select(sock+1, NULL, &fds, NULL, timeout >= 0? &tv: NULL) <= 0) {
if (errno == EINTR) return IO_RETRY;
else return IO_TIMEOUT;
} else return IO_DONE;
} else {
*sent = put;
return IO_DONE;
2002-07-08 22:14:09 +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;
ssize_t taken;
2003-11-27 01:30:54 +01:00
if (sock == SOCK_INVALID) return IO_CLOSED;
do taken = read(sock, data, count);
while (taken < 0 && errno == EINTR);
if (taken <= 0) {
struct timeval tv;
fd_set fds;
int ret;
2002-07-08 22:14:09 +02:00
*got = 0;
if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT;
else return IO_DONE;
} else {
*got = taken;
return IO_DONE;
2002-07-08 22:14:09 +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,
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;
ssize_t taken;
2003-11-27 01:30:54 +01:00
if (sock == SOCK_INVALID) return IO_CLOSED;
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
while (taken < 0 && errno == EINTR);
if (taken <= 0) {
struct timeval tv;
fd_set fds;
int ret;
2002-07-08 22:14:09 +02:00
*got = 0;
if (taken == 0) return IO_CLOSED;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
FD_ZERO(&fds);
FD_SET(sock, &fds);
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
if (ret < 0 && errno == EINTR) return IO_RETRY;
if (ret == 0) return IO_TIMEOUT;
else return IO_DONE;
} else {
*got = taken;
return IO_DONE;
2002-07-08 22:14:09 +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
\*-------------------------------------------------------------------------*/
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";
case NO_ADDRESS: return "unable to resolve host name";
case NO_RECOVERY: return "name server error";
case TRY_AGAIN: return "name server unavailable, try again later";
default: return "unknown error";
}
}
2003-05-25 03:54:13 +02:00
const char *sock_createstrerror(void)
2002-07-08 22:14:09 +02:00
{
switch (errno) {
case EACCES: return "access denied";
case EMFILE: return "descriptor table is full";
case ENFILE: return "too many open files";
case ENOBUFS: return "insuffucient buffer space";
default: return "unknown error";
}
}
2003-05-25 03:54:13 +02:00
const char *sock_bindstrerror(void)
2002-07-08 22:14:09 +02:00
{
switch (errno) {
case EBADF: return "invalid descriptor";
case EINVAL: return "socket already bound";
case EACCES: return "access denied";
case ENOTSOCK: return "not a socket descriptor";
case EADDRINUSE: return "address already in use";
case EADDRNOTAVAIL: return "address unavailable";
case ENOMEM: return "out of memory";
default: return "unknown error";
}
}
2003-05-25 03:54:13 +02:00
const char *sock_connectstrerror(void)
2002-07-08 22:14:09 +02:00
{
switch (errno) {
case EBADF: return "invalid descriptor";
case ENOTSOCK: return "not a socket descriptor";
case EADDRNOTAVAIL: return "address not availabe";
case ETIMEDOUT: return "connection timed out";
case ECONNREFUSED: return "connection refused";
case EACCES: return "access denied";
case ENETUNREACH: return "network is unreachable";
case EADDRINUSE: return "address already in use";
default: return "unknown error";
}
}