aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2019-02-26 00:06:02 -0300
committerGitHub <noreply@github.com>2019-02-26 00:06:02 -0300
commitc0fba03e4fe2a27a3471fa5af290be931f957002 (patch)
tree7a5f40572dd9ad9a6b56749cc256f7b658946873 /src
parent9b3f7a430454dc9568dfb063fbc324b29c5134d7 (diff)
parente2e43d62fa925e7e22385505ed0c635255c77c0a (diff)
downloadluasocket-c0fba03e4fe2a27a3471fa5af290be931f957002.tar.gz
luasocket-c0fba03e4fe2a27a3471fa5af290be931f957002.tar.bz2
luasocket-c0fba03e4fe2a27a3471fa5af290be931f957002.zip
Merge pull request #270 from ewestbrook/functionvisibility
Tag functions explicitly for shared library visibility
Diffstat (limited to 'src')
-rw-r--r--src/auxiliar.c26
-rw-r--r--src/buffer.c16
-rw-r--r--src/compat.c5
-rw-r--r--src/except.c4
-rw-r--r--src/inet.c26
-rw-r--r--src/io.c5
-rw-r--r--src/luasocket.h14
-rw-r--r--src/makefile39
-rwxr-xr-xsrc/mime.c2
-rw-r--r--src/mime.h10
-rw-r--r--src/options.c94
-rw-r--r--src/select.c6
-rw-r--r--src/serial.c2
-rw-r--r--src/tcp.c9
-rw-r--r--src/timeout.c32
-rw-r--r--src/udp.c8
-rw-r--r--src/unix.c4
-rw-r--r--src/unix.h7
-rw-r--r--src/unixdgram.c9
-rw-r--r--src/unixstream.c6
-rw-r--r--src/usocket.c58
-rwxr-xr-xsrc/wsocket.c50
22 files changed, 224 insertions, 208 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c
index 5251549..0bd7927 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -2,18 +2,18 @@
2* Auxiliar routines for class hierarchy manipulation 2* Auxiliar routines for class hierarchy manipulation
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6#include "auxiliar.h"
5#include <string.h> 7#include <string.h>
6#include <stdio.h> 8#include <stdio.h>
7 9
8#include "auxiliar.h"
9
10/*=========================================================================*\ 10/*=========================================================================*\
11* Exported functions 11* Exported functions
12\*=========================================================================*/ 12\*=========================================================================*/
13/*-------------------------------------------------------------------------*\ 13/*-------------------------------------------------------------------------*\
14* Initializes the module 14* Initializes the module
15\*-------------------------------------------------------------------------*/ 15\*-------------------------------------------------------------------------*/
16int auxiliar_open(lua_State *L) { 16LUASOCKET_PRIVATE int auxiliar_open(lua_State *L) {
17 (void) L; 17 (void) L;
18 return 0; 18 return 0;
19} 19}
@@ -22,7 +22,7 @@ int auxiliar_open(lua_State *L) {
22* Creates a new class with given methods 22* Creates a new class with given methods
23* Methods whose names start with __ are passed directly to the metatable. 23* Methods whose names start with __ are passed directly to the metatable.
24\*-------------------------------------------------------------------------*/ 24\*-------------------------------------------------------------------------*/
25void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) { 25LUASOCKET_PRIVATE void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
26 luaL_newmetatable(L, classname); /* mt */ 26 luaL_newmetatable(L, classname); /* mt */
27 /* create __index table to place methods */ 27 /* create __index table to place methods */
28 lua_pushstring(L, "__index"); /* mt,"__index" */ 28 lua_pushstring(L, "__index"); /* mt,"__index" */
@@ -45,7 +45,7 @@ void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
45/*-------------------------------------------------------------------------*\ 45/*-------------------------------------------------------------------------*\
46* Prints the value of a class in a nice way 46* Prints the value of a class in a nice way
47\*-------------------------------------------------------------------------*/ 47\*-------------------------------------------------------------------------*/
48int auxiliar_tostring(lua_State *L) { 48LUASOCKET_PRIVATE int auxiliar_tostring(lua_State *L) {
49 char buf[32]; 49 char buf[32];
50 if (!lua_getmetatable(L, 1)) goto error; 50 if (!lua_getmetatable(L, 1)) goto error;
51 lua_pushstring(L, "__index"); 51 lua_pushstring(L, "__index");
@@ -66,7 +66,7 @@ error:
66/*-------------------------------------------------------------------------*\ 66/*-------------------------------------------------------------------------*\
67* Insert class into group 67* Insert class into group
68\*-------------------------------------------------------------------------*/ 68\*-------------------------------------------------------------------------*/
69void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) { 69LUASOCKET_PRIVATE void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
70 luaL_getmetatable(L, classname); 70 luaL_getmetatable(L, classname);
71 lua_pushstring(L, groupname); 71 lua_pushstring(L, groupname);
72 lua_pushboolean(L, 1); 72 lua_pushboolean(L, 1);
@@ -77,7 +77,7 @@ void auxiliar_add2group(lua_State *L, const char *classname, const char *groupna
77/*-------------------------------------------------------------------------*\ 77/*-------------------------------------------------------------------------*\
78* Make sure argument is a boolean 78* Make sure argument is a boolean
79\*-------------------------------------------------------------------------*/ 79\*-------------------------------------------------------------------------*/
80int auxiliar_checkboolean(lua_State *L, int objidx) { 80LUASOCKET_PRIVATE int auxiliar_checkboolean(lua_State *L, int objidx) {
81 if (!lua_isboolean(L, objidx)) 81 if (!lua_isboolean(L, objidx))
82 auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); 82 auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
83 return lua_toboolean(L, objidx); 83 return lua_toboolean(L, objidx);
@@ -87,7 +87,7 @@ int auxiliar_checkboolean(lua_State *L, int objidx) {
87* Return userdata pointer if object belongs to a given class, abort with 87* Return userdata pointer if object belongs to a given class, abort with
88* error otherwise 88* error otherwise
89\*-------------------------------------------------------------------------*/ 89\*-------------------------------------------------------------------------*/
90void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { 90LUASOCKET_PRIVATE void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
91 void *data = auxiliar_getclassudata(L, classname, objidx); 91 void *data = auxiliar_getclassudata(L, classname, objidx);
92 if (!data) { 92 if (!data) {
93 char msg[45]; 93 char msg[45];
@@ -101,7 +101,7 @@ void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
101* Return userdata pointer if object belongs to a given group, abort with 101* Return userdata pointer if object belongs to a given group, abort with
102* error otherwise 102* error otherwise
103\*-------------------------------------------------------------------------*/ 103\*-------------------------------------------------------------------------*/
104void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { 104LUASOCKET_PRIVATE void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
105 void *data = auxiliar_getgroupudata(L, groupname, objidx); 105 void *data = auxiliar_getgroupudata(L, groupname, objidx);
106 if (!data) { 106 if (!data) {
107 char msg[45]; 107 char msg[45];
@@ -114,7 +114,7 @@ void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
114/*-------------------------------------------------------------------------*\ 114/*-------------------------------------------------------------------------*\
115* Set object class 115* Set object class
116\*-------------------------------------------------------------------------*/ 116\*-------------------------------------------------------------------------*/
117void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { 117LUASOCKET_PRIVATE void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
118 luaL_getmetatable(L, classname); 118 luaL_getmetatable(L, classname);
119 if (objidx < 0) objidx--; 119 if (objidx < 0) objidx--;
120 lua_setmetatable(L, objidx); 120 lua_setmetatable(L, objidx);
@@ -124,7 +124,7 @@ void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
124* Get a userdata pointer if object belongs to a given group. Return NULL 124* Get a userdata pointer if object belongs to a given group. Return NULL
125* otherwise 125* otherwise
126\*-------------------------------------------------------------------------*/ 126\*-------------------------------------------------------------------------*/
127void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { 127LUASOCKET_PRIVATE void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
128 if (!lua_getmetatable(L, objidx)) 128 if (!lua_getmetatable(L, objidx))
129 return NULL; 129 return NULL;
130 lua_pushstring(L, groupname); 130 lua_pushstring(L, groupname);
@@ -142,7 +142,7 @@ void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
142* Get a userdata pointer if object belongs to a given class. Return NULL 142* Get a userdata pointer if object belongs to a given class. Return NULL
143* otherwise 143* otherwise
144\*-------------------------------------------------------------------------*/ 144\*-------------------------------------------------------------------------*/
145void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { 145LUASOCKET_PRIVATE void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
146 return luaL_testudata(L, objidx, classname); 146 return luaL_testudata(L, objidx, classname);
147} 147}
148 148
@@ -150,7 +150,7 @@ void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
150* Throws error when argument does not have correct type. 150* Throws error when argument does not have correct type.
151* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2. 151* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
152\*-------------------------------------------------------------------------*/ 152\*-------------------------------------------------------------------------*/
153int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { 153LUASOCKET_PRIVATE int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
154 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, 154 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
155 luaL_typename(L, narg)); 155 luaL_typename(L, narg));
156 return luaL_argerror(L, narg, msg); 156 return luaL_argerror(L, narg, msg);
diff --git a/src/buffer.c b/src/buffer.c
index fff1634..357730a 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2,6 +2,8 @@
2* Input/Output interface for Lua programs 2* Input/Output interface for Lua programs
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6
5#include "lua.h" 7#include "lua.h"
6#include "lauxlib.h" 8#include "lauxlib.h"
7#include "compat.h" 9#include "compat.h"
@@ -32,7 +34,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
32/*-------------------------------------------------------------------------*\ 34/*-------------------------------------------------------------------------*\
33* Initializes module 35* Initializes module
34\*-------------------------------------------------------------------------*/ 36\*-------------------------------------------------------------------------*/
35int buffer_open(lua_State *L) { 37LUASOCKET_PRIVATE int buffer_open(lua_State *L) {
36 (void) L; 38 (void) L;
37 return 0; 39 return 0;
38} 40}
@@ -40,7 +42,7 @@ int buffer_open(lua_State *L) {
40/*-------------------------------------------------------------------------*\ 42/*-------------------------------------------------------------------------*\
41* Initializes C structure 43* Initializes C structure
42\*-------------------------------------------------------------------------*/ 44\*-------------------------------------------------------------------------*/
43void buffer_init(p_buffer buf, p_io io, p_timeout tm) { 45LUASOCKET_PRIVATE void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
44 buf->first = buf->last = 0; 46 buf->first = buf->last = 0;
45 buf->io = io; 47 buf->io = io;
46 buf->tm = tm; 48 buf->tm = tm;
@@ -51,7 +53,7 @@ void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
51/*-------------------------------------------------------------------------*\ 53/*-------------------------------------------------------------------------*\
52* object:getstats() interface 54* object:getstats() interface
53\*-------------------------------------------------------------------------*/ 55\*-------------------------------------------------------------------------*/
54int buffer_meth_getstats(lua_State *L, p_buffer buf) { 56LUASOCKET_PRIVATE int buffer_meth_getstats(lua_State *L, p_buffer buf) {
55 lua_pushnumber(L, (lua_Number) buf->received); 57 lua_pushnumber(L, (lua_Number) buf->received);
56 lua_pushnumber(L, (lua_Number) buf->sent); 58 lua_pushnumber(L, (lua_Number) buf->sent);
57 lua_pushnumber(L, timeout_gettime() - buf->birthday); 59 lua_pushnumber(L, timeout_gettime() - buf->birthday);
@@ -61,7 +63,7 @@ int buffer_meth_getstats(lua_State *L, p_buffer buf) {
61/*-------------------------------------------------------------------------*\ 63/*-------------------------------------------------------------------------*\
62* object:setstats() interface 64* object:setstats() interface
63\*-------------------------------------------------------------------------*/ 65\*-------------------------------------------------------------------------*/
64int buffer_meth_setstats(lua_State *L, p_buffer buf) { 66LUASOCKET_PRIVATE int buffer_meth_setstats(lua_State *L, p_buffer buf) {
65 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); 67 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
66 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); 68 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
67 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); 69 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
@@ -72,7 +74,7 @@ int buffer_meth_setstats(lua_State *L, p_buffer buf) {
72/*-------------------------------------------------------------------------*\ 74/*-------------------------------------------------------------------------*\
73* object:send() interface 75* object:send() interface
74\*-------------------------------------------------------------------------*/ 76\*-------------------------------------------------------------------------*/
75int buffer_meth_send(lua_State *L, p_buffer buf) { 77LUASOCKET_PRIVATE int buffer_meth_send(lua_State *L, p_buffer buf) {
76 int top = lua_gettop(L); 78 int top = lua_gettop(L);
77 int err = IO_DONE; 79 int err = IO_DONE;
78 size_t size = 0, sent = 0; 80 size_t size = 0, sent = 0;
@@ -105,7 +107,7 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
105/*-------------------------------------------------------------------------*\ 107/*-------------------------------------------------------------------------*\
106* object:receive() interface 108* object:receive() interface
107\*-------------------------------------------------------------------------*/ 109\*-------------------------------------------------------------------------*/
108int buffer_meth_receive(lua_State *L, p_buffer buf) { 110LUASOCKET_PRIVATE int buffer_meth_receive(lua_State *L, p_buffer buf) {
109 int err = IO_DONE, top = lua_gettop(L); 111 int err = IO_DONE, top = lua_gettop(L);
110 luaL_Buffer b; 112 luaL_Buffer b;
111 size_t size; 113 size_t size;
@@ -154,7 +156,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
154/*-------------------------------------------------------------------------*\ 156/*-------------------------------------------------------------------------*\
155* Determines if there is any data in the read buffer 157* Determines if there is any data in the read buffer
156\*-------------------------------------------------------------------------*/ 158\*-------------------------------------------------------------------------*/
157int buffer_isempty(p_buffer buf) { 159LUASOCKET_PRIVATE int buffer_isempty(p_buffer buf) {
158 return buf->first >= buf->last; 160 return buf->first >= buf->last;
159} 161}
160 162
diff --git a/src/compat.c b/src/compat.c
index 988fa68..1290f70 100644
--- a/src/compat.c
+++ b/src/compat.c
@@ -1,10 +1,11 @@
1#include "luasocket.h"
1#include "compat.h" 2#include "compat.h"
2 3
3#if LUA_VERSION_NUM==501 4#if LUA_VERSION_NUM==501
4/* 5/*
5** Adapted from Lua 5.2 6** Adapted from Lua 5.2
6*/ 7*/
7void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { 8LUASOCKET_PRIVATE void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
8 luaL_checkstack(L, nup+1, "too many upvalues"); 9 luaL_checkstack(L, nup+1, "too many upvalues");
9 for (; l->name != NULL; l++) { /* fill the table with given functions */ 10 for (; l->name != NULL; l++) { /* fill the table with given functions */
10 int i; 11 int i;
@@ -20,7 +21,7 @@ void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
20/* 21/*
21** Duplicated from Lua 5.2 22** Duplicated from Lua 5.2
22*/ 23*/
23void *luaL_testudata (lua_State *L, int ud, const char *tname) { 24LUASOCKET_PRIVATE void *luaL_testudata (lua_State *L, int ud, const char *tname) {
24 void *p = lua_touserdata(L, ud); 25 void *p = lua_touserdata(L, ud);
25 if (p != NULL) { /* value is a userdata? */ 26 if (p != NULL) { /* value is a userdata? */
26 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ 27 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
diff --git a/src/except.c b/src/except.c
index 60b5005..dab70e7 100644
--- a/src/except.c
+++ b/src/except.c
@@ -2,6 +2,8 @@
2* Simple exception support 2* Simple exception support
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6
5#include <stdio.h> 7#include <stdio.h>
6 8
7#include "lua.h" 9#include "lua.h"
@@ -124,7 +126,7 @@ static int global_protect(lua_State *L) {
124/*-------------------------------------------------------------------------*\ 126/*-------------------------------------------------------------------------*\
125* Init module 127* Init module
126\*-------------------------------------------------------------------------*/ 128\*-------------------------------------------------------------------------*/
127int except_open(lua_State *L) { 129LUASOCKET_PRIVATE int except_open(lua_State *L) {
128 lua_newtable(L); /* metatable for wrapped exceptions */ 130 lua_newtable(L); /* metatable for wrapped exceptions */
129 lua_pushboolean(L, 0); 131 lua_pushboolean(L, 0);
130 lua_setfield(L, -2, "__metatable"); 132 lua_setfield(L, -2, "__metatable");
diff --git a/src/inet.c b/src/inet.c
index f4c8404..bed8a7c 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -2,6 +2,8 @@
2* Internet domain functions 2* Internet domain functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6
5#include <stdio.h> 7#include <stdio.h>
6#include <stdlib.h> 8#include <stdlib.h>
7#include <string.h> 9#include <string.h>
@@ -38,7 +40,7 @@ static luaL_Reg func[] = {
38/*-------------------------------------------------------------------------*\ 40/*-------------------------------------------------------------------------*\
39* Initializes module 41* Initializes module
40\*-------------------------------------------------------------------------*/ 42\*-------------------------------------------------------------------------*/
41int inet_open(lua_State *L) 43LUASOCKET_PRIVATE int inet_open(lua_State *L)
42{ 44{
43 lua_pushstring(L, "dns"); 45 lua_pushstring(L, "dns");
44 lua_newtable(L); 46 lua_newtable(L);
@@ -143,7 +145,7 @@ static int inet_global_toip(lua_State *L)
143 return 2; 145 return 2;
144} 146}
145 147
146int inet_optfamily(lua_State* L, int narg, const char* def) 148LUASOCKET_PRIVATE int inet_optfamily(lua_State* L, int narg, const char* def)
147{ 149{
148 static const char* optname[] = { "unspec", "inet", "inet6", NULL }; 150 static const char* optname[] = { "unspec", "inet", "inet6", NULL };
149 static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 }; 151 static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
@@ -151,7 +153,7 @@ int inet_optfamily(lua_State* L, int narg, const char* def)
151 return optvalue[luaL_checkoption(L, narg, def, optname)]; 153 return optvalue[luaL_checkoption(L, narg, def, optname)];
152} 154}
153 155
154int inet_optsocktype(lua_State* L, int narg, const char* def) 156LUASOCKET_PRIVATE int inet_optsocktype(lua_State* L, int narg, const char* def)
155{ 157{
156 static const char* optname[] = { "stream", "dgram", NULL }; 158 static const char* optname[] = { "stream", "dgram", NULL };
157 static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 }; 159 static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 };
@@ -242,7 +244,7 @@ static int inet_global_gethostname(lua_State *L)
242/*-------------------------------------------------------------------------*\ 244/*-------------------------------------------------------------------------*\
243* Retrieves socket peer name 245* Retrieves socket peer name
244\*-------------------------------------------------------------------------*/ 246\*-------------------------------------------------------------------------*/
245int inet_meth_getpeername(lua_State *L, p_socket ps, int family) 247LUASOCKET_PRIVATE int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
246{ 248{
247 int err; 249 int err;
248 struct sockaddr_storage peer; 250 struct sockaddr_storage peer;
@@ -276,7 +278,7 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
276/*-------------------------------------------------------------------------*\ 278/*-------------------------------------------------------------------------*\
277* Retrieves socket local name 279* Retrieves socket local name
278\*-------------------------------------------------------------------------*/ 280\*-------------------------------------------------------------------------*/
279int inet_meth_getsockname(lua_State *L, p_socket ps, int family) 281LUASOCKET_PRIVATE int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
280{ 282{
281 int err; 283 int err;
282 struct sockaddr_storage peer; 284 struct sockaddr_storage peer;
@@ -352,7 +354,7 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
352/*-------------------------------------------------------------------------*\ 354/*-------------------------------------------------------------------------*\
353* Tries to create a new inet socket 355* Tries to create a new inet socket
354\*-------------------------------------------------------------------------*/ 356\*-------------------------------------------------------------------------*/
355const char *inet_trycreate(p_socket ps, int family, int type, int protocol) { 357LUASOCKET_PRIVATE const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
356 const char *err = socket_strerror(socket_create(ps, family, type, protocol)); 358 const char *err = socket_strerror(socket_create(ps, family, type, protocol));
357 if (err == NULL && family == AF_INET6) { 359 if (err == NULL && family == AF_INET6) {
358 int yes = 1; 360 int yes = 1;
@@ -364,7 +366,7 @@ const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
364/*-------------------------------------------------------------------------*\ 366/*-------------------------------------------------------------------------*\
365* "Disconnects" a DGRAM socket 367* "Disconnects" a DGRAM socket
366\*-------------------------------------------------------------------------*/ 368\*-------------------------------------------------------------------------*/
367const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) 369LUASOCKET_PRIVATE const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
368{ 370{
369 switch (family) { 371 switch (family) {
370 case AF_INET: { 372 case AF_INET: {
@@ -391,7 +393,7 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
391/*-------------------------------------------------------------------------*\ 393/*-------------------------------------------------------------------------*\
392* Tries to connect to remote address (address, port) 394* Tries to connect to remote address (address, port)
393\*-------------------------------------------------------------------------*/ 395\*-------------------------------------------------------------------------*/
394const char *inet_tryconnect(p_socket ps, int *family, const char *address, 396LUASOCKET_PRIVATE const char *inet_tryconnect(p_socket ps, int *family, const char *address,
395 const char *serv, p_timeout tm, struct addrinfo *connecthints) 397 const char *serv, p_timeout tm, struct addrinfo *connecthints)
396{ 398{
397 struct addrinfo *iterator = NULL, *resolved = NULL; 399 struct addrinfo *iterator = NULL, *resolved = NULL;
@@ -437,7 +439,7 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
437/*-------------------------------------------------------------------------*\ 439/*-------------------------------------------------------------------------*\
438* Tries to accept a socket 440* Tries to accept a socket
439\*-------------------------------------------------------------------------*/ 441\*-------------------------------------------------------------------------*/
440const char *inet_tryaccept(p_socket server, int family, p_socket client, 442LUASOCKET_PRIVATE const char *inet_tryaccept(p_socket server, int family, p_socket client,
441 p_timeout tm) { 443 p_timeout tm) {
442 socklen_t len; 444 socklen_t len;
443 t_sockaddr_storage addr; 445 t_sockaddr_storage addr;
@@ -453,7 +455,7 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
453/*-------------------------------------------------------------------------*\ 455/*-------------------------------------------------------------------------*\
454* Tries to bind socket to (address, port) 456* Tries to bind socket to (address, port)
455\*-------------------------------------------------------------------------*/ 457\*-------------------------------------------------------------------------*/
456const char *inet_trybind(p_socket ps, int *family, const char *address, 458LUASOCKET_PRIVATE const char *inet_trybind(p_socket ps, int *family, const char *address,
457 const char *serv, struct addrinfo *bindhints) { 459 const char *serv, struct addrinfo *bindhints) {
458 struct addrinfo *iterator = NULL, *resolved = NULL; 460 struct addrinfo *iterator = NULL, *resolved = NULL;
459 const char *err = NULL; 461 const char *err = NULL;
@@ -497,7 +499,7 @@ const char *inet_trybind(p_socket ps, int *family, const char *address,
497* Some systems do not provide these so that we provide our own. 499* Some systems do not provide these so that we provide our own.
498\*-------------------------------------------------------------------------*/ 500\*-------------------------------------------------------------------------*/
499#ifdef LUASOCKET_INET_ATON 501#ifdef LUASOCKET_INET_ATON
500int inet_aton(const char *cp, struct in_addr *inp) 502LUASOCKET_PRIVATE int inet_aton(const char *cp, struct in_addr *inp)
501{ 503{
502 unsigned int a = 0, b = 0, c = 0, d = 0; 504 unsigned int a = 0, b = 0, c = 0, d = 0;
503 int n = 0, r; 505 int n = 0, r;
@@ -519,7 +521,7 @@ int inet_aton(const char *cp, struct in_addr *inp)
519#endif 521#endif
520 522
521#ifdef LUASOCKET_INET_PTON 523#ifdef LUASOCKET_INET_PTON
522int inet_pton(int af, const char *src, void *dst) 524LUASOCKET_PRIVATE int inet_pton(int af, const char *src, void *dst)
523{ 525{
524 struct addrinfo hints, *res; 526 struct addrinfo hints, *res;
525 int ret = 1; 527 int ret = 1;
diff --git a/src/io.c b/src/io.c
index a4230ce..f1a2b9d 100644
--- a/src/io.c
+++ b/src/io.c
@@ -2,6 +2,7 @@
2* Input/Output abstraction 2* Input/Output abstraction
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
5#include "io.h" 6#include "io.h"
6 7
7/*=========================================================================*\ 8/*=========================================================================*\
@@ -10,7 +11,7 @@
10/*-------------------------------------------------------------------------*\ 11/*-------------------------------------------------------------------------*\
11* Initializes C structure 12* Initializes C structure
12\*-------------------------------------------------------------------------*/ 13\*-------------------------------------------------------------------------*/
13void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) { 14LUASOCKET_PRIVATE void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
14 io->send = send; 15 io->send = send;
15 io->recv = recv; 16 io->recv = recv;
16 io->error = error; 17 io->error = error;
@@ -20,7 +21,7 @@ void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
20/*-------------------------------------------------------------------------*\ 21/*-------------------------------------------------------------------------*\
21* I/O error strings 22* I/O error strings
22\*-------------------------------------------------------------------------*/ 23\*-------------------------------------------------------------------------*/
23const char *io_strerror(int err) { 24LUASOCKET_PRIVATE const char *io_strerror(int err) {
24 switch (err) { 25 switch (err) {
25 case IO_DONE: return NULL; 26 case IO_DONE: return NULL;
26 case IO_CLOSED: return "closed"; 27 case IO_CLOSED: return "closed";
diff --git a/src/luasocket.h b/src/luasocket.h
index f75d21f..0121a15 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -18,7 +18,19 @@
18* This macro prefixes all exported API functions 18* This macro prefixes all exported API functions
19\*-------------------------------------------------------------------------*/ 19\*-------------------------------------------------------------------------*/
20#ifndef LUASOCKET_API 20#ifndef LUASOCKET_API
21#define LUASOCKET_API extern 21#ifdef _WIN32
22#define LUASOCKET_API __declspec(dllexport)
23#else
24#define LUASOCKET_API __attribute__ ((visibility ("default")))
25#endif
26#endif
27
28#ifndef LUASOCKET_PRIVATE
29#ifdef _WIN32
30#define LUASOCKET_PRIVATE
31#else
32#define LUASOCKET_PRIVATE __attribute__ ((visibility ("hidden")))
33#endif
22#endif 34#endif
23 35
24/*-------------------------------------------------------------------------*\ 36/*-------------------------------------------------------------------------*\
diff --git a/src/makefile b/src/makefile
index cc1ec7e..74bf3d5 100644
--- a/src/makefile
+++ b/src/makefile
@@ -149,12 +149,8 @@ PLATS= macosx linux win32 mingw solaris
149SO_macosx=so 149SO_macosx=so
150O_macosx=o 150O_macosx=o
151CC_macosx=gcc 151CC_macosx=gcc
152DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN \ 152DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN
153 -DLUASOCKET_API='__attribute__((visibility("default")))' \ 153CFLAGS_macosx=$(LUAINC:%=-I%) $(DEF) -Wall -O2 -fno-common
154 -DUNIX_API='__attribute__((visibility("default")))' \
155 -DMIME_API='__attribute__((visibility("default")))'
156CFLAGS_macosx=$(LUAINC:%=-I%) $(DEF) -Wall -O2 -fno-common \
157 -fvisibility=hidden
158LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o 154LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o
159LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc 155LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
160SOCKET_macosx=usocket.o 156SOCKET_macosx=usocket.o
@@ -165,12 +161,9 @@ SOCKET_macosx=usocket.o
165SO_linux=so 161SO_linux=so
166O_linux=o 162O_linux=o
167CC_linux=gcc 163CC_linux=gcc
168DEF_linux=-DLUASOCKET_$(DEBUG) \ 164DEF_linux=-DLUASOCKET_$(DEBUG)
169 -DLUASOCKET_API='__attribute__((visibility("default")))' \
170 -DUNIX_API='__attribute__((visibility("default")))' \
171 -DMIME_API='__attribute__((visibility("default")))'
172CFLAGS_linux=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \ 165CFLAGS_linux=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \
173 -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden 166 -Wimplicit -O2 -ggdb3 -fpic
174LDFLAGS_linux=-O -shared -fpic -o 167LDFLAGS_linux=-O -shared -fpic -o
175LD_linux=gcc 168LD_linux=gcc
176SOCKET_linux=usocket.o 169SOCKET_linux=usocket.o
@@ -181,12 +174,9 @@ SOCKET_linux=usocket.o
181SO_freebsd=so 174SO_freebsd=so
182O_freebsd=o 175O_freebsd=o
183CC_freebsd=gcc 176CC_freebsd=gcc
184DEF_freebsd=-DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN \ 177DEF_freebsd=-DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN
185 -DLUASOCKET_API='__attribute__((visibility("default")))' \
186 -DUNIX_API='__attribute__((visibility("default")))' \
187 -DMIME_API='__attribute__((visibility("default")))'
188CFLAGS_freebsd=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \ 178CFLAGS_freebsd=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \
189 -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden 179 -Wimplicit -O2 -ggdb3 -fpic
190LDFLAGS_freebsd=-O -shared -fpic -o 180LDFLAGS_freebsd=-O -shared -fpic -o
191LD_freebsd=gcc 181LD_freebsd=gcc
192SOCKET_freebsd=usocket.o 182SOCKET_freebsd=usocket.o
@@ -197,12 +187,9 @@ SOCKET_freebsd=usocket.o
197SO_solaris=so 187SO_solaris=so
198O_solaris=o 188O_solaris=o
199CC_solaris=gcc 189CC_solaris=gcc
200DEF_solaris=-DLUASOCKET_$(DEBUG) \ 190DEF_solaris=-DLUASOCKET_$(DEBUG)
201 -DLUASOCKET_API='__attribute__((visibility("default")))' \
202 -DUNIX_API='__attribute__((visibility("default")))' \
203 -DMIME_API='__attribute__((visibility("default")))'
204CFLAGS_solaris=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \ 191CFLAGS_solaris=$(LUAINC:%=-I%) $(DEF) -Wall -Wshadow -Wextra \
205 -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden 192 -Wimplicit -O2 -ggdb3 -fpic
206LDFLAGS_solaris=-lnsl -lsocket -lresolv -O -shared -fpic -o 193LDFLAGS_solaris=-lnsl -lsocket -lresolv -O -shared -fpic -o
207LD_solaris=gcc 194LD_solaris=gcc
208SOCKET_solaris=usocket.o 195SOCKET_solaris=usocket.o
@@ -214,10 +201,8 @@ SO_mingw=dll
214O_mingw=o 201O_mingw=o
215CC_mingw=gcc 202CC_mingw=gcc
216DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) \ 203DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) \
217 -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \ 204 -DWINVER=0x0501
218 -DMIME_API='__declspec(dllexport)' 205CFLAGS_mingw=$(LUAINC:%=-I%) $(DEF) -Wall -O2 -fno-common
219CFLAGS_mingw=$(LUAINC:%=-I%) $(DEF) -Wall -O2 -fno-common \
220 -fvisibility=hidden
221LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lws2_32 -o 206LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lws2_32 -o
222LD_mingw=gcc 207LD_mingw=gcc
223SOCKET_mingw=wsocket.o 208SOCKET_mingw=wsocket.o
@@ -230,8 +215,8 @@ SO_win32=dll
230O_win32=obj 215O_win32=obj
231CC_win32=cl 216CC_win32=cl
232DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ 217DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \
233 //D "LUASOCKET_API=__declspec(dllexport)" //D "_CRT_SECURE_NO_WARNINGS" \ 218 //D "_CRT_SECURE_NO_WARNINGS" \
234 //D "_WINDLL" //D "MIME_API=__declspec(dllexport)" \ 219 //D "_WINDLL" \
235 //D "LUASOCKET_$(DEBUG)" 220 //D "LUASOCKET_$(DEBUG)"
236CFLAGS_win32=$(LUAINC:%=//I "%") $(DEF) //O2 //Ot //MD //W3 //nologo 221CFLAGS_win32=$(LUAINC:%=//I "%") $(DEF) //O2 //Ot //MD //W3 //nologo
237LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \ 222LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \
diff --git a/src/mime.c b/src/mime.c
index 338ecd4..6e359af 100755
--- a/src/mime.c
+++ b/src/mime.c
@@ -76,7 +76,7 @@ static UC b64unbase[256];
76/*-------------------------------------------------------------------------*\ 76/*-------------------------------------------------------------------------*\
77* Initializes module 77* Initializes module
78\*-------------------------------------------------------------------------*/ 78\*-------------------------------------------------------------------------*/
79MIME_API int luaopen_mime_core(lua_State *L) 79LUASOCKET_API int luaopen_mime_core(lua_State *L)
80{ 80{
81 lua_newtable(L); 81 lua_newtable(L);
82 luaL_setfuncs(L, func, 0); 82 luaL_setfuncs(L, func, 0);
diff --git a/src/mime.h b/src/mime.h
index 99968a5..e57fc9c 100644
--- a/src/mime.h
+++ b/src/mime.h
@@ -8,6 +8,7 @@
8* and formatting conforming to RFC 2045. It is used by mime.lua, which 8* and formatting conforming to RFC 2045. It is used by mime.lua, which
9* provide a higher level interface to this functionality. 9* provide a higher level interface to this functionality.
10\*=========================================================================*/ 10\*=========================================================================*/
11#include "luasocket.h"
11#include "lua.h" 12#include "lua.h"
12 13
13/*-------------------------------------------------------------------------*\ 14/*-------------------------------------------------------------------------*\
@@ -17,13 +18,6 @@
17#define MIME_COPYRIGHT "Copyright (C) 2004-2013 Diego Nehab" 18#define MIME_COPYRIGHT "Copyright (C) 2004-2013 Diego Nehab"
18#define MIME_AUTHORS "Diego Nehab" 19#define MIME_AUTHORS "Diego Nehab"
19 20
20/*-------------------------------------------------------------------------*\ 21LUASOCKET_API int luaopen_mime_core(lua_State *L);
21* This macro prefixes all exported API functions
22\*-------------------------------------------------------------------------*/
23#ifndef MIME_API
24#define MIME_API extern
25#endif
26
27MIME_API int luaopen_mime_core(lua_State *L);
28 22
29#endif /* MIME_H */ 23#endif /* MIME_H */
diff --git a/src/options.c b/src/options.c
index bbadec3..b0bacbf 100644
--- a/src/options.c
+++ b/src/options.c
@@ -2,14 +2,12 @@
2* Common option interface 2* Common option interface
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6
7#include "lauxlib.h" 6#include "lauxlib.h"
8
9#include "auxiliar.h" 7#include "auxiliar.h"
10#include "options.h" 8#include "options.h"
11#include "inet.h" 9#include "inet.h"
12 10#include <string.h>
13 11
14/*=========================================================================*\ 12/*=========================================================================*\
15* Internal functions prototypes 13* Internal functions prototypes
@@ -31,7 +29,7 @@ static int opt_get(lua_State *L, p_socket ps, int level, int name,
31/*-------------------------------------------------------------------------*\ 29/*-------------------------------------------------------------------------*\
32* Calls appropriate option handler 30* Calls appropriate option handler
33\*-------------------------------------------------------------------------*/ 31\*-------------------------------------------------------------------------*/
34int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) 32LUASOCKET_PRIVATE int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
35{ 33{
36 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 34 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
37 while (opt->name && strcmp(name, opt->name)) 35 while (opt->name && strcmp(name, opt->name))
@@ -44,7 +42,7 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
44 return opt->func(L, ps); 42 return opt->func(L, ps);
45} 43}
46 44
47int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) 45LUASOCKET_PRIVATE int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
48{ 46{
49 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 47 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
50 while (opt->name && strcmp(name, opt->name)) 48 while (opt->name && strcmp(name, opt->name))
@@ -58,165 +56,165 @@ int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
58} 56}
59 57
60/* enables reuse of local address */ 58/* enables reuse of local address */
61int opt_set_reuseaddr(lua_State *L, p_socket ps) 59LUASOCKET_PRIVATE int opt_set_reuseaddr(lua_State *L, p_socket ps)
62{ 60{
63 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 61 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
64} 62}
65 63
66int opt_get_reuseaddr(lua_State *L, p_socket ps) 64LUASOCKET_PRIVATE int opt_get_reuseaddr(lua_State *L, p_socket ps)
67{ 65{
68 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 66 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
69} 67}
70 68
71/* enables reuse of local port */ 69/* enables reuse of local port */
72int opt_set_reuseport(lua_State *L, p_socket ps) 70LUASOCKET_PRIVATE int opt_set_reuseport(lua_State *L, p_socket ps)
73{ 71{
74 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 72 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
75} 73}
76 74
77int opt_get_reuseport(lua_State *L, p_socket ps) 75LUASOCKET_PRIVATE int opt_get_reuseport(lua_State *L, p_socket ps)
78{ 76{
79 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 77 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
80} 78}
81 79
82/* disables the Naggle algorithm */ 80/* disables the Naggle algorithm */
83int opt_set_tcp_nodelay(lua_State *L, p_socket ps) 81LUASOCKET_PRIVATE int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
84{ 82{
85 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 83 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
86} 84}
87 85
88int opt_get_tcp_nodelay(lua_State *L, p_socket ps) 86LUASOCKET_PRIVATE int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
89{ 87{
90 return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 88 return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
91} 89}
92 90
93#ifdef TCP_KEEPIDLE 91#ifdef TCP_KEEPIDLE
94int opt_get_tcp_keepidle(lua_State *L, p_socket ps) 92LUASOCKET_PRIVATE int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
95{ 93{
96 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE); 94 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
97} 95}
98 96
99int opt_set_tcp_keepidle(lua_State *L, p_socket ps) 97LUASOCKET_PRIVATE int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
100{ 98{
101 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE); 99 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
102} 100}
103#endif 101#endif
104 102
105#ifdef TCP_KEEPCNT 103#ifdef TCP_KEEPCNT
106int opt_get_tcp_keepcnt(lua_State *L, p_socket ps) 104LUASOCKET_PRIVATE int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
107{ 105{
108 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT); 106 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
109} 107}
110 108
111int opt_set_tcp_keepcnt(lua_State *L, p_socket ps) 109LUASOCKET_PRIVATE int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
112{ 110{
113 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT); 111 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
114} 112}
115#endif 113#endif
116 114
117#ifdef TCP_KEEPINTVL 115#ifdef TCP_KEEPINTVL
118int opt_get_tcp_keepintvl(lua_State *L, p_socket ps) 116LUASOCKET_PRIVATE int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
119{ 117{
120 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL); 118 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
121} 119}
122 120
123int opt_set_tcp_keepintvl(lua_State *L, p_socket ps) 121LUASOCKET_PRIVATE int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
124{ 122{
125 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL); 123 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
126} 124}
127#endif 125#endif
128 126
129int opt_set_keepalive(lua_State *L, p_socket ps) 127LUASOCKET_PRIVATE int opt_set_keepalive(lua_State *L, p_socket ps)
130{ 128{
131 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 129 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
132} 130}
133 131
134int opt_get_keepalive(lua_State *L, p_socket ps) 132LUASOCKET_PRIVATE int opt_get_keepalive(lua_State *L, p_socket ps)
135{ 133{
136 return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 134 return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
137} 135}
138 136
139int opt_set_dontroute(lua_State *L, p_socket ps) 137LUASOCKET_PRIVATE int opt_set_dontroute(lua_State *L, p_socket ps)
140{ 138{
141 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 139 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
142} 140}
143 141
144int opt_get_dontroute(lua_State *L, p_socket ps) 142LUASOCKET_PRIVATE int opt_get_dontroute(lua_State *L, p_socket ps)
145{ 143{
146 return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 144 return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
147} 145}
148 146
149int opt_set_broadcast(lua_State *L, p_socket ps) 147LUASOCKET_PRIVATE int opt_set_broadcast(lua_State *L, p_socket ps)
150{ 148{
151 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 149 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
152} 150}
153 151
154int opt_set_recv_buf_size(lua_State *L, p_socket ps) 152LUASOCKET_PRIVATE int opt_set_recv_buf_size(lua_State *L, p_socket ps)
155{ 153{
156 return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF); 154 return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
157} 155}
158 156
159int opt_get_recv_buf_size(lua_State *L, p_socket ps) 157LUASOCKET_PRIVATE int opt_get_recv_buf_size(lua_State *L, p_socket ps)
160{ 158{
161 return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF); 159 return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
162} 160}
163 161
164int opt_get_send_buf_size(lua_State *L, p_socket ps) 162LUASOCKET_PRIVATE int opt_get_send_buf_size(lua_State *L, p_socket ps)
165{ 163{
166 return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF); 164 return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
167} 165}
168 166
169int opt_set_send_buf_size(lua_State *L, p_socket ps) 167LUASOCKET_PRIVATE int opt_set_send_buf_size(lua_State *L, p_socket ps)
170{ 168{
171 return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF); 169 return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
172} 170}
173 171
174int opt_get_broadcast(lua_State *L, p_socket ps) 172LUASOCKET_PRIVATE int opt_get_broadcast(lua_State *L, p_socket ps)
175{ 173{
176 return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 174 return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
177} 175}
178 176
179int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps) 177LUASOCKET_PRIVATE int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
180{ 178{
181 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 179 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
182} 180}
183 181
184int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps) 182LUASOCKET_PRIVATE int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
185{ 183{
186 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 184 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
187} 185}
188 186
189int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps) 187LUASOCKET_PRIVATE int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
190{ 188{
191 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 189 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
192} 190}
193 191
194int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps) 192LUASOCKET_PRIVATE int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
195{ 193{
196 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 194 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
197} 195}
198 196
199int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) 197LUASOCKET_PRIVATE int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
200{ 198{
201 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 199 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
202} 200}
203 201
204int opt_get_ip_multicast_loop(lua_State *L, p_socket ps) 202LUASOCKET_PRIVATE int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
205{ 203{
206 return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 204 return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
207} 205}
208 206
209int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps) 207LUASOCKET_PRIVATE int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
210{ 208{
211 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 209 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
212} 210}
213 211
214int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps) 212LUASOCKET_PRIVATE int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
215{ 213{
216 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 214 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
217} 215}
218 216
219int opt_set_linger(lua_State *L, p_socket ps) 217LUASOCKET_PRIVATE int opt_set_linger(lua_State *L, p_socket ps)
220{ 218{
221 struct linger li; /* obj, name, table */ 219 struct linger li; /* obj, name, table */
222 if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); 220 if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
@@ -233,7 +231,7 @@ int opt_set_linger(lua_State *L, p_socket ps)
233 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); 231 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
234} 232}
235 233
236int opt_get_linger(lua_State *L, p_socket ps) 234LUASOCKET_PRIVATE int opt_get_linger(lua_State *L, p_socket ps)
237{ 235{
238 struct linger li; /* obj, name */ 236 struct linger li; /* obj, name */
239 int len = sizeof(li); 237 int len = sizeof(li);
@@ -248,12 +246,12 @@ int opt_get_linger(lua_State *L, p_socket ps)
248 return 1; 246 return 1;
249} 247}
250 248
251int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) 249LUASOCKET_PRIVATE int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
252{ 250{
253 return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL); 251 return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
254} 252}
255 253
256int opt_set_ip_multicast_if(lua_State *L, p_socket ps) 254LUASOCKET_PRIVATE int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
257{ 255{
258 const char *address = luaL_checkstring(L, 3); /* obj, name, ip */ 256 const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
259 struct in_addr val; 257 struct in_addr val;
@@ -264,7 +262,7 @@ int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
264 (char *) &val, sizeof(val)); 262 (char *) &val, sizeof(val));
265} 263}
266 264
267int opt_get_ip_multicast_if(lua_State *L, p_socket ps) 265LUASOCKET_PRIVATE int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
268{ 266{
269 struct in_addr val; 267 struct in_addr val;
270 socklen_t len = sizeof(val); 268 socklen_t len = sizeof(val);
@@ -277,32 +275,32 @@ int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
277 return 1; 275 return 1;
278} 276}
279 277
280int opt_set_ip_add_membership(lua_State *L, p_socket ps) 278LUASOCKET_PRIVATE int opt_set_ip_add_membership(lua_State *L, p_socket ps)
281{ 279{
282 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); 280 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
283} 281}
284 282
285int opt_set_ip_drop_membersip(lua_State *L, p_socket ps) 283LUASOCKET_PRIVATE int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
286{ 284{
287 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); 285 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
288} 286}
289 287
290int opt_set_ip6_add_membership(lua_State *L, p_socket ps) 288LUASOCKET_PRIVATE int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
291{ 289{
292 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP); 290 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
293} 291}
294 292
295int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps) 293LUASOCKET_PRIVATE int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
296{ 294{
297 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP); 295 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
298} 296}
299 297
300int opt_get_ip6_v6only(lua_State *L, p_socket ps) 298LUASOCKET_PRIVATE int opt_get_ip6_v6only(lua_State *L, p_socket ps)
301{ 299{
302 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 300 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
303} 301}
304 302
305int opt_set_ip6_v6only(lua_State *L, p_socket ps) 303LUASOCKET_PRIVATE int opt_set_ip6_v6only(lua_State *L, p_socket ps)
306{ 304{
307 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 305 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
308} 306}
@@ -393,7 +391,7 @@ static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
393 return 1; 391 return 1;
394} 392}
395 393
396int opt_get_error(lua_State *L, p_socket ps) 394LUASOCKET_PRIVATE int opt_get_error(lua_State *L, p_socket ps)
397{ 395{
398 int val = 0; 396 int val = 0;
399 socklen_t len = sizeof(val); 397 socklen_t len = sizeof(val);
diff --git a/src/select.c b/src/select.c
index 9d133b7..b615b19 100644
--- a/src/select.c
+++ b/src/select.c
@@ -2,7 +2,7 @@
2* Select implementation 2* Select implementation
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6 6
7#include "lua.h" 7#include "lua.h"
8#include "lauxlib.h" 8#include "lauxlib.h"
@@ -12,6 +12,8 @@
12#include "timeout.h" 12#include "timeout.h"
13#include "select.h" 13#include "select.h"
14 14
15#include <string.h>
16
15/*=========================================================================*\ 17/*=========================================================================*\
16* Internal function prototypes. 18* Internal function prototypes.
17\*=========================================================================*/ 19\*=========================================================================*/
@@ -37,7 +39,7 @@ static luaL_Reg func[] = {
37/*-------------------------------------------------------------------------*\ 39/*-------------------------------------------------------------------------*\
38* Initializes module 40* Initializes module
39\*-------------------------------------------------------------------------*/ 41\*-------------------------------------------------------------------------*/
40int select_open(lua_State *L) { 42LUASOCKET_PRIVATE int select_open(lua_State *L) {
41 lua_pushstring(L, "_SETSIZE"); 43 lua_pushstring(L, "_SETSIZE");
42 lua_pushinteger(L, FD_SETSIZE); 44 lua_pushinteger(L, FD_SETSIZE);
43 lua_rawset(L, -3); 45 lua_rawset(L, -3);
diff --git a/src/serial.c b/src/serial.c
index 7bdb21c..cb46edb 100644
--- a/src/serial.c
+++ b/src/serial.c
@@ -2,6 +2,8 @@
2* Serial stream 2* Serial stream
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6
5#include <string.h> 7#include <string.h>
6 8
7#include "lua.h" 9#include "lua.h"
diff --git a/src/tcp.c b/src/tcp.c
index c7384b4..cc5b6a7 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -2,18 +2,21 @@
2* TCP object 2* TCP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6 6
7#include "lua.h" 7#include "lua.h"
8#include "lauxlib.h" 8#include "lauxlib.h"
9#include "compat.h"
10 9
10#include "compat.h"
11#include "auxiliar.h" 11#include "auxiliar.h"
12
12#include "socket.h" 13#include "socket.h"
13#include "inet.h" 14#include "inet.h"
14#include "options.h" 15#include "options.h"
15#include "tcp.h" 16#include "tcp.h"
16 17
18#include <string.h>
19
17/*=========================================================================*\ 20/*=========================================================================*\
18* Internal function prototypes 21* Internal function prototypes
19\*=========================================================================*/ 22\*=========================================================================*/
@@ -126,7 +129,7 @@ static luaL_Reg func[] = {
126/*-------------------------------------------------------------------------*\ 129/*-------------------------------------------------------------------------*\
127* Initializes module 130* Initializes module
128\*-------------------------------------------------------------------------*/ 131\*-------------------------------------------------------------------------*/
129int tcp_open(lua_State *L) 132LUASOCKET_PRIVATE int tcp_open(lua_State *L)
130{ 133{
131 /* create classes */ 134 /* create classes */
132 auxiliar_newclass(L, "tcp{master}", tcp_methods); 135 auxiliar_newclass(L, "tcp{master}", tcp_methods);
diff --git a/src/timeout.c b/src/timeout.c
index 5a601d5..8fb8f55 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -2,9 +2,7 @@
2* Timeout management functions 2* Timeout management functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <stdio.h> 5#include "luasocket.h"
6#include <limits.h>
7#include <float.h>
8 6
9#include "lua.h" 7#include "lua.h"
10#include "lauxlib.h" 8#include "lauxlib.h"
@@ -13,6 +11,10 @@
13#include "auxiliar.h" 11#include "auxiliar.h"
14#include "timeout.h" 12#include "timeout.h"
15 13
14#include <stdio.h>
15#include <limits.h>
16#include <float.h>
17
16#ifdef _WIN32 18#ifdef _WIN32
17#include <windows.h> 19#include <windows.h>
18#else 20#else
@@ -46,7 +48,7 @@ static luaL_Reg func[] = {
46/*-------------------------------------------------------------------------*\ 48/*-------------------------------------------------------------------------*\
47* Initialize structure 49* Initialize structure
48\*-------------------------------------------------------------------------*/ 50\*-------------------------------------------------------------------------*/
49void timeout_init(p_timeout tm, double block, double total) { 51LUASOCKET_PRIVATE void timeout_init(p_timeout tm, double block, double total) {
50 tm->block = block; 52 tm->block = block;
51 tm->total = total; 53 tm->total = total;
52} 54}
@@ -59,7 +61,7 @@ void timeout_init(p_timeout tm, double block, double total) {
59* Returns 61* Returns
60* the number of ms left or -1 if there is no time limit 62* the number of ms left or -1 if there is no time limit
61\*-------------------------------------------------------------------------*/ 63\*-------------------------------------------------------------------------*/
62double timeout_get(p_timeout tm) { 64LUASOCKET_PRIVATE double timeout_get(p_timeout tm) {
63 if (tm->block < 0.0 && tm->total < 0.0) { 65 if (tm->block < 0.0 && tm->total < 0.0) {
64 return -1; 66 return -1;
65 } else if (tm->block < 0.0) { 67 } else if (tm->block < 0.0) {
@@ -80,7 +82,7 @@ double timeout_get(p_timeout tm) {
80* Returns 82* Returns
81* start field of structure 83* start field of structure
82\*-------------------------------------------------------------------------*/ 84\*-------------------------------------------------------------------------*/
83double timeout_getstart(p_timeout tm) { 85LUASOCKET_PRIVATE double timeout_getstart(p_timeout tm) {
84 return tm->start; 86 return tm->start;
85} 87}
86 88
@@ -92,7 +94,7 @@ double timeout_getstart(p_timeout tm) {
92* Returns 94* Returns
93* the number of ms left or -1 if there is no time limit 95* the number of ms left or -1 if there is no time limit
94\*-------------------------------------------------------------------------*/ 96\*-------------------------------------------------------------------------*/
95double timeout_getretry(p_timeout tm) { 97LUASOCKET_PRIVATE double timeout_getretry(p_timeout tm) {
96 if (tm->block < 0.0 && tm->total < 0.0) { 98 if (tm->block < 0.0 && tm->total < 0.0) {
97 return -1; 99 return -1;
98 } else if (tm->block < 0.0) { 100 } else if (tm->block < 0.0) {
@@ -112,7 +114,7 @@ double timeout_getretry(p_timeout tm) {
112* Input 114* Input
113* tm: timeout control structure 115* tm: timeout control structure
114\*-------------------------------------------------------------------------*/ 116\*-------------------------------------------------------------------------*/
115p_timeout timeout_markstart(p_timeout tm) { 117LUASOCKET_PRIVATE p_timeout timeout_markstart(p_timeout tm) {
116 tm->start = timeout_gettime(); 118 tm->start = timeout_gettime();
117 return tm; 119 return tm;
118} 120}
@@ -123,7 +125,7 @@ p_timeout timeout_markstart(p_timeout tm) {
123* time in s. 125* time in s.
124\*-------------------------------------------------------------------------*/ 126\*-------------------------------------------------------------------------*/
125#ifdef _WIN32 127#ifdef _WIN32
126double timeout_gettime(void) { 128LUASOCKET_PRIVATE double timeout_gettime(void) {
127 FILETIME ft; 129 FILETIME ft;
128 double t; 130 double t;
129 GetSystemTimeAsFileTime(&ft); 131 GetSystemTimeAsFileTime(&ft);
@@ -133,7 +135,7 @@ double timeout_gettime(void) {
133 return (t - 11644473600.0); 135 return (t - 11644473600.0);
134} 136}
135#else 137#else
136double timeout_gettime(void) { 138LUASOCKET_PRIVATE double timeout_gettime(void) {
137 struct timeval v; 139 struct timeval v;
138 gettimeofday(&v, (struct timezone *) NULL); 140 gettimeofday(&v, (struct timezone *) NULL);
139 /* Unix Epoch time (time since January 1, 1970 (UTC)) */ 141 /* Unix Epoch time (time since January 1, 1970 (UTC)) */
@@ -144,7 +146,7 @@ double timeout_gettime(void) {
144/*-------------------------------------------------------------------------*\ 146/*-------------------------------------------------------------------------*\
145* Initializes module 147* Initializes module
146\*-------------------------------------------------------------------------*/ 148\*-------------------------------------------------------------------------*/
147int timeout_open(lua_State *L) { 149LUASOCKET_PRIVATE int timeout_open(lua_State *L) {
148 luaL_setfuncs(L, func, 0); 150 luaL_setfuncs(L, func, 0);
149 return 0; 151 return 0;
150} 152}
@@ -155,7 +157,7 @@ int timeout_open(lua_State *L) {
155* time: time out value in seconds 157* time: time out value in seconds
156* mode: "b" for block timeout, "t" for total timeout. (default: b) 158* mode: "b" for block timeout, "t" for total timeout. (default: b)
157\*-------------------------------------------------------------------------*/ 159\*-------------------------------------------------------------------------*/
158int timeout_meth_settimeout(lua_State *L, p_timeout tm) { 160LUASOCKET_PRIVATE int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
159 double t = luaL_optnumber(L, 2, -1); 161 double t = luaL_optnumber(L, 2, -1);
160 const char *mode = luaL_optstring(L, 3, "b"); 162 const char *mode = luaL_optstring(L, 3, "b");
161 switch (*mode) { 163 switch (*mode) {
@@ -177,7 +179,7 @@ int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
177* Gets timeout values for IO operations 179* Gets timeout values for IO operations
178* Lua Output: block, total 180* Lua Output: block, total
179\*-------------------------------------------------------------------------*/ 181\*-------------------------------------------------------------------------*/
180int timeout_meth_gettimeout(lua_State *L, p_timeout tm) { 182LUASOCKET_PRIVATE int timeout_meth_gettimeout(lua_State *L, p_timeout tm) {
181 lua_pushnumber(L, tm->block); 183 lua_pushnumber(L, tm->block);
182 lua_pushnumber(L, tm->total); 184 lua_pushnumber(L, tm->total);
183 return 2; 185 return 2;
@@ -199,7 +201,7 @@ static int timeout_lua_gettime(lua_State *L)
199* Sleep for n seconds. 201* Sleep for n seconds.
200\*-------------------------------------------------------------------------*/ 202\*-------------------------------------------------------------------------*/
201#ifdef _WIN32 203#ifdef _WIN32
202int timeout_lua_sleep(lua_State *L) 204LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
203{ 205{
204 double n = luaL_checknumber(L, 1); 206 double n = luaL_checknumber(L, 1);
205 if (n < 0.0) n = 0.0; 207 if (n < 0.0) n = 0.0;
@@ -209,7 +211,7 @@ int timeout_lua_sleep(lua_State *L)
209 return 0; 211 return 0;
210} 212}
211#else 213#else
212int timeout_lua_sleep(lua_State *L) 214LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
213{ 215{
214 double n = luaL_checknumber(L, 1); 216 double n = luaL_checknumber(L, 1);
215 struct timespec t, r; 217 struct timespec t, r;
diff --git a/src/udp.c b/src/udp.c
index 037f9a4..99a766f 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -2,8 +2,7 @@
2* UDP object 2* UDP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6#include <stdlib.h>
7 6
8#include "lua.h" 7#include "lua.h"
9#include "lauxlib.h" 8#include "lauxlib.h"
@@ -15,6 +14,9 @@
15#include "options.h" 14#include "options.h"
16#include "udp.h" 15#include "udp.h"
17 16
17#include <string.h>
18#include <stdlib.h>
19
18/* min and max macros */ 20/* min and max macros */
19#ifndef MIN 21#ifndef MIN
20#define MIN(x, y) ((x) < (y) ? x : y) 22#define MIN(x, y) ((x) < (y) ? x : y)
@@ -122,7 +124,7 @@ static luaL_Reg func[] = {
122/*-------------------------------------------------------------------------*\ 124/*-------------------------------------------------------------------------*\
123* Initializes module 125* Initializes module
124\*-------------------------------------------------------------------------*/ 126\*-------------------------------------------------------------------------*/
125int udp_open(lua_State *L) { 127LUASOCKET_PRIVATE int udp_open(lua_State *L) {
126 /* create classes */ 128 /* create classes */
127 auxiliar_newclass(L, "udp{connected}", udp_methods); 129 auxiliar_newclass(L, "udp{connected}", udp_methods);
128 auxiliar_newclass(L, "udp{unconnected}", udp_methods); 130 auxiliar_newclass(L, "udp{unconnected}", udp_methods);
diff --git a/src/unix.c b/src/unix.c
index dbc8710..c618a20 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -2,6 +2,8 @@
2* Unix domain socket 2* Unix domain socket
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6
5#include "lua.h" 7#include "lua.h"
6#include "lauxlib.h" 8#include "lauxlib.h"
7 9
@@ -45,7 +47,7 @@ static int compat_socket_unix_call(lua_State *L)
45/*-------------------------------------------------------------------------*\ 47/*-------------------------------------------------------------------------*\
46* Initializes module 48* Initializes module
47\*-------------------------------------------------------------------------*/ 49\*-------------------------------------------------------------------------*/
48int luaopen_socket_unix(lua_State *L) 50LUASOCKET_API int luaopen_socket_unix(lua_State *L)
49{ 51{
50 int i; 52 int i;
51 lua_newtable(L); 53 lua_newtable(L);
diff --git a/src/unix.h b/src/unix.h
index 8cc7a79..a1674ef 100644
--- a/src/unix.h
+++ b/src/unix.h
@@ -7,16 +7,13 @@
7* This module is just an example of how to extend LuaSocket with a new 7* This module is just an example of how to extend LuaSocket with a new
8* domain. 8* domain.
9\*=========================================================================*/ 9\*=========================================================================*/
10#include "luasocket.h"
10#include "lua.h" 11#include "lua.h"
11 12
12#include "buffer.h" 13#include "buffer.h"
13#include "timeout.h" 14#include "timeout.h"
14#include "socket.h" 15#include "socket.h"
15 16
16#ifndef UNIX_API
17#define UNIX_API extern
18#endif
19
20typedef struct t_unix_ { 17typedef struct t_unix_ {
21 t_socket sock; 18 t_socket sock;
22 t_io io; 19 t_io io;
@@ -25,6 +22,6 @@ typedef struct t_unix_ {
25} t_unix; 22} t_unix;
26typedef t_unix *p_unix; 23typedef t_unix *p_unix;
27 24
28UNIX_API int luaopen_socket_unix(lua_State *L); 25LUASOCKET_API int luaopen_socket_unix(lua_State *L);
29 26
30#endif /* UNIX_H */ 27#endif /* UNIX_H */
diff --git a/src/unixdgram.c b/src/unixdgram.c
index 0d6f18c..840257a 100644
--- a/src/unixdgram.c
+++ b/src/unixdgram.c
@@ -2,8 +2,7 @@
2* Unix domain socket dgram submodule 2* Unix domain socket dgram submodule
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6#include <stdlib.h>
7 6
8#include "lua.h" 7#include "lua.h"
9#include "lauxlib.h" 8#include "lauxlib.h"
@@ -13,6 +12,10 @@
13#include "socket.h" 12#include "socket.h"
14#include "options.h" 13#include "options.h"
15#include "unix.h" 14#include "unix.h"
15
16#include <string.h>
17#include <stdlib.h>
18
16#include <sys/un.h> 19#include <sys/un.h>
17 20
18#define UNIXDGRAM_DATAGRAMSIZE 8192 21#define UNIXDGRAM_DATAGRAMSIZE 8192
@@ -83,7 +86,7 @@ static luaL_Reg func[] = {
83/*-------------------------------------------------------------------------*\ 86/*-------------------------------------------------------------------------*\
84* Initializes module 87* Initializes module
85\*-------------------------------------------------------------------------*/ 88\*-------------------------------------------------------------------------*/
86int unixdgram_open(lua_State *L) 89LUASOCKET_PRIVATE int unixdgram_open(lua_State *L)
87{ 90{
88 /* create classes */ 91 /* create classes */
89 auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods); 92 auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
diff --git a/src/unixstream.c b/src/unixstream.c
index 0b9055c..ce2d3af 100644
--- a/src/unixstream.c
+++ b/src/unixstream.c
@@ -2,7 +2,7 @@
2* Unix domain socket stream sub module 2* Unix domain socket stream sub module
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6 6
7#include "lua.h" 7#include "lua.h"
8#include "lauxlib.h" 8#include "lauxlib.h"
@@ -12,6 +12,8 @@
12#include "socket.h" 12#include "socket.h"
13#include "options.h" 13#include "options.h"
14#include "unixstream.h" 14#include "unixstream.h"
15
16#include <string.h>
15#include <sys/un.h> 17#include <sys/un.h>
16 18
17/*=========================================================================*\ 19/*=========================================================================*\
@@ -80,7 +82,7 @@ static luaL_Reg func[] = {
80/*-------------------------------------------------------------------------*\ 82/*-------------------------------------------------------------------------*\
81* Initializes module 83* Initializes module
82\*-------------------------------------------------------------------------*/ 84\*-------------------------------------------------------------------------*/
83int unixstream_open(lua_State *L) 85LUASOCKET_PRIVATE int unixstream_open(lua_State *L)
84{ 86{
85 /* create classes */ 87 /* create classes */
86 auxiliar_newclass(L, "unixstream{master}", unixstream_methods); 88 auxiliar_newclass(L, "unixstream{master}", unixstream_methods);
diff --git a/src/usocket.c b/src/usocket.c
index 6e7f8f6..08a961d 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -6,12 +6,14 @@
6* The penalty of calling select to avoid busy-wait is only paid when 6* The penalty of calling select to avoid busy-wait is only paid when
7* the I/O call fail in the first place. 7* the I/O call fail in the first place.
8\*=========================================================================*/ 8\*=========================================================================*/
9#include <string.h> 9#include "luasocket.h"
10#include <signal.h>
11 10
12#include "socket.h" 11#include "socket.h"
13#include "pierror.h" 12#include "pierror.h"
14 13
14#include <string.h>
15#include <signal.h>
16
15/*-------------------------------------------------------------------------*\ 17/*-------------------------------------------------------------------------*\
16* Wait for readable/writable/connected socket with timeout 18* Wait for readable/writable/connected socket with timeout
17\*-------------------------------------------------------------------------*/ 19\*-------------------------------------------------------------------------*/
@@ -21,7 +23,7 @@
21#define WAITFD_R POLLIN 23#define WAITFD_R POLLIN
22#define WAITFD_W POLLOUT 24#define WAITFD_W POLLOUT
23#define WAITFD_C (POLLIN|POLLOUT) 25#define WAITFD_C (POLLIN|POLLOUT)
24int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 26LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
25 int ret; 27 int ret;
26 struct pollfd pfd; 28 struct pollfd pfd;
27 pfd.fd = *ps; 29 pfd.fd = *ps;
@@ -43,7 +45,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
43#define WAITFD_W 2 45#define WAITFD_W 2
44#define WAITFD_C (WAITFD_R|WAITFD_W) 46#define WAITFD_C (WAITFD_R|WAITFD_W)
45 47
46int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 48LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
47 int ret; 49 int ret;
48 fd_set rfds, wfds, *rp, *wp; 50 fd_set rfds, wfds, *rp, *wp;
49 struct timeval tv, *tp; 51 struct timeval tv, *tp;
@@ -75,7 +77,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
75/*-------------------------------------------------------------------------*\ 77/*-------------------------------------------------------------------------*\
76* Initializes module 78* Initializes module
77\*-------------------------------------------------------------------------*/ 79\*-------------------------------------------------------------------------*/
78int socket_open(void) { 80LUASOCKET_PRIVATE int socket_open(void) {
79 /* installs a handler to ignore sigpipe or it will crash us */ 81 /* installs a handler to ignore sigpipe or it will crash us */
80 signal(SIGPIPE, SIG_IGN); 82 signal(SIGPIPE, SIG_IGN);
81 return 1; 83 return 1;
@@ -84,14 +86,14 @@ int socket_open(void) {
84/*-------------------------------------------------------------------------*\ 86/*-------------------------------------------------------------------------*\
85* Close module 87* Close module
86\*-------------------------------------------------------------------------*/ 88\*-------------------------------------------------------------------------*/
87int socket_close(void) { 89LUASOCKET_PRIVATE int socket_close(void) {
88 return 1; 90 return 1;
89} 91}
90 92
91/*-------------------------------------------------------------------------*\ 93/*-------------------------------------------------------------------------*\
92* Close and inutilize socket 94* Close and inutilize socket
93\*-------------------------------------------------------------------------*/ 95\*-------------------------------------------------------------------------*/
94void socket_destroy(p_socket ps) { 96LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
95 if (*ps != SOCKET_INVALID) { 97 if (*ps != SOCKET_INVALID) {
96 close(*ps); 98 close(*ps);
97 *ps = SOCKET_INVALID; 99 *ps = SOCKET_INVALID;
@@ -101,7 +103,7 @@ void socket_destroy(p_socket ps) {
101/*-------------------------------------------------------------------------*\ 103/*-------------------------------------------------------------------------*\
102* Select with timeout control 104* Select with timeout control
103\*-------------------------------------------------------------------------*/ 105\*-------------------------------------------------------------------------*/
104int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 106LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
105 p_timeout tm) { 107 p_timeout tm) {
106 int ret; 108 int ret;
107 do { 109 do {
@@ -118,7 +120,7 @@ int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
118/*-------------------------------------------------------------------------*\ 120/*-------------------------------------------------------------------------*\
119* Creates and sets up a socket 121* Creates and sets up a socket
120\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
121int socket_create(p_socket ps, int domain, int type, int protocol) { 123LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
122 *ps = socket(domain, type, protocol); 124 *ps = socket(domain, type, protocol);
123 if (*ps != SOCKET_INVALID) return IO_DONE; 125 if (*ps != SOCKET_INVALID) return IO_DONE;
124 else return errno; 126 else return errno;
@@ -127,7 +129,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) {
127/*-------------------------------------------------------------------------*\ 129/*-------------------------------------------------------------------------*\
128* Binds or returns error message 130* Binds or returns error message
129\*-------------------------------------------------------------------------*/ 131\*-------------------------------------------------------------------------*/
130int socket_bind(p_socket ps, SA *addr, socklen_t len) { 132LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
131 int err = IO_DONE; 133 int err = IO_DONE;
132 socket_setblocking(ps); 134 socket_setblocking(ps);
133 if (bind(*ps, addr, len) < 0) err = errno; 135 if (bind(*ps, addr, len) < 0) err = errno;
@@ -138,7 +140,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) {
138/*-------------------------------------------------------------------------*\ 140/*-------------------------------------------------------------------------*\
139* 141*
140\*-------------------------------------------------------------------------*/ 142\*-------------------------------------------------------------------------*/
141int socket_listen(p_socket ps, int backlog) { 143LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
142 int err = IO_DONE; 144 int err = IO_DONE;
143 if (listen(*ps, backlog)) err = errno; 145 if (listen(*ps, backlog)) err = errno;
144 return err; 146 return err;
@@ -147,14 +149,14 @@ int socket_listen(p_socket ps, int backlog) {
147/*-------------------------------------------------------------------------*\ 149/*-------------------------------------------------------------------------*\
148* 150*
149\*-------------------------------------------------------------------------*/ 151\*-------------------------------------------------------------------------*/
150void socket_shutdown(p_socket ps, int how) { 152LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
151 shutdown(*ps, how); 153 shutdown(*ps, how);
152} 154}
153 155
154/*-------------------------------------------------------------------------*\ 156/*-------------------------------------------------------------------------*\
155* Connects or returns error message 157* Connects or returns error message
156\*-------------------------------------------------------------------------*/ 158\*-------------------------------------------------------------------------*/
157int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { 159LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
158 int err; 160 int err;
159 /* avoid calling on closed sockets */ 161 /* avoid calling on closed sockets */
160 if (*ps == SOCKET_INVALID) return IO_CLOSED; 162 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -176,7 +178,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
176/*-------------------------------------------------------------------------*\ 178/*-------------------------------------------------------------------------*\
177* Accept with timeout 179* Accept with timeout
178\*-------------------------------------------------------------------------*/ 180\*-------------------------------------------------------------------------*/
179int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { 181LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
180 if (*ps == SOCKET_INVALID) return IO_CLOSED; 182 if (*ps == SOCKET_INVALID) return IO_CLOSED;
181 for ( ;; ) { 183 for ( ;; ) {
182 int err; 184 int err;
@@ -193,7 +195,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout
193/*-------------------------------------------------------------------------*\ 195/*-------------------------------------------------------------------------*\
194* Send with timeout 196* Send with timeout
195\*-------------------------------------------------------------------------*/ 197\*-------------------------------------------------------------------------*/
196int socket_send(p_socket ps, const char *data, size_t count, 198LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
197 size_t *sent, p_timeout tm) 199 size_t *sent, p_timeout tm)
198{ 200{
199 int err; 201 int err;
@@ -227,7 +229,7 @@ int socket_send(p_socket ps, const char *data, size_t count,
227/*-------------------------------------------------------------------------*\ 229/*-------------------------------------------------------------------------*\
228* Sendto with timeout 230* Sendto with timeout
229\*-------------------------------------------------------------------------*/ 231\*-------------------------------------------------------------------------*/
230int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, 232LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
231 SA *addr, socklen_t len, p_timeout tm) 233 SA *addr, socklen_t len, p_timeout tm)
232{ 234{
233 int err; 235 int err;
@@ -252,7 +254,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
252/*-------------------------------------------------------------------------*\ 254/*-------------------------------------------------------------------------*\
253* Receive with timeout 255* Receive with timeout
254\*-------------------------------------------------------------------------*/ 256\*-------------------------------------------------------------------------*/
255int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 257LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
256 int err; 258 int err;
257 *got = 0; 259 *got = 0;
258 if (*ps == SOCKET_INVALID) return IO_CLOSED; 260 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -274,7 +276,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
274/*-------------------------------------------------------------------------*\ 276/*-------------------------------------------------------------------------*\
275* Recvfrom with timeout 277* Recvfrom with timeout
276\*-------------------------------------------------------------------------*/ 278\*-------------------------------------------------------------------------*/
277int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, 279LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
278 SA *addr, socklen_t *len, p_timeout tm) { 280 SA *addr, socklen_t *len, p_timeout tm) {
279 int err; 281 int err;
280 *got = 0; 282 *got = 0;
@@ -302,7 +304,7 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
302* with send/recv replaced with write/read. We can't just use write/read 304* with send/recv replaced with write/read. We can't just use write/read
303* in the socket version, because behaviour when size is zero is different. 305* in the socket version, because behaviour when size is zero is different.
304\*-------------------------------------------------------------------------*/ 306\*-------------------------------------------------------------------------*/
305int socket_write(p_socket ps, const char *data, size_t count, 307LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
306 size_t *sent, p_timeout tm) 308 size_t *sent, p_timeout tm)
307{ 309{
308 int err; 310 int err;
@@ -337,7 +339,7 @@ int socket_write(p_socket ps, const char *data, size_t count,
337* Read with timeout 339* Read with timeout
338* See note for socket_write 340* See note for socket_write
339\*-------------------------------------------------------------------------*/ 341\*-------------------------------------------------------------------------*/
340int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 342LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
341 int err; 343 int err;
342 *got = 0; 344 *got = 0;
343 if (*ps == SOCKET_INVALID) return IO_CLOSED; 345 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -359,7 +361,7 @@ int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm
359/*-------------------------------------------------------------------------*\ 361/*-------------------------------------------------------------------------*\
360* Put socket into blocking mode 362* Put socket into blocking mode
361\*-------------------------------------------------------------------------*/ 363\*-------------------------------------------------------------------------*/
362void socket_setblocking(p_socket ps) { 364LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
363 int flags = fcntl(*ps, F_GETFL, 0); 365 int flags = fcntl(*ps, F_GETFL, 0);
364 flags &= (~(O_NONBLOCK)); 366 flags &= (~(O_NONBLOCK));
365 fcntl(*ps, F_SETFL, flags); 367 fcntl(*ps, F_SETFL, flags);
@@ -368,7 +370,7 @@ void socket_setblocking(p_socket ps) {
368/*-------------------------------------------------------------------------*\ 370/*-------------------------------------------------------------------------*\
369* Put socket into non-blocking mode 371* Put socket into non-blocking mode
370\*-------------------------------------------------------------------------*/ 372\*-------------------------------------------------------------------------*/
371void socket_setnonblocking(p_socket ps) { 373LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
372 int flags = fcntl(*ps, F_GETFL, 0); 374 int flags = fcntl(*ps, F_GETFL, 0);
373 flags |= O_NONBLOCK; 375 flags |= O_NONBLOCK;
374 fcntl(*ps, F_SETFL, flags); 376 fcntl(*ps, F_SETFL, flags);
@@ -377,7 +379,7 @@ void socket_setnonblocking(p_socket ps) {
377/*-------------------------------------------------------------------------*\ 379/*-------------------------------------------------------------------------*\
378* DNS helpers 380* DNS helpers
379\*-------------------------------------------------------------------------*/ 381\*-------------------------------------------------------------------------*/
380int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 382LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
381 *hp = gethostbyaddr(addr, len, AF_INET); 383 *hp = gethostbyaddr(addr, len, AF_INET);
382 if (*hp) return IO_DONE; 384 if (*hp) return IO_DONE;
383 else if (h_errno) return h_errno; 385 else if (h_errno) return h_errno;
@@ -385,7 +387,7 @@ int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
385 else return IO_UNKNOWN; 387 else return IO_UNKNOWN;
386} 388}
387 389
388int socket_gethostbyname(const char *addr, struct hostent **hp) { 390LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
389 *hp = gethostbyname(addr); 391 *hp = gethostbyname(addr);
390 if (*hp) return IO_DONE; 392 if (*hp) return IO_DONE;
391 else if (h_errno) return h_errno; 393 else if (h_errno) return h_errno;
@@ -397,7 +399,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) {
397* Error translation functions 399* Error translation functions
398* Make sure important error messages are standard 400* Make sure important error messages are standard
399\*-------------------------------------------------------------------------*/ 401\*-------------------------------------------------------------------------*/
400const char *socket_hoststrerror(int err) { 402LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
401 if (err <= 0) return io_strerror(err); 403 if (err <= 0) return io_strerror(err);
402 switch (err) { 404 switch (err) {
403 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; 405 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@@ -405,7 +407,7 @@ const char *socket_hoststrerror(int err) {
405 } 407 }
406} 408}
407 409
408const char *socket_strerror(int err) { 410LUASOCKET_PRIVATE const char *socket_strerror(int err) {
409 if (err <= 0) return io_strerror(err); 411 if (err <= 0) return io_strerror(err);
410 switch (err) { 412 switch (err) {
411 case EADDRINUSE: return PIE_ADDRINUSE; 413 case EADDRINUSE: return PIE_ADDRINUSE;
@@ -421,12 +423,12 @@ const char *socket_strerror(int err) {
421 } 423 }
422} 424}
423 425
424const char *socket_ioerror(p_socket ps, int err) { 426LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
425 (void) ps; 427 (void) ps;
426 return socket_strerror(err); 428 return socket_strerror(err);
427} 429}
428 430
429const char *socket_gaistrerror(int err) { 431LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
430 if (err == 0) return NULL; 432 if (err == 0) return NULL;
431 switch (err) { 433 switch (err) {
432 case EAI_AGAIN: return PIE_AGAIN; 434 case EAI_AGAIN: return PIE_AGAIN;
diff --git a/src/wsocket.c b/src/wsocket.c
index ac8411f..c281058 100755
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -5,6 +5,8 @@
5* The penalty of calling select to avoid busy-wait is only paid when 5* The penalty of calling select to avoid busy-wait is only paid when
6* the I/O call fail in the first place. 6* the I/O call fail in the first place.
7\*=========================================================================*/ 7\*=========================================================================*/
8#include "luasocket.h"
9
8#include <string.h> 10#include <string.h>
9 11
10#include "socket.h" 12#include "socket.h"
@@ -16,7 +18,7 @@ static const char *wstrerror(int err);
16/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
17* Initializes module 19* Initializes module
18\*-------------------------------------------------------------------------*/ 20\*-------------------------------------------------------------------------*/
19int socket_open(void) { 21LUASOCKET_PRIVATE int socket_open(void) {
20 WSADATA wsaData; 22 WSADATA wsaData;
21 WORD wVersionRequested = MAKEWORD(2, 0); 23 WORD wVersionRequested = MAKEWORD(2, 0);
22 int err = WSAStartup(wVersionRequested, &wsaData ); 24 int err = WSAStartup(wVersionRequested, &wsaData );
@@ -32,7 +34,7 @@ int socket_open(void) {
32/*-------------------------------------------------------------------------*\ 34/*-------------------------------------------------------------------------*\
33* Close module 35* Close module
34\*-------------------------------------------------------------------------*/ 36\*-------------------------------------------------------------------------*/
35int socket_close(void) { 37LUASOCKET_PRIVATE int socket_close(void) {
36 WSACleanup(); 38 WSACleanup();
37 return 1; 39 return 1;
38} 40}
@@ -45,7 +47,7 @@ int socket_close(void) {
45#define WAITFD_E 4 47#define WAITFD_E 4
46#define WAITFD_C (WAITFD_E|WAITFD_W) 48#define WAITFD_C (WAITFD_E|WAITFD_W)
47 49
48int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 50LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
49 int ret; 51 int ret;
50 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; 52 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
51 struct timeval tv, *tp = NULL; 53 struct timeval tv, *tp = NULL;
@@ -73,7 +75,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
73/*-------------------------------------------------------------------------*\ 75/*-------------------------------------------------------------------------*\
74* Select with int timeout in ms 76* Select with int timeout in ms
75\*-------------------------------------------------------------------------*/ 77\*-------------------------------------------------------------------------*/
76int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 78LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
77 p_timeout tm) { 79 p_timeout tm) {
78 struct timeval tv; 80 struct timeval tv;
79 double t = timeout_get(tm); 81 double t = timeout_get(tm);
@@ -88,7 +90,7 @@ int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
88/*-------------------------------------------------------------------------*\ 90/*-------------------------------------------------------------------------*\
89* Close and inutilize socket 91* Close and inutilize socket
90\*-------------------------------------------------------------------------*/ 92\*-------------------------------------------------------------------------*/
91void socket_destroy(p_socket ps) { 93LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
92 if (*ps != SOCKET_INVALID) { 94 if (*ps != SOCKET_INVALID) {
93 socket_setblocking(ps); /* close can take a long time on WIN32 */ 95 socket_setblocking(ps); /* close can take a long time on WIN32 */
94 closesocket(*ps); 96 closesocket(*ps);
@@ -99,7 +101,7 @@ void socket_destroy(p_socket ps) {
99/*-------------------------------------------------------------------------*\ 101/*-------------------------------------------------------------------------*\
100* 102*
101\*-------------------------------------------------------------------------*/ 103\*-------------------------------------------------------------------------*/
102void socket_shutdown(p_socket ps, int how) { 104LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
103 socket_setblocking(ps); 105 socket_setblocking(ps);
104 shutdown(*ps, how); 106 shutdown(*ps, how);
105 socket_setnonblocking(ps); 107 socket_setnonblocking(ps);
@@ -108,7 +110,7 @@ void socket_shutdown(p_socket ps, int how) {
108/*-------------------------------------------------------------------------*\ 110/*-------------------------------------------------------------------------*\
109* Creates and sets up a socket 111* Creates and sets up a socket
110\*-------------------------------------------------------------------------*/ 112\*-------------------------------------------------------------------------*/
111int socket_create(p_socket ps, int domain, int type, int protocol) { 113LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) {
112 *ps = socket(domain, type, protocol); 114 *ps = socket(domain, type, protocol);
113 if (*ps != SOCKET_INVALID) return IO_DONE; 115 if (*ps != SOCKET_INVALID) return IO_DONE;
114 else return WSAGetLastError(); 116 else return WSAGetLastError();
@@ -117,7 +119,7 @@ int socket_create(p_socket ps, int domain, int type, int protocol) {
117/*-------------------------------------------------------------------------*\ 119/*-------------------------------------------------------------------------*\
118* Connects or returns error message 120* Connects or returns error message
119\*-------------------------------------------------------------------------*/ 121\*-------------------------------------------------------------------------*/
120int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { 122LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
121 int err; 123 int err;
122 /* don't call on closed socket */ 124 /* don't call on closed socket */
123 if (*ps == SOCKET_INVALID) return IO_CLOSED; 125 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -146,7 +148,7 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
146/*-------------------------------------------------------------------------*\ 148/*-------------------------------------------------------------------------*\
147* Binds or returns error message 149* Binds or returns error message
148\*-------------------------------------------------------------------------*/ 150\*-------------------------------------------------------------------------*/
149int socket_bind(p_socket ps, SA *addr, socklen_t len) { 151LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
150 int err = IO_DONE; 152 int err = IO_DONE;
151 socket_setblocking(ps); 153 socket_setblocking(ps);
152 if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); 154 if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
@@ -157,7 +159,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) {
157/*-------------------------------------------------------------------------*\ 159/*-------------------------------------------------------------------------*\
158* 160*
159\*-------------------------------------------------------------------------*/ 161\*-------------------------------------------------------------------------*/
160int socket_listen(p_socket ps, int backlog) { 162LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
161 int err = IO_DONE; 163 int err = IO_DONE;
162 socket_setblocking(ps); 164 socket_setblocking(ps);
163 if (listen(*ps, backlog) < 0) err = WSAGetLastError(); 165 if (listen(*ps, backlog) < 0) err = WSAGetLastError();
@@ -168,7 +170,7 @@ int socket_listen(p_socket ps, int backlog) {
168/*-------------------------------------------------------------------------*\ 170/*-------------------------------------------------------------------------*\
169* Accept with timeout 171* Accept with timeout
170\*-------------------------------------------------------------------------*/ 172\*-------------------------------------------------------------------------*/
171int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, 173LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
172 p_timeout tm) { 174 p_timeout tm) {
173 if (*ps == SOCKET_INVALID) return IO_CLOSED; 175 if (*ps == SOCKET_INVALID) return IO_CLOSED;
174 for ( ;; ) { 176 for ( ;; ) {
@@ -190,7 +192,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
190* this can take an awful lot of time and we will end up blocked. 192* this can take an awful lot of time and we will end up blocked.
191* Therefore, whoever calls this function should not pass a huge buffer. 193* Therefore, whoever calls this function should not pass a huge buffer.
192\*-------------------------------------------------------------------------*/ 194\*-------------------------------------------------------------------------*/
193int socket_send(p_socket ps, const char *data, size_t count, 195LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
194 size_t *sent, p_timeout tm) 196 size_t *sent, p_timeout tm)
195{ 197{
196 int err; 198 int err;
@@ -218,7 +220,7 @@ int socket_send(p_socket ps, const char *data, size_t count,
218/*-------------------------------------------------------------------------*\ 220/*-------------------------------------------------------------------------*\
219* Sendto with timeout 221* Sendto with timeout
220\*-------------------------------------------------------------------------*/ 222\*-------------------------------------------------------------------------*/
221int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, 223LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
222 SA *addr, socklen_t len, p_timeout tm) 224 SA *addr, socklen_t len, p_timeout tm)
223{ 225{
224 int err; 226 int err;
@@ -239,7 +241,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
239/*-------------------------------------------------------------------------*\ 241/*-------------------------------------------------------------------------*\
240* Receive with timeout 242* Receive with timeout
241\*-------------------------------------------------------------------------*/ 243\*-------------------------------------------------------------------------*/
242int socket_recv(p_socket ps, char *data, size_t count, size_t *got, 244LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
243 p_timeout tm) 245 p_timeout tm)
244{ 246{
245 int err, prev = IO_DONE; 247 int err, prev = IO_DONE;
@@ -268,7 +270,7 @@ int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
268/*-------------------------------------------------------------------------*\ 270/*-------------------------------------------------------------------------*\
269* Recvfrom with timeout 271* Recvfrom with timeout
270\*-------------------------------------------------------------------------*/ 272\*-------------------------------------------------------------------------*/
271int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, 273LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
272 SA *addr, socklen_t *len, p_timeout tm) 274 SA *addr, socklen_t *len, p_timeout tm)
273{ 275{
274 int err, prev = IO_DONE; 276 int err, prev = IO_DONE;
@@ -297,7 +299,7 @@ int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
297/*-------------------------------------------------------------------------*\ 299/*-------------------------------------------------------------------------*\
298* Put socket into blocking mode 300* Put socket into blocking mode
299\*-------------------------------------------------------------------------*/ 301\*-------------------------------------------------------------------------*/
300void socket_setblocking(p_socket ps) { 302LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
301 u_long argp = 0; 303 u_long argp = 0;
302 ioctlsocket(*ps, FIONBIO, &argp); 304 ioctlsocket(*ps, FIONBIO, &argp);
303} 305}
@@ -305,7 +307,7 @@ void socket_setblocking(p_socket ps) {
305/*-------------------------------------------------------------------------*\ 307/*-------------------------------------------------------------------------*\
306* Put socket into non-blocking mode 308* Put socket into non-blocking mode
307\*-------------------------------------------------------------------------*/ 309\*-------------------------------------------------------------------------*/
308void socket_setnonblocking(p_socket ps) { 310LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
309 u_long argp = 1; 311 u_long argp = 1;
310 ioctlsocket(*ps, FIONBIO, &argp); 312 ioctlsocket(*ps, FIONBIO, &argp);
311} 313}
@@ -313,13 +315,13 @@ void socket_setnonblocking(p_socket ps) {
313/*-------------------------------------------------------------------------*\ 315/*-------------------------------------------------------------------------*\
314* DNS helpers 316* DNS helpers
315\*-------------------------------------------------------------------------*/ 317\*-------------------------------------------------------------------------*/
316int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 318LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
317 *hp = gethostbyaddr(addr, len, AF_INET); 319 *hp = gethostbyaddr(addr, len, AF_INET);
318 if (*hp) return IO_DONE; 320 if (*hp) return IO_DONE;
319 else return WSAGetLastError(); 321 else return WSAGetLastError();
320} 322}
321 323
322int socket_gethostbyname(const char *addr, struct hostent **hp) { 324LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) {
323 *hp = gethostbyname(addr); 325 *hp = gethostbyname(addr);
324 if (*hp) return IO_DONE; 326 if (*hp) return IO_DONE;
325 else return WSAGetLastError(); 327 else return WSAGetLastError();
@@ -328,7 +330,7 @@ int socket_gethostbyname(const char *addr, struct hostent **hp) {
328/*-------------------------------------------------------------------------*\ 330/*-------------------------------------------------------------------------*\
329* Error translation functions 331* Error translation functions
330\*-------------------------------------------------------------------------*/ 332\*-------------------------------------------------------------------------*/
331const char *socket_hoststrerror(int err) { 333LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
332 if (err <= 0) return io_strerror(err); 334 if (err <= 0) return io_strerror(err);
333 switch (err) { 335 switch (err) {
334 case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; 336 case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@@ -336,7 +338,7 @@ const char *socket_hoststrerror(int err) {
336 } 338 }
337} 339}
338 340
339const char *socket_strerror(int err) { 341LUASOCKET_PRIVATE const char *socket_strerror(int err) {
340 if (err <= 0) return io_strerror(err); 342 if (err <= 0) return io_strerror(err);
341 switch (err) { 343 switch (err) {
342 case WSAEADDRINUSE: return PIE_ADDRINUSE; 344 case WSAEADDRINUSE: return PIE_ADDRINUSE;
@@ -350,12 +352,12 @@ const char *socket_strerror(int err) {
350 } 352 }
351} 353}
352 354
353const char *socket_ioerror(p_socket ps, int err) { 355LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) {
354 (void) ps; 356 (void) ps;
355 return socket_strerror(err); 357 return socket_strerror(err);
356} 358}
357 359
358static const char *wstrerror(int err) { 360LUASOCKET_PRIVATE static const char *wstrerror(int err) {
359 switch (err) { 361 switch (err) {
360 case WSAEINTR: return "Interrupted function call"; 362 case WSAEINTR: return "Interrupted function call";
361 case WSAEACCES: return PIE_ACCESS; // "Permission denied"; 363 case WSAEACCES: return PIE_ACCESS; // "Permission denied";
@@ -404,7 +406,7 @@ static const char *wstrerror(int err) {
404 } 406 }
405} 407}
406 408
407const char *socket_gaistrerror(int err) { 409LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
408 if (err == 0) return NULL; 410 if (err == 0) return NULL;
409 switch (err) { 411 switch (err) {
410 case EAI_AGAIN: return PIE_AGAIN; 412 case EAI_AGAIN: return PIE_AGAIN;