2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* 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
|
|
|
*
|
2003-06-26 20:47:49 +02:00
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2004-07-02 20:44:05 +02: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
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
int sock_open(void) {
|
2004-01-19 16:54:42 +01:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Close and inutilize socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
void sock_destroy(p_sock ps) {
|
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
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-07-01 05:32:09 +02:00
|
|
|
* Select with timeout control
|
2004-01-24 01:18:19 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
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;
|
2004-01-24 01:18:19 +01:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Creates and sets up a socket
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02: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-07-01 05:32:09 +02:00
|
|
|
if (sock == SOCK_INVALID) return sock_strerror();
|
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-07-01 05:32:09 +02:00
|
|
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm) {
|
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 */
|
2004-07-01 05:32:09 +02:00
|
|
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
2004-01-21 19:40:52 +01:00
|
|
|
/* 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-07-01 05:32:09 +02:00
|
|
|
if (errno != EINPROGRESS) return sock_strerror();
|
|
|
|
/* optimize for timeout = 0 */
|
|
|
|
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
|
2004-01-21 19:40:52 +01:00
|
|
|
/* wait for a timeout or for the system's answer */
|
|
|
|
for ( ;; ) {
|
2004-07-01 05:32:09 +02:00
|
|
|
fd_set rfds, wfds;
|
2004-01-18 01:04:20 +01:00
|
|
|
FD_ZERO(&rfds); FD_SET(sock, &rfds);
|
|
|
|
FD_ZERO(&wfds); FD_SET(sock, &wfds);
|
|
|
|
/* we run select to avoid busy waiting */
|
2004-07-01 05:32:09 +02:00
|
|
|
err = sock_select(sock+1, &rfds, &wfds, NULL, tm);
|
|
|
|
/* if there was an event, check what happened */
|
2004-01-18 01:04:20 +01:00
|
|
|
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-07-01 05:32:09 +02:00
|
|
|
if (err > 1 && FD_ISSET(sock, &rfds) &&
|
|
|
|
recv(sock, &dummy, 0, 0) < 0 && errno != EAGAIN)
|
|
|
|
return sock_strerror();
|
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-07-01 05:32:09 +02:00
|
|
|
} else if (err == 0) return io_strerror(IO_TIMEOUT);
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
2004-07-01 05:32:09 +02:00
|
|
|
return sock_strerror();
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Binds or returns error message
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len) {
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
2004-07-01 05:32:09 +02:00
|
|
|
if (bind(*ps, addr, addr_len) < 0) err = sock_strerror();
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
const char* sock_listen(p_sock ps, int backlog) {
|
2004-01-24 01:18:19 +01:00
|
|
|
const char *err = NULL;
|
|
|
|
sock_setblocking(ps);
|
2004-07-01 05:32:09 +02:00
|
|
|
if (listen(*ps, backlog)) err = sock_strerror();
|
2004-01-24 01:18:19 +01:00
|
|
|
sock_setnonblocking(ps);
|
|
|
|
return err;
|
2003-05-25 03:54:13 +02:00
|
|
|
}
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
*
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02: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,
|
2004-07-01 05:32:09 +02:00
|
|
|
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-07-01 05:32:09 +02: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-07-01 05:32:09 +02:00
|
|
|
/* if connection was aborted, we can try again if we have time */
|
|
|
|
if (errno != EAGAIN && errno != ECONNABORTED) return sock_strerror();
|
|
|
|
/* optimize for timeout = 0 case */
|
|
|
|
if (tm_get(tm) == 0.0) return io_strerror(IO_TIMEOUT);
|
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-07-01 05:32:09 +02:00
|
|
|
err = sock_select(sock+1, &fds, NULL, NULL, tm);
|
2004-01-24 01:18:19 +01:00
|
|
|
if (err == 0) return io_strerror(IO_TIMEOUT);
|
2004-07-02 20:44:05 +02:00
|
|
|
else if (err < 0) break;
|
2004-01-21 19:40:52 +01:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return sock_strerror();
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +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
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-01-16 08:06:31 +01:00
|
|
|
/* avoid making system calls on closed sockets */
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_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-06-23 03:08:54 +02:00
|
|
|
int ret;
|
2004-01-18 01:04:20 +01:00
|
|
|
fd_set fds;
|
2004-07-01 05:32:09 +02:00
|
|
|
ssize_t put;
|
|
|
|
/* make sure we repeat in case the call was interrupted */
|
|
|
|
do put = send(sock, data, count, 0);
|
|
|
|
while (put < 0 && errno == EINTR);
|
|
|
|
/* if we sent something, get out */
|
|
|
|
if (put > 0) {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
|
|
|
/* deal with failure */
|
2002-07-08 22:14:09 +02:00
|
|
|
*sent = 0;
|
2004-01-18 01:04:20 +01:00
|
|
|
/* here we know the connection has been closed */
|
2004-07-01 05:32:09 +02:00
|
|
|
if (put < 0 && errno == EPIPE) return IO_CLOSED;
|
|
|
|
/* send shouldn't return zero and we can only proceed if
|
|
|
|
* there was no serious error */
|
|
|
|
if (put == 0 || errno != EAGAIN) return IO_USER;
|
|
|
|
/* optimize for the timeout = 0 case */
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
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-07-01 05:32:09 +02:00
|
|
|
ret = sock_select(sock+1, NULL, &fds, NULL, tm);
|
2004-06-23 03:08:54 +02:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-07-02 20:44:05 +02:00
|
|
|
else if (ret < 0) break;
|
2004-07-01 05:32:09 +02:00
|
|
|
/* otherwise, try sending again */
|
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_USER;
|
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,
|
2004-07-01 05:32:09 +02:00
|
|
|
SA *addr, socklen_t addr_len, p_tm tm)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2004-07-01 05:32:09 +02:00
|
|
|
/* avoid making system calls on closed sockets */
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_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-06-23 03:08:54 +02:00
|
|
|
int ret;
|
2004-01-18 01:04:20 +01:00
|
|
|
fd_set fds;
|
2004-07-01 05:32:09 +02:00
|
|
|
ssize_t put;
|
|
|
|
do put = sendto(sock, data, count, 0, addr, addr_len);
|
|
|
|
while (put < 0 && errno == EINTR);
|
|
|
|
if (put > 0) {
|
|
|
|
*sent = put;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
2002-07-08 22:14:09 +02:00
|
|
|
*sent = 0;
|
2004-07-01 05:32:09 +02:00
|
|
|
if (put < 0 && errno == EPIPE) return IO_CLOSED;
|
|
|
|
if (put == 0 || errno != EAGAIN) return IO_USER;
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-18 01:04:20 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-07-01 05:32:09 +02:00
|
|
|
ret = sock_select(sock+1, NULL, &fds, NULL, tm);
|
2004-06-23 03:08:54 +02:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-07-02 20:44:05 +02:00
|
|
|
else if (ret < 0) break;
|
2004-07-01 05:32:09 +02:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_USER;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +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) {
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-01 05:32:09 +02:00
|
|
|
for ( ;; ) {
|
2004-01-16 08:06:31 +01:00
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2004-07-01 05:32:09 +02:00
|
|
|
ssize_t taken;
|
|
|
|
do taken = read(sock, data, count);
|
|
|
|
while (taken < 0 && errno == EINTR);
|
|
|
|
if (taken > 0) {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
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-07-01 05:32:09 +02:00
|
|
|
if (errno != EAGAIN) return IO_USER;
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-16 08:06:31 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-07-01 05:32:09 +02:00
|
|
|
ret = sock_select(sock+1, &fds, NULL, NULL, tm);
|
2004-01-18 01:04:20 +01:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-07-02 20:44:05 +02:00
|
|
|
else if (ret < 0) break;
|
2004-07-01 05:32:09 +02:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_USER;
|
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,
|
2004-07-01 05:32:09 +02:00
|
|
|
SA *addr, socklen_t *addr_len, p_tm tm) {
|
2003-05-25 03:54:13 +02:00
|
|
|
t_sock sock = *ps;
|
2003-11-27 01:30:54 +01:00
|
|
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
2004-07-01 05:32:09 +02:00
|
|
|
for ( ;; ) {
|
2004-01-17 01:17:46 +01:00
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
2004-07-01 05:32:09 +02:00
|
|
|
ssize_t taken;
|
|
|
|
do taken = recvfrom(sock, data, count, 0, addr, addr_len);
|
|
|
|
while (taken < 0 && errno == EINTR);
|
|
|
|
if (taken > 0) {
|
|
|
|
*got = taken;
|
|
|
|
return IO_DONE;
|
|
|
|
}
|
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-07-01 05:32:09 +02:00
|
|
|
if (errno != EAGAIN) return IO_USER;
|
|
|
|
if (tm_get(tm) == 0.0) return IO_TIMEOUT;
|
2004-01-17 01:17:46 +01:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sock, &fds);
|
2004-07-01 05:32:09 +02:00
|
|
|
ret = sock_select(sock+1, &fds, NULL, NULL, tm);
|
2004-01-18 01:04:20 +01:00
|
|
|
if (ret == 0) return IO_TIMEOUT;
|
2004-07-02 20:44:05 +02:00
|
|
|
else if (ret < 0) break;
|
2004-07-01 05:32:09 +02:00
|
|
|
}
|
2004-07-02 20:44:05 +02:00
|
|
|
return IO_USER;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Put socket into blocking mode
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
void sock_setblocking(p_sock ps) {
|
2003-06-26 20:47:49 +02:00
|
|
|
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) {
|
2003-06-26 20:47:49 +02:00
|
|
|
int flags = fcntl(*ps, F_GETFL, 0);
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
fcntl(*ps, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Error translation functions
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
const char *sock_hoststrerror(void) {
|
2004-07-02 20:44:05 +02:00
|
|
|
switch (h_errno) {
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
return "host not found";
|
|
|
|
default:
|
|
|
|
return hstrerror(h_errno);
|
|
|
|
}
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2004-07-01 05:32:09 +02:00
|
|
|
/* make sure important error messages are standard */
|
|
|
|
const char *sock_strerror(void) {
|
|
|
|
switch (errno) {
|
|
|
|
case EADDRINUSE:
|
|
|
|
return "address already in use";
|
|
|
|
default:
|
|
|
|
return strerror(errno);
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-01 05:32:09 +02:00
|
|
|
const char *sock_geterr(p_sock ps, int code) {
|
2004-07-01 05:47:22 +02:00
|
|
|
(void) ps;
|
|
|
|
(void) code;
|
2004-07-01 05:32:09 +02:00
|
|
|
return sock_strerror();
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|