moonbridge

view moonbridge_io.c @ 141:845e3dce3774

Fixed wrong treatment of Lua string buffer in drain mode
author jbe
date Fri May 01 04:05:31 2015 +0200 (2015-05-01)
parents 9ca22af4d4b1
children 8cd9acda3853
line source
2 #if defined(__linux__)
3 #define _GNU_SOURCE
4 #endif
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <signal.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 #include <netinet/in.h>
16 #include <netinet/tcp.h>
17 #include <sys/select.h>
18 #include <time.h>
19 #include <netdb.h>
20 #include <arpa/inet.h>
22 #include <lua.h>
23 #include <lauxlib.h>
24 #include <lualib.h>
26 #define MOONBR_IO_MAXSTRERRORLEN 80
27 #define MOONBR_IO_READBUFLEN 4096
28 #define MOONBR_IO_WRITEBUFLEN 4096
30 #define MOONBR_IO_LISTEN_BACKLOG 1024
32 #define moonbr_io_errmsg() \
33 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
34 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
36 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
37 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
38 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
40 typedef struct {
41 int fd;
42 int issock;
43 sa_family_t addrfam;
44 int finished;
45 int closed;
46 int nonblocking;
47 int nopush;
48 int readerr;
49 int readbufin;
50 int readbufout;
51 int writeerr;
52 size_t writeleft;
53 size_t flushedleft;
54 #if LUA_VERSION_NUM >= 503
55 lua_Integer writeqin;
56 lua_Integer writeqout;
57 #else
58 int writeqin;
59 int writeqout;
60 #endif
61 size_t writeqoff;
62 int writebufin;
63 int writebufout;
64 char readbuf[MOONBR_IO_READBUFLEN];
65 char writebuf[MOONBR_IO_WRITEBUFLEN];
66 } moonbr_io_handle_t;
68 typedef struct {
69 int fd;
70 sa_family_t addrfam;
71 int nonblocking;
72 } moonbr_io_listener_t;
74 static int moonbr_io_yield(lua_State *L) {
75 return lua_yield(L, 0);
76 }
78 #if LUA_VERSION_NUM >= 503
79 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
80 #else
81 static int moonbr_io_cont_returnall(lua_State *L) {
82 #endif
83 return lua_gettop(L);
84 }
86 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
87 int flags;
88 if (handle->nonblocking == nonblocking) return;
89 flags = fcntl(handle->fd, F_GETFL, 0);
90 if (flags == -1) {
91 moonbr_io_errmsg();
92 close(handle->fd);
93 handle->fd = -1;
94 handle->closed = 1;
95 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
96 }
97 if (nonblocking) flags |= O_NONBLOCK;
98 else flags &= ~O_NONBLOCK;
99 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
100 moonbr_io_errmsg();
101 close(handle->fd);
102 handle->fd = -1;
103 handle->closed = 1;
104 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
105 }
106 handle->nonblocking = nonblocking;
107 }
109 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
110 struct linger lingerval = { 0, };
111 if (!handle->issock) return;
112 if (timeout >= 0) {
113 lingerval.l_onoff = 1;
114 lingerval.l_linger = timeout;
115 }
116 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
117 moonbr_io_errmsg();
118 close(handle->fd);
119 handle->fd = -1;
120 handle->closed = 1;
121 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
122 }
123 }
125 static inline void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
126 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
127 if (
128 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
129 handle->nopush == nopush
130 ) return;
131 #if defined(TCP_NOPUSH)
132 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
133 #elif defined(TCP_CORK)
134 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
135 #endif
136 moonbr_io_errmsg();
137 close(handle->fd);
138 handle->fd = -1;
139 handle->closed = 1;
140 #if defined(TCP_NOPUSH)
141 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
142 #elif defined(TCP_CORK)
143 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
144 #endif
145 }
146 handle->nopush = nopush;
147 #else
148 #warning Neither TCP_NOPUSH nor TCP_CORK is available
149 #endif
150 }
152 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
153 moonbr_io_handle_t *handle;
154 lua_Integer maxread;
155 const char *terminatorstr;
156 size_t terminatorlen;
157 char terminator = 0; /* initialize to avoid compiler warning */
158 luaL_Buffer luabuf;
159 size_t luabufcnt = 0;
160 int remaining;
161 char *terminatorpos;
162 ssize_t bytesread;
163 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
164 maxread = luaL_optinteger(L, 2, 0);
165 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
166 if (terminatorlen) {
167 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
168 terminator = terminatorstr[0];
169 }
170 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
171 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
172 if (handle->readerr) {
173 lua_pushnil(L);
174 lua_pushliteral(L, "Previous read error");
175 return 2;
176 }
177 handle->readerr = 1;
178 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
179 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
180 if (!drain) luaL_buffinit(L, &luabuf);
181 while (1) {
182 remaining = -1;
183 if (
184 maxread > 0 &&
185 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
186 ) {
187 remaining = maxread - luabufcnt;
188 } else if (terminatorlen) {
189 terminatorpos = memchr(
190 handle->readbuf + handle->readbufout,
191 terminator,
192 handle->readbufin - handle->readbufout
193 );
194 if (terminatorpos) remaining = 1 + (
195 terminatorpos - (handle->readbuf + handle->readbufout)
196 );
197 }
198 if (remaining >= 0) {
199 if (!drain) {
200 luaL_addlstring(
201 &luabuf,
202 handle->readbuf + handle->readbufout,
203 remaining
204 );
205 luaL_pushresult(&luabuf);
206 } else {
207 lua_pushinteger(L, luabufcnt + remaining);
208 }
209 handle->readbufout += remaining;
210 if (handle->readbufout == handle->readbufin) {
211 handle->readbufin = 0;
212 handle->readbufout =0;
213 }
214 handle->readerr = 0;
215 return 1;
216 }
217 if (!drain) luaL_addlstring(
218 &luabuf,
219 handle->readbuf + handle->readbufout,
220 handle->readbufin - handle->readbufout
221 );
222 luabufcnt += handle->readbufin - handle->readbufout;
223 do {
224 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
225 } while (bytesread < 0 && (errno == EINTR));
226 if (
227 bytesread == 0 || (
228 nonblocking &&
229 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
230 )
231 ) {
232 handle->readbufin = 0;
233 handle->readbufout = 0;
234 if (!drain) {
235 luaL_pushresult(&luabuf);
236 if (!luabufcnt && bytesread == 0) {
237 lua_pop(L, 1);
238 moonbr_io_read_impl_eof:
239 lua_pushboolean(L, 0);
240 lua_pushliteral(L, "End of file");
241 handle->readerr = 0;
242 return 2;
243 }
244 } else {
245 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
246 else lua_pushboolean(L, luabufcnt);
247 }
248 handle->readerr = 0;
249 return 1;
250 }
251 if (bytesread < 0) {
252 moonbr_io_errmsg();
253 lua_pushnil(L);
254 lua_pushstring(L, errmsg);
255 return 2;
256 }
257 handle->readbufin = bytesread;
258 handle->readbufout = 0;
259 }
260 }
262 static int moonbr_io_read(lua_State *L) {
263 return moonbr_io_read_impl(L, 0, 0);
264 }
266 static int moonbr_io_read_nb(lua_State *L) {
267 return moonbr_io_read_impl(L, 1, 0);
268 }
270 static int moonbr_io_drain(lua_State *L) {
271 return moonbr_io_read_impl(L, 0, 1);
272 }
274 static int moonbr_io_drain_nb(lua_State *L) {
275 return moonbr_io_read_impl(L, 1, 1);
276 }
278 #if LUA_VERSION_NUM >= 503
279 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
280 #else
281 static int moonbr_io_read_cont(lua_State *L) {
282 #endif
283 size_t len, remaining;
284 const char *terminatorstr = NULL;
285 size_t terminatorlen = 0;
286 const char *chunk;
287 size_t chunklen;
288 #if !(LUA_VERSION_NUM >= 503)
289 int ctx = 0;
290 lua_getctx(L, &ctx);
291 #endif
292 remaining = lua_tointeger(L, 3);
293 while (1) {
294 lua_pushcfunction(L, moonbr_io_read_nb);
295 lua_pushvalue(L, 1);
296 lua_pushvalue(L, 3);
297 lua_pushvalue(L, 4);
298 lua_call(L, 3, 2);
299 if (lua_isnil(L, -2)) {
300 return 2;
301 } else if (!lua_toboolean(L, -2)) {
302 if (ctx) {
303 lua_pushnil(L);
304 lua_pushliteral(L, "Unexpected EOF");
305 }
306 return 2;
307 }
308 len = lua_rawlen(L, -2);
309 if (!len) {
310 lua_pop(L, 2);
311 } else {
312 lua_pop(L, 1);
313 if (!terminatorstr) {
314 terminatorstr = lua_tolstring(L, 4, &terminatorlen);
315 if (!terminatorstr) terminatorstr = "";
316 }
317 if (terminatorlen) chunk = lua_tolstring(L, -1, &chunklen);
318 if (ctx == 0) {
319 lua_replace(L, 5);
320 ctx = 1;
321 } else if (ctx == 1) {
322 lua_pushvalue(L, 5);
323 lua_newtable(L);
324 lua_replace(L, 5);
325 lua_rawseti(L, 5, 2);
326 lua_rawseti(L, 5, 1);
327 ctx = 2;
328 } else {
329 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
330 }
331 if (remaining) {
332 if (len >= remaining) break;
333 remaining -= len;
334 lua_pushinteger(L, remaining);
335 lua_replace(L, 3);
336 }
337 if (terminatorlen && chunk[chunklen-1] == terminatorstr[0]) break;
338 }
339 lua_pushvalue(L, 2);
340 lua_pushvalue(L, 1);
341 lua_pushliteral(L, "r");
342 lua_callk(L, 2, 0, ctx, moonbr_io_read_cont);
343 }
344 if (ctx == 1) {
345 lua_pushvalue(L, 5);
346 } else {
347 luaL_Buffer buf;
348 lua_Integer i, chunkcount;
349 chunkcount = lua_rawlen(L, 5);
350 luaL_buffinit(L, &buf);
351 for (i=1; i<=chunkcount && i>0; i++) {
352 lua_rawgeti(L, 5, i);
353 luaL_addvalue(&buf);
354 }
355 luaL_pushresult(&buf);
356 }
357 return 1;
358 }
360 static int moonbr_io_read_call(lua_State *L) {
361 lua_settop(L, 4);
362 lua_pushnil(L);
363 #if LUA_VERSION_NUM >= 503
364 return moonbr_io_read_cont(L, 0, 0);
365 #else
366 return moonbr_io_read_cont(L);
367 #endif
368 }
370 static int moonbr_io_read_yield(lua_State *L) {
371 int args;
372 lua_pushcfunction(L, moonbr_io_read_call);
373 lua_insert(L, 1);
374 args = lua_gettop(L);
375 lua_pushcfunction(L, moonbr_io_yield);
376 lua_insert(L, 3);
377 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall);
378 return lua_gettop(L);
379 }
381 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
382 moonbr_io_handle_t *handle;
383 int i, top;
384 const char *str;
385 size_t strlen;
386 ssize_t written;
387 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
388 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
389 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
390 if (handle->writeerr) {
391 lua_pushnil(L);
392 lua_pushliteral(L, "Previous write error");
393 return 2;
394 }
395 handle->writeerr = 1;
396 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
397 top = lua_gettop(L);
398 lua_getuservalue(L, 1);
399 lua_getfield(L, -1, "writequeue");
400 for (i=2; i<=top; i++) {
401 luaL_checklstring(L, i, &strlen);
402 lua_pushvalue(L, i);
403 lua_rawseti(L, -2, handle->writeqin++);
404 handle->writeleft += strlen;
405 }
406 if (flush) handle->flushedleft = handle->writeleft;
407 while (handle->writeqout != handle->writeqin) {
408 lua_rawgeti(L, -1, handle->writeqout);
409 str = lua_tolstring(L, -1, &strlen);
410 while (handle->writeqoff < strlen) {
411 if (
412 strlen - handle->writeqoff <
413 MOONBR_IO_WRITEBUFLEN - handle->writebufin
414 ) {
415 memcpy(
416 handle->writebuf + handle->writebufin,
417 str + handle->writeqoff,
418 strlen - handle->writeqoff
419 );
420 handle->writebufin += strlen - handle->writeqoff;
421 break;
422 } else {
423 memcpy(
424 handle->writebuf + handle->writebufin,
425 str + handle->writeqoff,
426 MOONBR_IO_WRITEBUFLEN - handle->writebufin
427 );
428 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
429 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
430 moonbr_io_handle_set_nopush(L, handle, 1);
431 written = write(
432 handle->fd,
433 handle->writebuf + handle->writebufout,
434 MOONBR_IO_WRITEBUFLEN - handle->writebufout
435 );
436 if (written < 0) {
437 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
438 goto moonbr_io_write_impl_block;
439 } else if (errno != EINTR) {
440 moonbr_io_errmsg();
441 lua_pushnil(L);
442 lua_pushstring(L, errmsg);
443 return 2;
444 }
445 } else {
446 handle->writebufout += written;
447 handle->writeleft -= written;
448 if (handle->flushedleft) {
449 if (written >= handle->flushedleft) {
450 handle->flushedleft = 0;
451 moonbr_io_handle_set_nopush(L, handle, 0);
452 } else {
453 handle->flushedleft -= written;
454 }
455 }
456 }
457 }
458 handle->writebufin = 0;
459 handle->writebufout = 0;
460 }
461 }
462 handle->writeqoff = 0;
463 lua_pop(L, 1);
464 lua_pushnil(L);
465 lua_rawseti(L, -2, handle->writeqout++);
466 }
467 while (handle->flushedleft) {
468 moonbr_io_handle_set_nopush(L, handle, 1);
469 written = write(
470 handle->fd,
471 handle->writebuf + handle->writebufout,
472 handle->writebufin - handle->writebufout
473 );
474 if (written < 0) {
475 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
476 goto moonbr_io_write_impl_block;
477 } else if (errno != EINTR) {
478 moonbr_io_errmsg();
479 lua_pushnil(L);
480 lua_pushstring(L, errmsg);
481 return 2;
482 }
483 } else {
484 handle->writebufout += written;
485 handle->writeleft -= written;
486 if (handle->flushedleft) {
487 if (written >= handle->flushedleft) {
488 handle->flushedleft = 0;
489 moonbr_io_handle_set_nopush(L, handle, 0);
490 } else {
491 handle->flushedleft -= written;
492 }
493 }
494 }
495 }
496 if (handle->writebufout == handle->writebufin) {
497 handle->writebufin = 0;
498 handle->writebufout = 0;
499 }
500 if (nonblocking) lua_pushinteger(L, 0);
501 else lua_pushvalue(L, 1);
502 handle->writeerr = 0;
503 return 1;
504 moonbr_io_write_impl_block:
505 lua_pushinteger(L, handle->writeleft);
506 handle->writeerr = 0;
507 return 1;
508 }
510 static int moonbr_io_write(lua_State *L) {
511 return moonbr_io_write_impl(L, 0, 0);
512 }
514 static int moonbr_io_write_nb(lua_State *L) {
515 return moonbr_io_write_impl(L, 1, 0);
516 }
518 static int moonbr_io_flush(lua_State *L) {
519 return moonbr_io_write_impl(L, 0, 1);
520 }
522 static int moonbr_io_flush_nb(lua_State *L) {
523 return moonbr_io_write_impl(L, 1, 1);
524 }
526 static int moonbr_io_finish(lua_State *L) {
527 moonbr_io_handle_t *handle;
528 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
529 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
530 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
531 if (handle->writeleft) {
532 lua_pushcfunction(L, moonbr_io_flush);
533 lua_pushvalue(L, 1);
534 if (lua_pcall(L, 1, 2, 0)) {
535 handle->finished = 1;
536 lua_error(L);
537 }
538 if (!lua_toboolean(L, -2)) {
539 handle->finished = 1;
540 return 2;
541 }
542 }
543 handle->finished = 1;
544 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
545 if (shutdown(handle->fd, SHUT_WR)) {
546 moonbr_io_errmsg();
547 lua_pushnil(L);
548 lua_pushstring(L, errmsg);
549 return 2;
550 }
551 } else {
552 if (close(handle->fd)) {
553 moonbr_io_errmsg();
554 handle->fd = -1;
555 lua_pushnil(L);
556 lua_pushstring(L, errmsg);
557 return 2;
558 }
559 handle->fd = -1; /* fake EOF on read */
560 }
561 lua_pushboolean(L, 1);
562 return 1;
563 }
565 static int moonbr_io_close_impl(lua_State *L, int reset) {
566 moonbr_io_handle_t *handle;
567 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
568 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
569 if (!reset) {
570 if (handle->writeleft) {
571 lua_pushcfunction(L, moonbr_io_flush);
572 lua_pushvalue(L, 1);
573 if (lua_pcall(L, 1, 2, 0)) {
574 handle->closed = 1;
575 close(handle->fd);
576 handle->fd = -1;
577 lua_error(L);
578 }
579 handle->closed = 1;
580 if (!lua_toboolean(L, -2)) {
581 close(handle->fd);
582 handle->fd = -1;
583 return 2;
584 }
585 } else {
586 handle->closed = 1;
587 moonbr_io_handle_set_linger(L, handle, -1);
588 }
589 } else {
590 handle->closed = 1;
591 }
592 if (handle->fd >= 0) {
593 if (close(handle->fd)) {
594 moonbr_io_errmsg();
595 handle->fd = -1;
596 lua_pushnil(L);
597 lua_pushstring(L, errmsg);
598 return 2;
599 }
600 handle->fd = -1;
601 }
602 lua_pushboolean(L, 1);
603 return 1;
605 }
607 static int moonbr_io_close(lua_State *L) {
608 return moonbr_io_close_impl(L, 0);
609 }
611 static int moonbr_io_reset(lua_State *L) {
612 return moonbr_io_close_impl(L, 1);
613 }
615 static int moonbr_io_handlegc(lua_State *L) {
616 moonbr_io_handle_t *handle;
617 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
618 if (handle->fd >= 0) {
619 lua_pushcfunction(L, moonbr_io_close);
620 lua_pushvalue(L, 1);
621 lua_pushinteger(L, 0);
622 lua_call(L, 2, 0);
623 }
624 return 0;
625 }
627 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
628 moonbr_io_handle_t *handle;
629 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
630 if (!handle->closed) {
631 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
632 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
633 lua_call(L, 1, 0);
634 }
635 }
637 void moonbr_io_pushhandle(lua_State *L, int fd) {
638 moonbr_io_handle_t *handle;
639 struct sockaddr addr;
640 socklen_t addrlen;
641 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
642 handle->fd = fd;
643 addrlen = sizeof(addr);
644 if (getsockname(fd, &addr, &addrlen)) {
645 if (errno != ENOTSOCK) {
646 moonbr_io_errmsg();
647 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
648 }
649 handle->issock = 0;
650 } else {
651 handle->issock = 1;
652 handle->addrfam = addr.sa_family;
653 }
654 handle->finished = 0;
655 handle->closed = 0;
656 handle->nonblocking = -1;
657 handle->nopush = -1;
658 handle->readerr = 0;
659 handle->readbufin = 0;
660 handle->readbufout = 0;
661 handle->writeerr = 0;
662 handle->writeleft = 0;
663 handle->flushedleft = 0;
664 handle->writeqin = 0;
665 handle->writeqout = 0;
666 handle->writeqoff = 0;
667 handle->writebufin = 0;
668 handle->writebufout = 0;
669 moonbr_io_handle_set_linger(L, handle, 0);
670 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
671 lua_setmetatable(L, -2);
672 lua_newtable(L); // uservalue
673 lua_newtable(L);
674 lua_setfield(L, -2, "writequeue");
675 lua_newtable(L); // public
676 if (handle->addrfam == AF_INET6) {
677 struct sockaddr_in6 addr_in6;
678 char addrstrbuf[INET6_ADDRSTRLEN];
679 const char *addrstr;
680 addrlen = sizeof(addr_in6);
681 if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
682 moonbr_io_errmsg();
683 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
684 }
685 if (addrlen > sizeof(addr_in6)) {
686 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
687 }
688 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
689 if (!addrstr) {
690 moonbr_io_errmsg();
691 luaL_error(L, "Could not format local IP address: %s", errmsg);
692 } else {
693 lua_pushstring(L, addrstr);
694 lua_setfield(L, -2, "local_ip6");
695 }
696 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
697 lua_setfield(L, -2, "local_tcpport");
698 if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
699 moonbr_io_errmsg();
700 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
701 }
702 if (addrlen > sizeof(addr_in6)) {
703 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
704 }
705 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
706 if (!addrstr) {
707 moonbr_io_errmsg();
708 luaL_error(L, "Could not format remote IP address: %s", errmsg);
709 } else {
710 lua_pushstring(L, addrstr);
711 lua_setfield(L, -2, "remote_ip6");
712 }
713 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
714 lua_setfield(L, -2, "remote_tcpport");
715 } else if (handle->addrfam == AF_INET) {
716 struct sockaddr_in addr_in;
717 char addrstrbuf[INET_ADDRSTRLEN];
718 const char *addrstr;
719 addrlen = sizeof(addr_in);
720 if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) {
721 moonbr_io_errmsg();
722 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
723 }
724 if (addrlen > sizeof(addr_in)) {
725 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
726 }
727 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
728 if (!addrstr) {
729 moonbr_io_errmsg();
730 luaL_error(L, "Could not format local IP address: %s", errmsg);
731 } else {
732 lua_pushstring(L, addrstr);
733 lua_setfield(L, -2, "local_ip4");
734 }
735 lua_pushinteger(L, ntohs(addr_in.sin_port));
736 lua_setfield(L, -2, "local_tcpport");
737 if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) {
738 moonbr_io_errmsg();
739 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
740 }
741 if (addrlen > sizeof(addr_in)) {
742 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
743 }
744 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
745 if (!addrstr) {
746 moonbr_io_errmsg();
747 luaL_error(L, "Could not format remote IP address: %s", errmsg);
748 } else {
749 lua_pushstring(L, addrstr);
750 lua_setfield(L, -2, "remote_ip4");
751 }
752 lua_pushinteger(L, ntohs(addr_in.sin_port));
753 lua_setfield(L, -2, "remote_tcpport");
754 }
755 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
756 lua_setmetatable(L, -2);
757 lua_setfield(L, -2, "public");
758 lua_setuservalue(L, -2);
759 }
761 static int moonbr_io_handleindex(lua_State *L) {
762 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
763 lua_getuservalue(L, 1);
764 lua_getfield(L, -1, "public");
765 lua_pushvalue(L, 2);
766 lua_gettable(L, -2);
767 return 1;
768 }
770 static int moonbr_io_handlenewindex(lua_State *L) {
771 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
772 lua_getuservalue(L, 1);
773 lua_getfield(L, -1, "public");
774 lua_pushvalue(L, 2);
775 lua_pushvalue(L, 3);
776 lua_settable(L, -3);
777 return 0;
778 }
780 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
781 const char *path;
782 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
783 const int path_maxlen = sizeof(struct sockaddr_un) - (
784 (void *)sockaddr.sun_path - (void *)&sockaddr
785 ) - 1; /* one byte for termination */
786 int sock;
787 path = luaL_checkstring(L, 1);
788 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
789 strcpy(sockaddr.sun_path, path);
790 sock = socket(
791 PF_LOCAL,
792 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
793 0
794 );
795 if (sock < 0) {
796 moonbr_io_errmsg();
797 lua_pushnil(L);
798 lua_pushstring(L, errmsg);
799 return 2;
800 }
801 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
802 if (!nonblocking && errno == EINTR) {
803 moonbr_io_errmsg();
804 close(sock);
805 lua_pushnil(L);
806 lua_pushstring(L, errmsg);
807 return 2;
808 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
809 moonbr_io_errmsg();
810 lua_pushnil(L);
811 lua_pushstring(L, errmsg);
812 return 2;
813 }
814 }
815 moonbr_io_pushhandle(L, sock);
816 return 1;
817 }
819 static int moonbr_io_localconnect(lua_State *L) {
820 return moonbr_io_localconnect_impl(L, 0);
821 }
823 static int moonbr_io_localconnect_nb(lua_State *L) {
824 return moonbr_io_localconnect_impl(L, 1);
825 }
827 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
828 const char *host, *port;
829 struct addrinfo hints = { 0, };
830 struct addrinfo *res, *addrinfo;
831 int errcode;
832 int sock;
833 host = luaL_checkstring(L, 1);
834 port = luaL_checkstring(L, 2);
835 hints.ai_family = AF_UNSPEC;
836 hints.ai_socktype = SOCK_STREAM;
837 hints.ai_protocol = IPPROTO_TCP;
838 hints.ai_flags = AI_ADDRCONFIG;
839 errcode = getaddrinfo(host, port, &hints, &res);
840 if (errcode) {
841 freeaddrinfo(res);
842 if (errcode == EAI_SYSTEM) {
843 moonbr_io_errmsg();
844 lua_pushnil(L);
845 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
846 } else {
847 lua_pushnil(L);
848 lua_pushstring(L, gai_strerror(errcode));
849 }
850 return 2;
851 }
852 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
853 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
854 }
855 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
856 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
857 }
858 addrinfo = res;
859 moonbr_io_tcpconnect_found:
860 sock = socket(
861 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
862 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
863 addrinfo->ai_protocol
864 );
865 if (sock < 0) {
866 moonbr_io_errmsg();
867 freeaddrinfo(res);
868 lua_pushnil(L);
869 lua_pushstring(L, errmsg);
870 return 2;
871 }
872 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
873 freeaddrinfo(res);
874 if (!nonblocking && errno == EINTR) {
875 moonbr_io_errmsg();
876 close(sock);
877 lua_pushnil(L);
878 lua_pushstring(L, errmsg);
879 return 2;
880 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
881 moonbr_io_errmsg();
882 lua_pushnil(L);
883 lua_pushstring(L, errmsg);
884 return 2;
885 }
886 } else {
887 freeaddrinfo(res);
888 }
889 moonbr_io_pushhandle(L, sock);
890 return 1;
891 }
893 static int moonbr_io_tcpconnect(lua_State *L) {
894 return moonbr_io_tcpconnect_impl(L, 0);
895 }
897 static int moonbr_io_tcpconnect_nb(lua_State *L) {
898 return moonbr_io_tcpconnect_impl(L, 1);
899 }
901 static int moonbr_io_locallisten(lua_State *L) {
902 moonbr_io_listener_t *listener;
903 const char *path;
904 struct stat sb;
905 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
906 const int path_maxlen = sizeof(struct sockaddr_un) - (
907 (void *)sockaddr.sun_path - (void *)&sockaddr
908 ) - 1; /* one byte for termination */
909 int sock;
910 path = luaL_checkstring(L, 1);
911 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
912 strcpy(sockaddr.sun_path, path);
913 if (stat(path, &sb) == 0) {
914 if (S_ISSOCK(sb.st_mode)) unlink(path);
915 }
916 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
917 listener->fd = -1;
918 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
919 sock = socket(
920 PF_LOCAL,
921 SOCK_STREAM | SOCK_CLOEXEC,
922 0
923 );
924 if (sock < 0) {
925 moonbr_io_errmsg();
926 lua_pushnil(L);
927 lua_pushstring(L, errmsg);
928 return 2;
929 }
930 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
931 moonbr_io_errmsg();
932 close(sock);
933 lua_pushnil(L);
934 lua_pushstring(L, errmsg);
935 return 2;
936 }
937 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
938 moonbr_io_errmsg();
939 close(sock);
940 lua_pushnil(L);
941 lua_pushstring(L, errmsg);
942 return 2;
943 }
944 listener->fd = sock;
945 listener->addrfam = AF_LOCAL;
946 listener->nonblocking = -1;
947 return 1;
948 }
950 static int moonbr_io_tcplisten(lua_State *L) {
951 moonbr_io_listener_t *listener;
952 const char *host, *port;
953 struct addrinfo hints = { 0, };
954 struct addrinfo *res, *addrinfo;
955 int errcode;
956 int sock;
957 host = luaL_optstring(L, 1, NULL);
958 port = luaL_checkstring(L, 2);
959 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
960 listener->fd = -1;
961 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
962 hints.ai_family = AF_UNSPEC;
963 hints.ai_socktype = SOCK_STREAM;
964 hints.ai_protocol = IPPROTO_TCP;
965 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
966 errcode = getaddrinfo(host, port, &hints, &res);
967 if (errcode) {
968 freeaddrinfo(res);
969 if (errcode == EAI_SYSTEM) {
970 moonbr_io_errmsg();
971 lua_pushnil(L);
972 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
973 } else {
974 lua_pushnil(L);
975 lua_pushstring(L, gai_strerror(errcode));
976 }
977 return 2;
978 }
979 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
980 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
981 }
982 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
983 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
984 }
985 addrinfo = res;
986 moonbr_io_tcpconnect_found:
987 listener->addrfam = addrinfo->ai_family;
988 sock = socket(
989 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
990 addrinfo->ai_socktype | SOCK_CLOEXEC,
991 addrinfo->ai_protocol
992 );
993 if (sock < 0) {
994 moonbr_io_errmsg();
995 freeaddrinfo(res);
996 lua_pushnil(L);
997 lua_pushstring(L, errmsg);
998 return 2;
999 }
1001 static const int reuseval = 1;
1002 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1003 moonbr_io_errmsg();
1004 freeaddrinfo(res);
1005 close(sock);
1006 lua_pushnil(L);
1007 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1008 return 2;
1011 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1012 moonbr_io_errmsg();
1013 freeaddrinfo(res);
1014 close(sock);
1015 lua_pushnil(L);
1016 lua_pushstring(L, errmsg);
1017 return 2;
1019 freeaddrinfo(res);
1020 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1021 moonbr_io_errmsg();
1022 close(sock);
1023 lua_pushnil(L);
1024 lua_pushstring(L, errmsg);
1025 return 2;
1027 listener->fd = sock;
1028 listener->nonblocking = -1;
1029 return 1;
1032 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1033 moonbr_io_listener_t *listener;
1034 int fd;
1035 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1036 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1037 if (listener->nonblocking != nonblocking) {
1038 int flags;
1039 flags = fcntl(listener->fd, F_GETFL, 0);
1040 if (flags == -1) {
1041 moonbr_io_errmsg();
1042 close(listener->fd);
1043 listener->fd = -1;
1044 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1046 if (nonblocking) flags |= O_NONBLOCK;
1047 else flags &= ~O_NONBLOCK;
1048 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1049 moonbr_io_errmsg();
1050 close(listener->fd);
1051 listener->fd = -1;
1052 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1054 listener->nonblocking = nonblocking;
1056 while (1) {
1057 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1058 if (fd < 0) {
1059 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1060 lua_pushboolean(L, 0);
1061 lua_pushliteral(L, "No incoming connection pending");
1062 return 2;
1063 } else if (errno != EINTR) {
1064 moonbr_io_errmsg();
1065 lua_pushnil(L);
1066 lua_pushstring(L, errmsg);
1067 return 2;
1069 } else {
1070 moonbr_io_pushhandle(L, fd);
1071 return 1;
1076 static int moonbr_io_accept(lua_State *L) {
1077 return moonbr_io_accept_impl(L, 0);
1080 static int moonbr_io_accept_nb(lua_State *L) {
1081 return moonbr_io_accept_impl(L, 1);
1084 static int moonbr_io_unlisten(lua_State *L) {
1085 moonbr_io_listener_t *listener;
1086 struct sockaddr_un addr;
1087 socklen_t addrlen;
1088 struct stat sb;
1089 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1090 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1091 addrlen = sizeof(addr);
1092 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1093 if (close(listener->fd)) {
1094 moonbr_io_errmsg();
1095 listener->fd = -1;
1096 if (addrlen && addrlen <= sizeof(addr)) {
1097 if (stat(addr.sun_path, &sb) == 0) {
1098 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1101 lua_pushnil(L);
1102 lua_pushstring(L, errmsg);
1103 return 2;
1105 listener->fd = -1;
1106 if (addrlen && addrlen <= sizeof(addr)) {
1107 if (stat(addr.sun_path, &sb) == 0) {
1108 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1111 lua_pushboolean(L, 1);
1112 return 1;
1115 static int moonbr_io_listenergc(lua_State *L) {
1116 moonbr_io_listener_t *listener;
1117 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1118 if (listener->fd >= 0) close(listener->fd);
1119 listener->fd = -1;
1120 return 0;
1123 static int moonbr_io_poll(lua_State *L) {
1124 moonbr_io_handle_t *handle;
1125 moonbr_io_listener_t *listener;
1126 int fd, isnum;
1127 int nfds = 0;
1128 fd_set readfds, writefds, exceptfds;
1129 struct timeval timeout = {0, };
1130 int status;
1131 FD_ZERO(&readfds);
1132 FD_ZERO(&writefds);
1133 FD_ZERO(&exceptfds);
1134 if (!lua_isnoneornil(L, 1)) {
1135 luaL_checktype(L, 1, LUA_TTABLE);
1136 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1137 if (lua_toboolean(L, -1)) {
1138 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1139 if (handle) {
1140 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1141 fd = handle->fd;
1142 if (
1143 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1144 handle->readbufin != handle->readbufout /* data pending in buffer */
1145 ) {
1146 lua_pushboolean(L, 1);
1147 return 1;
1149 } else {
1150 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1151 if (listener) {
1152 fd = listener->fd;
1153 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1154 } else {
1155 fd = lua_tointegerx(L, -2, &isnum);
1156 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1159 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1160 FD_SET(fd, &readfds);
1161 if (fd+1 > nfds) nfds = fd+1;
1165 if (!lua_isnoneornil(L, 2)) {
1166 luaL_checktype(L, 2, LUA_TTABLE);
1167 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1168 if (lua_toboolean(L, -1)) {
1169 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1170 if (handle) {
1171 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1172 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1173 fd = handle->fd;
1174 } else {
1175 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1176 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1177 fd = lua_tointegerx(L, -2, &isnum);
1178 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1180 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1181 FD_SET(fd, &writefds);
1182 if (fd+1 > nfds) nfds = fd+1;
1186 if (!lua_isnoneornil(L, 3)) {
1187 lua_Number n;
1188 n = lua_tonumberx(L, 3, &isnum);
1189 if (isnum && n>=0 && n<100000000) {
1190 timeout.tv_sec = n;
1191 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1192 } else {
1193 luaL_argcheck(L, 0, 3, "not a valid timeout");
1195 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1196 } else {
1197 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1199 if (status == -1) {
1200 if (errno == EINTR) {
1201 lua_pushboolean(L, 0);
1202 lua_pushliteral(L, "Signal received while polling file descriptors");
1203 return 2;
1204 } else {
1205 moonbr_io_errmsg();
1206 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1208 } else if (status == 0) {
1209 lua_pushboolean(L, 0);
1210 lua_pushliteral(L, "Timeout while polling file descriptors");
1211 return 2;
1212 } else {
1213 lua_pushboolean(L, 1);
1214 return 1;
1218 static int moonbr_io_timeref(lua_State *L) {
1219 lua_Number sub;
1220 struct timespec tp;
1221 sub = luaL_optnumber(L, 1, 0);
1222 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1223 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1225 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1226 return 1;
1229 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1230 {"read", moonbr_io_read},
1231 {"read_nb", moonbr_io_read_nb},
1232 {"read_call", moonbr_io_read_call},
1233 {"read_yield", moonbr_io_read_yield},
1234 {"drain", moonbr_io_drain},
1235 {"drain_nb", moonbr_io_drain_nb},
1236 {"write", moonbr_io_write},
1237 {"write_nb", moonbr_io_write_nb},
1238 {"flush", moonbr_io_flush},
1239 {"flush_nb", moonbr_io_flush_nb},
1240 {"finish", moonbr_io_finish},
1241 {"close", moonbr_io_close},
1242 {"reset", moonbr_io_reset},
1243 {NULL, NULL}
1244 };
1246 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1247 {"__index", moonbr_io_handleindex},
1248 {"__newindex", moonbr_io_handlenewindex},
1249 {"__gc", moonbr_io_handlegc},
1250 {NULL, NULL}
1251 };
1253 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1254 {"accept", moonbr_io_accept},
1255 {"accept_nb", moonbr_io_accept_nb},
1256 {"close", moonbr_io_unlisten},
1257 {NULL, NULL}
1258 };
1260 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1261 {"__gc", moonbr_io_listenergc},
1262 {NULL, NULL}
1263 };
1265 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1266 {"localconnect", moonbr_io_localconnect},
1267 {"localconnect_nb", moonbr_io_localconnect_nb},
1268 {"tcpconnect", moonbr_io_tcpconnect},
1269 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1270 {"locallisten", moonbr_io_locallisten},
1271 {"tcplisten", moonbr_io_tcplisten},
1272 {"poll", moonbr_io_poll},
1273 {"timeref", moonbr_io_timeref},
1274 {NULL, NULL}
1275 };
1277 int luaopen_moonbridge_io(lua_State *L) {
1279 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1281 lua_newtable(L); // module
1283 lua_newtable(L); // public metatable
1284 lua_newtable(L); // handle methods
1285 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1286 lua_pushvalue(L, -1);
1287 lua_setfield(L, -4, "prototype_handle");
1288 lua_setfield(L, -2, "__index");
1289 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1291 lua_newtable(L); // handle metatable
1292 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1293 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1295 lua_newtable(L); // listener metatable
1296 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1297 lua_newtable(L); // listener methods
1298 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1299 lua_pushvalue(L, -1);
1300 lua_setfield(L, -4, "prototype_listener");
1301 lua_setfield(L, -2, "__index");
1302 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1304 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1305 return 1;

Impressum / About Us