aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2019-03-11 00:08:41 -0300
committerGitHub <noreply@github.com>2019-03-11 00:08:41 -0300
commitc89a931cc31f219d51dd32a8a253d5ee3fbd31be (patch)
tree1fc604d7cbd6a42c801b92b145651d91c458dfa2 /src
parent2af4872a401cbd0c1255e19ee26620f0431dd9f5 (diff)
parent21514304be9e98a4386cb18542582068a59c5586 (diff)
downloadluasocket-c89a931cc31f219d51dd32a8a253d5ee3fbd31be.tar.gz
luasocket-c89a931cc31f219d51dd32a8a253d5ee3fbd31be.tar.bz2
luasocket-c89a931cc31f219d51dd32a8a253d5ee3fbd31be.zip
Merge pull request #271 from ewestbrook/pragmavisibility
Use visibility pragma around declarations instead of attributes on definitions
Diffstat (limited to 'src')
-rw-r--r--src/auxiliar.c26
-rw-r--r--src/auxiliar.h20
-rw-r--r--src/buffer.c19
-rw-r--r--src/buffer.h15
-rw-r--r--src/compat.c5
-rw-r--r--src/compat.h22
-rw-r--r--src/except.c10
-rw-r--r--src/except.h10
-rw-r--r--src/inet.c34
-rw-r--r--src/inet.h26
-rw-r--r--src/io.c7
-rw-r--r--src/io.h13
-rwxr-xr-xsrc/luasocket.c10
-rw-r--r--src/luasocket.h13
-rwxr-xr-xsrc/makefile1
-rwxr-xr-xsrc/mime.c8
-rw-r--r--src/mime.h1
-rw-r--r--src/options.c152
-rw-r--r--src/options.h102
-rw-r--r--src/select.c10
-rw-r--r--src/select.h8
-rw-r--r--src/serial.c7
-rw-r--r--src/socket.h55
-rw-r--r--src/tcp.c7
-rw-r--r--src/tcp.h10
-rw-r--r--src/timeout.c28
-rw-r--r--src/timeout.h17
-rw-r--r--src/udp.c6
-rw-r--r--src/udp.h10
-rw-r--r--src/unix.c3
-rw-r--r--src/unix.h1
-rw-r--r--src/unixdgram.c7
-rw-r--r--src/unixdgram.h8
-rw-r--r--src/unixstream.c6
-rw-r--r--src/unixstream.h8
-rw-r--r--src/usocket.c53
-rwxr-xr-xsrc/wsocket.c49
37 files changed, 418 insertions, 369 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c
index 0bd7927..93a66a0 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -7,13 +7,10 @@
7#include <string.h> 7#include <string.h>
8#include <stdio.h> 8#include <stdio.h>
9 9
10/*=========================================================================*\
11* Exported functions
12\*=========================================================================*/
13/*-------------------------------------------------------------------------*\ 10/*-------------------------------------------------------------------------*\
14* Initializes the module 11* Initializes the module
15\*-------------------------------------------------------------------------*/ 12\*-------------------------------------------------------------------------*/
16LUASOCKET_PRIVATE int auxiliar_open(lua_State *L) { 13int auxiliar_open(lua_State *L) {
17 (void) L; 14 (void) L;
18 return 0; 15 return 0;
19} 16}
@@ -22,7 +19,7 @@ LUASOCKET_PRIVATE int auxiliar_open(lua_State *L) {
22* Creates a new class with given methods 19* Creates a new class with given methods
23* Methods whose names start with __ are passed directly to the metatable. 20* Methods whose names start with __ are passed directly to the metatable.
24\*-------------------------------------------------------------------------*/ 21\*-------------------------------------------------------------------------*/
25LUASOCKET_PRIVATE void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) { 22void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) {
26 luaL_newmetatable(L, classname); /* mt */ 23 luaL_newmetatable(L, classname); /* mt */
27 /* create __index table to place methods */ 24 /* create __index table to place methods */
28 lua_pushstring(L, "__index"); /* mt,"__index" */ 25 lua_pushstring(L, "__index"); /* mt,"__index" */
@@ -45,7 +42,7 @@ LUASOCKET_PRIVATE void auxiliar_newclass(lua_State *L, const char *classname, lu
45/*-------------------------------------------------------------------------*\ 42/*-------------------------------------------------------------------------*\
46* Prints the value of a class in a nice way 43* Prints the value of a class in a nice way
47\*-------------------------------------------------------------------------*/ 44\*-------------------------------------------------------------------------*/
48LUASOCKET_PRIVATE int auxiliar_tostring(lua_State *L) { 45int auxiliar_tostring(lua_State *L) {
49 char buf[32]; 46 char buf[32];
50 if (!lua_getmetatable(L, 1)) goto error; 47 if (!lua_getmetatable(L, 1)) goto error;
51 lua_pushstring(L, "__index"); 48 lua_pushstring(L, "__index");
@@ -66,7 +63,7 @@ error:
66/*-------------------------------------------------------------------------*\ 63/*-------------------------------------------------------------------------*\
67* Insert class into group 64* Insert class into group
68\*-------------------------------------------------------------------------*/ 65\*-------------------------------------------------------------------------*/
69LUASOCKET_PRIVATE void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) { 66void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) {
70 luaL_getmetatable(L, classname); 67 luaL_getmetatable(L, classname);
71 lua_pushstring(L, groupname); 68 lua_pushstring(L, groupname);
72 lua_pushboolean(L, 1); 69 lua_pushboolean(L, 1);
@@ -77,7 +74,7 @@ LUASOCKET_PRIVATE void auxiliar_add2group(lua_State *L, const char *classname, c
77/*-------------------------------------------------------------------------*\ 74/*-------------------------------------------------------------------------*\
78* Make sure argument is a boolean 75* Make sure argument is a boolean
79\*-------------------------------------------------------------------------*/ 76\*-------------------------------------------------------------------------*/
80LUASOCKET_PRIVATE int auxiliar_checkboolean(lua_State *L, int objidx) { 77int auxiliar_checkboolean(lua_State *L, int objidx) {
81 if (!lua_isboolean(L, objidx)) 78 if (!lua_isboolean(L, objidx))
82 auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); 79 auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN));
83 return lua_toboolean(L, objidx); 80 return lua_toboolean(L, objidx);
@@ -87,7 +84,7 @@ LUASOCKET_PRIVATE int auxiliar_checkboolean(lua_State *L, int objidx) {
87* Return userdata pointer if object belongs to a given class, abort with 84* Return userdata pointer if object belongs to a given class, abort with
88* error otherwise 85* error otherwise
89\*-------------------------------------------------------------------------*/ 86\*-------------------------------------------------------------------------*/
90LUASOCKET_PRIVATE void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { 87void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) {
91 void *data = auxiliar_getclassudata(L, classname, objidx); 88 void *data = auxiliar_getclassudata(L, classname, objidx);
92 if (!data) { 89 if (!data) {
93 char msg[45]; 90 char msg[45];
@@ -101,7 +98,7 @@ LUASOCKET_PRIVATE void *auxiliar_checkclass(lua_State *L, const char *classname,
101* Return userdata pointer if object belongs to a given group, abort with 98* Return userdata pointer if object belongs to a given group, abort with
102* error otherwise 99* error otherwise
103\*-------------------------------------------------------------------------*/ 100\*-------------------------------------------------------------------------*/
104LUASOCKET_PRIVATE void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { 101void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) {
105 void *data = auxiliar_getgroupudata(L, groupname, objidx); 102 void *data = auxiliar_getgroupudata(L, groupname, objidx);
106 if (!data) { 103 if (!data) {
107 char msg[45]; 104 char msg[45];
@@ -114,7 +111,7 @@ LUASOCKET_PRIVATE void *auxiliar_checkgroup(lua_State *L, const char *groupname,
114/*-------------------------------------------------------------------------*\ 111/*-------------------------------------------------------------------------*\
115* Set object class 112* Set object class
116\*-------------------------------------------------------------------------*/ 113\*-------------------------------------------------------------------------*/
117LUASOCKET_PRIVATE void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { 114void auxiliar_setclass(lua_State *L, const char *classname, int objidx) {
118 luaL_getmetatable(L, classname); 115 luaL_getmetatable(L, classname);
119 if (objidx < 0) objidx--; 116 if (objidx < 0) objidx--;
120 lua_setmetatable(L, objidx); 117 lua_setmetatable(L, objidx);
@@ -124,7 +121,7 @@ LUASOCKET_PRIVATE void auxiliar_setclass(lua_State *L, const char *classname, in
124* Get a userdata pointer if object belongs to a given group. Return NULL 121* Get a userdata pointer if object belongs to a given group. Return NULL
125* otherwise 122* otherwise
126\*-------------------------------------------------------------------------*/ 123\*-------------------------------------------------------------------------*/
127LUASOCKET_PRIVATE void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { 124void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) {
128 if (!lua_getmetatable(L, objidx)) 125 if (!lua_getmetatable(L, objidx))
129 return NULL; 126 return NULL;
130 lua_pushstring(L, groupname); 127 lua_pushstring(L, groupname);
@@ -142,7 +139,7 @@ LUASOCKET_PRIVATE void *auxiliar_getgroupudata(lua_State *L, const char *groupna
142* Get a userdata pointer if object belongs to a given class. Return NULL 139* Get a userdata pointer if object belongs to a given class. Return NULL
143* otherwise 140* otherwise
144\*-------------------------------------------------------------------------*/ 141\*-------------------------------------------------------------------------*/
145LUASOCKET_PRIVATE void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { 142void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) {
146 return luaL_testudata(L, objidx, classname); 143 return luaL_testudata(L, objidx, classname);
147} 144}
148 145
@@ -150,9 +147,8 @@ LUASOCKET_PRIVATE void *auxiliar_getclassudata(lua_State *L, const char *classna
150* Throws error when argument does not have correct type. 147* 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. 148* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2.
152\*-------------------------------------------------------------------------*/ 149\*-------------------------------------------------------------------------*/
153LUASOCKET_PRIVATE int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { 150int auxiliar_typeerror (lua_State *L, int narg, const char *tname) {
154 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, 151 const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
155 luaL_typename(L, narg)); 152 luaL_typename(L, narg));
156 return luaL_argerror(L, narg, msg); 153 return luaL_argerror(L, narg, msg);
157} 154}
158
diff --git a/src/auxiliar.h b/src/auxiliar.h
index 65511d4..e8c3ead 100644
--- a/src/auxiliar.h
+++ b/src/auxiliar.h
@@ -29,20 +29,26 @@
29* reverse mapping are done using lauxlib. 29* reverse mapping are done using lauxlib.
30\*=========================================================================*/ 30\*=========================================================================*/
31 31
32#include "lua.h" 32#include "luasocket.h"
33#include "lauxlib.h" 33
34#include "compat.h" 34#ifndef _WIN32
35#pragma GCC visibility push(hidden)
36#endif
35 37
36int auxiliar_open(lua_State *L); 38int auxiliar_open(lua_State *L);
37void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); 39void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func);
40int auxiliar_tostring(lua_State *L);
38void auxiliar_add2group(lua_State *L, const char *classname, const char *group); 41void auxiliar_add2group(lua_State *L, const char *classname, const char *group);
39void auxiliar_setclass(lua_State *L, const char *classname, int objidx); 42int auxiliar_checkboolean(lua_State *L, int objidx);
40void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx); 43void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx);
41void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx); 44void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx);
42void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx); 45void auxiliar_setclass(lua_State *L, const char *classname, int objidx);
43void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx); 46void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx);
44int auxiliar_checkboolean(lua_State *L, int objidx); 47void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx);
45int auxiliar_tostring(lua_State *L);
46int auxiliar_typeerror(lua_State *L, int narg, const char *tname); 48int auxiliar_typeerror(lua_State *L, int narg, const char *tname);
47 49
50#ifndef _WIN32
51#pragma GCC visibility pop
52#endif
53
48#endif /* AUXILIAR_H */ 54#endif /* AUXILIAR_H */
diff --git a/src/buffer.c b/src/buffer.c
index 357730a..ac5c531 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -3,11 +3,6 @@
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "buffer.h" 6#include "buffer.h"
12 7
13/*=========================================================================*\ 8/*=========================================================================*\
@@ -34,7 +29,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
34/*-------------------------------------------------------------------------*\ 29/*-------------------------------------------------------------------------*\
35* Initializes module 30* Initializes module
36\*-------------------------------------------------------------------------*/ 31\*-------------------------------------------------------------------------*/
37LUASOCKET_PRIVATE int buffer_open(lua_State *L) { 32int buffer_open(lua_State *L) {
38 (void) L; 33 (void) L;
39 return 0; 34 return 0;
40} 35}
@@ -42,7 +37,7 @@ LUASOCKET_PRIVATE int buffer_open(lua_State *L) {
42/*-------------------------------------------------------------------------*\ 37/*-------------------------------------------------------------------------*\
43* Initializes C structure 38* Initializes C structure
44\*-------------------------------------------------------------------------*/ 39\*-------------------------------------------------------------------------*/
45LUASOCKET_PRIVATE void buffer_init(p_buffer buf, p_io io, p_timeout tm) { 40void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
46 buf->first = buf->last = 0; 41 buf->first = buf->last = 0;
47 buf->io = io; 42 buf->io = io;
48 buf->tm = tm; 43 buf->tm = tm;
@@ -53,7 +48,7 @@ LUASOCKET_PRIVATE void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
53/*-------------------------------------------------------------------------*\ 48/*-------------------------------------------------------------------------*\
54* object:getstats() interface 49* object:getstats() interface
55\*-------------------------------------------------------------------------*/ 50\*-------------------------------------------------------------------------*/
56LUASOCKET_PRIVATE int buffer_meth_getstats(lua_State *L, p_buffer buf) { 51int buffer_meth_getstats(lua_State *L, p_buffer buf) {
57 lua_pushnumber(L, (lua_Number) buf->received); 52 lua_pushnumber(L, (lua_Number) buf->received);
58 lua_pushnumber(L, (lua_Number) buf->sent); 53 lua_pushnumber(L, (lua_Number) buf->sent);
59 lua_pushnumber(L, timeout_gettime() - buf->birthday); 54 lua_pushnumber(L, timeout_gettime() - buf->birthday);
@@ -63,7 +58,7 @@ LUASOCKET_PRIVATE int buffer_meth_getstats(lua_State *L, p_buffer buf) {
63/*-------------------------------------------------------------------------*\ 58/*-------------------------------------------------------------------------*\
64* object:setstats() interface 59* object:setstats() interface
65\*-------------------------------------------------------------------------*/ 60\*-------------------------------------------------------------------------*/
66LUASOCKET_PRIVATE int buffer_meth_setstats(lua_State *L, p_buffer buf) { 61int buffer_meth_setstats(lua_State *L, p_buffer buf) {
67 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); 62 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
68 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); 63 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
69 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); 64 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
@@ -74,7 +69,7 @@ LUASOCKET_PRIVATE int buffer_meth_setstats(lua_State *L, p_buffer buf) {
74/*-------------------------------------------------------------------------*\ 69/*-------------------------------------------------------------------------*\
75* object:send() interface 70* object:send() interface
76\*-------------------------------------------------------------------------*/ 71\*-------------------------------------------------------------------------*/
77LUASOCKET_PRIVATE int buffer_meth_send(lua_State *L, p_buffer buf) { 72int buffer_meth_send(lua_State *L, p_buffer buf) {
78 int top = lua_gettop(L); 73 int top = lua_gettop(L);
79 int err = IO_DONE; 74 int err = IO_DONE;
80 size_t size = 0, sent = 0; 75 size_t size = 0, sent = 0;
@@ -107,7 +102,7 @@ LUASOCKET_PRIVATE int buffer_meth_send(lua_State *L, p_buffer buf) {
107/*-------------------------------------------------------------------------*\ 102/*-------------------------------------------------------------------------*\
108* object:receive() interface 103* object:receive() interface
109\*-------------------------------------------------------------------------*/ 104\*-------------------------------------------------------------------------*/
110LUASOCKET_PRIVATE int buffer_meth_receive(lua_State *L, p_buffer buf) { 105int buffer_meth_receive(lua_State *L, p_buffer buf) {
111 int err = IO_DONE, top = lua_gettop(L); 106 int err = IO_DONE, top = lua_gettop(L);
112 luaL_Buffer b; 107 luaL_Buffer b;
113 size_t size; 108 size_t size;
@@ -156,7 +151,7 @@ LUASOCKET_PRIVATE int buffer_meth_receive(lua_State *L, p_buffer buf) {
156/*-------------------------------------------------------------------------*\ 151/*-------------------------------------------------------------------------*\
157* Determines if there is any data in the read buffer 152* Determines if there is any data in the read buffer
158\*-------------------------------------------------------------------------*/ 153\*-------------------------------------------------------------------------*/
159LUASOCKET_PRIVATE int buffer_isempty(p_buffer buf) { 154int buffer_isempty(p_buffer buf) {
160 return buf->first >= buf->last; 155 return buf->first >= buf->last;
161} 156}
162 157
diff --git a/src/buffer.h b/src/buffer.h
index 1281bb3..a0901fc 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -15,8 +15,7 @@
15* The module is built on top of the I/O abstraction defined in io.h and the 15* The module is built on top of the I/O abstraction defined in io.h and the
16* timeout management is done with the timeout.h interface. 16* timeout management is done with the timeout.h interface.
17\*=========================================================================*/ 17\*=========================================================================*/
18#include "lua.h" 18#include "luasocket.h"
19
20#include "io.h" 19#include "io.h"
21#include "timeout.h" 20#include "timeout.h"
22 21
@@ -34,12 +33,20 @@ typedef struct t_buffer_ {
34} t_buffer; 33} t_buffer;
35typedef t_buffer *p_buffer; 34typedef t_buffer *p_buffer;
36 35
36#ifndef _WIN32
37#pragma GCC visibility push(hidden)
38#endif
39
37int buffer_open(lua_State *L); 40int buffer_open(lua_State *L);
38void buffer_init(p_buffer buf, p_io io, p_timeout tm); 41void buffer_init(p_buffer buf, p_io io, p_timeout tm);
39int buffer_meth_send(lua_State *L, p_buffer buf);
40int buffer_meth_receive(lua_State *L, p_buffer buf);
41int buffer_meth_getstats(lua_State *L, p_buffer buf); 42int buffer_meth_getstats(lua_State *L, p_buffer buf);
42int buffer_meth_setstats(lua_State *L, p_buffer buf); 43int buffer_meth_setstats(lua_State *L, p_buffer buf);
44int buffer_meth_send(lua_State *L, p_buffer buf);
45int buffer_meth_receive(lua_State *L, p_buffer buf);
43int buffer_isempty(p_buffer buf); 46int buffer_isempty(p_buffer buf);
44 47
48#ifndef _WIN32
49#pragma GCC visibility pop
50#endif
51
45#endif /* BUF_H */ 52#endif /* BUF_H */
diff --git a/src/compat.c b/src/compat.c
index 1290f70..34ffdaf 100644
--- a/src/compat.c
+++ b/src/compat.c
@@ -2,10 +2,11 @@
2#include "compat.h" 2#include "compat.h"
3 3
4#if LUA_VERSION_NUM==501 4#if LUA_VERSION_NUM==501
5
5/* 6/*
6** Adapted from Lua 5.2 7** Adapted from Lua 5.2
7*/ 8*/
8LUASOCKET_PRIVATE void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { 9void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
9 luaL_checkstack(L, nup+1, "too many upvalues"); 10 luaL_checkstack(L, nup+1, "too many upvalues");
10 for (; l->name != NULL; l++) { /* fill the table with given functions */ 11 for (; l->name != NULL; l++) { /* fill the table with given functions */
11 int i; 12 int i;
@@ -21,7 +22,7 @@ LUASOCKET_PRIVATE void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup)
21/* 22/*
22** Duplicated from Lua 5.2 23** Duplicated from Lua 5.2
23*/ 24*/
24LUASOCKET_PRIVATE void *luaL_testudata (lua_State *L, int ud, const char *tname) { 25void *luasocket_testudata (lua_State *L, int ud, const char *tname) {
25 void *p = lua_touserdata(L, ud); 26 void *p = lua_touserdata(L, ud);
26 if (p != NULL) { /* value is a userdata? */ 27 if (p != NULL) { /* value is a userdata? */
27 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ 28 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
diff --git a/src/compat.h b/src/compat.h
index 49e83f9..fa2d7d7 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -1,14 +1,22 @@
1#ifndef COMPAT_H 1#ifndef COMPAT_H
2#define COMPAT_H 2#define COMPAT_H
3 3
4#include "lua.h"
5#include "lauxlib.h"
6
7#if LUA_VERSION_NUM==501 4#if LUA_VERSION_NUM==501
8#define luaL_setfuncs socket_setfuncs 5
9#define luaL_testudata socket_testudata 6#ifndef _WIN32
10void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); 7#pragma GCC visibility push(hidden)
11void *luaL_testudata ( lua_State *L, int arg, const char *tname); 8#endif
9
10void luasocket_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
11void *luasocket_testudata ( lua_State *L, int arg, const char *tname);
12
13#ifndef _WIN32
14#pragma GCC visibility pop
15#endif
16
17#define luaL_setfuncs luasocket_setfuncs
18#define luaL_testudata luasocket_testudata
19
12#endif 20#endif
13 21
14#endif 22#endif
diff --git a/src/except.c b/src/except.c
index dab70e7..9c3317f 100644
--- a/src/except.c
+++ b/src/except.c
@@ -3,14 +3,8 @@
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6
7#include <stdio.h>
8
9#include "lua.h"
10#include "lauxlib.h"
11#include "compat.h"
12
13#include "except.h" 6#include "except.h"
7#include <stdio.h>
14 8
15#if LUA_VERSION_NUM < 502 9#if LUA_VERSION_NUM < 502
16#define lua_pcallk(L, na, nr, err, ctx, cont) \ 10#define lua_pcallk(L, na, nr, err, ctx, cont) \
@@ -126,7 +120,7 @@ static int global_protect(lua_State *L) {
126/*-------------------------------------------------------------------------*\ 120/*-------------------------------------------------------------------------*\
127* Init module 121* Init module
128\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
129LUASOCKET_PRIVATE int except_open(lua_State *L) { 123int except_open(lua_State *L) {
130 lua_newtable(L); /* metatable for wrapped exceptions */ 124 lua_newtable(L); /* metatable for wrapped exceptions */
131 lua_pushboolean(L, 0); 125 lua_pushboolean(L, 0);
132 lua_setfield(L, -2, "__metatable"); 126 lua_setfield(L, -2, "__metatable");
diff --git a/src/except.h b/src/except.h
index 2497c05..71c31fd 100644
--- a/src/except.h
+++ b/src/except.h
@@ -31,8 +31,16 @@
31* exceptions on error, but that don't interrupt the user script. 31* exceptions on error, but that don't interrupt the user script.
32\*=========================================================================*/ 32\*=========================================================================*/
33 33
34#include "lua.h" 34#include "luasocket.h"
35
36#ifndef _WIN32
37#pragma GCC visibility push(hidden)
38#endif
35 39
36int except_open(lua_State *L); 40int except_open(lua_State *L);
37 41
42#ifndef _WIN32
43#pragma GCC visibility pop
44#endif
45
38#endif 46#endif
diff --git a/src/inet.c b/src/inet.c
index bed8a7c..ec73fea 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -3,17 +3,12 @@
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6#include "inet.h"
6 7
7#include <stdio.h> 8#include <stdio.h>
8#include <stdlib.h> 9#include <stdlib.h>
9#include <string.h> 10#include <string.h>
10 11
11#include "lua.h"
12#include "lauxlib.h"
13#include "compat.h"
14
15#include "inet.h"
16
17/*=========================================================================*\ 12/*=========================================================================*\
18* Internal function prototypes. 13* Internal function prototypes.
19\*=========================================================================*/ 14\*=========================================================================*/
@@ -34,13 +29,10 @@ static luaL_Reg func[] = {
34 { NULL, NULL} 29 { NULL, NULL}
35}; 30};
36 31
37/*=========================================================================*\
38* Exported functions
39\*=========================================================================*/
40/*-------------------------------------------------------------------------*\ 32/*-------------------------------------------------------------------------*\
41* Initializes module 33* Initializes module
42\*-------------------------------------------------------------------------*/ 34\*-------------------------------------------------------------------------*/
43LUASOCKET_PRIVATE int inet_open(lua_State *L) 35int inet_open(lua_State *L)
44{ 36{
45 lua_pushstring(L, "dns"); 37 lua_pushstring(L, "dns");
46 lua_newtable(L); 38 lua_newtable(L);
@@ -145,7 +137,7 @@ static int inet_global_toip(lua_State *L)
145 return 2; 137 return 2;
146} 138}
147 139
148LUASOCKET_PRIVATE int inet_optfamily(lua_State* L, int narg, const char* def) 140int inet_optfamily(lua_State* L, int narg, const char* def)
149{ 141{
150 static const char* optname[] = { "unspec", "inet", "inet6", NULL }; 142 static const char* optname[] = { "unspec", "inet", "inet6", NULL };
151 static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 }; 143 static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
@@ -153,7 +145,7 @@ LUASOCKET_PRIVATE int inet_optfamily(lua_State* L, int narg, const char* def)
153 return optvalue[luaL_checkoption(L, narg, def, optname)]; 145 return optvalue[luaL_checkoption(L, narg, def, optname)];
154} 146}
155 147
156LUASOCKET_PRIVATE int inet_optsocktype(lua_State* L, int narg, const char* def) 148int inet_optsocktype(lua_State* L, int narg, const char* def)
157{ 149{
158 static const char* optname[] = { "stream", "dgram", NULL }; 150 static const char* optname[] = { "stream", "dgram", NULL };
159 static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 }; 151 static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 };
@@ -244,7 +236,7 @@ static int inet_global_gethostname(lua_State *L)
244/*-------------------------------------------------------------------------*\ 236/*-------------------------------------------------------------------------*\
245* Retrieves socket peer name 237* Retrieves socket peer name
246\*-------------------------------------------------------------------------*/ 238\*-------------------------------------------------------------------------*/
247LUASOCKET_PRIVATE int inet_meth_getpeername(lua_State *L, p_socket ps, int family) 239int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
248{ 240{
249 int err; 241 int err;
250 struct sockaddr_storage peer; 242 struct sockaddr_storage peer;
@@ -278,7 +270,7 @@ LUASOCKET_PRIVATE int inet_meth_getpeername(lua_State *L, p_socket ps, int famil
278/*-------------------------------------------------------------------------*\ 270/*-------------------------------------------------------------------------*\
279* Retrieves socket local name 271* Retrieves socket local name
280\*-------------------------------------------------------------------------*/ 272\*-------------------------------------------------------------------------*/
281LUASOCKET_PRIVATE int inet_meth_getsockname(lua_State *L, p_socket ps, int family) 273int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
282{ 274{
283 int err; 275 int err;
284 struct sockaddr_storage peer; 276 struct sockaddr_storage peer;
@@ -354,7 +346,7 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
354/*-------------------------------------------------------------------------*\ 346/*-------------------------------------------------------------------------*\
355* Tries to create a new inet socket 347* Tries to create a new inet socket
356\*-------------------------------------------------------------------------*/ 348\*-------------------------------------------------------------------------*/
357LUASOCKET_PRIVATE const char *inet_trycreate(p_socket ps, int family, int type, int protocol) { 349const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
358 const char *err = socket_strerror(socket_create(ps, family, type, protocol)); 350 const char *err = socket_strerror(socket_create(ps, family, type, protocol));
359 if (err == NULL && family == AF_INET6) { 351 if (err == NULL && family == AF_INET6) {
360 int yes = 1; 352 int yes = 1;
@@ -366,7 +358,7 @@ LUASOCKET_PRIVATE const char *inet_trycreate(p_socket ps, int family, int type,
366/*-------------------------------------------------------------------------*\ 358/*-------------------------------------------------------------------------*\
367* "Disconnects" a DGRAM socket 359* "Disconnects" a DGRAM socket
368\*-------------------------------------------------------------------------*/ 360\*-------------------------------------------------------------------------*/
369LUASOCKET_PRIVATE const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) 361const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
370{ 362{
371 switch (family) { 363 switch (family) {
372 case AF_INET: { 364 case AF_INET: {
@@ -393,7 +385,7 @@ LUASOCKET_PRIVATE const char *inet_trydisconnect(p_socket ps, int family, p_time
393/*-------------------------------------------------------------------------*\ 385/*-------------------------------------------------------------------------*\
394* Tries to connect to remote address (address, port) 386* Tries to connect to remote address (address, port)
395\*-------------------------------------------------------------------------*/ 387\*-------------------------------------------------------------------------*/
396LUASOCKET_PRIVATE const char *inet_tryconnect(p_socket ps, int *family, const char *address, 388const char *inet_tryconnect(p_socket ps, int *family, const char *address,
397 const char *serv, p_timeout tm, struct addrinfo *connecthints) 389 const char *serv, p_timeout tm, struct addrinfo *connecthints)
398{ 390{
399 struct addrinfo *iterator = NULL, *resolved = NULL; 391 struct addrinfo *iterator = NULL, *resolved = NULL;
@@ -439,7 +431,7 @@ LUASOCKET_PRIVATE const char *inet_tryconnect(p_socket ps, int *family, const ch
439/*-------------------------------------------------------------------------*\ 431/*-------------------------------------------------------------------------*\
440* Tries to accept a socket 432* Tries to accept a socket
441\*-------------------------------------------------------------------------*/ 433\*-------------------------------------------------------------------------*/
442LUASOCKET_PRIVATE const char *inet_tryaccept(p_socket server, int family, p_socket client, 434const char *inet_tryaccept(p_socket server, int family, p_socket client,
443 p_timeout tm) { 435 p_timeout tm) {
444 socklen_t len; 436 socklen_t len;
445 t_sockaddr_storage addr; 437 t_sockaddr_storage addr;
@@ -455,7 +447,7 @@ LUASOCKET_PRIVATE const char *inet_tryaccept(p_socket server, int family, p_sock
455/*-------------------------------------------------------------------------*\ 447/*-------------------------------------------------------------------------*\
456* Tries to bind socket to (address, port) 448* Tries to bind socket to (address, port)
457\*-------------------------------------------------------------------------*/ 449\*-------------------------------------------------------------------------*/
458LUASOCKET_PRIVATE const char *inet_trybind(p_socket ps, int *family, const char *address, 450const char *inet_trybind(p_socket ps, int *family, const char *address,
459 const char *serv, struct addrinfo *bindhints) { 451 const char *serv, struct addrinfo *bindhints) {
460 struct addrinfo *iterator = NULL, *resolved = NULL; 452 struct addrinfo *iterator = NULL, *resolved = NULL;
461 const char *err = NULL; 453 const char *err = NULL;
@@ -499,7 +491,7 @@ LUASOCKET_PRIVATE const char *inet_trybind(p_socket ps, int *family, const char
499* Some systems do not provide these so that we provide our own. 491* Some systems do not provide these so that we provide our own.
500\*-------------------------------------------------------------------------*/ 492\*-------------------------------------------------------------------------*/
501#ifdef LUASOCKET_INET_ATON 493#ifdef LUASOCKET_INET_ATON
502LUASOCKET_PRIVATE int inet_aton(const char *cp, struct in_addr *inp) 494int inet_aton(const char *cp, struct in_addr *inp)
503{ 495{
504 unsigned int a = 0, b = 0, c = 0, d = 0; 496 unsigned int a = 0, b = 0, c = 0, d = 0;
505 int n = 0, r; 497 int n = 0, r;
@@ -521,7 +513,7 @@ LUASOCKET_PRIVATE int inet_aton(const char *cp, struct in_addr *inp)
521#endif 513#endif
522 514
523#ifdef LUASOCKET_INET_PTON 515#ifdef LUASOCKET_INET_PTON
524LUASOCKET_PRIVATE int inet_pton(int af, const char *src, void *dst) 516int inet_pton(int af, const char *src, void *dst)
525{ 517{
526 struct addrinfo hints, *res; 518 struct addrinfo hints, *res;
527 int ret = 1; 519 int ret = 1;
diff --git a/src/inet.h b/src/inet.h
index feb3541..5618b61 100644
--- a/src/inet.h
+++ b/src/inet.h
@@ -14,7 +14,7 @@
14* 14*
15* The Lua functions toip and tohostname are also implemented here. 15* The Lua functions toip and tohostname are also implemented here.
16\*=========================================================================*/ 16\*=========================================================================*/
17#include "lua.h" 17#include "luasocket.h"
18#include "socket.h" 18#include "socket.h"
19#include "timeout.h" 19#include "timeout.h"
20 20
@@ -22,21 +22,23 @@
22#define LUASOCKET_INET_ATON 22#define LUASOCKET_INET_ATON
23#endif 23#endif
24 24
25#ifndef _WIN32
26#pragma GCC visibility push(hidden)
27#endif
28
25int inet_open(lua_State *L); 29int inet_open(lua_State *L);
26 30
27const char *inet_trycreate(p_socket ps, int family, int type, int protocol); 31int inet_optfamily(lua_State* L, int narg, const char* def);
28const char *inet_tryconnect(p_socket ps, int *family, const char *address, 32int inet_optsocktype(lua_State* L, int narg, const char* def);
29 const char *serv, p_timeout tm, struct addrinfo *connecthints);
30const char *inet_trybind(p_socket ps, int *family, const char *address,
31 const char *serv, struct addrinfo *bindhints);
32const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
33const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);
34 33
35int inet_meth_getpeername(lua_State *L, p_socket ps, int family); 34int inet_meth_getpeername(lua_State *L, p_socket ps, int family);
36int inet_meth_getsockname(lua_State *L, p_socket ps, int family); 35int inet_meth_getsockname(lua_State *L, p_socket ps, int family);
37 36
38int inet_optfamily(lua_State* L, int narg, const char* def); 37const char *inet_trycreate(p_socket ps, int family, int type, int protocol);
39int inet_optsocktype(lua_State* L, int narg, const char* def); 38const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
39const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *serv, p_timeout tm, struct addrinfo *connecthints);
40const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);
41const char *inet_trybind(p_socket ps, int *family, const char *address, const char *serv, struct addrinfo *bindhints);
40 42
41#ifdef LUASOCKET_INET_ATON 43#ifdef LUASOCKET_INET_ATON
42int inet_aton(const char *cp, struct in_addr *inp); 44int inet_aton(const char *cp, struct in_addr *inp);
@@ -47,4 +49,8 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
47int inet_pton(int af, const char *src, void *dst); 49int inet_pton(int af, const char *src, void *dst);
48#endif 50#endif
49 51
52#ifndef _WIN32
53#pragma GCC visibility pop
54#endif
55
50#endif /* INET_H */ 56#endif /* INET_H */
diff --git a/src/io.c b/src/io.c
index f1a2b9d..5ad4b3a 100644
--- a/src/io.c
+++ b/src/io.c
@@ -5,13 +5,10 @@
5#include "luasocket.h" 5#include "luasocket.h"
6#include "io.h" 6#include "io.h"
7 7
8/*=========================================================================*\
9* Exported functions
10\*=========================================================================*/
11/*-------------------------------------------------------------------------*\ 8/*-------------------------------------------------------------------------*\
12* Initializes C structure 9* Initializes C structure
13\*-------------------------------------------------------------------------*/ 10\*-------------------------------------------------------------------------*/
14LUASOCKET_PRIVATE void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) { 11void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) {
15 io->send = send; 12 io->send = send;
16 io->recv = recv; 13 io->recv = recv;
17 io->error = error; 14 io->error = error;
@@ -21,7 +18,7 @@ LUASOCKET_PRIVATE void io_init(p_io io, p_send send, p_recv recv, p_error error,
21/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
22* I/O error strings 19* I/O error strings
23\*-------------------------------------------------------------------------*/ 20\*-------------------------------------------------------------------------*/
24LUASOCKET_PRIVATE const char *io_strerror(int err) { 21const char *io_strerror(int err) {
25 switch (err) { 22 switch (err) {
26 case IO_DONE: return NULL; 23 case IO_DONE: return NULL;
27 case IO_CLOSED: return "closed"; 24 case IO_CLOSED: return "closed";
diff --git a/src/io.h b/src/io.h
index 8cca08a..b8a54df 100644
--- a/src/io.h
+++ b/src/io.h
@@ -12,9 +12,7 @@
12* The module socket.h implements this interface, and thus the module tcp.h 12* The module socket.h implements this interface, and thus the module tcp.h
13* is very simple. 13* is very simple.
14\*=========================================================================*/ 14\*=========================================================================*/
15#include <stdio.h> 15#include "luasocket.h"
16#include "lua.h"
17
18#include "timeout.h" 16#include "timeout.h"
19 17
20/* IO error codes */ 18/* IO error codes */
@@ -58,8 +56,15 @@ typedef struct t_io_ {
58} t_io; 56} t_io;
59typedef t_io *p_io; 57typedef t_io *p_io;
60 58
59#ifndef _WIN32
60#pragma GCC visibility push(hidden)
61#endif
62
61void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx); 63void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
62const char *io_strerror(int err); 64const char *io_strerror(int err);
63 65
64#endif /* IO_H */ 66#ifndef _WIN32
67#pragma GCC visibility pop
68#endif
65 69
70#endif /* IO_H */
diff --git a/src/luasocket.c b/src/luasocket.c
index d2752a7..0fd99f7 100755
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -12,16 +12,6 @@
12* standard Lua read and write functions. 12* standard Lua read and write functions.
13\*=========================================================================*/ 13\*=========================================================================*/
14 14
15/*=========================================================================*\
16* Standard include files
17\*=========================================================================*/
18#include "lua.h"
19#include "lauxlib.h"
20#include "compat.h"
21
22/*=========================================================================*\
23* LuaSocket includes
24\*=========================================================================*/
25#include "luasocket.h" 15#include "luasocket.h"
26#include "auxiliar.h" 16#include "auxiliar.h"
27#include "except.h" 17#include "except.h"
diff --git a/src/luasocket.h b/src/luasocket.h
index 0121a15..d22b1be 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -6,9 +6,8 @@
6* Diego Nehab 6* Diego Nehab
7* 9/11/1999 7* 9/11/1999
8\*=========================================================================*/ 8\*=========================================================================*/
9#include "lua.h"
10 9
11/*-------------------------------------------------------------------------*\ 10/*-------------------------------------------------------------------------* \
12* Current socket library version 11* Current socket library version
13\*-------------------------------------------------------------------------*/ 12\*-------------------------------------------------------------------------*/
14#define LUASOCKET_VERSION "LuaSocket 3.0-rc1" 13#define LUASOCKET_VERSION "LuaSocket 3.0-rc1"
@@ -25,13 +24,9 @@
25#endif 24#endif
26#endif 25#endif
27 26
28#ifndef LUASOCKET_PRIVATE 27#include "lua.h"
29#ifdef _WIN32 28#include "lauxlib.h"
30#define LUASOCKET_PRIVATE 29#include "compat.h"
31#else
32#define LUASOCKET_PRIVATE __attribute__ ((visibility ("hidden")))
33#endif
34#endif
35 30
36/*-------------------------------------------------------------------------*\ 31/*-------------------------------------------------------------------------*\
37* Initializes the library. 32* Initializes the library.
diff --git a/src/makefile b/src/makefile
index b9e2f93..5a9ba97 100755
--- a/src/makefile
+++ b/src/makefile
@@ -336,6 +336,7 @@ UNIX_OBJS=\
336# 336#
337SERIAL_OBJS=\ 337SERIAL_OBJS=\
338 buffer.$(O) \ 338 buffer.$(O) \
339 compat.$(O) \
339 auxiliar.$(O) \ 340 auxiliar.$(O) \
340 options.$(O) \ 341 options.$(O) \
341 timeout.$(O) \ 342 timeout.$(O) \
diff --git a/src/mime.c b/src/mime.c
index 0e9863b..6c210e4 100755
--- a/src/mime.c
+++ b/src/mime.c
@@ -2,15 +2,11 @@
2* MIME support functions 2* MIME support functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h"
6#include "mime.h"
5#include <string.h> 7#include <string.h>
6#include <ctype.h> 8#include <ctype.h>
7 9
8#include "lua.h"
9#include "lauxlib.h"
10#include "compat.h"
11
12#include "mime.h"
13
14/*=========================================================================*\ 10/*=========================================================================*\
15* Don't want to trust escape character constants 11* Don't want to trust escape character constants
16\*=========================================================================*/ 12\*=========================================================================*/
diff --git a/src/mime.h b/src/mime.h
index e57fc9c..4d938f4 100644
--- a/src/mime.h
+++ b/src/mime.h
@@ -9,7 +9,6 @@
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 "luasocket.h"
12#include "lua.h"
13 12
14/*-------------------------------------------------------------------------*\ 13/*-------------------------------------------------------------------------*\
15* Current MIME library version 14* Current MIME library version
diff --git a/src/options.c b/src/options.c
index b0bacbf..06ab58d 100644
--- a/src/options.c
+++ b/src/options.c
@@ -3,7 +3,6 @@
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6#include "lauxlib.h"
7#include "auxiliar.h" 6#include "auxiliar.h"
8#include "options.h" 7#include "options.h"
9#include "inet.h" 8#include "inet.h"
@@ -29,7 +28,7 @@ static int opt_get(lua_State *L, p_socket ps, int level, int name,
29/*-------------------------------------------------------------------------*\ 28/*-------------------------------------------------------------------------*\
30* Calls appropriate option handler 29* Calls appropriate option handler
31\*-------------------------------------------------------------------------*/ 30\*-------------------------------------------------------------------------*/
32LUASOCKET_PRIVATE int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) 31int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
33{ 32{
34 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 33 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
35 while (opt->name && strcmp(name, opt->name)) 34 while (opt->name && strcmp(name, opt->name))
@@ -42,7 +41,7 @@ LUASOCKET_PRIVATE int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
42 return opt->func(L, ps); 41 return opt->func(L, ps);
43} 42}
44 43
45LUASOCKET_PRIVATE int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) 44int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
46{ 45{
47 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ 46 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
48 while (opt->name && strcmp(name, opt->name)) 47 while (opt->name && strcmp(name, opt->name))
@@ -55,166 +54,188 @@ LUASOCKET_PRIVATE int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
55 return opt->func(L, ps); 54 return opt->func(L, ps);
56} 55}
57 56
57// -------------------------------------------------------
58/* enables reuse of local address */ 58/* enables reuse of local address */
59LUASOCKET_PRIVATE int opt_set_reuseaddr(lua_State *L, p_socket ps) 59int opt_set_reuseaddr(lua_State *L, p_socket ps)
60{ 60{
61 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 61 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
62} 62}
63 63
64LUASOCKET_PRIVATE int opt_get_reuseaddr(lua_State *L, p_socket ps) 64int opt_get_reuseaddr(lua_State *L, p_socket ps)
65{ 65{
66 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 66 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
67} 67}
68 68
69// -------------------------------------------------------
69/* enables reuse of local port */ 70/* enables reuse of local port */
70LUASOCKET_PRIVATE int opt_set_reuseport(lua_State *L, p_socket ps) 71int opt_set_reuseport(lua_State *L, p_socket ps)
71{ 72{
72 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 73 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
73} 74}
74 75
75LUASOCKET_PRIVATE int opt_get_reuseport(lua_State *L, p_socket ps) 76int opt_get_reuseport(lua_State *L, p_socket ps)
76{ 77{
77 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); 78 return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
78} 79}
79 80
80/* disables the Naggle algorithm */ 81// -------------------------------------------------------
81LUASOCKET_PRIVATE int opt_set_tcp_nodelay(lua_State *L, p_socket ps) 82/* disables the Nagle algorithm */
83int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
82{ 84{
83 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 85 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
84} 86}
85 87
86LUASOCKET_PRIVATE int opt_get_tcp_nodelay(lua_State *L, p_socket ps) 88int opt_get_tcp_nodelay(lua_State *L, p_socket ps)
87{ 89{
88 return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 90 return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
89} 91}
90 92
93// -------------------------------------------------------
91#ifdef TCP_KEEPIDLE 94#ifdef TCP_KEEPIDLE
92LUASOCKET_PRIVATE int opt_get_tcp_keepidle(lua_State *L, p_socket ps) 95
96int opt_get_tcp_keepidle(lua_State *L, p_socket ps)
93{ 97{
94 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE); 98 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
95} 99}
96 100
97LUASOCKET_PRIVATE int opt_set_tcp_keepidle(lua_State *L, p_socket ps) 101int opt_set_tcp_keepidle(lua_State *L, p_socket ps)
98{ 102{
99 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE); 103 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPIDLE);
100} 104}
105
101#endif 106#endif
102 107
108// -------------------------------------------------------
103#ifdef TCP_KEEPCNT 109#ifdef TCP_KEEPCNT
104LUASOCKET_PRIVATE int opt_get_tcp_keepcnt(lua_State *L, p_socket ps) 110
111int opt_get_tcp_keepcnt(lua_State *L, p_socket ps)
105{ 112{
106 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT); 113 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
107} 114}
108 115
109LUASOCKET_PRIVATE int opt_set_tcp_keepcnt(lua_State *L, p_socket ps) 116int opt_set_tcp_keepcnt(lua_State *L, p_socket ps)
110{ 117{
111 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT); 118 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPCNT);
112} 119}
120
113#endif 121#endif
114 122
123// -------------------------------------------------------
115#ifdef TCP_KEEPINTVL 124#ifdef TCP_KEEPINTVL
116LUASOCKET_PRIVATE int opt_get_tcp_keepintvl(lua_State *L, p_socket ps) 125
126int opt_get_tcp_keepintvl(lua_State *L, p_socket ps)
117{ 127{
118 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL); 128 return opt_getint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
119} 129}
120 130
121LUASOCKET_PRIVATE int opt_set_tcp_keepintvl(lua_State *L, p_socket ps) 131int opt_set_tcp_keepintvl(lua_State *L, p_socket ps)
122{ 132{
123 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL); 133 return opt_setint(L, ps, IPPROTO_TCP, TCP_KEEPINTVL);
124} 134}
135
125#endif 136#endif
126 137
127LUASOCKET_PRIVATE int opt_set_keepalive(lua_State *L, p_socket ps) 138// -------------------------------------------------------
139int opt_set_keepalive(lua_State *L, p_socket ps)
128{ 140{
129 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 141 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
130} 142}
131 143
132LUASOCKET_PRIVATE int opt_get_keepalive(lua_State *L, p_socket ps) 144int opt_get_keepalive(lua_State *L, p_socket ps)
133{ 145{
134 return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 146 return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
135} 147}
136 148
137LUASOCKET_PRIVATE int opt_set_dontroute(lua_State *L, p_socket ps) 149// -------------------------------------------------------
150int opt_set_dontroute(lua_State *L, p_socket ps)
138{ 151{
139 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 152 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
140} 153}
141 154
142LUASOCKET_PRIVATE int opt_get_dontroute(lua_State *L, p_socket ps) 155int opt_get_dontroute(lua_State *L, p_socket ps)
143{ 156{
144 return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 157 return opt_getboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
145} 158}
146 159
147LUASOCKET_PRIVATE int opt_set_broadcast(lua_State *L, p_socket ps) 160// -------------------------------------------------------
161int opt_set_broadcast(lua_State *L, p_socket ps)
148{ 162{
149 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 163 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
150} 164}
151 165
152LUASOCKET_PRIVATE int opt_set_recv_buf_size(lua_State *L, p_socket ps) 166int opt_get_broadcast(lua_State *L, p_socket ps)
167{
168 return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
169}
170
171// -------------------------------------------------------
172int opt_set_recv_buf_size(lua_State *L, p_socket ps)
153{ 173{
154 return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF); 174 return opt_setint(L, ps, SOL_SOCKET, SO_RCVBUF);
155} 175}
156 176
157LUASOCKET_PRIVATE int opt_get_recv_buf_size(lua_State *L, p_socket ps) 177int opt_get_recv_buf_size(lua_State *L, p_socket ps)
158{ 178{
159 return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF); 179 return opt_getint(L, ps, SOL_SOCKET, SO_RCVBUF);
160} 180}
161 181
162LUASOCKET_PRIVATE int opt_get_send_buf_size(lua_State *L, p_socket ps) 182// -------------------------------------------------------
183int opt_get_send_buf_size(lua_State *L, p_socket ps)
163{ 184{
164 return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF); 185 return opt_getint(L, ps, SOL_SOCKET, SO_SNDBUF);
165} 186}
166 187
167LUASOCKET_PRIVATE int opt_set_send_buf_size(lua_State *L, p_socket ps) 188int opt_set_send_buf_size(lua_State *L, p_socket ps)
168{ 189{
169 return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF); 190 return opt_setint(L, ps, SOL_SOCKET, SO_SNDBUF);
170} 191}
171 192
172LUASOCKET_PRIVATE int opt_get_broadcast(lua_State *L, p_socket ps) 193// -------------------------------------------------------
173{ 194int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
174 return opt_getboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
175}
176
177LUASOCKET_PRIVATE int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps)
178{ 195{
179 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 196 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
180} 197}
181 198
182LUASOCKET_PRIVATE int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps) 199int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps)
183{ 200{
184 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS); 201 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
185} 202}
186 203
187LUASOCKET_PRIVATE int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps) 204// -------------------------------------------------------
205int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps)
188{ 206{
189 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 207 return opt_setint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
190} 208}
191 209
192LUASOCKET_PRIVATE int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps) 210int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps)
193{ 211{
194 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 212 return opt_getint(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
195} 213}
196 214
197LUASOCKET_PRIVATE int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) 215// -------------------------------------------------------
216int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
198{ 217{
199 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 218 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
200} 219}
201 220
202LUASOCKET_PRIVATE int opt_get_ip_multicast_loop(lua_State *L, p_socket ps) 221int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
203{ 222{
204 return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 223 return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
205} 224}
206 225
207LUASOCKET_PRIVATE int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps) 226// -------------------------------------------------------
227int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps)
208{ 228{
209 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 229 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
210} 230}
211 231
212LUASOCKET_PRIVATE int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps) 232int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps)
213{ 233{
214 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 234 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
215} 235}
216 236
217LUASOCKET_PRIVATE int opt_set_linger(lua_State *L, p_socket ps) 237// -------------------------------------------------------
238int opt_set_linger(lua_State *L, p_socket ps)
218{ 239{
219 struct linger li; /* obj, name, table */ 240 struct linger li; /* obj, name, table */
220 if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); 241 if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE));
@@ -231,7 +252,7 @@ LUASOCKET_PRIVATE int opt_set_linger(lua_State *L, p_socket ps)
231 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); 252 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
232} 253}
233 254
234LUASOCKET_PRIVATE int opt_get_linger(lua_State *L, p_socket ps) 255int opt_get_linger(lua_State *L, p_socket ps)
235{ 256{
236 struct linger li; /* obj, name */ 257 struct linger li; /* obj, name */
237 int len = sizeof(li); 258 int len = sizeof(li);
@@ -246,12 +267,14 @@ LUASOCKET_PRIVATE int opt_get_linger(lua_State *L, p_socket ps)
246 return 1; 267 return 1;
247} 268}
248 269
249LUASOCKET_PRIVATE int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) 270// -------------------------------------------------------
271int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
250{ 272{
251 return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL); 273 return opt_setint(L, ps, IPPROTO_IP, IP_MULTICAST_TTL);
252} 274}
253 275
254LUASOCKET_PRIVATE int opt_set_ip_multicast_if(lua_State *L, p_socket ps) 276// -------------------------------------------------------
277int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
255{ 278{
256 const char *address = luaL_checkstring(L, 3); /* obj, name, ip */ 279 const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
257 struct in_addr val; 280 struct in_addr val;
@@ -262,7 +285,7 @@ LUASOCKET_PRIVATE int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
262 (char *) &val, sizeof(val)); 285 (char *) &val, sizeof(val));
263} 286}
264 287
265LUASOCKET_PRIVATE int opt_get_ip_multicast_if(lua_State *L, p_socket ps) 288int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
266{ 289{
267 struct in_addr val; 290 struct in_addr val;
268 socklen_t len = sizeof(val); 291 socklen_t len = sizeof(val);
@@ -275,36 +298,52 @@ LUASOCKET_PRIVATE int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
275 return 1; 298 return 1;
276} 299}
277 300
278LUASOCKET_PRIVATE int opt_set_ip_add_membership(lua_State *L, p_socket ps) 301// -------------------------------------------------------
302int opt_set_ip_add_membership(lua_State *L, p_socket ps)
279{ 303{
280 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); 304 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
281} 305}
282 306
283LUASOCKET_PRIVATE int opt_set_ip_drop_membersip(lua_State *L, p_socket ps) 307int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
284{ 308{
285 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); 309 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
286} 310}
287 311
288LUASOCKET_PRIVATE int opt_set_ip6_add_membership(lua_State *L, p_socket ps) 312// -------------------------------------------------------
313int opt_set_ip6_add_membership(lua_State *L, p_socket ps)
289{ 314{
290 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP); 315 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP);
291} 316}
292 317
293LUASOCKET_PRIVATE int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps) 318int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps)
294{ 319{
295 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP); 320 return opt_ip6_setmembership(L, ps, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP);
296} 321}
297 322// -------------------------------------------------------
298LUASOCKET_PRIVATE int opt_get_ip6_v6only(lua_State *L, p_socket ps) 323int opt_get_ip6_v6only(lua_State *L, p_socket ps)
299{ 324{
300 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 325 return opt_getboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
301} 326}
302 327
303LUASOCKET_PRIVATE int opt_set_ip6_v6only(lua_State *L, p_socket ps) 328int opt_set_ip6_v6only(lua_State *L, p_socket ps)
304{ 329{
305 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); 330 return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY);
306} 331}
307 332
333// -------------------------------------------------------
334int opt_get_error(lua_State *L, p_socket ps)
335{
336 int val = 0;
337 socklen_t len = sizeof(val);
338 if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
339 lua_pushnil(L);
340 lua_pushstring(L, "getsockopt failed");
341 return 2;
342 }
343 lua_pushstring(L, socket_strerror(val));
344 return 1;
345}
346
308/*=========================================================================*\ 347/*=========================================================================*\
309* Auxiliar functions 348* Auxiliar functions
310\*=========================================================================*/ 349\*=========================================================================*/
@@ -391,19 +430,6 @@ static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
391 return 1; 430 return 1;
392} 431}
393 432
394LUASOCKET_PRIVATE int opt_get_error(lua_State *L, p_socket ps)
395{
396 int val = 0;
397 socklen_t len = sizeof(val);
398 if (getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *) &val, &len) < 0) {
399 lua_pushnil(L);
400 lua_pushstring(L, "getsockopt failed");
401 return 2;
402 }
403 lua_pushstring(L, socket_strerror(val));
404 return 1;
405}
406
407static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) 433static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
408{ 434{
409 int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ 435 int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
diff --git a/src/options.h b/src/options.h
index fb82c4d..41f7337 100644
--- a/src/options.h
+++ b/src/options.h
@@ -8,7 +8,7 @@
8* modules UDP and TCP. 8* modules UDP and TCP.
9\*=========================================================================*/ 9\*=========================================================================*/
10 10
11#include "lua.h" 11#include "luasocket.h"
12#include "socket.h" 12#include "socket.h"
13 13
14/* option registry */ 14/* option registry */
@@ -18,67 +18,85 @@ typedef struct t_opt {
18} t_opt; 18} t_opt;
19typedef t_opt *p_opt; 19typedef t_opt *p_opt;
20 20
21/* supported options for setoption */ 21#ifndef _WIN32
22int opt_set_dontroute(lua_State *L, p_socket ps); 22#pragma GCC visibility push(hidden)
23int opt_set_broadcast(lua_State *L, p_socket ps); 23#endif
24
25int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
26int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps);
27
28int opt_set_reuseaddr(lua_State *L, p_socket ps);
29int opt_get_reuseaddr(lua_State *L, p_socket ps);
30
31int opt_set_reuseport(lua_State *L, p_socket ps);
32int opt_get_reuseport(lua_State *L, p_socket ps);
33
24int opt_set_tcp_nodelay(lua_State *L, p_socket ps); 34int opt_set_tcp_nodelay(lua_State *L, p_socket ps);
35int opt_get_tcp_nodelay(lua_State *L, p_socket ps);
36
25#ifdef TCP_KEEPIDLE 37#ifdef TCP_KEEPIDLE
26int opt_set_tcp_keepidle(lua_State *L, p_socket ps); 38int opt_set_tcp_keepidle(lua_State *L, p_socket ps);
39int opt_get_tcp_keepidle(lua_State *L, p_socket ps);
27#endif 40#endif
41
28#ifdef TCP_KEEPCNT 42#ifdef TCP_KEEPCNT
29int opt_set_tcp_keepcnt(lua_State *L, p_socket ps); 43int opt_set_tcp_keepcnt(lua_State *L, p_socket ps);
44int opt_get_tcp_keepcnt(lua_State *L, p_socket ps);
30#endif 45#endif
46
31#ifdef TCP_KEEPINTVL 47#ifdef TCP_KEEPINTVL
32int opt_set_tcp_keepintvl(lua_State *L, p_socket ps); 48int opt_set_tcp_keepintvl(lua_State *L, p_socket ps);
49int opt_get_tcp_keepintvl(lua_State *L, p_socket ps);
33#endif 50#endif
51
34int opt_set_keepalive(lua_State *L, p_socket ps); 52int opt_set_keepalive(lua_State *L, p_socket ps);
53int opt_get_keepalive(lua_State *L, p_socket ps);
54
55int opt_set_dontroute(lua_State *L, p_socket ps);
56int opt_get_dontroute(lua_State *L, p_socket ps);
57
58int opt_set_broadcast(lua_State *L, p_socket ps);
59int opt_get_broadcast(lua_State *L, p_socket ps);
60
61int opt_set_recv_buf_size(lua_State *L, p_socket ps);
62int opt_get_recv_buf_size(lua_State *L, p_socket ps);
63
64int opt_set_send_buf_size(lua_State *L, p_socket ps);
65int opt_get_send_buf_size(lua_State *L, p_socket ps);
66
67int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps);
68int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps);
69
70int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps);
71int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps);
72
73int opt_set_ip_multicast_loop(lua_State *L, p_socket ps);
74int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
75
76int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps);
77int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps);
78
35int opt_set_linger(lua_State *L, p_socket ps); 79int opt_set_linger(lua_State *L, p_socket ps);
36int opt_set_reuseaddr(lua_State *L, p_socket ps); 80int opt_get_linger(lua_State *L, p_socket ps);
37int opt_set_reuseport(lua_State *L, p_socket ps); 81
38int opt_set_ip_multicast_if(lua_State *L, p_socket ps);
39int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps); 82int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps);
40int opt_set_ip_multicast_loop(lua_State *L, p_socket ps); 83
84int opt_set_ip_multicast_if(lua_State *L, p_socket ps);
85int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
86
41int opt_set_ip_add_membership(lua_State *L, p_socket ps); 87int opt_set_ip_add_membership(lua_State *L, p_socket ps);
42int opt_set_ip_drop_membersip(lua_State *L, p_socket ps); 88int opt_set_ip_drop_membersip(lua_State *L, p_socket ps);
43int opt_set_ip6_unicast_hops(lua_State *L, p_socket ps); 89
44int opt_set_ip6_multicast_hops(lua_State *L, p_socket ps);
45int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps);
46int opt_set_ip6_add_membership(lua_State *L, p_socket ps); 90int opt_set_ip6_add_membership(lua_State *L, p_socket ps);
47int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps); 91int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps);
92
48int opt_set_ip6_v6only(lua_State *L, p_socket ps); 93int opt_set_ip6_v6only(lua_State *L, p_socket ps);
49int opt_set_recv_buf_size(lua_State *L, p_socket ps); 94int opt_get_ip6_v6only(lua_State *L, p_socket ps);
50int opt_set_send_buf_size(lua_State *L, p_socket ps);
51 95
52/* supported options for getoption */
53int opt_get_dontroute(lua_State *L, p_socket ps);
54int opt_get_broadcast(lua_State *L, p_socket ps);
55int opt_get_reuseaddr(lua_State *L, p_socket ps);
56int opt_get_reuseport(lua_State *L, p_socket ps);
57int opt_get_tcp_nodelay(lua_State *L, p_socket ps);
58#ifdef TCP_KEEPIDLE
59int opt_get_tcp_keepidle(lua_State *L, p_socket ps);
60#endif
61#ifdef TCP_KEEPCNT
62int opt_get_tcp_keepcnt(lua_State *L, p_socket ps);
63#endif
64#ifdef TCP_KEEPINTVL
65int opt_get_tcp_keepintvl(lua_State *L, p_socket ps);
66#endif
67int opt_get_keepalive(lua_State *L, p_socket ps);
68int opt_get_linger(lua_State *L, p_socket ps);
69int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
70int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
71int opt_get_error(lua_State *L, p_socket ps); 96int opt_get_error(lua_State *L, p_socket ps);
72int opt_get_ip6_multicast_loop(lua_State *L, p_socket ps);
73int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps);
74int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps);
75int opt_get_ip6_v6only(lua_State *L, p_socket ps);
76int opt_get_reuseport(lua_State *L, p_socket ps);
77int opt_get_recv_buf_size(lua_State *L, p_socket ps);
78int opt_get_send_buf_size(lua_State *L, p_socket ps);
79 97
80/* invokes the appropriate option handler */ 98#ifndef _WIN32
81int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); 99#pragma GCC visibility pop
82int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps); 100#endif
83 101
84#endif 102#endif
diff --git a/src/select.c b/src/select.c
index b615b19..bb47c45 100644
--- a/src/select.c
+++ b/src/select.c
@@ -4,10 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "socket.h" 7#include "socket.h"
12#include "timeout.h" 8#include "timeout.h"
13#include "select.h" 9#include "select.h"
@@ -33,13 +29,10 @@ static luaL_Reg func[] = {
33 {NULL, NULL} 29 {NULL, NULL}
34}; 30};
35 31
36/*=========================================================================*\
37* Exported functions
38\*=========================================================================*/
39/*-------------------------------------------------------------------------*\ 32/*-------------------------------------------------------------------------*\
40* Initializes module 33* Initializes module
41\*-------------------------------------------------------------------------*/ 34\*-------------------------------------------------------------------------*/
42LUASOCKET_PRIVATE int select_open(lua_State *L) { 35int select_open(lua_State *L) {
43 lua_pushstring(L, "_SETSIZE"); 36 lua_pushstring(L, "_SETSIZE");
44 lua_pushinteger(L, FD_SETSIZE); 37 lua_pushinteger(L, FD_SETSIZE);
45 lua_rawset(L, -3); 38 lua_rawset(L, -3);
@@ -219,4 +212,3 @@ static void make_assoc(lua_State *L, int tab) {
219 i = i+1; 212 i = i+1;
220 } 213 }
221} 214}
222
diff --git a/src/select.h b/src/select.h
index 8750200..5d45fe7 100644
--- a/src/select.h
+++ b/src/select.h
@@ -10,6 +10,14 @@
10* true if there is data ready for reading (required for buffered input). 10* true if there is data ready for reading (required for buffered input).
11\*=========================================================================*/ 11\*=========================================================================*/
12 12
13#ifndef _WIN32
14#pragma GCC visibility push(hidden)
15#endif
16
13int select_open(lua_State *L); 17int select_open(lua_State *L);
14 18
19#ifndef _WIN32
20#pragma GCC visibility pop
21#endif
22
15#endif /* SELECT_H */ 23#endif /* SELECT_H */
diff --git a/src/serial.c b/src/serial.c
index cb46edb..21485d3 100644
--- a/src/serial.c
+++ b/src/serial.c
@@ -4,15 +4,12 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include <string.h>
8
9#include "lua.h"
10#include "lauxlib.h"
11
12#include "auxiliar.h" 7#include "auxiliar.h"
13#include "socket.h" 8#include "socket.h"
14#include "options.h" 9#include "options.h"
15#include "unix.h" 10#include "unix.h"
11
12#include <string.h>
16#include <sys/un.h> 13#include <sys/un.h>
17 14
18/* 15/*
diff --git a/src/socket.h b/src/socket.h
index 63573de..e541f27 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -28,51 +28,46 @@
28\*=========================================================================*/ 28\*=========================================================================*/
29#include "timeout.h" 29#include "timeout.h"
30 30
31/* we are lazy... */ 31/* convenient shorthand */
32typedef struct sockaddr SA; 32typedef struct sockaddr SA;
33 33
34/*=========================================================================*\ 34/*=========================================================================*\
35* Functions bellow implement a comfortable platform independent 35* Functions bellow implement a comfortable platform independent
36* interface to sockets 36* interface to sockets
37\*=========================================================================*/ 37\*=========================================================================*/
38int socket_open(void);
39int socket_close(void);
40void socket_destroy(p_socket ps);
41void socket_shutdown(p_socket ps, int how);
42int socket_sendto(p_socket ps, const char *data, size_t count,
43 size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
44int socket_recvfrom(p_socket ps, char *data, size_t count,
45 size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
46 38
47void socket_setnonblocking(p_socket ps); 39#ifndef _WIN32
48void socket_setblocking(p_socket ps); 40#pragma GCC visibility push(hidden)
41#endif
49 42
50int socket_waitfd(p_socket ps, int sw, p_timeout tm); 43int socket_waitfd(p_socket ps, int sw, p_timeout tm);
51int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 44int socket_open(void);
52 p_timeout tm); 45int socket_close(void);
53 46void socket_destroy(p_socket ps);
54int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm); 47int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm);
55int socket_create(p_socket ps, int domain, int type, int protocol); 48int socket_create(p_socket ps, int domain, int type, int protocol);
56int socket_bind(p_socket ps, SA *addr, socklen_t addr_len); 49int socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
57int socket_listen(p_socket ps, int backlog); 50int socket_listen(p_socket ps, int backlog);
58int socket_accept(p_socket ps, p_socket pa, SA *addr, 51void socket_shutdown(p_socket ps, int how);
59 socklen_t *addr_len, p_timeout tm); 52int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
60 53int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *addr_len, p_timeout tm);
61const char *socket_hoststrerror(int err); 54int socket_send(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
62const char *socket_gaistrerror(int err); 55int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
63const char *socket_strerror(int err);
64
65/* these are perfect to use with the io abstraction module
66 and the buffered input module */
67int socket_send(p_socket ps, const char *data, size_t count,
68 size_t *sent, p_timeout tm);
69int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); 56int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
70int socket_write(p_socket ps, const char *data, size_t count, 57int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
71 size_t *sent, p_timeout tm); 58int socket_write(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
72int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); 59int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
73const char *socket_ioerror(p_socket ps, int err); 60void socket_setblocking(p_socket ps);
74 61void socket_setnonblocking(p_socket ps);
75int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); 62int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
76int socket_gethostbyname(const char *addr, struct hostent **hp); 63int socket_gethostbyname(const char *addr, struct hostent **hp);
64const char *socket_hoststrerror(int err);
65const char *socket_strerror(int err);
66const char *socket_ioerror(p_socket ps, int err);
67const char *socket_gaistrerror(int err);
68
69#ifndef _WIN32
70#pragma GCC visibility pop
71#endif
77 72
78#endif /* SOCKET_H */ 73#endif /* SOCKET_H */
diff --git a/src/tcp.c b/src/tcp.c
index cc5b6a7..5876bfb 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -4,12 +4,7 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9
10#include "compat.h"
11#include "auxiliar.h" 7#include "auxiliar.h"
12
13#include "socket.h" 8#include "socket.h"
14#include "inet.h" 9#include "inet.h"
15#include "options.h" 10#include "options.h"
@@ -129,7 +124,7 @@ static luaL_Reg func[] = {
129/*-------------------------------------------------------------------------*\ 124/*-------------------------------------------------------------------------*\
130* Initializes module 125* Initializes module
131\*-------------------------------------------------------------------------*/ 126\*-------------------------------------------------------------------------*/
132LUASOCKET_PRIVATE int tcp_open(lua_State *L) 127int tcp_open(lua_State *L)
133{ 128{
134 /* create classes */ 129 /* create classes */
135 auxiliar_newclass(L, "tcp{master}", tcp_methods); 130 auxiliar_newclass(L, "tcp{master}", tcp_methods);
diff --git a/src/tcp.h b/src/tcp.h
index eded620..9b282ef 100644
--- a/src/tcp.h
+++ b/src/tcp.h
@@ -14,7 +14,7 @@
14* tcp objects either connected to some address or returned by the accept 14* tcp objects either connected to some address or returned by the accept
15* method of a server object. 15* method of a server object.
16\*=========================================================================*/ 16\*=========================================================================*/
17#include "lua.h" 17#include "luasocket.h"
18 18
19#include "buffer.h" 19#include "buffer.h"
20#include "timeout.h" 20#include "timeout.h"
@@ -30,6 +30,14 @@ typedef struct t_tcp_ {
30 30
31typedef t_tcp *p_tcp; 31typedef t_tcp *p_tcp;
32 32
33#ifndef _WIN32
34#pragma GCC visibility push(hidden)
35#endif
36
33int tcp_open(lua_State *L); 37int tcp_open(lua_State *L);
34 38
39#ifndef _WIN32
40#pragma GCC visibility pop
41#endif
42
35#endif /* TCP_H */ 43#endif /* TCP_H */
diff --git a/src/timeout.c b/src/timeout.c
index 8fb8f55..2bdc069 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -4,10 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "auxiliar.h" 7#include "auxiliar.h"
12#include "timeout.h" 8#include "timeout.h"
13 9
@@ -48,7 +44,7 @@ static luaL_Reg func[] = {
48/*-------------------------------------------------------------------------*\ 44/*-------------------------------------------------------------------------*\
49* Initialize structure 45* Initialize structure
50\*-------------------------------------------------------------------------*/ 46\*-------------------------------------------------------------------------*/
51LUASOCKET_PRIVATE void timeout_init(p_timeout tm, double block, double total) { 47void timeout_init(p_timeout tm, double block, double total) {
52 tm->block = block; 48 tm->block = block;
53 tm->total = total; 49 tm->total = total;
54} 50}
@@ -61,7 +57,7 @@ LUASOCKET_PRIVATE void timeout_init(p_timeout tm, double block, double total) {
61* Returns 57* Returns
62* the number of ms left or -1 if there is no time limit 58* the number of ms left or -1 if there is no time limit
63\*-------------------------------------------------------------------------*/ 59\*-------------------------------------------------------------------------*/
64LUASOCKET_PRIVATE double timeout_get(p_timeout tm) { 60double timeout_get(p_timeout tm) {
65 if (tm->block < 0.0 && tm->total < 0.0) { 61 if (tm->block < 0.0 && tm->total < 0.0) {
66 return -1; 62 return -1;
67 } else if (tm->block < 0.0) { 63 } else if (tm->block < 0.0) {
@@ -82,7 +78,7 @@ LUASOCKET_PRIVATE double timeout_get(p_timeout tm) {
82* Returns 78* Returns
83* start field of structure 79* start field of structure
84\*-------------------------------------------------------------------------*/ 80\*-------------------------------------------------------------------------*/
85LUASOCKET_PRIVATE double timeout_getstart(p_timeout tm) { 81double timeout_getstart(p_timeout tm) {
86 return tm->start; 82 return tm->start;
87} 83}
88 84
@@ -94,7 +90,7 @@ LUASOCKET_PRIVATE double timeout_getstart(p_timeout tm) {
94* Returns 90* Returns
95* the number of ms left or -1 if there is no time limit 91* the number of ms left or -1 if there is no time limit
96\*-------------------------------------------------------------------------*/ 92\*-------------------------------------------------------------------------*/
97LUASOCKET_PRIVATE double timeout_getretry(p_timeout tm) { 93double timeout_getretry(p_timeout tm) {
98 if (tm->block < 0.0 && tm->total < 0.0) { 94 if (tm->block < 0.0 && tm->total < 0.0) {
99 return -1; 95 return -1;
100 } else if (tm->block < 0.0) { 96 } else if (tm->block < 0.0) {
@@ -114,7 +110,7 @@ LUASOCKET_PRIVATE double timeout_getretry(p_timeout tm) {
114* Input 110* Input
115* tm: timeout control structure 111* tm: timeout control structure
116\*-------------------------------------------------------------------------*/ 112\*-------------------------------------------------------------------------*/
117LUASOCKET_PRIVATE p_timeout timeout_markstart(p_timeout tm) { 113p_timeout timeout_markstart(p_timeout tm) {
118 tm->start = timeout_gettime(); 114 tm->start = timeout_gettime();
119 return tm; 115 return tm;
120} 116}
@@ -125,7 +121,7 @@ LUASOCKET_PRIVATE p_timeout timeout_markstart(p_timeout tm) {
125* time in s. 121* time in s.
126\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
127#ifdef _WIN32 123#ifdef _WIN32
128LUASOCKET_PRIVATE double timeout_gettime(void) { 124double timeout_gettime(void) {
129 FILETIME ft; 125 FILETIME ft;
130 double t; 126 double t;
131 GetSystemTimeAsFileTime(&ft); 127 GetSystemTimeAsFileTime(&ft);
@@ -135,7 +131,7 @@ LUASOCKET_PRIVATE double timeout_gettime(void) {
135 return (t - 11644473600.0); 131 return (t - 11644473600.0);
136} 132}
137#else 133#else
138LUASOCKET_PRIVATE double timeout_gettime(void) { 134double timeout_gettime(void) {
139 struct timeval v; 135 struct timeval v;
140 gettimeofday(&v, (struct timezone *) NULL); 136 gettimeofday(&v, (struct timezone *) NULL);
141 /* Unix Epoch time (time since January 1, 1970 (UTC)) */ 137 /* Unix Epoch time (time since January 1, 1970 (UTC)) */
@@ -146,7 +142,7 @@ LUASOCKET_PRIVATE double timeout_gettime(void) {
146/*-------------------------------------------------------------------------*\ 142/*-------------------------------------------------------------------------*\
147* Initializes module 143* Initializes module
148\*-------------------------------------------------------------------------*/ 144\*-------------------------------------------------------------------------*/
149LUASOCKET_PRIVATE int timeout_open(lua_State *L) { 145int timeout_open(lua_State *L) {
150 luaL_setfuncs(L, func, 0); 146 luaL_setfuncs(L, func, 0);
151 return 0; 147 return 0;
152} 148}
@@ -157,7 +153,7 @@ LUASOCKET_PRIVATE int timeout_open(lua_State *L) {
157* time: time out value in seconds 153* time: time out value in seconds
158* mode: "b" for block timeout, "t" for total timeout. (default: b) 154* mode: "b" for block timeout, "t" for total timeout. (default: b)
159\*-------------------------------------------------------------------------*/ 155\*-------------------------------------------------------------------------*/
160LUASOCKET_PRIVATE int timeout_meth_settimeout(lua_State *L, p_timeout tm) { 156int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
161 double t = luaL_optnumber(L, 2, -1); 157 double t = luaL_optnumber(L, 2, -1);
162 const char *mode = luaL_optstring(L, 3, "b"); 158 const char *mode = luaL_optstring(L, 3, "b");
163 switch (*mode) { 159 switch (*mode) {
@@ -179,7 +175,7 @@ LUASOCKET_PRIVATE int timeout_meth_settimeout(lua_State *L, p_timeout tm) {
179* Gets timeout values for IO operations 175* Gets timeout values for IO operations
180* Lua Output: block, total 176* Lua Output: block, total
181\*-------------------------------------------------------------------------*/ 177\*-------------------------------------------------------------------------*/
182LUASOCKET_PRIVATE int timeout_meth_gettimeout(lua_State *L, p_timeout tm) { 178int timeout_meth_gettimeout(lua_State *L, p_timeout tm) {
183 lua_pushnumber(L, tm->block); 179 lua_pushnumber(L, tm->block);
184 lua_pushnumber(L, tm->total); 180 lua_pushnumber(L, tm->total);
185 return 2; 181 return 2;
@@ -201,7 +197,7 @@ static int timeout_lua_gettime(lua_State *L)
201* Sleep for n seconds. 197* Sleep for n seconds.
202\*-------------------------------------------------------------------------*/ 198\*-------------------------------------------------------------------------*/
203#ifdef _WIN32 199#ifdef _WIN32
204LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L) 200int timeout_lua_sleep(lua_State *L)
205{ 201{
206 double n = luaL_checknumber(L, 1); 202 double n = luaL_checknumber(L, 1);
207 if (n < 0.0) n = 0.0; 203 if (n < 0.0) n = 0.0;
@@ -211,7 +207,7 @@ LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L)
211 return 0; 207 return 0;
212} 208}
213#else 209#else
214LUASOCKET_PRIVATE int timeout_lua_sleep(lua_State *L) 210int timeout_lua_sleep(lua_State *L)
215{ 211{
216 double n = luaL_checknumber(L, 1); 212 double n = luaL_checknumber(L, 1);
217 struct timespec t, r; 213 struct timespec t, r;
diff --git a/src/timeout.h b/src/timeout.h
index af90231..9e5250d 100644
--- a/src/timeout.h
+++ b/src/timeout.h
@@ -4,7 +4,7 @@
4* Timeout management functions 4* Timeout management functions
5* LuaSocket toolkit 5* LuaSocket toolkit
6\*=========================================================================*/ 6\*=========================================================================*/
7#include "lua.h" 7#include "luasocket.h"
8 8
9/* timeout control structure */ 9/* timeout control structure */
10typedef struct t_timeout_ { 10typedef struct t_timeout_ {
@@ -14,16 +14,27 @@ typedef struct t_timeout_ {
14} t_timeout; 14} t_timeout;
15typedef t_timeout *p_timeout; 15typedef t_timeout *p_timeout;
16 16
17int timeout_open(lua_State *L); 17#ifndef _WIN32
18#pragma GCC visibility push(hidden)
19#endif
20
18void timeout_init(p_timeout tm, double block, double total); 21void timeout_init(p_timeout tm, double block, double total);
19double timeout_get(p_timeout tm); 22double timeout_get(p_timeout tm);
23double timeout_getstart(p_timeout tm);
20double timeout_getretry(p_timeout tm); 24double timeout_getretry(p_timeout tm);
21p_timeout timeout_markstart(p_timeout tm); 25p_timeout timeout_markstart(p_timeout tm);
22double timeout_getstart(p_timeout tm); 26
23double timeout_gettime(void); 27double timeout_gettime(void);
28
29int timeout_open(lua_State *L);
30
24int timeout_meth_settimeout(lua_State *L, p_timeout tm); 31int timeout_meth_settimeout(lua_State *L, p_timeout tm);
25int timeout_meth_gettimeout(lua_State *L, p_timeout tm); 32int timeout_meth_gettimeout(lua_State *L, p_timeout tm);
26 33
34#ifndef _WIN32
35#pragma GCC visibility pop
36#endif
37
27#define timeout_iszero(tm) ((tm)->block == 0.0) 38#define timeout_iszero(tm) ((tm)->block == 0.0)
28 39
29#endif /* TIMEOUT_H */ 40#endif /* TIMEOUT_H */
diff --git a/src/udp.c b/src/udp.c
index e59fa1b..62b6a20 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -4,10 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "auxiliar.h" 7#include "auxiliar.h"
12#include "socket.h" 8#include "socket.h"
13#include "inet.h" 9#include "inet.h"
@@ -124,7 +120,7 @@ static luaL_Reg func[] = {
124/*-------------------------------------------------------------------------*\ 120/*-------------------------------------------------------------------------*\
125* Initializes module 121* Initializes module
126\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
127LUASOCKET_PRIVATE int udp_open(lua_State *L) { 123int udp_open(lua_State *L) {
128 /* create classes */ 124 /* create classes */
129 auxiliar_newclass(L, "udp{connected}", udp_methods); 125 auxiliar_newclass(L, "udp{connected}", udp_methods);
130 auxiliar_newclass(L, "udp{unconnected}", udp_methods); 126 auxiliar_newclass(L, "udp{unconnected}", udp_methods);
diff --git a/src/udp.h b/src/udp.h
index be9b6a5..07d5247 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -12,7 +12,7 @@
12* with a call to the setpeername function. The same function can be used to 12* with a call to the setpeername function. The same function can be used to
13* break the connection. 13* break the connection.
14\*=========================================================================*/ 14\*=========================================================================*/
15#include "lua.h" 15#include "luasocket.h"
16 16
17#include "timeout.h" 17#include "timeout.h"
18#include "socket.h" 18#include "socket.h"
@@ -26,6 +26,14 @@ typedef struct t_udp_ {
26} t_udp; 26} t_udp;
27typedef t_udp *p_udp; 27typedef t_udp *p_udp;
28 28
29#ifndef _WIN32
30#pragma GCC visibility push(hidden)
31#endif
32
29int udp_open(lua_State *L); 33int udp_open(lua_State *L);
30 34
35#ifndef _WIN32
36#pragma GCC visibility pop
37#endif
38
31#endif /* UDP_H */ 39#endif /* UDP_H */
diff --git a/src/unix.c b/src/unix.c
index c618a20..268d8b2 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -4,9 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9
10#include "unixstream.h" 7#include "unixstream.h"
11#include "unixdgram.h" 8#include "unixdgram.h"
12 9
diff --git a/src/unix.h b/src/unix.h
index a1674ef..c203561 100644
--- a/src/unix.h
+++ b/src/unix.h
@@ -8,7 +8,6 @@
8* domain. 8* domain.
9\*=========================================================================*/ 9\*=========================================================================*/
10#include "luasocket.h" 10#include "luasocket.h"
11#include "lua.h"
12 11
13#include "buffer.h" 12#include "buffer.h"
14#include "timeout.h" 13#include "timeout.h"
diff --git a/src/unixdgram.c b/src/unixdgram.c
index 840257a..3ac3c5e 100644
--- a/src/unixdgram.c
+++ b/src/unixdgram.c
@@ -4,10 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "auxiliar.h" 7#include "auxiliar.h"
12#include "socket.h" 8#include "socket.h"
13#include "options.h" 9#include "options.h"
@@ -26,6 +22,7 @@
26 ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ 22 ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
27 + strlen ((ptr)->sun_path)) 23 + strlen ((ptr)->sun_path))
28#endif 24#endif
25
29/*=========================================================================*\ 26/*=========================================================================*\
30* Internal function prototypes 27* Internal function prototypes
31\*=========================================================================*/ 28\*=========================================================================*/
@@ -86,7 +83,7 @@ static luaL_Reg func[] = {
86/*-------------------------------------------------------------------------*\ 83/*-------------------------------------------------------------------------*\
87* Initializes module 84* Initializes module
88\*-------------------------------------------------------------------------*/ 85\*-------------------------------------------------------------------------*/
89LUASOCKET_PRIVATE int unixdgram_open(lua_State *L) 86int unixdgram_open(lua_State *L)
90{ 87{
91 /* create classes */ 88 /* create classes */
92 auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods); 89 auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
diff --git a/src/unixdgram.h b/src/unixdgram.h
index 7187966..a1a0166 100644
--- a/src/unixdgram.h
+++ b/src/unixdgram.h
@@ -15,6 +15,14 @@
15 15
16#include "unix.h" 16#include "unix.h"
17 17
18#ifndef _WIN32
19#pragma GCC visibility push(hidden)
20#endif
21
18int unixdgram_open(lua_State *L); 22int unixdgram_open(lua_State *L);
19 23
24#ifndef _WIN32
25#pragma GCC visibility pop
26#endif
27
20#endif /* UNIXDGRAM_H */ 28#endif /* UNIXDGRAM_H */
diff --git a/src/unixstream.c b/src/unixstream.c
index ce2d3af..02aced9 100644
--- a/src/unixstream.c
+++ b/src/unixstream.c
@@ -4,10 +4,6 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "luasocket.h" 5#include "luasocket.h"
6 6
7#include "lua.h"
8#include "lauxlib.h"
9#include "compat.h"
10
11#include "auxiliar.h" 7#include "auxiliar.h"
12#include "socket.h" 8#include "socket.h"
13#include "options.h" 9#include "options.h"
@@ -82,7 +78,7 @@ static luaL_Reg func[] = {
82/*-------------------------------------------------------------------------*\ 78/*-------------------------------------------------------------------------*\
83* Initializes module 79* Initializes module
84\*-------------------------------------------------------------------------*/ 80\*-------------------------------------------------------------------------*/
85LUASOCKET_PRIVATE int unixstream_open(lua_State *L) 81int unixstream_open(lua_State *L)
86{ 82{
87 /* create classes */ 83 /* create classes */
88 auxiliar_newclass(L, "unixstream{master}", unixstream_methods); 84 auxiliar_newclass(L, "unixstream{master}", unixstream_methods);
diff --git a/src/unixstream.h b/src/unixstream.h
index ef1d071..7916aff 100644
--- a/src/unixstream.h
+++ b/src/unixstream.h
@@ -16,6 +16,14 @@
16\*=========================================================================*/ 16\*=========================================================================*/
17#include "unix.h" 17#include "unix.h"
18 18
19#ifndef _WIN32
20#pragma GCC visibility push(hidden)
21#endif
22
19int unixstream_open(lua_State *L); 23int unixstream_open(lua_State *L);
20 24
25#ifndef _WIN32
26#pragma GCC visibility pop
27#endif
28
21#endif /* UNIXSTREAM_H */ 29#endif /* UNIXSTREAM_H */
diff --git a/src/usocket.c b/src/usocket.c
index aee876d..acfe186 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -23,7 +23,7 @@
23#define WAITFD_R POLLIN 23#define WAITFD_R POLLIN
24#define WAITFD_W POLLOUT 24#define WAITFD_W POLLOUT
25#define WAITFD_C (POLLIN|POLLOUT) 25#define WAITFD_C (POLLIN|POLLOUT)
26LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 26int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
27 int ret; 27 int ret;
28 struct pollfd pfd; 28 struct pollfd pfd;
29 pfd.fd = *ps; 29 pfd.fd = *ps;
@@ -45,7 +45,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
45#define WAITFD_W 2 45#define WAITFD_W 2
46#define WAITFD_C (WAITFD_R|WAITFD_W) 46#define WAITFD_C (WAITFD_R|WAITFD_W)
47 47
48LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 48int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
49 int ret; 49 int ret;
50 fd_set rfds, wfds, *rp, *wp; 50 fd_set rfds, wfds, *rp, *wp;
51 struct timeval tv, *tp; 51 struct timeval tv, *tp;
@@ -77,7 +77,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
77/*-------------------------------------------------------------------------*\ 77/*-------------------------------------------------------------------------*\
78* Initializes module 78* Initializes module
79\*-------------------------------------------------------------------------*/ 79\*-------------------------------------------------------------------------*/
80LUASOCKET_PRIVATE int socket_open(void) { 80int socket_open(void) {
81 /* installs a handler to ignore sigpipe or it will crash us */ 81 /* installs a handler to ignore sigpipe or it will crash us */
82 signal(SIGPIPE, SIG_IGN); 82 signal(SIGPIPE, SIG_IGN);
83 return 1; 83 return 1;
@@ -86,14 +86,14 @@ LUASOCKET_PRIVATE int socket_open(void) {
86/*-------------------------------------------------------------------------*\ 86/*-------------------------------------------------------------------------*\
87* Close module 87* Close module
88\*-------------------------------------------------------------------------*/ 88\*-------------------------------------------------------------------------*/
89LUASOCKET_PRIVATE int socket_close(void) { 89int socket_close(void) {
90 return 1; 90 return 1;
91} 91}
92 92
93/*-------------------------------------------------------------------------*\ 93/*-------------------------------------------------------------------------*\
94* Close and inutilize socket 94* Close and inutilize socket
95\*-------------------------------------------------------------------------*/ 95\*-------------------------------------------------------------------------*/
96LUASOCKET_PRIVATE void socket_destroy(p_socket ps) { 96void socket_destroy(p_socket ps) {
97 if (*ps != SOCKET_INVALID) { 97 if (*ps != SOCKET_INVALID) {
98 close(*ps); 98 close(*ps);
99 *ps = SOCKET_INVALID; 99 *ps = SOCKET_INVALID;
@@ -103,7 +103,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
103/*-------------------------------------------------------------------------*\ 103/*-------------------------------------------------------------------------*\
104* Select with timeout control 104* Select with timeout control
105\*-------------------------------------------------------------------------*/ 105\*-------------------------------------------------------------------------*/
106LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 106int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
107 p_timeout tm) { 107 p_timeout tm) {
108 int ret; 108 int ret;
109 do { 109 do {
@@ -120,7 +120,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
120/*-------------------------------------------------------------------------*\ 120/*-------------------------------------------------------------------------*\
121* Creates and sets up a socket 121* Creates and sets up a socket
122\*-------------------------------------------------------------------------*/ 122\*-------------------------------------------------------------------------*/
123LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) { 123int socket_create(p_socket ps, int domain, int type, int protocol) {
124 *ps = socket(domain, type, protocol); 124 *ps = socket(domain, type, protocol);
125 if (*ps != SOCKET_INVALID) return IO_DONE; 125 if (*ps != SOCKET_INVALID) return IO_DONE;
126 else return errno; 126 else return errno;
@@ -129,7 +129,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
129/*-------------------------------------------------------------------------*\ 129/*-------------------------------------------------------------------------*\
130* Binds or returns error message 130* Binds or returns error message
131\*-------------------------------------------------------------------------*/ 131\*-------------------------------------------------------------------------*/
132LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) { 132int socket_bind(p_socket ps, SA *addr, socklen_t len) {
133 int err = IO_DONE; 133 int err = IO_DONE;
134 socket_setblocking(ps); 134 socket_setblocking(ps);
135 if (bind(*ps, addr, len) < 0) err = errno; 135 if (bind(*ps, addr, len) < 0) err = errno;
@@ -140,7 +140,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
140/*-------------------------------------------------------------------------*\ 140/*-------------------------------------------------------------------------*\
141* 141*
142\*-------------------------------------------------------------------------*/ 142\*-------------------------------------------------------------------------*/
143LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) { 143int socket_listen(p_socket ps, int backlog) {
144 int err = IO_DONE; 144 int err = IO_DONE;
145 if (listen(*ps, backlog)) err = errno; 145 if (listen(*ps, backlog)) err = errno;
146 return err; 146 return err;
@@ -149,14 +149,14 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
149/*-------------------------------------------------------------------------*\ 149/*-------------------------------------------------------------------------*\
150* 150*
151\*-------------------------------------------------------------------------*/ 151\*-------------------------------------------------------------------------*/
152LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) { 152void socket_shutdown(p_socket ps, int how) {
153 shutdown(*ps, how); 153 shutdown(*ps, how);
154} 154}
155 155
156/*-------------------------------------------------------------------------*\ 156/*-------------------------------------------------------------------------*\
157* Connects or returns error message 157* Connects or returns error message
158\*-------------------------------------------------------------------------*/ 158\*-------------------------------------------------------------------------*/
159LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { 159int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
160 int err; 160 int err;
161 /* avoid calling on closed sockets */ 161 /* avoid calling on closed sockets */
162 if (*ps == SOCKET_INVALID) return IO_CLOSED; 162 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -178,7 +178,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
178/*-------------------------------------------------------------------------*\ 178/*-------------------------------------------------------------------------*\
179* Accept with timeout 179* Accept with timeout
180\*-------------------------------------------------------------------------*/ 180\*-------------------------------------------------------------------------*/
181LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { 181int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) {
182 if (*ps == SOCKET_INVALID) return IO_CLOSED; 182 if (*ps == SOCKET_INVALID) return IO_CLOSED;
183 for ( ;; ) { 183 for ( ;; ) {
184 int err; 184 int err;
@@ -195,7 +195,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
195/*-------------------------------------------------------------------------*\ 195/*-------------------------------------------------------------------------*\
196* Send with timeout 196* Send with timeout
197\*-------------------------------------------------------------------------*/ 197\*-------------------------------------------------------------------------*/
198LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count, 198int socket_send(p_socket ps, const char *data, size_t count,
199 size_t *sent, p_timeout tm) 199 size_t *sent, p_timeout tm)
200{ 200{
201 int err; 201 int err;
@@ -229,7 +229,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
229/*-------------------------------------------------------------------------*\ 229/*-------------------------------------------------------------------------*\
230* Sendto with timeout 230* Sendto with timeout
231\*-------------------------------------------------------------------------*/ 231\*-------------------------------------------------------------------------*/
232LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, 232int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
233 SA *addr, socklen_t len, p_timeout tm) 233 SA *addr, socklen_t len, p_timeout tm)
234{ 234{
235 int err; 235 int err;
@@ -254,7 +254,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
254/*-------------------------------------------------------------------------*\ 254/*-------------------------------------------------------------------------*\
255* Receive with timeout 255* Receive with timeout
256\*-------------------------------------------------------------------------*/ 256\*-------------------------------------------------------------------------*/
257LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 257int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
258 int err; 258 int err;
259 *got = 0; 259 *got = 0;
260 if (*ps == SOCKET_INVALID) return IO_CLOSED; 260 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -276,7 +276,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
276/*-------------------------------------------------------------------------*\ 276/*-------------------------------------------------------------------------*\
277* Recvfrom with timeout 277* Recvfrom with timeout
278\*-------------------------------------------------------------------------*/ 278\*-------------------------------------------------------------------------*/
279LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, 279int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
280 SA *addr, socklen_t *len, p_timeout tm) { 280 SA *addr, socklen_t *len, p_timeout tm) {
281 int err; 281 int err;
282 *got = 0; 282 *got = 0;
@@ -304,7 +304,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
304* 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
305* 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.
306\*-------------------------------------------------------------------------*/ 306\*-------------------------------------------------------------------------*/
307LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count, 307int socket_write(p_socket ps, const char *data, size_t count,
308 size_t *sent, p_timeout tm) 308 size_t *sent, p_timeout tm)
309{ 309{
310 int err; 310 int err;
@@ -339,7 +339,7 @@ LUASOCKET_PRIVATE int socket_write(p_socket ps, const char *data, size_t count,
339* Read with timeout 339* Read with timeout
340* See note for socket_write 340* See note for socket_write
341\*-------------------------------------------------------------------------*/ 341\*-------------------------------------------------------------------------*/
342LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { 342int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) {
343 int err; 343 int err;
344 *got = 0; 344 *got = 0;
345 if (*ps == SOCKET_INVALID) return IO_CLOSED; 345 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -361,7 +361,7 @@ LUASOCKET_PRIVATE int socket_read(p_socket ps, char *data, size_t count, size_t
361/*-------------------------------------------------------------------------*\ 361/*-------------------------------------------------------------------------*\
362* Put socket into blocking mode 362* Put socket into blocking mode
363\*-------------------------------------------------------------------------*/ 363\*-------------------------------------------------------------------------*/
364LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) { 364void socket_setblocking(p_socket ps) {
365 int flags = fcntl(*ps, F_GETFL, 0); 365 int flags = fcntl(*ps, F_GETFL, 0);
366 flags &= (~(O_NONBLOCK)); 366 flags &= (~(O_NONBLOCK));
367 fcntl(*ps, F_SETFL, flags); 367 fcntl(*ps, F_SETFL, flags);
@@ -370,7 +370,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
370/*-------------------------------------------------------------------------*\ 370/*-------------------------------------------------------------------------*\
371* Put socket into non-blocking mode 371* Put socket into non-blocking mode
372\*-------------------------------------------------------------------------*/ 372\*-------------------------------------------------------------------------*/
373LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) { 373void socket_setnonblocking(p_socket ps) {
374 int flags = fcntl(*ps, F_GETFL, 0); 374 int flags = fcntl(*ps, F_GETFL, 0);
375 flags |= O_NONBLOCK; 375 flags |= O_NONBLOCK;
376 fcntl(*ps, F_SETFL, flags); 376 fcntl(*ps, F_SETFL, flags);
@@ -379,7 +379,7 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
379/*-------------------------------------------------------------------------*\ 379/*-------------------------------------------------------------------------*\
380* DNS helpers 380* DNS helpers
381\*-------------------------------------------------------------------------*/ 381\*-------------------------------------------------------------------------*/
382LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 382int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
383 *hp = gethostbyaddr(addr, len, AF_INET); 383 *hp = gethostbyaddr(addr, len, AF_INET);
384 if (*hp) return IO_DONE; 384 if (*hp) return IO_DONE;
385 else if (h_errno) return h_errno; 385 else if (h_errno) return h_errno;
@@ -387,7 +387,7 @@ LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, stru
387 else return IO_UNKNOWN; 387 else return IO_UNKNOWN;
388} 388}
389 389
390LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) { 390int socket_gethostbyname(const char *addr, struct hostent **hp) {
391 *hp = gethostbyname(addr); 391 *hp = gethostbyname(addr);
392 if (*hp) return IO_DONE; 392 if (*hp) return IO_DONE;
393 else if (h_errno) return h_errno; 393 else if (h_errno) return h_errno;
@@ -399,7 +399,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
399* Error translation functions 399* Error translation functions
400* Make sure important error messages are standard 400* Make sure important error messages are standard
401\*-------------------------------------------------------------------------*/ 401\*-------------------------------------------------------------------------*/
402LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) { 402const char *socket_hoststrerror(int err) {
403 if (err <= 0) return io_strerror(err); 403 if (err <= 0) return io_strerror(err);
404 switch (err) { 404 switch (err) {
405 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; 405 case HOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@@ -407,7 +407,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
407 } 407 }
408} 408}
409 409
410LUASOCKET_PRIVATE const char *socket_strerror(int err) { 410const char *socket_strerror(int err) {
411 if (err <= 0) return io_strerror(err); 411 if (err <= 0) return io_strerror(err);
412 switch (err) { 412 switch (err) {
413 case EADDRINUSE: return PIE_ADDRINUSE; 413 case EADDRINUSE: return PIE_ADDRINUSE;
@@ -423,12 +423,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
423 } 423 }
424} 424}
425 425
426LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) { 426const char *socket_ioerror(p_socket ps, int err) {
427 (void) ps; 427 (void) ps;
428 return socket_strerror(err); 428 return socket_strerror(err);
429} 429}
430 430
431LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) { 431const char *socket_gaistrerror(int err) {
432 if (err == 0) return NULL; 432 if (err == 0) return NULL;
433 switch (err) { 433 switch (err) {
434 case EAI_AGAIN: return PIE_AGAIN; 434 case EAI_AGAIN: return PIE_AGAIN;
@@ -452,4 +452,3 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
452 default: return gai_strerror(err); 452 default: return gai_strerror(err);
453 } 453 }
454} 454}
455
diff --git a/src/wsocket.c b/src/wsocket.c
index c281058..20da330 100755
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -18,7 +18,7 @@ static const char *wstrerror(int err);
18/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
19* Initializes module 19* Initializes module
20\*-------------------------------------------------------------------------*/ 20\*-------------------------------------------------------------------------*/
21LUASOCKET_PRIVATE int socket_open(void) { 21int socket_open(void) {
22 WSADATA wsaData; 22 WSADATA wsaData;
23 WORD wVersionRequested = MAKEWORD(2, 0); 23 WORD wVersionRequested = MAKEWORD(2, 0);
24 int err = WSAStartup(wVersionRequested, &wsaData ); 24 int err = WSAStartup(wVersionRequested, &wsaData );
@@ -34,7 +34,7 @@ LUASOCKET_PRIVATE int socket_open(void) {
34/*-------------------------------------------------------------------------*\ 34/*-------------------------------------------------------------------------*\
35* Close module 35* Close module
36\*-------------------------------------------------------------------------*/ 36\*-------------------------------------------------------------------------*/
37LUASOCKET_PRIVATE int socket_close(void) { 37int socket_close(void) {
38 WSACleanup(); 38 WSACleanup();
39 return 1; 39 return 1;
40} 40}
@@ -47,7 +47,7 @@ LUASOCKET_PRIVATE int socket_close(void) {
47#define WAITFD_E 4 47#define WAITFD_E 4
48#define WAITFD_C (WAITFD_E|WAITFD_W) 48#define WAITFD_C (WAITFD_E|WAITFD_W)
49 49
50LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) { 50int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
51 int ret; 51 int ret;
52 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; 52 fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
53 struct timeval tv, *tp = NULL; 53 struct timeval tv, *tp = NULL;
@@ -75,7 +75,7 @@ LUASOCKET_PRIVATE int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
75/*-------------------------------------------------------------------------*\ 75/*-------------------------------------------------------------------------*\
76* Select with int timeout in ms 76* Select with int timeout in ms
77\*-------------------------------------------------------------------------*/ 77\*-------------------------------------------------------------------------*/
78LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, 78int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
79 p_timeout tm) { 79 p_timeout tm) {
80 struct timeval tv; 80 struct timeval tv;
81 double t = timeout_get(tm); 81 double t = timeout_get(tm);
@@ -90,7 +90,7 @@ LUASOCKET_PRIVATE int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_s
90/*-------------------------------------------------------------------------*\ 90/*-------------------------------------------------------------------------*\
91* Close and inutilize socket 91* Close and inutilize socket
92\*-------------------------------------------------------------------------*/ 92\*-------------------------------------------------------------------------*/
93LUASOCKET_PRIVATE void socket_destroy(p_socket ps) { 93void socket_destroy(p_socket ps) {
94 if (*ps != SOCKET_INVALID) { 94 if (*ps != SOCKET_INVALID) {
95 socket_setblocking(ps); /* close can take a long time on WIN32 */ 95 socket_setblocking(ps); /* close can take a long time on WIN32 */
96 closesocket(*ps); 96 closesocket(*ps);
@@ -101,7 +101,7 @@ LUASOCKET_PRIVATE void socket_destroy(p_socket ps) {
101/*-------------------------------------------------------------------------*\ 101/*-------------------------------------------------------------------------*\
102* 102*
103\*-------------------------------------------------------------------------*/ 103\*-------------------------------------------------------------------------*/
104LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) { 104void socket_shutdown(p_socket ps, int how) {
105 socket_setblocking(ps); 105 socket_setblocking(ps);
106 shutdown(*ps, how); 106 shutdown(*ps, how);
107 socket_setnonblocking(ps); 107 socket_setnonblocking(ps);
@@ -110,7 +110,7 @@ LUASOCKET_PRIVATE void socket_shutdown(p_socket ps, int how) {
110/*-------------------------------------------------------------------------*\ 110/*-------------------------------------------------------------------------*\
111* Creates and sets up a socket 111* Creates and sets up a socket
112\*-------------------------------------------------------------------------*/ 112\*-------------------------------------------------------------------------*/
113LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int protocol) { 113int socket_create(p_socket ps, int domain, int type, int protocol) {
114 *ps = socket(domain, type, protocol); 114 *ps = socket(domain, type, protocol);
115 if (*ps != SOCKET_INVALID) return IO_DONE; 115 if (*ps != SOCKET_INVALID) return IO_DONE;
116 else return WSAGetLastError(); 116 else return WSAGetLastError();
@@ -119,7 +119,7 @@ LUASOCKET_PRIVATE int socket_create(p_socket ps, int domain, int type, int proto
119/*-------------------------------------------------------------------------*\ 119/*-------------------------------------------------------------------------*\
120* Connects or returns error message 120* Connects or returns error message
121\*-------------------------------------------------------------------------*/ 121\*-------------------------------------------------------------------------*/
122LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { 122int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
123 int err; 123 int err;
124 /* don't call on closed socket */ 124 /* don't call on closed socket */
125 if (*ps == SOCKET_INVALID) return IO_CLOSED; 125 if (*ps == SOCKET_INVALID) return IO_CLOSED;
@@ -148,7 +148,7 @@ LUASOCKET_PRIVATE int socket_connect(p_socket ps, SA *addr, socklen_t len, p_tim
148/*-------------------------------------------------------------------------*\ 148/*-------------------------------------------------------------------------*\
149* Binds or returns error message 149* Binds or returns error message
150\*-------------------------------------------------------------------------*/ 150\*-------------------------------------------------------------------------*/
151LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) { 151int socket_bind(p_socket ps, SA *addr, socklen_t len) {
152 int err = IO_DONE; 152 int err = IO_DONE;
153 socket_setblocking(ps); 153 socket_setblocking(ps);
154 if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); 154 if (bind(*ps, addr, len) < 0) err = WSAGetLastError();
@@ -159,7 +159,7 @@ LUASOCKET_PRIVATE int socket_bind(p_socket ps, SA *addr, socklen_t len) {
159/*-------------------------------------------------------------------------*\ 159/*-------------------------------------------------------------------------*\
160* 160*
161\*-------------------------------------------------------------------------*/ 161\*-------------------------------------------------------------------------*/
162LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) { 162int socket_listen(p_socket ps, int backlog) {
163 int err = IO_DONE; 163 int err = IO_DONE;
164 socket_setblocking(ps); 164 socket_setblocking(ps);
165 if (listen(*ps, backlog) < 0) err = WSAGetLastError(); 165 if (listen(*ps, backlog) < 0) err = WSAGetLastError();
@@ -170,7 +170,7 @@ LUASOCKET_PRIVATE int socket_listen(p_socket ps, int backlog) {
170/*-------------------------------------------------------------------------*\ 170/*-------------------------------------------------------------------------*\
171* Accept with timeout 171* Accept with timeout
172\*-------------------------------------------------------------------------*/ 172\*-------------------------------------------------------------------------*/
173LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, 173int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len,
174 p_timeout tm) { 174 p_timeout tm) {
175 if (*ps == SOCKET_INVALID) return IO_CLOSED; 175 if (*ps == SOCKET_INVALID) return IO_CLOSED;
176 for ( ;; ) { 176 for ( ;; ) {
@@ -192,7 +192,7 @@ LUASOCKET_PRIVATE int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_
192* 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.
193* Therefore, whoever calls this function should not pass a huge buffer. 193* Therefore, whoever calls this function should not pass a huge buffer.
194\*-------------------------------------------------------------------------*/ 194\*-------------------------------------------------------------------------*/
195LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count, 195int socket_send(p_socket ps, const char *data, size_t count,
196 size_t *sent, p_timeout tm) 196 size_t *sent, p_timeout tm)
197{ 197{
198 int err; 198 int err;
@@ -220,7 +220,7 @@ LUASOCKET_PRIVATE int socket_send(p_socket ps, const char *data, size_t count,
220/*-------------------------------------------------------------------------*\ 220/*-------------------------------------------------------------------------*\
221* Sendto with timeout 221* Sendto with timeout
222\*-------------------------------------------------------------------------*/ 222\*-------------------------------------------------------------------------*/
223LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, 223int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
224 SA *addr, socklen_t len, p_timeout tm) 224 SA *addr, socklen_t len, p_timeout tm)
225{ 225{
226 int err; 226 int err;
@@ -241,7 +241,7 @@ LUASOCKET_PRIVATE int socket_sendto(p_socket ps, const char *data, size_t count,
241/*-------------------------------------------------------------------------*\ 241/*-------------------------------------------------------------------------*\
242* Receive with timeout 242* Receive with timeout
243\*-------------------------------------------------------------------------*/ 243\*-------------------------------------------------------------------------*/
244LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t *got, 244int socket_recv(p_socket ps, char *data, size_t count, size_t *got,
245 p_timeout tm) 245 p_timeout tm)
246{ 246{
247 int err, prev = IO_DONE; 247 int err, prev = IO_DONE;
@@ -270,7 +270,7 @@ LUASOCKET_PRIVATE int socket_recv(p_socket ps, char *data, size_t count, size_t
270/*-------------------------------------------------------------------------*\ 270/*-------------------------------------------------------------------------*\
271* Recvfrom with timeout 271* Recvfrom with timeout
272\*-------------------------------------------------------------------------*/ 272\*-------------------------------------------------------------------------*/
273LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, 273int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got,
274 SA *addr, socklen_t *len, p_timeout tm) 274 SA *addr, socklen_t *len, p_timeout tm)
275{ 275{
276 int err, prev = IO_DONE; 276 int err, prev = IO_DONE;
@@ -299,7 +299,7 @@ LUASOCKET_PRIVATE int socket_recvfrom(p_socket ps, char *data, size_t count, siz
299/*-------------------------------------------------------------------------*\ 299/*-------------------------------------------------------------------------*\
300* Put socket into blocking mode 300* Put socket into blocking mode
301\*-------------------------------------------------------------------------*/ 301\*-------------------------------------------------------------------------*/
302LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) { 302void socket_setblocking(p_socket ps) {
303 u_long argp = 0; 303 u_long argp = 0;
304 ioctlsocket(*ps, FIONBIO, &argp); 304 ioctlsocket(*ps, FIONBIO, &argp);
305} 305}
@@ -307,7 +307,7 @@ LUASOCKET_PRIVATE void socket_setblocking(p_socket ps) {
307/*-------------------------------------------------------------------------*\ 307/*-------------------------------------------------------------------------*\
308* Put socket into non-blocking mode 308* Put socket into non-blocking mode
309\*-------------------------------------------------------------------------*/ 309\*-------------------------------------------------------------------------*/
310LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) { 310void socket_setnonblocking(p_socket ps) {
311 u_long argp = 1; 311 u_long argp = 1;
312 ioctlsocket(*ps, FIONBIO, &argp); 312 ioctlsocket(*ps, FIONBIO, &argp);
313} 313}
@@ -315,13 +315,13 @@ LUASOCKET_PRIVATE void socket_setnonblocking(p_socket ps) {
315/*-------------------------------------------------------------------------*\ 315/*-------------------------------------------------------------------------*\
316* DNS helpers 316* DNS helpers
317\*-------------------------------------------------------------------------*/ 317\*-------------------------------------------------------------------------*/
318LUASOCKET_PRIVATE int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { 318int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) {
319 *hp = gethostbyaddr(addr, len, AF_INET); 319 *hp = gethostbyaddr(addr, len, AF_INET);
320 if (*hp) return IO_DONE; 320 if (*hp) return IO_DONE;
321 else return WSAGetLastError(); 321 else return WSAGetLastError();
322} 322}
323 323
324LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp) { 324int socket_gethostbyname(const char *addr, struct hostent **hp) {
325 *hp = gethostbyname(addr); 325 *hp = gethostbyname(addr);
326 if (*hp) return IO_DONE; 326 if (*hp) return IO_DONE;
327 else return WSAGetLastError(); 327 else return WSAGetLastError();
@@ -330,7 +330,7 @@ LUASOCKET_PRIVATE int socket_gethostbyname(const char *addr, struct hostent **hp
330/*-------------------------------------------------------------------------*\ 330/*-------------------------------------------------------------------------*\
331* Error translation functions 331* Error translation functions
332\*-------------------------------------------------------------------------*/ 332\*-------------------------------------------------------------------------*/
333LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) { 333const char *socket_hoststrerror(int err) {
334 if (err <= 0) return io_strerror(err); 334 if (err <= 0) return io_strerror(err);
335 switch (err) { 335 switch (err) {
336 case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; 336 case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND;
@@ -338,7 +338,7 @@ LUASOCKET_PRIVATE const char *socket_hoststrerror(int err) {
338 } 338 }
339} 339}
340 340
341LUASOCKET_PRIVATE const char *socket_strerror(int err) { 341const char *socket_strerror(int err) {
342 if (err <= 0) return io_strerror(err); 342 if (err <= 0) return io_strerror(err);
343 switch (err) { 343 switch (err) {
344 case WSAEADDRINUSE: return PIE_ADDRINUSE; 344 case WSAEADDRINUSE: return PIE_ADDRINUSE;
@@ -352,12 +352,12 @@ LUASOCKET_PRIVATE const char *socket_strerror(int err) {
352 } 352 }
353} 353}
354 354
355LUASOCKET_PRIVATE const char *socket_ioerror(p_socket ps, int err) { 355const char *socket_ioerror(p_socket ps, int err) {
356 (void) ps; 356 (void) ps;
357 return socket_strerror(err); 357 return socket_strerror(err);
358} 358}
359 359
360LUASOCKET_PRIVATE static const char *wstrerror(int err) { 360static const char *wstrerror(int err) {
361 switch (err) { 361 switch (err) {
362 case WSAEINTR: return "Interrupted function call"; 362 case WSAEINTR: return "Interrupted function call";
363 case WSAEACCES: return PIE_ACCESS; // "Permission denied"; 363 case WSAEACCES: return PIE_ACCESS; // "Permission denied";
@@ -406,7 +406,7 @@ LUASOCKET_PRIVATE static const char *wstrerror(int err) {
406 } 406 }
407} 407}
408 408
409LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) { 409const char *socket_gaistrerror(int err) {
410 if (err == 0) return NULL; 410 if (err == 0) return NULL;
411 switch (err) { 411 switch (err) {
412 case EAI_AGAIN: return PIE_AGAIN; 412 case EAI_AGAIN: return PIE_AGAIN;
@@ -432,4 +432,3 @@ LUASOCKET_PRIVATE const char *socket_gaistrerror(int err) {
432 default: return gai_strerror(err); 432 default: return gai_strerror(err);
433 } 433 }
434} 434}
435