Debian Codegraph @

texk/web2c/luatexdir/luasocket/src/unixdgram.c

raw
1 /*=========================================================================*\
2 * Unix domain socket dgram submodule
3 * LuaSocket toolkit
4 \*=========================================================================*/
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "lua.h"
9 #include "lauxlib.h"
10 #include "compat.h"
11
12 #include "auxiliar.h"
13 #include "socket.h"
14 #include "options.h"
15 #include "unix.h"
16 #include <sys/un.h>
17
18 #define UNIXDGRAM_DATAGRAMSIZE 8192
19
20 /*=========================================================================*\
21 * Internal function prototypes
22 \*=========================================================================*/
23 static int global_create(lua_State *L);
24 static int meth_connect(lua_State *L);
25 static int meth_bind(lua_State *L);
26 static int meth_send(lua_State *L);
27 static int meth_receive(lua_State *L);
28 static int meth_close(lua_State *L);
29 static int meth_setoption(lua_State *L);
30 static int meth_settimeout(lua_State *L);
31 static int meth_gettimeout(lua_State *L);
32 static int meth_getfd(lua_State *L);
33 static int meth_setfd(lua_State *L);
34 static int meth_dirty(lua_State *L);
35 static int meth_receivefrom(lua_State *L);
36 static int meth_sendto(lua_State *L);
37 static int meth_getsockname(lua_State *L);
38
39 static const char *unixdgram_tryconnect(p_unix un, const char *path);
40 static const char *unixdgram_trybind(p_unix un, const char *path);
41
42 /* unixdgram object methods */
43 static luaL_Reg unixdgram_methods[] = {
44 {"__gc", meth_close},
45 {"__tostring", auxiliar_tostring},
46 {"bind", meth_bind},
47 {"close", meth_close},
48 {"connect", meth_connect},
49 {"dirty", meth_dirty},
50 {"getfd", meth_getfd},
51 {"send", meth_send},
52 {"sendto", meth_sendto},
53 {"receive", meth_receive},
54 {"receivefrom", meth_receivefrom},
55 {"setfd", meth_setfd},
56 {"setoption", meth_setoption},
57 {"setpeername", meth_connect},
58 {"setsockname", meth_bind},
59 {"getsockname", meth_getsockname},
60 {"settimeout", meth_settimeout},
61 {"gettimeout", meth_gettimeout},
62 {NULL, NULL}
63 };
64
65 /* socket option handlers */
66 static t_opt optset[] = {
67 {"reuseaddr", opt_set_reuseaddr},
68 {NULL, NULL}
69 };
70
71 /* functions in library namespace */
72 static luaL_Reg func[] = {
73 {"dgram", global_create},
74 {NULL, NULL}
75 };
76
77 /*-------------------------------------------------------------------------*\
78 * Initializes module
79 \*-------------------------------------------------------------------------*/
80 int unixdgram_open(lua_State *L)
81 {
82 /* create classes */
83 auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
84 auxiliar_newclass(L, "unixdgram{unconnected}", unixdgram_methods);
85 /* create class groups */
86 auxiliar_add2group(L, "unixdgram{connected}", "unixdgram{any}");
87 auxiliar_add2group(L, "unixdgram{unconnected}", "unixdgram{any}");
88 auxiliar_add2group(L, "unixdgram{connected}", "select{able}");
89 auxiliar_add2group(L, "unixdgram{unconnected}", "select{able}");
90
91 luaL_setfuncs(L, func, 0);
92 return 0;
93 }
94
95 /*=========================================================================*\
96 * Lua methods
97 \*=========================================================================*/
98 static const char *unixdgram_strerror(int err)
99 {
100 /* a 'closed' error on an unconnected means the target address was not
101 * accepted by the transport layer */
102 if (err == IO_CLOSED) return "refused";
103 else return socket_strerror(err);
104 }
105
106 static int meth_send(lua_State *L)
107 {
108 p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{connected}", 1);
109 p_timeout tm = &un->tm;
110 size_t count, sent = 0;
111 int err;
112 const char *data = luaL_checklstring(L, 2, &count);
113 timeout_markstart(tm);
114 err = socket_send(&un->sock, data, count, &sent, tm);
115 if (err != IO_DONE) {
116 lua_pushnil(L);
117 lua_pushstring(L, unixdgram_strerror(err));
118 return 2;
119 }
120 lua_pushnumber(L, (lua_Number) sent);
121 return 1;
122 }
123
124 /*-------------------------------------------------------------------------*\
125 * Send data through unconnected unixdgram socket
126 \*-------------------------------------------------------------------------*/
127 static int meth_sendto(lua_State *L)
128 {
129 p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
130 size_t count, sent = 0;
131 const char *data = luaL_checklstring(L, 2, &count);
132 const char *path = luaL_checkstring(L, 3);
133 p_timeout tm = &un->tm;
134 int err;
135 struct sockaddr_un remote;
136 size_t len = strlen(path);
137
138 if (len >= sizeof(remote.sun_path)) {
139 lua_pushnil(L);
140 lua_pushstring(L, "path too long");
141 return 2;
142 }
143
144 memset(&remote, 0, sizeof(remote));
145 strcpy(remote.sun_path, path);
146 remote.sun_family = AF_UNIX;
147 timeout_markstart(tm);
148 #ifdef UNIX_HAS_SUN_LEN
149 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
150 + len + 1;
151 err = socket_sendto(&un->sock, data, count, &sent, (SA *) &remote, remote.sun_len, tm);
152 #else
153 err = socket_sendto(&un->sock, data, count, &sent, (SA *) &remote,
154 sizeof(remote.sun_family) + len, tm);
155 #endif
156 if (err != IO_DONE) {
157 lua_pushnil(L);
158 lua_pushstring(L, unixdgram_strerror(err));
159 return 2;
160 }
161 lua_pushnumber(L, (lua_Number) sent);
162 return 1;
163 }
164
165 static int meth_receive(lua_State *L) {
166 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
167 char buf[UNIXDGRAM_DATAGRAMSIZE];
168 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
169 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
170 int err;
171 p_timeout tm = &un->tm;
172 timeout_markstart(tm);
173 if (!dgram) {
174 lua_pushnil(L);
175 lua_pushliteral(L, "out of memory");
176 return 2;
177 }
178 err = socket_recv(&un->sock, dgram, wanted, &got, tm);
179 /* Unlike STREAM, recv() of zero is not closed, but a zero-length packet. */
180 if (err != IO_DONE && err != IO_CLOSED) {
181 lua_pushnil(L);
182 lua_pushstring(L, unixdgram_strerror(err));
183 if (wanted > sizeof(buf)) free(dgram);
184 return 2;
185 }
186 lua_pushlstring(L, dgram, got);
187 if (wanted > sizeof(buf)) free(dgram);
188 return 1;
189 }
190
191 /*-------------------------------------------------------------------------*\
192 * Receives data and sender from a DGRAM socket
193 \*-------------------------------------------------------------------------*/
194 static int meth_receivefrom(lua_State *L) {
195 p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
196 char buf[UNIXDGRAM_DATAGRAMSIZE];
197 size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
198 char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
199 struct sockaddr_un addr;
200 socklen_t addr_len = sizeof(addr);
201 int err;
202 p_timeout tm = &un->tm;
203 timeout_markstart(tm);
204 if (!dgram) {
205 lua_pushnil(L);
206 lua_pushliteral(L, "out of memory");
207 return 2;
208 }
209 addr.sun_path[0] = '\0';
210 err = socket_recvfrom(&un->sock, dgram, wanted, &got, (SA *) &addr,
211 &addr_len, tm);
212 /* Unlike STREAM, recv() of zero is not closed, but a zero-length packet. */
213 if (err != IO_DONE && err != IO_CLOSED) {
214 lua_pushnil(L);
215 lua_pushstring(L, unixdgram_strerror(err));
216 if (wanted > sizeof(buf)) free(dgram);
217 return 2;
218 }
219
220 lua_pushlstring(L, dgram, got);
221 /* the path may be empty, when client send without bind */
222 lua_pushstring(L, addr.sun_path);
223 if (wanted > sizeof(buf)) free(dgram);
224 return 2;
225 }
226
227 /*-------------------------------------------------------------------------*\
228 * Just call option handler
229 \*-------------------------------------------------------------------------*/
230 static int meth_setoption(lua_State *L) {
231 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
232 return opt_meth_setoption(L, optset, &un->sock);
233 }
234
235 /*-------------------------------------------------------------------------*\
236 * Select support methods
237 \*-------------------------------------------------------------------------*/
238 static int meth_getfd(lua_State *L) {
239 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
240 lua_pushnumber(L, (int) un->sock);
241 return 1;
242 }
243
244 /* this is very dangerous, but can be handy for those that are brave enough */
245 static int meth_setfd(lua_State *L) {
246 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
247 un->sock = (t_socket) luaL_checknumber(L, 2);
248 return 0;
249 }
250
251 static int meth_dirty(lua_State *L) {
252 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
253 (void) un;
254 lua_pushboolean(L, 0);
255 return 1;
256 }
257
258 /*-------------------------------------------------------------------------*\
259 * Binds an object to an address
260 \*-------------------------------------------------------------------------*/
261 static const char *unixdgram_trybind(p_unix un, const char *path) {
262 struct sockaddr_un local;
263 size_t len = strlen(path);
264 int err;
265 if (len >= sizeof(local.sun_path)) return "path too long";
266 memset(&local, 0, sizeof(local));
267 strcpy(local.sun_path, path);
268 local.sun_family = AF_UNIX;
269 #ifdef UNIX_HAS_SUN_LEN
270 local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
271 + len + 1;
272 err = socket_bind(&un->sock, (SA *) &local, local.sun_len);
273
274 #else
275 err = socket_bind(&un->sock, (SA *) &local,
276 sizeof(local.sun_family) + len);
277 #endif
278 if (err != IO_DONE) socket_destroy(&un->sock);
279 return socket_strerror(err);
280 }
281
282 static int meth_bind(lua_State *L)
283 {
284 p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
285 const char *path = luaL_checkstring(L, 2);
286 const char *err = unixdgram_trybind(un, path);
287 if (err) {
288 lua_pushnil(L);
289 lua_pushstring(L, err);
290 return 2;
291 }
292 lua_pushnumber(L, 1);
293 return 1;
294 }
295
296 static int meth_getsockname(lua_State *L)
297 {
298 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
299 struct sockaddr_un peer = {0};
300 socklen_t peer_len = sizeof(peer);
301
302 if (getsockname(un->sock, (SA *) &peer, &peer_len) < 0) {
303 lua_pushnil(L);
304 lua_pushstring(L, socket_strerror(errno));
305 return 2;
306 }
307
308 lua_pushstring(L, peer.sun_path);
309 return 1;
310 }
311
312 /*-------------------------------------------------------------------------*\
313 * Turns a master unixdgram object into a client object.
314 \*-------------------------------------------------------------------------*/
315 static const char *unixdgram_tryconnect(p_unix un, const char *path)
316 {
317 struct sockaddr_un remote;
318 int err;
319 size_t len = strlen(path);
320 if (len >= sizeof(remote.sun_path)) return "path too long";
321 memset(&remote, 0, sizeof(remote));
322 strcpy(remote.sun_path, path);
323 remote.sun_family = AF_UNIX;
324 timeout_markstart(&un->tm);
325 #ifdef UNIX_HAS_SUN_LEN
326 remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
327 + len + 1;
328 err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
329 #else
330 err = socket_connect(&un->sock, (SA *) &remote,
331 sizeof(remote.sun_family) + len, &un->tm);
332 #endif
333 if (err != IO_DONE) socket_destroy(&un->sock);
334 return socket_strerror(err);
335 }
336
337 static int meth_connect(lua_State *L)
338 {
339 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
340 const char *path = luaL_checkstring(L, 2);
341 const char *err = unixdgram_tryconnect(un, path);
342 if (err) {
343 lua_pushnil(L);
344 lua_pushstring(L, err);
345 return 2;
346 }
347 /* turn unconnected object into a connected object */
348 auxiliar_setclass(L, "unixdgram{connected}", 1);
349 lua_pushnumber(L, 1);
350 return 1;
351 }
352
353 /*-------------------------------------------------------------------------*\
354 * Closes socket used by object
355 \*-------------------------------------------------------------------------*/
356 static int meth_close(lua_State *L)
357 {
358 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
359 socket_destroy(&un->sock);
360 lua_pushnumber(L, 1);
361 return 1;
362 }
363
364 /*-------------------------------------------------------------------------*\
365 * Just call tm methods
366 \*-------------------------------------------------------------------------*/
367 static int meth_settimeout(lua_State *L)
368 {
369 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
370 return timeout_meth_settimeout(L, &un->tm);
371 }
372
373 static int meth_gettimeout(lua_State *L)
374 {
375 p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
376 return timeout_meth_gettimeout(L, &un->tm);
377 }
378
379 /*=========================================================================*\
380 * Library functions
381 \*=========================================================================*/
382 /*-------------------------------------------------------------------------*\
383 * Creates a master unixdgram object
384 \*-------------------------------------------------------------------------*/
385 static int global_create(lua_State *L)
386 {
387 t_socket sock;
388 int err = socket_create(&sock, AF_UNIX, SOCK_DGRAM, 0);
389 /* try to allocate a system socket */
390 if (err == IO_DONE) {
391 /* allocate unixdgram object */
392 p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
393 /* set its type as master object */
394 auxiliar_setclass(L, "unixdgram{unconnected}", -1);
395 /* initialize remaining structure fields */
396 socket_setnonblocking(&sock);
397 un->sock = sock;
398 io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv,
399 (p_error) socket_ioerror, &un->sock);
400 timeout_init(&un->tm, -1, -1);
401 buffer_init(&un->buf, &un->io, &un->tm);
402 return 1;
403 } else {
404 lua_pushnil(L);
405 lua_pushstring(L, socket_strerror(err));
406 return 2;
407 }
408 }