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