aboutsummaryrefslogtreecommitdiff
path: root/src/unix.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unix.c349
1 files changed, 44 insertions, 305 deletions
diff --git a/src/unix.c b/src/unix.c
index 5bc3148..268d8b2 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -2,329 +2,68 @@
2* Unix domain socket 2* Unix domain socket
3* LuaSocket toolkit 3* LuaSocket toolkit
4\*=========================================================================*/ 4\*=========================================================================*/
5#include <string.h> 5#include "luasocket.h"
6 6
7#include "lua.h" 7#include "unixstream.h"
8#include "lauxlib.h" 8#include "unixdgram.h"
9
10#include "auxiliar.h"
11#include "socket.h"
12#include "options.h"
13#include "unix.h"
14#include <sys/un.h>
15
16/*=========================================================================*\
17* Internal function prototypes
18\*=========================================================================*/
19static int global_create(lua_State *L);
20static int meth_connect(lua_State *L);
21static int meth_listen(lua_State *L);
22static int meth_bind(lua_State *L);
23static int meth_send(lua_State *L);
24static int meth_shutdown(lua_State *L);
25static int meth_receive(lua_State *L);
26static int meth_accept(lua_State *L);
27static int meth_close(lua_State *L);
28static int meth_setoption(lua_State *L);
29static int meth_settimeout(lua_State *L);
30static int meth_getfd(lua_State *L);
31static int meth_setfd(lua_State *L);
32static int meth_dirty(lua_State *L);
33static int meth_getstats(lua_State *L);
34static int meth_setstats(lua_State *L);
35
36static const char *unix_tryconnect(p_unix un, const char *path);
37static const char *unix_trybind(p_unix un, const char *path);
38
39/* unix object methods */
40static luaL_Reg unix_methods[] = {
41 {"__gc", meth_close},
42 {"__tostring", auxiliar_tostring},
43 {"accept", meth_accept},
44 {"bind", meth_bind},
45 {"close", meth_close},
46 {"connect", meth_connect},
47 {"dirty", meth_dirty},
48 {"getfd", meth_getfd},
49 {"getstats", meth_getstats},
50 {"setstats", meth_setstats},
51 {"listen", meth_listen},
52 {"receive", meth_receive},
53 {"send", meth_send},
54 {"setfd", meth_setfd},
55 {"setoption", meth_setoption},
56 {"setpeername", meth_connect},
57 {"setsockname", meth_bind},
58 {"settimeout", meth_settimeout},
59 {"shutdown", meth_shutdown},
60 {NULL, NULL}
61};
62
63/* socket option handlers */
64static t_opt optset[] = {
65 {"keepalive", opt_set_keepalive},
66 {"reuseaddr", opt_set_reuseaddr},
67 {"linger", opt_set_linger},
68 {NULL, NULL}
69};
70 9
71/*-------------------------------------------------------------------------*\ 10/*-------------------------------------------------------------------------*\
72* Initializes module 11* Modules and functions
73\*-------------------------------------------------------------------------*/ 12\*-------------------------------------------------------------------------*/
74int luaopen_socket_unix(lua_State *L) { 13static const luaL_Reg mod[] = {
75 /* create classes */ 14 {"stream", unixstream_open},
76 auxiliar_newclass(L, "unix{master}", unix_methods); 15 {"dgram", unixdgram_open},
77 auxiliar_newclass(L, "unix{client}", unix_methods); 16 {NULL, NULL}
78 auxiliar_newclass(L, "unix{server}", unix_methods); 17};
79 /* create class groups */
80 auxiliar_add2group(L, "unix{master}", "unix{any}");
81 auxiliar_add2group(L, "unix{client}", "unix{any}");
82 auxiliar_add2group(L, "unix{server}", "unix{any}");
83 /* return the function instead of the 'socket' table */
84 lua_pushcfunction(L, global_create);
85 return 1;
86}
87
88/*=========================================================================*\
89* Lua methods
90\*=========================================================================*/
91/*-------------------------------------------------------------------------*\
92* Just call buffered IO methods
93\*-------------------------------------------------------------------------*/
94static int meth_send(lua_State *L) {
95 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
96 return buffer_meth_send(L, &un->buf);
97}
98
99static int meth_receive(lua_State *L) {
100 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
101 return buffer_meth_receive(L, &un->buf);
102}
103
104static int meth_getstats(lua_State *L) {
105 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
106 return buffer_meth_getstats(L, &un->buf);
107}
108
109static int meth_setstats(lua_State *L) {
110 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
111 return buffer_meth_setstats(L, &un->buf);
112}
113
114/*-------------------------------------------------------------------------*\
115* Just call option handler
116\*-------------------------------------------------------------------------*/
117static int meth_setoption(lua_State *L) {
118 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
119 return opt_meth_setoption(L, optset, &un->sock);
120}
121
122/*-------------------------------------------------------------------------*\
123* Select support methods
124\*-------------------------------------------------------------------------*/
125static int meth_getfd(lua_State *L) {
126 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
127 lua_pushnumber(L, (int) un->sock);
128 return 1;
129}
130
131/* this is very dangerous, but can be handy for those that are brave enough */
132static int meth_setfd(lua_State *L) {
133 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
134 un->sock = (t_socket) luaL_checknumber(L, 2);
135 return 0;
136}
137 18
138static int meth_dirty(lua_State *L) { 19static void add_alias(lua_State *L, int index, const char *name, const char *target)
139 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); 20{
140 lua_pushboolean(L, !buffer_isempty(&un->buf)); 21 lua_getfield(L, index, target);
141 return 1; 22 lua_setfield(L, index, name);
142} 23}
143 24
144/*-------------------------------------------------------------------------*\ 25static int compat_socket_unix_call(lua_State *L)
145* Waits for and returns a client object attempting connection to the 26{
146* server object 27 /* Look up socket.unix.stream in the socket.unix table (which is the first
147\*-------------------------------------------------------------------------*/ 28 * argument). */
148static int meth_accept(lua_State *L) { 29 lua_getfield(L, 1, "stream");
149 p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1);
150 p_timeout tm = timeout_markstart(&server->tm);
151 t_socket sock;
152 int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
153 /* if successful, push client socket */
154 if (err == IO_DONE) {
155 p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix));
156 auxiliar_setclass(L, "unix{client}", -1);
157 /* initialize structure fields */
158 socket_setnonblocking(&sock);
159 clnt->sock = sock;
160 io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv,
161 (p_error) socket_ioerror, &clnt->sock);
162 timeout_init(&clnt->tm, -1, -1);
163 buffer_init(&clnt->buf, &clnt->io, &clnt->tm);
164 return 1;
165 } else {
166 lua_pushnil(L);
167 lua_pushstring(L, socket_strerror(err));
168 return 2;
169 }
170}
171 30
172/*-------------------------------------------------------------------------*\ 31 /* Replace the stack entry for the socket.unix table with the
173* Binds an object to an address 32 * socket.unix.stream function. */
174\*-------------------------------------------------------------------------*/ 33 lua_replace(L, 1);
175static const char *unix_trybind(p_unix un, const char *path) {
176 struct sockaddr_un local;
177 size_t len = strlen(path);
178 int err;
179 if (len >= sizeof(local.sun_path)) return "path too long";
180 memset(&local, 0, sizeof(local));
181 strcpy(local.sun_path, path);
182 local.sun_family = AF_UNIX;
183#ifdef UNIX_HAS_SUN_LEN
184 local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
185 + len + 1;
186 err = socket_bind(&un->sock, (SA *) &local, local.sun_len);
187 34
188#else 35 /* Call socket.unix.stream, passing along any arguments. */
189 err = socket_bind(&un->sock, (SA *) &local, 36 int n = lua_gettop(L);
190 sizeof(local.sun_family) + len); 37 lua_call(L, n-1, LUA_MULTRET);
191#endif
192 if (err != IO_DONE) socket_destroy(&un->sock);
193 return socket_strerror(err);
194}
195 38
196static int meth_bind(lua_State *L) { 39 /* Pass along the return values from socket.unix.stream. */
197 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); 40 n = lua_gettop(L);
198 const char *path = luaL_checkstring(L, 2); 41 return n;
199 const char *err = unix_trybind(un, path);
200 if (err) {
201 lua_pushnil(L);
202 lua_pushstring(L, err);
203 return 2;
204 }
205 lua_pushnumber(L, 1);
206 return 1;
207} 42}
208 43
209/*-------------------------------------------------------------------------*\ 44/*-------------------------------------------------------------------------*\
210* Turns a master unix object into a client object. 45* Initializes module
211\*-------------------------------------------------------------------------*/ 46\*-------------------------------------------------------------------------*/
212static const char *unix_tryconnect(p_unix un, const char *path) 47LUASOCKET_API int luaopen_socket_unix(lua_State *L)
213{ 48{
214 struct sockaddr_un remote; 49 int i;
215 int err; 50 lua_newtable(L);
216 size_t len = strlen(path); 51 int socket_unix_table = lua_gettop(L);
217 if (len >= sizeof(remote.sun_path)) return "path too long";
218 memset(&remote, 0, sizeof(remote));
219 strcpy(remote.sun_path, path);
220 remote.sun_family = AF_UNIX;
221 timeout_markstart(&un->tm);
222#ifdef UNIX_HAS_SUN_LEN
223 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
224 + len + 1;
225 err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
226#else
227 err = socket_connect(&un->sock, (SA *) &remote,
228 sizeof(remote.sun_family) + len, &un->tm);
229#endif
230 if (err != IO_DONE) socket_destroy(&un->sock);
231 return socket_strerror(err);
232}
233 52
234static int meth_connect(lua_State *L) 53 for (i = 0; mod[i].name; i++)
235{ 54 mod[i].func(L);
236 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1);
237 const char *path = luaL_checkstring(L, 2);
238 const char *err = unix_tryconnect(un, path);
239 if (err) {
240 lua_pushnil(L);
241 lua_pushstring(L, err);
242 return 2;
243 }
244 /* turn master object into a client object */
245 auxiliar_setclass(L, "unix{client}", 1);
246 lua_pushnumber(L, 1);
247 return 1;
248}
249 55
250/*-------------------------------------------------------------------------*\ 56 /* Add backwards compatibility aliases "tcp" and "udp" for the "stream" and
251* Closes socket used by object 57 * "dgram" functions. */
252\*-------------------------------------------------------------------------*/ 58 add_alias(L, socket_unix_table, "tcp", "stream");
253static int meth_close(lua_State *L) 59 add_alias(L, socket_unix_table, "udp", "dgram");
254{
255 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
256 socket_destroy(&un->sock);
257 lua_pushnumber(L, 1);
258 return 1;
259}
260 60
261/*-------------------------------------------------------------------------*\ 61 /* Add a backwards compatibility function and a metatable setup to call it
262* Puts the sockt in listen mode 62 * for the old socket.unix() interface. */
263\*-------------------------------------------------------------------------*/ 63 lua_pushcfunction(L, compat_socket_unix_call);
264static int meth_listen(lua_State *L) 64 lua_setfield(L, socket_unix_table, "__call");
265{ 65 lua_pushvalue(L, socket_unix_table);
266 p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); 66 lua_setmetatable(L, socket_unix_table);
267 int backlog = (int) luaL_optnumber(L, 2, 32);
268 int err = socket_listen(&un->sock, backlog);
269 if (err != IO_DONE) {
270 lua_pushnil(L);
271 lua_pushstring(L, socket_strerror(err));
272 return 2;
273 }
274 /* turn master object into a server object */
275 auxiliar_setclass(L, "unix{server}", 1);
276 lua_pushnumber(L, 1);
277 return 1;
278}
279 67
280/*-------------------------------------------------------------------------*\
281* Shuts the connection down partially
282\*-------------------------------------------------------------------------*/
283static int meth_shutdown(lua_State *L)
284{
285 /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */
286 static const char* methods[] = { "receive", "send", "both", NULL };
287 p_unix tcp = (p_unix) auxiliar_checkclass(L, "unix{client}", 1);
288 int how = luaL_checkoption(L, 2, "both", methods);
289 socket_shutdown(&tcp->sock, how);
290 lua_pushnumber(L, 1);
291 return 1; 68 return 1;
292} 69}
293
294/*-------------------------------------------------------------------------*\
295* Just call tm methods
296\*-------------------------------------------------------------------------*/
297static int meth_settimeout(lua_State *L) {
298 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
299 return timeout_meth_settimeout(L, &un->tm);
300}
301
302/*=========================================================================*\
303* Library functions
304\*=========================================================================*/
305/*-------------------------------------------------------------------------*\
306* Creates a master unix object
307\*-------------------------------------------------------------------------*/
308static int global_create(lua_State *L) {
309 t_socket sock;
310 int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0);
311 /* try to allocate a system socket */
312 if (err == IO_DONE) {
313 /* allocate unix object */
314 p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
315 /* set its type as master object */
316 auxiliar_setclass(L, "unix{master}", -1);
317 /* initialize remaining structure fields */
318 socket_setnonblocking(&sock);
319 un->sock = sock;
320 io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv,
321 (p_error) socket_ioerror, &un->sock);
322 timeout_init(&un->tm, -1, -1);
323 buffer_init(&un->buf, &un->io, &un->tm);
324 return 1;
325 } else {
326 lua_pushnil(L);
327 lua_pushstring(L, socket_strerror(err));
328 return 2;
329 }
330}