luasocket/src/usocket.c

455 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.
\*=========================================================================*/
2019-02-26 00:04:16 +01:00
#include "luasocket.h"
2002-07-08 22:14:09 +02:00
#include "socket.h"
#include "pierror.h"
2002-07-08 22:14:09 +02:00
2019-02-26 00:04:16 +01:00
#include <string.h>
#include <signal.h>
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)
2019-02-28 05:02:24 +01: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)
2019-02-28 05:02:24 +01: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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_open(void) {
2017-04-17 17:47:48 +02:00
/* installs 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-05-30 23:36:22 +02:00
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_close(void) {
2004-05-30 23:36:22 +02:00
return 1;
}
/*-------------------------------------------------------------------------*\
* Close and inutilize socket
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
void socket_destroy(p_socket ps) {
2005-10-07 06:40:59 +02:00
if (*ps != SOCKET_INVALID) {
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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);
if (*ps != SOCKET_INVALID) return IO_DONE;
else return errno;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
* Binds or returns error message
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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);
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
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_listen(p_socket ps, int backlog) {
int err = IO_DONE;
if (listen(*ps, backlog)) err = errno;
return err;
2003-05-25 03:54:13 +02:00
}
/*-------------------------------------------------------------------------*\
*
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
void socket_shutdown(p_socket ps, int how) {
shutdown(*ps, how);
}
2004-07-15 08:11:53 +02:00
/*-------------------------------------------------------------------------*\
* Connects or returns error message
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
if (*ps == SOCKET_INVALID) return IO_CLOSED;
2004-07-15 08:11:53 +02:00
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_send(p_socket ps, const char *data, size_t count,
2005-10-07 06:40:59 +02:00
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;
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
if (err == EPROTOTYPE) continue;
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
2005-10-07 06:40:59 +02:00
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 ( ;; ) {
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;
if (err == EPROTOTYPE) continue;
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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;
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
2005-10-07 06:40:59 +02:00
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;
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.
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
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;
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
if (err == EPROTOTYPE) continue;
/* 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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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
2004-07-15 08:11:53 +02:00
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01: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
}
2019-02-28 05:02:24 +01: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
\*-------------------------------------------------------------------------*/
2019-02-28 05:02:24 +01:00
const char *socket_hoststrerror(int err) {
2004-07-15 08:11:53 +02:00
if (err <= 0) return io_strerror(err);
switch (err) {
case HOST_NOT_FOUND: return PIE_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
}
2019-02-28 05:02:24 +01:00
const char *socket_strerror(int err) {
2004-07-15 08:11:53 +02:00
if (err <= 0) return io_strerror(err);
switch (err) {
case EADDRINUSE: return PIE_ADDRINUSE;
case EISCONN: return PIE_ISCONN;
case EACCES: return PIE_ACCESS;
case ECONNREFUSED: return PIE_CONNREFUSED;
case ECONNABORTED: return PIE_CONNABORTED;
case ECONNRESET: return PIE_CONNRESET;
case ETIMEDOUT: return PIE_TIMEDOUT;
default: {
return strerror(err);
}
2002-07-08 22:14:09 +02:00
}
}
2019-02-28 05:02:24 +01: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);
}
2012-04-11 22:21:25 +02:00
2019-02-28 05:02:24 +01:00
const char *socket_gaistrerror(int err) {
if (err == 0) return NULL;
2012-04-11 22:21:25 +02:00
switch (err) {
case EAI_AGAIN: return PIE_AGAIN;
case EAI_BADFLAGS: return PIE_BADFLAGS;
#ifdef EAI_BADHINTS
case EAI_BADHINTS: return PIE_BADHINTS;
#endif
case EAI_FAIL: return PIE_FAIL;
case EAI_FAMILY: return PIE_FAMILY;
case EAI_MEMORY: return PIE_MEMORY;
case EAI_NONAME: return PIE_NONAME;
#ifdef EAI_OVERFLOW
case EAI_OVERFLOW: return PIE_OVERFLOW;
#endif
#ifdef EAI_PROTOCOL
case EAI_PROTOCOL: return PIE_PROTOCOL;
#endif
case EAI_SERVICE: return PIE_SERVICE;
case EAI_SOCKTYPE: return PIE_SOCKTYPE;
case EAI_SYSTEM: return strerror(errno);
default: return gai_strerror(err);
2012-04-11 22:21:25 +02:00
}
}