moonbridge

view moonbridge_io.c @ 194:822ccaeccccb

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

Impressum / About Us