aboutsummaryrefslogtreecommitdiff
path: root/src/unix.c
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2015-08-21 15:39:34 -0300
committerDiego Nehab <diego.nehab@gmail.com>2015-08-21 15:39:34 -0300
commite75444ccd1f30a3b5fbc7cec4a85e831bd0560ed (patch)
tree71475c18fee070c770fc0fe25d0859b7d54c8fbb /src/unix.c
parent321c0c9b1f7b6b83cd83b58e7e259f53eca69373 (diff)
downloadluasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.gz
luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.tar.bz2
luasocket-e75444ccd1f30a3b5fbc7cec4a85e831bd0560ed.zip
New compat.h module implements luaL_setfuncs.
Makes initialization code simpler everywhere.
Diffstat (limited to 'src/unix.c')
-rw-r--r--src/unix.c56
1 files changed, 20 insertions, 36 deletions
diff --git a/src/unix.c b/src/unix.c
index 91aaaf8..5bc3148 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -1,8 +1,8 @@
1/*=========================================================================*\ 1/*=========================================================================*\
2* Unix domain socket 2* Unix domain socket
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include <string.h>
6 6
7#include "lua.h" 7#include "lua.h"
8#include "lauxlib.h" 8#include "lauxlib.h"
@@ -11,7 +11,7 @@
11#include "socket.h" 11#include "socket.h"
12#include "options.h" 12#include "options.h"
13#include "unix.h" 13#include "unix.h"
14#include <sys/un.h> 14#include <sys/un.h>
15 15
16/*=========================================================================*\ 16/*=========================================================================*\
17* Internal function prototypes 17* Internal function prototypes
@@ -68,15 +68,6 @@ static t_opt optset[] = {
68 {NULL, NULL} 68 {NULL, NULL}
69}; 69};
70 70
71/* our socket creation function */
72/* this is an ad-hoc module that returns a single function
73 * as such, do not include other functions in this array. */
74static luaL_Reg func[] = {
75 {"unix", global_create},
76 {NULL, NULL}
77};
78
79
80/*-------------------------------------------------------------------------*\ 71/*-------------------------------------------------------------------------*\
81* Initializes module 72* Initializes module
82\*-------------------------------------------------------------------------*/ 73\*-------------------------------------------------------------------------*/
@@ -89,15 +80,8 @@ int luaopen_socket_unix(lua_State *L) {
89 auxiliar_add2group(L, "unix{master}", "unix{any}"); 80 auxiliar_add2group(L, "unix{master}", "unix{any}");
90 auxiliar_add2group(L, "unix{client}", "unix{any}"); 81 auxiliar_add2group(L, "unix{client}", "unix{any}");
91 auxiliar_add2group(L, "unix{server}", "unix{any}"); 82 auxiliar_add2group(L, "unix{server}", "unix{any}");
92#if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE)
93 lua_pushcfunction(L, global_create);
94 (void) func;
95#else
96 /* set function into socket namespace */
97 luaL_openlib(L, "socket", func, 0);
98 lua_pushcfunction(L, global_create);
99#endif
100 /* return the function instead of the 'socket' table */ 83 /* return the function instead of the 'socket' table */
84 lua_pushcfunction(L, global_create);
101 return 1; 85 return 1;
102} 86}
103 87
@@ -147,7 +131,7 @@ static int meth_getfd(lua_State *L) {
147/* this is very dangerous, but can be handy for those that are brave enough */ 131/* this is very dangerous, but can be handy for those that are brave enough */
148static int meth_setfd(lua_State *L) { 132static int meth_setfd(lua_State *L) {
149 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); 133 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
150 un->sock = (t_socket) luaL_checknumber(L, 2); 134 un->sock = (t_socket) luaL_checknumber(L, 2);
151 return 0; 135 return 0;
152} 136}
153 137
@@ -158,8 +142,8 @@ static int meth_dirty(lua_State *L) {
158} 142}
159 143
160/*-------------------------------------------------------------------------*\ 144/*-------------------------------------------------------------------------*\
161* Waits for and returns a client object attempting connection to the 145* Waits for and returns a client object attempting connection to the
162* server object 146* server object
163\*-------------------------------------------------------------------------*/ 147\*-------------------------------------------------------------------------*/
164static int meth_accept(lua_State *L) { 148static int meth_accept(lua_State *L) {
165 p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); 149 p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1);
@@ -173,20 +157,20 @@ static int meth_accept(lua_State *L) {
173 /* initialize structure fields */ 157 /* initialize structure fields */
174 socket_setnonblocking(&sock); 158 socket_setnonblocking(&sock);
175 clnt->sock = sock; 159 clnt->sock = sock;
176 io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, 160 io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv,
177 (p_error) socket_ioerror, &clnt->sock); 161 (p_error) socket_ioerror, &clnt->sock);
178 timeout_init(&clnt->tm, -1, -1); 162 timeout_init(&clnt->tm, -1, -1);
179 buffer_init(&clnt->buf, &clnt->io, &clnt->tm); 163 buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
180 return 1; 164 return 1;
181 } else { 165 } else {
182 lua_pushnil(L); 166 lua_pushnil(L);
183 lua_pushstring(L, socket_strerror(err)); 167 lua_pushstring(L, socket_strerror(err));
184 return 2; 168 return 2;
185 } 169 }
186} 170}
187 171
188/*-------------------------------------------------------------------------*\ 172/*-------------------------------------------------------------------------*\
189* Binds an object to an address 173* Binds an object to an address
190\*-------------------------------------------------------------------------*/ 174\*-------------------------------------------------------------------------*/
191static const char *unix_trybind(p_unix un, const char *path) { 175static const char *unix_trybind(p_unix un, const char *path) {
192 struct sockaddr_un local; 176 struct sockaddr_un local;
@@ -197,16 +181,16 @@ static const char *unix_trybind(p_unix un, const char *path) {
197 strcpy(local.sun_path, path); 181 strcpy(local.sun_path, path);
198 local.sun_family = AF_UNIX; 182 local.sun_family = AF_UNIX;
199#ifdef UNIX_HAS_SUN_LEN 183#ifdef UNIX_HAS_SUN_LEN
200 local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) 184 local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
201 + len + 1; 185 + len + 1;
202 err = socket_bind(&un->sock, (SA *) &local, local.sun_len); 186 err = socket_bind(&un->sock, (SA *) &local, local.sun_len);
203 187
204#else 188#else
205 err = socket_bind(&un->sock, (SA *) &local, 189 err = socket_bind(&un->sock, (SA *) &local,
206 sizeof(local.sun_family) + len); 190 sizeof(local.sun_family) + len);
207#endif 191#endif
208 if (err != IO_DONE) socket_destroy(&un->sock); 192 if (err != IO_DONE) socket_destroy(&un->sock);
209 return socket_strerror(err); 193 return socket_strerror(err);
210} 194}
211 195
212static int meth_bind(lua_State *L) { 196static int meth_bind(lua_State *L) {
@@ -236,11 +220,11 @@ static const char *unix_tryconnect(p_unix un, const char *path)
236 remote.sun_family = AF_UNIX; 220 remote.sun_family = AF_UNIX;
237 timeout_markstart(&un->tm); 221 timeout_markstart(&un->tm);
238#ifdef UNIX_HAS_SUN_LEN 222#ifdef UNIX_HAS_SUN_LEN
239 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) 223 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
240 + len + 1; 224 + len + 1;
241 err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); 225 err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
242#else 226#else
243 err = socket_connect(&un->sock, (SA *) &remote, 227 err = socket_connect(&un->sock, (SA *) &remote,
244 sizeof(remote.sun_family) + len, &un->tm); 228 sizeof(remote.sun_family) + len, &un->tm);
245#endif 229#endif
246 if (err != IO_DONE) socket_destroy(&un->sock); 230 if (err != IO_DONE) socket_destroy(&un->sock);
@@ -264,7 +248,7 @@ static int meth_connect(lua_State *L)
264} 248}
265 249
266/*-------------------------------------------------------------------------*\ 250/*-------------------------------------------------------------------------*\
267* Closes socket used by object 251* Closes socket used by object
268\*-------------------------------------------------------------------------*/ 252\*-------------------------------------------------------------------------*/
269static int meth_close(lua_State *L) 253static int meth_close(lua_State *L)
270{ 254{
@@ -319,13 +303,13 @@ static int meth_settimeout(lua_State *L) {
319* Library functions 303* Library functions
320\*=========================================================================*/ 304\*=========================================================================*/
321/*-------------------------------------------------------------------------*\ 305/*-------------------------------------------------------------------------*\
322* Creates a master unix object 306* Creates a master unix object
323\*-------------------------------------------------------------------------*/ 307\*-------------------------------------------------------------------------*/
324static int global_create(lua_State *L) { 308static int global_create(lua_State *L) {
325 t_socket sock; 309 t_socket sock;
326 int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); 310 int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0);
327 /* try to allocate a system socket */ 311 /* try to allocate a system socket */
328 if (err == IO_DONE) { 312 if (err == IO_DONE) {
329 /* allocate unix object */ 313 /* allocate unix object */
330 p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); 314 p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
331 /* set its type as master object */ 315 /* set its type as master object */
@@ -333,7 +317,7 @@ static int global_create(lua_State *L) {
333 /* initialize remaining structure fields */ 317 /* initialize remaining structure fields */
334 socket_setnonblocking(&sock); 318 socket_setnonblocking(&sock);
335 un->sock = sock; 319 un->sock = sock;
336 io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, 320 io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv,
337 (p_error) socket_ioerror, &un->sock); 321 (p_error) socket_ioerror, &un->sock);
338 timeout_init(&un->tm, -1, -1); 322 timeout_init(&un->tm, -1, -1);
339 buffer_init(&un->buf, &un->io, &un->tm); 323 buffer_init(&un->buf, &un->io, &un->tm);