From f4dadea763c1959a27dead24df3ee6c54c209842 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Fri, 7 Oct 2005 04:40:59 +0000 Subject: [PATCH] Before compiling on Windows. --- TODO | 1 + config | 20 ++++----- src/auxiliar.c | 32 +++++-------- src/auxiliar.h | 26 +++++------ src/buffer.c | 74 +++++++++++++++--------------- src/buffer.h | 22 ++++----- src/inet.c | 40 ++++++++--------- src/inet.h | 12 ++--- src/io.h | 4 +- src/ltn12.lua | 52 +++++++++++++-------- src/luasocket.c | 10 ++--- src/options.c | 36 +++++++-------- src/options.h | 26 +++++------ src/select.c | 8 ++-- src/socket.h | 56 ++++++++++++----------- src/tcp.c | 114 +++++++++++++++++++++++------------------------ src/tcp.h | 6 +-- src/timeout.c | 44 +++++++++--------- src/timeout.h | 30 ++++++------- src/udp.c | 88 ++++++++++++++++++------------------ src/udp.h | 4 +- src/usocket.c | 113 +++++++++++++++++++++++----------------------- src/usocket.h | 6 +-- src/wsocket.c | 95 ++++++++++++++++++++------------------- src/wsocket.h | 6 +-- test/ftptest.lua | 12 ++--- 26 files changed, 475 insertions(+), 462 deletions(-) diff --git a/TODO b/TODO index a405fbf..9586e23 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,4 @@ + fix unix.c to return just a function get rid of setmetatable(, nil) since packages don't need this anymore in 5.1 diff --git a/config b/config index 175a885..255477b 100644 --- a/config +++ b/config @@ -37,20 +37,20 @@ INSTALL_EXEC=cp # Compiler and linker settings # for Mac OS X # -#CC=gcc -#DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN -#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common -#LDFLAGS=-bundle -undefined dynamic_lookup -#LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc +CC=gcc +DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN +CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common +LDFLAGS=-bundle -undefined dynamic_lookup +LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc #------ # Compiler and linker settings # for Linux -CC=gcc -DEF=-DLUASOCKET_DEBUG -CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic -LDFLAGS=-O -shared -LD=gcc +#CC=gcc +#DEF=-DLUASOCKET_DEBUG +#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic +#LDFLAGS=-O -shared +#LD=gcc #------ # End of makefile configuration diff --git a/src/auxiliar.c b/src/auxiliar.c index b228785..fcc7e23 100644 --- a/src/auxiliar.c +++ b/src/auxiliar.c @@ -15,7 +15,7 @@ /*-------------------------------------------------------------------------*\ * Initializes the module \*-------------------------------------------------------------------------*/ -int aux_open(lua_State *L) { +int auxiliar_open(lua_State *L) { (void) L; return 0; } @@ -24,7 +24,7 @@ int aux_open(lua_State *L) { * Creates a new class with given methods * Methods whose names start with __ are passed directly to the metatable. \*-------------------------------------------------------------------------*/ -void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) { +void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func) { luaL_newmetatable(L, classname); /* mt */ /* create __index table to place methods */ lua_pushstring(L, "__index"); /* mt,"__index" */ @@ -47,7 +47,7 @@ void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) { /*-------------------------------------------------------------------------*\ * Prints the value of a class in a nice way \*-------------------------------------------------------------------------*/ -int aux_tostring(lua_State *L) { +int auxiliar_tostring(lua_State *L) { char buf[32]; if (!lua_getmetatable(L, 1)) goto error; lua_pushstring(L, "__index"); @@ -68,7 +68,7 @@ error: /*-------------------------------------------------------------------------*\ * Insert class into group \*-------------------------------------------------------------------------*/ -void aux_add2group(lua_State *L, const char *classname, const char *groupname) { +void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) { luaL_getmetatable(L, classname); lua_pushstring(L, groupname); lua_pushboolean(L, 1); @@ -79,7 +79,7 @@ void aux_add2group(lua_State *L, const char *classname, const char *groupname) { /*-------------------------------------------------------------------------*\ * Make sure argument is a boolean \*-------------------------------------------------------------------------*/ -int aux_checkboolean(lua_State *L, int objidx) { +int auxiliar_checkboolean(lua_State *L, int objidx) { if (!lua_isboolean(L, objidx)) luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); return lua_toboolean(L, objidx); @@ -89,8 +89,8 @@ int aux_checkboolean(lua_State *L, int objidx) { * Return userdata pointer if object belongs to a given class, abort with * error otherwise \*-------------------------------------------------------------------------*/ -void *aux_checkclass(lua_State *L, const char *classname, int objidx) { - void *data = aux_getclassudata(L, classname, objidx); +void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { + void *data = auxiliar_getclassudata(L, classname, objidx); if (!data) { char msg[45]; sprintf(msg, "%.35s expected", classname); @@ -103,8 +103,8 @@ void *aux_checkclass(lua_State *L, const char *classname, int objidx) { * Return userdata pointer if object belongs to a given group, abort with * error otherwise \*-------------------------------------------------------------------------*/ -void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) { - void *data = aux_getgroupudata(L, groupname, objidx); +void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { + void *data = auxiliar_getgroupudata(L, groupname, objidx); if (!data) { char msg[45]; sprintf(msg, "%.35s expected", groupname); @@ -116,7 +116,7 @@ void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) { /*-------------------------------------------------------------------------*\ * Set object class \*-------------------------------------------------------------------------*/ -void aux_setclass(lua_State *L, const char *classname, int objidx) { +void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { luaL_getmetatable(L, classname); if (objidx < 0) objidx--; lua_setmetatable(L, objidx); @@ -126,10 +126,7 @@ void aux_setclass(lua_State *L, const char *classname, int objidx) { * Get a userdata pointer if object belongs to a given group. Return NULL * otherwise \*-------------------------------------------------------------------------*/ -void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) { -#if 0 - return lua_touserdata(L, objidx); -#else +void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { if (!lua_getmetatable(L, objidx)) return NULL; lua_pushstring(L, groupname); @@ -141,17 +138,12 @@ void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) { lua_pop(L, 2); return lua_touserdata(L, objidx); } -#endif } /*-------------------------------------------------------------------------*\ * Get a userdata pointer if object belongs to a given class. Return NULL * otherwise \*-------------------------------------------------------------------------*/ -void *aux_getclassudata(lua_State *L, const char *classname, int objidx) { -#if 0 - return lua_touserdata(L, objidx); -#else +void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { return luaL_checkudata(L, objidx, classname); -#endif } diff --git a/src/auxiliar.h b/src/auxiliar.h index ff20b50..8a18bcf 100644 --- a/src/auxiliar.h +++ b/src/auxiliar.h @@ -1,5 +1,5 @@ -#ifndef AUX_H -#define AUX_H +#ifndef AUXILIAR_H +#define AUXILIAR_H /*=========================================================================*\ * Auxiliar routines for class hierarchy manipulation * LuaSocket toolkit (but completely independent of other LuaSocket modules) @@ -34,15 +34,15 @@ #include "lua.h" #include "lauxlib.h" -int aux_open(lua_State *L); -void aux_newclass(lua_State *L, const char *classname, luaL_reg *func); -void aux_add2group(lua_State *L, const char *classname, const char *group); -void aux_setclass(lua_State *L, const char *classname, int objidx); -void *aux_checkclass(lua_State *L, const char *classname, int objidx); -void *aux_checkgroup(lua_State *L, const char *groupname, int objidx); -void *aux_getclassudata(lua_State *L, const char *groupname, int objidx); -void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx); -int aux_checkboolean(lua_State *L, int objidx); -int aux_tostring(lua_State *L); +int auxiliar_open(lua_State *L); +void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func); +void auxiliar_add2group(lua_State *L, const char *classname, const char *group); +void auxiliar_setclass(lua_State *L, const char *classname, int objidx); +void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx); +void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx); +void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx); +void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx); +int auxiliar_checkboolean(lua_State *L, int objidx); +int auxiliar_tostring(lua_State *L); -#endif /* AUX_H */ +#endif /* AUXILIAR_H */ diff --git a/src/buffer.c b/src/buffer.c index b69a9b8..df1a0bc 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -12,12 +12,12 @@ /*=========================================================================*\ * Internal function prototypes \*=========================================================================*/ -static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b); -static int recvline(p_buf buf, luaL_Buffer *b); -static int recvall(p_buf buf, luaL_Buffer *b); -static int buf_get(p_buf buf, const char **data, size_t *count); -static void buf_skip(p_buf buf, size_t count); -static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent); +static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b); +static int recvline(p_buffer buf, luaL_Buffer *b); +static int recvall(p_buffer buf, luaL_Buffer *b); +static int buffer_get(p_buffer buf, const char **data, size_t *count); +static void buffer_skip(p_buffer buf, size_t count); +static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent); /* min and max macros */ #ifndef MIN @@ -33,7 +33,7 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent); /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ -int buf_open(lua_State *L) { +int buffer_open(lua_State *L) { (void) L; return 0; } @@ -41,31 +41,31 @@ int buf_open(lua_State *L) { /*-------------------------------------------------------------------------*\ * Initializes C structure \*-------------------------------------------------------------------------*/ -void buf_init(p_buf buf, p_io io, p_tm tm) { +void buffer_init(p_buffer buf, p_io io, p_timeout tm) { buf->first = buf->last = 0; buf->io = io; buf->tm = tm; buf->received = buf->sent = 0; - buf->birthday = tm_gettime(); + buf->birthday = timeout_gettime(); } /*-------------------------------------------------------------------------*\ * object:getstats() interface \*-------------------------------------------------------------------------*/ -int buf_meth_getstats(lua_State *L, p_buf buf) { +int buffer_meth_getstats(lua_State *L, p_buffer buf) { lua_pushnumber(L, buf->received); lua_pushnumber(L, buf->sent); - lua_pushnumber(L, tm_gettime() - buf->birthday); + lua_pushnumber(L, timeout_gettime() - buf->birthday); return 3; } /*-------------------------------------------------------------------------*\ * object:setstats() interface \*-------------------------------------------------------------------------*/ -int buf_meth_setstats(lua_State *L, p_buf buf) { +int buffer_meth_setstats(lua_State *L, p_buffer buf) { buf->received = (long) luaL_optnumber(L, 2, buf->received); buf->sent = (long) luaL_optnumber(L, 3, buf->sent); - if (lua_isnumber(L, 4)) buf->birthday = tm_gettime() - lua_tonumber(L, 4); + if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); lua_pushnumber(L, 1); return 1; } @@ -73,9 +73,9 @@ int buf_meth_setstats(lua_State *L, p_buf buf) { /*-------------------------------------------------------------------------*\ * object:send() interface \*-------------------------------------------------------------------------*/ -int buf_meth_send(lua_State *L, p_buf buf) { +int buffer_meth_send(lua_State *L, p_buffer buf) { int top = lua_gettop(L); - p_tm tm = tm_markstart(buf->tm); + p_timeout tm = timeout_markstart(buf->tm); int err = IO_DONE; size_t size = 0, sent = 0; const char *data = luaL_checklstring(L, 2, &size); @@ -98,7 +98,7 @@ int buf_meth_send(lua_State *L, p_buf buf) { } #ifdef LUASOCKET_DEBUG /* push time elapsed during operation as the last return value */ - lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); + lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); #endif return lua_gettop(L) - top; } @@ -106,9 +106,9 @@ int buf_meth_send(lua_State *L, p_buf buf) { /*-------------------------------------------------------------------------*\ * object:receive() interface \*-------------------------------------------------------------------------*/ -int buf_meth_receive(lua_State *L, p_buf buf) { +int buffer_meth_receive(lua_State *L, p_buffer buf) { int err = IO_DONE, top = lua_gettop(L); - p_tm tm = tm_markstart(buf->tm); + p_timeout tm = timeout_markstart(buf->tm); luaL_Buffer b; size_t size; const char *part = luaL_optlstring(L, 3, "", &size); @@ -141,7 +141,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) { } #ifdef LUASOCKET_DEBUG /* push time elapsed during operation as the last return value */ - lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); + lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); #endif return lua_gettop(L) - top; } @@ -149,7 +149,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) { /*-------------------------------------------------------------------------*\ * Determines if there is any data in the read buffer \*-------------------------------------------------------------------------*/ -int buf_isempty(p_buf buf) { +int buffer_isempty(p_buffer buf) { return buf->first >= buf->last; } @@ -160,9 +160,9 @@ int buf_isempty(p_buf buf) { * Sends a block of data (unbuffered) \*-------------------------------------------------------------------------*/ #define STEPSIZE 8192 -static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { +static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) { p_io io = buf->io; - p_tm tm = buf->tm; + p_timeout tm = buf->tm; size_t total = 0; int err = IO_DONE; while (total < count && err == IO_DONE) { @@ -179,15 +179,15 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { /*-------------------------------------------------------------------------*\ * Reads a fixed number of bytes (buffered) \*-------------------------------------------------------------------------*/ -static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { +static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { int err = IO_DONE; size_t total = 0; while (total < wanted && err == IO_DONE) { size_t count; const char *data; - err = buf_get(buf, &data, &count); + err = buffer_get(buf, &data, &count); count = MIN(count, wanted - total); luaL_addlstring(b, data, count); - buf_skip(buf, count); + buffer_skip(buf, count); total += count; } return err; @@ -196,13 +196,13 @@ static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { /*-------------------------------------------------------------------------*\ * Reads everything until the connection is closed (buffered) \*-------------------------------------------------------------------------*/ -static int recvall(p_buf buf, luaL_Buffer *b) { +static int recvall(p_buffer buf, luaL_Buffer *b) { int err = IO_DONE; while (err == IO_DONE) { const char *data; size_t count; - err = buf_get(buf, &data, &count); + err = buffer_get(buf, &data, &count); luaL_addlstring(b, data, count); - buf_skip(buf, count); + buffer_skip(buf, count); } if (err == IO_CLOSED) return IO_DONE; else return err; @@ -212,11 +212,11 @@ static int recvall(p_buf buf, luaL_Buffer *b) { * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF * are not returned by the function and are discarded from the buffer \*-------------------------------------------------------------------------*/ -static int recvline(p_buf buf, luaL_Buffer *b) { +static int recvline(p_buffer buf, luaL_Buffer *b) { int err = IO_DONE; while (err == IO_DONE) { size_t count, pos; const char *data; - err = buf_get(buf, &data, &count); + err = buffer_get(buf, &data, &count); pos = 0; while (pos < count && data[pos] != '\n') { /* we ignore all \r's */ @@ -224,10 +224,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) { pos++; } if (pos < count) { /* found '\n' */ - buf_skip(buf, pos+1); /* skip '\n' too */ + buffer_skip(buf, pos+1); /* skip '\n' too */ break; /* we are done */ } else /* reached the end of the buffer */ - buf_skip(buf, pos); + buffer_skip(buf, pos); } return err; } @@ -236,10 +236,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) { * Skips a given number of bytes from read buffer. No data is read from the * transport layer \*-------------------------------------------------------------------------*/ -static void buf_skip(p_buf buf, size_t count) { +static void buffer_skip(p_buffer buf, size_t count) { buf->received += count; buf->first += count; - if (buf_isempty(buf)) + if (buffer_isempty(buf)) buf->first = buf->last = 0; } @@ -247,11 +247,11 @@ static void buf_skip(p_buf buf, size_t count) { * Return any data available in buffer, or get more data from transport layer * if buffer is empty \*-------------------------------------------------------------------------*/ -static int buf_get(p_buf buf, const char **data, size_t *count) { +static int buffer_get(p_buffer buf, const char **data, size_t *count) { int err = IO_DONE; p_io io = buf->io; - p_tm tm = buf->tm; - if (buf_isempty(buf)) { + p_timeout tm = buf->tm; + if (buffer_isempty(buf)) { size_t got; err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); buf->first = 0; diff --git a/src/buffer.h b/src/buffer.h index f43e676..0a4a335 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -26,22 +26,22 @@ #define BUF_SIZE 8192 /* buffer control structure */ -typedef struct t_buf_ { +typedef struct t_buffer_ { double birthday; /* throttle support info: creation time, */ size_t sent, received; /* bytes sent, and bytes received */ p_io io; /* IO driver used for this buffer */ - p_tm tm; /* timeout management for this buffer */ + p_timeout tm; /* timeout management for this buffer */ size_t first, last; /* index of first and last bytes of stored data */ char data[BUF_SIZE]; /* storage space for buffer data */ -} t_buf; -typedef t_buf *p_buf; +} t_buffer; +typedef t_buffer *p_buffer; -int buf_open(lua_State *L); -void buf_init(p_buf buf, p_io io, p_tm tm); -int buf_meth_send(lua_State *L, p_buf buf); -int buf_meth_receive(lua_State *L, p_buf buf); -int buf_meth_getstats(lua_State *L, p_buf buf); -int buf_meth_setstats(lua_State *L, p_buf buf); -int buf_isempty(p_buf buf); +int buffer_open(lua_State *L); +void buffer_init(p_buffer buf, p_io io, p_timeout tm); +int buffer_meth_send(lua_State *L, p_buffer buf); +int buffer_meth_receive(lua_State *L, p_buffer buf); +int buffer_meth_getstats(lua_State *L, p_buffer buf); +int buffer_meth_setstats(lua_State *L, p_buffer buf); +int buffer_isempty(p_buffer buf); #endif /* BUF_H */ diff --git a/src/inet.c b/src/inet.c index 81ecd22..32f0cd2 100644 --- a/src/inet.c +++ b/src/inet.c @@ -53,9 +53,9 @@ int inet_open(lua_State *L) static int inet_gethost(const char *address, struct hostent **hp) { struct in_addr addr; if (inet_aton(address, &addr)) - return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp); + return socket_gethostbyaddr((char *) &addr, sizeof(addr), hp); else - return sock_gethostbyname(address, hp); + return socket_gethostbyname(address, hp); } /*-------------------------------------------------------------------------*\ @@ -68,7 +68,7 @@ static int inet_global_tohostname(lua_State *L) { int err = inet_gethost(address, &hp); if (err != IO_DONE) { lua_pushnil(L); - lua_pushstring(L, sock_hoststrerror(err)); + lua_pushstring(L, socket_hoststrerror(err)); return 2; } lua_pushstring(L, hp->h_name); @@ -87,7 +87,7 @@ static int inet_global_toip(lua_State *L) int err = inet_gethost(address, &hp); if (err != IO_DONE) { lua_pushnil(L); - lua_pushstring(L, sock_hoststrerror(err)); + lua_pushstring(L, socket_hoststrerror(err)); return 2; } lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr))); @@ -121,7 +121,7 @@ static int inet_global_gethostname(lua_State *L) /*-------------------------------------------------------------------------*\ * Retrieves socket peer name \*-------------------------------------------------------------------------*/ -int inet_meth_getpeername(lua_State *L, p_sock ps) +int inet_meth_getpeername(lua_State *L, p_socket ps) { struct sockaddr_in peer; socklen_t peer_len = sizeof(peer); @@ -138,7 +138,7 @@ int inet_meth_getpeername(lua_State *L, p_sock ps) /*-------------------------------------------------------------------------*\ * Retrieves socket local name \*-------------------------------------------------------------------------*/ -int inet_meth_getsockname(lua_State *L, p_sock ps) +int inet_meth_getsockname(lua_State *L, p_socket ps) { struct sockaddr_in local; socklen_t local_len = sizeof(local); @@ -198,15 +198,15 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp) /*-------------------------------------------------------------------------*\ * Tries to create a new inet socket \*-------------------------------------------------------------------------*/ -const char *inet_trycreate(p_sock ps, int type) { - return sock_strerror(sock_create(ps, AF_INET, type, 0)); +const char *inet_trycreate(p_socket ps, int type) { + return socket_strerror(socket_create(ps, AF_INET, type, 0)); } /*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ -const char *inet_tryconnect(p_sock ps, const char *address, - unsigned short port, p_tm tm) +const char *inet_tryconnect(p_socket ps, const char *address, + unsigned short port, p_timeout tm) { struct sockaddr_in remote; int err; @@ -217,20 +217,20 @@ const char *inet_tryconnect(p_sock ps, const char *address, if (!inet_aton(address, &remote.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; - err = sock_gethostbyname(address, &hp); - if (err != IO_DONE) return sock_hoststrerror(err); + err = socket_gethostbyname(address, &hp); + if (err != IO_DONE) return socket_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); } } else remote.sin_family = AF_UNSPEC; - err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); - return sock_strerror(err); + err = socket_connect(ps, (SA *) &remote, sizeof(remote), tm); + return socket_strerror(err); } /*-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/ -const char *inet_trybind(p_sock ps, const char *address, unsigned short port) +const char *inet_trybind(p_socket ps, const char *address, unsigned short port) { struct sockaddr_in local; int err; @@ -242,14 +242,14 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port) if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; - err = sock_gethostbyname(address, &hp); - if (err != IO_DONE) return sock_hoststrerror(err); + err = socket_gethostbyname(address, &hp); + if (err != IO_DONE) return socket_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); } - err = sock_bind(ps, (SA *) &local, sizeof(local)); - if (err != IO_DONE) sock_destroy(ps); - return sock_strerror(err); + err = socket_bind(ps, (SA *) &local, sizeof(local)); + if (err != IO_DONE) socket_destroy(ps); + return socket_strerror(err); } /*-------------------------------------------------------------------------*\ diff --git a/src/inet.h b/src/inet.h index da95e7e..c6d717d 100644 --- a/src/inet.h +++ b/src/inet.h @@ -26,14 +26,14 @@ int inet_open(lua_State *L); -const char *inet_trycreate(p_sock ps, int type); -const char *inet_tryconnect(p_sock ps, const char *address, - unsigned short port, p_tm tm); -const char *inet_trybind(p_sock ps, const char *address, +const char *inet_trycreate(p_socket ps, int type); +const char *inet_tryconnect(p_socket ps, const char *address, + unsigned short port, p_timeout tm); +const char *inet_trybind(p_socket ps, const char *address, unsigned short port); -int inet_meth_getpeername(lua_State *L, p_sock ps); -int inet_meth_getsockname(lua_State *L, p_sock ps); +int inet_meth_getpeername(lua_State *L, p_socket ps); +int inet_meth_getsockname(lua_State *L, p_socket ps); #ifdef INET_ATON int inet_aton(const char *cp, struct in_addr *inp); diff --git a/src/io.h b/src/io.h index bcaf416..bff48a6 100644 --- a/src/io.h +++ b/src/io.h @@ -39,7 +39,7 @@ typedef int (*p_send) ( const char *data, /* pointer to buffer with data to send */ size_t count, /* number of bytes to send from buffer */ size_t *sent, /* number of bytes sent uppon return */ - p_tm tm /* timeout control */ + p_timeout tm /* timeout control */ ); /* interface to recv function */ @@ -48,7 +48,7 @@ typedef int (*p_recv) ( char *data, /* pointer to buffer where data will be writen */ size_t count, /* number of bytes to receive into buffer */ size_t *got, /* number of bytes received uppon return */ - p_tm tm /* timeout control */ + p_timeout tm /* timeout control */ ); /* IO driver definition */ diff --git a/src/ltn12.lua b/src/ltn12.lua index 733422d..2c16253 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -134,33 +134,49 @@ function source.rewind(src) end end --- chains a source with a filter function source.chain(src, f) base.assert(src and f) local last_in, last_out = "", "" + local state = "feeding" + local err return function() - if last_out == "" then - while true do - local err + if not last_out then + base.error('source is empty!', 2) + end + while true do + if state == "feeding" then last_in, err = src() if err then return nil, err end last_out = f(last_in) - if last_out ~= "" then return last_out end - if not last_in then - base.error('filter returned inappropriate ""') + if not last_out then + if last_in then + base.error('filter returned inappropriate nil') + else + return nil + end + elseif last_out ~= "" then + state = "eating" + if last_in then last_in = "" end + return last_out + end + else + last_out = f(last_in) + if last_out == "" then + if last_in == "" then + state = "feeding" + else + base.error('filter returned ""') + end + elseif not last_out then + if last_in then + base.error('filter returned inappropriate nil') + else + return nil + end + else + return last_out end end - elseif last_out then - last_out = f(last_in and "") - if last_in and not last_out then - base.error('filter returned inappropriate nil') - end - if last_out == "" and not last_in then - base.error(base.tostring(f) .. ' returned inappropriate ""') - end - return last_out - else - base.error("source is empty", 2) end end end diff --git a/src/luasocket.c b/src/luasocket.c index 434b5b7..142aa95 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -48,10 +48,10 @@ static int base_open(lua_State *L); * Modules and functions \*-------------------------------------------------------------------------*/ static const luaL_reg mod[] = { - {"auxiliar", aux_open}, + {"auxiliar", auxiliar_open}, {"except", except_open}, - {"timeout", tm_open}, - {"buffer", buf_open}, + {"timeout", timeout_open}, + {"buffer", buffer_open}, {"inet", inet_open}, {"tcp", tcp_open}, {"udp", udp_open}, @@ -79,7 +79,7 @@ static int global_skip(lua_State *L) { \*-------------------------------------------------------------------------*/ static int global_unload(lua_State *L) { (void) L; - sock_close(); + socket_close(); return 0; } @@ -87,7 +87,7 @@ static int global_unload(lua_State *L) { * Setup basic stuff. \*-------------------------------------------------------------------------*/ static int base_open(lua_State *L) { - if (sock_open()) { + if (socket_open()) { /* export functions (and leave namespace table on top of stack) */ luaL_openlib(L, "socket", func, 0); #ifdef LUASOCKET_DEBUG diff --git a/src/options.c b/src/options.c index 5236a3f..0f1ca2c 100644 --- a/src/options.c +++ b/src/options.c @@ -16,9 +16,9 @@ /*=========================================================================*\ * Internal functions prototypes \*=========================================================================*/ -static int opt_setmembership(lua_State *L, p_sock ps, int level, int name); -static int opt_setboolean(lua_State *L, p_sock ps, int level, int name); -static int opt_set(lua_State *L, p_sock ps, int level, int name, +static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); +static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); +static int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len); /*=========================================================================*\ @@ -27,7 +27,7 @@ static int opt_set(lua_State *L, p_sock ps, int level, int name, /*-------------------------------------------------------------------------*\ * Calls appropriate option handler \*-------------------------------------------------------------------------*/ -int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps) +int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) { const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ while (opt->name && strcmp(name, opt->name)) @@ -41,38 +41,38 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps) } /* enables reuse of local address */ -int opt_reuseaddr(lua_State *L, p_sock ps) +int opt_reuseaddr(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); } /* disables the Naggle algorithm */ -int opt_tcp_nodelay(lua_State *L, p_sock ps) +int opt_tcp_nodelay(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); } -int opt_keepalive(lua_State *L, p_sock ps) +int opt_keepalive(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); } -int opt_dontroute(lua_State *L, p_sock ps) +int opt_dontroute(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); } -int opt_broadcast(lua_State *L, p_sock ps) +int opt_broadcast(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); } -int opt_ip_multicast_loop(lua_State *L, p_sock ps) +int opt_ip_multicast_loop(lua_State *L, p_socket ps) { return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); } -int opt_linger(lua_State *L, p_sock ps) +int opt_linger(lua_State *L, p_socket ps) { struct linger li; /* obj, name, table */ if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); @@ -89,18 +89,18 @@ int opt_linger(lua_State *L, p_sock ps) return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); } -int opt_ip_multicast_ttl(lua_State *L, p_sock ps) +int opt_ip_multicast_ttl(lua_State *L, p_socket ps) { int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val)); } -int opt_ip_add_membership(lua_State *L, p_sock ps) +int opt_ip_add_membership(lua_State *L, p_socket ps) { return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); } -int opt_ip_drop_membersip(lua_State *L, p_sock ps) +int opt_ip_drop_membersip(lua_State *L, p_socket ps) { return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); } @@ -108,7 +108,7 @@ int opt_ip_drop_membersip(lua_State *L, p_sock ps) /*=========================================================================*\ * Auxiliar functions \*=========================================================================*/ -static int opt_setmembership(lua_State *L, p_sock ps, int level, int name) +static int opt_setmembership(lua_State *L, p_socket ps, int level, int name) { struct ip_mreq val; /* obj, name, table */ if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); @@ -130,7 +130,7 @@ static int opt_setmembership(lua_State *L, p_sock ps, int level, int name) } static -int opt_set(lua_State *L, p_sock ps, int level, int name, void *val, int len) +int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) { if (setsockopt(*ps, level, name, (char *) val, len) < 0) { lua_pushnil(L); @@ -141,9 +141,9 @@ int opt_set(lua_State *L, p_sock ps, int level, int name, void *val, int len) return 1; } -static int opt_setboolean(lua_State *L, p_sock ps, int level, int name) +static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) { - int val = aux_checkboolean(L, 3); /* obj, name, bool */ + int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); } diff --git a/src/options.h b/src/options.h index 6ebf1f6..900761e 100644 --- a/src/options.h +++ b/src/options.h @@ -16,24 +16,24 @@ /* option registry */ typedef struct t_opt { const char *name; - int (*func)(lua_State *L, p_sock ps); + int (*func)(lua_State *L, p_socket ps); } t_opt; typedef t_opt *p_opt; /* supported options */ -int opt_dontroute(lua_State *L, p_sock ps); -int opt_broadcast(lua_State *L, p_sock ps); -int opt_reuseaddr(lua_State *L, p_sock ps); -int opt_tcp_nodelay(lua_State *L, p_sock ps); -int opt_keepalive(lua_State *L, p_sock ps); -int opt_linger(lua_State *L, p_sock ps); -int opt_reuseaddr(lua_State *L, p_sock ps); -int opt_ip_multicast_ttl(lua_State *L, p_sock ps); -int opt_ip_multicast_loop(lua_State *L, p_sock ps); -int opt_ip_add_membership(lua_State *L, p_sock ps); -int opt_ip_drop_membersip(lua_State *L, p_sock ps); +int opt_dontroute(lua_State *L, p_socket ps); +int opt_broadcast(lua_State *L, p_socket ps); +int opt_reuseaddr(lua_State *L, p_socket ps); +int opt_tcp_nodelay(lua_State *L, p_socket ps); +int opt_keepalive(lua_State *L, p_socket ps); +int opt_linger(lua_State *L, p_socket ps); +int opt_reuseaddr(lua_State *L, p_socket ps); +int opt_ip_multicast_ttl(lua_State *L, p_socket ps); +int opt_ip_multicast_loop(lua_State *L, p_socket ps); +int opt_ip_add_membership(lua_State *L, p_socket ps); +int opt_ip_drop_membersip(lua_State *L, p_socket ps); /* invokes the appropriate option handler */ -int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps); +int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); #endif diff --git a/src/select.c b/src/select.c index d28ade1..5a3d502 100644 --- a/src/select.c +++ b/src/select.c @@ -51,7 +51,7 @@ int select_open(lua_State *L) { static int global_select(lua_State *L) { int rtab, wtab, itab, max_fd, ret, ndirty; fd_set rset, wset; - t_tm tm; + t_timeout tm; double t = luaL_optnumber(L, 3, -1); FD_ZERO(&rset); FD_ZERO(&wset); lua_settop(L, 3); @@ -61,10 +61,10 @@ static int global_select(lua_State *L) { max_fd = collect_fd(L, 1, -1, itab, &rset); ndirty = check_dirty(L, 1, rtab, &rset); t = ndirty > 0? 0.0: t; - tm_init(&tm, t, -1); - tm_markstart(&tm); + timeout_init(&tm, t, -1); + timeout_markstart(&tm); max_fd = collect_fd(L, 2, max_fd, itab, &wset); - ret = sock_select(max_fd+1, &rset, &wset, NULL, &tm); + ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm); if (ret > 0 || ndirty > 0) { return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); return_fd(L, &wset, max_fd+1, itab, wtab, 0); diff --git a/src/socket.h b/src/socket.h index ad12b1b..a599d8a 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,5 +1,5 @@ -#ifndef SOCK_H -#define SOCK_H +#ifndef SOCKET_H +#define SOCKET_H /*=========================================================================*\ * Socket compatibilization module * LuaSocket toolkit @@ -37,37 +37,39 @@ typedef struct sockaddr SA; * Functions bellow implement a comfortable platform independent * interface to sockets \*=========================================================================*/ -int sock_open(void); -int sock_close(void); -void sock_destroy(p_sock ps); -void sock_shutdown(p_sock ps, int how); -int sock_sendto(p_sock ps, const char *data, size_t count, - size_t *sent, SA *addr, socklen_t addr_len, p_tm tm); -int sock_recvfrom(p_sock ps, char *data, size_t count, - size_t *got, SA *addr, socklen_t *addr_len, p_tm tm); +int socket_open(void); +int socket_close(void); +void socket_destroy(p_socket ps); +void socket_shutdown(p_socket ps, int how); +int socket_sendto(p_socket ps, const char *data, size_t count, + size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm); +int socket_recvfrom(p_socket ps, char *data, size_t count, + size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm); -void sock_setnonblocking(p_sock ps); -void sock_setblocking(p_sock ps); +void socket_setnonblocking(p_socket ps); +void socket_setblocking(p_socket ps); -int sock_waitfd(p_sock ps, int sw, p_tm tm); -int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); +int socket_waitfd(p_socket ps, int sw, p_timeout tm); +int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm); -int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); -int sock_create(p_sock ps, int domain, int type, int protocol); -int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); -int sock_listen(p_sock ps, int backlog); -int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm); +int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm); +int socket_create(p_socket ps, int domain, int type, int protocol); +int socket_bind(p_socket ps, SA *addr, socklen_t addr_len); +int socket_listen(p_socket ps, int backlog); +int socket_accept(p_socket ps, p_socket pa, SA *addr, + socklen_t *addr_len, p_timeout tm); -const char *sock_hoststrerror(int err); -const char *sock_strerror(int err); +const char *socket_hoststrerror(int err); +const char *socket_strerror(int err); /* these are perfect to use with the io abstraction module and the buffered input module */ -int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm); -int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm); -const char *sock_ioerror(p_sock ps, int err); +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm); +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); +const char *socket_ioerror(p_socket ps, int err); -int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); -int sock_gethostbyname(const char *addr, struct hostent **hp); +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); +int socket_gethostbyname(const char *addr, struct hostent **hp); -#endif /* SOCK_H */ +#endif /* SOCKET_H */ diff --git a/src/tcp.c b/src/tcp.c index 8b4c7e3..539ad5e 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -40,7 +40,7 @@ static int meth_dirty(lua_State *L); /* tcp object methods */ static luaL_reg tcp[] = { {"__gc", meth_close}, - {"__tostring", aux_tostring}, + {"__tostring", auxiliar_tostring}, {"accept", meth_accept}, {"bind", meth_bind}, {"close", meth_close}, @@ -84,13 +84,13 @@ static luaL_reg func[] = { int tcp_open(lua_State *L) { /* create classes */ - aux_newclass(L, "tcp{master}", tcp); - aux_newclass(L, "tcp{client}", tcp); - aux_newclass(L, "tcp{server}", tcp); + auxiliar_newclass(L, "tcp{master}", tcp); + auxiliar_newclass(L, "tcp{client}", tcp); + auxiliar_newclass(L, "tcp{server}", tcp); /* create class groups */ - aux_add2group(L, "tcp{master}", "tcp{any}"); - aux_add2group(L, "tcp{client}", "tcp{any}"); - aux_add2group(L, "tcp{server}", "tcp{any}"); + auxiliar_add2group(L, "tcp{master}", "tcp{any}"); + auxiliar_add2group(L, "tcp{client}", "tcp{any}"); + auxiliar_add2group(L, "tcp{server}", "tcp{any}"); /* define library functions */ luaL_openlib(L, NULL, func, 0); return 0; @@ -103,23 +103,23 @@ int tcp_open(lua_State *L) * Just call buffered IO methods \*-------------------------------------------------------------------------*/ static int meth_send(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); - return buf_meth_send(L, &tcp->buf); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_send(L, &tcp->buf); } static int meth_receive(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); - return buf_meth_receive(L, &tcp->buf); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_receive(L, &tcp->buf); } static int meth_getstats(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); - return buf_meth_getstats(L, &tcp->buf); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_getstats(L, &tcp->buf); } static int meth_setstats(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); - return buf_meth_setstats(L, &tcp->buf); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_setstats(L, &tcp->buf); } /*-------------------------------------------------------------------------*\ @@ -127,7 +127,7 @@ static int meth_setstats(lua_State *L) { \*-------------------------------------------------------------------------*/ static int meth_setoption(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); return opt_meth_setoption(L, opt, &tcp->sock); } @@ -136,7 +136,7 @@ static int meth_setoption(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_getfd(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); lua_pushnumber(L, (int) tcp->sock); return 1; } @@ -144,15 +144,15 @@ static int meth_getfd(lua_State *L) /* this is very dangerous, but can be handy for those that are brave enough */ static int meth_setfd(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - tcp->sock = (t_sock) luaL_checknumber(L, 2); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + tcp->sock = (t_socket) luaL_checknumber(L, 2); return 0; } static int meth_dirty(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - lua_pushboolean(L, !buf_isempty(&tcp->buf)); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + lua_pushboolean(L, !buffer_isempty(&tcp->buf)); return 1; } @@ -162,25 +162,25 @@ static int meth_dirty(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_accept(lua_State *L) { - p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); - p_tm tm = tm_markstart(&server->tm); - t_sock sock; - int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); + p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); + p_timeout tm = timeout_markstart(&server->tm); + t_socket sock; + int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); /* if successful, push client socket */ if (err == IO_DONE) { p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); - aux_setclass(L, "tcp{client}", -1); + auxiliar_setclass(L, "tcp{client}", -1); /* initialize structure fields */ - sock_setnonblocking(&sock); + socket_setnonblocking(&sock); clnt->sock = sock; - io_init(&clnt->io, (p_send) sock_send, (p_recv) sock_recv, - (p_error) sock_ioerror, &clnt->sock); - tm_init(&clnt->tm, -1, -1); - buf_init(&clnt->buf, &clnt->io, &clnt->tm); + io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &clnt->sock); + timeout_init(&clnt->tm, -1, -1); + buffer_init(&clnt->buf, &clnt->io, &clnt->tm); return 1; } else { lua_pushnil(L); - lua_pushstring(L, sock_strerror(err)); + lua_pushstring(L, socket_strerror(err)); return 2; } } @@ -190,7 +190,7 @@ static int meth_accept(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_bind(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); const char *err = inet_trybind(&tcp->sock, address, port); @@ -208,13 +208,13 @@ static int meth_bind(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_connect(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); - p_tm tm = tm_markstart(&tcp->tm); + p_timeout tm = timeout_markstart(&tcp->tm); const char *err = inet_tryconnect(&tcp->sock, address, port, tm); /* have to set the class even if it failed due to non-blocking connects */ - aux_setclass(L, "tcp{client}", 1); + auxiliar_setclass(L, "tcp{client}", 1); if (err) { lua_pushnil(L); lua_pushstring(L, err); @@ -230,8 +230,8 @@ static int meth_connect(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_close(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - sock_destroy(&tcp->sock); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + socket_destroy(&tcp->sock); lua_pushnumber(L, 1); return 1; } @@ -241,16 +241,16 @@ static int meth_close(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_listen(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); int backlog = (int) luaL_optnumber(L, 2, 32); - int err = sock_listen(&tcp->sock, backlog); + int err = socket_listen(&tcp->sock, backlog); if (err != IO_DONE) { lua_pushnil(L); - lua_pushstring(L, sock_strerror(err)); + lua_pushstring(L, socket_strerror(err)); return 2; } /* turn master object into a server object */ - aux_setclass(L, "tcp{server}", 1); + auxiliar_setclass(L, "tcp{server}", 1); lua_pushnumber(L, 1); return 1; } @@ -260,20 +260,20 @@ static int meth_listen(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_shutdown(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); const char *how = luaL_optstring(L, 2, "both"); switch (how[0]) { case 'b': if (strcmp(how, "both")) goto error; - sock_shutdown(&tcp->sock, 2); + socket_shutdown(&tcp->sock, 2); break; case 's': if (strcmp(how, "send")) goto error; - sock_shutdown(&tcp->sock, 1); + socket_shutdown(&tcp->sock, 1); break; case 'r': if (strcmp(how, "receive")) goto error; - sock_shutdown(&tcp->sock, 0); + socket_shutdown(&tcp->sock, 0); break; } lua_pushnumber(L, 1); @@ -288,13 +288,13 @@ error: \*-------------------------------------------------------------------------*/ static int meth_getpeername(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); return inet_meth_getpeername(L, &tcp->sock); } static int meth_getsockname(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); return inet_meth_getsockname(L, &tcp->sock); } @@ -303,8 +303,8 @@ static int meth_getsockname(lua_State *L) \*-------------------------------------------------------------------------*/ static int meth_settimeout(lua_State *L) { - p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); - return tm_meth_settimeout(L, &tcp->tm); + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return timeout_meth_settimeout(L, &tcp->tm); } /*=========================================================================*\ @@ -315,21 +315,21 @@ static int meth_settimeout(lua_State *L) \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { - t_sock sock; + t_socket sock; const char *err = inet_trycreate(&sock, SOCK_STREAM); /* try to allocate a system socket */ if (!err) { /* allocate tcp object */ p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); /* set its type as master object */ - aux_setclass(L, "tcp{master}", -1); + auxiliar_setclass(L, "tcp{master}", -1); /* initialize remaining structure fields */ - sock_setnonblocking(&sock); + socket_setnonblocking(&sock); tcp->sock = sock; - io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, - (p_error) sock_ioerror, &tcp->sock); - tm_init(&tcp->tm, -1, -1); - buf_init(&tcp->buf, &tcp->io, &tcp->tm); + io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &tcp->sock); + timeout_init(&tcp->tm, -1, -1); + buffer_init(&tcp->buf, &tcp->io, &tcp->tm); return 1; } else { lua_pushnil(L); diff --git a/src/tcp.h b/src/tcp.h index fe4fd8c..7e98ab6 100644 --- a/src/tcp.h +++ b/src/tcp.h @@ -23,10 +23,10 @@ #include "socket.h" typedef struct t_tcp_ { - t_sock sock; + t_socket sock; t_io io; - t_buf buf; - t_tm tm; + t_buffer buf; + t_timeout tm; } t_tcp; typedef t_tcp *p_tcp; diff --git a/src/timeout.c b/src/timeout.c index 4f1d345..863546e 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -30,12 +30,12 @@ /*=========================================================================*\ * Internal function prototypes \*=========================================================================*/ -static int tm_lua_gettime(lua_State *L); -static int tm_lua_sleep(lua_State *L); +static int timeout_lua_gettime(lua_State *L); +static int timeout_lua_sleep(lua_State *L); static luaL_reg func[] = { - { "gettime", tm_lua_gettime }, - { "sleep", tm_lua_sleep }, + { "gettime", timeout_lua_gettime }, + { "sleep", timeout_lua_sleep }, { NULL, NULL } }; @@ -45,7 +45,7 @@ static luaL_reg func[] = { /*-------------------------------------------------------------------------*\ * Initialize structure \*-------------------------------------------------------------------------*/ -void tm_init(p_tm tm, double block, double total) { +void timeout_init(p_timeout tm, double block, double total) { tm->block = block; tm->total = total; } @@ -58,16 +58,16 @@ void tm_init(p_tm tm, double block, double total) { * Returns * the number of ms left or -1 if there is no time limit \*-------------------------------------------------------------------------*/ -double tm_get(p_tm tm) { +double timeout_get(p_timeout tm) { if (tm->block < 0.0 && tm->total < 0.0) { return -1; } else if (tm->block < 0.0) { - double t = tm->total - tm_gettime() + tm->start; + double t = tm->total - timeout_gettime() + tm->start; return MAX(t, 0.0); } else if (tm->total < 0.0) { return tm->block; } else { - double t = tm->total - tm_gettime() + tm->start; + double t = tm->total - timeout_gettime() + tm->start; return MIN(tm->block, MAX(t, 0.0)); } } @@ -79,7 +79,7 @@ double tm_get(p_tm tm) { * Returns * start field of structure \*-------------------------------------------------------------------------*/ -double tm_getstart(p_tm tm) { +double timeout_getstart(p_timeout tm) { return tm->start; } @@ -91,17 +91,17 @@ double tm_getstart(p_tm tm) { * Returns * the number of ms left or -1 if there is no time limit \*-------------------------------------------------------------------------*/ -double tm_getretry(p_tm tm) { +double timeout_getretry(p_timeout tm) { if (tm->block < 0.0 && tm->total < 0.0) { return -1; } else if (tm->block < 0.0) { - double t = tm->total - tm_gettime() + tm->start; + double t = tm->total - timeout_gettime() + tm->start; return MAX(t, 0.0); } else if (tm->total < 0.0) { - double t = tm->block - tm_gettime() + tm->start; + double t = tm->block - timeout_gettime() + tm->start; return MAX(t, 0.0); } else { - double t = tm->total - tm_gettime() + tm->start; + double t = tm->total - timeout_gettime() + tm->start; return MIN(tm->block, MAX(t, 0.0)); } } @@ -111,8 +111,8 @@ double tm_getretry(p_tm tm) { * Input * tm: timeout control structure \*-------------------------------------------------------------------------*/ -p_tm tm_markstart(p_tm tm) { - tm->start = tm_gettime(); +p_timeout timeout_markstart(p_timeout tm) { + tm->start = timeout_gettime(); return tm; } @@ -122,7 +122,7 @@ p_tm tm_markstart(p_tm tm) { * time in s. \*-------------------------------------------------------------------------*/ #ifdef _WIN32 -double tm_gettime(void) { +double timeout_gettime(void) { FILETIME ft; double t; GetSystemTimeAsFileTime(&ft); @@ -132,7 +132,7 @@ double tm_gettime(void) { return (t - 11644473600.0); } #else -double tm_gettime(void) { +double timeout_gettime(void) { struct timeval v; gettimeofday(&v, (struct timezone *) NULL); /* Unix Epoch time (time since January 1, 1970 (UTC)) */ @@ -143,7 +143,7 @@ double tm_gettime(void) { /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ -int tm_open(lua_State *L) { +int timeout_open(lua_State *L) { luaL_openlib(L, NULL, func, 0); return 0; } @@ -154,7 +154,7 @@ int tm_open(lua_State *L) { * time: time out value in seconds * mode: "b" for block timeout, "t" for total timeout. (default: b) \*-------------------------------------------------------------------------*/ -int tm_meth_settimeout(lua_State *L, p_tm tm) { +int timeout_meth_settimeout(lua_State *L, p_timeout tm) { double t = luaL_optnumber(L, 2, -1); const char *mode = luaL_optstring(L, 3, "b"); switch (*mode) { @@ -178,16 +178,16 @@ int tm_meth_settimeout(lua_State *L, p_tm tm) { /*-------------------------------------------------------------------------*\ * Returns the time the system has been up, in secconds. \*-------------------------------------------------------------------------*/ -static int tm_lua_gettime(lua_State *L) +static int timeout_lua_gettime(lua_State *L) { - lua_pushnumber(L, tm_gettime()); + lua_pushnumber(L, timeout_gettime()); return 1; } /*-------------------------------------------------------------------------*\ * Sleep for n seconds. \*-------------------------------------------------------------------------*/ -int tm_lua_sleep(lua_State *L) +int timeout_lua_sleep(lua_State *L) { double n = luaL_checknumber(L, 1); #ifdef _WIN32 diff --git a/src/timeout.h b/src/timeout.h index 27e0a8a..a328bcc 100644 --- a/src/timeout.h +++ b/src/timeout.h @@ -1,5 +1,5 @@ -#ifndef TM_H -#define TM_H +#ifndef TIMEOUT_H +#define TIMEOUT_H /*=========================================================================*\ * Timeout management functions * LuaSocket toolkit @@ -9,22 +9,22 @@ #include "lua.h" /* timeout control structure */ -typedef struct t_tm_ { +typedef struct t_timeout_ { double block; /* maximum time for blocking calls */ double total; /* total number of miliseconds for operation */ double start; /* time of start of operation */ -} t_tm; -typedef t_tm *p_tm; +} t_timeout; +typedef t_timeout *p_timeout; -int tm_open(lua_State *L); -void tm_init(p_tm tm, double block, double total); -double tm_get(p_tm tm); -double tm_getretry(p_tm tm); -p_tm tm_markstart(p_tm tm); -double tm_getstart(p_tm tm); -double tm_gettime(void); -int tm_meth_settimeout(lua_State *L, p_tm tm); +int timeout_open(lua_State *L); +void timeout_init(p_timeout tm, double block, double total); +double timeout_get(p_timeout tm); +double timeout_getretry(p_timeout tm); +p_timeout timeout_markstart(p_timeout tm); +double timeout_getstart(p_timeout tm); +double timeout_gettime(void); +int timeout_meth_settimeout(lua_State *L, p_timeout tm); -#define tm_iszero(tm) ((tm)->block == 0.0) +#define timeout_iszero(tm) ((tm)->block == 0.0) -#endif /* TM_H */ +#endif /* TIMEOUT_H */ diff --git a/src/udp.c b/src/udp.c index 4de7248..e604bea 100644 --- a/src/udp.c +++ b/src/udp.c @@ -45,7 +45,7 @@ static int meth_dirty(lua_State *L); /* udp object methods */ static luaL_reg udp[] = { {"__gc", meth_close}, - {"__tostring", aux_tostring}, + {"__tostring", auxiliar_tostring}, {"close", meth_close}, {"dirty", meth_dirty}, {"getfd", meth_getfd}, @@ -87,13 +87,13 @@ static luaL_reg func[] = { int udp_open(lua_State *L) { /* create classes */ - aux_newclass(L, "udp{connected}", udp); - aux_newclass(L, "udp{unconnected}", udp); + auxiliar_newclass(L, "udp{connected}", udp); + auxiliar_newclass(L, "udp{unconnected}", udp); /* create class groups */ - aux_add2group(L, "udp{connected}", "udp{any}"); - aux_add2group(L, "udp{unconnected}", "udp{any}"); - aux_add2group(L, "udp{connected}", "select{able}"); - aux_add2group(L, "udp{unconnected}", "select{able}"); + auxiliar_add2group(L, "udp{connected}", "udp{any}"); + auxiliar_add2group(L, "udp{unconnected}", "udp{any}"); + auxiliar_add2group(L, "udp{connected}", "select{able}"); + auxiliar_add2group(L, "udp{unconnected}", "select{able}"); /* define library functions */ luaL_openlib(L, NULL, func, 0); return 0; @@ -106,20 +106,20 @@ const char *udp_strerror(int err) { /* a 'closed' error on an unconnected means the target address was not * accepted by the transport layer */ if (err == IO_CLOSED) return "refused"; - else return sock_strerror(err); + else return socket_strerror(err); } /*-------------------------------------------------------------------------*\ * Send data through connected udp socket \*-------------------------------------------------------------------------*/ static int meth_send(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); - p_tm tm = &udp->tm; + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); + p_timeout tm = &udp->tm; size_t count, sent = 0; int err; const char *data = luaL_checklstring(L, 2, &count); - tm_markstart(tm); - err = sock_send(&udp->sock, data, count, &sent, tm); + timeout_markstart(tm); + err = socket_send(&udp->sock, data, count, &sent, tm); if (err != IO_DONE) { lua_pushnil(L); lua_pushstring(L, udp_strerror(err)); @@ -133,12 +133,12 @@ static int meth_send(lua_State *L) { * Send data through unconnected udp socket \*-------------------------------------------------------------------------*/ static int meth_sendto(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); size_t count, sent = 0; const char *data = luaL_checklstring(L, 2, &count); const char *ip = luaL_checkstring(L, 3); unsigned short port = (unsigned short) luaL_checknumber(L, 4); - p_tm tm = &udp->tm; + p_timeout tm = &udp->tm; struct sockaddr_in addr; int err; memset(&addr, 0, sizeof(addr)); @@ -146,8 +146,8 @@ static int meth_sendto(lua_State *L) { luaL_argerror(L, 3, "invalid ip address"); addr.sin_family = AF_INET; addr.sin_port = htons(port); - tm_markstart(tm); - err = sock_sendto(&udp->sock, data, count, &sent, + timeout_markstart(tm); + err = socket_sendto(&udp->sock, data, count, &sent, (SA *) &addr, sizeof(addr), tm); if (err != IO_DONE) { lua_pushnil(L); @@ -162,14 +162,14 @@ static int meth_sendto(lua_State *L) { * Receives data from a UDP socket \*-------------------------------------------------------------------------*/ static int meth_receive(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); char buffer[UDP_DATAGRAMSIZE]; size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); int err; - p_tm tm = &udp->tm; + p_timeout tm = &udp->tm; count = MIN(count, sizeof(buffer)); - tm_markstart(tm); - err = sock_recv(&udp->sock, buffer, count, &got, tm); + timeout_markstart(tm); + err = socket_recv(&udp->sock, buffer, count, &got, tm); if (err != IO_DONE) { lua_pushnil(L); lua_pushstring(L, udp_strerror(err)); @@ -183,16 +183,16 @@ static int meth_receive(lua_State *L) { * Receives data and sender from a UDP socket \*-------------------------------------------------------------------------*/ static int meth_receivefrom(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); struct sockaddr_in addr; socklen_t addr_len = sizeof(addr); char buffer[UDP_DATAGRAMSIZE]; size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); int err; - p_tm tm = &udp->tm; - tm_markstart(tm); + p_timeout tm = &udp->tm; + timeout_markstart(tm); count = MIN(count, sizeof(buffer)); - err = sock_recvfrom(&udp->sock, buffer, count, &got, + err = socket_recvfrom(&udp->sock, buffer, count, &got, (SA *) &addr, &addr_len, tm); if (err == IO_DONE) { lua_pushlstring(L, buffer, got); @@ -210,20 +210,20 @@ static int meth_receivefrom(lua_State *L) { * Select support methods \*-------------------------------------------------------------------------*/ static int meth_getfd(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); lua_pushnumber(L, (int) udp->sock); return 1; } /* this is very dangerous, but can be handy for those that are brave enough */ static int meth_setfd(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - udp->sock = (t_sock) luaL_checknumber(L, 2); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + udp->sock = (t_socket) luaL_checknumber(L, 2); return 0; } static int meth_dirty(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); (void) udp; lua_pushboolean(L, 0); return 1; @@ -233,12 +233,12 @@ static int meth_dirty(lua_State *L) { * Just call inet methods \*-------------------------------------------------------------------------*/ static int meth_getpeername(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); return inet_meth_getpeername(L, &udp->sock); } static int meth_getsockname(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); return inet_meth_getsockname(L, &udp->sock); } @@ -246,7 +246,7 @@ static int meth_getsockname(lua_State *L) { * Just call option handler \*-------------------------------------------------------------------------*/ static int meth_setoption(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); return opt_meth_setoption(L, opt, &udp->sock); } @@ -254,16 +254,16 @@ static int meth_setoption(lua_State *L) { * Just call tm methods \*-------------------------------------------------------------------------*/ static int meth_settimeout(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - return tm_meth_settimeout(L, &udp->tm); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + return timeout_meth_settimeout(L, &udp->tm); } /*-------------------------------------------------------------------------*\ * Turns a master udp object into a client object. \*-------------------------------------------------------------------------*/ static int meth_setpeername(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - p_tm tm = &udp->tm; + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + p_timeout tm = &udp->tm; const char *address = luaL_checkstring(L, 2); int connecting = strcmp(address, "*"); unsigned short port = connecting ? @@ -276,8 +276,8 @@ static int meth_setpeername(lua_State *L) { return 2; } /* change class to connected or unconnected depending on address */ - if (connecting) aux_setclass(L, "udp{connected}", 1); - else aux_setclass(L, "udp{unconnected}", 1); + if (connecting) auxiliar_setclass(L, "udp{connected}", 1); + else auxiliar_setclass(L, "udp{unconnected}", 1); lua_pushnumber(L, 1); return 1; } @@ -286,8 +286,8 @@ static int meth_setpeername(lua_State *L) { * Closes socket used by object \*-------------------------------------------------------------------------*/ static int meth_close(lua_State *L) { - p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); - sock_destroy(&udp->sock); + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + socket_destroy(&udp->sock); lua_pushnumber(L, 1); return 1; } @@ -296,7 +296,7 @@ static int meth_close(lua_State *L) { * Turns a master object into a server object \*-------------------------------------------------------------------------*/ static int meth_setsockname(lua_State *L) { - p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); const char *address = luaL_checkstring(L, 2); unsigned short port = (unsigned short) luaL_checknumber(L, 3); const char *err = inet_trybind(&udp->sock, address, port); @@ -316,17 +316,17 @@ static int meth_setsockname(lua_State *L) { * Creates a master udp object \*-------------------------------------------------------------------------*/ static int global_create(lua_State *L) { - t_sock sock; + t_socket sock; const char *err = inet_trycreate(&sock, SOCK_DGRAM); /* try to allocate a system socket */ if (!err) { /* allocate tcp object */ p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); - aux_setclass(L, "udp{unconnected}", -1); + auxiliar_setclass(L, "udp{unconnected}", -1); /* initialize remaining structure fields */ - sock_setnonblocking(&sock); + socket_setnonblocking(&sock); udp->sock = sock; - tm_init(&udp->tm, -1, -1); + timeout_init(&udp->tm, -1, -1); return 1; } else { lua_pushnil(L); diff --git a/src/udp.h b/src/udp.h index 3591998..d29f833 100644 --- a/src/udp.h +++ b/src/udp.h @@ -23,8 +23,8 @@ #define UDP_DATAGRAMSIZE 8192 typedef struct t_udp_ { - t_sock sock; - t_tm tm; + t_socket sock; + t_timeout tm; } t_udp; typedef t_udp *p_udp; diff --git a/src/usocket.c b/src/usocket.c index f067fa1..e9db6dd 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -16,21 +16,21 @@ /*-------------------------------------------------------------------------*\ * Wait for readable/writable/connected socket with timeout \*-------------------------------------------------------------------------*/ -#ifdef SOCK_POLL +#ifdef SOCKET_POLL #include #define WAITFD_R POLLIN #define WAITFD_W POLLOUT #define WAITFD_C (POLLIN|POLLOUT) -int sock_waitfd(p_sock ps, int sw, p_tm tm) { +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { int ret; struct pollfd pfd; pfd.fd = *ps; pfd.events = sw; pfd.revents = 0; - if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ do { - int t = (int)(tm_getretry(tm)*1e3); + int t = (int)(timeout_getretry(tm)*1e3); ret = poll(&pfd, 1, t >= 0? t: -1); } while (ret == -1 && errno == EINTR); if (ret == -1) return errno; @@ -44,18 +44,18 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) { #define WAITFD_W 2 #define WAITFD_C (WAITFD_R|WAITFD_W) -int sock_waitfd(p_sock ps, int sw, p_tm tm) { +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { int ret; fd_set rfds, wfds, *rp, *wp; struct timeval tv, *tp; double t; - if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 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; } - t = tm_getretry(tm); + t = timeout_getretry(tm); tp = NULL; if (t >= 0.0) { tv.tv_sec = (int)t; @@ -75,7 +75,7 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) { /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ -int sock_open(void) { +int socket_open(void) { /* instals a handler to ignore sigpipe or it will crash us */ signal(SIGPIPE, SIG_IGN); return 1; @@ -84,29 +84,29 @@ int sock_open(void) { /*-------------------------------------------------------------------------*\ * Close module \*-------------------------------------------------------------------------*/ -int sock_close(void) { +int socket_close(void) { return 1; } /*-------------------------------------------------------------------------*\ * Close and inutilize socket \*-------------------------------------------------------------------------*/ -void sock_destroy(p_sock ps) { - if (*ps != SOCK_INVALID) { - sock_setblocking(ps); +void socket_destroy(p_socket ps) { + if (*ps != SOCKET_INVALID) { + socket_setblocking(ps); close(*ps); - *ps = SOCK_INVALID; + *ps = SOCKET_INVALID; } } /*-------------------------------------------------------------------------*\ * Select with timeout control \*-------------------------------------------------------------------------*/ -int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { +int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm) { int ret; do { struct timeval tv; - double t = tm_getretry(tm); + double t = timeout_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); @@ -117,59 +117,59 @@ int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { /*-------------------------------------------------------------------------*\ * Creates and sets up a socket \*-------------------------------------------------------------------------*/ -int sock_create(p_sock ps, int domain, int type, int protocol) { +int socket_create(p_socket ps, int domain, int type, int protocol) { *ps = socket(domain, type, protocol); - if (*ps != SOCK_INVALID) return IO_DONE; + if (*ps != SOCKET_INVALID) return IO_DONE; else return errno; } /*-------------------------------------------------------------------------*\ * Binds or returns error message \*-------------------------------------------------------------------------*/ -int sock_bind(p_sock ps, SA *addr, socklen_t len) { +int socket_bind(p_socket ps, SA *addr, socklen_t len) { int err = IO_DONE; - sock_setblocking(ps); + socket_setblocking(ps); if (bind(*ps, addr, len) < 0) err = errno; - sock_setnonblocking(ps); + socket_setnonblocking(ps); return err; } /*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ -int sock_listen(p_sock ps, int backlog) { +int socket_listen(p_socket ps, int backlog) { int err = IO_DONE; - sock_setblocking(ps); + socket_setblocking(ps); if (listen(*ps, backlog)) err = errno; - sock_setnonblocking(ps); + socket_setnonblocking(ps); return err; } /*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ -void sock_shutdown(p_sock ps, int how) { - sock_setblocking(ps); +void socket_shutdown(p_socket ps, int how) { + socket_setblocking(ps); shutdown(*ps, how); - sock_setnonblocking(ps); + socket_setnonblocking(ps); } /*-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/ -int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { +int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { int err; /* avoid calling on closed sockets */ - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; /* 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; /* zero timeout case optimization */ - if (tm_iszero(tm)) return IO_TIMEOUT; + if (timeout_iszero(tm)) return IO_TIMEOUT; /* wait until we have the result of the connection attempt or timeout */ - err = sock_waitfd(ps, WAITFD_C, tm); + err = socket_waitfd(ps, WAITFD_C, tm); if (err == IO_CLOSED) { if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; else return errno; @@ -179,19 +179,19 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { /*-------------------------------------------------------------------------*\ * Accept with timeout \*-------------------------------------------------------------------------*/ -int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { +int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { SA daddr; socklen_t dlen = sizeof(daddr); - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; if (!addr) addr = &daddr; if (!len) len = &dlen; for ( ;; ) { int err; - if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE; + if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; err = errno; if (err == EINTR) continue; if (err != EAGAIN && err != ECONNABORTED) return err; - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } /* can't reach here */ return IO_UNKNOWN; @@ -200,11 +200,12 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { /*-------------------------------------------------------------------------*\ * Send with timeout \*-------------------------------------------------------------------------*/ -int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm) { int err; /* avoid making system calls on closed sockets */ - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; /* loop until we send something or we give up on error */ *sent = 0; for ( ;; ) { @@ -223,7 +224,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) /* if failed fatal reason, report error */ if (err != EAGAIN) return err; /* wait until we can send something or we timeout */ - if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; } /* can't reach here */ return IO_UNKNOWN; @@ -232,11 +233,11 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) /*-------------------------------------------------------------------------*\ * Sendto with timeout \*-------------------------------------------------------------------------*/ -int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, - SA *addr, socklen_t len, p_tm tm) +int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, + SA *addr, socklen_t len, p_timeout tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; *sent = 0; for ( ;; ) { long put = (long) sendto(*ps, data, count, 0, addr, len); @@ -248,7 +249,7 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, if (put == 0 || err == EPIPE) return IO_CLOSED; if (err == EINTR) continue; if (err != EAGAIN) return err; - if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -256,9 +257,9 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, /*-------------------------------------------------------------------------*\ * Receive with timeout \*-------------------------------------------------------------------------*/ -int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; for ( ;; ) { long taken = (long) recv(*ps, data, count, 0); if (taken > 0) { @@ -270,7 +271,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { if (taken == 0) return IO_CLOSED; if (err == EINTR) continue; if (err != EAGAIN) return err; - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -278,10 +279,10 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { /*-------------------------------------------------------------------------*\ * Recvfrom with timeout \*-------------------------------------------------------------------------*/ -int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, - SA *addr, socklen_t *len, p_tm tm) { +int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, + SA *addr, socklen_t *len, p_timeout tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; for ( ;; ) { long taken = (long) recvfrom(*ps, data, count, 0, addr, len); if (taken > 0) { @@ -293,7 +294,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, if (taken == 0) return IO_CLOSED; if (err == EINTR) continue; if (err != EAGAIN) return err; - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -301,7 +302,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, /*-------------------------------------------------------------------------*\ * Put socket into blocking mode \*-------------------------------------------------------------------------*/ -void sock_setblocking(p_sock ps) { +void socket_setblocking(p_socket ps) { int flags = fcntl(*ps, F_GETFL, 0); flags &= (~(O_NONBLOCK)); fcntl(*ps, F_SETFL, flags); @@ -310,7 +311,7 @@ void sock_setblocking(p_sock ps) { /*-------------------------------------------------------------------------*\ * Put socket into non-blocking mode \*-------------------------------------------------------------------------*/ -void sock_setnonblocking(p_sock ps) { +void socket_setnonblocking(p_socket ps) { int flags = fcntl(*ps, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(*ps, F_SETFL, flags); @@ -319,7 +320,7 @@ void sock_setnonblocking(p_sock ps) { /*-------------------------------------------------------------------------*\ * DNS helpers \*-------------------------------------------------------------------------*/ -int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { *hp = gethostbyaddr(addr, len, AF_INET); if (*hp) return IO_DONE; else if (h_errno) return h_errno; @@ -327,7 +328,7 @@ int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { else return IO_UNKNOWN; } -int sock_gethostbyname(const char *addr, struct hostent **hp) { +int socket_gethostbyname(const char *addr, struct hostent **hp) { *hp = gethostbyname(addr); if (*hp) return IO_DONE; else if (h_errno) return h_errno; @@ -339,7 +340,7 @@ int sock_gethostbyname(const char *addr, struct hostent **hp) { * Error translation functions * Make sure important error messages are standard \*-------------------------------------------------------------------------*/ -const char *sock_hoststrerror(int err) { +const char *socket_hoststrerror(int err) { if (err <= 0) return io_strerror(err); switch (err) { case HOST_NOT_FOUND: return "host not found"; @@ -347,7 +348,7 @@ const char *sock_hoststrerror(int err) { } } -const char *sock_strerror(int err) { +const char *socket_strerror(int err) { if (err <= 0) return io_strerror(err); switch (err) { case EADDRINUSE: return "address already in use"; @@ -361,7 +362,7 @@ const char *sock_strerror(int err) { } } -const char *sock_ioerror(p_sock ps, int err) { +const char *socket_ioerror(p_socket ps, int err) { (void) ps; - return sock_strerror(err); + return socket_strerror(err); } diff --git a/src/usocket.h b/src/usocket.h index d3d4f48..e1f7498 100644 --- a/src/usocket.h +++ b/src/usocket.h @@ -32,9 +32,9 @@ /* TCP options (nagle algorithm disable) */ #include -typedef int t_sock; -typedef t_sock *p_sock; +typedef int t_socket; +typedef t_socket *p_socket; -#define SOCK_INVALID (-1) +#define SOCKET_INVALID (-1) #endif /* USOCKET_H */ diff --git a/src/wsocket.c b/src/wsocket.c index b533eae..a690dad 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -17,7 +17,7 @@ static const char *wstrerror(int err); /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ -int sock_open(void) { +int socket_open(void) { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); int err = WSAStartup(wVersionRequested, &wsaData ); @@ -33,7 +33,7 @@ int sock_open(void) { /*-------------------------------------------------------------------------*\ * Close module \*-------------------------------------------------------------------------*/ -int sock_close(void) { +int socket_close(void) { WSACleanup(); return 1; } @@ -46,7 +46,7 @@ int sock_close(void) { #define WAITFD_E 4 #define WAITFD_C (WAITFD_E|WAITFD_W) -int sock_waitfd(p_sock ps, int sw, p_tm tm) { +int socket_waitfd(p_socket ps, int sw, p_tm tm) { int ret; fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; struct timeval tv, *tp = NULL; @@ -70,7 +70,7 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) { /*-------------------------------------------------------------------------*\ * Select with int timeout in ms \*-------------------------------------------------------------------------*/ -int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { +int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { struct timeval tv; double t = tm_get(tm); tv.tv_sec = (int) t; @@ -84,39 +84,39 @@ int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { /*-------------------------------------------------------------------------*\ * Close and inutilize socket \*-------------------------------------------------------------------------*/ -void sock_destroy(p_sock ps) { - if (*ps != SOCK_INVALID) { - sock_setblocking(ps); /* close can take a long time on WIN32 */ +void socket_destroy(p_socket ps) { + if (*ps != SOCKET_INVALID) { + socket_setblocking(ps); /* close can take a long time on WIN32 */ closesocket(*ps); - *ps = SOCK_INVALID; + *ps = SOCKET_INVALID; } } /*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ -void sock_shutdown(p_sock ps, int how) { - sock_setblocking(ps); +void socket_shutdown(p_socket ps, int how) { + socket_setblocking(ps); shutdown(*ps, how); - sock_setnonblocking(ps); + socket_setnonblocking(ps); } /*-------------------------------------------------------------------------*\ * Creates and sets up a socket \*-------------------------------------------------------------------------*/ -int sock_create(p_sock ps, int domain, int type, int protocol) { +int socket_create(p_socket ps, int domain, int type, int protocol) { *ps = socket(domain, type, protocol); - if (*ps != SOCK_INVALID) return IO_DONE; + if (*ps != SOCKET_INVALID) return IO_DONE; else return WSAGetLastError(); } /*-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/ -int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { +int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) { int err; /* don't call on closed socket */ - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; /* ask system to connect */ if (connect(*ps, addr, len) == 0) return IO_DONE; /* make sure the system is trying to connect */ @@ -125,13 +125,13 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { /* zero timeout case optimization */ if (tm_iszero(tm)) return IO_TIMEOUT; /* we wait until something happens */ - err = sock_waitfd(ps, WAITFD_C, tm); + err = socket_waitfd(ps, WAITFD_C, tm); if (err == IO_CLOSED) { int len = sizeof(err); /* give windows time to set the error (yes, disgusting) */ Sleep(10); /* find out why we failed */ - getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); + getsockopt(*ps, SOL_SOCKETET, SO_ERROR, (char *)&err, &len); /* we KNOW there was an error. if 'why' is 0, we will return * "unknown error", but it's not really our fault */ return err > 0? err: IO_UNKNOWN; @@ -142,44 +142,44 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { /*-------------------------------------------------------------------------*\ * Binds or returns error message \*-------------------------------------------------------------------------*/ -int sock_bind(p_sock ps, SA *addr, socklen_t len) { +int socket_bind(p_socket ps, SA *addr, socklen_t len) { int err = IO_DONE; - sock_setblocking(ps); + socket_setblocking(ps); if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); - sock_setnonblocking(ps); + socket_setnonblocking(ps); return err; } /*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ -int sock_listen(p_sock ps, int backlog) { +int socket_listen(p_socket ps, int backlog) { int err = IO_DONE; - sock_setblocking(ps); + socket_setblocking(ps); if (listen(*ps, backlog) < 0) err = WSAGetLastError(); - sock_setnonblocking(ps); + socket_setnonblocking(ps); return err; } /*-------------------------------------------------------------------------*\ * Accept with timeout \*-------------------------------------------------------------------------*/ -int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { +int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) { SA daddr; socklen_t dlen = sizeof(daddr); - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; if (!addr) addr = &daddr; if (!len) len = &dlen; for ( ;; ) { int err; /* try to get client socket */ - if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE; + if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; /* find out why we failed */ err = WSAGetLastError(); /* if we failed because there was no connectoin, keep trying */ if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err; /* call select to avoid busy wait */ - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } /* can't reach here */ return IO_UNKNOWN; @@ -191,11 +191,12 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { * this can take an awful lot of time and we will end up blocked. * Therefore, whoever calls this function should not pass a huge buffer. \*-------------------------------------------------------------------------*/ -int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_tm tm) { int err; /* avoid making system calls on closed sockets */ - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; /* loop until we send something or we give up on error */ *sent = 0; for ( ;; ) { @@ -211,7 +212,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) /* we can only proceed if there was no serious error */ if (err != WSAEWOULDBLOCK) return err; /* avoid busy wait */ - if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; } /* can't reach here */ return IO_UNKNOWN; @@ -220,11 +221,11 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) /*-------------------------------------------------------------------------*\ * Sendto with timeout \*-------------------------------------------------------------------------*/ -int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, +int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, SA *addr, socklen_t len, p_tm tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; *sent = 0; for ( ;; ) { int put = sendto(*ps, data, (int) count, 0, addr, len); @@ -234,7 +235,7 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, } err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) return err; - if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -242,9 +243,9 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, /*-------------------------------------------------------------------------*\ * Receive with timeout \*-------------------------------------------------------------------------*/ -int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; *got = 0; for ( ;; ) { int taken = recv(*ps, data, (int) count, 0); @@ -255,7 +256,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { if (taken == 0) return IO_CLOSED; err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) return err; - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -263,10 +264,10 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { /*-------------------------------------------------------------------------*\ * Recvfrom with timeout \*-------------------------------------------------------------------------*/ -int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, +int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, SA *addr, socklen_t *len, p_tm tm) { int err; - if (*ps == SOCK_INVALID) return IO_CLOSED; + if (*ps == SOCKET_INVALID) return IO_CLOSED; *got = 0; for ( ;; ) { int taken = recvfrom(*ps, data, (int) count, 0, addr, len); @@ -277,7 +278,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, if (taken == 0) return IO_CLOSED; err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) return err; - if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; } return IO_UNKNOWN; } @@ -285,7 +286,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, /*-------------------------------------------------------------------------*\ * Put socket into blocking mode \*-------------------------------------------------------------------------*/ -void sock_setblocking(p_sock ps) { +void socket_setblocking(p_socket ps) { u_long argp = 0; ioctlsocket(*ps, FIONBIO, &argp); } @@ -293,7 +294,7 @@ void sock_setblocking(p_sock ps) { /*-------------------------------------------------------------------------*\ * Put socket into non-blocking mode \*-------------------------------------------------------------------------*/ -void sock_setnonblocking(p_sock ps) { +void socket_setnonblocking(p_socket ps) { u_long argp = 1; ioctlsocket(*ps, FIONBIO, &argp); } @@ -301,13 +302,13 @@ void sock_setnonblocking(p_sock ps) { /*-------------------------------------------------------------------------*\ * DNS helpers \*-------------------------------------------------------------------------*/ -int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { *hp = gethostbyaddr(addr, len, AF_INET); if (*hp) return IO_DONE; else return WSAGetLastError(); } -int sock_gethostbyname(const char *addr, struct hostent **hp) { +int socket_gethostbyname(const char *addr, struct hostent **hp) { *hp = gethostbyname(addr); if (*hp) return IO_DONE; else return WSAGetLastError(); @@ -316,7 +317,7 @@ int sock_gethostbyname(const char *addr, struct hostent **hp) { /*-------------------------------------------------------------------------*\ * Error translation functions \*-------------------------------------------------------------------------*/ -const char *sock_hoststrerror(int err) { +const char *socket_hoststrerror(int err) { if (err <= 0) return io_strerror(err); switch (err) { case WSAHOST_NOT_FOUND: return "host not found"; @@ -324,7 +325,7 @@ const char *sock_hoststrerror(int err) { } } -const char *sock_strerror(int err) { +const char *socket_strerror(int err) { if (err <= 0) return io_strerror(err); switch (err) { case WSAEADDRINUSE: return "address already in use"; @@ -338,9 +339,9 @@ const char *sock_strerror(int err) { } } -const char *sock_ioerror(p_sock ps, int err) { +const char *socket_ioerror(p_socket ps, int err) { (void) ps; - return sock_strerror(err); + return socket_strerror(err); } static const char *wstrerror(int err) { diff --git a/src/wsocket.h b/src/wsocket.h index c048c58..e4f0e92 100644 --- a/src/wsocket.h +++ b/src/wsocket.h @@ -13,9 +13,9 @@ #include typedef int socklen_t; -typedef SOCKET t_sock; -typedef t_sock *p_sock; +typedef SOCKET t_socket; +typedef t_socket *p_socket; -#define SOCK_INVALID (INVALID_SOCKET) +#define SOCKET_INVALID (INVALID_SOCKET) #endif /* WSOCKET_H */ diff --git a/test/ftptest.lua b/test/ftptest.lua index 63d20e6..f35e16f 100644 --- a/test/ftptest.lua +++ b/test/ftptest.lua @@ -55,27 +55,27 @@ assert(not err and back == index, err) print("ok") io.write("erasing before upload: ") -ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") +ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html") if not ret then print(err) else print("ok") end io.write("testing upload: ") -ret, err = ftp.put("ftp://luasocket:password@" .. host .. "/index.up.html;type=i", index) +ret, err = ftp.put("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i", index) assert(ret and not err, err) print("ok") io.write("downloading uploaded file: ") -back, err = ftp.get("ftp://luasocket:password@" .. host .. "/index.up.html;type=i") +back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i") assert(ret and not err and index == back, err) print("ok") io.write("erasing after upload/download: ") -ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") +ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html") assert(ret and not err, err) print("ok") io.write("testing weird-character translation: ") -back, err = ftp.get("ftp://luasocket:password@" .. host .. "/%23%3f;type=i") +back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/%23%3f;type=i") assert(not err and back == index, err) print("ok") @@ -84,7 +84,7 @@ local back = {} ret, err = ftp.get{ url = "//stupid:mistake@" .. host .. "/index.html", user = "luasocket", - password = "password", + password = "pedrovian", type = "i", sink = ltn12.sink.table(back) }