luasocket/src/usocket.c

374 lines
13 KiB
C
Raw Normal View History

/*=========================================================================*\
* Socket compatibilization module for Unix
* LuaSocket toolkit
*
2004-07-02 20:44:05 +02:00
* The code is now interrupt-safe.
* The penalty of calling select to avoid busy-wait is only paid when
* the I/O call fail in the first place.
2004-01-17 09:02:04 +01:00
*
* RCS ID: $Id$
\*=========================================================================*/
2004-07-02 20:44:05 +02:00
#include <string.h>
#include <signal.h>
2002-07-08 22:14:09 +02:00
#include "socket.h"
2002-07-08 22:14:09 +02:00
2004-07-15 08:11:53 +02:00
/*-------------------------------------------------------------------------*\
* Wait for readable/writable/connected socket with timeout
\*-------------------------------------------------------------------------*/
#ifdef SOCK_POLL
2004-07-15 08:11:53 +02:00
#include <sys/poll.h>
#define WAITFD_R POLLIN
#define WAITFD_W POLLOUT
#define WAITFD_C (POLLIN|POLLOUT)
int sock_waitfd(p_sock ps, int sw, p_tm tm) {
2004-07-15 08:11:53 +02:00
int ret;
struct pollfd pfd;
pfd.fd = *ps;
2004-07-15 08:11:53 +02:00
pfd.events = sw;
pfd.revents = 0;
if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
do {
int t = (int)(tm_getretry(tm)*1e3);
ret = poll(&pfd, 1, t >= 0? t: -1);
} while (ret == -1 && errno == EINTR);
2004-07-15 08:11:53 +02:00
if (ret == -1) return errno;
if (ret == 0) return IO_TIMEOUT;
if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED;
return IO_DONE;
}
#else
#define WAITFD_R 1
#define WAITFD_W 2
#define WAITFD_C (WAITFD_R|WAITFD_W)
int sock_waitfd(p_sock ps, int sw, p_tm tm) {
2004-07-15 08:11:53 +02:00
int ret;
fd_set rfds, wfds, *rp, *wp;
struct timeval tv, *tp;
double t;
if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
do {
/* must set bits within loop, because select may have modifed them */
rp = wp = NULL;
if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
2004-07-15 08:11:53 +02:00
t = tm_getretry(tm);
tp = NULL;
if (t >= 0.0) {
tv.tv_sec = (int)t;
tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6);
tp = &tv;
}
ret = select(*ps+1, rp, wp, NULL, tp);
2004-07-15 08:11:53 +02:00
} while (ret == -1 && errno == EINTR);
if (ret == -1) return errno;
if (ret == 0) return IO_TIMEOUT;
if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
return IO_DONE;
}
#endif
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
int sock_open(void) {
/* 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
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
int sock_close(void) {
2004-05-30 23:36:22 +02:00
return 1;
}
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
void sock_destroy(p_sock ps) {
if (*ps != SOCK_INVALID) {
sock_setblocking(ps);
close(*ps);
*ps = SOCK_INVALID;
}
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
2004-07-01 05:32:09 +02:00
* Select with timeout control
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) {
int ret;
do {
struct timeval tv;
double t = tm_getretry(tm);
tv.tv_sec = (int) t;
tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
ret = select(n, rfds, wfds, efds, t >= 0.0? &tv: NULL);
} while (ret < 0 && errno == EINTR);
return ret;
}
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
2004-07-15 08:11:53 +02:00
int sock_create(p_sock ps, int domain, int type, int protocol) {
*ps = socket(domain, type, protocol);
if (*ps != SOCK_INVALID) return IO_DONE;
else return errno;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
2004-07-15 08:11:53 +02:00
int sock_bind(p_sock ps, SA *addr, socklen_t len) {
int err = IO_DONE;
sock_setblocking(ps);
2004-07-15 08:11:53 +02:00
if (bind(*ps, addr, len) < 0) err = errno;
sock_setnonblocking(ps);
return err;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2004-07-15 08:11:53 +02:00
int sock_listen(p_sock ps, int backlog) {
int err = IO_DONE;
sock_setblocking(ps);
2004-07-15 08:11:53 +02:00
if (listen(*ps, backlog)) err = errno;
sock_setnonblocking(ps);
return err;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
void sock_shutdown(p_sock ps, int how) {
sock_setblocking(ps);
shutdown(*ps, how);
sock_setnonblocking(ps);
}
2004-07-15 08:11:53 +02:00
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
int err;
/* avoid calling on closed sockets */
if (*ps == SOCK_INVALID) return IO_CLOSED;
/* call connect until done or failed without being interrupted */
do if (connect(*ps, addr, len) == 0) return IO_DONE;
while ((err = errno) == EINTR);
/* if connection failed immediately, return error code */
if (err != EINPROGRESS && err != EAGAIN) return err;
2005-02-08 11:01:01 +01:00
/* zero timeout case optimization */
if (tm_iszero(tm)) return IO_TIMEOUT;
2004-07-15 08:11:53 +02:00
/* wait until we have the result of the connection attempt or timeout */
2005-02-08 11:01:01 +01:00
return sock_connected(ps, tm);
}
/*-------------------------------------------------------------------------*\
* Checks if socket is connected, or return reason for failure
\*-------------------------------------------------------------------------*/
int sock_connected(p_sock ps, p_tm tm) {
int err;
if ((err = sock_waitfd(ps, WAITFD_C, tm) == IO_CLOSED)) {
2004-07-15 08:11:53 +02:00
if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
else return errno;
} else return err;
}
/*-------------------------------------------------------------------------*\
* Accept with timeout
\*-------------------------------------------------------------------------*/
2004-07-15 08:11:53 +02:00
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
SA daddr;
socklen_t dlen = sizeof(daddr);
if (*ps == SOCK_INVALID) return IO_CLOSED;
if (!addr) addr = &daddr;
if (!len) len = &dlen;
for ( ;; ) {
int err;
2004-07-15 08:11:53 +02:00
if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE;
err = errno;
if (err == EINTR) continue;
if (err != EAGAIN && err != ECONNABORTED) return err;
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
2004-07-15 08:11:53 +02:00
}
/* can't reach here */
return IO_UNKNOWN;
2002-07-08 22:14:09 +02:00
}
/*-------------------------------------------------------------------------*\
* Send with timeout
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm)
2002-07-08 22:14:09 +02:00
{
2004-07-15 08:11:53 +02:00
int err;
/* avoid making system calls on closed sockets */
2004-07-15 08:11:53 +02:00
if (*ps == SOCK_INVALID) return IO_CLOSED;
2004-07-01 05:32:09 +02:00
/* loop until we send something or we give up on error */
2004-07-15 08:11:53 +02:00
*sent = 0;
2004-07-01 05:32:09 +02:00
for ( ;; ) {
2004-07-26 06:03:55 +02:00
long put = (long) send(*ps, data, count, 0);
2004-07-15 08:11:53 +02:00
/* if we sent anything, we are done */
if (put > 0) {
2004-07-01 05:32:09 +02:00
*sent = put;
return IO_DONE;
}
2004-07-15 08:11:53 +02:00
err = errno;
/* send can't really return 0, but EPIPE means the connection was
closed */
if (put == 0 || err == EPIPE) return IO_CLOSED;
/* we call was interrupted, just try again */
if (err == EINTR) continue;
/* if failed fatal reason, report error */
if (err != EAGAIN) return err;
/* wait until we can send something or we timeout */
if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
2004-07-15 08:11:53 +02:00
}
/* can't reach here */
return IO_UNKNOWN;
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,
2004-07-15 08:11:53 +02:00
SA *addr, socklen_t len, p_tm tm)
2002-07-08 22:14:09 +02:00
{
2004-07-15 08:11:53 +02:00
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
*sent = 0;
2004-07-01 05:32:09 +02:00
for ( ;; ) {
2004-07-26 06:03:55 +02:00
long put = (long) sendto(*ps, data, count, 0, addr, len);
2004-07-15 08:11:53 +02:00
if (put > 0) {
2004-07-01 05:32:09 +02:00
*sent = put;
return IO_DONE;
}
2004-07-15 08:11:53 +02:00
err = errno;
if (put == 0 || err == EPIPE) return IO_CLOSED;
if (err == EINTR) continue;
if (err != EAGAIN) return err;
if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
2004-07-15 08:11:53 +02:00
}
return IO_UNKNOWN;
2002-07-08 22:14:09 +02:00
}
/*-------------------------------------------------------------------------*\
* Receive with timeout
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
2004-07-15 08:11:53 +02:00
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
2004-07-01 05:32:09 +02:00
for ( ;; ) {
2004-07-26 06:03:55 +02:00
long taken = (long) recv(*ps, data, count, 0);
2004-07-01 05:32:09 +02:00
if (taken > 0) {
*got = taken;
return IO_DONE;
}
2004-07-15 08:11:53 +02:00
err = errno;
2002-07-08 22:14:09 +02:00
*got = 0;
if (taken == 0) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (err == EINTR) continue;
if (err != EAGAIN) return err;
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
2004-07-15 08:11:53 +02:00
}
return IO_UNKNOWN;
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,
2004-07-15 08:11:53 +02:00
SA *addr, socklen_t *len, p_tm tm) {
int err;
if (*ps == SOCK_INVALID) return IO_CLOSED;
2004-07-01 05:32:09 +02:00
for ( ;; ) {
2004-07-26 06:03:55 +02:00
long taken = (long) recvfrom(*ps, data, count, 0, addr, len);
2004-07-01 05:32:09 +02:00
if (taken > 0) {
*got = taken;
return IO_DONE;
}
2004-07-15 08:11:53 +02:00
err = errno;
2002-07-08 22:14:09 +02:00
*got = 0;
if (taken == 0) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (err == EINTR) continue;
if (err != EAGAIN) return err;
if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
2004-07-15 08:11:53 +02:00
}
return IO_UNKNOWN;
2002-07-08 22:14:09 +02:00
}
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
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
\*-------------------------------------------------------------------------*/
2004-07-01 05:32:09 +02:00
void sock_setnonblocking(p_sock ps) {
int flags = fcntl(*ps, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(*ps, F_SETFL, flags);
}
2004-07-15 08:11:53 +02:00
/*-------------------------------------------------------------------------*\
* DNS helpers
\*-------------------------------------------------------------------------*/
int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
*hp = gethostbyaddr(addr, len, AF_INET);
if (*hp) return IO_DONE;
2005-02-08 11:01:01 +01:00
else if (h_errno) return h_errno;
else if (errno) return errno;
else return IO_UNKNOWN;
2004-07-15 08:11:53 +02:00
}
int sock_gethostbyname(const char *addr, struct hostent **hp) {
*hp = gethostbyname(addr);
if (*hp) return IO_DONE;
2005-02-08 11:01:01 +01:00
else if (h_errno) return h_errno;
else if (errno) return errno;
else return IO_UNKNOWN;
2004-07-15 08:11:53 +02:00
}
/*-------------------------------------------------------------------------*\
* Error translation functions
2004-07-15 08:11:53 +02:00
* Make sure important error messages are standard
\*-------------------------------------------------------------------------*/
2004-07-15 08:11:53 +02:00
const char *sock_hoststrerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
2004-07-26 06:03:55 +02:00
case HOST_NOT_FOUND: return "host not found";
2004-07-15 08:11:53 +02:00
default: return hstrerror(err);
2004-07-02 20:44:05 +02:00
}
2002-07-08 22:14:09 +02:00
}
2004-07-15 08:11:53 +02:00
const char *sock_strerror(int err) {
if (err <= 0) return io_strerror(err);
switch (err) {
2004-07-26 06:03:55 +02:00
case EADDRINUSE: return "address already in use";
case EACCES: return "permission denied";
case ECONNREFUSED: return "connection refused";
case ECONNABORTED: return "closed";
case ECONNRESET: return "closed";
case ETIMEDOUT: return "timedout";
2004-07-15 08:11:53 +02:00
default: return strerror(errno);
2002-07-08 22:14:09 +02:00
}
}
2004-07-15 08:11:53 +02:00
const char *sock_ioerror(p_sock ps, int err) {
2004-07-01 05:47:22 +02:00
(void) ps;
2004-07-15 08:11:53 +02:00
return sock_strerror(err);
}