moonbridge

annotate moonbridge_io.c @ 251:f3988723732a

Call tls_close always before closing file descriptor
author jbe
date Sat Sep 03 11:41:26 2016 +0200 (2016-09-03)
parents 28bffa2af1ec
children d39f818aff02
rev   line source
jbe@209 1
jbe@209 2 #ifndef __has_include
jbe@209 3 #define __has_include(x) 0
jbe@209 4 #endif
jbe@79 5
jbe@79 6 #include <stdlib.h>
jbe@135 7 #include <stdint.h>
jbe@135 8 #include <string.h>
jbe@135 9 #include <errno.h>
jbe@79 10 #include <unistd.h>
jbe@135 11 #include <signal.h>
jbe@135 12 #include <fcntl.h>
jbe@135 13 #include <sys/stat.h>
jbe@79 14 #include <sys/socket.h>
jbe@111 15 #include <sys/un.h>
jbe@95 16 #include <netinet/in.h>
jbe@95 17 #include <netinet/tcp.h>
jbe@135 18 #include <sys/select.h>
jbe@136 19 #include <time.h>
jbe@135 20 #include <netdb.h>
jbe@107 21 #include <arpa/inet.h>
jbe@205 22 #include <sys/types.h>
jbe@205 23 #include <sys/wait.h>
jbe@209 24 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
jbe@209 25 #include <bsd/unistd.h>
jbe@209 26 #endif
jbe@211 27
jbe@243 28 #ifdef MOONBR_IO_USE_TLS
jbe@243 29 #include <tls.h>
jbe@243 30 #endif
jbe@243 31
jbe@79 32 #include <lua.h>
jbe@79 33 #include <lauxlib.h>
jbe@79 34 #include <lualib.h>
jbe@79 35
jbe@149 36 #include <assert.h>
jbe@149 37
jbe@80 38 #define MOONBR_IO_MAXSTRERRORLEN 80
jbe@85 39 #define MOONBR_IO_READBUFLEN 4096
jbe@80 40 #define MOONBR_IO_WRITEBUFLEN 4096
jbe@80 41
jbe@108 42 #define MOONBR_IO_LISTEN_BACKLOG 1024
jbe@108 43
jbe@213 44 #define MOONBR_IO_STRERROR_R_MSG "Error detail unavailable due to noncompliant strerror_r() implementation"
jbe@80 45 #define moonbr_io_errmsg() \
jbe@213 46 char errmsg[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG; \
jbe@80 47 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
jbe@80 48
jbe@79 49 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
jbe@79 50 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
jbe@108 51 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
jbe@205 52 #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child"
jbe@205 53 #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt"
jbe@79 54
jbe@243 55 #ifdef MOONBR_IO_USE_TLS
jbe@243 56 #define MOONBR_IO_TLSCONF_MT_REGKEY "moonbridge_io_tlsconf"
jbe@243 57 #endif
jbe@243 58
jbe@79 59 typedef struct {
jbe@79 60 int fd;
jbe@107 61 int issock;
jbe@107 62 sa_family_t addrfam;
jbe@94 63 int finished;
jbe@94 64 int closed;
jbe@81 65 int nonblocking;
jbe@95 66 int nopush;
jbe@85 67 int readerr;
jbe@105 68 int readbufin;
jbe@105 69 int readbufout;
jbe@81 70 int writeerr;
jbe@83 71 size_t writeleft;
jbe@132 72 size_t flushedleft;
jbe@83 73 #if LUA_VERSION_NUM >= 503
jbe@83 74 lua_Integer writeqin;
jbe@83 75 lua_Integer writeqout;
jbe@83 76 #else
jbe@83 77 int writeqin;
jbe@83 78 int writeqout;
jbe@83 79 #endif
jbe@83 80 size_t writeqoff;
jbe@103 81 int writebufin;
jbe@103 82 int writebufout;
jbe@85 83 char readbuf[MOONBR_IO_READBUFLEN];
jbe@80 84 char writebuf[MOONBR_IO_WRITEBUFLEN];
jbe@246 85 #ifdef MOONBR_IO_USE_TLS
jbe@250 86 struct tls *tls;
jbe@250 87 struct tls *servertls;
jbe@250 88 int tlshandshake;
jbe@246 89 #endif
jbe@79 90 } moonbr_io_handle_t;
jbe@79 91
jbe@108 92 typedef struct {
jbe@108 93 int fd;
jbe@118 94 sa_family_t addrfam;
jbe@108 95 int nonblocking;
jbe@108 96 } moonbr_io_listener_t;
jbe@108 97
jbe@205 98 typedef struct {
jbe@205 99 pid_t pid;
jbe@205 100 } moonbr_io_child_t;
jbe@205 101
jbe@148 102 static int moonbr_io_yield(lua_State *L) {
jbe@149 103 return lua_yield(L, lua_gettop(L));
jbe@140 104 }
jbe@140 105
jbe@140 106 #if LUA_VERSION_NUM >= 503
jbe@140 107 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
jbe@140 108 #else
jbe@140 109 static int moonbr_io_cont_returnall(lua_State *L) {
jbe@140 110 #endif
jbe@140 111 return lua_gettop(L);
jbe@140 112 }
jbe@140 113
jbe@145 114 #define moonbr_io_yield_wrapper(yieldfunc, callfunc) \
jbe@145 115 static int yieldfunc(lua_State *L) { \
jbe@145 116 int args; \
jbe@145 117 lua_pushcfunction(L, callfunc); \
jbe@145 118 lua_insert(L, 1); \
jbe@145 119 args = lua_gettop(L); \
jbe@148 120 lua_pushcfunction(L, moonbr_io_yield); \
jbe@145 121 lua_insert(L, 3); \
jbe@145 122 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall); \
jbe@145 123 return lua_gettop(L); \
jbe@145 124 }
jbe@145 125
jbe@81 126 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
jbe@95 127 int flags;
jbe@95 128 if (handle->nonblocking == nonblocking) return;
jbe@95 129 flags = fcntl(handle->fd, F_GETFL, 0);
jbe@95 130 if (flags == -1) {
jbe@95 131 moonbr_io_errmsg();
jbe@251 132 #ifdef MOONBR_IO_USE_TLS
jbe@251 133 if (handle->tls) tls_close(handle->tls);
jbe@251 134 #endif
jbe@96 135 close(handle->fd);
jbe@96 136 handle->fd = -1;
jbe@96 137 handle->closed = 1;
jbe@95 138 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@81 139 }
jbe@95 140 if (nonblocking) flags |= O_NONBLOCK;
jbe@95 141 else flags &= ~O_NONBLOCK;
jbe@95 142 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
jbe@95 143 moonbr_io_errmsg();
jbe@251 144 #ifdef MOONBR_IO_USE_TLS
jbe@251 145 if (handle->tls) tls_close(handle->tls);
jbe@251 146 #endif
jbe@251 147 close(handle->fd);
jbe@96 148 handle->fd = -1;
jbe@96 149 handle->closed = 1;
jbe@95 150 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@95 151 }
jbe@95 152 handle->nonblocking = nonblocking;
jbe@81 153 }
jbe@81 154
jbe@87 155 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
jbe@87 156 struct linger lingerval = { 0, };
jbe@107 157 if (!handle->issock) return;
jbe@87 158 if (timeout >= 0) {
jbe@87 159 lingerval.l_onoff = 1;
jbe@87 160 lingerval.l_linger = timeout;
jbe@87 161 }
jbe@87 162 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
jbe@87 163 moonbr_io_errmsg();
jbe@251 164 #ifdef MOONBR_IO_USE_TLS
jbe@251 165 if (handle->tls) tls_close(handle->tls);
jbe@251 166 #endif
jbe@96 167 close(handle->fd);
jbe@96 168 handle->fd = -1;
jbe@96 169 handle->closed = 1;
jbe@95 170 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
jbe@87 171 }
jbe@87 172 }
jbe@87 173
jbe@133 174 static inline void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
jbe@96 175 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
jbe@107 176 if (
jbe@107 177 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
jbe@107 178 handle->nopush == nopush
jbe@107 179 ) return;
jbe@96 180 #if defined(TCP_NOPUSH)
jbe@96 181 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
jbe@104 182 #elif defined(TCP_CORK)
jbe@104 183 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
jbe@104 184 #endif
jbe@96 185 moonbr_io_errmsg();
jbe@251 186 #ifdef MOONBR_IO_USE_TLS
jbe@251 187 if (handle->tls) tls_close(handle->tls);
jbe@251 188 #endif
jbe@96 189 close(handle->fd);
jbe@96 190 handle->fd = -1;
jbe@96 191 handle->closed = 1;
jbe@104 192 #if defined(TCP_NOPUSH)
jbe@96 193 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
jbe@96 194 #elif defined(TCP_CORK)
jbe@95 195 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
jbe@104 196 #endif
jbe@95 197 }
jbe@95 198 handle->nopush = nopush;
jbe@96 199 #else
jbe@96 200 #warning Neither TCP_NOPUSH nor TCP_CORK is available
jbe@96 201 #endif
jbe@95 202 }
jbe@95 203
jbe@86 204 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
jbe@85 205 moonbr_io_handle_t *handle;
jbe@85 206 lua_Integer maxread;
jbe@85 207 const char *terminatorstr;
jbe@210 208 size_t terminatorlen;
jbe@210 209 char terminator = 0; /* initialize to avoid compiler warning */
jbe@85 210 luaL_Buffer luabuf;
jbe@85 211 size_t luabufcnt = 0;
jbe@105 212 int remaining;
jbe@85 213 char *terminatorpos;
jbe@103 214 ssize_t bytesread;
jbe@85 215 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@153 216 maxread = luaL_optinteger(L, 2, -1);
jbe@85 217 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
jbe@85 218 if (terminatorlen) {
jbe@85 219 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
jbe@85 220 terminator = terminatorstr[0];
jbe@85 221 }
jbe@86 222 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
jbe@94 223 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
jbe@85 224 if (handle->readerr) {
jbe@85 225 lua_pushnil(L);
jbe@85 226 lua_pushliteral(L, "Previous read error");
jbe@85 227 return 2;
jbe@85 228 }
jbe@143 229 if (handle->fd < 0) {
jbe@143 230 /* fake EOF to simulate shutdown */
jbe@143 231 if (!drain) lua_pushliteral(L, "");
jbe@143 232 else lua_pushinteger(L, 0);
jbe@143 233 lua_pushliteral(L, "eof");
jbe@143 234 return 2;
jbe@143 235 }
jbe@105 236 handle->readerr = 1;
jbe@85 237 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@86 238 if (!drain) luaL_buffinit(L, &luabuf);
jbe@85 239 while (1) {
jbe@105 240 remaining = -1;
jbe@142 241 terminatorpos = NULL;
jbe@105 242 if (
jbe@153 243 maxread >= 0 &&
jbe@143 244 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
jbe@105 245 ) {
jbe@142 246 remaining = (size_t)maxread - luabufcnt;
jbe@193 247 if (terminatorlen) {
jbe@193 248 terminatorpos = memchr(
jbe@193 249 handle->readbuf + handle->readbufout,
jbe@193 250 terminator,
jbe@193 251 remaining
jbe@193 252 );
jbe@193 253 }
jbe@85 254 } else if (terminatorlen) {
jbe@105 255 terminatorpos = memchr(
jbe@105 256 handle->readbuf + handle->readbufout,
jbe@105 257 terminator,
jbe@105 258 handle->readbufin - handle->readbufout
jbe@105 259 );
jbe@85 260 }
jbe@142 261 if (terminatorpos) remaining = 1 + (
jbe@142 262 terminatorpos - (handle->readbuf + handle->readbufout)
jbe@142 263 );
jbe@105 264 if (remaining >= 0) {
jbe@86 265 if (!drain) {
jbe@105 266 luaL_addlstring(
jbe@105 267 &luabuf,
jbe@105 268 handle->readbuf + handle->readbufout,
jbe@105 269 remaining
jbe@105 270 );
jbe@86 271 luaL_pushresult(&luabuf);
jbe@90 272 } else {
jbe@105 273 lua_pushinteger(L, luabufcnt + remaining);
jbe@86 274 }
jbe@143 275 if (terminatorpos) lua_pushliteral(L, "term");
jbe@143 276 else lua_pushliteral(L, "maxlen");
jbe@105 277 handle->readbufout += remaining;
jbe@105 278 if (handle->readbufout == handle->readbufin) {
jbe@105 279 handle->readbufin = 0;
jbe@143 280 handle->readbufout = 0;
jbe@105 281 }
jbe@104 282 handle->readerr = 0;
jbe@143 283 return 2;
jbe@85 284 }
jbe@105 285 if (!drain) luaL_addlstring(
jbe@105 286 &luabuf,
jbe@105 287 handle->readbuf + handle->readbufout,
jbe@105 288 handle->readbufin - handle->readbufout
jbe@105 289 );
jbe@105 290 luabufcnt += handle->readbufin - handle->readbufout;
jbe@143 291 handle->readbufout = 0;
jbe@249 292 #ifdef MOONBR_IO_USE_TLS
jbe@249 293 if (handle->tls) {
jbe@250 294 do {
jbe@250 295 if (!handle->tlshandshake) {
jbe@250 296 do bytesread = tls_handshake(handle->tls);
jbe@250 297 while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT));
jbe@250 298 if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) {
jbe@250 299 handle->tlshandshake = bytesread;
jbe@250 300 errno = EAGAIN;
jbe@250 301 break;
jbe@250 302 }
jbe@250 303 if (bytesread < 0) {
jbe@250 304 lua_pushnil(L);
jbe@250 305 lua_pushstring(L, tls_error(handle->tls));
jbe@250 306 return 2;
jbe@250 307 }
jbe@250 308 handle->tlshandshake = 1;
jbe@250 309 }
jbe@250 310 do bytesread = tls_read(handle->tls, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@250 311 while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT));
jbe@250 312 if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) {
jbe@250 313 errno = EAGAIN;
jbe@250 314 break;
jbe@250 315 }
jbe@250 316 if (bytesread < 0) {
jbe@250 317 lua_pushnil(L);
jbe@250 318 lua_pushstring(L, tls_error(handle->tls));
jbe@250 319 return 2;
jbe@250 320 }
jbe@250 321 } while (0);
jbe@249 322 }
jbe@249 323 else
jbe@249 324 #endif
jbe@249 325 do bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@249 326 while (bytesread < 0 && (errno == EINTR));
jbe@105 327 if (
jbe@105 328 bytesread == 0 || (
jbe@105 329 nonblocking &&
jbe@105 330 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
jbe@105 331 )
jbe@105 332 ) {
jbe@105 333 handle->readbufin = 0;
jbe@143 334 if (!drain) luaL_pushresult(&luabuf);
jbe@143 335 else lua_pushinteger(L, luabufcnt);
jbe@143 336 if (bytesread == 0) lua_pushliteral(L, "eof");
jbe@143 337 else lua_pushliteral(L, "block");
jbe@105 338 handle->readerr = 0;
jbe@143 339 return 2;
jbe@105 340 }
jbe@103 341 if (bytesread < 0) {
jbe@85 342 moonbr_io_errmsg();
jbe@85 343 lua_pushnil(L);
jbe@85 344 lua_pushstring(L, errmsg);
jbe@85 345 return 2;
jbe@85 346 }
jbe@105 347 handle->readbufin = bytesread;
jbe@86 348 }
jbe@85 349 }
jbe@85 350
jbe@85 351 static int moonbr_io_read(lua_State *L) {
jbe@86 352 return moonbr_io_read_impl(L, 0, 0);
jbe@85 353 }
jbe@85 354
jbe@85 355 static int moonbr_io_read_nb(lua_State *L) {
jbe@86 356 return moonbr_io_read_impl(L, 1, 0);
jbe@86 357 }
jbe@86 358
jbe@86 359 static int moonbr_io_drain(lua_State *L) {
jbe@86 360 return moonbr_io_read_impl(L, 0, 1);
jbe@86 361 }
jbe@86 362
jbe@86 363 static int moonbr_io_drain_nb(lua_State *L) {
jbe@86 364 return moonbr_io_read_impl(L, 1, 1);
jbe@85 365 }
jbe@85 366
jbe@140 367 #if LUA_VERSION_NUM >= 503
jbe@140 368 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@140 369 #else
jbe@140 370 static int moonbr_io_read_cont(lua_State *L) {
jbe@140 371 #endif
jbe@143 372 lua_Integer remaining;
jbe@143 373 size_t len;
jbe@140 374 #if !(LUA_VERSION_NUM >= 503)
jbe@140 375 int ctx = 0;
jbe@140 376 lua_getctx(L, &ctx);
jbe@140 377 #endif
jbe@140 378 remaining = lua_tointeger(L, 3);
jbe@140 379 while (1) {
jbe@140 380 lua_pushcfunction(L, moonbr_io_read_nb);
jbe@140 381 lua_pushvalue(L, 1);
jbe@140 382 lua_pushvalue(L, 3);
jbe@140 383 lua_pushvalue(L, 4);
jbe@140 384 lua_call(L, 3, 2);
jbe@143 385 if (lua_isnil(L, -2)) return 2;
jbe@143 386 lua_insert(L, -2);
jbe@143 387 len = lua_rawlen(L, -1);
jbe@143 388 if (ctx == 0) {
jbe@143 389 lua_replace(L, 5);
jbe@143 390 ctx = 1;
jbe@143 391 } else if (ctx == 1) {
jbe@143 392 lua_pushvalue(L, 5);
jbe@143 393 lua_newtable(L);
jbe@143 394 lua_replace(L, 5);
jbe@143 395 lua_rawseti(L, 5, 2);
jbe@143 396 lua_rawseti(L, 5, 1);
jbe@143 397 ctx = 2;
jbe@140 398 } else {
jbe@143 399 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
jbe@140 400 }
jbe@144 401 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
jbe@144 402 lua_pop(L, 1);
jbe@144 403 if (remaining >= 0 && len) {
jbe@143 404 remaining -= len;
jbe@143 405 lua_pushinteger(L, remaining);
jbe@143 406 lua_replace(L, 3);
jbe@143 407 }
jbe@140 408 lua_pushvalue(L, 2);
jbe@152 409 lua_callk(L, 0, 0, ctx, moonbr_io_read_cont);
jbe@140 410 }
jbe@140 411 if (ctx == 1) {
jbe@140 412 lua_pushvalue(L, 5);
jbe@140 413 } else {
jbe@140 414 luaL_Buffer buf;
jbe@140 415 lua_Integer i, chunkcount;
jbe@140 416 chunkcount = lua_rawlen(L, 5);
jbe@140 417 luaL_buffinit(L, &buf);
jbe@140 418 for (i=1; i<=chunkcount && i>0; i++) {
jbe@140 419 lua_rawgeti(L, 5, i);
jbe@140 420 luaL_addvalue(&buf);
jbe@140 421 }
jbe@140 422 luaL_pushresult(&buf);
jbe@140 423 }
jbe@143 424 lua_pushvalue(L, -2);
jbe@143 425 return 2;
jbe@140 426 }
jbe@140 427
jbe@140 428 static int moonbr_io_read_call(lua_State *L) {
jbe@140 429 lua_settop(L, 4);
jbe@140 430 lua_pushnil(L);
jbe@140 431 #if LUA_VERSION_NUM >= 503
jbe@140 432 return moonbr_io_read_cont(L, 0, 0);
jbe@140 433 #else
jbe@140 434 return moonbr_io_read_cont(L);
jbe@140 435 #endif
jbe@140 436 }
jbe@140 437
jbe@145 438 moonbr_io_yield_wrapper(moonbr_io_read_yield, moonbr_io_read_call);
jbe@140 439
jbe@144 440 #if LUA_VERSION_NUM >= 503
jbe@144 441 static int moonbr_io_drain_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@144 442 #else
jbe@144 443 static int moonbr_io_drain_cont(lua_State *L) {
jbe@144 444 #endif
jbe@144 445 lua_Integer remaining, len;
jbe@144 446 size_t totallen = 0;
jbe@144 447 #if !(LUA_VERSION_NUM >= 503)
jbe@144 448 int ctx = 0;
jbe@144 449 lua_getctx(L, &ctx);
jbe@144 450 #endif
jbe@144 451 remaining = lua_tointeger(L, 3);
jbe@144 452 while (1) {
jbe@144 453 lua_pushcfunction(L, moonbr_io_drain_nb);
jbe@144 454 lua_pushvalue(L, 1);
jbe@144 455 lua_pushvalue(L, 3);
jbe@144 456 lua_pushvalue(L, 4);
jbe@144 457 lua_call(L, 3, 2);
jbe@144 458 if (lua_isnil(L, -2)) return 2;
jbe@144 459 lua_insert(L, -2);
jbe@144 460 len = lua_tointeger(L, -1);
jbe@144 461 lua_pop(L, 1);
jbe@144 462 totallen += len;
jbe@144 463 if (strcmp(lua_tostring(L, -1), "block") != 0) break;
jbe@144 464 lua_pop(L, 1);
jbe@144 465 if (remaining >= 0 && len) {
jbe@144 466 remaining -= len;
jbe@144 467 lua_pushinteger(L, remaining);
jbe@144 468 lua_replace(L, 3);
jbe@144 469 }
jbe@144 470 lua_pushvalue(L, 2);
jbe@152 471 lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont);
jbe@144 472 }
jbe@144 473 lua_pushinteger(L, totallen);
jbe@144 474 lua_pushvalue(L, -2);
jbe@144 475 return 2;
jbe@144 476 }
jbe@144 477
jbe@144 478 static int moonbr_io_drain_call(lua_State *L) {
jbe@144 479 #if LUA_VERSION_NUM >= 503
jbe@144 480 return moonbr_io_drain_cont(L, 0, 0);
jbe@144 481 #else
jbe@144 482 return moonbr_io_drain_cont(L);
jbe@144 483 #endif
jbe@144 484 }
jbe@144 485
jbe@145 486 moonbr_io_yield_wrapper(moonbr_io_drain_yield, moonbr_io_drain_call);
jbe@144 487
jbe@250 488 #ifdef MOONBR_IO_USE_TLS
jbe@250 489 #define moonbr_io_write_tls(buf, buflen) \
jbe@250 490 if (handle->tls) { \
jbe@250 491 do { \
jbe@250 492 if (!handle->tlshandshake) { \
jbe@250 493 do written = tls_handshake(handle->tls); \
jbe@250 494 while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); \
jbe@250 495 if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { \
jbe@250 496 handle->tlshandshake = written; \
jbe@250 497 errno = EAGAIN; \
jbe@250 498 break; \
jbe@250 499 } \
jbe@250 500 if (written < 0) { \
jbe@250 501 lua_pushnil(L); \
jbe@250 502 lua_pushstring(L, tls_error(handle->tls)); \
jbe@250 503 return 2; \
jbe@250 504 } \
jbe@250 505 handle->tlshandshake = 1; \
jbe@250 506 } \
jbe@250 507 do written = tls_write(handle->tls, (buf), (buflen)); \
jbe@250 508 while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); \
jbe@250 509 if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { \
jbe@250 510 errno = EAGAIN; \
jbe@250 511 break; \
jbe@250 512 } \
jbe@250 513 if (written < 0) { \
jbe@250 514 lua_pushnil(L); \
jbe@250 515 lua_pushstring(L, tls_error(handle->tls)); \
jbe@250 516 return 2; \
jbe@250 517 } \
jbe@250 518 } while (0); \
jbe@250 519 } \
jbe@250 520 else
jbe@250 521 #endif
jbe@250 522
jbe@81 523 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
jbe@80 524 moonbr_io_handle_t *handle;
jbe@80 525 int i, top;
jbe@80 526 const char *str;
jbe@92 527 size_t strlen;
jbe@103 528 ssize_t written;
jbe@80 529 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 530 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
jbe@94 531 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
jbe@81 532 if (handle->writeerr) {
jbe@80 533 lua_pushnil(L);
jbe@80 534 lua_pushliteral(L, "Previous write error");
jbe@80 535 return 2;
jbe@80 536 }
jbe@103 537 handle->writeerr = 1;
jbe@81 538 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@84 539 top = lua_gettop(L);
jbe@81 540 lua_getuservalue(L, 1);
jbe@103 541 lua_getfield(L, -1, "writequeue");
jbe@84 542 for (i=2; i<=top; i++) {
jbe@84 543 luaL_checklstring(L, i, &strlen);
jbe@84 544 lua_pushvalue(L, i);
jbe@84 545 lua_rawseti(L, -2, handle->writeqin++);
jbe@84 546 handle->writeleft += strlen;
jbe@81 547 }
jbe@132 548 if (flush) handle->flushedleft = handle->writeleft;
jbe@83 549 while (handle->writeqout != handle->writeqin) {
jbe@83 550 lua_rawgeti(L, -1, handle->writeqout);
jbe@81 551 str = lua_tolstring(L, -1, &strlen);
jbe@92 552 while (handle->writeqoff < strlen) {
jbe@103 553 if (
jbe@132 554 strlen - handle->writeqoff <
jbe@103 555 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 556 ) {
jbe@103 557 memcpy(
jbe@103 558 handle->writebuf + handle->writebufin,
jbe@103 559 str + handle->writeqoff,
jbe@103 560 strlen - handle->writeqoff
jbe@103 561 );
jbe@103 562 handle->writebufin += strlen - handle->writeqoff;
jbe@80 563 break;
jbe@80 564 } else {
jbe@103 565 memcpy(
jbe@103 566 handle->writebuf + handle->writebufin,
jbe@103 567 str + handle->writeqoff,
jbe@103 568 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 569 );
jbe@103 570 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
jbe@228 571 handle->writebufin = MOONBR_IO_WRITEBUFLEN;
jbe@103 572 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
jbe@133 573 moonbr_io_handle_set_nopush(L, handle, 1);
jbe@249 574 #ifdef MOONBR_IO_USE_TLS
jbe@250 575 moonbr_io_write_tls(
jbe@250 576 handle->writebuf + handle->writebufout,
jbe@250 577 MOONBR_IO_WRITEBUFLEN - handle->writebufout
jbe@250 578 )
jbe@249 579 #endif
jbe@103 580 written = write(
jbe@103 581 handle->fd,
jbe@103 582 handle->writebuf + handle->writebufout,
jbe@103 583 MOONBR_IO_WRITEBUFLEN - handle->writebufout
jbe@103 584 );
jbe@103 585 if (written < 0) {
jbe@81 586 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 587 goto moonbr_io_write_impl_block;
jbe@81 588 } else if (errno != EINTR) {
jbe@80 589 moonbr_io_errmsg();
jbe@80 590 lua_pushnil(L);
jbe@80 591 lua_pushstring(L, errmsg);
jbe@80 592 return 2;
jbe@80 593 }
jbe@103 594 } else {
jbe@103 595 handle->writebufout += written;
jbe@103 596 handle->writeleft -= written;
jbe@133 597 if (handle->flushedleft) {
jbe@133 598 if (written >= handle->flushedleft) {
jbe@133 599 handle->flushedleft = 0;
jbe@133 600 moonbr_io_handle_set_nopush(L, handle, 0);
jbe@133 601 } else {
jbe@133 602 handle->flushedleft -= written;
jbe@133 603 }
jbe@133 604 }
jbe@80 605 }
jbe@80 606 }
jbe@103 607 handle->writebufin = 0;
jbe@103 608 handle->writebufout = 0;
jbe@80 609 }
jbe@80 610 }
jbe@81 611 handle->writeqoff = 0;
jbe@81 612 lua_pop(L, 1);
jbe@81 613 lua_pushnil(L);
jbe@83 614 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 615 }
jbe@132 616 while (handle->flushedleft) {
jbe@133 617 moonbr_io_handle_set_nopush(L, handle, 1);
jbe@249 618 #ifdef MOONBR_IO_USE_TLS
jbe@250 619 moonbr_io_write_tls(
jbe@250 620 handle->writebuf + handle->writebufout,
jbe@250 621 handle->writebufin - handle->writebufout
jbe@250 622 )
jbe@249 623 #endif
jbe@132 624 written = write(
jbe@132 625 handle->fd,
jbe@132 626 handle->writebuf + handle->writebufout,
jbe@132 627 handle->writebufin - handle->writebufout
jbe@132 628 );
jbe@132 629 if (written < 0) {
jbe@132 630 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@132 631 goto moonbr_io_write_impl_block;
jbe@132 632 } else if (errno != EINTR) {
jbe@132 633 moonbr_io_errmsg();
jbe@132 634 lua_pushnil(L);
jbe@132 635 lua_pushstring(L, errmsg);
jbe@132 636 return 2;
jbe@81 637 }
jbe@132 638 } else {
jbe@132 639 handle->writebufout += written;
jbe@132 640 handle->writeleft -= written;
jbe@133 641 if (handle->flushedleft) {
jbe@133 642 if (written >= handle->flushedleft) {
jbe@133 643 handle->flushedleft = 0;
jbe@133 644 moonbr_io_handle_set_nopush(L, handle, 0);
jbe@133 645 } else {
jbe@133 646 handle->flushedleft -= written;
jbe@133 647 }
jbe@133 648 }
jbe@81 649 }
jbe@132 650 }
jbe@132 651 if (handle->writebufout == handle->writebufin) {
jbe@103 652 handle->writebufin = 0;
jbe@103 653 handle->writebufout = 0;
jbe@81 654 }
jbe@103 655 if (nonblocking) lua_pushinteger(L, 0);
jbe@103 656 else lua_pushvalue(L, 1);
jbe@103 657 handle->writeerr = 0;
jbe@80 658 return 1;
jbe@81 659 moonbr_io_write_impl_block:
jbe@91 660 lua_pushinteger(L, handle->writeleft);
jbe@103 661 handle->writeerr = 0;
jbe@81 662 return 1;
jbe@81 663 }
jbe@81 664
jbe@81 665 static int moonbr_io_write(lua_State *L) {
jbe@81 666 return moonbr_io_write_impl(L, 0, 0);
jbe@81 667 }
jbe@81 668
jbe@81 669 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 670 return moonbr_io_write_impl(L, 1, 0);
jbe@80 671 }
jbe@80 672
jbe@80 673 static int moonbr_io_flush(lua_State *L) {
jbe@81 674 return moonbr_io_write_impl(L, 0, 1);
jbe@81 675 }
jbe@81 676
jbe@81 677 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 678 return moonbr_io_write_impl(L, 1, 1);
jbe@80 679 }
jbe@80 680
jbe@145 681 #if LUA_VERSION_NUM >= 503
jbe@145 682 static int moonbr_io_write_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@145 683 #else
jbe@145 684 static int moonbr_io_write_cont(lua_State *L) {
jbe@145 685 #endif
jbe@145 686 while (1) {
jbe@145 687 lua_pushcfunction(L, moonbr_io_write_nb);
jbe@145 688 lua_pushvalue(L, 1);
jbe@145 689 lua_call(L, 1, 2);
jbe@145 690 if (lua_isnil(L, -2)) return 2;
jbe@145 691 if (!lua_tointeger(L, -2)) {
jbe@145 692 lua_pushvalue(L, 1);
jbe@145 693 return 1;
jbe@145 694 }
jbe@145 695 lua_pop(L, 2);
jbe@145 696 lua_pushvalue(L, 2);
jbe@152 697 lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
jbe@145 698 }
jbe@145 699 }
jbe@145 700
jbe@145 701 static int moonbr_io_write_call(lua_State *L) {
jbe@145 702 lua_pushcfunction(L, moonbr_io_write_nb);
jbe@145 703 lua_insert(L, 3);
jbe@145 704 lua_pushvalue(L, 1);
jbe@145 705 lua_insert(L, 4);
jbe@145 706 lua_call(L, lua_gettop(L) - 3, 2);
jbe@145 707 if (lua_isnil(L, -2)) return 2;
jbe@145 708 if (!lua_tointeger(L, -2)) {
jbe@145 709 lua_pushvalue(L, 1);
jbe@145 710 return 1;
jbe@145 711 }
jbe@145 712 #if LUA_VERSION_NUM >= 503
jbe@145 713 return moonbr_io_write_cont(L, 0, 0);
jbe@145 714 #else
jbe@145 715 return moonbr_io_write_cont(L);
jbe@145 716 #endif
jbe@145 717 }
jbe@145 718
jbe@145 719 moonbr_io_yield_wrapper(moonbr_io_write_yield, moonbr_io_write_call);
jbe@145 720
jbe@145 721 static int moonbr_io_flush_call(lua_State *L) {
jbe@145 722 lua_pushcfunction(L, moonbr_io_flush_nb);
jbe@145 723 lua_insert(L, 3);
jbe@145 724 lua_pushvalue(L, 1);
jbe@145 725 lua_insert(L, 4);
jbe@145 726 lua_call(L, lua_gettop(L) - 3, 2);
jbe@145 727 if (lua_isnil(L, -2)) return 2;
jbe@145 728 if (!lua_tointeger(L, -2)) {
jbe@145 729 lua_pushvalue(L, 1);
jbe@145 730 return 1;
jbe@145 731 }
jbe@145 732 #if LUA_VERSION_NUM >= 503
jbe@145 733 return moonbr_io_write_cont(L, 0, 0);
jbe@145 734 #else
jbe@145 735 return moonbr_io_write_cont(L);
jbe@145 736 #endif
jbe@145 737 }
jbe@145 738
jbe@145 739 moonbr_io_yield_wrapper(moonbr_io_flush_yield, moonbr_io_flush_call);
jbe@145 740
jbe@88 741 static int moonbr_io_finish(lua_State *L) {
jbe@88 742 moonbr_io_handle_t *handle;
jbe@88 743 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 744 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@94 745 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@94 746 if (handle->writeleft) {
jbe@94 747 lua_pushcfunction(L, moonbr_io_flush);
jbe@94 748 lua_pushvalue(L, 1);
jbe@116 749 if (lua_pcall(L, 1, 2, 0)) {
jbe@116 750 handle->finished = 1;
jbe@116 751 lua_error(L);
jbe@116 752 }
jbe@94 753 if (!lua_toboolean(L, -2)) {
jbe@94 754 handle->finished = 1;
jbe@94 755 return 2;
jbe@88 756 }
jbe@94 757 }
jbe@94 758 handle->finished = 1;
jbe@107 759 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
jbe@88 760 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 761 moonbr_io_errmsg();
jbe@88 762 lua_pushnil(L);
jbe@88 763 lua_pushstring(L, errmsg);
jbe@88 764 return 2;
jbe@88 765 }
jbe@94 766 } else {
jbe@251 767 #ifdef MOONBR_IO_USE_TLS
jbe@251 768 if (handle->tls) tls_close(handle->tls);
jbe@251 769 #endif
jbe@94 770 if (close(handle->fd)) {
jbe@94 771 moonbr_io_errmsg();
jbe@94 772 handle->fd = -1;
jbe@94 773 lua_pushnil(L);
jbe@94 774 lua_pushstring(L, errmsg);
jbe@94 775 return 2;
jbe@94 776 }
jbe@94 777 handle->fd = -1; /* fake EOF on read */
jbe@88 778 }
jbe@88 779 lua_pushboolean(L, 1);
jbe@88 780 return 1;
jbe@88 781 }
jbe@88 782
jbe@94 783 static int moonbr_io_close_impl(lua_State *L, int reset) {
jbe@83 784 moonbr_io_handle_t *handle;
jbe@83 785 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 786 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@94 787 if (!reset) {
jbe@87 788 if (handle->writeleft) {
jbe@87 789 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 790 lua_pushvalue(L, 1);
jbe@116 791 if (lua_pcall(L, 1, 2, 0)) {
jbe@129 792 handle->closed = 1;
jbe@251 793 #ifdef MOONBR_IO_USE_TLS
jbe@251 794 if (handle->tls) tls_close(handle->tls);
jbe@251 795 #endif
jbe@116 796 close(handle->fd);
jbe@116 797 handle->fd = -1;
jbe@116 798 lua_error(L);
jbe@116 799 }
jbe@129 800 handle->closed = 1;
jbe@87 801 if (!lua_toboolean(L, -2)) {
jbe@251 802 #ifdef MOONBR_IO_USE_TLS
jbe@251 803 if (handle->tls) tls_close(handle->tls);
jbe@251 804 #endif
jbe@87 805 close(handle->fd);
jbe@87 806 handle->fd = -1;
jbe@87 807 return 2;
jbe@87 808 }
jbe@129 809 } else {
jbe@129 810 handle->closed = 1;
jbe@129 811 moonbr_io_handle_set_linger(L, handle, -1);
jbe@83 812 }
jbe@129 813 } else {
jbe@129 814 handle->closed = 1;
jbe@83 815 }
jbe@94 816 if (handle->fd >= 0) {
jbe@251 817 #ifdef MOONBR_IO_USE_TLS
jbe@251 818 if (handle->tls) tls_close(handle->tls);
jbe@251 819 #endif
jbe@94 820 if (close(handle->fd)) {
jbe@94 821 moonbr_io_errmsg();
jbe@94 822 handle->fd = -1;
jbe@94 823 lua_pushnil(L);
jbe@94 824 lua_pushstring(L, errmsg);
jbe@94 825 return 2;
jbe@94 826 }
jbe@104 827 handle->fd = -1;
jbe@83 828 }
jbe@83 829 lua_pushboolean(L, 1);
jbe@83 830 return 1;
jbe@84 831
jbe@83 832 }
jbe@83 833
jbe@94 834 static int moonbr_io_close(lua_State *L) {
jbe@94 835 return moonbr_io_close_impl(L, 0);
jbe@94 836 }
jbe@94 837
jbe@84 838 static int moonbr_io_reset(lua_State *L) {
jbe@94 839 return moonbr_io_close_impl(L, 1);
jbe@84 840 }
jbe@84 841
jbe@108 842 static int moonbr_io_handlegc(lua_State *L) {
jbe@88 843 moonbr_io_handle_t *handle;
jbe@88 844 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 845 if (handle->fd >= 0) {
jbe@218 846 lua_pushcfunction(L, moonbr_io_reset);
jbe@88 847 lua_pushvalue(L, 1);
jbe@88 848 lua_pushinteger(L, 0);
jbe@88 849 lua_call(L, 2, 0);
jbe@88 850 }
jbe@246 851 #ifdef MOONBR_IO_USE_TLS
jbe@246 852 if (handle->tls) {
jbe@246 853 tls_free(handle->tls);
jbe@246 854 handle->tls = NULL;
jbe@246 855 }
jbe@246 856 if (handle->servertls) {
jbe@246 857 tls_free(handle->servertls);
jbe@246 858 handle->servertls = NULL;
jbe@246 859 }
jbe@246 860 #endif
jbe@88 861 return 0;
jbe@88 862 }
jbe@88 863
jbe@100 864 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
jbe@88 865 moonbr_io_handle_t *handle;
jbe@88 866 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@100 867 if (!handle->closed) {
jbe@100 868 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
jbe@100 869 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
jbe@100 870 lua_call(L, 1, 0);
jbe@88 871 }
jbe@88 872 }
jbe@88 873
jbe@208 874 static int moonbr_io_pushhandle_impl(lua_State *L) {
jbe@208 875 int *fd;
jbe@79 876 moonbr_io_handle_t *handle;
jbe@107 877 struct sockaddr addr;
jbe@107 878 socklen_t addrlen;
jbe@208 879 fd = lua_touserdata(L, 1);
jbe@79 880 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@208 881 handle->fd = -1; /* avoid closing incomplete handle */
jbe@107 882 addrlen = sizeof(addr);
jbe@208 883 if (getsockname(*fd, &addr, &addrlen)) {
jbe@107 884 if (errno != ENOTSOCK) {
jbe@107 885 moonbr_io_errmsg();
jbe@107 886 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
jbe@107 887 }
jbe@107 888 handle->issock = 0;
jbe@107 889 } else {
jbe@107 890 handle->issock = 1;
jbe@107 891 handle->addrfam = addr.sa_family;
jbe@107 892 }
jbe@94 893 handle->finished = 0;
jbe@94 894 handle->closed = 0;
jbe@81 895 handle->nonblocking = -1;
jbe@95 896 handle->nopush = -1;
jbe@85 897 handle->readerr = 0;
jbe@105 898 handle->readbufin = 0;
jbe@105 899 handle->readbufout = 0;
jbe@81 900 handle->writeerr = 0;
jbe@81 901 handle->writeleft = 0;
jbe@132 902 handle->flushedleft = 0;
jbe@83 903 handle->writeqin = 0;
jbe@83 904 handle->writeqout = 0;
jbe@81 905 handle->writeqoff = 0;
jbe@103 906 handle->writebufin = 0;
jbe@103 907 handle->writebufout = 0;
jbe@246 908 #ifdef MOONBR_IO_USE_TLS
jbe@246 909 handle->tls = NULL;
jbe@246 910 handle->servertls = NULL;
jbe@250 911 handle->tlshandshake = 0;
jbe@246 912 #endif
jbe@208 913 handle->fd = *fd; /* required for set_linger call */
jbe@87 914 moonbr_io_handle_set_linger(L, handle, 0);
jbe@208 915 handle->fd = -1; /* avoid closing incomplete handle */
jbe@238 916 luaL_setmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 917 lua_newtable(L); // uservalue
jbe@81 918 lua_newtable(L);
jbe@103 919 lua_setfield(L, -2, "writequeue");
jbe@79 920 lua_newtable(L); // public
jbe@107 921 if (handle->addrfam == AF_INET6) {
jbe@107 922 struct sockaddr_in6 addr_in6;
jbe@107 923 char addrstrbuf[INET6_ADDRSTRLEN];
jbe@107 924 const char *addrstr;
jbe@107 925 addrlen = sizeof(addr_in6);
jbe@208 926 if (getsockname(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@107 927 moonbr_io_errmsg();
jbe@107 928 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 929 }
jbe@107 930 if (addrlen > sizeof(addr_in6)) {
jbe@107 931 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 932 }
jbe@107 933 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 934 if (!addrstr) {
jbe@107 935 moonbr_io_errmsg();
jbe@107 936 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 937 } else {
jbe@107 938 lua_pushstring(L, addrstr);
jbe@107 939 lua_setfield(L, -2, "local_ip6");
jbe@107 940 }
jbe@107 941 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 942 lua_setfield(L, -2, "local_tcpport");
jbe@208 943 if (getpeername(*fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@107 944 moonbr_io_errmsg();
jbe@107 945 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 946 }
jbe@107 947 if (addrlen > sizeof(addr_in6)) {
jbe@107 948 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 949 }
jbe@107 950 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 951 if (!addrstr) {
jbe@107 952 moonbr_io_errmsg();
jbe@107 953 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 954 } else {
jbe@107 955 lua_pushstring(L, addrstr);
jbe@107 956 lua_setfield(L, -2, "remote_ip6");
jbe@107 957 }
jbe@107 958 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 959 lua_setfield(L, -2, "remote_tcpport");
jbe@107 960 } else if (handle->addrfam == AF_INET) {
jbe@107 961 struct sockaddr_in addr_in;
jbe@107 962 char addrstrbuf[INET_ADDRSTRLEN];
jbe@107 963 const char *addrstr;
jbe@107 964 addrlen = sizeof(addr_in);
jbe@208 965 if (getsockname(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@107 966 moonbr_io_errmsg();
jbe@107 967 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 968 }
jbe@107 969 if (addrlen > sizeof(addr_in)) {
jbe@107 970 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 971 }
jbe@107 972 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 973 if (!addrstr) {
jbe@107 974 moonbr_io_errmsg();
jbe@107 975 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 976 } else {
jbe@107 977 lua_pushstring(L, addrstr);
jbe@107 978 lua_setfield(L, -2, "local_ip4");
jbe@107 979 }
jbe@107 980 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 981 lua_setfield(L, -2, "local_tcpport");
jbe@208 982 if (getpeername(*fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@107 983 moonbr_io_errmsg();
jbe@107 984 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 985 }
jbe@107 986 if (addrlen > sizeof(addr_in)) {
jbe@107 987 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 988 }
jbe@107 989 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 990 if (!addrstr) {
jbe@107 991 moonbr_io_errmsg();
jbe@107 992 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 993 } else {
jbe@107 994 lua_pushstring(L, addrstr);
jbe@107 995 lua_setfield(L, -2, "remote_ip4");
jbe@107 996 }
jbe@107 997 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 998 lua_setfield(L, -2, "remote_tcpport");
jbe@107 999 }
jbe@238 1000 luaL_setmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 1001 lua_setfield(L, -2, "public");
jbe@79 1002 lua_setuservalue(L, -2);
jbe@208 1003 handle->fd = *fd;
jbe@208 1004 *fd = -1; /* closing is now handled by garbage collection */
jbe@208 1005 return 1;
jbe@208 1006 }
jbe@208 1007
jbe@208 1008 void moonbr_io_pushhandle(lua_State *L, int fd) {
jbe@208 1009 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1010 lua_pushlightuserdata(L, &fd);
jbe@208 1011 if (lua_pcall(L, 1, 1, 0)) {
jbe@251 1012 if (fd != -1) close(fd); // TODO: correct to close file descriptor here?
jbe@208 1013 lua_error(L);
jbe@208 1014 }
jbe@79 1015 }
jbe@79 1016
jbe@79 1017 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 1018 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@205 1019 luaL_checkany(L, 2);
jbe@79 1020 lua_getuservalue(L, 1);
jbe@79 1021 lua_getfield(L, -1, "public");
jbe@79 1022 lua_pushvalue(L, 2);
jbe@79 1023 lua_gettable(L, -2);
jbe@79 1024 return 1;
jbe@79 1025 }
jbe@79 1026
jbe@79 1027 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 1028 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@205 1029 luaL_checkany(L, 2);
jbe@205 1030 luaL_checkany(L, 3);
jbe@79 1031 lua_getuservalue(L, 1);
jbe@79 1032 lua_getfield(L, -1, "public");
jbe@79 1033 lua_pushvalue(L, 2);
jbe@79 1034 lua_pushvalue(L, 3);
jbe@79 1035 lua_settable(L, -3);
jbe@79 1036 return 0;
jbe@79 1037 }
jbe@79 1038
jbe@111 1039 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
jbe@111 1040 const char *path;
jbe@111 1041 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@111 1042 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@111 1043 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@111 1044 ) - 1; /* one byte for termination */
jbe@111 1045 int sock;
jbe@111 1046 path = luaL_checkstring(L, 1);
jbe@111 1047 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@111 1048 strcpy(sockaddr.sun_path, path);
jbe@111 1049 sock = socket(
jbe@111 1050 PF_LOCAL,
jbe@111 1051 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@111 1052 0
jbe@111 1053 );
jbe@111 1054 if (sock < 0) {
jbe@111 1055 moonbr_io_errmsg();
jbe@111 1056 lua_pushnil(L);
jbe@111 1057 lua_pushstring(L, errmsg);
jbe@111 1058 return 2;
jbe@111 1059 }
jbe@111 1060 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@111 1061 if (!nonblocking && errno == EINTR) {
jbe@111 1062 moonbr_io_errmsg();
jbe@111 1063 close(sock);
jbe@111 1064 lua_pushnil(L);
jbe@111 1065 lua_pushstring(L, errmsg);
jbe@111 1066 return 2;
jbe@111 1067 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
jbe@111 1068 moonbr_io_errmsg();
jbe@111 1069 lua_pushnil(L);
jbe@111 1070 lua_pushstring(L, errmsg);
jbe@111 1071 return 2;
jbe@111 1072 }
jbe@111 1073 }
jbe@111 1074 moonbr_io_pushhandle(L, sock);
jbe@111 1075 return 1;
jbe@111 1076 }
jbe@111 1077
jbe@111 1078 static int moonbr_io_localconnect(lua_State *L) {
jbe@111 1079 return moonbr_io_localconnect_impl(L, 0);
jbe@111 1080 }
jbe@111 1081
jbe@111 1082 static int moonbr_io_localconnect_nb(lua_State *L) {
jbe@111 1083 return moonbr_io_localconnect_impl(L, 1);
jbe@111 1084 }
jbe@111 1085
jbe@99 1086 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
jbe@98 1087 const char *host, *port;
jbe@98 1088 struct addrinfo hints = { 0, };
jbe@98 1089 struct addrinfo *res, *addrinfo;
jbe@98 1090 int errcode;
jbe@98 1091 int sock;
jbe@98 1092 host = luaL_checkstring(L, 1);
jbe@98 1093 port = luaL_checkstring(L, 2);
jbe@98 1094 hints.ai_family = AF_UNSPEC;
jbe@98 1095 hints.ai_socktype = SOCK_STREAM;
jbe@98 1096 hints.ai_protocol = IPPROTO_TCP;
jbe@98 1097 hints.ai_flags = AI_ADDRCONFIG;
jbe@98 1098 errcode = getaddrinfo(host, port, &hints, &res);
jbe@98 1099 if (errcode) {
jbe@102 1100 freeaddrinfo(res);
jbe@98 1101 if (errcode == EAI_SYSTEM) {
jbe@98 1102 moonbr_io_errmsg();
jbe@98 1103 lua_pushnil(L);
jbe@98 1104 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@98 1105 } else {
jbe@98 1106 lua_pushnil(L);
jbe@98 1107 lua_pushstring(L, gai_strerror(errcode));
jbe@98 1108 }
jbe@98 1109 return 2;
jbe@98 1110 }
jbe@98 1111 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1112 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@98 1113 }
jbe@98 1114 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1115 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@98 1116 }
jbe@98 1117 addrinfo = res;
jbe@98 1118 moonbr_io_tcpconnect_found:
jbe@99 1119 sock = socket(
jbe@134 1120 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@99 1121 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@99 1122 addrinfo->ai_protocol
jbe@99 1123 );
jbe@98 1124 if (sock < 0) {
jbe@98 1125 moonbr_io_errmsg();
jbe@108 1126 freeaddrinfo(res);
jbe@98 1127 lua_pushnil(L);
jbe@98 1128 lua_pushstring(L, errmsg);
jbe@108 1129 return 2;
jbe@98 1130 }
jbe@98 1131 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@108 1132 freeaddrinfo(res);
jbe@99 1133 if (!nonblocking && errno == EINTR) {
jbe@99 1134 moonbr_io_errmsg();
jbe@99 1135 close(sock);
jbe@99 1136 lua_pushnil(L);
jbe@99 1137 lua_pushstring(L, errmsg);
jbe@99 1138 return 2;
jbe@99 1139 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
jbe@99 1140 moonbr_io_errmsg();
jbe@99 1141 lua_pushnil(L);
jbe@99 1142 lua_pushstring(L, errmsg);
jbe@99 1143 return 2;
jbe@99 1144 }
jbe@108 1145 } else {
jbe@108 1146 freeaddrinfo(res);
jbe@98 1147 }
jbe@107 1148 moonbr_io_pushhandle(L, sock);
jbe@98 1149 return 1;
jbe@98 1150 }
jbe@98 1151
jbe@99 1152 static int moonbr_io_tcpconnect(lua_State *L) {
jbe@99 1153 return moonbr_io_tcpconnect_impl(L, 0);
jbe@99 1154 }
jbe@99 1155
jbe@99 1156 static int moonbr_io_tcpconnect_nb(lua_State *L) {
jbe@99 1157 return moonbr_io_tcpconnect_impl(L, 1);
jbe@99 1158 }
jbe@99 1159
jbe@112 1160 static int moonbr_io_locallisten(lua_State *L) {
jbe@112 1161 moonbr_io_listener_t *listener;
jbe@112 1162 const char *path;
jbe@118 1163 struct stat sb;
jbe@112 1164 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@112 1165 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@112 1166 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@112 1167 ) - 1; /* one byte for termination */
jbe@112 1168 int sock;
jbe@112 1169 path = luaL_checkstring(L, 1);
jbe@112 1170 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@112 1171 strcpy(sockaddr.sun_path, path);
jbe@118 1172 if (stat(path, &sb) == 0) {
jbe@118 1173 if (S_ISSOCK(sb.st_mode)) unlink(path);
jbe@118 1174 }
jbe@112 1175 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 1176 listener->fd = -1;
jbe@112 1177 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@112 1178 sock = socket(
jbe@112 1179 PF_LOCAL,
jbe@112 1180 SOCK_STREAM | SOCK_CLOEXEC,
jbe@112 1181 0
jbe@112 1182 );
jbe@112 1183 if (sock < 0) {
jbe@112 1184 moonbr_io_errmsg();
jbe@112 1185 lua_pushnil(L);
jbe@112 1186 lua_pushstring(L, errmsg);
jbe@112 1187 return 2;
jbe@112 1188 }
jbe@112 1189 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@112 1190 moonbr_io_errmsg();
jbe@112 1191 close(sock);
jbe@112 1192 lua_pushnil(L);
jbe@112 1193 lua_pushstring(L, errmsg);
jbe@112 1194 return 2;
jbe@112 1195 }
jbe@112 1196 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@112 1197 moonbr_io_errmsg();
jbe@112 1198 close(sock);
jbe@112 1199 lua_pushnil(L);
jbe@112 1200 lua_pushstring(L, errmsg);
jbe@112 1201 return 2;
jbe@112 1202 }
jbe@112 1203 listener->fd = sock;
jbe@118 1204 listener->addrfam = AF_LOCAL;
jbe@112 1205 listener->nonblocking = -1;
jbe@112 1206 return 1;
jbe@112 1207 }
jbe@112 1208
jbe@108 1209 static int moonbr_io_tcplisten(lua_State *L) {
jbe@108 1210 moonbr_io_listener_t *listener;
jbe@108 1211 const char *host, *port;
jbe@108 1212 struct addrinfo hints = { 0, };
jbe@108 1213 struct addrinfo *res, *addrinfo;
jbe@108 1214 int errcode;
jbe@108 1215 int sock;
jbe@108 1216 host = luaL_optstring(L, 1, NULL);
jbe@108 1217 port = luaL_checkstring(L, 2);
jbe@108 1218 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 1219 listener->fd = -1;
jbe@108 1220 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1221 hints.ai_family = AF_UNSPEC;
jbe@108 1222 hints.ai_socktype = SOCK_STREAM;
jbe@108 1223 hints.ai_protocol = IPPROTO_TCP;
jbe@108 1224 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
jbe@108 1225 errcode = getaddrinfo(host, port, &hints, &res);
jbe@108 1226 if (errcode) {
jbe@108 1227 freeaddrinfo(res);
jbe@108 1228 if (errcode == EAI_SYSTEM) {
jbe@108 1229 moonbr_io_errmsg();
jbe@108 1230 lua_pushnil(L);
jbe@108 1231 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@108 1232 } else {
jbe@108 1233 lua_pushnil(L);
jbe@108 1234 lua_pushstring(L, gai_strerror(errcode));
jbe@108 1235 }
jbe@108 1236 return 2;
jbe@108 1237 }
jbe@108 1238 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1239 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@108 1240 }
jbe@108 1241 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 1242 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@108 1243 }
jbe@108 1244 addrinfo = res;
jbe@108 1245 moonbr_io_tcpconnect_found:
jbe@118 1246 listener->addrfam = addrinfo->ai_family;
jbe@108 1247 sock = socket(
jbe@134 1248 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@108 1249 addrinfo->ai_socktype | SOCK_CLOEXEC,
jbe@108 1250 addrinfo->ai_protocol
jbe@108 1251 );
jbe@108 1252 if (sock < 0) {
jbe@108 1253 moonbr_io_errmsg();
jbe@108 1254 freeaddrinfo(res);
jbe@108 1255 lua_pushnil(L);
jbe@108 1256 lua_pushstring(L, errmsg);
jbe@108 1257 return 2;
jbe@108 1258 }
jbe@120 1259 {
jbe@120 1260 static const int reuseval = 1;
jbe@120 1261 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
jbe@120 1262 moonbr_io_errmsg();
jbe@120 1263 freeaddrinfo(res);
jbe@120 1264 close(sock);
jbe@120 1265 lua_pushnil(L);
jbe@120 1266 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
jbe@120 1267 return 2;
jbe@120 1268 }
jbe@120 1269 }
jbe@108 1270 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@108 1271 moonbr_io_errmsg();
jbe@108 1272 freeaddrinfo(res);
jbe@108 1273 close(sock);
jbe@108 1274 lua_pushnil(L);
jbe@108 1275 lua_pushstring(L, errmsg);
jbe@108 1276 return 2;
jbe@108 1277 }
jbe@108 1278 freeaddrinfo(res);
jbe@108 1279 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@108 1280 moonbr_io_errmsg();
jbe@108 1281 close(sock);
jbe@108 1282 lua_pushnil(L);
jbe@108 1283 lua_pushstring(L, errmsg);
jbe@108 1284 return 2;
jbe@108 1285 }
jbe@108 1286 listener->fd = sock;
jbe@108 1287 listener->nonblocking = -1;
jbe@108 1288 return 1;
jbe@108 1289 }
jbe@108 1290
jbe@108 1291 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
jbe@108 1292 moonbr_io_listener_t *listener;
jbe@108 1293 int fd;
jbe@108 1294 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1295 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
jbe@108 1296 if (listener->nonblocking != nonblocking) {
jbe@108 1297 int flags;
jbe@108 1298 flags = fcntl(listener->fd, F_GETFL, 0);
jbe@108 1299 if (flags == -1) {
jbe@108 1300 moonbr_io_errmsg();
jbe@108 1301 close(listener->fd);
jbe@108 1302 listener->fd = -1;
jbe@108 1303 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1304 }
jbe@108 1305 if (nonblocking) flags |= O_NONBLOCK;
jbe@108 1306 else flags &= ~O_NONBLOCK;
jbe@108 1307 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
jbe@108 1308 moonbr_io_errmsg();
jbe@108 1309 close(listener->fd);
jbe@108 1310 listener->fd = -1;
jbe@108 1311 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1312 }
jbe@108 1313 listener->nonblocking = nonblocking;
jbe@108 1314 }
jbe@108 1315 while (1) {
jbe@214 1316 #if defined(__linux__) && !defined(_GNU_SOURCE)
jbe@215 1317 fd = accept(listener->fd, NULL, NULL);
jbe@214 1318 if (fd != -1) {
jbe@214 1319 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
jbe@214 1320 moonbr_io_errmsg();
jbe@214 1321 close(listener->fd);
jbe@214 1322 listener->fd = -1;
jbe@214 1323 close(fd);
jbe@214 1324 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@214 1325 }
jbe@214 1326 }
jbe@214 1327 #else
jbe@108 1328 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
jbe@214 1329 #endif
jbe@108 1330 if (fd < 0) {
jbe@108 1331 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@108 1332 lua_pushboolean(L, 0);
jbe@108 1333 lua_pushliteral(L, "No incoming connection pending");
jbe@108 1334 return 2;
jbe@108 1335 } else if (errno != EINTR) {
jbe@108 1336 moonbr_io_errmsg();
jbe@108 1337 lua_pushnil(L);
jbe@108 1338 lua_pushstring(L, errmsg);
jbe@108 1339 return 2;
jbe@108 1340 }
jbe@108 1341 } else {
jbe@108 1342 moonbr_io_pushhandle(L, fd);
jbe@108 1343 return 1;
jbe@108 1344 }
jbe@108 1345 }
jbe@108 1346 }
jbe@108 1347
jbe@108 1348 static int moonbr_io_accept(lua_State *L) {
jbe@108 1349 return moonbr_io_accept_impl(L, 0);
jbe@108 1350 }
jbe@108 1351
jbe@108 1352 static int moonbr_io_accept_nb(lua_State *L) {
jbe@108 1353 return moonbr_io_accept_impl(L, 1);
jbe@108 1354 }
jbe@108 1355
jbe@108 1356 static int moonbr_io_unlisten(lua_State *L) {
jbe@108 1357 moonbr_io_listener_t *listener;
jbe@118 1358 struct sockaddr_un addr;
jbe@118 1359 socklen_t addrlen;
jbe@118 1360 struct stat sb;
jbe@108 1361 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1362 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
jbe@118 1363 addrlen = sizeof(addr);
jbe@118 1364 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
jbe@108 1365 if (close(listener->fd)) {
jbe@108 1366 moonbr_io_errmsg();
jbe@108 1367 listener->fd = -1;
jbe@118 1368 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1369 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1370 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1371 }
jbe@118 1372 }
jbe@108 1373 lua_pushnil(L);
jbe@108 1374 lua_pushstring(L, errmsg);
jbe@108 1375 return 2;
jbe@108 1376 }
jbe@108 1377 listener->fd = -1;
jbe@118 1378 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1379 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1380 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1381 }
jbe@118 1382 }
jbe@108 1383 lua_pushboolean(L, 1);
jbe@108 1384 return 1;
jbe@108 1385 }
jbe@108 1386
jbe@108 1387 static int moonbr_io_listenergc(lua_State *L) {
jbe@108 1388 moonbr_io_listener_t *listener;
jbe@108 1389 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@117 1390 if (listener->fd >= 0) close(listener->fd);
jbe@108 1391 listener->fd = -1;
jbe@108 1392 return 0;
jbe@108 1393 }
jbe@108 1394
jbe@205 1395 static int moonbr_io_exec(lua_State *L) {
jbe@205 1396 char **argv;
jbe@205 1397 int i, argc;
jbe@205 1398 int sockin[2], sockout[2], sockerr[2];
jbe@205 1399 volatile int errorcond = 0;
jbe@213 1400 volatile char errmsgbuf[MOONBR_IO_MAXSTRERRORLEN] = MOONBR_IO_STRERROR_R_MSG;
jbe@205 1401 moonbr_io_child_t *child;
jbe@205 1402 argc = lua_gettop(L);
jbe@205 1403 argv = lua_newuserdata(L, (argc + 1) * sizeof(char *));
jbe@205 1404 for (i=0; i<argc; i++) argv[i] = (char *)luaL_checkstring(L, i+1);
jbe@205 1405 argv[argc] = NULL;
jbe@208 1406 child = lua_newuserdata(L, sizeof(moonbr_io_child_t));
jbe@208 1407 child->pid = 0;
jbe@208 1408 lua_newtable(L);
jbe@208 1409 lua_setuservalue(L, -2);
jbe@238 1410 luaL_setmetatable(L, MOONBR_IO_CHILD_MT_REGKEY);
jbe@206 1411 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockin)) {
jbe@205 1412 moonbr_io_errmsg();
jbe@205 1413 lua_pushnil(L);
jbe@205 1414 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1415 return 2;
jbe@205 1416 }
jbe@206 1417 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockout)) {
jbe@205 1418 moonbr_io_errmsg();
jbe@205 1419 close(sockin[0]);
jbe@205 1420 close(sockin[1]);
jbe@205 1421 lua_pushnil(L);
jbe@205 1422 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1423 return 2;
jbe@205 1424 }
jbe@206 1425 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sockerr)) {
jbe@205 1426 moonbr_io_errmsg();
jbe@205 1427 close(sockin[0]);
jbe@205 1428 close(sockin[1]);
jbe@205 1429 close(sockout[0]);
jbe@205 1430 close(sockout[1]);
jbe@205 1431 lua_pushnil(L);
jbe@205 1432 lua_pushfstring(L, "Could not create socket pair: %s", errmsg);
jbe@205 1433 return 2;
jbe@205 1434 }
jbe@205 1435 child->pid = vfork();
jbe@205 1436 if (child->pid == -1) {
jbe@205 1437 moonbr_io_errmsg();
jbe@205 1438 close(sockin[0]);
jbe@205 1439 close(sockin[1]);
jbe@205 1440 close(sockout[0]);
jbe@205 1441 close(sockout[1]);
jbe@205 1442 close(sockerr[0]);
jbe@205 1443 close(sockerr[1]);
jbe@205 1444 lua_pushnil(L);
jbe@205 1445 lua_pushfstring(L, "Could not fork: %s", errmsg);
jbe@205 1446 return 2;
jbe@205 1447 }
jbe@205 1448 if (!child->pid) {
jbe@208 1449 if (dup2(sockin[1], 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1450 if (dup2(sockout[1], 1) == -1) goto moonbr_io_exec_error1;
jbe@208 1451 if (dup2(sockerr[1], 2) == -1) goto moonbr_io_exec_error1;
jbe@232 1452 closefrom(3);
jbe@208 1453 if (fcntl(0, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1454 if (fcntl(1, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@208 1455 if (fcntl(2, F_SETFD, 0) == -1) goto moonbr_io_exec_error1;
jbe@205 1456 if (execvp(argv[0], argv)) {
jbe@205 1457 errorcond = 2;
jbe@205 1458 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
jbe@205 1459 _exit(0);
jbe@205 1460 }
jbe@208 1461 moonbr_io_exec_error1:
jbe@206 1462 errorcond = 1;
jbe@206 1463 strerror_r(errno, (char *)errmsgbuf, MOONBR_IO_MAXSTRERRORLEN);
jbe@206 1464 _exit(0);
jbe@205 1465 }
jbe@205 1466 close(sockin[1]);
jbe@205 1467 close(sockout[1]);
jbe@205 1468 close(sockerr[1]);
jbe@205 1469 if (errorcond) {
jbe@205 1470 int status;
jbe@205 1471 close(sockin[0]);
jbe@205 1472 close(sockout[0]);
jbe@205 1473 close(sockerr[0]);
jbe@205 1474 while (waitpid(child->pid, &status, 0) == -1) {
jbe@205 1475 if (errno != EINTR) {
jbe@205 1476 moonbr_io_errmsg();
jbe@205 1477 luaL_error(L, "Error in waitpid call after unsuccessful exec: %s", errmsg);
jbe@205 1478 }
jbe@205 1479 }
jbe@208 1480 child->pid = 0;
jbe@205 1481 lua_pushnil(L);
jbe@205 1482 if (errorcond == 2) lua_pushfstring(L, "Could not execute: %s", errmsgbuf);
jbe@205 1483 else lua_pushfstring(L, "Error in fork: %s", errmsgbuf);
jbe@205 1484 return 2;
jbe@205 1485 }
jbe@208 1486 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1487 lua_pushlightuserdata(L, &sockin[0]);
jbe@208 1488 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1489 if (sockin[0] != -1) close(sockin[0]);
jbe@208 1490 close(sockout[0]);
jbe@208 1491 close(sockerr[0]);
jbe@208 1492 goto moonbr_io_exec_error2;
jbe@208 1493 }
jbe@205 1494 lua_setfield(L, -2, "stdin");
jbe@208 1495 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1496 lua_pushlightuserdata(L, &sockout[0]);
jbe@208 1497 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1498 if (sockout[0] != -1) close(sockout[0]);
jbe@208 1499 close(sockerr[0]);
jbe@208 1500 goto moonbr_io_exec_error2;
jbe@208 1501 }
jbe@205 1502 lua_setfield(L, -2, "stdout");
jbe@208 1503 lua_pushcfunction(L, moonbr_io_pushhandle_impl);
jbe@208 1504 lua_pushlightuserdata(L, &sockerr[0]);
jbe@208 1505 if (lua_pcall(L, 1, 1, 0)) {
jbe@208 1506 if (sockerr[0] != -1) close(sockerr[0]);
jbe@208 1507 goto moonbr_io_exec_error2;
jbe@208 1508 }
jbe@205 1509 lua_setfield(L, -2, "stderr");
jbe@205 1510 return 1;
jbe@208 1511 moonbr_io_exec_error2:
jbe@208 1512 {
jbe@208 1513 int status;
jbe@208 1514 while (waitpid(child->pid, &status, 0) == -1) {
jbe@208 1515 if (errno != EINTR) {
jbe@208 1516 moonbr_io_errmsg();
jbe@208 1517 luaL_error(L, "Error in waitpid call after error creating socket handles: %s", errmsg);
jbe@208 1518 }
jbe@208 1519 }
jbe@208 1520 }
jbe@208 1521 child->pid = 0;
jbe@208 1522 return lua_error(L);
jbe@205 1523 }
jbe@205 1524
jbe@205 1525 static int moonbr_io_childindex(lua_State *L) {
jbe@205 1526 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1527 luaL_checkany(L, 2);
jbe@205 1528 lua_getuservalue(L, 1);
jbe@205 1529 lua_pushvalue(L, 2);
jbe@205 1530 lua_gettable(L, -2);
jbe@205 1531 if (lua_isnil(L, -1)) {
jbe@205 1532 luaL_getmetatable(L, MOONBR_IO_CHILD_PT_REGKEY);
jbe@205 1533 lua_pushvalue(L, 2);
jbe@205 1534 lua_gettable(L, -2);
jbe@205 1535 }
jbe@205 1536 return 1;
jbe@205 1537 }
jbe@205 1538
jbe@205 1539 static int moonbr_io_childnewindex(lua_State *L) {
jbe@205 1540 luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1541 luaL_checkany(L, 2);
jbe@205 1542 luaL_checkany(L, 3);
jbe@205 1543 lua_getuservalue(L, 1);
jbe@205 1544 lua_pushvalue(L, 2);
jbe@205 1545 lua_pushvalue(L, 3);
jbe@205 1546 lua_settable(L, -3);
jbe@205 1547 return 0;
jbe@205 1548 }
jbe@205 1549
jbe@205 1550 static int moonbr_io_childgc(lua_State *L) {
jbe@205 1551 moonbr_io_child_t *child;
jbe@205 1552 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1553 if (child->pid) {
jbe@205 1554 int status;
jbe@205 1555 if (kill(child->pid, SIGKILL)) {
jbe@205 1556 moonbr_io_errmsg();
jbe@205 1557 luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
jbe@205 1558 }
jbe@205 1559 while (waitpid(child->pid, &status, 0) == -1) {
jbe@205 1560 if (errno != EINTR) {
jbe@205 1561 moonbr_io_errmsg();
jbe@205 1562 luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
jbe@205 1563 }
jbe@205 1564 }
jbe@205 1565 }
jbe@205 1566 return 0;
jbe@205 1567 }
jbe@205 1568
jbe@205 1569 static int moonbr_io_kill(lua_State *L) {
jbe@205 1570 moonbr_io_child_t *child;
jbe@205 1571 int sig;
jbe@205 1572 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1573 sig = luaL_optinteger(L, 2, SIGTERM);
jbe@205 1574 if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
jbe@205 1575 if (kill(child->pid, sig)) {
jbe@205 1576 moonbr_io_errmsg();
jbe@205 1577 luaL_error(L, "Error in kill call: %s", errmsg);
jbe@205 1578 }
jbe@205 1579 lua_settop(L, 1);
jbe@205 1580 return 1;
jbe@205 1581 }
jbe@205 1582
jbe@205 1583 static int moonbr_io_wait_impl(lua_State *L, int nonblocking) {
jbe@205 1584 moonbr_io_child_t *child;
jbe@205 1585 pid_t waitedpid;
jbe@205 1586 int status;
jbe@205 1587 child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 1588 if (!child->pid) luaL_error(L, "Attempt to wait for an already collected child process");
jbe@205 1589 while ((waitedpid = waitpid(child->pid, &status, nonblocking ? WNOHANG : 0)) == -1) {
jbe@205 1590 if (errno != EINTR) {
jbe@205 1591 moonbr_io_errmsg();
jbe@205 1592 luaL_error(L, "Error in waitpid call: %s", errmsg);
jbe@205 1593 }
jbe@205 1594 }
jbe@205 1595 if (!waitedpid) {
jbe@219 1596 lua_pushboolean(L, 0);
jbe@219 1597 lua_pushliteral(L, "Process is still running");
jbe@219 1598 return 2;
jbe@205 1599 } else {
jbe@205 1600 child->pid = 0;
jbe@205 1601 if (WIFEXITED(status)) {
jbe@205 1602 lua_pushinteger(L, WEXITSTATUS(status));
jbe@205 1603 } else if (WIFSIGNALED(status)) {
jbe@205 1604 lua_pushinteger(L, -WTERMSIG(status));
jbe@205 1605 } else {
jbe@205 1606 luaL_error(L, "Unexpected status value returned by waitpid call");
jbe@205 1607 }
jbe@219 1608 return 1;
jbe@205 1609 }
jbe@205 1610 }
jbe@205 1611
jbe@205 1612 static int moonbr_io_wait(lua_State *L) {
jbe@205 1613 return moonbr_io_wait_impl(L, 0);
jbe@205 1614 }
jbe@205 1615
jbe@205 1616 static int moonbr_io_wait_nb(lua_State *L) {
jbe@205 1617 return moonbr_io_wait_impl(L, 1);
jbe@205 1618 }
jbe@205 1619
jbe@205 1620 #if LUA_VERSION_NUM >= 503
jbe@205 1621 static int moonbr_io_wait_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@205 1622 #else
jbe@205 1623 static int moonbr_io_wait_cont(lua_State *L) {
jbe@205 1624 #endif
jbe@205 1625 #if !(LUA_VERSION_NUM >= 503)
jbe@205 1626 int ctx = 0;
jbe@205 1627 lua_getctx(L, &ctx);
jbe@205 1628 #endif
jbe@205 1629 while (1) {
jbe@205 1630 lua_pushcfunction(L, moonbr_io_wait_nb);
jbe@205 1631 lua_pushvalue(L, 1);
jbe@205 1632 lua_call(L, 1, 1);
jbe@205 1633 if (!lua_isnil(L, -1)) break;
jbe@205 1634 lua_pushvalue(L, 2);
jbe@205 1635 lua_callk(L, 0, 0, ctx, moonbr_io_wait_cont);
jbe@205 1636 }
jbe@205 1637 return 1;
jbe@205 1638 }
jbe@205 1639
jbe@205 1640 static int moonbr_io_wait_call(lua_State *L) {
jbe@205 1641 lua_settop(L, 2);
jbe@205 1642 #if LUA_VERSION_NUM >= 503
jbe@205 1643 return moonbr_io_wait_cont(L, 0, 0);
jbe@205 1644 #else
jbe@205 1645 return moonbr_io_wait_cont(L);
jbe@205 1646 #endif
jbe@205 1647 }
jbe@205 1648
jbe@205 1649 moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
jbe@205 1650
jbe@250 1651 #ifdef MOONBR_IO_USE_TLS
jbe@250 1652 #define moonbr_io_poll_tls() \
jbe@250 1653 if (!handle->tlshandshake) { \
jbe@250 1654 lua_pushboolean(L, 1); \
jbe@250 1655 return 1; \
jbe@250 1656 } \
jbe@250 1657 if (handle->tlshandshake == TLS_WANT_POLLIN) { \
jbe@250 1658 if (fd < 0) { \
jbe@250 1659 lua_pushboolean(L, 1); \
jbe@250 1660 return 1; \
jbe@250 1661 } \
jbe@250 1662 FD_SET(fd, &readfds); \
jbe@250 1663 if (fd+1 > nfds) nfds = fd+1; \
jbe@250 1664 continue; \
jbe@250 1665 } \
jbe@250 1666 if (handle->tlshandshake == TLS_WANT_POLLOUT) { \
jbe@250 1667 if (fd < 0) { \
jbe@250 1668 lua_pushboolean(L, 1); \
jbe@250 1669 return 1; \
jbe@250 1670 } \
jbe@250 1671 FD_SET(fd, &writefds); \
jbe@250 1672 if (fd+1 > nfds) nfds = fd+1; \
jbe@250 1673 continue; \
jbe@250 1674 } \
jbe@250 1675 while (0)
jbe@250 1676 #endif
jbe@250 1677
jbe@106 1678 static int moonbr_io_poll(lua_State *L) {
jbe@106 1679 moonbr_io_handle_t *handle;
jbe@108 1680 moonbr_io_listener_t *listener;
jbe@106 1681 int fd, isnum;
jbe@106 1682 int nfds = 0;
jbe@106 1683 fd_set readfds, writefds, exceptfds;
jbe@106 1684 struct timeval timeout = {0, };
jbe@106 1685 int status;
jbe@106 1686 FD_ZERO(&readfds);
jbe@106 1687 FD_ZERO(&writefds);
jbe@106 1688 FD_ZERO(&exceptfds);
jbe@106 1689 if (!lua_isnoneornil(L, 1)) {
jbe@106 1690 luaL_checktype(L, 1, LUA_TTABLE);
jbe@106 1691 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
jbe@106 1692 if (lua_toboolean(L, -1)) {
jbe@106 1693 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1694 if (handle) {
jbe@109 1695 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@106 1696 fd = handle->fd;
jbe@247 1697 #if MOONBR_IO_USE_TLS
jbe@250 1698 moonbr_io_poll_tls();
jbe@247 1699 #endif
jbe@122 1700 if (
jbe@122 1701 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
jbe@122 1702 handle->readbufin != handle->readbufout /* data pending in buffer */
jbe@122 1703 ) {
jbe@109 1704 lua_pushboolean(L, 1);
jbe@109 1705 return 1;
jbe@109 1706 }
jbe@106 1707 } else {
jbe@108 1708 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1709 if (listener) {
jbe@108 1710 fd = listener->fd;
jbe@108 1711 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
jbe@108 1712 } else {
jbe@108 1713 fd = lua_tointegerx(L, -2, &isnum);
jbe@108 1714 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
jbe@108 1715 }
jbe@106 1716 }
jbe@138 1717 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1718 FD_SET(fd, &readfds);
jbe@106 1719 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1720 }
jbe@106 1721 }
jbe@106 1722 }
jbe@106 1723 if (!lua_isnoneornil(L, 2)) {
jbe@106 1724 luaL_checktype(L, 2, LUA_TTABLE);
jbe@106 1725 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
jbe@106 1726 if (lua_toboolean(L, -1)) {
jbe@106 1727 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1728 if (handle) {
jbe@109 1729 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@109 1730 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
jbe@106 1731 fd = handle->fd;
jbe@247 1732 #if MOONBR_IO_USE_TLS
jbe@250 1733 moonbr_io_poll_tls();
jbe@247 1734 #endif
jbe@106 1735 } else {
jbe@108 1736 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@109 1737 if (listener) luaL_error(L, "Attempt to write-poll a listener");
jbe@109 1738 fd = lua_tointegerx(L, -2, &isnum);
jbe@109 1739 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
jbe@106 1740 }
jbe@138 1741 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1742 FD_SET(fd, &writefds);
jbe@106 1743 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1744 }
jbe@106 1745 }
jbe@106 1746 }
jbe@106 1747 if (!lua_isnoneornil(L, 3)) {
jbe@106 1748 lua_Number n;
jbe@106 1749 n = lua_tonumberx(L, 3, &isnum);
jbe@146 1750 if (isnum && n<0) {
jbe@146 1751 lua_pushboolean(L, 0);
jbe@146 1752 lua_pushliteral(L, "Negative timeout");
jbe@146 1753 return 2;
jbe@146 1754 } else if (isnum && n>=0 && n<100000000) {
jbe@106 1755 timeout.tv_sec = n;
jbe@106 1756 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
jbe@106 1757 } else {
jbe@106 1758 luaL_argcheck(L, 0, 3, "not a valid timeout");
jbe@106 1759 }
jbe@106 1760 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
jbe@106 1761 } else {
jbe@106 1762 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
jbe@106 1763 }
jbe@106 1764 if (status == -1) {
jbe@106 1765 if (errno == EINTR) {
jbe@146 1766 lua_pushnil(L);
jbe@106 1767 lua_pushliteral(L, "Signal received while polling file descriptors");
jbe@106 1768 return 2;
jbe@106 1769 } else {
jbe@106 1770 moonbr_io_errmsg();
jbe@106 1771 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
jbe@106 1772 }
jbe@106 1773 } else if (status == 0) {
jbe@106 1774 lua_pushboolean(L, 0);
jbe@106 1775 lua_pushliteral(L, "Timeout while polling file descriptors");
jbe@106 1776 return 2;
jbe@106 1777 } else {
jbe@106 1778 lua_pushboolean(L, 1);
jbe@106 1779 return 1;
jbe@106 1780 }
jbe@106 1781 }
jbe@106 1782
jbe@115 1783 static int moonbr_io_timeref(lua_State *L) {
jbe@115 1784 lua_Number sub;
jbe@115 1785 struct timespec tp;
jbe@115 1786 sub = luaL_optnumber(L, 1, 0);
jbe@115 1787 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
jbe@115 1788 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
jbe@115 1789 }
jbe@115 1790 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
jbe@115 1791 return 1;
jbe@115 1792 }
jbe@115 1793
jbe@243 1794 #ifdef MOONBR_IO_USE_TLS
jbe@243 1795
jbe@243 1796 #define moonbr_io_tlsconf_string(name, field, func) \
jbe@243 1797 /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
jbe@243 1798 lua_getfield(L, 1, (field)); \
jbe@243 1799 valuetype = lua_type(L, -1); \
jbe@243 1800 if (valuetype != LUA_TNIL) { \
jbe@243 1801 luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
jbe@243 1802 value = lua_tostring(L, -1); \
jbe@243 1803 if (func(tlsconf, value)) { \
jbe@243 1804 lua_pushnil(L); \
jbe@243 1805 lua_pushfstring(L, "Could not set " name " \"%s\"", value); \
jbe@243 1806 return 2; \
jbe@243 1807 } \
jbe@243 1808 } \
jbe@243 1809 lua_pop(L, 1);
jbe@243 1810
jbe@243 1811 #define moonbr_io_tlsconf_binary(name, field, func) \
jbe@243 1812 /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
jbe@243 1813 lua_getfield(L, 1, (field)); \
jbe@243 1814 valuetype = lua_type(L, -1); \
jbe@243 1815 if (valuetype != LUA_TNIL) { \
jbe@243 1816 luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
jbe@243 1817 value = lua_tolstring(L, -1, &valuelen); \
jbe@243 1818 if (func(tlsconf, (void *)value, valuelen)) { \
jbe@243 1819 lua_pushnil(L); \
jbe@243 1820 lua_pushliteral(L, "Could not set " name); \
jbe@243 1821 return 2; \
jbe@243 1822 } \
jbe@243 1823 } \
jbe@243 1824 lua_pop(L, 1);
jbe@243 1825
jbe@243 1826 static int moonbr_io_tlsconf(lua_State *L) {
jbe@243 1827 struct tls_config *tlsconf;
jbe@243 1828 int valuetype;
jbe@243 1829 const char *value;
jbe@243 1830 size_t valuelen;
jbe@243 1831 luaL_checktype(L, 1, LUA_TTABLE);
jbe@243 1832 tlsconf = tls_config_new();
jbe@243 1833 if (!tlsconf) {
jbe@243 1834 return luaL_error(L, "Could not allocate memory for TLS configuration");
jbe@243 1835 }
jbe@243 1836 lua_pushlightuserdata(L, tlsconf);
jbe@243 1837 luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@243 1838 lua_pushvalue(L, 1);
jbe@243 1839 lua_setuservalue(L, -2);
jbe@245 1840 #if LUA_VERSION_NUM >= 503
jbe@245 1841 if (lua_getfield(L, 1, "mode") == LUA_TSTRING) value = lua_tostring(L, -1);
jbe@245 1842 #else
jbe@245 1843 lua_getfield(L, 1, "mode");
jbe@245 1844 if (lua_type(L, -1) == LUA_TSTRING) value = lua_tostring(L, -1);
jbe@245 1845 #endif
jbe@245 1846 else value = "";
jbe@245 1847 if (strcmp(value, "server") && strcmp(value, "client")) {
jbe@245 1848 luaL_argcheck(L, 0, 1, "field \"mode\" must be set to \"server\" or \"client\"");
jbe@245 1849 }
jbe@245 1850 lua_pop(L, 1);
jbe@243 1851 moonbr_io_tlsconf_string("CA file", "ca_file", tls_config_set_ca_file);
jbe@243 1852 moonbr_io_tlsconf_string("CA path", "ca_path", tls_config_set_ca_path);
jbe@243 1853 moonbr_io_tlsconf_binary("CA", "ca_mem", tls_config_set_ca_mem);
jbe@243 1854 moonbr_io_tlsconf_string("certificate file", "cert_file", tls_config_set_cert_file);
jbe@243 1855 moonbr_io_tlsconf_binary("certificate", "cert_mem", tls_config_set_cert_mem);
jbe@243 1856 moonbr_io_tlsconf_string("key file", "key_file", tls_config_set_key_file);
jbe@243 1857 moonbr_io_tlsconf_binary("key", "key_mem", tls_config_set_key_mem);
jbe@245 1858 #if LUA_VERSION_NUM >= 503
jbe@245 1859 valuetype = lua_getfield(L, 1, "verify_client");
jbe@245 1860 #else
jbe@244 1861 lua_getfield(L, 1, "verify_client");
jbe@245 1862 #endif
jbe@244 1863 if (lua_toboolean(L, -1)) {
jbe@245 1864 #if LUA_VERSION_NUM >= 503
jbe@245 1865 if (valuetype == LUA_TSTRING) value = lua_tostring(L, -1);
jbe@245 1866 #else
jbe@244 1867 if (lua_type(L, -1) == LUA_TSTRING) value = lua_tostring(L, -1);
jbe@245 1868 #endif
jbe@244 1869 else value = "";
jbe@244 1870 if (!strcmp(value, "required")) {
jbe@244 1871 tls_config_verify_client(tlsconf);
jbe@244 1872 } else if (!strcmp(value, "optional")) {
jbe@244 1873 tls_config_verify_client_optional(tlsconf);
jbe@244 1874 } else {
jbe@244 1875 luaL_argcheck(L, 0, 1, "field \"verify_client\" must be set to \"required\", \"optional\", or be false or nil");
jbe@244 1876 }
jbe@244 1877 }
jbe@244 1878 lua_pop(L, 1);
jbe@243 1879 return 1;
jbe@243 1880 }
jbe@243 1881
jbe@243 1882 static int moonbr_io_tlsconfindex(lua_State *L) {
jbe@243 1883 struct tls_config *tlsconf;
jbe@243 1884 tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@243 1885 luaL_checkany(L, 2);
jbe@243 1886 #if LUA_VERSION_NUM >= 503
jbe@243 1887 if (lua_getuservalue(L, 1) == LUA_TNIL) {
jbe@243 1888 #else
jbe@243 1889 lua_getuservalue(L, 1);
jbe@243 1890 if (lua_isnil(L, -1)) {
jbe@243 1891 #endif
jbe@243 1892 return luaL_error(L, "Attempt to use a destroyed TLS configuration");
jbe@243 1893 }
jbe@243 1894 lua_pushvalue(L, 2);
jbe@243 1895 lua_gettable(L, -2);
jbe@243 1896 return 1;
jbe@243 1897 }
jbe@243 1898
jbe@243 1899 static int moonbr_io_tlsconfgc(lua_State *L) {
jbe@243 1900 struct tls_config *tlsconf;
jbe@243 1901 tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@243 1902 #if LUA_VERSION_NUM >= 503
jbe@243 1903 if (lua_getuservalue(L, 1) == LUA_TNIL) return 0;
jbe@243 1904 #else
jbe@243 1905 lua_getuservalue(L, 1);
jbe@243 1906 if (lua_isnil(L, -1)) return 0;
jbe@243 1907 #endif
jbe@243 1908 tls_config_free(tlsconf);
jbe@243 1909 lua_pushnil(L);
jbe@243 1910 lua_setuservalue(L, 1);
jbe@243 1911 return 0;
jbe@243 1912 }
jbe@243 1913
jbe@246 1914 static int moonbr_io_starttls(lua_State *L) {
jbe@246 1915 moonbr_io_handle_t *handle;
jbe@246 1916 struct tls_config *tlsconf;
jbe@246 1917 const char *mode;
jbe@246 1918 const char *servername;
jbe@246 1919 struct tls *tls, *tls2;
jbe@246 1920 int is_server = 0;
jbe@246 1921 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@246 1922 if (lua_type(L, 2) == LUA_TTABLE) {
jbe@246 1923 lua_pushcfunction(L, moonbr_io_tlsconf);
jbe@246 1924 lua_pushvalue(L, 2);
jbe@246 1925 lua_call(L, 1, 2);
jbe@246 1926 if (lua_isnil(L, -2)) return 2;
jbe@246 1927 lua_pop(L, 1);
jbe@246 1928 lua_replace(L, 2);
jbe@246 1929 }
jbe@246 1930 tlsconf = luaL_checkudata(L, 2, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@246 1931 if (handle->closed) return luaL_error(L, "Attempt to start TLS on a closed I/O handle");
jbe@246 1932 if (handle->finished) return luaL_error(L, "Attempt to start TLS on a finished I/O handle");
jbe@248 1933 #ifdef MOONBR_IO_USE_TLS
jbe@247 1934 if (handle->tls) return luaL_error(L, "Attempt to start TLS twice");
jbe@249 1935 #endif
jbe@246 1936 if (handle->readbufin || handle->writebufin) {
jbe@246 1937 return luaL_error(L, "Attempt to start TLS on an I/O handle with non-empty buffers");
jbe@246 1938 }
jbe@246 1939 lua_getfield(L, 2, "mode");
jbe@246 1940 mode = lua_tostring(L, -1);
jbe@246 1941 if (mode && !strcmp(mode, "server")) {
jbe@246 1942 lua_pop(L, 1);
jbe@246 1943 tls = tls_server();
jbe@246 1944 is_server = 1;
jbe@246 1945 } else if (mode && !strcmp(mode, "client")) {
jbe@246 1946 lua_pop(L, 1);
jbe@246 1947 servername = luaL_checkstring(L, 3);
jbe@246 1948 tls = tls_client();
jbe@246 1949 } else {
jbe@246 1950 /* shouldn't happen unless table has been modified */
jbe@246 1951 lua_pop(L, 1);
jbe@246 1952 return luaL_error(L, "Field \"mode\" of TLS configuration is neither set to \"server\" nor \"client\"");
jbe@246 1953 }
jbe@246 1954 if (!tls) {
jbe@246 1955 return luaL_error(L, "Could not allocate memory for TLS context");
jbe@246 1956 }
jbe@246 1957 if (tls_configure(tls, tlsconf)) goto moonbr_io_starttls_error;
jbe@246 1958 if (is_server) {
jbe@246 1959 if (tls_accept_socket(tls, &tls2, handle->fd)) goto moonbr_io_starttls_error;
jbe@246 1960 handle->servertls = tls;
jbe@246 1961 handle->tls = tls2;
jbe@246 1962 } else {
jbe@246 1963 if (tls_connect_socket(tls, handle->fd, servername)) goto moonbr_io_starttls_error;
jbe@246 1964 handle->tls = tls;
jbe@246 1965 }
jbe@246 1966 lua_settop(L, 1);
jbe@246 1967 return 1;
jbe@246 1968 moonbr_io_starttls_error:
jbe@246 1969 lua_pushnil(L);
jbe@246 1970 lua_pushstring(L, tls_error(tls));
jbe@246 1971 tls_free(tls);
jbe@246 1972 return 2;
jbe@246 1973 }
jbe@246 1974
jbe@243 1975 #endif
jbe@243 1976
jbe@79 1977 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 1978 {"read", moonbr_io_read},
jbe@85 1979 {"read_nb", moonbr_io_read_nb},
jbe@140 1980 {"read_call", moonbr_io_read_call},
jbe@140 1981 {"read_yield", moonbr_io_read_yield},
jbe@86 1982 {"drain", moonbr_io_drain},
jbe@86 1983 {"drain_nb", moonbr_io_drain_nb},
jbe@144 1984 {"drain_call", moonbr_io_drain_call},
jbe@144 1985 {"drain_yield", moonbr_io_drain_yield},
jbe@80 1986 {"write", moonbr_io_write},
jbe@81 1987 {"write_nb", moonbr_io_write_nb},
jbe@145 1988 {"write_call", moonbr_io_write_call},
jbe@145 1989 {"write_yield", moonbr_io_write_yield},
jbe@80 1990 {"flush", moonbr_io_flush},
jbe@81 1991 {"flush_nb", moonbr_io_flush_nb},
jbe@145 1992 {"flush_call", moonbr_io_flush_call},
jbe@145 1993 {"flush_yield", moonbr_io_flush_yield},
jbe@88 1994 {"finish", moonbr_io_finish},
jbe@87 1995 {"close", moonbr_io_close},
jbe@85 1996 {"reset", moonbr_io_reset},
jbe@246 1997 #ifdef MOONBR_IO_USE_TLS
jbe@246 1998 {"starttls", moonbr_io_starttls},
jbe@246 1999 #endif
jbe@79 2000 {NULL, NULL}
jbe@79 2001 };
jbe@79 2002
jbe@79 2003 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 2004 {"__index", moonbr_io_handleindex},
jbe@79 2005 {"__newindex", moonbr_io_handlenewindex},
jbe@108 2006 {"__gc", moonbr_io_handlegc},
jbe@108 2007 {NULL, NULL}
jbe@108 2008 };
jbe@108 2009
jbe@108 2010 static const struct luaL_Reg moonbr_io_listener_methods[] = {
jbe@108 2011 {"accept", moonbr_io_accept},
jbe@108 2012 {"accept_nb", moonbr_io_accept_nb},
jbe@108 2013 {"close", moonbr_io_unlisten},
jbe@108 2014 {NULL, NULL}
jbe@108 2015 };
jbe@108 2016
jbe@108 2017 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
jbe@108 2018 {"__gc", moonbr_io_listenergc},
jbe@79 2019 {NULL, NULL}
jbe@79 2020 };
jbe@79 2021
jbe@205 2022 static const struct luaL_Reg moonbr_io_child_methods[] = {
jbe@205 2023 {"kill", moonbr_io_kill},
jbe@205 2024 {"wait", moonbr_io_wait},
jbe@205 2025 {"wait_nb", moonbr_io_wait_nb},
jbe@205 2026 {"wait_call", moonbr_io_wait_call},
jbe@205 2027 {"wait_yield", moonbr_io_wait_yield},
jbe@205 2028 {NULL, NULL}
jbe@205 2029 };
jbe@205 2030
jbe@205 2031 static const struct luaL_Reg moonbr_io_child_metamethods[] = {
jbe@205 2032 {"__index", moonbr_io_childindex},
jbe@205 2033 {"__newindex", moonbr_io_childnewindex},
jbe@205 2034 {"__gc", moonbr_io_childgc},
jbe@205 2035 {NULL, NULL}
jbe@205 2036 };
jbe@205 2037
jbe@79 2038 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@111 2039 {"localconnect", moonbr_io_localconnect},
jbe@111 2040 {"localconnect_nb", moonbr_io_localconnect_nb},
jbe@98 2041 {"tcpconnect", moonbr_io_tcpconnect},
jbe@99 2042 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
jbe@112 2043 {"locallisten", moonbr_io_locallisten},
jbe@108 2044 {"tcplisten", moonbr_io_tcplisten},
jbe@205 2045 {"exec", moonbr_io_exec},
jbe@106 2046 {"poll", moonbr_io_poll},
jbe@115 2047 {"timeref", moonbr_io_timeref},
jbe@243 2048 #ifdef MOONBR_IO_USE_TLS
jbe@243 2049 {"tlsconf", moonbr_io_tlsconf},
jbe@243 2050 #endif
jbe@79 2051 {NULL, NULL}
jbe@79 2052 };
jbe@79 2053
jbe@243 2054 #ifdef MOONBR_IO_USE_TLS
jbe@243 2055 static const struct luaL_Reg moonbr_io_tlsconf_metamethods[] = {
jbe@243 2056 {"__index", moonbr_io_tlsconfindex},
jbe@243 2057 {"__gc", moonbr_io_tlsconfgc},
jbe@243 2058 {NULL, NULL}
jbe@243 2059 };
jbe@243 2060 #endif
jbe@243 2061
jbe@79 2062 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 2063
jbe@113 2064 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
jbe@113 2065
jbe@80 2066 lua_newtable(L); // module
jbe@80 2067
jbe@79 2068 lua_newtable(L); // public metatable
jbe@79 2069 lua_newtable(L); // handle methods
jbe@79 2070 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 2071 lua_pushvalue(L, -1);
jbe@146 2072 lua_setfield(L, -4, "handle_pt");
jbe@79 2073 lua_setfield(L, -2, "__index");
jbe@79 2074 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 2075
jbe@79 2076 lua_newtable(L); // handle metatable
jbe@79 2077 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@146 2078 lua_pushvalue(L, -1);
jbe@146 2079 lua_setfield(L, -3, "handle_mt");
jbe@79 2080 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 2081
jbe@108 2082 lua_newtable(L); // listener metatable
jbe@108 2083 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
jbe@108 2084 lua_newtable(L); // listener methods
jbe@108 2085 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
jbe@108 2086 lua_pushvalue(L, -1);
jbe@146 2087 lua_setfield(L, -4, "listener_pt");
jbe@108 2088 lua_setfield(L, -2, "__index");
jbe@146 2089 lua_pushvalue(L, -1);
jbe@146 2090 lua_setfield(L, -3, "listener_mt");
jbe@108 2091 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 2092
jbe@205 2093 lua_newtable(L); // child methods
jbe@205 2094 luaL_setfuncs(L, moonbr_io_child_methods, 0);
jbe@205 2095 lua_pushvalue(L, -1);
jbe@205 2096 lua_setfield(L, -3, "child_pt");
jbe@205 2097 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_PT_REGKEY);
jbe@205 2098 lua_newtable(L); // child metatable
jbe@205 2099 luaL_setfuncs(L, moonbr_io_child_metamethods, 0);
jbe@205 2100 lua_pushvalue(L, -1);
jbe@205 2101 lua_setfield(L, -3, "child_mt");
jbe@205 2102 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
jbe@205 2103
jbe@243 2104 #ifdef MOONBR_IO_USE_TLS
jbe@243 2105 if(tls_init()) {
jbe@243 2106 return luaL_error(L, "Could not initialize TLS library");
jbe@243 2107 }
jbe@243 2108 lua_newtable(L); // tlsconf metatable
jbe@243 2109 luaL_setfuncs(L, moonbr_io_tlsconf_metamethods, 0);
jbe@243 2110 lua_pushvalue(L, -1);
jbe@243 2111 lua_setfield(L, -3, "tlsconf_mt");
jbe@243 2112 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_TLSCONF_MT_REGKEY);
jbe@243 2113 #endif
jbe@243 2114
jbe@205 2115 moonbr_io_pushhandle(L, 0);
jbe@205 2116 lua_setfield(L, -2, "stdin");
jbe@205 2117 moonbr_io_pushhandle(L, 1);
jbe@205 2118 lua_setfield(L, -2, "stdout");
jbe@205 2119 moonbr_io_pushhandle(L, 2);
jbe@205 2120 lua_setfield(L, -2, "stderr");
jbe@205 2121
jbe@79 2122 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 2123 return 1;
jbe@79 2124
jbe@79 2125 }
jbe@79 2126

Impressum / About Us