moonbridge

view moonbridge_io.c @ 104:5e8eeb5b6c84

Some code cleanup in I/O library
author jbe
date Wed Apr 08 18:38:14 2015 +0200 (2015-04-08)
parents 4f9e4c6109f4
children 11d03470ef19
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/select.h>
9 #include <fcntl.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <sys/types.h>
13 #include <netdb.h>
15 #include <lua.h>
16 #include <lauxlib.h>
17 #include <lualib.h>
19 #define MOONBR_IO_MAXSTRERRORLEN 80
20 #define MOONBR_IO_READBUFLEN 4096
21 #define MOONBR_IO_WRITEBUFLEN 4096
23 #define moonbr_io_errmsg() \
24 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
25 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
27 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
28 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
30 typedef struct {
31 int fd;
32 int isnetwork;
33 int finished;
34 int closed;
35 int nonblocking;
36 int nopush;
37 int readerr;
38 int readbufcnt;
39 int writeerr;
40 size_t writeleft;
41 #if LUA_VERSION_NUM >= 503
42 lua_Integer writeqin;
43 lua_Integer writeqout;
44 #else
45 int writeqin;
46 int writeqout;
47 #endif
48 size_t writeqoff;
49 int writebufin;
50 int writebufout;
51 char readbuf[MOONBR_IO_READBUFLEN];
52 char writebuf[MOONBR_IO_WRITEBUFLEN];
53 } moonbr_io_handle_t;
55 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
56 int flags;
57 if (handle->nonblocking == nonblocking) return;
58 flags = fcntl(handle->fd, F_GETFL, 0);
59 if (flags == -1) {
60 moonbr_io_errmsg();
61 close(handle->fd);
62 handle->fd = -1;
63 handle->closed = 1;
64 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
65 }
66 if (nonblocking) flags |= O_NONBLOCK;
67 else flags &= ~O_NONBLOCK;
68 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
69 moonbr_io_errmsg();
70 close(handle->fd);
71 handle->fd = -1;
72 handle->closed = 1;
73 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
74 }
75 handle->nonblocking = nonblocking;
76 }
78 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
79 struct linger lingerval = { 0, };
80 if (!handle->isnetwork) return;
81 if (timeout >= 0) {
82 lingerval.l_onoff = 1;
83 lingerval.l_linger = timeout;
84 }
85 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
86 moonbr_io_errmsg();
87 close(handle->fd);
88 handle->fd = -1;
89 handle->closed = 1;
90 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
91 }
92 }
94 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
95 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
96 if (!handle->isnetwork || handle->nopush == nopush) return;
97 #if defined(TCP_NOPUSH)
98 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
99 #elif defined(TCP_CORK)
100 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
101 #endif
102 moonbr_io_errmsg();
103 close(handle->fd);
104 handle->fd = -1;
105 handle->closed = 1;
106 #if defined(TCP_NOPUSH)
107 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
108 #elif defined(TCP_CORK)
109 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
110 #endif
111 }
112 handle->nopush = nopush;
113 #else
114 #warning Neither TCP_NOPUSH nor TCP_CORK is available
115 #endif
116 }
118 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
119 moonbr_io_handle_t *handle;
120 lua_Integer maxread;
121 const char *terminatorstr;
122 size_t terminatorlen;
123 char terminator;
124 luaL_Buffer luabuf;
125 size_t luabufcnt = 0;
126 int endcnt;
127 char *terminatorpos;
128 ssize_t bytesread;
129 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
130 maxread = luaL_optinteger(L, 2, 0);
131 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
132 if (terminatorlen) {
133 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
134 terminator = terminatorstr[0];
135 }
136 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
137 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
138 if (handle->readerr) {
139 lua_pushnil(L);
140 lua_pushliteral(L, "Previous read error");
141 return 2;
142 }
143 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
144 handle->readerr = 1;
145 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
146 if (!drain) luaL_buffinit(L, &luabuf);
147 while (1) {
148 endcnt = -1;
149 if (maxread > 0 && handle->readbufcnt >= (size_t)maxread - luabufcnt) {
150 endcnt = maxread - luabufcnt;
151 } else if (terminatorlen) {
152 terminatorpos = memchr(handle->readbuf, terminator, handle->readbufcnt);
153 if (terminatorpos) endcnt = 1 + (terminatorpos - handle->readbuf);
154 }
155 if (endcnt >= 0) {
156 if (!drain) {
157 luaL_addlstring(&luabuf, handle->readbuf, endcnt);
158 luaL_pushresult(&luabuf);
159 } else {
160 luabufcnt += handle->readbufcnt;
161 lua_pushinteger(L, luabufcnt);
162 }
163 handle->readbufcnt -= endcnt;
164 memmove(handle->readbuf, handle->readbuf + endcnt, handle->readbufcnt);
165 handle->readerr = 0;
166 return 1;
167 }
168 if (!drain) luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
169 luabufcnt += handle->readbufcnt;
170 handle->readbufcnt = 0;
171 do {
172 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
173 } while (bytesread < 0 && (errno == EINTR));
174 if (bytesread == 0 || (nonblocking && bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) break;
175 if (bytesread < 0) {
176 moonbr_io_errmsg();
177 lua_pushnil(L);
178 lua_pushstring(L, errmsg);
179 return 2;
180 }
181 handle->readbufcnt += bytesread;
182 }
183 if (!drain) {
184 luaL_addlstring(&luabuf, handle->readbuf, handle->readbufcnt);
185 luaL_pushresult(&luabuf);
186 }
187 luabufcnt += handle->readbufcnt;
188 handle->readbufcnt = 0;
189 if (!drain) {
190 if (!luabufcnt && bytesread == 0) {
191 handle->readerr = 0;
192 moonbr_io_read_impl_eof:
193 lua_pushboolean(L, 0);
194 lua_pushliteral(L, "End of file");
195 return 2;
196 }
197 } else {
198 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
199 else lua_pushboolean(L, luabufcnt);
200 }
201 handle->readerr = 0;
202 return 1;
203 }
205 static int moonbr_io_read(lua_State *L) {
206 return moonbr_io_read_impl(L, 0, 0);
207 }
209 static int moonbr_io_read_nb(lua_State *L) {
210 return moonbr_io_read_impl(L, 1, 0);
211 }
213 static int moonbr_io_drain(lua_State *L) {
214 return moonbr_io_read_impl(L, 0, 1);
215 }
217 static int moonbr_io_drain_nb(lua_State *L) {
218 return moonbr_io_read_impl(L, 1, 1);
219 }
221 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
222 moonbr_io_handle_t *handle;
223 int i, top;
224 const char *str;
225 size_t strlen;
226 ssize_t written;
227 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
228 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
229 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
230 if (handle->writeerr) {
231 lua_pushnil(L);
232 lua_pushliteral(L, "Previous write error");
233 return 2;
234 }
235 handle->writeerr = 1;
236 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
237 top = lua_gettop(L);
238 lua_getuservalue(L, 1);
239 lua_getfield(L, -1, "writequeue");
240 for (i=2; i<=top; i++) {
241 luaL_checklstring(L, i, &strlen);
242 lua_pushvalue(L, i);
243 lua_rawseti(L, -2, handle->writeqin++);
244 handle->writeleft += strlen;
245 }
246 while (handle->writeqout != handle->writeqin) {
247 lua_rawgeti(L, -1, handle->writeqout);
248 str = lua_tolstring(L, -1, &strlen);
249 while (handle->writeqoff < strlen) {
250 if (
251 strlen - handle->writeqoff <=
252 MOONBR_IO_WRITEBUFLEN - handle->writebufin
253 ) {
254 memcpy(
255 handle->writebuf + handle->writebufin,
256 str + handle->writeqoff,
257 strlen - handle->writeqoff
258 );
259 handle->writebufin += strlen - handle->writeqoff;
260 break;
261 } else {
262 moonbr_io_handle_set_nopush(L, handle, 1);
263 memcpy(
264 handle->writebuf + handle->writebufin,
265 str + handle->writeqoff,
266 MOONBR_IO_WRITEBUFLEN - handle->writebufin
267 );
268 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
269 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
270 written = write(
271 handle->fd,
272 handle->writebuf + handle->writebufout,
273 MOONBR_IO_WRITEBUFLEN - handle->writebufout
274 );
275 if (written < 0) {
276 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
277 goto moonbr_io_write_impl_block;
278 } else if (errno != EINTR) {
279 moonbr_io_errmsg();
280 lua_pushnil(L);
281 lua_pushstring(L, errmsg);
282 return 2;
283 }
284 } else {
285 handle->writebufout += written;
286 handle->writeleft -= written;
287 }
288 }
289 handle->writebufin = 0;
290 handle->writebufout = 0;
291 }
292 }
293 handle->writeqoff = 0;
294 lua_pop(L, 1);
295 lua_pushnil(L);
296 lua_rawseti(L, -2, handle->writeqout++);
297 }
298 if (flush) {
299 moonbr_io_handle_set_nopush(L, handle, 0);
300 while (handle->writebufout < handle->writebufin) {
301 written = write(
302 handle->fd,
303 handle->writebuf + handle->writebufout,
304 handle->writebufin - handle->writebufout
305 );
306 if (written < 0) {
307 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
308 goto moonbr_io_write_impl_block;
309 } else if (errno != EINTR) {
310 moonbr_io_errmsg();
311 lua_pushnil(L);
312 lua_pushstring(L, errmsg);
313 return 2;
314 }
315 } else {
316 handle->writebufout += written;
317 handle->writeleft -= written;
318 }
319 }
320 handle->writebufin = 0;
321 handle->writebufout = 0;
322 }
323 if (nonblocking) lua_pushinteger(L, 0);
324 else lua_pushvalue(L, 1);
325 handle->writeerr = 0;
326 return 1;
327 moonbr_io_write_impl_block:
328 lua_pushinteger(L, handle->writeleft);
329 handle->writeerr = 0;
330 return 1;
331 }
333 static int moonbr_io_write(lua_State *L) {
334 return moonbr_io_write_impl(L, 0, 0);
335 }
337 static int moonbr_io_write_nb(lua_State *L) {
338 return moonbr_io_write_impl(L, 1, 0);
339 }
341 static int moonbr_io_flush(lua_State *L) {
342 return moonbr_io_write_impl(L, 0, 1);
343 }
345 static int moonbr_io_flush_nb(lua_State *L) {
346 return moonbr_io_write_impl(L, 1, 1);
347 }
349 static int moonbr_io_finish(lua_State *L) {
350 moonbr_io_handle_t *handle;
351 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
352 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
353 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
354 if (handle->writeleft) {
355 lua_pushcfunction(L, moonbr_io_flush);
356 lua_pushvalue(L, 1);
357 lua_call(L, 1, 2);
358 if (!lua_toboolean(L, -2)) {
359 handle->finished = 1;
360 return 2;
361 }
362 }
363 handle->finished = 1;
364 if (handle->isnetwork) {
365 if (shutdown(handle->fd, SHUT_WR)) {
366 moonbr_io_errmsg();
367 lua_pushnil(L);
368 lua_pushstring(L, errmsg);
369 return 2;
370 }
371 } else {
372 if (close(handle->fd)) {
373 moonbr_io_errmsg();
374 handle->fd = -1;
375 lua_pushnil(L);
376 lua_pushstring(L, errmsg);
377 return 2;
378 }
379 handle->fd = -1; /* fake EOF on read */
380 }
381 lua_pushboolean(L, 1);
382 return 1;
383 }
385 static int moonbr_io_close_impl(lua_State *L, int reset) {
386 moonbr_io_handle_t *handle;
387 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
388 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
389 if (!reset) {
390 if (handle->writeleft) {
391 lua_pushcfunction(L, moonbr_io_flush);
392 lua_pushvalue(L, 1);
393 lua_call(L, 1, 2);
394 if (!lua_toboolean(L, -2)) {
395 close(handle->fd);
396 handle->fd = -1;
397 return 2;
398 }
399 }
400 moonbr_io_handle_set_linger(L, handle, -1);
401 }
402 if (handle->fd >= 0) {
403 if (close(handle->fd)) {
404 moonbr_io_errmsg();
405 handle->fd = -1;
406 lua_pushnil(L);
407 lua_pushstring(L, errmsg);
408 return 2;
409 }
410 handle->fd = -1;
411 }
412 lua_pushboolean(L, 1);
413 return 1;
415 }
417 static int moonbr_io_close(lua_State *L) {
418 return moonbr_io_close_impl(L, 0);
419 }
421 static int moonbr_io_reset(lua_State *L) {
422 return moonbr_io_close_impl(L, 1);
423 }
425 static int moonbr_io_gc(lua_State *L) {
426 moonbr_io_handle_t *handle;
427 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
428 if (handle->fd >= 0) {
429 lua_pushcfunction(L, moonbr_io_close);
430 lua_pushvalue(L, 1);
431 lua_pushinteger(L, 0);
432 lua_call(L, 2, 0);
433 }
434 return 0;
435 }
437 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
438 moonbr_io_handle_t *handle;
439 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
440 if (!handle->closed) {
441 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
442 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
443 lua_call(L, 1, 0);
444 }
445 }
447 void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) {
448 moonbr_io_handle_t *handle;
449 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
450 handle->fd = fd;
451 handle->isnetwork = isnetwork;
452 handle->finished = 0;
453 handle->closed = 0;
454 handle->nonblocking = -1;
455 handle->nopush = -1;
456 handle->readerr = 0;
457 handle->readbufcnt = 0;
458 handle->writeerr = 0;
459 handle->writeleft = 0;
460 handle->writeqin = 0;
461 handle->writeqout = 0;
462 handle->writeqoff = 0;
463 handle->writebufin = 0;
464 handle->writebufout = 0;
465 moonbr_io_handle_set_linger(L, handle, 0);
466 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
467 lua_setmetatable(L, -2);
468 lua_newtable(L); // uservalue
469 lua_newtable(L);
470 lua_setfield(L, -2, "writequeue");
471 lua_newtable(L); // public
472 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
473 lua_setmetatable(L, -2);
474 lua_setfield(L, -2, "public");
475 lua_setuservalue(L, -2);
476 }
478 static int moonbr_io_handleindex(lua_State *L) {
479 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
480 lua_getuservalue(L, 1);
481 lua_getfield(L, -1, "public");
482 lua_pushvalue(L, 2);
483 lua_gettable(L, -2);
484 return 1;
485 }
487 static int moonbr_io_handlenewindex(lua_State *L) {
488 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
489 lua_getuservalue(L, 1);
490 lua_getfield(L, -1, "public");
491 lua_pushvalue(L, 2);
492 lua_pushvalue(L, 3);
493 lua_settable(L, -3);
494 return 0;
495 }
497 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
498 const char *host, *port;
499 struct addrinfo hints = { 0, };
500 struct addrinfo *res, *addrinfo;
501 int errcode;
502 int sock;
503 host = luaL_checkstring(L, 1);
504 port = luaL_checkstring(L, 2);
505 hints.ai_family = AF_UNSPEC;
506 hints.ai_socktype = SOCK_STREAM;
507 hints.ai_protocol = IPPROTO_TCP;
508 hints.ai_flags = AI_ADDRCONFIG;
509 errcode = getaddrinfo(host, port, &hints, &res);
510 if (errcode) {
511 freeaddrinfo(res);
512 if (errcode == EAI_SYSTEM) {
513 moonbr_io_errmsg();
514 lua_pushnil(L);
515 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
516 } else {
517 lua_pushnil(L);
518 lua_pushstring(L, gai_strerror(errcode));
519 }
520 return 2;
521 }
522 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
523 if (addrinfo->ai_family == PF_INET6) goto moonbr_io_tcpconnect_found;
524 }
525 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
526 if (addrinfo->ai_family == PF_INET) goto moonbr_io_tcpconnect_found;
527 }
528 addrinfo = res;
529 moonbr_io_tcpconnect_found:
530 sock = socket(
531 addrinfo->ai_family,
532 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
533 addrinfo->ai_protocol
534 );
535 freeaddrinfo(res);
536 if (sock < 0) {
537 moonbr_io_errmsg();
538 lua_pushnil(L);
539 lua_pushstring(L, errmsg);
540 }
541 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
542 if (!nonblocking && errno == EINTR) {
543 moonbr_io_errmsg();
544 close(sock);
545 lua_pushnil(L);
546 lua_pushstring(L, errmsg);
547 return 2;
548 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
549 moonbr_io_errmsg();
550 lua_pushnil(L);
551 lua_pushstring(L, errmsg);
552 return 2;
553 }
554 }
555 moonbr_io_pushhandle(L, sock, 1);
556 return 1;
557 }
559 static int moonbr_io_tcpconnect(lua_State *L) {
560 return moonbr_io_tcpconnect_impl(L, 0);
561 }
563 static int moonbr_io_tcpconnect_nb(lua_State *L) {
564 return moonbr_io_tcpconnect_impl(L, 1);
565 }
567 static const struct luaL_Reg moonbr_io_handle_methods[] = {
568 {"read", moonbr_io_read},
569 {"read_nb", moonbr_io_read_nb},
570 {"drain", moonbr_io_drain},
571 {"drain_nb", moonbr_io_drain_nb},
572 {"write", moonbr_io_write},
573 {"write_nb", moonbr_io_write_nb},
574 {"flush", moonbr_io_flush},
575 {"flush_nb", moonbr_io_flush_nb},
576 {"finish", moonbr_io_finish},
577 {"close", moonbr_io_close},
578 {"reset", moonbr_io_reset},
579 {NULL, NULL}
580 };
582 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
583 {"__index", moonbr_io_handleindex},
584 {"__newindex", moonbr_io_handlenewindex},
585 {"__gc", moonbr_io_gc},
586 {NULL, NULL}
587 };
589 static const struct luaL_Reg moonbr_io_module_funcs[] = {
590 {"tcpconnect", moonbr_io_tcpconnect},
591 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
592 {NULL, NULL}
593 };
595 int luaopen_moonbridge_io(lua_State *L) {
597 lua_newtable(L); // module
599 lua_newtable(L); // public metatable
600 lua_newtable(L); // handle methods
601 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
602 lua_pushvalue(L, -1);
603 lua_setfield(L, -4, "handle");
604 lua_setfield(L, -2, "__index");
605 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
607 lua_newtable(L); // handle metatable
608 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
609 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
611 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
612 return 1;
614 }

Impressum / About Us