moonbridge

view moonbridge_io.c @ 112:e99265c3372d

New function moonbridge_io.locallisten(path) to listen on local (unix domain) sockets
author jbe
date Thu Apr 09 18:59:53 2015 +0200 (2015-04-09)
parents bff286e7496e
children 113185a57b06
line source
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 #include <sys/select.h>
10 #include <fcntl.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
13 #include <arpa/inet.h>
14 #include <sys/types.h>
15 #include <netdb.h>
17 #include <lua.h>
18 #include <lauxlib.h>
19 #include <lualib.h>
21 #define MOONBR_IO_MAXSTRERRORLEN 80
22 #define MOONBR_IO_READBUFLEN 4096
23 #define MOONBR_IO_WRITEBUFLEN 4096
25 #define MOONBR_IO_LISTEN_BACKLOG 1024
27 #define moonbr_io_errmsg() \
28 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
29 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
31 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
32 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
33 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
35 typedef struct {
36 int fd;
37 int issock;
38 sa_family_t addrfam;
39 int finished;
40 int closed;
41 int nonblocking;
42 int nopush;
43 int readerr;
44 int readbufin;
45 int readbufout;
46 int writeerr;
47 size_t writeleft;
48 #if LUA_VERSION_NUM >= 503
49 lua_Integer writeqin;
50 lua_Integer writeqout;
51 #else
52 int writeqin;
53 int writeqout;
54 #endif
55 size_t writeqoff;
56 int writebufin;
57 int writebufout;
58 char readbuf[MOONBR_IO_READBUFLEN];
59 char writebuf[MOONBR_IO_WRITEBUFLEN];
60 } moonbr_io_handle_t;
62 typedef struct {
63 int fd;
64 int nonblocking;
65 } moonbr_io_listener_t;
67 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
68 int flags;
69 if (handle->nonblocking == nonblocking) return;
70 flags = fcntl(handle->fd, F_GETFL, 0);
71 if (flags == -1) {
72 moonbr_io_errmsg();
73 close(handle->fd);
74 handle->fd = -1;
75 handle->closed = 1;
76 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
77 }
78 if (nonblocking) flags |= O_NONBLOCK;
79 else flags &= ~O_NONBLOCK;
80 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
81 moonbr_io_errmsg();
82 close(handle->fd);
83 handle->fd = -1;
84 handle->closed = 1;
85 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
86 }
87 handle->nonblocking = nonblocking;
88 }
90 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
91 struct linger lingerval = { 0, };
92 if (!handle->issock) return;
93 if (timeout >= 0) {
94 lingerval.l_onoff = 1;
95 lingerval.l_linger = timeout;
96 }
97 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
98 moonbr_io_errmsg();
99 close(handle->fd);
100 handle->fd = -1;
101 handle->closed = 1;
102 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
103 }
104 }
106 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
107 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
108 if (
109 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
110 handle->nopush == nopush
111 ) return;
112 #if defined(TCP_NOPUSH)
113 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
114 #elif defined(TCP_CORK)
115 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
116 #endif
117 moonbr_io_errmsg();
118 close(handle->fd);
119 handle->fd = -1;
120 handle->closed = 1;
121 #if defined(TCP_NOPUSH)
122 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
123 #elif defined(TCP_CORK)
124 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
125 #endif
126 }
127 handle->nopush = nopush;
128 #else
129 #warning Neither TCP_NOPUSH nor TCP_CORK is available
130 #endif
131 }
133 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
134 moonbr_io_handle_t *handle;
135 lua_Integer maxread;
136 const char *terminatorstr;
137 size_t terminatorlen;
138 char terminator;
139 luaL_Buffer luabuf;
140 size_t luabufcnt = 0;
141 int remaining;
142 char *terminatorpos;
143 ssize_t bytesread;
144 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
145 maxread = luaL_optinteger(L, 2, 0);
146 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
147 if (terminatorlen) {
148 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
149 terminator = terminatorstr[0];
150 }
151 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
152 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
153 if (handle->readerr) {
154 lua_pushnil(L);
155 lua_pushliteral(L, "Previous read error");
156 return 2;
157 }
158 handle->readerr = 1;
159 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
160 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
161 if (!drain) luaL_buffinit(L, &luabuf);
162 while (1) {
163 remaining = -1;
164 if (
165 maxread > 0 &&
166 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
167 ) {
168 remaining = maxread - luabufcnt;
169 } else if (terminatorlen) {
170 terminatorpos = memchr(
171 handle->readbuf + handle->readbufout,
172 terminator,
173 handle->readbufin - handle->readbufout
174 );
175 if (terminatorpos) remaining = 1 + (
176 terminatorpos - (handle->readbuf + handle->readbufout)
177 );
178 }
179 if (remaining >= 0) {
180 if (!drain) {
181 luaL_addlstring(
182 &luabuf,
183 handle->readbuf + handle->readbufout,
184 remaining
185 );
186 luaL_pushresult(&luabuf);
187 } else {
188 luaL_pushresult(&luabuf);
189 lua_pop(L, 1);
190 lua_pushinteger(L, luabufcnt + remaining);
191 }
192 handle->readbufout += remaining;
193 if (handle->readbufout == handle->readbufin) {
194 handle->readbufin = 0;
195 handle->readbufout =0;
196 }
197 handle->readerr = 0;
198 return 1;
199 }
200 if (!drain) luaL_addlstring(
201 &luabuf,
202 handle->readbuf + handle->readbufout,
203 handle->readbufin - handle->readbufout
204 );
205 luabufcnt += handle->readbufin - handle->readbufout;
206 do {
207 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
208 } while (bytesread < 0 && (errno == EINTR));
209 if (
210 bytesread == 0 || (
211 nonblocking &&
212 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
213 )
214 ) {
215 handle->readbufin = 0;
216 handle->readbufout = 0;
217 if (!drain) {
218 luaL_pushresult(&luabuf);
219 if (!luabufcnt && bytesread == 0) {
220 lua_pop(L, 1);
221 moonbr_io_read_impl_eof:
222 lua_pushboolean(L, 0);
223 lua_pushliteral(L, "End of file");
224 handle->readerr = 0;
225 return 2;
226 }
227 } else {
228 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
229 else lua_pushboolean(L, luabufcnt);
230 }
231 handle->readerr = 0;
232 return 1;
233 }
234 if (bytesread < 0) {
235 moonbr_io_errmsg();
236 lua_pushnil(L);
237 lua_pushstring(L, errmsg);
238 return 2;
239 }
240 handle->readbufin = bytesread;
241 handle->readbufout = 0;
242 }
243 }
245 static int moonbr_io_read(lua_State *L) {
246 return moonbr_io_read_impl(L, 0, 0);
247 }
249 static int moonbr_io_read_nb(lua_State *L) {
250 return moonbr_io_read_impl(L, 1, 0);
251 }
253 static int moonbr_io_drain(lua_State *L) {
254 return moonbr_io_read_impl(L, 0, 1);
255 }
257 static int moonbr_io_drain_nb(lua_State *L) {
258 return moonbr_io_read_impl(L, 1, 1);
259 }
261 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
262 moonbr_io_handle_t *handle;
263 int i, top;
264 const char *str;
265 size_t strlen;
266 ssize_t written;
267 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
268 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
269 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
270 if (handle->writeerr) {
271 lua_pushnil(L);
272 lua_pushliteral(L, "Previous write error");
273 return 2;
274 }
275 handle->writeerr = 1;
276 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
277 top = lua_gettop(L);
278 lua_getuservalue(L, 1);
279 lua_getfield(L, -1, "writequeue");
280 for (i=2; i<=top; i++) {
281 luaL_checklstring(L, i, &strlen);
282 lua_pushvalue(L, i);
283 lua_rawseti(L, -2, handle->writeqin++);
284 handle->writeleft += strlen;
285 }
286 while (handle->writeqout != handle->writeqin) {
287 lua_rawgeti(L, -1, handle->writeqout);
288 str = lua_tolstring(L, -1, &strlen);
289 while (handle->writeqoff < strlen) {
290 if (
291 strlen - handle->writeqoff <=
292 MOONBR_IO_WRITEBUFLEN - handle->writebufin
293 ) {
294 memcpy(
295 handle->writebuf + handle->writebufin,
296 str + handle->writeqoff,
297 strlen - handle->writeqoff
298 );
299 handle->writebufin += strlen - handle->writeqoff;
300 break;
301 } else {
302 moonbr_io_handle_set_nopush(L, handle, 1);
303 memcpy(
304 handle->writebuf + handle->writebufin,
305 str + handle->writeqoff,
306 MOONBR_IO_WRITEBUFLEN - handle->writebufin
307 );
308 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
309 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
310 written = write(
311 handle->fd,
312 handle->writebuf + handle->writebufout,
313 MOONBR_IO_WRITEBUFLEN - handle->writebufout
314 );
315 if (written < 0) {
316 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
317 goto moonbr_io_write_impl_block;
318 } else if (errno != EINTR) {
319 moonbr_io_errmsg();
320 lua_pushnil(L);
321 lua_pushstring(L, errmsg);
322 return 2;
323 }
324 } else {
325 handle->writebufout += written;
326 handle->writeleft -= written;
327 }
328 }
329 handle->writebufin = 0;
330 handle->writebufout = 0;
331 }
332 }
333 handle->writeqoff = 0;
334 lua_pop(L, 1);
335 lua_pushnil(L);
336 lua_rawseti(L, -2, handle->writeqout++);
337 }
338 if (flush) {
339 moonbr_io_handle_set_nopush(L, handle, 0);
340 while (handle->writebufout < handle->writebufin) {
341 written = write(
342 handle->fd,
343 handle->writebuf + handle->writebufout,
344 handle->writebufin - handle->writebufout
345 );
346 if (written < 0) {
347 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
348 goto moonbr_io_write_impl_block;
349 } else if (errno != EINTR) {
350 moonbr_io_errmsg();
351 lua_pushnil(L);
352 lua_pushstring(L, errmsg);
353 return 2;
354 }
355 } else {
356 handle->writebufout += written;
357 handle->writeleft -= written;
358 }
359 }
360 handle->writebufin = 0;
361 handle->writebufout = 0;
362 }
363 if (nonblocking) lua_pushinteger(L, 0);
364 else lua_pushvalue(L, 1);
365 handle->writeerr = 0;
366 return 1;
367 moonbr_io_write_impl_block:
368 lua_pushinteger(L, handle->writeleft);
369 handle->writeerr = 0;
370 return 1;
371 }
373 static int moonbr_io_write(lua_State *L) {
374 return moonbr_io_write_impl(L, 0, 0);
375 }
377 static int moonbr_io_write_nb(lua_State *L) {
378 return moonbr_io_write_impl(L, 1, 0);
379 }
381 static int moonbr_io_flush(lua_State *L) {
382 return moonbr_io_write_impl(L, 0, 1);
383 }
385 static int moonbr_io_flush_nb(lua_State *L) {
386 return moonbr_io_write_impl(L, 1, 1);
387 }
389 static int moonbr_io_finish(lua_State *L) {
390 moonbr_io_handle_t *handle;
391 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
392 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
393 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
394 if (handle->writeleft) {
395 lua_pushcfunction(L, moonbr_io_flush);
396 lua_pushvalue(L, 1);
397 lua_call(L, 1, 2);
398 if (!lua_toboolean(L, -2)) {
399 handle->finished = 1;
400 return 2;
401 }
402 }
403 handle->finished = 1;
404 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
405 if (shutdown(handle->fd, SHUT_WR)) {
406 moonbr_io_errmsg();
407 lua_pushnil(L);
408 lua_pushstring(L, errmsg);
409 return 2;
410 }
411 } else {
412 if (close(handle->fd)) {
413 moonbr_io_errmsg();
414 handle->fd = -1;
415 lua_pushnil(L);
416 lua_pushstring(L, errmsg);
417 return 2;
418 }
419 handle->fd = -1; /* fake EOF on read */
420 }
421 lua_pushboolean(L, 1);
422 return 1;
423 }
425 static int moonbr_io_close_impl(lua_State *L, int reset) {
426 moonbr_io_handle_t *handle;
427 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
428 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
429 if (!reset) {
430 if (handle->writeleft) {
431 lua_pushcfunction(L, moonbr_io_flush);
432 lua_pushvalue(L, 1);
433 lua_call(L, 1, 2);
434 if (!lua_toboolean(L, -2)) {
435 close(handle->fd);
436 handle->fd = -1;
437 return 2;
438 }
439 }
440 moonbr_io_handle_set_linger(L, handle, -1);
441 }
442 if (handle->fd >= 0) {
443 if (close(handle->fd)) {
444 moonbr_io_errmsg();
445 handle->fd = -1;
446 lua_pushnil(L);
447 lua_pushstring(L, errmsg);
448 return 2;
449 }
450 handle->fd = -1;
451 }
452 lua_pushboolean(L, 1);
453 return 1;
455 }
457 static int moonbr_io_close(lua_State *L) {
458 return moonbr_io_close_impl(L, 0);
459 }
461 static int moonbr_io_reset(lua_State *L) {
462 return moonbr_io_close_impl(L, 1);
463 }
465 static int moonbr_io_handlegc(lua_State *L) {
466 moonbr_io_handle_t *handle;
467 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
468 if (handle->fd >= 0) {
469 lua_pushcfunction(L, moonbr_io_close);
470 lua_pushvalue(L, 1);
471 lua_pushinteger(L, 0);
472 lua_call(L, 2, 0);
473 }
474 return 0;
475 }
477 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
478 moonbr_io_handle_t *handle;
479 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
480 if (!handle->closed) {
481 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
482 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
483 lua_call(L, 1, 0);
484 }
485 }
487 void moonbr_io_pushhandle(lua_State *L, int fd) {
488 moonbr_io_handle_t *handle;
489 struct sockaddr addr;
490 socklen_t addrlen;
491 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
492 handle->fd = fd;
493 addrlen = sizeof(addr);
494 if (getsockname(fd, &addr, &addrlen)) {
495 if (errno != ENOTSOCK) {
496 moonbr_io_errmsg();
497 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
498 }
499 handle->issock = 0;
500 } else {
501 handle->issock = 1;
502 handle->addrfam = addr.sa_family;
503 }
504 handle->finished = 0;
505 handle->closed = 0;
506 handle->nonblocking = -1;
507 handle->nopush = -1;
508 handle->readerr = 0;
509 handle->readbufin = 0;
510 handle->readbufout = 0;
511 handle->writeerr = 0;
512 handle->writeleft = 0;
513 handle->writeqin = 0;
514 handle->writeqout = 0;
515 handle->writeqoff = 0;
516 handle->writebufin = 0;
517 handle->writebufout = 0;
518 moonbr_io_handle_set_linger(L, handle, 0);
519 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
520 lua_setmetatable(L, -2);
521 lua_newtable(L); // uservalue
522 lua_newtable(L);
523 lua_setfield(L, -2, "writequeue");
524 lua_newtable(L); // public
525 if (handle->addrfam == AF_INET6) {
526 struct sockaddr_in6 addr_in6;
527 char addrstrbuf[INET6_ADDRSTRLEN];
528 const char *addrstr;
529 addrlen = sizeof(addr_in6);
530 if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
531 moonbr_io_errmsg();
532 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
533 }
534 if (addrlen > sizeof(addr_in6)) {
535 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
536 }
537 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
538 if (!addrstr) {
539 moonbr_io_errmsg();
540 luaL_error(L, "Could not format local IP address: %s", errmsg);
541 } else {
542 lua_pushstring(L, addrstr);
543 lua_setfield(L, -2, "local_ip6");
544 }
545 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
546 lua_setfield(L, -2, "local_tcpport");
547 if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
548 moonbr_io_errmsg();
549 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
550 }
551 if (addrlen > sizeof(addr_in6)) {
552 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
553 }
554 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
555 if (!addrstr) {
556 moonbr_io_errmsg();
557 luaL_error(L, "Could not format remote IP address: %s", errmsg);
558 } else {
559 lua_pushstring(L, addrstr);
560 lua_setfield(L, -2, "remote_ip6");
561 }
562 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
563 lua_setfield(L, -2, "remote_tcpport");
564 } else if (handle->addrfam == AF_INET) {
565 struct sockaddr_in addr_in;
566 char addrstrbuf[INET_ADDRSTRLEN];
567 const char *addrstr;
568 addrlen = sizeof(addr_in);
569 if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) {
570 moonbr_io_errmsg();
571 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
572 }
573 if (addrlen > sizeof(addr_in)) {
574 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
575 }
576 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
577 if (!addrstr) {
578 moonbr_io_errmsg();
579 luaL_error(L, "Could not format local IP address: %s", errmsg);
580 } else {
581 lua_pushstring(L, addrstr);
582 lua_setfield(L, -2, "local_ip4");
583 }
584 lua_pushinteger(L, ntohs(addr_in.sin_port));
585 lua_setfield(L, -2, "local_tcpport");
586 if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) {
587 moonbr_io_errmsg();
588 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
589 }
590 if (addrlen > sizeof(addr_in)) {
591 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
592 }
593 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
594 if (!addrstr) {
595 moonbr_io_errmsg();
596 luaL_error(L, "Could not format remote IP address: %s", errmsg);
597 } else {
598 lua_pushstring(L, addrstr);
599 lua_setfield(L, -2, "remote_ip4");
600 }
601 lua_pushinteger(L, ntohs(addr_in.sin_port));
602 lua_setfield(L, -2, "remote_tcpport");
603 }
604 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
605 lua_setmetatable(L, -2);
606 lua_setfield(L, -2, "public");
607 lua_setuservalue(L, -2);
608 }
610 static int moonbr_io_handleindex(lua_State *L) {
611 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
612 lua_getuservalue(L, 1);
613 lua_getfield(L, -1, "public");
614 lua_pushvalue(L, 2);
615 lua_gettable(L, -2);
616 return 1;
617 }
619 static int moonbr_io_handlenewindex(lua_State *L) {
620 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
621 lua_getuservalue(L, 1);
622 lua_getfield(L, -1, "public");
623 lua_pushvalue(L, 2);
624 lua_pushvalue(L, 3);
625 lua_settable(L, -3);
626 return 0;
627 }
629 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
630 const char *path;
631 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
632 const int path_maxlen = sizeof(struct sockaddr_un) - (
633 (void *)sockaddr.sun_path - (void *)&sockaddr
634 ) - 1; /* one byte for termination */
635 int sock;
636 path = luaL_checkstring(L, 1);
637 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
638 strcpy(sockaddr.sun_path, path);
639 sock = socket(
640 PF_LOCAL,
641 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
642 0
643 );
644 if (sock < 0) {
645 moonbr_io_errmsg();
646 lua_pushnil(L);
647 lua_pushstring(L, errmsg);
648 return 2;
649 }
650 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
651 if (!nonblocking && errno == EINTR) {
652 moonbr_io_errmsg();
653 close(sock);
654 lua_pushnil(L);
655 lua_pushstring(L, errmsg);
656 return 2;
657 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
658 moonbr_io_errmsg();
659 lua_pushnil(L);
660 lua_pushstring(L, errmsg);
661 return 2;
662 }
663 }
664 moonbr_io_pushhandle(L, sock);
665 return 1;
666 }
668 static int moonbr_io_localconnect(lua_State *L) {
669 return moonbr_io_localconnect_impl(L, 0);
670 }
672 static int moonbr_io_localconnect_nb(lua_State *L) {
673 return moonbr_io_localconnect_impl(L, 1);
674 }
676 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
677 const char *host, *port;
678 struct addrinfo hints = { 0, };
679 struct addrinfo *res, *addrinfo;
680 int errcode;
681 int sock;
682 host = luaL_checkstring(L, 1);
683 port = luaL_checkstring(L, 2);
684 hints.ai_family = AF_UNSPEC;
685 hints.ai_socktype = SOCK_STREAM;
686 hints.ai_protocol = IPPROTO_TCP;
687 hints.ai_flags = AI_ADDRCONFIG;
688 errcode = getaddrinfo(host, port, &hints, &res);
689 if (errcode) {
690 freeaddrinfo(res);
691 if (errcode == EAI_SYSTEM) {
692 moonbr_io_errmsg();
693 lua_pushnil(L);
694 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
695 } else {
696 lua_pushnil(L);
697 lua_pushstring(L, gai_strerror(errcode));
698 }
699 return 2;
700 }
701 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
702 if (addrinfo->ai_family == PF_INET6) goto moonbr_io_tcpconnect_found;
703 }
704 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
705 if (addrinfo->ai_family == PF_INET) goto moonbr_io_tcpconnect_found;
706 }
707 addrinfo = res;
708 moonbr_io_tcpconnect_found:
709 sock = socket(
710 addrinfo->ai_family,
711 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
712 addrinfo->ai_protocol
713 );
714 if (sock < 0) {
715 moonbr_io_errmsg();
716 freeaddrinfo(res);
717 lua_pushnil(L);
718 lua_pushstring(L, errmsg);
719 return 2;
720 }
721 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
722 freeaddrinfo(res);
723 if (!nonblocking && errno == EINTR) {
724 moonbr_io_errmsg();
725 close(sock);
726 lua_pushnil(L);
727 lua_pushstring(L, errmsg);
728 return 2;
729 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
730 moonbr_io_errmsg();
731 lua_pushnil(L);
732 lua_pushstring(L, errmsg);
733 return 2;
734 }
735 } else {
736 freeaddrinfo(res);
737 }
738 moonbr_io_pushhandle(L, sock);
739 return 1;
740 }
742 static int moonbr_io_tcpconnect(lua_State *L) {
743 return moonbr_io_tcpconnect_impl(L, 0);
744 }
746 static int moonbr_io_tcpconnect_nb(lua_State *L) {
747 return moonbr_io_tcpconnect_impl(L, 1);
748 }
750 static int moonbr_io_locallisten(lua_State *L) {
751 moonbr_io_listener_t *listener;
752 const char *path;
753 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
754 const int path_maxlen = sizeof(struct sockaddr_un) - (
755 (void *)sockaddr.sun_path - (void *)&sockaddr
756 ) - 1; /* one byte for termination */
757 int sock;
758 path = luaL_checkstring(L, 1);
759 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
760 strcpy(sockaddr.sun_path, path);
761 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
762 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
763 sock = socket(
764 PF_LOCAL,
765 SOCK_STREAM | SOCK_CLOEXEC,
766 0
767 );
768 if (sock < 0) {
769 moonbr_io_errmsg();
770 lua_pushnil(L);
771 lua_pushstring(L, errmsg);
772 return 2;
773 }
774 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
775 moonbr_io_errmsg();
776 close(sock);
777 lua_pushnil(L);
778 lua_pushstring(L, errmsg);
779 return 2;
780 }
781 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
782 moonbr_io_errmsg();
783 close(sock);
784 lua_pushnil(L);
785 lua_pushstring(L, errmsg);
786 return 2;
787 }
788 listener->fd = sock;
789 listener->nonblocking = -1;
790 return 1;
791 }
793 static int moonbr_io_tcplisten(lua_State *L) {
794 moonbr_io_listener_t *listener;
795 const char *host, *port;
796 struct addrinfo hints = { 0, };
797 struct addrinfo *res, *addrinfo;
798 int errcode;
799 int sock;
800 host = luaL_optstring(L, 1, NULL);
801 port = luaL_checkstring(L, 2);
802 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
803 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
804 hints.ai_family = AF_UNSPEC;
805 hints.ai_socktype = SOCK_STREAM;
806 hints.ai_protocol = IPPROTO_TCP;
807 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
808 errcode = getaddrinfo(host, port, &hints, &res);
809 if (errcode) {
810 freeaddrinfo(res);
811 if (errcode == EAI_SYSTEM) {
812 moonbr_io_errmsg();
813 lua_pushnil(L);
814 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
815 } else {
816 lua_pushnil(L);
817 lua_pushstring(L, gai_strerror(errcode));
818 }
819 return 2;
820 }
821 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
822 if (addrinfo->ai_family == PF_INET6) goto moonbr_io_tcpconnect_found;
823 }
824 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
825 if (addrinfo->ai_family == PF_INET) goto moonbr_io_tcpconnect_found;
826 }
827 addrinfo = res;
828 moonbr_io_tcpconnect_found:
829 sock = socket(
830 addrinfo->ai_family,
831 addrinfo->ai_socktype | SOCK_CLOEXEC,
832 addrinfo->ai_protocol
833 );
834 if (sock < 0) {
835 moonbr_io_errmsg();
836 freeaddrinfo(res);
837 lua_pushnil(L);
838 lua_pushstring(L, errmsg);
839 return 2;
840 }
841 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
842 moonbr_io_errmsg();
843 freeaddrinfo(res);
844 close(sock);
845 lua_pushnil(L);
846 lua_pushstring(L, errmsg);
847 return 2;
848 }
849 freeaddrinfo(res);
850 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
851 moonbr_io_errmsg();
852 close(sock);
853 lua_pushnil(L);
854 lua_pushstring(L, errmsg);
855 return 2;
856 }
857 listener->fd = sock;
858 listener->nonblocking = -1;
859 return 1;
860 }
862 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
863 moonbr_io_listener_t *listener;
864 int fd;
865 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
866 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
867 if (listener->nonblocking != nonblocking) {
868 int flags;
869 flags = fcntl(listener->fd, F_GETFL, 0);
870 if (flags == -1) {
871 moonbr_io_errmsg();
872 close(listener->fd);
873 listener->fd = -1;
874 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
875 }
876 if (nonblocking) flags |= O_NONBLOCK;
877 else flags &= ~O_NONBLOCK;
878 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
879 moonbr_io_errmsg();
880 close(listener->fd);
881 listener->fd = -1;
882 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
883 }
884 listener->nonblocking = nonblocking;
885 }
886 while (1) {
887 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
888 if (fd < 0) {
889 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
890 lua_pushboolean(L, 0);
891 lua_pushliteral(L, "No incoming connection pending");
892 return 2;
893 } else if (errno != EINTR) {
894 moonbr_io_errmsg();
895 lua_pushnil(L);
896 lua_pushstring(L, errmsg);
897 return 2;
898 }
899 } else {
900 moonbr_io_pushhandle(L, fd);
901 return 1;
902 }
903 }
904 }
906 static int moonbr_io_accept(lua_State *L) {
907 return moonbr_io_accept_impl(L, 0);
908 }
910 static int moonbr_io_accept_nb(lua_State *L) {
911 return moonbr_io_accept_impl(L, 1);
912 }
914 static int moonbr_io_unlisten(lua_State *L) {
915 moonbr_io_listener_t *listener;
916 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
917 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
918 if (close(listener->fd)) {
919 moonbr_io_errmsg();
920 listener->fd = -1;
921 lua_pushnil(L);
922 lua_pushstring(L, errmsg);
923 return 2;
924 }
925 listener->fd = -1;
926 lua_pushboolean(L, 1);
927 return 1;
928 }
930 static int moonbr_io_listenergc(lua_State *L) {
931 moonbr_io_listener_t *listener;
932 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
933 if (listener->fd) close(listener->fd);
934 listener->fd = -1;
935 return 0;
936 }
938 static int moonbr_io_poll(lua_State *L) {
939 moonbr_io_handle_t *handle;
940 moonbr_io_listener_t *listener;
941 int fd, isnum;
942 int nfds = 0;
943 fd_set readfds, writefds, exceptfds;
944 struct timeval timeout = {0, };
945 int status;
946 FD_ZERO(&readfds);
947 FD_ZERO(&writefds);
948 FD_ZERO(&exceptfds);
949 if (!lua_isnoneornil(L, 1)) {
950 luaL_checktype(L, 1, LUA_TTABLE);
951 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
952 if (lua_toboolean(L, -1)) {
953 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
954 if (handle) {
955 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
956 fd = handle->fd;
957 if (fd < 0) { /* fake EOF to simulate shutdown */
958 lua_pushboolean(L, 1);
959 return 1;
960 }
961 } else {
962 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
963 if (listener) {
964 fd = listener->fd;
965 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
966 } else {
967 fd = lua_tointegerx(L, -2, &isnum);
968 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
969 }
970 }
971 FD_SET(fd, &readfds);
972 if (fd+1 > nfds) nfds = fd+1;
973 }
974 }
975 }
976 if (!lua_isnoneornil(L, 2)) {
977 luaL_checktype(L, 2, LUA_TTABLE);
978 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
979 if (lua_toboolean(L, -1)) {
980 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
981 if (handle) {
982 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
983 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
984 fd = handle->fd;
985 } else {
986 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
987 if (listener) luaL_error(L, "Attempt to write-poll a listener");
988 fd = lua_tointegerx(L, -2, &isnum);
989 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
990 }
991 FD_SET(fd, &writefds);
992 if (fd+1 > nfds) nfds = fd+1;
993 }
994 }
995 }
996 if (!lua_isnoneornil(L, 3)) {
997 lua_Number n;
998 n = lua_tonumberx(L, 3, &isnum);
999 if (isnum && n>=0 && n<100000000) {
1000 timeout.tv_sec = n;
1001 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
1002 } else {
1003 luaL_argcheck(L, 0, 3, "not a valid timeout");
1005 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
1006 } else {
1007 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
1009 if (status == -1) {
1010 if (errno == EINTR) {
1011 lua_pushboolean(L, 0);
1012 lua_pushliteral(L, "Signal received while polling file descriptors");
1013 return 2;
1014 } else {
1015 moonbr_io_errmsg();
1016 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
1018 } else if (status == 0) {
1019 lua_pushboolean(L, 0);
1020 lua_pushliteral(L, "Timeout while polling file descriptors");
1021 return 2;
1022 } else {
1023 lua_pushboolean(L, 1);
1024 return 1;
1028 static const struct luaL_Reg moonbr_io_handle_methods[] = {
1029 {"read", moonbr_io_read},
1030 {"read_nb", moonbr_io_read_nb},
1031 {"drain", moonbr_io_drain},
1032 {"drain_nb", moonbr_io_drain_nb},
1033 {"write", moonbr_io_write},
1034 {"write_nb", moonbr_io_write_nb},
1035 {"flush", moonbr_io_flush},
1036 {"flush_nb", moonbr_io_flush_nb},
1037 {"finish", moonbr_io_finish},
1038 {"close", moonbr_io_close},
1039 {"reset", moonbr_io_reset},
1040 {NULL, NULL}
1041 };
1043 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
1044 {"__index", moonbr_io_handleindex},
1045 {"__newindex", moonbr_io_handlenewindex},
1046 {"__gc", moonbr_io_handlegc},
1047 {NULL, NULL}
1048 };
1050 static const struct luaL_Reg moonbr_io_listener_methods[] = {
1051 {"accept", moonbr_io_accept},
1052 {"accept_nb", moonbr_io_accept_nb},
1053 {"close", moonbr_io_unlisten},
1054 {NULL, NULL}
1055 };
1057 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
1058 {"__gc", moonbr_io_listenergc},
1059 {NULL, NULL}
1060 };
1062 static const struct luaL_Reg moonbr_io_module_funcs[] = {
1063 {"localconnect", moonbr_io_localconnect},
1064 {"localconnect_nb", moonbr_io_localconnect_nb},
1065 {"tcpconnect", moonbr_io_tcpconnect},
1066 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
1067 {"locallisten", moonbr_io_locallisten},
1068 {"tcplisten", moonbr_io_tcplisten},
1069 {"poll", moonbr_io_poll},
1070 {NULL, NULL}
1071 };
1073 int luaopen_moonbridge_io(lua_State *L) {
1075 lua_newtable(L); // module
1077 lua_newtable(L); // public metatable
1078 lua_newtable(L); // handle methods
1079 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
1080 lua_pushvalue(L, -1);
1081 lua_setfield(L, -4, "prototype_handle");
1082 lua_setfield(L, -2, "__index");
1083 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
1085 lua_newtable(L); // handle metatable
1086 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
1087 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
1089 lua_newtable(L); // listener metatable
1090 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
1091 lua_newtable(L); // listener methods
1092 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
1093 lua_pushvalue(L, -1);
1094 lua_setfield(L, -4, "prototype_listener");
1095 lua_setfield(L, -2, "__index");
1096 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
1098 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
1099 return 1;

Impressum / About Us