moonbridge

annotate moonbridge_io.c @ 258:56c12449bba0

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

Impressum / About Us