diff options
Diffstat (limited to 'src/unix.c')
-rw-r--r-- | src/unix.c | 328 |
1 files changed, 14 insertions, 314 deletions
@@ -2,329 +2,29 @@ | |||
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 "unixtcp.h" |
11 | #include "socket.h" | 9 | #include "unixudp.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 | /*-------------------------------------------------------------------------*\ | ||
72 | * Initializes module | ||
73 | \*-------------------------------------------------------------------------*/ | ||
74 | int luaopen_socket_unix(lua_State *L) { | ||
75 | /* create classes */ | ||
76 | auxiliar_newclass(L, "unix{master}", unix_methods); | ||
77 | auxiliar_newclass(L, "unix{client}", unix_methods); | ||
78 | auxiliar_newclass(L, "unix{server}", unix_methods); | ||
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 | \*-------------------------------------------------------------------------*/ | ||
94 | static 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 | |||
99 | static 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 | |||
104 | static 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 | |||
109 | static 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 | \*-------------------------------------------------------------------------*/ | ||
117 | static 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 | \*-------------------------------------------------------------------------*/ | ||
125 | static 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 */ | ||
132 | static 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 | |||
138 | static int meth_dirty(lua_State *L) { | ||
139 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); | ||
140 | lua_pushboolean(L, !buffer_isempty(&un->buf)); | ||
141 | return 1; | ||
142 | } | ||
143 | 10 | ||
144 | /*-------------------------------------------------------------------------*\ | 11 | /*-------------------------------------------------------------------------*\ |
145 | * Waits for and returns a client object attempting connection to the | 12 | * Modules and functions |
146 | * server object | ||
147 | \*-------------------------------------------------------------------------*/ | 13 | \*-------------------------------------------------------------------------*/ |
148 | static int meth_accept(lua_State *L) { | 14 | static const luaL_Reg mod[] = { |
149 | p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); | 15 | {"tcp", unixtcp_open}, |
150 | p_timeout tm = timeout_markstart(&server->tm); | 16 | {"udp", unixudp_open}, |
151 | t_socket sock; | 17 | {NULL, NULL} |
152 | int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); | 18 | }; |
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 | |||
172 | /*-------------------------------------------------------------------------*\ | ||
173 | * Binds an object to an address | ||
174 | \*-------------------------------------------------------------------------*/ | ||
175 | static 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 | |||
188 | #else | ||
189 | err = socket_bind(&un->sock, (SA *) &local, | ||
190 | sizeof(local.sun_family) + len); | ||
191 | #endif | ||
192 | if (err != IO_DONE) socket_destroy(&un->sock); | ||
193 | return socket_strerror(err); | ||
194 | } | ||
195 | |||
196 | static int meth_bind(lua_State *L) { | ||
197 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); | ||
198 | const char *path = luaL_checkstring(L, 2); | ||
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 | } | ||
208 | |||
209 | /*-------------------------------------------------------------------------*\ | ||
210 | * Turns a master unix object into a client object. | ||
211 | \*-------------------------------------------------------------------------*/ | ||
212 | static const char *unix_tryconnect(p_unix un, const char *path) | ||
213 | { | ||
214 | struct sockaddr_un remote; | ||
215 | int err; | ||
216 | size_t len = strlen(path); | ||
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 | |||
234 | static int meth_connect(lua_State *L) | ||
235 | { | ||
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 | |||
250 | /*-------------------------------------------------------------------------*\ | ||
251 | * Closes socket used by object | ||
252 | \*-------------------------------------------------------------------------*/ | ||
253 | static int meth_close(lua_State *L) | ||
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 | |||
261 | /*-------------------------------------------------------------------------*\ | ||
262 | * Puts the sockt in listen mode | ||
263 | \*-------------------------------------------------------------------------*/ | ||
264 | static int meth_listen(lua_State *L) | ||
265 | { | ||
266 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); | ||
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 | 19 | ||
280 | /*-------------------------------------------------------------------------*\ | 20 | /*-------------------------------------------------------------------------*\ |
281 | * Shuts the connection down partially | 21 | * Initializes module |
282 | \*-------------------------------------------------------------------------*/ | 22 | \*-------------------------------------------------------------------------*/ |
283 | static int meth_shutdown(lua_State *L) | 23 | int luaopen_socket_unix(lua_State *L) |
284 | { | 24 | { |
285 | /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ | 25 | int i; |
286 | static const char* methods[] = { "receive", "send", "both", NULL }; | 26 | lua_newtable(L); |
287 | p_unix tcp = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); | 27 | for (i = 0; mod[i].name; i++) mod[i].func(L); |
288 | int how = luaL_checkoption(L, 2, "both", methods); | 28 | return 1; |
289 | socket_shutdown(&tcp->sock, how); | ||
290 | lua_pushnumber(L, 1); | ||
291 | return 1; | ||
292 | } | 29 | } |
293 | 30 | ||
294 | /*-------------------------------------------------------------------------*\ | ||
295 | * Just call tm methods | ||
296 | \*-------------------------------------------------------------------------*/ | ||
297 | static 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 | \*-------------------------------------------------------------------------*/ | ||
308 | static 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 | } | ||