aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
commitf4dadea763c1959a27dead24df3ee6c54c209842 (patch)
treec13b294a8ca5438d59b60e3f5a25a4f7c1fc9a1b
parent562d8cceb704a96a7b2f9acc4bc229ab9f5c6541 (diff)
downloadluasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.gz
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.bz2
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.zip
Before compiling on Windows.
-rw-r--r--TODO1
-rw-r--r--config20
-rw-r--r--src/auxiliar.c32
-rw-r--r--src/auxiliar.h26
-rw-r--r--src/buffer.c74
-rw-r--r--src/buffer.h22
-rw-r--r--src/inet.c40
-rw-r--r--src/inet.h12
-rw-r--r--src/io.h4
-rw-r--r--src/ltn12.lua52
-rw-r--r--src/luasocket.c10
-rw-r--r--src/options.c36
-rw-r--r--src/options.h26
-rw-r--r--src/select.c8
-rw-r--r--src/socket.h56
-rw-r--r--src/tcp.c114
-rw-r--r--src/tcp.h6
-rw-r--r--src/timeout.c44
-rw-r--r--src/timeout.h30
-rw-r--r--src/udp.c88
-rw-r--r--src/udp.h4
-rw-r--r--src/usocket.c113
-rw-r--r--src/usocket.h6
-rw-r--r--src/wsocket.c95
-rw-r--r--src/wsocket.h6
-rw-r--r--test/ftptest.lua12
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 @@
1
1fix unix.c to return just a function 2fix unix.c to return just a function
2get rid of setmetatable(, nil) since packages don't need this anymore in 3get rid of setmetatable(, nil) since packages don't need this anymore in
35.1 45.1
diff --git a/config b/config
index 175a885..255477b 100644
--- a/config
+++ b/config
@@ -37,20 +37,20 @@ INSTALL_EXEC=cp
37# Compiler and linker settings 37# Compiler and linker settings
38# for Mac OS X 38# for Mac OS X
39# 39#
40#CC=gcc 40CC=gcc
41#DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN 41DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN
42#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common 42CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common
43#LDFLAGS=-bundle -undefined dynamic_lookup 43LDFLAGS=-bundle -undefined dynamic_lookup
44#LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc 44LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
45 45
46#------ 46#------
47# Compiler and linker settings 47# Compiler and linker settings
48# for Linux 48# for Linux
49CC=gcc 49#CC=gcc
50DEF=-DLUASOCKET_DEBUG 50#DEF=-DLUASOCKET_DEBUG
51CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic 51#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic
52LDFLAGS=-O -shared 52#LDFLAGS=-O -shared
53LD=gcc 53#LD=gcc
54 54
55#------ 55#------
56# End of makefile configuration 56# 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 @@
15/*-------------------------------------------------------------------------*\ 15/*-------------------------------------------------------------------------*\
16* Initializes the module 16* Initializes the module
17\*-------------------------------------------------------------------------*/ 17\*-------------------------------------------------------------------------*/
18int aux_open(lua_State *L) { 18int auxiliar_open(lua_State *L) {
19 (void) L; 19 (void) L;
20 return 0; 20 return 0;
21} 21}
@@ -24,7 +24,7 @@ int aux_open(lua_State *L) {
24* Creates a new class with given methods 24* Creates a new class with given methods
25* Methods whose names start with __ are passed directly to the metatable. 25* Methods whose names start with __ are passed directly to the metatable.
26\*-------------------------------------------------------------------------*/ 26\*-------------------------------------------------------------------------*/
27void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) { 27void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func) {
28 luaL_newmetatable(L, classname); /* mt */ 28 luaL_newmetatable(L, classname); /* mt */
29 /* create __index table to place methods */ 29 /* create __index table to place methods */
30 lua_pushstring(L, "__index"); /* mt,"__index" */ 30 lua_pushstring(L, "__index"); /* mt,"__index" */
@@ -47,7 +47,7 @@ void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) {
47/*-------------------------------------------------------------------------*\ 47/*-------------------------------------------------------------------------*\
48* Prints the value of a class in a nice way 48* Prints the value of a class in a nice way
49\*-------------------------------------------------------------------------*/ 49\*-------------------------------------------------------------------------*/
50int aux_tostring(lua_State *L) { 50int auxiliar_tostring(lua_State *L) {
51 char buf[32]; 51 char buf[32];
52 if (!lua_getmetatable(L, 1)) goto error; 52 if (!lua_getmetatable(L, 1)) goto error;
53 lua_pushstring(L, "__index"); 53 lua_pushstring(L, "__index");
@@ -68,7 +68,7 @@ error:
68/*-------------------------------------------------------------------------*\ 68/*-------------------------------------------------------------------------*\
69* Insert class into group 69* Insert class into group
70\*-------------------------------------------------------------------------*/ 70\*-------------------------------------------------------------------------*/
71void aux_add2group(lua_State *L, const char *classname, const char *groupname) { 71void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
72 luaL_getmetatable(L, classname); 72 luaL_getmetatable(L, classname);
73 lua_pushstring(L, groupname); 73 lua_pushstring(L, groupname);
74 lua_pushboolean(L, 1); 74 lua_pushboolean(L, 1);
@@ -79,7 +79,7 @@ void aux_add2group(lua_State *L, const char *classname, const char *groupname) {
79/*-------------------------------------------------------------------------*\ 79/*-------------------------------------------------------------------------*\
80* Make sure argument is a boolean 80* Make sure argument is a boolean
81\*-------------------------------------------------------------------------*/ 81\*-------------------------------------------------------------------------*/
82int aux_checkboolean(lua_State *L, int objidx) { 82int auxiliar_checkboolean(lua_State *L, int objidx) {
83 if (!lua_isboolean(L, objidx)) 83 if (!lua_isboolean(L, objidx))
84 luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); 84 luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
85 return lua_toboolean(L, objidx); 85 return lua_toboolean(L, objidx);
@@ -89,8 +89,8 @@ int aux_checkboolean(lua_State *L, int objidx) {
89* Return userdata pointer if object belongs to a given class, abort with 89* Return userdata pointer if object belongs to a given class, abort with
90* error otherwise 90* error otherwise
91\*-------------------------------------------------------------------------*/ 91\*-------------------------------------------------------------------------*/
92void *aux_checkclass(lua_State *L, const char *classname, int objidx) { 92void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
93 void *data = aux_getclassudata(L, classname, objidx); 93 void *data = auxiliar_getclassudata(L, classname, objidx);
94 if (!data) { 94 if (!data) {
95 char msg[45]; 95 char msg[45];
96 sprintf(msg, "%.35s expected", classname); 96 sprintf(msg, "%.35s expected", classname);
@@ -103,8 +103,8 @@ void *aux_checkclass(lua_State *L, const char *classname, int objidx) {
103* Return userdata pointer if object belongs to a given group, abort with 103* Return userdata pointer if object belongs to a given group, abort with
104* error otherwise 104* error otherwise
105\*-------------------------------------------------------------------------*/ 105\*-------------------------------------------------------------------------*/
106void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) { 106void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
107 void *data = aux_getgroupudata(L, groupname, objidx); 107 void *data = auxiliar_getgroupudata(L, groupname, objidx);
108 if (!data) { 108 if (!data) {
109 char msg[45]; 109 char msg[45];
110 sprintf(msg, "%.35s expected", groupname); 110 sprintf(msg, "%.35s expected", groupname);
@@ -116,7 +116,7 @@ void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) {
116/*-------------------------------------------------------------------------*\ 116/*-------------------------------------------------------------------------*\
117* Set object class 117* Set object class
118\*-------------------------------------------------------------------------*/ 118\*-------------------------------------------------------------------------*/
119void aux_setclass(lua_State *L, const char *classname, int objidx) { 119void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
120 luaL_getmetatable(L, classname); 120 luaL_getmetatable(L, classname);
121 if (objidx < 0) objidx--; 121 if (objidx < 0) objidx--;
122 lua_setmetatable(L, objidx); 122 lua_setmetatable(L, objidx);
@@ -126,10 +126,7 @@ void aux_setclass(lua_State *L, const char *classname, int objidx) {
126* Get a userdata pointer if object belongs to a given group. Return NULL 126* Get a userdata pointer if object belongs to a given group. Return NULL
127* otherwise 127* otherwise
128\*-------------------------------------------------------------------------*/ 128\*-------------------------------------------------------------------------*/
129void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) { 129void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
130#if 0
131 return lua_touserdata(L, objidx);
132#else
133 if (!lua_getmetatable(L, objidx)) 130 if (!lua_getmetatable(L, objidx))
134 return NULL; 131 return NULL;
135 lua_pushstring(L, groupname); 132 lua_pushstring(L, groupname);
@@ -141,17 +138,12 @@ void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) {
141 lua_pop(L, 2); 138 lua_pop(L, 2);
142 return lua_touserdata(L, objidx); 139 return lua_touserdata(L, objidx);
143 } 140 }
144#endif
145} 141}
146 142
147/*-------------------------------------------------------------------------*\ 143/*-------------------------------------------------------------------------*\
148* Get a userdata pointer if object belongs to a given class. Return NULL 144* Get a userdata pointer if object belongs to a given class. Return NULL
149* otherwise 145* otherwise
150\*-------------------------------------------------------------------------*/ 146\*-------------------------------------------------------------------------*/
151void *aux_getclassudata(lua_State *L, const char *classname, int objidx) { 147void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
152#if 0
153 return lua_touserdata(L, objidx);
154#else
155 return luaL_checkudata(L, objidx, classname); 148 return luaL_checkudata(L, objidx, classname);
156#endif
157} 149}
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 @@
1#ifndef AUX_H 1#ifndef AUXILIAR_H
2#define AUX_H 2#define AUXILIAR_H
3/*=========================================================================*\ 3/*=========================================================================*\
4* Auxiliar routines for class hierarchy manipulation 4* Auxiliar routines for class hierarchy manipulation
5* LuaSocket toolkit (but completely independent of other LuaSocket modules) 5* LuaSocket toolkit (but completely independent of other LuaSocket modules)
@@ -34,15 +34,15 @@
34#include "lua.h" 34#include "lua.h"
35#include "lauxlib.h" 35#include "lauxlib.h"
36 36
37int aux_open(lua_State *L); 37int auxiliar_open(lua_State *L);
38void aux_newclass(lua_State *L, const char *classname, luaL_reg *func); 38void auxiliar_newclass(lua_State *L, const char *classname, luaL_reg *func);
39void aux_add2group(lua_State *L, const char *classname, const char *group); 39void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
40void aux_setclass(lua_State *L, const char *classname, int objidx); 40void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
41void *aux_checkclass(lua_State *L, const char *classname, int objidx); 41void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
42void *aux_checkgroup(lua_State *L, const char *groupname, int objidx); 42void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx);
43void *aux_getclassudata(lua_State *L, const char *groupname, int objidx); 43void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
44void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx); 44void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx);
45int aux_checkboolean(lua_State *L, int objidx); 45int auxiliar_checkboolean(lua_State *L, int objidx);
46int aux_tostring(lua_State *L); 46int auxiliar_tostring(lua_State *L);
47 47
48#endif /* AUX_H */ 48#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 @@
12/*=========================================================================*\ 12/*=========================================================================*\
13* Internal function prototypes 13* Internal function prototypes
14\*=========================================================================*/ 14\*=========================================================================*/
15static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b); 15static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b);
16static int recvline(p_buf buf, luaL_Buffer *b); 16static int recvline(p_buffer buf, luaL_Buffer *b);
17static int recvall(p_buf buf, luaL_Buffer *b); 17static int recvall(p_buffer buf, luaL_Buffer *b);
18static int buf_get(p_buf buf, const char **data, size_t *count); 18static int buffer_get(p_buffer buf, const char **data, size_t *count);
19static void buf_skip(p_buf buf, size_t count); 19static void buffer_skip(p_buffer buf, size_t count);
20static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent); 20static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
21 21
22/* min and max macros */ 22/* min and max macros */
23#ifndef MIN 23#ifndef MIN
@@ -33,7 +33,7 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent);
33/*-------------------------------------------------------------------------*\ 33/*-------------------------------------------------------------------------*\
34* Initializes module 34* Initializes module
35\*-------------------------------------------------------------------------*/ 35\*-------------------------------------------------------------------------*/
36int buf_open(lua_State *L) { 36int buffer_open(lua_State *L) {
37 (void) L; 37 (void) L;
38 return 0; 38 return 0;
39} 39}
@@ -41,31 +41,31 @@ int buf_open(lua_State *L) {
41/*-------------------------------------------------------------------------*\ 41/*-------------------------------------------------------------------------*\
42* Initializes C structure 42* Initializes C structure
43\*-------------------------------------------------------------------------*/ 43\*-------------------------------------------------------------------------*/
44void buf_init(p_buf buf, p_io io, p_tm tm) { 44void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
45 buf->first = buf->last = 0; 45 buf->first = buf->last = 0;
46 buf->io = io; 46 buf->io = io;
47 buf->tm = tm; 47 buf->tm = tm;
48 buf->received = buf->sent = 0; 48 buf->received = buf->sent = 0;
49 buf->birthday = tm_gettime(); 49 buf->birthday = timeout_gettime();
50} 50}
51 51
52/*-------------------------------------------------------------------------*\ 52/*-------------------------------------------------------------------------*\
53* object:getstats() interface 53* object:getstats() interface
54\*-------------------------------------------------------------------------*/ 54\*-------------------------------------------------------------------------*/
55int buf_meth_getstats(lua_State *L, p_buf buf) { 55int buffer_meth_getstats(lua_State *L, p_buffer buf) {
56 lua_pushnumber(L, buf->received); 56 lua_pushnumber(L, buf->received);
57 lua_pushnumber(L, buf->sent); 57 lua_pushnumber(L, buf->sent);
58 lua_pushnumber(L, tm_gettime() - buf->birthday); 58 lua_pushnumber(L, timeout_gettime() - buf->birthday);
59 return 3; 59 return 3;
60} 60}
61 61
62/*-------------------------------------------------------------------------*\ 62/*-------------------------------------------------------------------------*\
63* object:setstats() interface 63* object:setstats() interface
64\*-------------------------------------------------------------------------*/ 64\*-------------------------------------------------------------------------*/
65int buf_meth_setstats(lua_State *L, p_buf buf) { 65int buffer_meth_setstats(lua_State *L, p_buffer buf) {
66 buf->received = (long) luaL_optnumber(L, 2, buf->received); 66 buf->received = (long) luaL_optnumber(L, 2, buf->received);
67 buf->sent = (long) luaL_optnumber(L, 3, buf->sent); 67 buf->sent = (long) luaL_optnumber(L, 3, buf->sent);
68 if (lua_isnumber(L, 4)) buf->birthday = tm_gettime() - lua_tonumber(L, 4); 68 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
69 lua_pushnumber(L, 1); 69 lua_pushnumber(L, 1);
70 return 1; 70 return 1;
71} 71}
@@ -73,9 +73,9 @@ int buf_meth_setstats(lua_State *L, p_buf buf) {
73/*-------------------------------------------------------------------------*\ 73/*-------------------------------------------------------------------------*\
74* object:send() interface 74* object:send() interface
75\*-------------------------------------------------------------------------*/ 75\*-------------------------------------------------------------------------*/
76int buf_meth_send(lua_State *L, p_buf buf) { 76int buffer_meth_send(lua_State *L, p_buffer buf) {
77 int top = lua_gettop(L); 77 int top = lua_gettop(L);
78 p_tm tm = tm_markstart(buf->tm); 78 p_timeout tm = timeout_markstart(buf->tm);
79 int err = IO_DONE; 79 int err = IO_DONE;
80 size_t size = 0, sent = 0; 80 size_t size = 0, sent = 0;
81 const char *data = luaL_checklstring(L, 2, &size); 81 const char *data = luaL_checklstring(L, 2, &size);
@@ -98,7 +98,7 @@ int buf_meth_send(lua_State *L, p_buf buf) {
98 } 98 }
99#ifdef LUASOCKET_DEBUG 99#ifdef LUASOCKET_DEBUG
100 /* push time elapsed during operation as the last return value */ 100 /* push time elapsed during operation as the last return value */
101 lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); 101 lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
102#endif 102#endif
103 return lua_gettop(L) - top; 103 return lua_gettop(L) - top;
104} 104}
@@ -106,9 +106,9 @@ int buf_meth_send(lua_State *L, p_buf buf) {
106/*-------------------------------------------------------------------------*\ 106/*-------------------------------------------------------------------------*\
107* object:receive() interface 107* object:receive() interface
108\*-------------------------------------------------------------------------*/ 108\*-------------------------------------------------------------------------*/
109int buf_meth_receive(lua_State *L, p_buf buf) { 109int buffer_meth_receive(lua_State *L, p_buffer buf) {
110 int err = IO_DONE, top = lua_gettop(L); 110 int err = IO_DONE, top = lua_gettop(L);
111 p_tm tm = tm_markstart(buf->tm); 111 p_timeout tm = timeout_markstart(buf->tm);
112 luaL_Buffer b; 112 luaL_Buffer b;
113 size_t size; 113 size_t size;
114 const char *part = luaL_optlstring(L, 3, "", &size); 114 const char *part = luaL_optlstring(L, 3, "", &size);
@@ -141,7 +141,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
141 } 141 }
142#ifdef LUASOCKET_DEBUG 142#ifdef LUASOCKET_DEBUG
143 /* push time elapsed during operation as the last return value */ 143 /* push time elapsed during operation as the last return value */
144 lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); 144 lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
145#endif 145#endif
146 return lua_gettop(L) - top; 146 return lua_gettop(L) - top;
147} 147}
@@ -149,7 +149,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
149/*-------------------------------------------------------------------------*\ 149/*-------------------------------------------------------------------------*\
150* Determines if there is any data in the read buffer 150* Determines if there is any data in the read buffer
151\*-------------------------------------------------------------------------*/ 151\*-------------------------------------------------------------------------*/
152int buf_isempty(p_buf buf) { 152int buffer_isempty(p_buffer buf) {
153 return buf->first >= buf->last; 153 return buf->first >= buf->last;
154} 154}
155 155
@@ -160,9 +160,9 @@ int buf_isempty(p_buf buf) {
160* Sends a block of data (unbuffered) 160* Sends a block of data (unbuffered)
161\*-------------------------------------------------------------------------*/ 161\*-------------------------------------------------------------------------*/
162#define STEPSIZE 8192 162#define STEPSIZE 8192
163static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { 163static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
164 p_io io = buf->io; 164 p_io io = buf->io;
165 p_tm tm = buf->tm; 165 p_timeout tm = buf->tm;
166 size_t total = 0; 166 size_t total = 0;
167 int err = IO_DONE; 167 int err = IO_DONE;
168 while (total < count && err == IO_DONE) { 168 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) {
179/*-------------------------------------------------------------------------*\ 179/*-------------------------------------------------------------------------*\
180* Reads a fixed number of bytes (buffered) 180* Reads a fixed number of bytes (buffered)
181\*-------------------------------------------------------------------------*/ 181\*-------------------------------------------------------------------------*/
182static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { 182static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) {
183 int err = IO_DONE; 183 int err = IO_DONE;
184 size_t total = 0; 184 size_t total = 0;
185 while (total < wanted && err == IO_DONE) { 185 while (total < wanted && err == IO_DONE) {
186 size_t count; const char *data; 186 size_t count; const char *data;
187 err = buf_get(buf, &data, &count); 187 err = buffer_get(buf, &data, &count);
188 count = MIN(count, wanted - total); 188 count = MIN(count, wanted - total);
189 luaL_addlstring(b, data, count); 189 luaL_addlstring(b, data, count);
190 buf_skip(buf, count); 190 buffer_skip(buf, count);
191 total += count; 191 total += count;
192 } 192 }
193 return err; 193 return err;
@@ -196,13 +196,13 @@ static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) {
196/*-------------------------------------------------------------------------*\ 196/*-------------------------------------------------------------------------*\
197* Reads everything until the connection is closed (buffered) 197* Reads everything until the connection is closed (buffered)
198\*-------------------------------------------------------------------------*/ 198\*-------------------------------------------------------------------------*/
199static int recvall(p_buf buf, luaL_Buffer *b) { 199static int recvall(p_buffer buf, luaL_Buffer *b) {
200 int err = IO_DONE; 200 int err = IO_DONE;
201 while (err == IO_DONE) { 201 while (err == IO_DONE) {
202 const char *data; size_t count; 202 const char *data; size_t count;
203 err = buf_get(buf, &data, &count); 203 err = buffer_get(buf, &data, &count);
204 luaL_addlstring(b, data, count); 204 luaL_addlstring(b, data, count);
205 buf_skip(buf, count); 205 buffer_skip(buf, count);
206 } 206 }
207 if (err == IO_CLOSED) return IO_DONE; 207 if (err == IO_CLOSED) return IO_DONE;
208 else return err; 208 else return err;
@@ -212,11 +212,11 @@ static int recvall(p_buf buf, luaL_Buffer *b) {
212* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 212* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF
213* are not returned by the function and are discarded from the buffer 213* are not returned by the function and are discarded from the buffer
214\*-------------------------------------------------------------------------*/ 214\*-------------------------------------------------------------------------*/
215static int recvline(p_buf buf, luaL_Buffer *b) { 215static int recvline(p_buffer buf, luaL_Buffer *b) {
216 int err = IO_DONE; 216 int err = IO_DONE;
217 while (err == IO_DONE) { 217 while (err == IO_DONE) {
218 size_t count, pos; const char *data; 218 size_t count, pos; const char *data;
219 err = buf_get(buf, &data, &count); 219 err = buffer_get(buf, &data, &count);
220 pos = 0; 220 pos = 0;
221 while (pos < count && data[pos] != '\n') { 221 while (pos < count && data[pos] != '\n') {
222 /* we ignore all \r's */ 222 /* we ignore all \r's */
@@ -224,10 +224,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) {
224 pos++; 224 pos++;
225 } 225 }
226 if (pos < count) { /* found '\n' */ 226 if (pos < count) { /* found '\n' */
227 buf_skip(buf, pos+1); /* skip '\n' too */ 227 buffer_skip(buf, pos+1); /* skip '\n' too */
228 break; /* we are done */ 228 break; /* we are done */
229 } else /* reached the end of the buffer */ 229 } else /* reached the end of the buffer */
230 buf_skip(buf, pos); 230 buffer_skip(buf, pos);
231 } 231 }
232 return err; 232 return err;
233} 233}
@@ -236,10 +236,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) {
236* Skips a given number of bytes from read buffer. No data is read from the 236* Skips a given number of bytes from read buffer. No data is read from the
237* transport layer 237* transport layer
238\*-------------------------------------------------------------------------*/ 238\*-------------------------------------------------------------------------*/
239static void buf_skip(p_buf buf, size_t count) { 239static void buffer_skip(p_buffer buf, size_t count) {
240 buf->received += count; 240 buf->received += count;
241 buf->first += count; 241 buf->first += count;
242 if (buf_isempty(buf)) 242 if (buffer_isempty(buf))
243 buf->first = buf->last = 0; 243 buf->first = buf->last = 0;
244} 244}
245 245
@@ -247,11 +247,11 @@ static void buf_skip(p_buf buf, size_t count) {
247* Return any data available in buffer, or get more data from transport layer 247* Return any data available in buffer, or get more data from transport layer
248* if buffer is empty 248* if buffer is empty
249\*-------------------------------------------------------------------------*/ 249\*-------------------------------------------------------------------------*/
250static int buf_get(p_buf buf, const char **data, size_t *count) { 250static int buffer_get(p_buffer buf, const char **data, size_t *count) {
251 int err = IO_DONE; 251 int err = IO_DONE;
252 p_io io = buf->io; 252 p_io io = buf->io;
253 p_tm tm = buf->tm; 253 p_timeout tm = buf->tm;
254 if (buf_isempty(buf)) { 254 if (buffer_isempty(buf)) {
255 size_t got; 255 size_t got;
256 err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); 256 err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm);
257 buf->first = 0; 257 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 @@
26#define BUF_SIZE 8192 26#define BUF_SIZE 8192
27 27
28/* buffer control structure */ 28/* buffer control structure */
29typedef struct t_buf_ { 29typedef struct t_buffer_ {
30 double birthday; /* throttle support info: creation time, */ 30 double birthday; /* throttle support info: creation time, */
31 size_t sent, received; /* bytes sent, and bytes received */ 31 size_t sent, received; /* bytes sent, and bytes received */
32 p_io io; /* IO driver used for this buffer */ 32 p_io io; /* IO driver used for this buffer */
33 p_tm tm; /* timeout management for this buffer */ 33 p_timeout tm; /* timeout management for this buffer */
34 size_t first, last; /* index of first and last bytes of stored data */ 34 size_t first, last; /* index of first and last bytes of stored data */
35 char data[BUF_SIZE]; /* storage space for buffer data */ 35 char data[BUF_SIZE]; /* storage space for buffer data */
36} t_buf; 36} t_buffer;
37typedef t_buf *p_buf; 37typedef t_buffer *p_buffer;
38 38
39int buf_open(lua_State *L); 39int buffer_open(lua_State *L);
40void buf_init(p_buf buf, p_io io, p_tm tm); 40void buffer_init(p_buffer buf, p_io io, p_timeout tm);
41int buf_meth_send(lua_State *L, p_buf buf); 41int buffer_meth_send(lua_State *L, p_buffer buf);
42int buf_meth_receive(lua_State *L, p_buf buf); 42int buffer_meth_receive(lua_State *L, p_buffer buf);
43int buf_meth_getstats(lua_State *L, p_buf buf); 43int buffer_meth_getstats(lua_State *L, p_buffer buf);
44int buf_meth_setstats(lua_State *L, p_buf buf); 44int buffer_meth_setstats(lua_State *L, p_buffer buf);
45int buf_isempty(p_buf buf); 45int buffer_isempty(p_buffer buf);
46 46
47#endif /* BUF_H */ 47#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)
53static int inet_gethost(const char *address, struct hostent **hp) { 53static int inet_gethost(const char *address, struct hostent **hp) {
54 struct in_addr addr; 54 struct in_addr addr;
55 if (inet_aton(address, &addr)) 55 if (inet_aton(address, &addr))
56 return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp); 56 return socket_gethostbyaddr((char *) &addr, sizeof(addr), hp);
57 else 57 else
58 return sock_gethostbyname(address, hp); 58 return socket_gethostbyname(address, hp);
59} 59}
60 60
61/*-------------------------------------------------------------------------*\ 61/*-------------------------------------------------------------------------*\
@@ -68,7 +68,7 @@ static int inet_global_tohostname(lua_State *L) {
68 int err = inet_gethost(address, &hp); 68 int err = inet_gethost(address, &hp);
69 if (err != IO_DONE) { 69 if (err != IO_DONE) {
70 lua_pushnil(L); 70 lua_pushnil(L);
71 lua_pushstring(L, sock_hoststrerror(err)); 71 lua_pushstring(L, socket_hoststrerror(err));
72 return 2; 72 return 2;
73 } 73 }
74 lua_pushstring(L, hp->h_name); 74 lua_pushstring(L, hp->h_name);
@@ -87,7 +87,7 @@ static int inet_global_toip(lua_State *L)
87 int err = inet_gethost(address, &hp); 87 int err = inet_gethost(address, &hp);
88 if (err != IO_DONE) { 88 if (err != IO_DONE) {
89 lua_pushnil(L); 89 lua_pushnil(L);
90 lua_pushstring(L, sock_hoststrerror(err)); 90 lua_pushstring(L, socket_hoststrerror(err));
91 return 2; 91 return 2;
92 } 92 }
93 lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr))); 93 lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr)));
@@ -121,7 +121,7 @@ static int inet_global_gethostname(lua_State *L)
121/*-------------------------------------------------------------------------*\ 121/*-------------------------------------------------------------------------*\
122* Retrieves socket peer name 122* Retrieves socket peer name
123\*-------------------------------------------------------------------------*/ 123\*-------------------------------------------------------------------------*/
124int inet_meth_getpeername(lua_State *L, p_sock ps) 124int inet_meth_getpeername(lua_State *L, p_socket ps)
125{ 125{
126 struct sockaddr_in peer; 126 struct sockaddr_in peer;
127 socklen_t peer_len = sizeof(peer); 127 socklen_t peer_len = sizeof(peer);
@@ -138,7 +138,7 @@ int inet_meth_getpeername(lua_State *L, p_sock ps)
138/*-------------------------------------------------------------------------*\ 138/*-------------------------------------------------------------------------*\
139* Retrieves socket local name 139* Retrieves socket local name
140\*-------------------------------------------------------------------------*/ 140\*-------------------------------------------------------------------------*/
141int inet_meth_getsockname(lua_State *L, p_sock ps) 141int inet_meth_getsockname(lua_State *L, p_socket ps)
142{ 142{
143 struct sockaddr_in local; 143 struct sockaddr_in local;
144 socklen_t local_len = sizeof(local); 144 socklen_t local_len = sizeof(local);
@@ -198,15 +198,15 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
198/*-------------------------------------------------------------------------*\ 198/*-------------------------------------------------------------------------*\
199* Tries to create a new inet socket 199* Tries to create a new inet socket
200\*-------------------------------------------------------------------------*/ 200\*-------------------------------------------------------------------------*/
201const char *inet_trycreate(p_sock ps, int type) { 201const char *inet_trycreate(p_socket ps, int type) {
202 return sock_strerror(sock_create(ps, AF_INET, type, 0)); 202 return socket_strerror(socket_create(ps, AF_INET, type, 0));
203} 203}
204 204
205/*-------------------------------------------------------------------------*\ 205/*-------------------------------------------------------------------------*\
206* Tries to connect to remote address (address, port) 206* Tries to connect to remote address (address, port)
207\*-------------------------------------------------------------------------*/ 207\*-------------------------------------------------------------------------*/
208const char *inet_tryconnect(p_sock ps, const char *address, 208const char *inet_tryconnect(p_socket ps, const char *address,
209 unsigned short port, p_tm tm) 209 unsigned short port, p_timeout tm)
210{ 210{
211 struct sockaddr_in remote; 211 struct sockaddr_in remote;
212 int err; 212 int err;
@@ -217,20 +217,20 @@ const char *inet_tryconnect(p_sock ps, const char *address,
217 if (!inet_aton(address, &remote.sin_addr)) { 217 if (!inet_aton(address, &remote.sin_addr)) {
218 struct hostent *hp = NULL; 218 struct hostent *hp = NULL;
219 struct in_addr **addr; 219 struct in_addr **addr;
220 err = sock_gethostbyname(address, &hp); 220 err = socket_gethostbyname(address, &hp);
221 if (err != IO_DONE) return sock_hoststrerror(err); 221 if (err != IO_DONE) return socket_hoststrerror(err);
222 addr = (struct in_addr **) hp->h_addr_list; 222 addr = (struct in_addr **) hp->h_addr_list;
223 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); 223 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
224 } 224 }
225 } else remote.sin_family = AF_UNSPEC; 225 } else remote.sin_family = AF_UNSPEC;
226 err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); 226 err = socket_connect(ps, (SA *) &remote, sizeof(remote), tm);
227 return sock_strerror(err); 227 return socket_strerror(err);
228} 228}
229 229
230/*-------------------------------------------------------------------------*\ 230/*-------------------------------------------------------------------------*\
231* Tries to bind socket to (address, port) 231* Tries to bind socket to (address, port)
232\*-------------------------------------------------------------------------*/ 232\*-------------------------------------------------------------------------*/
233const char *inet_trybind(p_sock ps, const char *address, unsigned short port) 233const char *inet_trybind(p_socket ps, const char *address, unsigned short port)
234{ 234{
235 struct sockaddr_in local; 235 struct sockaddr_in local;
236 int err; 236 int err;
@@ -242,14 +242,14 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port)
242 if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) { 242 if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) {
243 struct hostent *hp = NULL; 243 struct hostent *hp = NULL;
244 struct in_addr **addr; 244 struct in_addr **addr;
245 err = sock_gethostbyname(address, &hp); 245 err = socket_gethostbyname(address, &hp);
246 if (err != IO_DONE) return sock_hoststrerror(err); 246 if (err != IO_DONE) return socket_hoststrerror(err);
247 addr = (struct in_addr **) hp->h_addr_list; 247 addr = (struct in_addr **) hp->h_addr_list;
248 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); 248 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
249 } 249 }
250 err = sock_bind(ps, (SA *) &local, sizeof(local)); 250 err = socket_bind(ps, (SA *) &local, sizeof(local));
251 if (err != IO_DONE) sock_destroy(ps); 251 if (err != IO_DONE) socket_destroy(ps);
252 return sock_strerror(err); 252 return socket_strerror(err);
253} 253}
254 254
255/*-------------------------------------------------------------------------*\ 255/*-------------------------------------------------------------------------*\
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 @@
26 26
27int inet_open(lua_State *L); 27int inet_open(lua_State *L);
28 28
29const char *inet_trycreate(p_sock ps, int type); 29const char *inet_trycreate(p_socket ps, int type);
30const char *inet_tryconnect(p_sock ps, const char *address, 30const char *inet_tryconnect(p_socket ps, const char *address,
31 unsigned short port, p_tm tm); 31 unsigned short port, p_timeout tm);
32const char *inet_trybind(p_sock ps, const char *address, 32const char *inet_trybind(p_socket ps, const char *address,
33 unsigned short port); 33 unsigned short port);
34 34
35int inet_meth_getpeername(lua_State *L, p_sock ps); 35int inet_meth_getpeername(lua_State *L, p_socket ps);
36int inet_meth_getsockname(lua_State *L, p_sock ps); 36int inet_meth_getsockname(lua_State *L, p_socket ps);
37 37
38#ifdef INET_ATON 38#ifdef INET_ATON
39int inet_aton(const char *cp, struct in_addr *inp); 39int 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) (
39 const char *data, /* pointer to buffer with data to send */ 39 const char *data, /* pointer to buffer with data to send */
40 size_t count, /* number of bytes to send from buffer */ 40 size_t count, /* number of bytes to send from buffer */
41 size_t *sent, /* number of bytes sent uppon return */ 41 size_t *sent, /* number of bytes sent uppon return */
42 p_tm tm /* timeout control */ 42 p_timeout tm /* timeout control */
43); 43);
44 44
45/* interface to recv function */ 45/* interface to recv function */
@@ -48,7 +48,7 @@ typedef int (*p_recv) (
48 char *data, /* pointer to buffer where data will be writen */ 48 char *data, /* pointer to buffer where data will be writen */
49 size_t count, /* number of bytes to receive into buffer */ 49 size_t count, /* number of bytes to receive into buffer */
50 size_t *got, /* number of bytes received uppon return */ 50 size_t *got, /* number of bytes received uppon return */
51 p_tm tm /* timeout control */ 51 p_timeout tm /* timeout control */
52); 52);
53 53
54/* IO driver definition */ 54/* 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)
134 end 134 end
135end 135end
136 136
137-- chains a source with a filter
138function source.chain(src, f) 137function source.chain(src, f)
139 base.assert(src and f) 138 base.assert(src and f)
140 local last_in, last_out = "", "" 139 local last_in, last_out = "", ""
140 local state = "feeding"
141 local err
141 return function() 142 return function()
142 if last_out == "" then 143 if not last_out then
143 while true do 144 base.error('source is empty!', 2)
144 local err 145 end
146 while true do
147 if state == "feeding" then
145 last_in, err = src() 148 last_in, err = src()
146 if err then return nil, err end 149 if err then return nil, err end
147 last_out = f(last_in) 150 last_out = f(last_in)
148 if last_out ~= "" then return last_out end 151 if not last_out then
149 if not last_in then 152 if last_in then
150 base.error('filter returned inappropriate ""') 153 base.error('filter returned inappropriate nil')
154 else
155 return nil
156 end
157 elseif last_out ~= "" then
158 state = "eating"
159 if last_in then last_in = "" end
160 return last_out
161 end
162 else
163 last_out = f(last_in)
164 if last_out == "" then
165 if last_in == "" then
166 state = "feeding"
167 else
168 base.error('filter returned ""')
169 end
170 elseif not last_out then
171 if last_in then
172 base.error('filter returned inappropriate nil')
173 else
174 return nil
175 end
176 else
177 return last_out
151 end 178 end
152 end 179 end
153 elseif last_out then
154 last_out = f(last_in and "")
155 if last_in and not last_out then
156 base.error('filter returned inappropriate nil')
157 end
158 if last_out == "" and not last_in then
159 base.error(base.tostring(f) .. ' returned inappropriate ""')
160 end
161 return last_out
162 else
163 base.error("source is empty", 2)
164 end 180 end
165 end 181 end
166end 182end
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);
48* Modules and functions 48* Modules and functions
49\*-------------------------------------------------------------------------*/ 49\*-------------------------------------------------------------------------*/
50static const luaL_reg mod[] = { 50static const luaL_reg mod[] = {
51 {"auxiliar", aux_open}, 51 {"auxiliar", auxiliar_open},
52 {"except", except_open}, 52 {"except", except_open},
53 {"timeout", tm_open}, 53 {"timeout", timeout_open},
54 {"buffer", buf_open}, 54 {"buffer", buffer_open},
55 {"inet", inet_open}, 55 {"inet", inet_open},
56 {"tcp", tcp_open}, 56 {"tcp", tcp_open},
57 {"udp", udp_open}, 57 {"udp", udp_open},
@@ -79,7 +79,7 @@ static int global_skip(lua_State *L) {
79\*-------------------------------------------------------------------------*/ 79\*-------------------------------------------------------------------------*/
80static int global_unload(lua_State *L) { 80static int global_unload(lua_State *L) {
81 (void) L; 81 (void) L;
82 sock_close(); 82 socket_close();
83 return 0; 83 return 0;
84} 84}
85 85
@@ -87,7 +87,7 @@ static int global_unload(lua_State *L) {
87* Setup basic stuff. 87* Setup basic stuff.
88\*-------------------------------------------------------------------------*/ 88\*-------------------------------------------------------------------------*/
89static int base_open(lua_State *L) { 89static int base_open(lua_State *L) {
90 if (sock_open()) { 90 if (socket_open()) {
91 /* export functions (and leave namespace table on top of stack) */ 91 /* export functions (and leave namespace table on top of stack) */
92 luaL_openlib(L, "socket", func, 0); 92 luaL_openlib(L, "socket", func, 0);
93#ifdef LUASOCKET_DEBUG 93#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 @@
16/*=========================================================================*\ 16/*=========================================================================*\
17* Internal functions prototypes 17* Internal functions prototypes
18\*=========================================================================*/ 18\*=========================================================================*/
19static int opt_setmembership(lua_State *L, p_sock ps, int level, int name); 19static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
20static int opt_setboolean(lua_State *L, p_sock ps, int level, int name); 20static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
21static int opt_set(lua_State *L, p_sock ps, int level, int name, 21static int opt_set(lua_State *L, p_socket ps, int level, int name,
22 void *val, int len); 22 void *val, int len);
23 23
24/*=========================================================================*\ 24/*=========================================================================*\
@@ -27,7 +27,7 @@ static int opt_set(lua_State *L, p_sock ps, int level, int name,
27/*-------------------------------------------------------------------------*\ 27/*-------------------------------------------------------------------------*\
28* Calls appropriate option handler 28* Calls appropriate option handler
29\*-------------------------------------------------------------------------*/ 29\*-------------------------------------------------------------------------*/
30int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps) 30int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
31{ 31{
32 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 32 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
33 while (opt->name && strcmp(name, opt->name)) 33 while (opt->name && strcmp(name, opt->name))
@@ -41,38 +41,38 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps)
41} 41}
42 42
43/* enables reuse of local address */ 43/* enables reuse of local address */
44int opt_reuseaddr(lua_State *L, p_sock ps) 44int opt_reuseaddr(lua_State *L, p_socket ps)
45{ 45{
46 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 46 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
47} 47}
48 48
49/* disables the Naggle algorithm */ 49/* disables the Naggle algorithm */
50int opt_tcp_nodelay(lua_State *L, p_sock ps) 50int opt_tcp_nodelay(lua_State *L, p_socket ps)
51{ 51{
52 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 52 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
53} 53}
54 54
55int opt_keepalive(lua_State *L, p_sock ps) 55int opt_keepalive(lua_State *L, p_socket ps)
56{ 56{
57 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 57 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
58} 58}
59 59
60int opt_dontroute(lua_State *L, p_sock ps) 60int opt_dontroute(lua_State *L, p_socket ps)
61{ 61{
62 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 62 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
63} 63}
64 64
65int opt_broadcast(lua_State *L, p_sock ps) 65int opt_broadcast(lua_State *L, p_socket ps)
66{ 66{
67 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 67 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
68} 68}
69 69
70int opt_ip_multicast_loop(lua_State *L, p_sock ps) 70int opt_ip_multicast_loop(lua_State *L, p_socket ps)
71{ 71{
72 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 72 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
73} 73}
74 74
75int opt_linger(lua_State *L, p_sock ps) 75int opt_linger(lua_State *L, p_socket ps)
76{ 76{
77 struct linger li; /* obj, name, table */ 77 struct linger li; /* obj, name, table */
78 if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); 78 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)
89 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); 89 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
90} 90}
91 91
92int opt_ip_multicast_ttl(lua_State *L, p_sock ps) 92int opt_ip_multicast_ttl(lua_State *L, p_socket ps)
93{ 93{
94 int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ 94 int val = (int) luaL_checknumber(L, 3); /* obj, name, int */
95 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val)); 95 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val));
96} 96}
97 97
98int opt_ip_add_membership(lua_State *L, p_sock ps) 98int opt_ip_add_membership(lua_State *L, p_socket ps)
99{ 99{
100 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); 100 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
101} 101}
102 102
103int opt_ip_drop_membersip(lua_State *L, p_sock ps) 103int opt_ip_drop_membersip(lua_State *L, p_socket ps)
104{ 104{
105 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); 105 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
106} 106}
@@ -108,7 +108,7 @@ int opt_ip_drop_membersip(lua_State *L, p_sock ps)
108/*=========================================================================*\ 108/*=========================================================================*\
109* Auxiliar functions 109* Auxiliar functions
110\*=========================================================================*/ 110\*=========================================================================*/
111static int opt_setmembership(lua_State *L, p_sock ps, int level, int name) 111static int opt_setmembership(lua_State *L, p_socket ps, int level, int name)
112{ 112{
113 struct ip_mreq val; /* obj, name, table */ 113 struct ip_mreq val; /* obj, name, table */
114 if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); 114 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)
130} 130}
131 131
132static 132static
133int opt_set(lua_State *L, p_sock ps, int level, int name, void *val, int len) 133int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
134{ 134{
135 if (setsockopt(*ps, level, name, (char *) val, len) < 0) { 135 if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
136 lua_pushnil(L); 136 lua_pushnil(L);
@@ -141,9 +141,9 @@ int opt_set(lua_State *L, p_sock ps, int level, int name, void *val, int len)
141 return 1; 141 return 1;
142} 142}
143 143
144static int opt_setboolean(lua_State *L, p_sock ps, int level, int name) 144static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
145{ 145{
146 int val = aux_checkboolean(L, 3); /* obj, name, bool */ 146 int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
147 return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); 147 return opt_set(L, ps, level, name, (char *) &val, sizeof(val));
148} 148}
149 149
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 @@
16/* option registry */ 16/* option registry */
17typedef struct t_opt { 17typedef struct t_opt {
18 const char *name; 18 const char *name;
19 int (*func)(lua_State *L, p_sock ps); 19 int (*func)(lua_State *L, p_socket ps);
20} t_opt; 20} t_opt;
21typedef t_opt *p_opt; 21typedef t_opt *p_opt;
22 22
23/* supported options */ 23/* supported options */
24int opt_dontroute(lua_State *L, p_sock ps); 24int opt_dontroute(lua_State *L, p_socket ps);
25int opt_broadcast(lua_State *L, p_sock ps); 25int opt_broadcast(lua_State *L, p_socket ps);
26int opt_reuseaddr(lua_State *L, p_sock ps); 26int opt_reuseaddr(lua_State *L, p_socket ps);
27int opt_tcp_nodelay(lua_State *L, p_sock ps); 27int opt_tcp_nodelay(lua_State *L, p_socket ps);
28int opt_keepalive(lua_State *L, p_sock ps); 28int opt_keepalive(lua_State *L, p_socket ps);
29int opt_linger(lua_State *L, p_sock ps); 29int opt_linger(lua_State *L, p_socket ps);
30int opt_reuseaddr(lua_State *L, p_sock ps); 30int opt_reuseaddr(lua_State *L, p_socket ps);
31int opt_ip_multicast_ttl(lua_State *L, p_sock ps); 31int opt_ip_multicast_ttl(lua_State *L, p_socket ps);
32int opt_ip_multicast_loop(lua_State *L, p_sock ps); 32int opt_ip_multicast_loop(lua_State *L, p_socket ps);
33int opt_ip_add_membership(lua_State *L, p_sock ps); 33int opt_ip_add_membership(lua_State *L, p_socket ps);
34int opt_ip_drop_membersip(lua_State *L, p_sock ps); 34int opt_ip_drop_membersip(lua_State *L, p_socket ps);
35 35
36/* invokes the appropriate option handler */ 36/* invokes the appropriate option handler */
37int opt_meth_setoption(lua_State *L, p_opt opt, p_sock ps); 37int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
38 38
39#endif 39#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) {
51static int global_select(lua_State *L) { 51static int global_select(lua_State *L) {
52 int rtab, wtab, itab, max_fd, ret, ndirty; 52 int rtab, wtab, itab, max_fd, ret, ndirty;
53 fd_set rset, wset; 53 fd_set rset, wset;
54 t_tm tm; 54 t_timeout tm;
55 double t = luaL_optnumber(L, 3, -1); 55 double t = luaL_optnumber(L, 3, -1);
56 FD_ZERO(&rset); FD_ZERO(&wset); 56 FD_ZERO(&rset); FD_ZERO(&wset);
57 lua_settop(L, 3); 57 lua_settop(L, 3);
@@ -61,10 +61,10 @@ static int global_select(lua_State *L) {
61 max_fd = collect_fd(L, 1, -1, itab, &rset); 61 max_fd = collect_fd(L, 1, -1, itab, &rset);
62 ndirty = check_dirty(L, 1, rtab, &rset); 62 ndirty = check_dirty(L, 1, rtab, &rset);
63 t = ndirty > 0? 0.0: t; 63 t = ndirty > 0? 0.0: t;
64 tm_init(&tm, t, -1); 64 timeout_init(&tm, t, -1);
65 tm_markstart(&tm); 65 timeout_markstart(&tm);
66 max_fd = collect_fd(L, 2, max_fd, itab, &wset); 66 max_fd = collect_fd(L, 2, max_fd, itab, &wset);
67 ret = sock_select(max_fd+1, &rset, &wset, NULL, &tm); 67 ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm);
68 if (ret > 0 || ndirty > 0) { 68 if (ret > 0 || ndirty > 0) {
69 return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); 69 return_fd(L, &rset, max_fd+1, itab, rtab, ndirty);
70 return_fd(L, &wset, max_fd+1, itab, wtab, 0); 70 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 @@
1#ifndef SOCK_H 1#ifndef SOCKET_H
2#define SOCK_H 2#define SOCKET_H
3/*=========================================================================*\ 3/*=========================================================================*\
4* Socket compatibilization module 4* Socket compatibilization module
5* LuaSocket toolkit 5* LuaSocket toolkit
@@ -37,37 +37,39 @@ typedef struct sockaddr SA;
37* Functions bellow implement a comfortable platform independent 37* Functions bellow implement a comfortable platform independent
38* interface to sockets 38* interface to sockets
39\*=========================================================================*/ 39\*=========================================================================*/
40int sock_open(void); 40int socket_open(void);
41int sock_close(void); 41int socket_close(void);
42void sock_destroy(p_sock ps); 42void socket_destroy(p_socket ps);
43void sock_shutdown(p_sock ps, int how); 43void socket_shutdown(p_socket ps, int how);
44int sock_sendto(p_sock ps, const char *data, size_t count, 44int socket_sendto(p_socket ps, const char *data, size_t count,
45 size_t *sent, SA *addr, socklen_t addr_len, p_tm tm); 45 size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
46int sock_recvfrom(p_sock ps, char *data, size_t count, 46int socket_recvfrom(p_socket ps, char *data, size_t count,
47 size_t *got, SA *addr, socklen_t *addr_len, p_tm tm); 47 size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
48 48
49void sock_setnonblocking(p_sock ps); 49void socket_setnonblocking(p_socket ps);
50void sock_setblocking(p_sock ps); 50void socket_setblocking(p_socket ps);
51 51
52int sock_waitfd(p_sock ps, int sw, p_tm tm); 52int socket_waitfd(p_socket ps, int sw, p_timeout tm);
53int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); 53int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm);
54 54
55int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); 55int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
56int sock_create(p_sock ps, int domain, int type, int protocol); 56int socket_create(p_socket ps, int domain, int type, int protocol);
57int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); 57int socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
58int sock_listen(p_sock ps, int backlog); 58int socket_listen(p_socket ps, int backlog);
59int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm); 59int socket_accept(p_socket ps, p_socket pa, SA *addr,
60 socklen_t *addr_len, p_timeout tm);
60 61
61const char *sock_hoststrerror(int err); 62const char *socket_hoststrerror(int err);
62const char *sock_strerror(int err); 63const char *socket_strerror(int err);
63 64
64/* these are perfect to use with the io abstraction module 65/* these are perfect to use with the io abstraction module
65 and the buffered input module */ 66 and the buffered input module */
66int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm); 67int socket_send(p_socket ps, const char *data, size_t count,
67int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm); 68 size_t *sent, p_timeout tm);
68const char *sock_ioerror(p_sock ps, int err); 69int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
70const char *socket_ioerror(p_socket ps, int err);
69 71
70int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); 72int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
71int sock_gethostbyname(const char *addr, struct hostent **hp); 73int socket_gethostbyname(const char *addr, struct hostent **hp);
72 74
73#endif /* SOCK_H */ 75#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);
40/* tcp object methods */ 40/* tcp object methods */
41static luaL_reg tcp[] = { 41static luaL_reg tcp[] = {
42 {"__gc", meth_close}, 42 {"__gc", meth_close},
43 {"__tostring", aux_tostring}, 43 {"__tostring", auxiliar_tostring},
44 {"accept", meth_accept}, 44 {"accept", meth_accept},
45 {"bind", meth_bind}, 45 {"bind", meth_bind},
46 {"close", meth_close}, 46 {"close", meth_close},
@@ -84,13 +84,13 @@ static luaL_reg func[] = {
84int tcp_open(lua_State *L) 84int tcp_open(lua_State *L)
85{ 85{
86 /* create classes */ 86 /* create classes */
87 aux_newclass(L, "tcp{master}", tcp); 87 auxiliar_newclass(L, "tcp{master}", tcp);
88 aux_newclass(L, "tcp{client}", tcp); 88 auxiliar_newclass(L, "tcp{client}", tcp);
89 aux_newclass(L, "tcp{server}", tcp); 89 auxiliar_newclass(L, "tcp{server}", tcp);
90 /* create class groups */ 90 /* create class groups */
91 aux_add2group(L, "tcp{master}", "tcp{any}"); 91 auxiliar_add2group(L, "tcp{master}", "tcp{any}");
92 aux_add2group(L, "tcp{client}", "tcp{any}"); 92 auxiliar_add2group(L, "tcp{client}", "tcp{any}");
93 aux_add2group(L, "tcp{server}", "tcp{any}"); 93 auxiliar_add2group(L, "tcp{server}", "tcp{any}");
94 /* define library functions */ 94 /* define library functions */
95 luaL_openlib(L, NULL, func, 0); 95 luaL_openlib(L, NULL, func, 0);
96 return 0; 96 return 0;
@@ -103,23 +103,23 @@ int tcp_open(lua_State *L)
103* Just call buffered IO methods 103* Just call buffered IO methods
104\*-------------------------------------------------------------------------*/ 104\*-------------------------------------------------------------------------*/
105static int meth_send(lua_State *L) { 105static int meth_send(lua_State *L) {
106 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 106 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
107 return buf_meth_send(L, &tcp->buf); 107 return buffer_meth_send(L, &tcp->buf);
108} 108}
109 109
110static int meth_receive(lua_State *L) { 110static int meth_receive(lua_State *L) {
111 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 111 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
112 return buf_meth_receive(L, &tcp->buf); 112 return buffer_meth_receive(L, &tcp->buf);
113} 113}
114 114
115static int meth_getstats(lua_State *L) { 115static int meth_getstats(lua_State *L) {
116 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 116 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
117 return buf_meth_getstats(L, &tcp->buf); 117 return buffer_meth_getstats(L, &tcp->buf);
118} 118}
119 119
120static int meth_setstats(lua_State *L) { 120static int meth_setstats(lua_State *L) {
121 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 121 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
122 return buf_meth_setstats(L, &tcp->buf); 122 return buffer_meth_setstats(L, &tcp->buf);
123} 123}
124 124
125/*-------------------------------------------------------------------------*\ 125/*-------------------------------------------------------------------------*\
@@ -127,7 +127,7 @@ static int meth_setstats(lua_State *L) {
127\*-------------------------------------------------------------------------*/ 127\*-------------------------------------------------------------------------*/
128static int meth_setoption(lua_State *L) 128static int meth_setoption(lua_State *L)
129{ 129{
130 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 130 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
131 return opt_meth_setoption(L, opt, &tcp->sock); 131 return opt_meth_setoption(L, opt, &tcp->sock);
132} 132}
133 133
@@ -136,7 +136,7 @@ static int meth_setoption(lua_State *L)
136\*-------------------------------------------------------------------------*/ 136\*-------------------------------------------------------------------------*/
137static int meth_getfd(lua_State *L) 137static int meth_getfd(lua_State *L)
138{ 138{
139 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 139 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
140 lua_pushnumber(L, (int) tcp->sock); 140 lua_pushnumber(L, (int) tcp->sock);
141 return 1; 141 return 1;
142} 142}
@@ -144,15 +144,15 @@ static int meth_getfd(lua_State *L)
144/* this is very dangerous, but can be handy for those that are brave enough */ 144/* this is very dangerous, but can be handy for those that are brave enough */
145static int meth_setfd(lua_State *L) 145static int meth_setfd(lua_State *L)
146{ 146{
147 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 147 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
148 tcp->sock = (t_sock) luaL_checknumber(L, 2); 148 tcp->sock = (t_socket) luaL_checknumber(L, 2);
149 return 0; 149 return 0;
150} 150}
151 151
152static int meth_dirty(lua_State *L) 152static int meth_dirty(lua_State *L)
153{ 153{
154 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 154 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
155 lua_pushboolean(L, !buf_isempty(&tcp->buf)); 155 lua_pushboolean(L, !buffer_isempty(&tcp->buf));
156 return 1; 156 return 1;
157} 157}
158 158
@@ -162,25 +162,25 @@ static int meth_dirty(lua_State *L)
162\*-------------------------------------------------------------------------*/ 162\*-------------------------------------------------------------------------*/
163static int meth_accept(lua_State *L) 163static int meth_accept(lua_State *L)
164{ 164{
165 p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); 165 p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1);
166 p_tm tm = tm_markstart(&server->tm); 166 p_timeout tm = timeout_markstart(&server->tm);
167 t_sock sock; 167 t_socket sock;
168 int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); 168 int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
169 /* if successful, push client socket */ 169 /* if successful, push client socket */
170 if (err == IO_DONE) { 170 if (err == IO_DONE) {
171 p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); 171 p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
172 aux_setclass(L, "tcp{client}", -1); 172 auxiliar_setclass(L, "tcp{client}", -1);
173 /* initialize structure fields */ 173 /* initialize structure fields */
174 sock_setnonblocking(&sock); 174 socket_setnonblocking(&sock);
175 clnt->sock = sock; 175 clnt->sock = sock;
176 io_init(&clnt->io, (p_send) sock_send, (p_recv) sock_recv, 176 io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv,
177 (p_error) sock_ioerror, &clnt->sock); 177 (p_error) socket_ioerror, &clnt->sock);
178 tm_init(&clnt->tm, -1, -1); 178 timeout_init(&clnt->tm, -1, -1);
179 buf_init(&clnt->buf, &clnt->io, &clnt->tm); 179 buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
180 return 1; 180 return 1;
181 } else { 181 } else {
182 lua_pushnil(L); 182 lua_pushnil(L);
183 lua_pushstring(L, sock_strerror(err)); 183 lua_pushstring(L, socket_strerror(err));
184 return 2; 184 return 2;
185 } 185 }
186} 186}
@@ -190,7 +190,7 @@ static int meth_accept(lua_State *L)
190\*-------------------------------------------------------------------------*/ 190\*-------------------------------------------------------------------------*/
191static int meth_bind(lua_State *L) 191static int meth_bind(lua_State *L)
192{ 192{
193 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); 193 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1);
194 const char *address = luaL_checkstring(L, 2); 194 const char *address = luaL_checkstring(L, 2);
195 unsigned short port = (unsigned short) luaL_checknumber(L, 3); 195 unsigned short port = (unsigned short) luaL_checknumber(L, 3);
196 const char *err = inet_trybind(&tcp->sock, address, port); 196 const char *err = inet_trybind(&tcp->sock, address, port);
@@ -208,13 +208,13 @@ static int meth_bind(lua_State *L)
208\*-------------------------------------------------------------------------*/ 208\*-------------------------------------------------------------------------*/
209static int meth_connect(lua_State *L) 209static int meth_connect(lua_State *L)
210{ 210{
211 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 211 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
212 const char *address = luaL_checkstring(L, 2); 212 const char *address = luaL_checkstring(L, 2);
213 unsigned short port = (unsigned short) luaL_checknumber(L, 3); 213 unsigned short port = (unsigned short) luaL_checknumber(L, 3);
214 p_tm tm = tm_markstart(&tcp->tm); 214 p_timeout tm = timeout_markstart(&tcp->tm);
215 const char *err = inet_tryconnect(&tcp->sock, address, port, tm); 215 const char *err = inet_tryconnect(&tcp->sock, address, port, tm);
216 /* have to set the class even if it failed due to non-blocking connects */ 216 /* have to set the class even if it failed due to non-blocking connects */
217 aux_setclass(L, "tcp{client}", 1); 217 auxiliar_setclass(L, "tcp{client}", 1);
218 if (err) { 218 if (err) {
219 lua_pushnil(L); 219 lua_pushnil(L);
220 lua_pushstring(L, err); 220 lua_pushstring(L, err);
@@ -230,8 +230,8 @@ static int meth_connect(lua_State *L)
230\*-------------------------------------------------------------------------*/ 230\*-------------------------------------------------------------------------*/
231static int meth_close(lua_State *L) 231static int meth_close(lua_State *L)
232{ 232{
233 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 233 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
234 sock_destroy(&tcp->sock); 234 socket_destroy(&tcp->sock);
235 lua_pushnumber(L, 1); 235 lua_pushnumber(L, 1);
236 return 1; 236 return 1;
237} 237}
@@ -241,16 +241,16 @@ static int meth_close(lua_State *L)
241\*-------------------------------------------------------------------------*/ 241\*-------------------------------------------------------------------------*/
242static int meth_listen(lua_State *L) 242static int meth_listen(lua_State *L)
243{ 243{
244 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); 244 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1);
245 int backlog = (int) luaL_optnumber(L, 2, 32); 245 int backlog = (int) luaL_optnumber(L, 2, 32);
246 int err = sock_listen(&tcp->sock, backlog); 246 int err = socket_listen(&tcp->sock, backlog);
247 if (err != IO_DONE) { 247 if (err != IO_DONE) {
248 lua_pushnil(L); 248 lua_pushnil(L);
249 lua_pushstring(L, sock_strerror(err)); 249 lua_pushstring(L, socket_strerror(err));
250 return 2; 250 return 2;
251 } 251 }
252 /* turn master object into a server object */ 252 /* turn master object into a server object */
253 aux_setclass(L, "tcp{server}", 1); 253 auxiliar_setclass(L, "tcp{server}", 1);
254 lua_pushnumber(L, 1); 254 lua_pushnumber(L, 1);
255 return 1; 255 return 1;
256} 256}
@@ -260,20 +260,20 @@ static int meth_listen(lua_State *L)
260\*-------------------------------------------------------------------------*/ 260\*-------------------------------------------------------------------------*/
261static int meth_shutdown(lua_State *L) 261static int meth_shutdown(lua_State *L)
262{ 262{
263 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 263 p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1);
264 const char *how = luaL_optstring(L, 2, "both"); 264 const char *how = luaL_optstring(L, 2, "both");
265 switch (how[0]) { 265 switch (how[0]) {
266 case 'b': 266 case 'b':
267 if (strcmp(how, "both")) goto error; 267 if (strcmp(how, "both")) goto error;
268 sock_shutdown(&tcp->sock, 2); 268 socket_shutdown(&tcp->sock, 2);
269 break; 269 break;
270 case 's': 270 case 's':
271 if (strcmp(how, "send")) goto error; 271 if (strcmp(how, "send")) goto error;
272 sock_shutdown(&tcp->sock, 1); 272 socket_shutdown(&tcp->sock, 1);
273 break; 273 break;
274 case 'r': 274 case 'r':
275 if (strcmp(how, "receive")) goto error; 275 if (strcmp(how, "receive")) goto error;
276 sock_shutdown(&tcp->sock, 0); 276 socket_shutdown(&tcp->sock, 0);
277 break; 277 break;
278 } 278 }
279 lua_pushnumber(L, 1); 279 lua_pushnumber(L, 1);
@@ -288,13 +288,13 @@ error:
288\*-------------------------------------------------------------------------*/ 288\*-------------------------------------------------------------------------*/
289static int meth_getpeername(lua_State *L) 289static int meth_getpeername(lua_State *L)
290{ 290{
291 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 291 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
292 return inet_meth_getpeername(L, &tcp->sock); 292 return inet_meth_getpeername(L, &tcp->sock);
293} 293}
294 294
295static int meth_getsockname(lua_State *L) 295static int meth_getsockname(lua_State *L)
296{ 296{
297 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 297 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
298 return inet_meth_getsockname(L, &tcp->sock); 298 return inet_meth_getsockname(L, &tcp->sock);
299} 299}
300 300
@@ -303,8 +303,8 @@ static int meth_getsockname(lua_State *L)
303\*-------------------------------------------------------------------------*/ 303\*-------------------------------------------------------------------------*/
304static int meth_settimeout(lua_State *L) 304static int meth_settimeout(lua_State *L)
305{ 305{
306 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 306 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
307 return tm_meth_settimeout(L, &tcp->tm); 307 return timeout_meth_settimeout(L, &tcp->tm);
308} 308}
309 309
310/*=========================================================================*\ 310/*=========================================================================*\
@@ -315,21 +315,21 @@ static int meth_settimeout(lua_State *L)
315\*-------------------------------------------------------------------------*/ 315\*-------------------------------------------------------------------------*/
316static int global_create(lua_State *L) 316static int global_create(lua_State *L)
317{ 317{
318 t_sock sock; 318 t_socket sock;
319 const char *err = inet_trycreate(&sock, SOCK_STREAM); 319 const char *err = inet_trycreate(&sock, SOCK_STREAM);
320 /* try to allocate a system socket */ 320 /* try to allocate a system socket */
321 if (!err) { 321 if (!err) {
322 /* allocate tcp object */ 322 /* allocate tcp object */
323 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); 323 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
324 /* set its type as master object */ 324 /* set its type as master object */
325 aux_setclass(L, "tcp{master}", -1); 325 auxiliar_setclass(L, "tcp{master}", -1);
326 /* initialize remaining structure fields */ 326 /* initialize remaining structure fields */
327 sock_setnonblocking(&sock); 327 socket_setnonblocking(&sock);
328 tcp->sock = sock; 328 tcp->sock = sock;
329 io_init(&tcp->io, (p_send) sock_send, (p_recv) sock_recv, 329 io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
330 (p_error) sock_ioerror, &tcp->sock); 330 (p_error) socket_ioerror, &tcp->sock);
331 tm_init(&tcp->tm, -1, -1); 331 timeout_init(&tcp->tm, -1, -1);
332 buf_init(&tcp->buf, &tcp->io, &tcp->tm); 332 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
333 return 1; 333 return 1;
334 } else { 334 } else {
335 lua_pushnil(L); 335 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 @@
23#include "socket.h" 23#include "socket.h"
24 24
25typedef struct t_tcp_ { 25typedef struct t_tcp_ {
26 t_sock sock; 26 t_socket sock;
27 t_io io; 27 t_io io;
28 t_buf buf; 28 t_buffer buf;
29 t_tm tm; 29 t_timeout tm;
30} t_tcp; 30} t_tcp;
31 31
32typedef t_tcp *p_tcp; 32typedef 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 @@
30/*=========================================================================*\ 30/*=========================================================================*\
31* Internal function prototypes 31* Internal function prototypes
32\*=========================================================================*/ 32\*=========================================================================*/
33static int tm_lua_gettime(lua_State *L); 33static int timeout_lua_gettime(lua_State *L);
34static int tm_lua_sleep(lua_State *L); 34static int timeout_lua_sleep(lua_State *L);
35 35
36static luaL_reg func[] = { 36static luaL_reg func[] = {
37 { "gettime", tm_lua_gettime }, 37 { "gettime", timeout_lua_gettime },
38 { "sleep", tm_lua_sleep }, 38 { "sleep", timeout_lua_sleep },
39 { NULL, NULL } 39 { NULL, NULL }
40}; 40};
41 41
@@ -45,7 +45,7 @@ static luaL_reg func[] = {
45/*-------------------------------------------------------------------------*\ 45/*-------------------------------------------------------------------------*\
46* Initialize structure 46* Initialize structure
47\*-------------------------------------------------------------------------*/ 47\*-------------------------------------------------------------------------*/
48void tm_init(p_tm tm, double block, double total) { 48void timeout_init(p_timeout tm, double block, double total) {
49 tm->block = block; 49 tm->block = block;
50 tm->total = total; 50 tm->total = total;
51} 51}
@@ -58,16 +58,16 @@ void tm_init(p_tm tm, double block, double total) {
58* Returns 58* Returns
59* the number of ms left or -1 if there is no time limit 59* the number of ms left or -1 if there is no time limit
60\*-------------------------------------------------------------------------*/ 60\*-------------------------------------------------------------------------*/
61double tm_get(p_tm tm) { 61double timeout_get(p_timeout tm) {
62 if (tm->block < 0.0 && tm->total < 0.0) { 62 if (tm->block < 0.0 && tm->total < 0.0) {
63 return -1; 63 return -1;
64 } else if (tm->block < 0.0) { 64 } else if (tm->block < 0.0) {
65 double t = tm->total - tm_gettime() + tm->start; 65 double t = tm->total - timeout_gettime() + tm->start;
66 return MAX(t, 0.0); 66 return MAX(t, 0.0);
67 } else if (tm->total < 0.0) { 67 } else if (tm->total < 0.0) {
68 return tm->block; 68 return tm->block;
69 } else { 69 } else {
70 double t = tm->total - tm_gettime() + tm->start; 70 double t = tm->total - timeout_gettime() + tm->start;
71 return MIN(tm->block, MAX(t, 0.0)); 71 return MIN(tm->block, MAX(t, 0.0));
72 } 72 }
73} 73}
@@ -79,7 +79,7 @@ double tm_get(p_tm tm) {
79* Returns 79* Returns
80* start field of structure 80* start field of structure
81\*-------------------------------------------------------------------------*/ 81\*-------------------------------------------------------------------------*/
82double tm_getstart(p_tm tm) { 82double timeout_getstart(p_timeout tm) {
83 return tm->start; 83 return tm->start;
84} 84}
85 85
@@ -91,17 +91,17 @@ double tm_getstart(p_tm tm) {
91* Returns 91* Returns
92* the number of ms left or -1 if there is no time limit 92* the number of ms left or -1 if there is no time limit
93\*-------------------------------------------------------------------------*/ 93\*-------------------------------------------------------------------------*/
94double tm_getretry(p_tm tm) { 94double timeout_getretry(p_timeout tm) {
95 if (tm->block < 0.0 && tm->total < 0.0) { 95 if (tm->block < 0.0 && tm->total < 0.0) {
96 return -1; 96 return -1;
97 } else if (tm->block < 0.0) { 97 } else if (tm->block < 0.0) {
98 double t = tm->total - tm_gettime() + tm->start; 98 double t = tm->total - timeout_gettime() + tm->start;
99 return MAX(t, 0.0); 99 return MAX(t, 0.0);
100 } else if (tm->total < 0.0) { 100 } else if (tm->total < 0.0) {
101 double t = tm->block - tm_gettime() + tm->start; 101 double t = tm->block - timeout_gettime() + tm->start;
102 return MAX(t, 0.0); 102 return MAX(t, 0.0);
103 } else { 103 } else {
104 double t = tm->total - tm_gettime() + tm->start; 104 double t = tm->total - timeout_gettime() + tm->start;
105 return MIN(tm->block, MAX(t, 0.0)); 105 return MIN(tm->block, MAX(t, 0.0));
106 } 106 }
107} 107}
@@ -111,8 +111,8 @@ double tm_getretry(p_tm tm) {
111* Input 111* Input
112* tm: timeout control structure 112* tm: timeout control structure
113\*-------------------------------------------------------------------------*/ 113\*-------------------------------------------------------------------------*/
114p_tm tm_markstart(p_tm tm) { 114p_timeout timeout_markstart(p_timeout tm) {
115 tm->start = tm_gettime(); 115 tm->start = timeout_gettime();
116 return tm; 116 return tm;
117} 117}
118 118
@@ -122,7 +122,7 @@ p_tm tm_markstart(p_tm tm) {
122* time in s. 122* time in s.
123\*-------------------------------------------------------------------------*/ 123\*-------------------------------------------------------------------------*/
124#ifdef _WIN32 124#ifdef _WIN32
125double tm_gettime(void) { 125double timeout_gettime(void) {
126 FILETIME ft; 126 FILETIME ft;
127 double t; 127 double t;
128 GetSystemTimeAsFileTime(&ft); 128 GetSystemTimeAsFileTime(&ft);
@@ -132,7 +132,7 @@ double tm_gettime(void) {
132 return (t - 11644473600.0); 132 return (t - 11644473600.0);
133} 133}
134#else 134#else
135double tm_gettime(void) { 135double timeout_gettime(void) {
136 struct timeval v; 136 struct timeval v;
137 gettimeofday(&v, (struct timezone *) NULL); 137 gettimeofday(&v, (struct timezone *) NULL);
138 /* Unix Epoch time (time since January 1, 1970 (UTC)) */ 138 /* Unix Epoch time (time since January 1, 1970 (UTC)) */
@@ -143,7 +143,7 @@ double tm_gettime(void) {
143/*-------------------------------------------------------------------------*\ 143/*-------------------------------------------------------------------------*\
144* Initializes module 144* Initializes module
145\*-------------------------------------------------------------------------*/ 145\*-------------------------------------------------------------------------*/
146int tm_open(lua_State *L) { 146int timeout_open(lua_State *L) {
147 luaL_openlib(L, NULL, func, 0); 147 luaL_openlib(L, NULL, func, 0);
148 return 0; 148 return 0;
149} 149}
@@ -154,7 +154,7 @@ int tm_open(lua_State *L) {
154* time: time out value in seconds 154* time: time out value in seconds
155* mode: "b" for block timeout, "t" for total timeout. (default: b) 155* mode: "b" for block timeout, "t" for total timeout. (default: b)
156\*-------------------------------------------------------------------------*/ 156\*-------------------------------------------------------------------------*/
157int tm_meth_settimeout(lua_State *L, p_tm tm) { 157int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
158 double t = luaL_optnumber(L, 2, -1); 158 double t = luaL_optnumber(L, 2, -1);
159 const char *mode = luaL_optstring(L, 3, "b"); 159 const char *mode = luaL_optstring(L, 3, "b");
160 switch (*mode) { 160 switch (*mode) {
@@ -178,16 +178,16 @@ int tm_meth_settimeout(lua_State *L, p_tm tm) {
178/*-------------------------------------------------------------------------*\ 178/*-------------------------------------------------------------------------*\
179* Returns the time the system has been up, in secconds. 179* Returns the time the system has been up, in secconds.
180\*-------------------------------------------------------------------------*/ 180\*-------------------------------------------------------------------------*/
181static int tm_lua_gettime(lua_State *L) 181static int timeout_lua_gettime(lua_State *L)
182{ 182{
183 lua_pushnumber(L, tm_gettime()); 183 lua_pushnumber(L, timeout_gettime());
184 return 1; 184 return 1;
185} 185}
186 186
187/*-------------------------------------------------------------------------*\ 187/*-------------------------------------------------------------------------*\
188* Sleep for n seconds. 188* Sleep for n seconds.
189\*-------------------------------------------------------------------------*/ 189\*-------------------------------------------------------------------------*/
190int tm_lua_sleep(lua_State *L) 190int timeout_lua_sleep(lua_State *L)
191{ 191{
192 double n = luaL_checknumber(L, 1); 192 double n = luaL_checknumber(L, 1);
193#ifdef _WIN32 193#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 @@
1#ifndef TM_H 1#ifndef TIMEOUT_H
2#define TM_H 2#define TIMEOUT_H
3/*=========================================================================*\ 3/*=========================================================================*\
4* Timeout management functions 4* Timeout management functions
5* LuaSocket toolkit 5* LuaSocket toolkit
@@ -9,22 +9,22 @@
9#include "lua.h" 9#include "lua.h"
10 10
11/* timeout control structure */ 11/* timeout control structure */
12typedef struct t_tm_ { 12typedef struct t_timeout_ {
13 double block; /* maximum time for blocking calls */ 13 double block; /* maximum time for blocking calls */
14 double total; /* total number of miliseconds for operation */ 14 double total; /* total number of miliseconds for operation */
15 double start; /* time of start of operation */ 15 double start; /* time of start of operation */
16} t_tm; 16} t_timeout;
17typedef t_tm *p_tm; 17typedef t_timeout *p_timeout;
18 18
19int tm_open(lua_State *L); 19int timeout_open(lua_State *L);
20void tm_init(p_tm tm, double block, double total); 20void timeout_init(p_timeout tm, double block, double total);
21double tm_get(p_tm tm); 21double timeout_get(p_timeout tm);
22double tm_getretry(p_tm tm); 22double timeout_getretry(p_timeout tm);
23p_tm tm_markstart(p_tm tm); 23p_timeout timeout_markstart(p_timeout tm);
24double tm_getstart(p_tm tm); 24double timeout_getstart(p_timeout tm);
25double tm_gettime(void); 25double timeout_gettime(void);
26int tm_meth_settimeout(lua_State *L, p_tm tm); 26int timeout_meth_settimeout(lua_State *L, p_timeout tm);
27 27
28#define tm_iszero(tm) ((tm)->block == 0.0) 28#define timeout_iszero(tm) ((tm)->block == 0.0)
29 29
30#endif /* TM_H */ 30#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);
45/* udp object methods */ 45/* udp object methods */
46static luaL_reg udp[] = { 46static luaL_reg udp[] = {
47 {"__gc", meth_close}, 47 {"__gc", meth_close},
48 {"__tostring", aux_tostring}, 48 {"__tostring", auxiliar_tostring},
49 {"close", meth_close}, 49 {"close", meth_close},
50 {"dirty", meth_dirty}, 50 {"dirty", meth_dirty},
51 {"getfd", meth_getfd}, 51 {"getfd", meth_getfd},
@@ -87,13 +87,13 @@ static luaL_reg func[] = {
87int udp_open(lua_State *L) 87int udp_open(lua_State *L)
88{ 88{
89 /* create classes */ 89 /* create classes */
90 aux_newclass(L, "udp{connected}", udp); 90 auxiliar_newclass(L, "udp{connected}", udp);
91 aux_newclass(L, "udp{unconnected}", udp); 91 auxiliar_newclass(L, "udp{unconnected}", udp);
92 /* create class groups */ 92 /* create class groups */
93 aux_add2group(L, "udp{connected}", "udp{any}"); 93 auxiliar_add2group(L, "udp{connected}", "udp{any}");
94 aux_add2group(L, "udp{unconnected}", "udp{any}"); 94 auxiliar_add2group(L, "udp{unconnected}", "udp{any}");
95 aux_add2group(L, "udp{connected}", "select{able}"); 95 auxiliar_add2group(L, "udp{connected}", "select{able}");
96 aux_add2group(L, "udp{unconnected}", "select{able}"); 96 auxiliar_add2group(L, "udp{unconnected}", "select{able}");
97 /* define library functions */ 97 /* define library functions */
98 luaL_openlib(L, NULL, func, 0); 98 luaL_openlib(L, NULL, func, 0);
99 return 0; 99 return 0;
@@ -106,20 +106,20 @@ const char *udp_strerror(int err) {
106 /* a 'closed' error on an unconnected means the target address was not 106 /* a 'closed' error on an unconnected means the target address was not
107 * accepted by the transport layer */ 107 * accepted by the transport layer */
108 if (err == IO_CLOSED) return "refused"; 108 if (err == IO_CLOSED) return "refused";
109 else return sock_strerror(err); 109 else return socket_strerror(err);
110} 110}
111 111
112/*-------------------------------------------------------------------------*\ 112/*-------------------------------------------------------------------------*\
113* Send data through connected udp socket 113* Send data through connected udp socket
114\*-------------------------------------------------------------------------*/ 114\*-------------------------------------------------------------------------*/
115static int meth_send(lua_State *L) { 115static int meth_send(lua_State *L) {
116 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); 116 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
117 p_tm tm = &udp->tm; 117 p_timeout tm = &udp->tm;
118 size_t count, sent = 0; 118 size_t count, sent = 0;
119 int err; 119 int err;
120 const char *data = luaL_checklstring(L, 2, &count); 120 const char *data = luaL_checklstring(L, 2, &count);
121 tm_markstart(tm); 121 timeout_markstart(tm);
122 err = sock_send(&udp->sock, data, count, &sent, tm); 122 err = socket_send(&udp->sock, data, count, &sent, tm);
123 if (err != IO_DONE) { 123 if (err != IO_DONE) {
124 lua_pushnil(L); 124 lua_pushnil(L);
125 lua_pushstring(L, udp_strerror(err)); 125 lua_pushstring(L, udp_strerror(err));
@@ -133,12 +133,12 @@ static int meth_send(lua_State *L) {
133* Send data through unconnected udp socket 133* Send data through unconnected udp socket
134\*-------------------------------------------------------------------------*/ 134\*-------------------------------------------------------------------------*/
135static int meth_sendto(lua_State *L) { 135static int meth_sendto(lua_State *L) {
136 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 136 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
137 size_t count, sent = 0; 137 size_t count, sent = 0;
138 const char *data = luaL_checklstring(L, 2, &count); 138 const char *data = luaL_checklstring(L, 2, &count);
139 const char *ip = luaL_checkstring(L, 3); 139 const char *ip = luaL_checkstring(L, 3);
140 unsigned short port = (unsigned short) luaL_checknumber(L, 4); 140 unsigned short port = (unsigned short) luaL_checknumber(L, 4);
141 p_tm tm = &udp->tm; 141 p_timeout tm = &udp->tm;
142 struct sockaddr_in addr; 142 struct sockaddr_in addr;
143 int err; 143 int err;
144 memset(&addr, 0, sizeof(addr)); 144 memset(&addr, 0, sizeof(addr));
@@ -146,8 +146,8 @@ static int meth_sendto(lua_State *L) {
146 luaL_argerror(L, 3, "invalid ip address"); 146 luaL_argerror(L, 3, "invalid ip address");
147 addr.sin_family = AF_INET; 147 addr.sin_family = AF_INET;
148 addr.sin_port = htons(port); 148 addr.sin_port = htons(port);
149 tm_markstart(tm); 149 timeout_markstart(tm);
150 err = sock_sendto(&udp->sock, data, count, &sent, 150 err = socket_sendto(&udp->sock, data, count, &sent,
151 (SA *) &addr, sizeof(addr), tm); 151 (SA *) &addr, sizeof(addr), tm);
152 if (err != IO_DONE) { 152 if (err != IO_DONE) {
153 lua_pushnil(L); 153 lua_pushnil(L);
@@ -162,14 +162,14 @@ static int meth_sendto(lua_State *L) {
162* Receives data from a UDP socket 162* Receives data from a UDP socket
163\*-------------------------------------------------------------------------*/ 163\*-------------------------------------------------------------------------*/
164static int meth_receive(lua_State *L) { 164static int meth_receive(lua_State *L) {
165 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 165 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
166 char buffer[UDP_DATAGRAMSIZE]; 166 char buffer[UDP_DATAGRAMSIZE];
167 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); 167 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
168 int err; 168 int err;
169 p_tm tm = &udp->tm; 169 p_timeout tm = &udp->tm;
170 count = MIN(count, sizeof(buffer)); 170 count = MIN(count, sizeof(buffer));
171 tm_markstart(tm); 171 timeout_markstart(tm);
172 err = sock_recv(&udp->sock, buffer, count, &got, tm); 172 err = socket_recv(&udp->sock, buffer, count, &got, tm);
173 if (err != IO_DONE) { 173 if (err != IO_DONE) {
174 lua_pushnil(L); 174 lua_pushnil(L);
175 lua_pushstring(L, udp_strerror(err)); 175 lua_pushstring(L, udp_strerror(err));
@@ -183,16 +183,16 @@ static int meth_receive(lua_State *L) {
183* Receives data and sender from a UDP socket 183* Receives data and sender from a UDP socket
184\*-------------------------------------------------------------------------*/ 184\*-------------------------------------------------------------------------*/
185static int meth_receivefrom(lua_State *L) { 185static int meth_receivefrom(lua_State *L) {
186 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 186 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
187 struct sockaddr_in addr; 187 struct sockaddr_in addr;
188 socklen_t addr_len = sizeof(addr); 188 socklen_t addr_len = sizeof(addr);
189 char buffer[UDP_DATAGRAMSIZE]; 189 char buffer[UDP_DATAGRAMSIZE];
190 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); 190 size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer));
191 int err; 191 int err;
192 p_tm tm = &udp->tm; 192 p_timeout tm = &udp->tm;
193 tm_markstart(tm); 193 timeout_markstart(tm);
194 count = MIN(count, sizeof(buffer)); 194 count = MIN(count, sizeof(buffer));
195 err = sock_recvfrom(&udp->sock, buffer, count, &got, 195 err = socket_recvfrom(&udp->sock, buffer, count, &got,
196 (SA *) &addr, &addr_len, tm); 196 (SA *) &addr, &addr_len, tm);
197 if (err == IO_DONE) { 197 if (err == IO_DONE) {
198 lua_pushlstring(L, buffer, got); 198 lua_pushlstring(L, buffer, got);
@@ -210,20 +210,20 @@ static int meth_receivefrom(lua_State *L) {
210* Select support methods 210* Select support methods
211\*-------------------------------------------------------------------------*/ 211\*-------------------------------------------------------------------------*/
212static int meth_getfd(lua_State *L) { 212static int meth_getfd(lua_State *L) {
213 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 213 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
214 lua_pushnumber(L, (int) udp->sock); 214 lua_pushnumber(L, (int) udp->sock);
215 return 1; 215 return 1;
216} 216}
217 217
218/* this is very dangerous, but can be handy for those that are brave enough */ 218/* this is very dangerous, but can be handy for those that are brave enough */
219static int meth_setfd(lua_State *L) { 219static int meth_setfd(lua_State *L) {
220 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 220 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
221 udp->sock = (t_sock) luaL_checknumber(L, 2); 221 udp->sock = (t_socket) luaL_checknumber(L, 2);
222 return 0; 222 return 0;
223} 223}
224 224
225static int meth_dirty(lua_State *L) { 225static int meth_dirty(lua_State *L) {
226 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 226 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
227 (void) udp; 227 (void) udp;
228 lua_pushboolean(L, 0); 228 lua_pushboolean(L, 0);
229 return 1; 229 return 1;
@@ -233,12 +233,12 @@ static int meth_dirty(lua_State *L) {
233* Just call inet methods 233* Just call inet methods
234\*-------------------------------------------------------------------------*/ 234\*-------------------------------------------------------------------------*/
235static int meth_getpeername(lua_State *L) { 235static int meth_getpeername(lua_State *L) {
236 p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); 236 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);
237 return inet_meth_getpeername(L, &udp->sock); 237 return inet_meth_getpeername(L, &udp->sock);
238} 238}
239 239
240static int meth_getsockname(lua_State *L) { 240static int meth_getsockname(lua_State *L) {
241 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 241 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
242 return inet_meth_getsockname(L, &udp->sock); 242 return inet_meth_getsockname(L, &udp->sock);
243} 243}
244 244
@@ -246,7 +246,7 @@ static int meth_getsockname(lua_State *L) {
246* Just call option handler 246* Just call option handler
247\*-------------------------------------------------------------------------*/ 247\*-------------------------------------------------------------------------*/
248static int meth_setoption(lua_State *L) { 248static int meth_setoption(lua_State *L) {
249 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 249 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
250 return opt_meth_setoption(L, opt, &udp->sock); 250 return opt_meth_setoption(L, opt, &udp->sock);
251} 251}
252 252
@@ -254,16 +254,16 @@ static int meth_setoption(lua_State *L) {
254* Just call tm methods 254* Just call tm methods
255\*-------------------------------------------------------------------------*/ 255\*-------------------------------------------------------------------------*/
256static int meth_settimeout(lua_State *L) { 256static int meth_settimeout(lua_State *L) {
257 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 257 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
258 return tm_meth_settimeout(L, &udp->tm); 258 return timeout_meth_settimeout(L, &udp->tm);
259} 259}
260 260
261/*-------------------------------------------------------------------------*\ 261/*-------------------------------------------------------------------------*\
262* Turns a master udp object into a client object. 262* Turns a master udp object into a client object.
263\*-------------------------------------------------------------------------*/ 263\*-------------------------------------------------------------------------*/
264static int meth_setpeername(lua_State *L) { 264static int meth_setpeername(lua_State *L) {
265 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 265 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
266 p_tm tm = &udp->tm; 266 p_timeout tm = &udp->tm;
267 const char *address = luaL_checkstring(L, 2); 267 const char *address = luaL_checkstring(L, 2);
268 int connecting = strcmp(address, "*"); 268 int connecting = strcmp(address, "*");
269 unsigned short port = connecting ? 269 unsigned short port = connecting ?
@@ -276,8 +276,8 @@ static int meth_setpeername(lua_State *L) {
276 return 2; 276 return 2;
277 } 277 }
278 /* change class to connected or unconnected depending on address */ 278 /* change class to connected or unconnected depending on address */
279 if (connecting) aux_setclass(L, "udp{connected}", 1); 279 if (connecting) auxiliar_setclass(L, "udp{connected}", 1);
280 else aux_setclass(L, "udp{unconnected}", 1); 280 else auxiliar_setclass(L, "udp{unconnected}", 1);
281 lua_pushnumber(L, 1); 281 lua_pushnumber(L, 1);
282 return 1; 282 return 1;
283} 283}
@@ -286,8 +286,8 @@ static int meth_setpeername(lua_State *L) {
286* Closes socket used by object 286* Closes socket used by object
287\*-------------------------------------------------------------------------*/ 287\*-------------------------------------------------------------------------*/
288static int meth_close(lua_State *L) { 288static int meth_close(lua_State *L) {
289 p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); 289 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
290 sock_destroy(&udp->sock); 290 socket_destroy(&udp->sock);
291 lua_pushnumber(L, 1); 291 lua_pushnumber(L, 1);
292 return 1; 292 return 1;
293} 293}
@@ -296,7 +296,7 @@ static int meth_close(lua_State *L) {
296* Turns a master object into a server object 296* Turns a master object into a server object
297\*-------------------------------------------------------------------------*/ 297\*-------------------------------------------------------------------------*/
298static int meth_setsockname(lua_State *L) { 298static int meth_setsockname(lua_State *L) {
299 p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); 299 p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
300 const char *address = luaL_checkstring(L, 2); 300 const char *address = luaL_checkstring(L, 2);
301 unsigned short port = (unsigned short) luaL_checknumber(L, 3); 301 unsigned short port = (unsigned short) luaL_checknumber(L, 3);
302 const char *err = inet_trybind(&udp->sock, address, port); 302 const char *err = inet_trybind(&udp->sock, address, port);
@@ -316,17 +316,17 @@ static int meth_setsockname(lua_State *L) {
316* Creates a master udp object 316* Creates a master udp object
317\*-------------------------------------------------------------------------*/ 317\*-------------------------------------------------------------------------*/
318static int global_create(lua_State *L) { 318static int global_create(lua_State *L) {
319 t_sock sock; 319 t_socket sock;
320 const char *err = inet_trycreate(&sock, SOCK_DGRAM); 320 const char *err = inet_trycreate(&sock, SOCK_DGRAM);
321 /* try to allocate a system socket */ 321 /* try to allocate a system socket */
322 if (!err) { 322 if (!err) {
323 /* allocate tcp object */ 323 /* allocate tcp object */
324 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); 324 p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
325 aux_setclass(L, "udp{unconnected}", -1); 325 auxiliar_setclass(L, "udp{unconnected}", -1);
326 /* initialize remaining structure fields */ 326 /* initialize remaining structure fields */
327 sock_setnonblocking(&sock); 327 socket_setnonblocking(&sock);
328 udp->sock = sock; 328 udp->sock = sock;
329 tm_init(&udp->tm, -1, -1); 329 timeout_init(&udp->tm, -1, -1);
330 return 1; 330 return 1;
331 } else { 331 } else {
332 lua_pushnil(L); 332 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 @@
23#define UDP_DATAGRAMSIZE 8192 23#define UDP_DATAGRAMSIZE 8192
24 24
25typedef struct t_udp_ { 25typedef struct t_udp_ {
26 t_sock sock; 26 t_socket sock;
27 t_tm tm; 27 t_timeout tm;
28} t_udp; 28} t_udp;
29typedef t_udp *p_udp; 29typedef t_udp *p_udp;
30 30
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 @@
16/*-------------------------------------------------------------------------*\ 16/*-------------------------------------------------------------------------*\
17* Wait for readable/writable/connected socket with timeout 17* Wait for readable/writable/connected socket with timeout
18\*-------------------------------------------------------------------------*/ 18\*-------------------------------------------------------------------------*/
19#ifdef SOCK_POLL 19#ifdef SOCKET_POLL
20#include <sys/poll.h> 20#include <sys/poll.h>
21 21
22#define WAITFD_R POLLIN 22#define WAITFD_R POLLIN
23#define WAITFD_W POLLOUT 23#define WAITFD_W POLLOUT
24#define WAITFD_C (POLLIN|POLLOUT) 24#define WAITFD_C (POLLIN|POLLOUT)
25int sock_waitfd(p_sock ps, int sw, p_tm tm) { 25int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
26 int ret; 26 int ret;
27 struct pollfd pfd; 27 struct pollfd pfd;
28 pfd.fd = *ps; 28 pfd.fd = *ps;
29 pfd.events = sw; 29 pfd.events = sw;
30 pfd.revents = 0; 30 pfd.revents = 0;
31 if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 31 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
32 do { 32 do {
33 int t = (int)(tm_getretry(tm)*1e3); 33 int t = (int)(timeout_getretry(tm)*1e3);
34 ret = poll(&pfd, 1, t >= 0? t: -1); 34 ret = poll(&pfd, 1, t >= 0? t: -1);
35 } while (ret == -1 && errno == EINTR); 35 } while (ret == -1 && errno == EINTR);
36 if (ret == -1) return errno; 36 if (ret == -1) return errno;
@@ -44,18 +44,18 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) {
44#define WAITFD_W 2 44#define WAITFD_W 2
45#define WAITFD_C (WAITFD_R|WAITFD_W) 45#define WAITFD_C (WAITFD_R|WAITFD_W)
46 46
47int sock_waitfd(p_sock ps, int sw, p_tm tm) { 47int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
48 int ret; 48 int ret;
49 fd_set rfds, wfds, *rp, *wp; 49 fd_set rfds, wfds, *rp, *wp;
50 struct timeval tv, *tp; 50 struct timeval tv, *tp;
51 double t; 51 double t;
52 if (tm_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 52 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
53 do { 53 do {
54 /* must set bits within loop, because select may have modifed them */ 54 /* must set bits within loop, because select may have modifed them */
55 rp = wp = NULL; 55 rp = wp = NULL;
56 if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; } 56 if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; }
57 if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } 57 if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; }
58 t = tm_getretry(tm); 58 t = timeout_getretry(tm);
59 tp = NULL; 59 tp = NULL;
60 if (t >= 0.0) { 60 if (t >= 0.0) {
61 tv.tv_sec = (int)t; 61 tv.tv_sec = (int)t;
@@ -75,7 +75,7 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) {
75/*-------------------------------------------------------------------------*\ 75/*-------------------------------------------------------------------------*\
76* Initializes module 76* Initializes module
77\*-------------------------------------------------------------------------*/ 77\*-------------------------------------------------------------------------*/
78int sock_open(void) { 78int socket_open(void) {
79 /* instals a handler to ignore sigpipe or it will crash us */ 79 /* instals a handler to ignore sigpipe or it will crash us */
80 signal(SIGPIPE, SIG_IGN); 80 signal(SIGPIPE, SIG_IGN);
81 return 1; 81 return 1;
@@ -84,29 +84,29 @@ int sock_open(void) {
84/*-------------------------------------------------------------------------*\ 84/*-------------------------------------------------------------------------*\
85* Close module 85* Close module
86\*-------------------------------------------------------------------------*/ 86\*-------------------------------------------------------------------------*/
87int sock_close(void) { 87int socket_close(void) {
88 return 1; 88 return 1;
89} 89}
90 90
91/*-------------------------------------------------------------------------*\ 91/*-------------------------------------------------------------------------*\
92* Close and inutilize socket 92* Close and inutilize socket
93\*-------------------------------------------------------------------------*/ 93\*-------------------------------------------------------------------------*/
94void sock_destroy(p_sock ps) { 94void socket_destroy(p_socket ps) {
95 if (*ps != SOCK_INVALID) { 95 if (*ps != SOCKET_INVALID) {
96 sock_setblocking(ps); 96 socket_setblocking(ps);
97 close(*ps); 97 close(*ps);
98 *ps = SOCK_INVALID; 98 *ps = SOCKET_INVALID;
99 } 99 }
100} 100}
101 101
102/*-------------------------------------------------------------------------*\ 102/*-------------------------------------------------------------------------*\
103* Select with timeout control 103* Select with timeout control
104\*-------------------------------------------------------------------------*/ 104\*-------------------------------------------------------------------------*/
105int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { 105int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm) {
106 int ret; 106 int ret;
107 do { 107 do {
108 struct timeval tv; 108 struct timeval tv;
109 double t = tm_getretry(tm); 109 double t = timeout_getretry(tm);
110 tv.tv_sec = (int) t; 110 tv.tv_sec = (int) t;
111 tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); 111 tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6);
112 ret = select(n, rfds, wfds, efds, t >= 0.0? &tv: NULL); 112 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) {
117/*-------------------------------------------------------------------------*\ 117/*-------------------------------------------------------------------------*\
118* Creates and sets up a socket 118* Creates and sets up a socket
119\*-------------------------------------------------------------------------*/ 119\*-------------------------------------------------------------------------*/
120int sock_create(p_sock ps, int domain, int type, int protocol) { 120int socket_create(p_socket ps, int domain, int type, int protocol) {
121 *ps = socket(domain, type, protocol); 121 *ps = socket(domain, type, protocol);
122 if (*ps != SOCK_INVALID) return IO_DONE; 122 if (*ps != SOCKET_INVALID) return IO_DONE;
123 else return errno; 123 else return errno;
124} 124}
125 125
126/*-------------------------------------------------------------------------*\ 126/*-------------------------------------------------------------------------*\
127* Binds or returns error message 127* Binds or returns error message
128\*-------------------------------------------------------------------------*/ 128\*-------------------------------------------------------------------------*/
129int sock_bind(p_sock ps, SA *addr, socklen_t len) { 129int socket_bind(p_socket ps, SA *addr, socklen_t len) {
130 int err = IO_DONE; 130 int err = IO_DONE;
131 sock_setblocking(ps); 131 socket_setblocking(ps);
132 if (bind(*ps, addr, len) < 0) err = errno; 132 if (bind(*ps, addr, len) < 0) err = errno;
133 sock_setnonblocking(ps); 133 socket_setnonblocking(ps);
134 return err; 134 return err;
135} 135}
136 136
137/*-------------------------------------------------------------------------*\ 137/*-------------------------------------------------------------------------*\
138* 138*
139\*-------------------------------------------------------------------------*/ 139\*-------------------------------------------------------------------------*/
140int sock_listen(p_sock ps, int backlog) { 140int socket_listen(p_socket ps, int backlog) {
141 int err = IO_DONE; 141 int err = IO_DONE;
142 sock_setblocking(ps); 142 socket_setblocking(ps);
143 if (listen(*ps, backlog)) err = errno; 143 if (listen(*ps, backlog)) err = errno;
144 sock_setnonblocking(ps); 144 socket_setnonblocking(ps);
145 return err; 145 return err;
146} 146}
147 147
148/*-------------------------------------------------------------------------*\ 148/*-------------------------------------------------------------------------*\
149* 149*
150\*-------------------------------------------------------------------------*/ 150\*-------------------------------------------------------------------------*/
151void sock_shutdown(p_sock ps, int how) { 151void socket_shutdown(p_socket ps, int how) {
152 sock_setblocking(ps); 152 socket_setblocking(ps);
153 shutdown(*ps, how); 153 shutdown(*ps, how);
154 sock_setnonblocking(ps); 154 socket_setnonblocking(ps);
155} 155}
156 156
157/*-------------------------------------------------------------------------*\ 157/*-------------------------------------------------------------------------*\
158* Connects or returns error message 158* Connects or returns error message
159\*-------------------------------------------------------------------------*/ 159\*-------------------------------------------------------------------------*/
160int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { 160int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
161 int err; 161 int err;
162 /* avoid calling on closed sockets */ 162 /* avoid calling on closed sockets */
163 if (*ps == SOCK_INVALID) return IO_CLOSED; 163 if (*ps == SOCKET_INVALID) return IO_CLOSED;
164 /* call connect until done or failed without being interrupted */ 164 /* call connect until done or failed without being interrupted */
165 do if (connect(*ps, addr, len) == 0) return IO_DONE; 165 do if (connect(*ps, addr, len) == 0) return IO_DONE;
166 while ((err = errno) == EINTR); 166 while ((err = errno) == EINTR);
167 /* if connection failed immediately, return error code */ 167 /* if connection failed immediately, return error code */
168 if (err != EINPROGRESS && err != EAGAIN) return err; 168 if (err != EINPROGRESS && err != EAGAIN) return err;
169 /* zero timeout case optimization */ 169 /* zero timeout case optimization */
170 if (tm_iszero(tm)) return IO_TIMEOUT; 170 if (timeout_iszero(tm)) return IO_TIMEOUT;
171 /* wait until we have the result of the connection attempt or timeout */ 171 /* wait until we have the result of the connection attempt or timeout */
172 err = sock_waitfd(ps, WAITFD_C, tm); 172 err = socket_waitfd(ps, WAITFD_C, tm);
173 if (err == IO_CLOSED) { 173 if (err == IO_CLOSED) {
174 if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; 174 if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
175 else return errno; 175 else return errno;
@@ -179,19 +179,19 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
179/*-------------------------------------------------------------------------*\ 179/*-------------------------------------------------------------------------*\
180* Accept with timeout 180* Accept with timeout
181\*-------------------------------------------------------------------------*/ 181\*-------------------------------------------------------------------------*/
182int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { 182int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
183 SA daddr; 183 SA daddr;
184 socklen_t dlen = sizeof(daddr); 184 socklen_t dlen = sizeof(daddr);
185 if (*ps == SOCK_INVALID) return IO_CLOSED; 185 if (*ps == SOCKET_INVALID) return IO_CLOSED;
186 if (!addr) addr = &daddr; 186 if (!addr) addr = &daddr;
187 if (!len) len = &dlen; 187 if (!len) len = &dlen;
188 for ( ;; ) { 188 for ( ;; ) {
189 int err; 189 int err;
190 if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE; 190 if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE;
191 err = errno; 191 err = errno;
192 if (err == EINTR) continue; 192 if (err == EINTR) continue;
193 if (err != EAGAIN && err != ECONNABORTED) return err; 193 if (err != EAGAIN && err != ECONNABORTED) return err;
194 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 194 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
195 } 195 }
196 /* can't reach here */ 196 /* can't reach here */
197 return IO_UNKNOWN; 197 return IO_UNKNOWN;
@@ -200,11 +200,12 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
200/*-------------------------------------------------------------------------*\ 200/*-------------------------------------------------------------------------*\
201* Send with timeout 201* Send with timeout
202\*-------------------------------------------------------------------------*/ 202\*-------------------------------------------------------------------------*/
203int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) 203int socket_send(p_socket ps, const char *data, size_t count,
204 size_t *sent, p_timeout tm)
204{ 205{
205 int err; 206 int err;
206 /* avoid making system calls on closed sockets */ 207 /* avoid making system calls on closed sockets */
207 if (*ps == SOCK_INVALID) return IO_CLOSED; 208 if (*ps == SOCKET_INVALID) return IO_CLOSED;
208 /* loop until we send something or we give up on error */ 209 /* loop until we send something or we give up on error */
209 *sent = 0; 210 *sent = 0;
210 for ( ;; ) { 211 for ( ;; ) {
@@ -223,7 +224,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm)
223 /* if failed fatal reason, report error */ 224 /* if failed fatal reason, report error */
224 if (err != EAGAIN) return err; 225 if (err != EAGAIN) return err;
225 /* wait until we can send something or we timeout */ 226 /* wait until we can send something or we timeout */
226 if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; 227 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
227 } 228 }
228 /* can't reach here */ 229 /* can't reach here */
229 return IO_UNKNOWN; 230 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)
232/*-------------------------------------------------------------------------*\ 233/*-------------------------------------------------------------------------*\
233* Sendto with timeout 234* Sendto with timeout
234\*-------------------------------------------------------------------------*/ 235\*-------------------------------------------------------------------------*/
235int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, 236int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
236 SA *addr, socklen_t len, p_tm tm) 237 SA *addr, socklen_t len, p_timeout tm)
237{ 238{
238 int err; 239 int err;
239 if (*ps == SOCK_INVALID) return IO_CLOSED; 240 if (*ps == SOCKET_INVALID) return IO_CLOSED;
240 *sent = 0; 241 *sent = 0;
241 for ( ;; ) { 242 for ( ;; ) {
242 long put = (long) sendto(*ps, data, count, 0, addr, len); 243 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,
248 if (put == 0 || err == EPIPE) return IO_CLOSED; 249 if (put == 0 || err == EPIPE) return IO_CLOSED;
249 if (err == EINTR) continue; 250 if (err == EINTR) continue;
250 if (err != EAGAIN) return err; 251 if (err != EAGAIN) return err;
251 if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; 252 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
252 } 253 }
253 return IO_UNKNOWN; 254 return IO_UNKNOWN;
254} 255}
@@ -256,9 +257,9 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
256/*-------------------------------------------------------------------------*\ 257/*-------------------------------------------------------------------------*\
257* Receive with timeout 258* Receive with timeout
258\*-------------------------------------------------------------------------*/ 259\*-------------------------------------------------------------------------*/
259int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { 260int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
260 int err; 261 int err;
261 if (*ps == SOCK_INVALID) return IO_CLOSED; 262 if (*ps == SOCKET_INVALID) return IO_CLOSED;
262 for ( ;; ) { 263 for ( ;; ) {
263 long taken = (long) recv(*ps, data, count, 0); 264 long taken = (long) recv(*ps, data, count, 0);
264 if (taken > 0) { 265 if (taken > 0) {
@@ -270,7 +271,7 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
270 if (taken == 0) return IO_CLOSED; 271 if (taken == 0) return IO_CLOSED;
271 if (err == EINTR) continue; 272 if (err == EINTR) continue;
272 if (err != EAGAIN) return err; 273 if (err != EAGAIN) return err;
273 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 274 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
274 } 275 }
275 return IO_UNKNOWN; 276 return IO_UNKNOWN;
276} 277}
@@ -278,10 +279,10 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
278/*-------------------------------------------------------------------------*\ 279/*-------------------------------------------------------------------------*\
279* Recvfrom with timeout 280* Recvfrom with timeout
280\*-------------------------------------------------------------------------*/ 281\*-------------------------------------------------------------------------*/
281int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, 282int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
282 SA *addr, socklen_t *len, p_tm tm) { 283 SA *addr, socklen_t *len, p_timeout tm) {
283 int err; 284 int err;
284 if (*ps == SOCK_INVALID) return IO_CLOSED; 285 if (*ps == SOCKET_INVALID) return IO_CLOSED;
285 for ( ;; ) { 286 for ( ;; ) {
286 long taken = (long) recvfrom(*ps, data, count, 0, addr, len); 287 long taken = (long) recvfrom(*ps, data, count, 0, addr, len);
287 if (taken > 0) { 288 if (taken > 0) {
@@ -293,7 +294,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
293 if (taken == 0) return IO_CLOSED; 294 if (taken == 0) return IO_CLOSED;
294 if (err == EINTR) continue; 295 if (err == EINTR) continue;
295 if (err != EAGAIN) return err; 296 if (err != EAGAIN) return err;
296 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 297 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
297 } 298 }
298 return IO_UNKNOWN; 299 return IO_UNKNOWN;
299} 300}
@@ -301,7 +302,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
301/*-------------------------------------------------------------------------*\ 302/*-------------------------------------------------------------------------*\
302* Put socket into blocking mode 303* Put socket into blocking mode
303\*-------------------------------------------------------------------------*/ 304\*-------------------------------------------------------------------------*/
304void sock_setblocking(p_sock ps) { 305void socket_setblocking(p_socket ps) {
305 int flags = fcntl(*ps, F_GETFL, 0); 306 int flags = fcntl(*ps, F_GETFL, 0);
306 flags &= (~(O_NONBLOCK)); 307 flags &= (~(O_NONBLOCK));
307 fcntl(*ps, F_SETFL, flags); 308 fcntl(*ps, F_SETFL, flags);
@@ -310,7 +311,7 @@ void sock_setblocking(p_sock ps) {
310/*-------------------------------------------------------------------------*\ 311/*-------------------------------------------------------------------------*\
311* Put socket into non-blocking mode 312* Put socket into non-blocking mode
312\*-------------------------------------------------------------------------*/ 313\*-------------------------------------------------------------------------*/
313void sock_setnonblocking(p_sock ps) { 314void socket_setnonblocking(p_socket ps) {
314 int flags = fcntl(*ps, F_GETFL, 0); 315 int flags = fcntl(*ps, F_GETFL, 0);
315 flags |= O_NONBLOCK; 316 flags |= O_NONBLOCK;
316 fcntl(*ps, F_SETFL, flags); 317 fcntl(*ps, F_SETFL, flags);
@@ -319,7 +320,7 @@ void sock_setnonblocking(p_sock ps) {
319/*-------------------------------------------------------------------------*\ 320/*-------------------------------------------------------------------------*\
320* DNS helpers 321* DNS helpers
321\*-------------------------------------------------------------------------*/ 322\*-------------------------------------------------------------------------*/
322int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 323int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
323 *hp = gethostbyaddr(addr, len, AF_INET); 324 *hp = gethostbyaddr(addr, len, AF_INET);
324 if (*hp) return IO_DONE; 325 if (*hp) return IO_DONE;
325 else if (h_errno) return h_errno; 326 else if (h_errno) return h_errno;
@@ -327,7 +328,7 @@ int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
327 else return IO_UNKNOWN; 328 else return IO_UNKNOWN;
328} 329}
329 330
330int sock_gethostbyname(const char *addr, struct hostent **hp) { 331int socket_gethostbyname(const char *addr, struct hostent **hp) {
331 *hp = gethostbyname(addr); 332 *hp = gethostbyname(addr);
332 if (*hp) return IO_DONE; 333 if (*hp) return IO_DONE;
333 else if (h_errno) return h_errno; 334 else if (h_errno) return h_errno;
@@ -339,7 +340,7 @@ int sock_gethostbyname(const char *addr, struct hostent **hp) {
339* Error translation functions 340* Error translation functions
340* Make sure important error messages are standard 341* Make sure important error messages are standard
341\*-------------------------------------------------------------------------*/ 342\*-------------------------------------------------------------------------*/
342const char *sock_hoststrerror(int err) { 343const char *socket_hoststrerror(int err) {
343 if (err <= 0) return io_strerror(err); 344 if (err <= 0) return io_strerror(err);
344 switch (err) { 345 switch (err) {
345 case HOST_NOT_FOUND: return "host not found"; 346 case HOST_NOT_FOUND: return "host not found";
@@ -347,7 +348,7 @@ const char *sock_hoststrerror(int err) {
347 } 348 }
348} 349}
349 350
350const char *sock_strerror(int err) { 351const char *socket_strerror(int err) {
351 if (err <= 0) return io_strerror(err); 352 if (err <= 0) return io_strerror(err);
352 switch (err) { 353 switch (err) {
353 case EADDRINUSE: return "address already in use"; 354 case EADDRINUSE: return "address already in use";
@@ -361,7 +362,7 @@ const char *sock_strerror(int err) {
361 } 362 }
362} 363}
363 364
364const char *sock_ioerror(p_sock ps, int err) { 365const char *socket_ioerror(p_socket ps, int err) {
365 (void) ps; 366 (void) ps;
366 return sock_strerror(err); 367 return socket_strerror(err);
367} 368}
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 @@
32/* TCP options (nagle algorithm disable) */ 32/* TCP options (nagle algorithm disable) */
33#include <netinet/tcp.h> 33#include <netinet/tcp.h>
34 34
35typedef int t_sock; 35typedef int t_socket;
36typedef t_sock *p_sock; 36typedef t_socket *p_socket;
37 37
38#define SOCK_INVALID (-1) 38#define SOCKET_INVALID (-1)
39 39
40#endif /* USOCKET_H */ 40#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);
17/*-------------------------------------------------------------------------*\ 17/*-------------------------------------------------------------------------*\
18* Initializes module 18* Initializes module
19\*-------------------------------------------------------------------------*/ 19\*-------------------------------------------------------------------------*/
20int sock_open(void) { 20int socket_open(void) {
21 WSADATA wsaData; 21 WSADATA wsaData;
22 WORD wVersionRequested = MAKEWORD(2, 0); 22 WORD wVersionRequested = MAKEWORD(2, 0);
23 int err = WSAStartup(wVersionRequested, &wsaData ); 23 int err = WSAStartup(wVersionRequested, &wsaData );
@@ -33,7 +33,7 @@ int sock_open(void) {
33/*-------------------------------------------------------------------------*\ 33/*-------------------------------------------------------------------------*\
34* Close module 34* Close module
35\*-------------------------------------------------------------------------*/ 35\*-------------------------------------------------------------------------*/
36int sock_close(void) { 36int socket_close(void) {
37 WSACleanup(); 37 WSACleanup();
38 return 1; 38 return 1;
39} 39}
@@ -46,7 +46,7 @@ int sock_close(void) {
46#define WAITFD_E 4 46#define WAITFD_E 4
47#define WAITFD_C (WAITFD_E|WAITFD_W) 47#define WAITFD_C (WAITFD_E|WAITFD_W)
48 48
49int sock_waitfd(p_sock ps, int sw, p_tm tm) { 49int socket_waitfd(p_socket ps, int sw, p_tm tm) {
50 int ret; 50 int ret;
51 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; 51 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
52 struct timeval tv, *tp = NULL; 52 struct timeval tv, *tp = NULL;
@@ -70,7 +70,7 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm) {
70/*-------------------------------------------------------------------------*\ 70/*-------------------------------------------------------------------------*\
71* Select with int timeout in ms 71* Select with int timeout in ms
72\*-------------------------------------------------------------------------*/ 72\*-------------------------------------------------------------------------*/
73int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { 73int socket_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) {
74 struct timeval tv; 74 struct timeval tv;
75 double t = tm_get(tm); 75 double t = tm_get(tm);
76 tv.tv_sec = (int) t; 76 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) {
84/*-------------------------------------------------------------------------*\ 84/*-------------------------------------------------------------------------*\
85* Close and inutilize socket 85* Close and inutilize socket
86\*-------------------------------------------------------------------------*/ 86\*-------------------------------------------------------------------------*/
87void sock_destroy(p_sock ps) { 87void socket_destroy(p_socket ps) {
88 if (*ps != SOCK_INVALID) { 88 if (*ps != SOCKET_INVALID) {
89 sock_setblocking(ps); /* close can take a long time on WIN32 */ 89 socket_setblocking(ps); /* close can take a long time on WIN32 */
90 closesocket(*ps); 90 closesocket(*ps);
91 *ps = SOCK_INVALID; 91 *ps = SOCKET_INVALID;
92 } 92 }
93} 93}
94 94
95/*-------------------------------------------------------------------------*\ 95/*-------------------------------------------------------------------------*\
96* 96*
97\*-------------------------------------------------------------------------*/ 97\*-------------------------------------------------------------------------*/
98void sock_shutdown(p_sock ps, int how) { 98void socket_shutdown(p_socket ps, int how) {
99 sock_setblocking(ps); 99 socket_setblocking(ps);
100 shutdown(*ps, how); 100 shutdown(*ps, how);
101 sock_setnonblocking(ps); 101 socket_setnonblocking(ps);
102} 102}
103 103
104/*-------------------------------------------------------------------------*\ 104/*-------------------------------------------------------------------------*\
105* Creates and sets up a socket 105* Creates and sets up a socket
106\*-------------------------------------------------------------------------*/ 106\*-------------------------------------------------------------------------*/
107int sock_create(p_sock ps, int domain, int type, int protocol) { 107int socket_create(p_socket ps, int domain, int type, int protocol) {
108 *ps = socket(domain, type, protocol); 108 *ps = socket(domain, type, protocol);
109 if (*ps != SOCK_INVALID) return IO_DONE; 109 if (*ps != SOCKET_INVALID) return IO_DONE;
110 else return WSAGetLastError(); 110 else return WSAGetLastError();
111} 111}
112 112
113/*-------------------------------------------------------------------------*\ 113/*-------------------------------------------------------------------------*\
114* Connects or returns error message 114* Connects or returns error message
115\*-------------------------------------------------------------------------*/ 115\*-------------------------------------------------------------------------*/
116int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) { 116int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tm tm) {
117 int err; 117 int err;
118 /* don't call on closed socket */ 118 /* don't call on closed socket */
119 if (*ps == SOCK_INVALID) return IO_CLOSED; 119 if (*ps == SOCKET_INVALID) return IO_CLOSED;
120 /* ask system to connect */ 120 /* ask system to connect */
121 if (connect(*ps, addr, len) == 0) return IO_DONE; 121 if (connect(*ps, addr, len) == 0) return IO_DONE;
122 /* make sure the system is trying to connect */ 122 /* 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) {
125 /* zero timeout case optimization */ 125 /* zero timeout case optimization */
126 if (tm_iszero(tm)) return IO_TIMEOUT; 126 if (tm_iszero(tm)) return IO_TIMEOUT;
127 /* we wait until something happens */ 127 /* we wait until something happens */
128 err = sock_waitfd(ps, WAITFD_C, tm); 128 err = socket_waitfd(ps, WAITFD_C, tm);
129 if (err == IO_CLOSED) { 129 if (err == IO_CLOSED) {
130 int len = sizeof(err); 130 int len = sizeof(err);
131 /* give windows time to set the error (yes, disgusting) */ 131 /* give windows time to set the error (yes, disgusting) */
132 Sleep(10); 132 Sleep(10);
133 /* find out why we failed */ 133 /* find out why we failed */
134 getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); 134 getsockopt(*ps, SOL_SOCKETET, SO_ERROR, (char *)&err, &len);
135 /* we KNOW there was an error. if 'why' is 0, we will return 135 /* we KNOW there was an error. if 'why' is 0, we will return
136 * "unknown error", but it's not really our fault */ 136 * "unknown error", but it's not really our fault */
137 return err > 0? err: IO_UNKNOWN; 137 return err > 0? err: IO_UNKNOWN;
@@ -142,44 +142,44 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
142/*-------------------------------------------------------------------------*\ 142/*-------------------------------------------------------------------------*\
143* Binds or returns error message 143* Binds or returns error message
144\*-------------------------------------------------------------------------*/ 144\*-------------------------------------------------------------------------*/
145int sock_bind(p_sock ps, SA *addr, socklen_t len) { 145int socket_bind(p_socket ps, SA *addr, socklen_t len) {
146 int err = IO_DONE; 146 int err = IO_DONE;
147 sock_setblocking(ps); 147 socket_setblocking(ps);
148 if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); 148 if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
149 sock_setnonblocking(ps); 149 socket_setnonblocking(ps);
150 return err; 150 return err;
151} 151}
152 152
153/*-------------------------------------------------------------------------*\ 153/*-------------------------------------------------------------------------*\
154* 154*
155\*-------------------------------------------------------------------------*/ 155\*-------------------------------------------------------------------------*/
156int sock_listen(p_sock ps, int backlog) { 156int socket_listen(p_socket ps, int backlog) {
157 int err = IO_DONE; 157 int err = IO_DONE;
158 sock_setblocking(ps); 158 socket_setblocking(ps);
159 if (listen(*ps, backlog) < 0) err = WSAGetLastError(); 159 if (listen(*ps, backlog) < 0) err = WSAGetLastError();
160 sock_setnonblocking(ps); 160 socket_setnonblocking(ps);
161 return err; 161 return err;
162} 162}
163 163
164/*-------------------------------------------------------------------------*\ 164/*-------------------------------------------------------------------------*\
165* Accept with timeout 165* Accept with timeout
166\*-------------------------------------------------------------------------*/ 166\*-------------------------------------------------------------------------*/
167int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) { 167int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_tm tm) {
168 SA daddr; 168 SA daddr;
169 socklen_t dlen = sizeof(daddr); 169 socklen_t dlen = sizeof(daddr);
170 if (*ps == SOCK_INVALID) return IO_CLOSED; 170 if (*ps == SOCKET_INVALID) return IO_CLOSED;
171 if (!addr) addr = &daddr; 171 if (!addr) addr = &daddr;
172 if (!len) len = &dlen; 172 if (!len) len = &dlen;
173 for ( ;; ) { 173 for ( ;; ) {
174 int err; 174 int err;
175 /* try to get client socket */ 175 /* try to get client socket */
176 if ((*pa = accept(*ps, addr, len)) != SOCK_INVALID) return IO_DONE; 176 if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE;
177 /* find out why we failed */ 177 /* find out why we failed */
178 err = WSAGetLastError(); 178 err = WSAGetLastError();
179 /* if we failed because there was no connectoin, keep trying */ 179 /* if we failed because there was no connectoin, keep trying */
180 if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err; 180 if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err;
181 /* call select to avoid busy wait */ 181 /* call select to avoid busy wait */
182 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 182 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
183 } 183 }
184 /* can't reach here */ 184 /* can't reach here */
185 return IO_UNKNOWN; 185 return IO_UNKNOWN;
@@ -191,11 +191,12 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *len, p_tm tm) {
191* this can take an awful lot of time and we will end up blocked. 191* this can take an awful lot of time and we will end up blocked.
192* Therefore, whoever calls this function should not pass a huge buffer. 192* Therefore, whoever calls this function should not pass a huge buffer.
193\*-------------------------------------------------------------------------*/ 193\*-------------------------------------------------------------------------*/
194int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) 194int socket_send(p_socket ps, const char *data, size_t count,
195 size_t *sent, p_tm tm)
195{ 196{
196 int err; 197 int err;
197 /* avoid making system calls on closed sockets */ 198 /* avoid making system calls on closed sockets */
198 if (*ps == SOCK_INVALID) return IO_CLOSED; 199 if (*ps == SOCKET_INVALID) return IO_CLOSED;
199 /* loop until we send something or we give up on error */ 200 /* loop until we send something or we give up on error */
200 *sent = 0; 201 *sent = 0;
201 for ( ;; ) { 202 for ( ;; ) {
@@ -211,7 +212,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm)
211 /* we can only proceed if there was no serious error */ 212 /* we can only proceed if there was no serious error */
212 if (err != WSAEWOULDBLOCK) return err; 213 if (err != WSAEWOULDBLOCK) return err;
213 /* avoid busy wait */ 214 /* avoid busy wait */
214 if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; 215 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
215 } 216 }
216 /* can't reach here */ 217 /* can't reach here */
217 return IO_UNKNOWN; 218 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)
220/*-------------------------------------------------------------------------*\ 221/*-------------------------------------------------------------------------*\
221* Sendto with timeout 222* Sendto with timeout
222\*-------------------------------------------------------------------------*/ 223\*-------------------------------------------------------------------------*/
223int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent, 224int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
224 SA *addr, socklen_t len, p_tm tm) 225 SA *addr, socklen_t len, p_tm tm)
225{ 226{
226 int err; 227 int err;
227 if (*ps == SOCK_INVALID) return IO_CLOSED; 228 if (*ps == SOCKET_INVALID) return IO_CLOSED;
228 *sent = 0; 229 *sent = 0;
229 for ( ;; ) { 230 for ( ;; ) {
230 int put = sendto(*ps, data, (int) count, 0, addr, len); 231 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,
234 } 235 }
235 err = WSAGetLastError(); 236 err = WSAGetLastError();
236 if (err != WSAEWOULDBLOCK) return err; 237 if (err != WSAEWOULDBLOCK) return err;
237 if ((err = sock_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; 238 if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
238 } 239 }
239 return IO_UNKNOWN; 240 return IO_UNKNOWN;
240} 241}
@@ -242,9 +243,9 @@ int sock_sendto(p_sock ps, const char *data, size_t count, size_t *sent,
242/*-------------------------------------------------------------------------*\ 243/*-------------------------------------------------------------------------*\
243* Receive with timeout 244* Receive with timeout
244\*-------------------------------------------------------------------------*/ 245\*-------------------------------------------------------------------------*/
245int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) { 246int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_tm tm) {
246 int err; 247 int err;
247 if (*ps == SOCK_INVALID) return IO_CLOSED; 248 if (*ps == SOCKET_INVALID) return IO_CLOSED;
248 *got = 0; 249 *got = 0;
249 for ( ;; ) { 250 for ( ;; ) {
250 int taken = recv(*ps, data, (int) count, 0); 251 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) {
255 if (taken == 0) return IO_CLOSED; 256 if (taken == 0) return IO_CLOSED;
256 err = WSAGetLastError(); 257 err = WSAGetLastError();
257 if (err != WSAEWOULDBLOCK) return err; 258 if (err != WSAEWOULDBLOCK) return err;
258 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 259 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
259 } 260 }
260 return IO_UNKNOWN; 261 return IO_UNKNOWN;
261} 262}
@@ -263,10 +264,10 @@ int sock_recv(p_sock ps, char *data, size_t count, size_t *got, p_tm tm) {
263/*-------------------------------------------------------------------------*\ 264/*-------------------------------------------------------------------------*\
264* Recvfrom with timeout 265* Recvfrom with timeout
265\*-------------------------------------------------------------------------*/ 266\*-------------------------------------------------------------------------*/
266int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got, 267int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
267 SA *addr, socklen_t *len, p_tm tm) { 268 SA *addr, socklen_t *len, p_tm tm) {
268 int err; 269 int err;
269 if (*ps == SOCK_INVALID) return IO_CLOSED; 270 if (*ps == SOCKET_INVALID) return IO_CLOSED;
270 *got = 0; 271 *got = 0;
271 for ( ;; ) { 272 for ( ;; ) {
272 int taken = recvfrom(*ps, data, (int) count, 0, addr, len); 273 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,
277 if (taken == 0) return IO_CLOSED; 278 if (taken == 0) return IO_CLOSED;
278 err = WSAGetLastError(); 279 err = WSAGetLastError();
279 if (err != WSAEWOULDBLOCK) return err; 280 if (err != WSAEWOULDBLOCK) return err;
280 if ((err = sock_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; 281 if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err;
281 } 282 }
282 return IO_UNKNOWN; 283 return IO_UNKNOWN;
283} 284}
@@ -285,7 +286,7 @@ int sock_recvfrom(p_sock ps, char *data, size_t count, size_t *got,
285/*-------------------------------------------------------------------------*\ 286/*-------------------------------------------------------------------------*\
286* Put socket into blocking mode 287* Put socket into blocking mode
287\*-------------------------------------------------------------------------*/ 288\*-------------------------------------------------------------------------*/
288void sock_setblocking(p_sock ps) { 289void socket_setblocking(p_socket ps) {
289 u_long argp = 0; 290 u_long argp = 0;
290 ioctlsocket(*ps, FIONBIO, &argp); 291 ioctlsocket(*ps, FIONBIO, &argp);
291} 292}
@@ -293,7 +294,7 @@ void sock_setblocking(p_sock ps) {
293/*-------------------------------------------------------------------------*\ 294/*-------------------------------------------------------------------------*\
294* Put socket into non-blocking mode 295* Put socket into non-blocking mode
295\*-------------------------------------------------------------------------*/ 296\*-------------------------------------------------------------------------*/
296void sock_setnonblocking(p_sock ps) { 297void socket_setnonblocking(p_socket ps) {
297 u_long argp = 1; 298 u_long argp = 1;
298 ioctlsocket(*ps, FIONBIO, &argp); 299 ioctlsocket(*ps, FIONBIO, &argp);
299} 300}
@@ -301,13 +302,13 @@ void sock_setnonblocking(p_sock ps) {
301/*-------------------------------------------------------------------------*\ 302/*-------------------------------------------------------------------------*\
302* DNS helpers 303* DNS helpers
303\*-------------------------------------------------------------------------*/ 304\*-------------------------------------------------------------------------*/
304int sock_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 305int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
305 *hp = gethostbyaddr(addr, len, AF_INET); 306 *hp = gethostbyaddr(addr, len, AF_INET);
306 if (*hp) return IO_DONE; 307 if (*hp) return IO_DONE;
307 else return WSAGetLastError(); 308 else return WSAGetLastError();
308} 309}
309 310
310int sock_gethostbyname(const char *addr, struct hostent **hp) { 311int socket_gethostbyname(const char *addr, struct hostent **hp) {
311 *hp = gethostbyname(addr); 312 *hp = gethostbyname(addr);
312 if (*hp) return IO_DONE; 313 if (*hp) return IO_DONE;
313 else return WSAGetLastError(); 314 else return WSAGetLastError();
@@ -316,7 +317,7 @@ int sock_gethostbyname(const char *addr, struct hostent **hp) {
316/*-------------------------------------------------------------------------*\ 317/*-------------------------------------------------------------------------*\
317* Error translation functions 318* Error translation functions
318\*-------------------------------------------------------------------------*/ 319\*-------------------------------------------------------------------------*/
319const char *sock_hoststrerror(int err) { 320const char *socket_hoststrerror(int err) {
320 if (err <= 0) return io_strerror(err); 321 if (err <= 0) return io_strerror(err);
321 switch (err) { 322 switch (err) {
322 case WSAHOST_NOT_FOUND: return "host not found"; 323 case WSAHOST_NOT_FOUND: return "host not found";
@@ -324,7 +325,7 @@ const char *sock_hoststrerror(int err) {
324 } 325 }
325} 326}
326 327
327const char *sock_strerror(int err) { 328const char *socket_strerror(int err) {
328 if (err <= 0) return io_strerror(err); 329 if (err <= 0) return io_strerror(err);
329 switch (err) { 330 switch (err) {
330 case WSAEADDRINUSE: return "address already in use"; 331 case WSAEADDRINUSE: return "address already in use";
@@ -338,9 +339,9 @@ const char *sock_strerror(int err) {
338 } 339 }
339} 340}
340 341
341const char *sock_ioerror(p_sock ps, int err) { 342const char *socket_ioerror(p_socket ps, int err) {
342 (void) ps; 343 (void) ps;
343 return sock_strerror(err); 344 return socket_strerror(err);
344} 345}
345 346
346static const char *wstrerror(int err) { 347static 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 @@
13#include <winsock.h> 13#include <winsock.h>
14 14
15typedef int socklen_t; 15typedef int socklen_t;
16typedef SOCKET t_sock; 16typedef SOCKET t_socket;
17typedef t_sock *p_sock; 17typedef t_socket *p_socket;
18 18
19#define SOCK_INVALID (INVALID_SOCKET) 19#define SOCKET_INVALID (INVALID_SOCKET)
20 20
21#endif /* WSOCKET_H */ 21#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)
55print("ok") 55print("ok")
56 56
57io.write("erasing before upload: ") 57io.write("erasing before upload: ")
58ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") 58ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
59if not ret then print(err) 59if not ret then print(err)
60else print("ok") end 60else print("ok") end
61 61
62io.write("testing upload: ") 62io.write("testing upload: ")
63ret, err = ftp.put("ftp://luasocket:password@" .. host .. "/index.up.html;type=i", index) 63ret, err = ftp.put("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i", index)
64assert(ret and not err, err) 64assert(ret and not err, err)
65print("ok") 65print("ok")
66 66
67io.write("downloading uploaded file: ") 67io.write("downloading uploaded file: ")
68back, err = ftp.get("ftp://luasocket:password@" .. host .. "/index.up.html;type=i") 68back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i")
69assert(ret and not err and index == back, err) 69assert(ret and not err and index == back, err)
70print("ok") 70print("ok")
71 71
72io.write("erasing after upload/download: ") 72io.write("erasing after upload/download: ")
73ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") 73ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
74assert(ret and not err, err) 74assert(ret and not err, err)
75print("ok") 75print("ok")
76 76
77io.write("testing weird-character translation: ") 77io.write("testing weird-character translation: ")
78back, err = ftp.get("ftp://luasocket:password@" .. host .. "/%23%3f;type=i") 78back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/%23%3f;type=i")
79assert(not err and back == index, err) 79assert(not err and back == index, err)
80print("ok") 80print("ok")
81 81
@@ -84,7 +84,7 @@ local back = {}
84ret, err = ftp.get{ 84ret, err = ftp.get{
85 url = "//stupid:mistake@" .. host .. "/index.html", 85 url = "//stupid:mistake@" .. host .. "/index.html",
86 user = "luasocket", 86 user = "luasocket",
87 password = "password", 87 password = "pedrovian",
88 type = "i", 88 type = "i",
89 sink = ltn12.sink.table(back) 89 sink = ltn12.sink.table(back)
90} 90}