luasocket/src/usocket.c

348 lines
11 KiB
C
Raw Normal View History

2002-07-08 22:14:09 +02:00
/*=========================================================================*\
2003-05-25 03:54:13 +02:00
* Socket compatibilization module for Unix
2003-03-28 22:08:50 +01:00
*
* RCS ID: $Id$
2002-07-08 22:14:09 +02:00
\*=========================================================================*/
#include <lua.h>
#include <lauxlib.h>
2003-03-28 22:08:50 +01:00
#include <string.h>
2002-07-08 22:14:09 +02:00
2003-05-25 03:54:13 +02:00
#include "sock.h"
2002-07-08 22:14:09 +02:00
/*=========================================================================*\
* Internal function prototypes
\*=========================================================================*/
2003-05-25 03:54:13 +02:00
static const char *try_setoption(lua_State *L, p_sock ps);
static const char *try_setbooloption(lua_State *L, p_sock ps, int name);
2002-07-08 22:14:09 +02:00
/*=========================================================================*\
* Exported functions.
\*=========================================================================*/
2003-05-25 03:54:13 +02:00
int sock_open(lua_State *L)
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;
}
const char *sock_connect(p_sock ps, SA *addr, size_t addr_len)
{
if (connect(*ps, addr, addr_len) < 0) return sock_connectstrerror();
else return NULL;
}
const char *sock_bind(p_sock ps, SA *addr, size_t addr_len)
{
if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror();
else return NULL;
}
void sock_listen(p_sock ps, int backlog)
{
listen(*ps, backlog);
}
void sock_accept(p_sock ps, p_sock pa, SA *addr, size_t *addr_len, int timeout)
{
t_sock sock = *ps;
2002-07-08 22:14:09 +02:00
struct timeval tv;
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);
select(sock+1, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL);
*pa = accept(sock, addr, addr_len);
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,
SA *addr, size_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,
SA *addr, size_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
}
}
/*-------------------------------------------------------------------------*\
* Returns a string describing the last host manipulation error.
\*-------------------------------------------------------------------------*/
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";
}
}
/*-------------------------------------------------------------------------*\
* Returns a string describing the last socket manipulation 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";
}
}
/*-------------------------------------------------------------------------*\
* Returns a string describing the last bind command 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";
}
}
/*-------------------------------------------------------------------------*\
* Returns a string describing the last connect 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";
}
}
/*-------------------------------------------------------------------------*\
* Sets the SO_REUSEADDR socket option
* Input
* sock: socket descriptor
\*-------------------------------------------------------------------------*/
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
}
/*-------------------------------------------------------------------------*\
* Put socket into blocking mode.
\*-------------------------------------------------------------------------*/
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
}
/*-------------------------------------------------------------------------*\
* Put socket into non-blocking mode.
\*-------------------------------------------------------------------------*/
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
}
/*-------------------------------------------------------------------------*\
* Tries to set extended udp socket options
* Input
* udp: udp structure
* oldtop: top of stack
* Returns
* NULL if successfull, error message on error
\*-------------------------------------------------------------------------*/
2003-05-25 03:54:13 +02:00
const char *sock_trysetoptions(lua_State *L, p_sock ps)
2002-07-08 22:14:09 +02:00
{
if (!lua_istable(L, 1)) luaL_argerror(L, 1, "invalid options table");
lua_pushnil(L);
while (lua_next(L, 1)) {
2003-05-25 03:54:13 +02:00
const char *err = try_setoption(L, ps);
2002-07-08 22:14:09 +02:00
lua_pop(L, 1);
if (err) return err;
}
return NULL;
}
/*-------------------------------------------------------------------------*\
* Set socket options from a table on top of Lua stack.
2003-05-25 03:54:13 +02:00
* Supports SO_KEEPALIVE, SO_DONTROUTE, and SO_BROADCAST options.
2002-07-08 22:14:09 +02:00
* Input
2003-05-25 03:54:13 +02:00
* sock: socket
2002-07-08 22:14:09 +02:00
* Returns
* 1 if successful, 0 otherwise
\*-------------------------------------------------------------------------*/
2003-05-25 03:54:13 +02:00
static const char *try_setoption(lua_State *L, p_sock ps)
2002-07-08 22:14:09 +02:00
{
2003-05-25 03:54:13 +02:00
static const char *options[] = {
"SO_KEEPALIVE", "SO_DONTROUTE", "SO_BROADCAST", NULL
2002-07-08 22:14:09 +02:00
};
2003-05-25 03:54:13 +02:00
const char *option = lua_tostring(L, -2);
2002-07-08 22:14:09 +02:00
if (!lua_isstring(L, -2)) return "invalid option";
switch (luaL_findstring(option, options)) {
2003-05-25 03:54:13 +02:00
case 0: return try_setbooloption(L, ps, SO_KEEPALIVE);
case 1: return try_setbooloption(L, ps, SO_DONTROUTE);
case 2: return try_setbooloption(L, ps, SO_BROADCAST);
2002-07-08 22:14:09 +02:00
default: return "unsupported option";
}
}
2003-05-25 03:54:13 +02:00
/*=========================================================================*\
* Internal functions.
\*=========================================================================*/
static const char *try_setbooloption(lua_State *L, p_sock ps, int name)
{
int bool, res;
if (!lua_isnumber(L, -1)) luaL_error(L, "invalid option value");
bool = (int) lua_tonumber(L, -1);
res = setsockopt(*ps, SOL_SOCKET, name, (char *) &bool, sizeof(bool));
if (res < 0) return "error setting option";
else return NULL;
}