luasocket/src/usocket.c

454 lines
16 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-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
\*-------------------------------------------------------------------------*/
#ifndef SOCKET_SELECT
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)
2005-10-07 06:40:59 +02:00
int socket_waitfd(p_socket ps, int sw, p_timeout 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;
2005-10-07 06:40:59 +02:00
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
do {
int t = (int)(timeout_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)
2005-10-07 06:40:59 +02:00
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
2004-07-15 08:11:53 +02:00
int ret;
fd_set rfds, wfds, *rp, *wp;
struct timeval tv, *tp;
double t;
if (*ps >= FD_SETSIZE) return EINVAL;
2005-10-07 06:40:59 +02:00
if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
2004-07-15 08:11:53 +02:00
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; }
2005-10-07 06:40:59 +02:00
t = timeout_getretry(tm);
2004-07-15 08:11:53 +02:00
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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_close(void) {
2004-05-30 23:36:22 +02:00
return 1;
}
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
void socket_destroy(p_socket ps) {
if (*ps != SOCKET_INVALID) {
socket_setblocking(ps);
close(*ps);
2005-10-07 06:40:59 +02:00
*ps = SOCKET_INVALID;
}
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
2004-07-01 05:32:09 +02:00
* Select with timeout control
\*-------------------------------------------------------------------------*/
int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
p_timeout tm) {
2004-07-01 05:32:09 +02:00
int ret;
do {
struct timeval tv;
2005-10-07 06:40:59 +02:00
double t = timeout_getretry(tm);
2004-07-01 05:32:09 +02:00
tv.tv_sec = (int) t;
tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
/* timeout = 0 means no wait */
ret = select(n, rfds, wfds, efds, t >= 0.0 ? &tv: NULL);
2004-07-01 05:32:09 +02:00
} while (ret < 0 && errno == EINTR);
return ret;
}
/*-------------------------------------------------------------------------*\
* Creates and sets up a socket
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_create(p_socket ps, int domain, int type, int protocol) {
2004-07-15 08:11:53 +02:00
*ps = socket(domain, type, protocol);
2005-10-07 06:40:59 +02:00
if (*ps != SOCKET_INVALID) return IO_DONE;
2004-07-15 08:11:53 +02:00
else return errno;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_bind(p_socket ps, SA *addr, socklen_t len) {
2004-07-15 08:11:53 +02:00
int err = IO_DONE;
2005-10-07 06:40:59 +02:00
socket_setblocking(ps);
2004-07-15 08:11:53 +02:00
if (bind(*ps, addr, len) < 0) err = errno;
2005-10-07 06:40:59 +02:00
socket_setnonblocking(ps);
return err;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_listen(p_socket ps, int backlog) {
2004-07-15 08:11:53 +02:00
int err = IO_DONE;
2005-10-07 06:40:59 +02:00
socket_setblocking(ps);
2004-07-15 08:11:53 +02:00
if (listen(*ps, backlog)) err = errno;
2005-10-07 06:40:59 +02:00
socket_setnonblocking(ps);
return err;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
void socket_shutdown(p_socket ps, int how) {
socket_setblocking(ps);
shutdown(*ps, how);
2005-10-07 06:40:59 +02:00
socket_setnonblocking(ps);
}
2004-07-15 08:11:53 +02:00
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
2004-07-15 08:11:53 +02:00
int err;
/* avoid calling on closed sockets */
2005-10-07 06:40:59 +02:00
if (*ps == SOCKET_INVALID) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
/* 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 */
2005-10-07 06:40:59 +02:00
if (timeout_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-10-07 06:40:59 +02:00
err = socket_waitfd(ps, WAITFD_C, tm);
2005-09-29 08:11:42 +02:00
if (err == 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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
2004-07-15 08:11:53 +02:00
SA daddr;
socklen_t dlen = sizeof(daddr);
2005-10-07 06:40:59 +02:00
if (*ps == SOCKET_INVALID) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (!addr) addr = &daddr;
if (!len) len = &dlen;
for ( ;; ) {
int err;
2005-10-07 06:40:59 +02:00
if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE;
2004-07-15 08:11:53 +02:00
err = errno;
if (err == EINTR) continue;
if (err != EAGAIN && err != ECONNABORTED) return err;
2005-10-07 06:40:59 +02:00
if ((err = socket_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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_send(p_socket ps, const char *data, size_t count,
size_t *sent, p_timeout tm)
2002-07-08 22:14:09 +02:00
{
2004-07-15 08:11:53 +02:00
int err;
*sent = 0;
/* avoid making system calls on closed sockets */
2005-10-07 06:40:59 +02:00
if (*ps == SOCKET_INVALID) return IO_CLOSED;
2004-07-01 05:32:09 +02:00
/* loop until we send something or we give up on error */
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;
/* EPIPE means the connection was closed */
if (err == EPIPE) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
/* 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 */
2005-10-07 06:40:59 +02:00
if ((err = socket_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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
SA *addr, socklen_t len, p_timeout tm)
2002-07-08 22:14:09 +02:00
{
2004-07-15 08:11:53 +02:00
int err;
*sent = 0;
if (*ps == SOCKET_INVALID) return IO_CLOSED;
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);
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 (err == EPIPE) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (err == EINTR) continue;
if (err != EAGAIN) return err;
2005-10-07 06:40:59 +02:00
if ((err = socket_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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
2004-07-15 08:11:53 +02:00
int err;
*got = 0;
2005-10-07 06:40:59 +02:00
if (*ps == SOCKET_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;
if (taken == 0) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (err == EINTR) continue;
if (err != EAGAIN) return err;
2005-10-07 06:40:59 +02:00
if ((err = socket_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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
SA *addr, socklen_t *len, p_timeout tm) {
2004-07-15 08:11:53 +02:00
int err;
*got = 0;
2005-10-07 06:40:59 +02:00
if (*ps == SOCKET_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;
if (taken == 0) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
if (err == EINTR) continue;
if (err != EAGAIN) return err;
2005-10-07 06:40:59 +02:00
if ((err = socket_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
}
/*-------------------------------------------------------------------------*\
* Write with timeout
*
* socket_read and socket_write are cut-n-paste of socket_send and socket_recv,
* with send/recv replaced with write/read. We can't just use write/read
* in the socket version, because behaviour when size is zero is different.
\*-------------------------------------------------------------------------*/
int socket_write(p_socket ps, const char *data, size_t count,
size_t *sent, p_timeout tm)
{
int err;
*sent = 0;
/* avoid making system calls on closed sockets */
if (*ps == SOCKET_INVALID) return IO_CLOSED;
/* loop until we send something or we give up on error */
for ( ;; ) {
long put = (long) write(*ps, data, count);
/* if we sent anything, we are done */
if (put >= 0) {
*sent = put;
return IO_DONE;
}
err = errno;
/* EPIPE means the connection was closed */
if (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 = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
}
/* can't reach here */
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Read with timeout
* See note for socket_write
\*-------------------------------------------------------------------------*/
int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
int err;
*got = 0;
if (*ps == SOCKET_INVALID) return IO_CLOSED;
for ( ;; ) {
long taken = (long) read(*ps, data, count);
if (taken > 0) {
*got = taken;
return IO_DONE;
}
err = errno;
if (taken == 0) return IO_CLOSED;
if (err == EINTR) continue;
if (err != EAGAIN) return err;
if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
}
return IO_UNKNOWN;
}
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
void socket_setblocking(p_socket ps) {
int flags = fcntl(*ps, F_GETFL, 0);
flags &= (~(O_NONBLOCK));
fcntl(*ps, F_SETFL, flags);
}
/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
void socket_setnonblocking(p_socket 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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
2004-07-15 08:11:53 +02:00
*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
}
2005-10-07 06:40:59 +02:00
int socket_gethostbyname(const char *addr, struct hostent **hp) {
2004-07-15 08:11:53 +02:00
*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
\*-------------------------------------------------------------------------*/
2005-10-07 06:40:59 +02:00
const char *socket_hoststrerror(int err) {
2004-07-15 08:11:53 +02:00
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
}
2005-10-07 06:40:59 +02:00
const char *socket_strerror(int err) {
2004-07-15 08:11:53 +02:00
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 EISCONN: return "already connected";
2004-07-26 06:03:55 +02:00
case EACCES: return "permission denied";
case ECONNREFUSED: return "connection refused";
case ECONNABORTED: return "closed";
case ECONNRESET: return "closed";
2007-10-14 01:55:20 +02:00
case ETIMEDOUT: return "timeout";
default: return strerror(err);
2002-07-08 22:14:09 +02:00
}
}
2005-10-07 06:40:59 +02:00
const char *socket_ioerror(p_socket ps, int err) {
2004-07-01 05:47:22 +02:00
(void) ps;
2005-10-07 06:40:59 +02:00
return socket_strerror(err);
2004-07-15 08:11:53 +02:00
}
2012-04-11 22:21:25 +02:00
const char *socket_gaistrerror(int err) {
if (err == 0) return NULL;
switch (err) {
case EAI_AGAIN: return "temporary failure in name resolution";
case EAI_BADFLAGS: return "invalid value for ai_flags";
#ifdef EAI_BADHINTS
2012-04-11 22:21:25 +02:00
case EAI_BADHINTS: return "invalid value for hints";
#endif
2012-04-11 22:21:25 +02:00
case EAI_FAIL: return "non-recoverable failure in name resolution";
case EAI_FAMILY: return "ai_family not supported";
case EAI_MEMORY: return "memory allocation failure";
case EAI_NONAME:
return "hostname or servname not provided, or not known";
case EAI_OVERFLOW: return "argument buffer overflow";
#ifdef EAI_PROTOCOL
2012-04-11 22:21:25 +02:00
case EAI_PROTOCOL: return "resolved protocol is unknown";
#endif
2012-04-11 22:21:25 +02:00
case EAI_SERVICE: return "servname not supported for socktype";
case EAI_SOCKTYPE: return "ai_socktype not supported";
case EAI_SYSTEM: return strerror(errno);
default: return "unknown error";
}
}