moonbridge

view moonbridge_io.c @ 176:b2d024220782

Renamed remaining_header_size_limit and remaining_body_size_limit to local "limit" variables
author jbe
date Thu Jun 18 22:16:08 2015 +0200 (2015-06-18)
parents 08cf8e1865e9
children d338068fad0d
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 = 0; /* initialize to avoid compiler warning */
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 terminatorpos = memchr(
210 handle->readbuf + handle->readbufout,
211 terminator,
212 remaining
213 );
214 } else if (terminatorlen) {
215 terminatorpos = memchr(
216 handle->readbuf + handle->readbufout,
217 terminator,
218 handle->readbufin - handle->readbufout
219 );
220 }
221 if (terminatorpos) remaining = 1 + (
222 terminatorpos - (handle->readbuf + handle->readbufout)
223 );
224 if (remaining >= 0) {
225 if (!drain) {
226 luaL_addlstring(
227 &luabuf,
228 handle->readbuf + handle->readbufout,
229 remaining
230 );
231 luaL_pushresult(&luabuf);
232 } else {
233 lua_pushinteger(L, luabufcnt + remaining);
234 }
235 if (terminatorpos) lua_pushliteral(L, "term");
236 else lua_pushliteral(L, "maxlen");
237 handle->readbufout += remaining;
238 if (handle->readbufout == handle->readbufin) {
239 handle->readbufin = 0;
240 handle->readbufout = 0;
241 }
242 handle->readerr = 0;
243 return 2;
244 }
245 if (!drain) luaL_addlstring(
246 &luabuf,
247 handle->readbuf + handle->readbufout,
248 handle->readbufin - handle->readbufout
249 );
250 luabufcnt += handle->readbufin - handle->readbufout;
251 handle->readbufout = 0;
252 do {
253 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
254 } while (bytesread < 0 && (errno == EINTR));
255 if (
256 bytesread == 0 || (
257 nonblocking &&
258 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
259 )
260 ) {
261 handle->readbufin = 0;
262 if (!drain) luaL_pushresult(&luabuf);
263 else lua_pushinteger(L, luabufcnt);
264 if (bytesread == 0) lua_pushliteral(L, "eof");
265 else lua_pushliteral(L, "block");
266 handle->readerr = 0;
267 return 2;
268 }
269 if (bytesread < 0) {
270 moonbr_io_errmsg();
271 lua_pushnil(L);
272 lua_pushstring(L, errmsg);
273 return 2;
274 }
275 handle->readbufin = bytesread;
276 }
277 }
279 static int moonbr_io_read(lua_State *L) {
280 return moonbr_io_read_impl(L, 0, 0);
281 }
283 static int moonbr_io_read_nb(lua_State *L) {
284 return moonbr_io_read_impl(L, 1, 0);
285 }
287 static int moonbr_io_drain(lua_State *L) {
288 return moonbr_io_read_impl(L, 0, 1);
289 }
291 static int moonbr_io_drain_nb(lua_State *L) {
292 return moonbr_io_read_impl(L, 1, 1);
293 }
295 #if LUA_VERSION_NUM >= 503
296 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
297 #else
298 static int moonbr_io_read_cont(lua_State *L) {
299 #endif
300 lua_Integer remaining;
301 size_t len;
302 #if !(LUA_VERSION_NUM >= 503)
303 int ctx = 0;
304 lua_getctx(L, &ctx);
305 #endif
306 remaining = lua_tointeger(L, 3);
307 while (1) {
308 lua_pushcfunction(L, moonbr_io_read_nb);
309 lua_pushvalue(L, 1);
310 lua_pushvalue(L, 3);
311 lua_pushvalue(L, 4);
312 lua_call(L, 3, 2);
313 if (lua_isnil(L, -2)) return 2;
314 lua_insert(L, -2);
315 len = lua_rawlen(L, -1);
316 if (ctx == 0) {
317 lua_replace(L, 5);
318 ctx = 1;
319 } else if (ctx == 1) {
320 lua_pushvalue(L, 5);
321 lua_newtable(L);
322 lua_replace(L, 5);
323 lua_rawseti(L, 5, 2);
324 lua_rawseti(L, 5, 1);
325 ctx = 2;
326 } else {
327 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
328 }
329 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
330 lua_pop(L, 1);
331 if (remaining >= 0 && len) {
332 remaining -= len;
333 lua_pushinteger(L, remaining);
334 lua_replace(L, 3);
335 }
336 lua_pushvalue(L, 2);
337 lua_callk(L, 0, 0, ctx, moonbr_io_read_cont);
338 }
339 if (ctx == 1) {
340 lua_pushvalue(L, 5);
341 } else {
342 luaL_Buffer buf;
343 lua_Integer i, chunkcount;
344 chunkcount = lua_rawlen(L, 5);
345 luaL_buffinit(L, &buf);
346 for (i=1; i<=chunkcount && i>0; i++) {
347 lua_rawgeti(L, 5, i);
348 luaL_addvalue(&buf);
349 }
350 luaL_pushresult(&buf);
351 }
352 lua_pushvalue(L, -2);
353 return 2;
354 }
356 static int moonbr_io_read_call(lua_State *L) {
357 lua_settop(L, 4);
358 lua_pushnil(L);
359 #if LUA_VERSION_NUM >= 503
360 return moonbr_io_read_cont(L, 0, 0);
361 #else
362 return moonbr_io_read_cont(L);
363 #endif
364 }
366 moonbr_io_yield_wrapper(moonbr_io_read_yield, moonbr_io_read_call);
368 #if LUA_VERSION_NUM >= 503
369 static int moonbr_io_drain_cont(lua_State *L, int status, lua_KContext ctx) {
370 #else
371 static int moonbr_io_drain_cont(lua_State *L) {
372 #endif
373 lua_Integer remaining, len;
374 size_t totallen = 0;
375 #if !(LUA_VERSION_NUM >= 503)
376 int ctx = 0;
377 lua_getctx(L, &ctx);
378 #endif
379 remaining = lua_tointeger(L, 3);
380 while (1) {
381 lua_pushcfunction(L, moonbr_io_drain_nb);
382 lua_pushvalue(L, 1);
383 lua_pushvalue(L, 3);
384 lua_pushvalue(L, 4);
385 lua_call(L, 3, 2);
386 if (lua_isnil(L, -2)) return 2;
387 lua_insert(L, -2);
388 len = lua_tointeger(L, -1);
389 lua_pop(L, 1);
390 totallen += len;
391 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
392 lua_pop(L, 1);
393 if (remaining >= 0 && len) {
394 remaining -= len;
395 lua_pushinteger(L, remaining);
396 lua_replace(L, 3);
397 }
398 lua_pushvalue(L, 2);
399 lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont);
400 }
401 lua_pushinteger(L, totallen);
402 lua_pushvalue(L, -2);
403 return 2;
404 }
406 static int moonbr_io_drain_call(lua_State *L) {
407 #if LUA_VERSION_NUM >= 503
408 return moonbr_io_drain_cont(L, 0, 0);
409 #else
410 return moonbr_io_drain_cont(L);
411 #endif
412 }
414 moonbr_io_yield_wrapper(moonbr_io_drain_yield, moonbr_io_drain_call);
416 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
417 moonbr_io_handle_t *handle;
418 int i, top;
419 const char *str;
420 size_t strlen;
421 ssize_t written;
422 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
423 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
424 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
425 if (handle->writeerr) {
426 lua_pushnil(L);
427 lua_pushliteral(L, "Previous write error");
428 return 2;
429 }
430 handle->writeerr = 1;
431 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
432 top = lua_gettop(L);
433 lua_getuservalue(L, 1);
434 lua_getfield(L, -1, "writequeue");
435 for (i=2; i<=top; i++) {
436 luaL_checklstring(L, i, &strlen);
437 lua_pushvalue(L, i);
438 lua_rawseti(L, -2, handle->writeqin++);
439 handle->writeleft += strlen;
440 }
441 if (flush) handle->flushedleft = handle->writeleft;
442 while (handle->writeqout != handle->writeqin) {
443 lua_rawgeti(L, -1, handle->writeqout);
444 str = lua_tolstring(L, -1, &strlen);
445 while (handle->writeqoff < strlen) {
446 if (
447 strlen - handle->writeqoff <
448 MOONBR_IO_WRITEBUFLEN - handle->writebufin
449 ) {
450 memcpy(
451 handle->writebuf + handle->writebufin,
452 str + handle->writeqoff,
453 strlen - handle->writeqoff
454 );
455 handle->writebufin += strlen - handle->writeqoff;
456 break;
457 } else {
458 memcpy(
459 handle->writebuf + handle->writebufin,
460 str + handle->writeqoff,
461 MOONBR_IO_WRITEBUFLEN - handle->writebufin
462 );
463 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
464 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
465 moonbr_io_handle_set_nopush(L, handle, 1);
466 written = write(
467 handle->fd,
468 handle->writebuf + handle->writebufout,
469 MOONBR_IO_WRITEBUFLEN - handle->writebufout
470 );
471 if (written < 0) {
472 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
473 goto moonbr_io_write_impl_block;
474 } else if (errno != EINTR) {
475 moonbr_io_errmsg();
476 lua_pushnil(L);
477 lua_pushstring(L, errmsg);
478 return 2;
479 }
480 } else {
481 handle->writebufout += written;
482 handle->writeleft -= written;
483 if (handle->flushedleft) {
484 if (written >= handle->flushedleft) {
485 handle->flushedleft = 0;
486 moonbr_io_handle_set_nopush(L, handle, 0);
487 } else {
488 handle->flushedleft -= written;
489 }
490 }
491 }
492 }
493 handle->writebufin = 0;
494 handle->writebufout = 0;
495 }
496 }
497 handle->writeqoff = 0;
498 lua_pop(L, 1);
499 lua_pushnil(L);
500 lua_rawseti(L, -2, handle->writeqout++);
501 }
502 while (handle->flushedleft) {
503 moonbr_io_handle_set_nopush(L, handle, 1);
504 written = write(
505 handle->fd,
506 handle->writebuf + handle->writebufout,
507 handle->writebufin - handle->writebufout
508 );
509 if (written < 0) {
510 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
511 goto moonbr_io_write_impl_block;
512 } else if (errno != EINTR) {
513 moonbr_io_errmsg();
514 lua_pushnil(L);
515 lua_pushstring(L, errmsg);
516 return 2;
517 }
518 } else {
519 handle->writebufout += written;
520 handle->writeleft -= written;
521 if (handle->flushedleft) {
522 if (written >= handle->flushedleft) {
523 handle->flushedleft = 0;
524 moonbr_io_handle_set_nopush(L, handle, 0);
525 } else {
526 handle->flushedleft -= written;
527 }
528 }
529 }
530 }
531 if (handle->writebufout == handle->writebufin) {
532 handle->writebufin = 0;
533 handle->writebufout = 0;
534 }
535 if (nonblocking) lua_pushinteger(L, 0);
536 else lua_pushvalue(L, 1);
537 handle->writeerr = 0;
538 return 1;
539 moonbr_io_write_impl_block:
540 lua_pushinteger(L, handle->writeleft);
541 handle->writeerr = 0;
542 return 1;
543 }
545 static int moonbr_io_write(lua_State *L) {
546 return moonbr_io_write_impl(L, 0, 0);
547 }
549 static int moonbr_io_write_nb(lua_State *L) {
550 return moonbr_io_write_impl(L, 1, 0);
551 }
553 static int moonbr_io_flush(lua_State *L) {
554 return moonbr_io_write_impl(L, 0, 1);
555 }
557 static int moonbr_io_flush_nb(lua_State *L) {
558 return moonbr_io_write_impl(L, 1, 1);
559 }
561 #if LUA_VERSION_NUM >= 503
562 static int moonbr_io_write_cont(lua_State *L, int status, lua_KContext ctx) {
563 #else
564 static int moonbr_io_write_cont(lua_State *L) {
565 #endif
566 while (1) {
567 lua_pushcfunction(L, moonbr_io_write_nb);
568 lua_pushvalue(L, 1);
569 lua_call(L, 1, 2);
570 if (lua_isnil(L, -2)) return 2;
571 if (!lua_tointeger(L, -2)) {
572 lua_pushvalue(L, 1);
573 return 1;
574 }
575 lua_pop(L, 2);
576 lua_pushvalue(L, 2);
577 lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
578 }
579 }
581 static int moonbr_io_write_call(lua_State *L) {
582 lua_pushcfunction(L, moonbr_io_write_nb);
583 lua_insert(L, 3);
584 lua_pushvalue(L, 1);
585 lua_insert(L, 4);
586 lua_call(L, lua_gettop(L) - 3, 2);
587 if (lua_isnil(L, -2)) return 2;
588 if (!lua_tointeger(L, -2)) {
589 lua_pushvalue(L, 1);
590 return 1;
591 }
592 #if LUA_VERSION_NUM >= 503
593 return moonbr_io_write_cont(L, 0, 0);
594 #else
595 return moonbr_io_write_cont(L);
596 #endif
597 }
599 moonbr_io_yield_wrapper(moonbr_io_write_yield, moonbr_io_write_call);
601 static int moonbr_io_flush_call(lua_State *L) {
602 lua_pushcfunction(L, moonbr_io_flush_nb);
603 lua_insert(L, 3);
604 lua_pushvalue(L, 1);
605 lua_insert(L, 4);
606 lua_call(L, lua_gettop(L) - 3, 2);
607 if (lua_isnil(L, -2)) return 2;
608 if (!lua_tointeger(L, -2)) {
609 lua_pushvalue(L, 1);
610 return 1;
611 }
612 #if LUA_VERSION_NUM >= 503
613 return moonbr_io_write_cont(L, 0, 0);
614 #else
615 return moonbr_io_write_cont(L);
616 #endif
617 }
619 moonbr_io_yield_wrapper(moonbr_io_flush_yield, moonbr_io_flush_call);
621 static int moonbr_io_finish(lua_State *L) {
622 moonbr_io_handle_t *handle;
623 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
624 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
625 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
626 if (handle->writeleft) {
627 lua_pushcfunction(L, moonbr_io_flush);
628 lua_pushvalue(L, 1);
629 if (lua_pcall(L, 1, 2, 0)) {
630 handle->finished = 1;
631 lua_error(L);
632 }
633 if (!lua_toboolean(L, -2)) {
634 handle->finished = 1;
635 return 2;
636 }
637 }
638 handle->finished = 1;
639 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
640 if (shutdown(handle->fd, SHUT_WR)) {
641 moonbr_io_errmsg();
642 lua_pushnil(L);
643 lua_pushstring(L, errmsg);
644 return 2;
645 }
646 } else {
647 if (close(handle->fd)) {
648 moonbr_io_errmsg();
649 handle->fd = -1;
650 lua_pushnil(L);
651 lua_pushstring(L, errmsg);
652 return 2;
653 }
654 handle->fd = -1; /* fake EOF on read */
655 }
656 lua_pushboolean(L, 1);
657 return 1;
658 }
660 static int moonbr_io_close_impl(lua_State *L, int reset) {
661 moonbr_io_handle_t *handle;
662 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
663 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
664 if (!reset) {
665 if (handle->writeleft) {
666 lua_pushcfunction(L, moonbr_io_flush);
667 lua_pushvalue(L, 1);
668 if (lua_pcall(L, 1, 2, 0)) {
669 handle->closed = 1;
670 close(handle->fd);
671 handle->fd = -1;
672 lua_error(L);
673 }
674 handle->closed = 1;
675 if (!lua_toboolean(L, -2)) {
676 close(handle->fd);
677 handle->fd = -1;
678 return 2;
679 }
680 } else {
681 handle->closed = 1;
682 moonbr_io_handle_set_linger(L, handle, -1);
683 }
684 } else {
685 handle->closed = 1;
686 }
687 if (handle->fd >= 0) {
688 if (close(handle->fd)) {
689 moonbr_io_errmsg();
690 handle->fd = -1;
691 lua_pushnil(L);
692 lua_pushstring(L, errmsg);
693 return 2;
694 }
695 handle->fd = -1;
696 }
697 lua_pushboolean(L, 1);
698 return 1;
700 }
702 static int moonbr_io_close(lua_State *L) {
703 return moonbr_io_close_impl(L, 0);
704 }
706 static int moonbr_io_reset(lua_State *L) {
707 return moonbr_io_close_impl(L, 1);
708 }
710 static int moonbr_io_handlegc(lua_State *L) {
711 moonbr_io_handle_t *handle;
712 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
713 if (handle->fd >= 0) {
714 lua_pushcfunction(L, moonbr_io_close);
715 lua_pushvalue(L, 1);
716 lua_pushinteger(L, 0);
717 lua_call(L, 2, 0);
718 }
719 return 0;
720 }
722 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
723 moonbr_io_handle_t *handle;
724 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
725 if (!handle->closed) {
726 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
727 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
728 lua_call(L, 1, 0);
729 }
730 }
732 void moonbr_io_pushhandle(lua_State *L, int fd) {
733 moonbr_io_handle_t *handle;
734 struct sockaddr addr;
735 socklen_t addrlen;
736 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
737 handle->fd = fd;
738 addrlen = sizeof(addr);
739 if (getsockname(fd, &addr, &addrlen)) {
740 if (errno != ENOTSOCK) {
741 moonbr_io_errmsg();
742 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
743 }
744 handle->issock = 0;
745 } else {
746 handle->issock = 1;
747 handle->addrfam = addr.sa_family;
748 }
749 handle->finished = 0;
750 handle->closed = 0;
751 handle->nonblocking = -1;
752 handle->nopush = -1;
753 handle->readerr = 0;
754 handle->readbufin = 0;
755 handle->readbufout = 0;
756 handle->writeerr = 0;
757 handle->writeleft = 0;
758 handle->flushedleft = 0;
759 handle->writeqin = 0;
760 handle->writeqout = 0;
761 handle->writeqoff = 0;
762 handle->writebufin = 0;
763 handle->writebufout = 0;
764 moonbr_io_handle_set_linger(L, handle, 0);
765 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
766 lua_setmetatable(L, -2);
767 lua_newtable(L); // uservalue
768 lua_newtable(L);
769 lua_setfield(L, -2, "writequeue");
770 lua_newtable(L); // public
771 if (handle->addrfam == AF_INET6) {
772 struct sockaddr_in6 addr_in6;
773 char addrstrbuf[INET6_ADDRSTRLEN];
774 const char *addrstr;
775 addrlen = sizeof(addr_in6);
776 if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
777 moonbr_io_errmsg();
778 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
779 }
780 if (addrlen > sizeof(addr_in6)) {
781 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
782 }
783 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
784 if (!addrstr) {
785 moonbr_io_errmsg();
786 luaL_error(L, "Could not format local IP address: %s", errmsg);
787 } else {
788 lua_pushstring(L, addrstr);
789 lua_setfield(L, -2, "local_ip6");
790 }
791 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
792 lua_setfield(L, -2, "local_tcpport");
793 if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
794 moonbr_io_errmsg();
795 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
796 }
797 if (addrlen > sizeof(addr_in6)) {
798 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
799 }
800 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
801 if (!addrstr) {
802 moonbr_io_errmsg();
803 luaL_error(L, "Could not format remote IP address: %s", errmsg);
804 } else {
805 lua_pushstring(L, addrstr);
806 lua_setfield(L, -2, "remote_ip6");
807 }
808 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
809 lua_setfield(L, -2, "remote_tcpport");
810 } else if (handle->addrfam == AF_INET) {
811 struct sockaddr_in addr_in;
812 char addrstrbuf[INET_ADDRSTRLEN];
813 const char *addrstr;
814 addrlen = sizeof(addr_in);
815 if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) {
816 moonbr_io_errmsg();
817 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
818 }
819 if (addrlen > sizeof(addr_in)) {
820 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
821 }
822 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
823 if (!addrstr) {
824 moonbr_io_errmsg();
825 luaL_error(L, "Could not format local IP address: %s", errmsg);
826 } else {
827 lua_pushstring(L, addrstr);
828 lua_setfield(L, -2, "local_ip4");
829 }
830 lua_pushinteger(L, ntohs(addr_in.sin_port));
831 lua_setfield(L, -2, "local_tcpport");
832 if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) {
833 moonbr_io_errmsg();
834 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
835 }
836 if (addrlen > sizeof(addr_in)) {
837 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
838 }
839 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
840 if (!addrstr) {
841 moonbr_io_errmsg();
842 luaL_error(L, "Could not format remote IP address: %s", errmsg);
843 } else {
844 lua_pushstring(L, addrstr);
845 lua_setfield(L, -2, "remote_ip4");
846 }
847 lua_pushinteger(L, ntohs(addr_in.sin_port));
848 lua_setfield(L, -2, "remote_tcpport");
849 }
850 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
851 lua_setmetatable(L, -2);
852 lua_setfield(L, -2, "public");
853 lua_setuservalue(L, -2);
854 }
856 static int moonbr_io_handleindex(lua_State *L) {
857 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
858 lua_getuservalue(L, 1);
859 lua_getfield(L, -1, "public");
860 lua_pushvalue(L, 2);
861 lua_gettable(L, -2);
862 return 1;
863 }
865 static int moonbr_io_handlenewindex(lua_State *L) {
866 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
867 lua_getuservalue(L, 1);
868 lua_getfield(L, -1, "public");
869 lua_pushvalue(L, 2);
870 lua_pushvalue(L, 3);
871 lua_settable(L, -3);
872 return 0;
873 }
875 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
876 const char *path;
877 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
878 const int path_maxlen = sizeof(struct sockaddr_un) - (
879 (void *)sockaddr.sun_path - (void *)&sockaddr
880 ) - 1; /* one byte for termination */
881 int sock;
882 path = luaL_checkstring(L, 1);
883 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
884 strcpy(sockaddr.sun_path, path);
885 sock = socket(
886 PF_LOCAL,
887 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
888 0
889 );
890 if (sock < 0) {
891 moonbr_io_errmsg();
892 lua_pushnil(L);
893 lua_pushstring(L, errmsg);
894 return 2;
895 }
896 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
897 if (!nonblocking && errno == EINTR) {
898 moonbr_io_errmsg();
899 close(sock);
900 lua_pushnil(L);
901 lua_pushstring(L, errmsg);
902 return 2;
903 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
904 moonbr_io_errmsg();
905 lua_pushnil(L);
906 lua_pushstring(L, errmsg);
907 return 2;
908 }
909 }
910 moonbr_io_pushhandle(L, sock);
911 return 1;
912 }
914 static int moonbr_io_localconnect(lua_State *L) {
915 return moonbr_io_localconnect_impl(L, 0);
916 }
918 static int moonbr_io_localconnect_nb(lua_State *L) {
919 return moonbr_io_localconnect_impl(L, 1);
920 }
922 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
923 const char *host, *port;
924 struct addrinfo hints = { 0, };
925 struct addrinfo *res, *addrinfo;
926 int errcode;
927 int sock;
928 host = luaL_checkstring(L, 1);
929 port = luaL_checkstring(L, 2);
930 hints.ai_family = AF_UNSPEC;
931 hints.ai_socktype = SOCK_STREAM;
932 hints.ai_protocol = IPPROTO_TCP;
933 hints.ai_flags = AI_ADDRCONFIG;
934 errcode = getaddrinfo(host, port, &hints, &res);
935 if (errcode) {
936 freeaddrinfo(res);
937 if (errcode == EAI_SYSTEM) {
938 moonbr_io_errmsg();
939 lua_pushnil(L);
940 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
941 } else {
942 lua_pushnil(L);
943 lua_pushstring(L, gai_strerror(errcode));
944 }
945 return 2;
946 }
947 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
948 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
949 }
950 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
951 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
952 }
953 addrinfo = res;
954 moonbr_io_tcpconnect_found:
955 sock = socket(
956 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
957 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
958 addrinfo->ai_protocol
959 );
960 if (sock < 0) {
961 moonbr_io_errmsg();
962 freeaddrinfo(res);
963 lua_pushnil(L);
964 lua_pushstring(L, errmsg);
965 return 2;
966 }
967 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
968 freeaddrinfo(res);
969 if (!nonblocking && errno == EINTR) {
970 moonbr_io_errmsg();
971 close(sock);
972 lua_pushnil(L);
973 lua_pushstring(L, errmsg);
974 return 2;
975 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
976 moonbr_io_errmsg();
977 lua_pushnil(L);
978 lua_pushstring(L, errmsg);
979 return 2;
980 }
981 } else {
982 freeaddrinfo(res);
983 }
984 moonbr_io_pushhandle(L, sock);
985 return 1;
986 }
988 static int moonbr_io_tcpconnect(lua_State *L) {
989 return moonbr_io_tcpconnect_impl(L, 0);
990 }
992 static int moonbr_io_tcpconnect_nb(lua_State *L) {
993 return moonbr_io_tcpconnect_impl(L, 1);
994 }
996 static int moonbr_io_locallisten(lua_State *L) {
997 moonbr_io_listener_t *listener;
998 const char *path;
999 struct stat sb;
1000 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
1001 const int path_maxlen = sizeof(struct sockaddr_un) - (
1002 (void *)sockaddr.sun_path - (void *)&sockaddr
1003 ) - 1; /* one byte for termination */
1004 int sock;
1005 path = luaL_checkstring(L, 1);
1006 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
1007 strcpy(sockaddr.sun_path, path);
1008 if (stat(path, &sb) == 0) {
1009 if (S_ISSOCK(sb.st_mode)) unlink(path);
1011 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1012 listener->fd = -1;
1013 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1014 sock = socket(
1015 PF_LOCAL,
1016 SOCK_STREAM | SOCK_CLOEXEC,
1018 );
1019 if (sock < 0) {
1020 moonbr_io_errmsg();
1021 lua_pushnil(L);
1022 lua_pushstring(L, errmsg);
1023 return 2;
1025 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
1026 moonbr_io_errmsg();
1027 close(sock);
1028 lua_pushnil(L);
1029 lua_pushstring(L, errmsg);
1030 return 2;
1032 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1033 moonbr_io_errmsg();
1034 close(sock);
1035 lua_pushnil(L);
1036 lua_pushstring(L, errmsg);
1037 return 2;
1039 listener->fd = sock;
1040 listener->addrfam = AF_LOCAL;
1041 listener->nonblocking = -1;
1042 return 1;
1045 static int moonbr_io_tcplisten(lua_State *L) {
1046 moonbr_io_listener_t *listener;
1047 const char *host, *port;
1048 struct addrinfo hints = { 0, };
1049 struct addrinfo *res, *addrinfo;
1050 int errcode;
1051 int sock;
1052 host = luaL_optstring(L, 1, NULL);
1053 port = luaL_checkstring(L, 2);
1054 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
1055 listener->fd = -1;
1056 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
1057 hints.ai_family = AF_UNSPEC;
1058 hints.ai_socktype = SOCK_STREAM;
1059 hints.ai_protocol = IPPROTO_TCP;
1060 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1061 errcode = getaddrinfo(host, port, &hints, &res);
1062 if (errcode) {
1063 freeaddrinfo(res);
1064 if (errcode == EAI_SYSTEM) {
1065 moonbr_io_errmsg();
1066 lua_pushnil(L);
1067 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
1068 } else {
1069 lua_pushnil(L);
1070 lua_pushstring(L, gai_strerror(errcode));
1072 return 2;
1074 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1075 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
1077 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1078 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
1080 addrinfo = res;
1081 moonbr_io_tcpconnect_found:
1082 listener->addrfam = addrinfo->ai_family;
1083 sock = socket(
1084 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
1085 addrinfo->ai_socktype | SOCK_CLOEXEC,
1086 addrinfo->ai_protocol
1087 );
1088 if (sock < 0) {
1089 moonbr_io_errmsg();
1090 freeaddrinfo(res);
1091 lua_pushnil(L);
1092 lua_pushstring(L, errmsg);
1093 return 2;
1096 static const int reuseval = 1;
1097 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
1098 moonbr_io_errmsg();
1099 freeaddrinfo(res);
1100 close(sock);
1101 lua_pushnil(L);
1102 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
1103 return 2;
1106 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
1107 moonbr_io_errmsg();
1108 freeaddrinfo(res);
1109 close(sock);
1110 lua_pushnil(L);
1111 lua_pushstring(L, errmsg);
1112 return 2;
1114 freeaddrinfo(res);
1115 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
1116 moonbr_io_errmsg();
1117 close(sock);
1118 lua_pushnil(L);
1119 lua_pushstring(L, errmsg);
1120 return 2;
1122 listener->fd = sock;
1123 listener->nonblocking = -1;
1124 return 1;
1127 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
1128 moonbr_io_listener_t *listener;
1129 int fd;
1130 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1131 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
1132 if (listener->nonblocking != nonblocking) {
1133 int flags;
1134 flags = fcntl(listener->fd, F_GETFL, 0);
1135 if (flags == -1) {
1136 moonbr_io_errmsg();
1137 close(listener->fd);
1138 listener->fd = -1;
1139 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1141 if (nonblocking) flags |= O_NONBLOCK;
1142 else flags &= ~O_NONBLOCK;
1143 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
1144 moonbr_io_errmsg();
1145 close(listener->fd);
1146 listener->fd = -1;
1147 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
1149 listener->nonblocking = nonblocking;
1151 while (1) {
1152 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
1153 if (fd < 0) {
1154 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
1155 lua_pushboolean(L, 0);
1156 lua_pushliteral(L, "No incoming connection pending");
1157 return 2;
1158 } else if (errno != EINTR) {
1159 moonbr_io_errmsg();
1160 lua_pushnil(L);
1161 lua_pushstring(L, errmsg);
1162 return 2;
1164 } else {
1165 moonbr_io_pushhandle(L, fd);
1166 return 1;
1171 static int moonbr_io_accept(lua_State *L) {
1172 return moonbr_io_accept_impl(L, 0);
1175 static int moonbr_io_accept_nb(lua_State *L) {
1176 return moonbr_io_accept_impl(L, 1);
1179 static int moonbr_io_unlisten(lua_State *L) {
1180 moonbr_io_listener_t *listener;
1181 struct sockaddr_un addr;
1182 socklen_t addrlen;
1183 struct stat sb;
1184 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1185 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
1186 addrlen = sizeof(addr);
1187 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
1188 if (close(listener->fd)) {
1189 moonbr_io_errmsg();
1190 listener->fd = -1;
1191 if (addrlen && addrlen <= sizeof(addr)) {
1192 if (stat(addr.sun_path, &sb) == 0) {
1193 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1196 lua_pushnil(L);
1197 lua_pushstring(L, errmsg);
1198 return 2;
1200 listener->fd = -1;
1201 if (addrlen && addrlen <= sizeof(addr)) {
1202 if (stat(addr.sun_path, &sb) == 0) {
1203 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
1206 lua_pushboolean(L, 1);
1207 return 1;
1210 static int moonbr_io_listenergc(lua_State *L) {
1211 moonbr_io_listener_t *listener;
1212 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
1213 if (listener->fd >= 0) close(listener->fd);
1214 listener->fd = -1;
1215 return 0;
1218 static int moonbr_io_poll(lua_State *L) {
1219 moonbr_io_handle_t *handle;
1220 moonbr_io_listener_t *listener;
1221 int fd, isnum;
1222 int nfds = 0;
1223 fd_set readfds, writefds, exceptfds;
1224 struct timeval timeout = {0, };
1225 int status;
1226 FD_ZERO(&readfds);
1227 FD_ZERO(&writefds);
1228 FD_ZERO(&exceptfds);
1229 if (!lua_isnoneornil(L, 1)) {
1230 luaL_checktype(L, 1, LUA_TTABLE);
1231 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
1232 if (lua_toboolean(L, -1)) {
1233 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1234 if (handle) {
1235 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1236 fd = handle->fd;
1237 if (
1238 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
1239 handle->readbufin != handle->readbufout /* data pending in buffer */
1240 ) {
1241 lua_pushboolean(L, 1);
1242 return 1;
1244 } else {
1245 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1246 if (listener) {
1247 fd = listener->fd;
1248 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
1249 } else {
1250 fd = lua_tointegerx(L, -2, &isnum);
1251 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
1254 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1255 FD_SET(fd, &readfds);
1256 if (fd+1 > nfds) nfds = fd+1;
1260 if (!lua_isnoneornil(L, 2)) {
1261 luaL_checktype(L, 2, LUA_TTABLE);
1262 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
1263 if (lua_toboolean(L, -1)) {
1264 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
1265 if (handle) {
1266 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
1267 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
1268 fd = handle->fd;
1269 } else {
1270 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
1271 if (listener) luaL_error(L, "Attempt to write-poll a listener");
1272 fd = lua_tointegerx(L, -2, &isnum);
1273 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
1275 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
1276 FD_SET(fd, &writefds);
1277 if (fd+1 > nfds) nfds = fd+1;
1281 if (!lua_isnoneornil(L, 3)) {
1282 lua_Number n;
1283 n = lua_tonumberx(L, 3, &isnum);
1284 if (isnum && n<0) {
1285 lua_pushboolean(L, 0);
1286 lua_pushliteral(L, "Negative timeout");
1287 return 2;
1288 } else if (isnum && n>=0 && n<100000000) {
1289 timeout.tv_sec = n;
1290 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1291 } else {
1292 luaL_argcheck(L, 0, 3, "not a valid timeout");
1294 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1295 } else {
1296 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1298 if (status == -1) {
1299 if (errno == EINTR) {
1300 lua_pushnil(L);
1301 lua_pushliteral(L, "Signal received while polling file descriptors");
1302 return 2;
1303 } else {
1304 moonbr_io_errmsg();
1305 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1307 } else if (status == 0) {
1308 lua_pushboolean(L, 0);
1309 lua_pushliteral(L, "Timeout while polling file descriptors");
1310 return 2;
1311 } else {
1312 lua_pushboolean(L, 1);
1313 return 1;
1317 static int moonbr_io_timeref(lua_State *L) {
1318 lua_Number sub;
1319 struct timespec tp;
1320 sub = luaL_optnumber(L, 1, 0);
1321 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
1322 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
1324 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
1325 return 1;
1328 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1329 {"read", moonbr_io_read},
1330 {"read_nb", moonbr_io_read_nb},
1331 {"read_call", moonbr_io_read_call},
1332 {"read_yield", moonbr_io_read_yield},
1333 {"drain", moonbr_io_drain},
1334 {"drain_nb", moonbr_io_drain_nb},
1335 {"drain_call", moonbr_io_drain_call},
1336 {"drain_yield", moonbr_io_drain_yield},
1337 {"write", moonbr_io_write},
1338 {"write_nb", moonbr_io_write_nb},
1339 {"write_call", moonbr_io_write_call},
1340 {"write_yield", moonbr_io_write_yield},
1341 {"flush", moonbr_io_flush},
1342 {"flush_nb", moonbr_io_flush_nb},
1343 {"flush_call", moonbr_io_flush_call},
1344 {"flush_yield", moonbr_io_flush_yield},
1345 {"finish", moonbr_io_finish},
1346 {"close", moonbr_io_close},
1347 {"reset", moonbr_io_reset},
1348 {NULL, NULL}
1349 };
1351 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1352 {"__index", moonbr_io_handleindex},
1353 {"__newindex", moonbr_io_handlenewindex},
1354 {"__gc", moonbr_io_handlegc},
1355 {NULL, NULL}
1356 };
1358 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1359 {"accept", moonbr_io_accept},
1360 {"accept_nb", moonbr_io_accept_nb},
1361 {"close", moonbr_io_unlisten},
1362 {NULL, NULL}
1363 };
1365 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1366 {"__gc", moonbr_io_listenergc},
1367 {NULL, NULL}
1368 };
1370 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1371 {"localconnect", moonbr_io_localconnect},
1372 {"localconnect_nb", moonbr_io_localconnect_nb},
1373 {"tcpconnect", moonbr_io_tcpconnect},
1374 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1375 {"locallisten", moonbr_io_locallisten},
1376 {"tcplisten", moonbr_io_tcplisten},
1377 {"poll", moonbr_io_poll},
1378 {"timeref", moonbr_io_timeref},
1379 {NULL, NULL}
1380 };
1382 int luaopen_moonbridge_io(lua_State *L) {
1384 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
1386 lua_newtable(L); // module
1388 lua_newtable(L); // public metatable
1389 lua_newtable(L); // handle methods
1390 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1391 lua_pushvalue(L, -1);
1392 lua_setfield(L, -4, "handle_pt");
1393 lua_setfield(L, -2, "__index");
1394 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1396 lua_newtable(L); // handle metatable
1397 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1398 lua_pushvalue(L, -1);
1399 lua_setfield(L, -3, "handle_mt");
1400 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1402 lua_newtable(L); // listener metatable
1403 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1404 lua_newtable(L); // listener methods
1405 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1406 lua_pushvalue(L, -1);
1407 lua_setfield(L, -4, "listener_pt");
1408 lua_setfield(L, -2, "__index");
1409 lua_pushvalue(L, -1);
1410 lua_setfield(L, -3, "listener_mt");
1411 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1413 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1414 return 1;

Impressum / About Us