2003-03-28 22:08:50 +01:00
|
|
|
#include <string.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-09 20:23:40 +02:00
|
|
|
int sock_open(void)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
/* instals a handler to ignore sigpipe. */
|
2002-07-08 22:14:09 +02:00
|
|
|
struct sigaction new;
|
|
|
|
memset(&new, 0, sizeof(new));
|
|
|
|
new.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &new, NULL);
|
2003-03-21 02:07:23 +01:00
|
|
|
return 1;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
void sock_destroy(p_sock ps)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
close(*ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *sock_create(p_sock ps, int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
t_sock sock = socket(domain, type, protocol);
|
|
|
|
if (sock == SOCK_INVALID) return sock_createstrerror();
|
|
|
|
*ps = sock;
|
|
|
|
sock_setnonblocking(ps);
|
|
|
|
sock_setreuseaddr(ps);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len)
|
2003-05-25 03:54:13 +02:00
|
|
|
{
|
|
|
|
if (connect(*ps, addr, addr_len) < 0) return sock_connectstrerror();
|
|
|
|
else return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
const char *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 sock_bindstrerror();
|
|
|
|
else return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sock_listen(p_sock ps, int backlog)
|
|
|
|
{
|
|
|
|
listen(*ps, backlog);
|
|
|
|
}
|
|
|
|
|
2003-06-09 20:23:40 +02:00
|
|
|
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;
|
2002-07-08 22:14:09 +02:00
|
|
|
struct timeval tv;
|
2003-06-09 20:23:40 +02:00
|
|
|
SA dummy_addr;
|
|
|
|
socklen_t dummy_len;
|
2002-07-08 22:14:09 +02:00
|
|
|
fd_set fds;
|
2003-05-25 03:54:13 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2002-07-08 22:14:09 +02:00
|
|
|
FD_ZERO(&fds);
|
2003-05-25 03:54:13 +02:00
|
|
|
FD_SET(sock, &fds);
|
2003-06-09 20:23:40 +02:00
|
|
|
if (select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL) <= 0)
|
|
|
|
return IO_TIMEOUT;
|
|
|
|
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);
|
2003-06-09 20:23:40 +02:00
|
|
|
if (*pa == SOCK_INVALID) return IO_ERROR;
|
|
|
|
else return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2002-07-08 22:14:09 +02:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fds;
|
|
|
|
ssize_t put = 0;
|
|
|
|
int err;
|
|
|
|
int ret;
|
2003-05-25 03:54:13 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2002-07-08 22:14:09 +02:00
|
|
|
FD_ZERO(&fds);
|
2003-05-25 03:54:13 +02:00
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (ret > 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
put = write(sock, data, count);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (put <= 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
err = IO_CLOSED;
|
2002-07-08 22:14:09 +02:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
/* this is for CYGWIN, which is like Unix but has Win32 bugs */
|
2003-05-25 03:54:13 +02:00
|
|
|
if (errno == EWOULDBLOCK) err = IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
#endif
|
|
|
|
*sent = 0;
|
|
|
|
} else {
|
|
|
|
*sent = put;
|
2003-05-25 03:54:13 +02:00
|
|
|
err = IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
} else {
|
|
|
|
*sent = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_TIMEOUT;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
|
2003-06-09 20:23:40 +02:00
|
|
|
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;
|
2002-07-08 22:14:09 +02:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fds;
|
|
|
|
ssize_t put = 0;
|
|
|
|
int err;
|
|
|
|
int ret;
|
2003-05-25 03:54:13 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2002-07-08 22:14:09 +02:00
|
|
|
FD_ZERO(&fds);
|
2003-05-25 03:54:13 +02:00
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = select(sock+1, NULL, &fds, NULL, timeout >= 0 ? &tv : NULL);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (ret > 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
put = sendto(sock, data, count, 0, addr, addr_len);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (put <= 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
err = IO_CLOSED;
|
2002-07-08 22:14:09 +02:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
/* this is for CYGWIN, which is like Unix but has Win32 bugs */
|
2003-05-25 03:54:13 +02:00
|
|
|
if (sent < 0 && errno == EWOULDBLOCK) err = IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
#endif
|
|
|
|
*sent = 0;
|
|
|
|
} else {
|
|
|
|
*sent = put;
|
2003-05-25 03:54:13 +02:00
|
|
|
err = IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
} else {
|
|
|
|
*sent = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_TIMEOUT;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2002-07-08 22:14:09 +02:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
|
|
|
ssize_t taken = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2002-07-08 22:14:09 +02:00
|
|
|
FD_ZERO(&fds);
|
2003-05-25 03:54:13 +02:00
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (ret > 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
taken = read(sock, data, count);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (taken <= 0) {
|
|
|
|
*got = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_CLOSED;
|
2002-07-08 22:14:09 +02:00
|
|
|
} else {
|
|
|
|
*got = taken;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*got = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_TIMEOUT;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
|
2003-06-09 20:23:40 +02:00
|
|
|
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;
|
2002-07-08 22:14:09 +02:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fds;
|
|
|
|
int ret;
|
|
|
|
ssize_t taken = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2002-07-08 22:14:09 +02:00
|
|
|
FD_ZERO(&fds);
|
2003-05-25 03:54:13 +02:00
|
|
|
FD_SET(sock, &fds);
|
|
|
|
ret = select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (ret > 0) {
|
2003-05-25 03:54:13 +02:00
|
|
|
taken = recvfrom(sock, data, count, 0, addr, addr_len);
|
2002-07-08 22:14:09 +02:00
|
|
|
if (taken <= 0) {
|
|
|
|
*got = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_CLOSED;
|
2002-07-08 22:14:09 +02:00
|
|
|
} else {
|
|
|
|
*got = taken;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_DONE;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*got = 0;
|
2003-05-25 03:54:13 +02:00
|
|
|
return IO_TIMEOUT;
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
void sock_setreuseaddr(p_sock ps)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
|
|
|
int val = 1;
|
2003-05-25 03:54:13 +02:00
|
|
|
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
void sock_setblocking(p_sock ps)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
int flags = fcntl(*ps, F_GETFL, 0);
|
2002-07-08 22:14:09 +02:00
|
|
|
flags &= (~(O_NONBLOCK));
|
2003-05-25 03:54:13 +02:00
|
|
|
fcntl(*ps, F_SETFL, flags);
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
void sock_setnonblocking(p_sock ps)
|
2002-07-08 22:14:09 +02:00
|
|
|
{
|
2003-05-25 03:54:13 +02:00
|
|
|
int flags = fcntl(*ps, F_GETFL, 0);
|
2002-07-08 22:14:09 +02:00
|
|
|
flags |= O_NONBLOCK;
|
2003-05-25 03:54:13 +02:00
|
|
|
fcntl(*ps, F_SETFL, flags);
|
2002-07-08 22:14:09 +02:00
|
|
|
}
|