moonbridge

annotate moonbridge_io.c @ 140:9ca22af4d4b1

Added methods "read_call" and "read_yield"
author jbe
date Fri May 01 01:46:07 2015 +0200 (2015-05-01)
parents 2097b1835d96
children 845e3dce3774
rev   line source
jbe@79 1
jbe@137 2 #if defined(__linux__)
jbe@137 3 #define _GNU_SOURCE
jbe@137 4 #endif
jbe@79 5 #include <stdlib.h>
jbe@135 6 #include <stdint.h>
jbe@135 7 #include <string.h>
jbe@135 8 #include <errno.h>
jbe@79 9 #include <unistd.h>
jbe@135 10 #include <signal.h>
jbe@135 11 #include <fcntl.h>
jbe@135 12 #include <sys/stat.h>
jbe@79 13 #include <sys/socket.h>
jbe@111 14 #include <sys/un.h>
jbe@95 15 #include <netinet/in.h>
jbe@95 16 #include <netinet/tcp.h>
jbe@135 17 #include <sys/select.h>
jbe@136 18 #include <time.h>
jbe@135 19 #include <netdb.h>
jbe@107 20 #include <arpa/inet.h>
jbe@79 21
jbe@79 22 #include <lua.h>
jbe@79 23 #include <lauxlib.h>
jbe@79 24 #include <lualib.h>
jbe@79 25
jbe@80 26 #define MOONBR_IO_MAXSTRERRORLEN 80
jbe@85 27 #define MOONBR_IO_READBUFLEN 4096
jbe@80 28 #define MOONBR_IO_WRITEBUFLEN 4096
jbe@80 29
jbe@108 30 #define MOONBR_IO_LISTEN_BACKLOG 1024
jbe@108 31
jbe@80 32 #define moonbr_io_errmsg() \
jbe@80 33 char errmsg[MOONBR_IO_MAXSTRERRORLEN]; \
jbe@80 34 strerror_r(errno, errmsg, MOONBR_IO_MAXSTRERRORLEN)
jbe@80 35
jbe@79 36 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
jbe@79 37 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
jbe@108 38 #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
jbe@79 39
jbe@79 40 typedef struct {
jbe@79 41 int fd;
jbe@107 42 int issock;
jbe@107 43 sa_family_t addrfam;
jbe@94 44 int finished;
jbe@94 45 int closed;
jbe@81 46 int nonblocking;
jbe@95 47 int nopush;
jbe@85 48 int readerr;
jbe@105 49 int readbufin;
jbe@105 50 int readbufout;
jbe@81 51 int writeerr;
jbe@83 52 size_t writeleft;
jbe@132 53 size_t flushedleft;
jbe@83 54 #if LUA_VERSION_NUM >= 503
jbe@83 55 lua_Integer writeqin;
jbe@83 56 lua_Integer writeqout;
jbe@83 57 #else
jbe@83 58 int writeqin;
jbe@83 59 int writeqout;
jbe@83 60 #endif
jbe@83 61 size_t writeqoff;
jbe@103 62 int writebufin;
jbe@103 63 int writebufout;
jbe@85 64 char readbuf[MOONBR_IO_READBUFLEN];
jbe@80 65 char writebuf[MOONBR_IO_WRITEBUFLEN];
jbe@79 66 } moonbr_io_handle_t;
jbe@79 67
jbe@108 68 typedef struct {
jbe@108 69 int fd;
jbe@118 70 sa_family_t addrfam;
jbe@108 71 int nonblocking;
jbe@108 72 } moonbr_io_listener_t;
jbe@108 73
jbe@140 74 static int moonbr_io_yield(lua_State *L) {
jbe@140 75 return lua_yield(L, 0);
jbe@140 76 }
jbe@140 77
jbe@140 78 #if LUA_VERSION_NUM >= 503
jbe@140 79 static int moonbr_io_cont_returnall(lua_State *L, int status, lua_KContext ctx) {
jbe@140 80 #else
jbe@140 81 static int moonbr_io_cont_returnall(lua_State *L) {
jbe@140 82 #endif
jbe@140 83 return lua_gettop(L);
jbe@140 84 }
jbe@140 85
jbe@81 86 static void moonbr_io_handle_set_nonblocking(lua_State *L, moonbr_io_handle_t *handle, int nonblocking) {
jbe@95 87 int flags;
jbe@95 88 if (handle->nonblocking == nonblocking) return;
jbe@95 89 flags = fcntl(handle->fd, F_GETFL, 0);
jbe@95 90 if (flags == -1) {
jbe@95 91 moonbr_io_errmsg();
jbe@96 92 close(handle->fd);
jbe@96 93 handle->fd = -1;
jbe@96 94 handle->closed = 1;
jbe@95 95 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@81 96 }
jbe@95 97 if (nonblocking) flags |= O_NONBLOCK;
jbe@95 98 else flags &= ~O_NONBLOCK;
jbe@95 99 if (fcntl(handle->fd, F_SETFL, flags) == -1) {
jbe@95 100 moonbr_io_errmsg();
jbe@96 101 close(handle->fd);
jbe@96 102 handle->fd = -1;
jbe@96 103 handle->closed = 1;
jbe@95 104 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@95 105 }
jbe@95 106 handle->nonblocking = nonblocking;
jbe@81 107 }
jbe@81 108
jbe@87 109 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) {
jbe@87 110 struct linger lingerval = { 0, };
jbe@107 111 if (!handle->issock) return;
jbe@87 112 if (timeout >= 0) {
jbe@87 113 lingerval.l_onoff = 1;
jbe@87 114 lingerval.l_linger = timeout;
jbe@87 115 }
jbe@87 116 if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
jbe@87 117 moonbr_io_errmsg();
jbe@96 118 close(handle->fd);
jbe@96 119 handle->fd = -1;
jbe@96 120 handle->closed = 1;
jbe@95 121 luaL_error(L, "Unexpected error while setting SO_LINGER with setsockopt: %s", errmsg);
jbe@87 122 }
jbe@87 123 }
jbe@87 124
jbe@133 125 static inline void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) {
jbe@96 126 #if defined(TCP_NOPUSH) || defined(TCP_CORK)
jbe@107 127 if (
jbe@107 128 !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) ||
jbe@107 129 handle->nopush == nopush
jbe@107 130 ) return;
jbe@96 131 #if defined(TCP_NOPUSH)
jbe@96 132 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) {
jbe@104 133 #elif defined(TCP_CORK)
jbe@104 134 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) {
jbe@104 135 #endif
jbe@96 136 moonbr_io_errmsg();
jbe@96 137 close(handle->fd);
jbe@96 138 handle->fd = -1;
jbe@96 139 handle->closed = 1;
jbe@104 140 #if defined(TCP_NOPUSH)
jbe@96 141 luaL_error(L, "Unexpected error while setting TCP_NOPUSH with setsockopt: %s", errmsg);
jbe@96 142 #elif defined(TCP_CORK)
jbe@95 143 luaL_error(L, "Unexpected error while setting TCP_CORK with setsockopt: %s", errmsg);
jbe@104 144 #endif
jbe@95 145 }
jbe@95 146 handle->nopush = nopush;
jbe@96 147 #else
jbe@96 148 #warning Neither TCP_NOPUSH nor TCP_CORK is available
jbe@96 149 #endif
jbe@95 150 }
jbe@95 151
jbe@86 152 static int moonbr_io_read_impl(lua_State *L, int nonblocking, int drain) {
jbe@85 153 moonbr_io_handle_t *handle;
jbe@85 154 lua_Integer maxread;
jbe@85 155 const char *terminatorstr;
jbe@85 156 size_t terminatorlen;
jbe@137 157 char terminator = 0; /* initialize to avoid compiler warning */
jbe@85 158 luaL_Buffer luabuf;
jbe@85 159 size_t luabufcnt = 0;
jbe@105 160 int remaining;
jbe@85 161 char *terminatorpos;
jbe@103 162 ssize_t bytesread;
jbe@85 163 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@85 164 maxread = luaL_optinteger(L, 2, 0);
jbe@85 165 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
jbe@85 166 if (terminatorlen) {
jbe@85 167 luaL_argcheck(L, terminatorlen == 1, 3, "single byte expected");
jbe@85 168 terminator = terminatorstr[0];
jbe@85 169 }
jbe@86 170 lua_settop(L, 1); /* return handle on drain, terminator string may be garbage collected */
jbe@94 171 if (handle->closed) luaL_error(L, "Attempt to read from a closed I/O handle");
jbe@85 172 if (handle->readerr) {
jbe@85 173 lua_pushnil(L);
jbe@85 174 lua_pushliteral(L, "Previous read error");
jbe@85 175 return 2;
jbe@85 176 }
jbe@105 177 handle->readerr = 1;
jbe@104 178 if (handle->fd < 0) goto moonbr_io_read_impl_eof; /* fake EOF to simulate shutdown */
jbe@85 179 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@86 180 if (!drain) luaL_buffinit(L, &luabuf);
jbe@85 181 while (1) {
jbe@105 182 remaining = -1;
jbe@105 183 if (
jbe@105 184 maxread > 0 &&
jbe@105 185 handle->readbufin - handle->readbufout >= (size_t)maxread - luabufcnt
jbe@105 186 ) {
jbe@105 187 remaining = maxread - luabufcnt;
jbe@85 188 } else if (terminatorlen) {
jbe@105 189 terminatorpos = memchr(
jbe@105 190 handle->readbuf + handle->readbufout,
jbe@105 191 terminator,
jbe@105 192 handle->readbufin - handle->readbufout
jbe@105 193 );
jbe@105 194 if (terminatorpos) remaining = 1 + (
jbe@105 195 terminatorpos - (handle->readbuf + handle->readbufout)
jbe@105 196 );
jbe@85 197 }
jbe@105 198 if (remaining >= 0) {
jbe@86 199 if (!drain) {
jbe@105 200 luaL_addlstring(
jbe@105 201 &luabuf,
jbe@105 202 handle->readbuf + handle->readbufout,
jbe@105 203 remaining
jbe@105 204 );
jbe@86 205 luaL_pushresult(&luabuf);
jbe@90 206 } else {
jbe@105 207 luaL_pushresult(&luabuf);
jbe@105 208 lua_pop(L, 1);
jbe@105 209 lua_pushinteger(L, luabufcnt + remaining);
jbe@86 210 }
jbe@105 211 handle->readbufout += remaining;
jbe@105 212 if (handle->readbufout == handle->readbufin) {
jbe@105 213 handle->readbufin = 0;
jbe@105 214 handle->readbufout =0;
jbe@105 215 }
jbe@104 216 handle->readerr = 0;
jbe@85 217 return 1;
jbe@85 218 }
jbe@105 219 if (!drain) luaL_addlstring(
jbe@105 220 &luabuf,
jbe@105 221 handle->readbuf + handle->readbufout,
jbe@105 222 handle->readbufin - handle->readbufout
jbe@105 223 );
jbe@105 224 luabufcnt += handle->readbufin - handle->readbufout;
jbe@85 225 do {
jbe@103 226 bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
jbe@103 227 } while (bytesread < 0 && (errno == EINTR));
jbe@105 228 if (
jbe@105 229 bytesread == 0 || (
jbe@105 230 nonblocking &&
jbe@105 231 bytesread < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)
jbe@105 232 )
jbe@105 233 ) {
jbe@105 234 handle->readbufin = 0;
jbe@105 235 handle->readbufout = 0;
jbe@105 236 if (!drain) {
jbe@105 237 luaL_pushresult(&luabuf);
jbe@105 238 if (!luabufcnt && bytesread == 0) {
jbe@105 239 lua_pop(L, 1);
jbe@105 240 moonbr_io_read_impl_eof:
jbe@105 241 lua_pushboolean(L, 0);
jbe@105 242 lua_pushliteral(L, "End of file");
jbe@105 243 handle->readerr = 0;
jbe@105 244 return 2;
jbe@105 245 }
jbe@105 246 } else {
jbe@105 247 if (!luabufcnt && bytesread == 0) lua_pushboolean(L, 1);
jbe@105 248 else lua_pushboolean(L, luabufcnt);
jbe@105 249 }
jbe@105 250 handle->readerr = 0;
jbe@105 251 return 1;
jbe@105 252 }
jbe@103 253 if (bytesread < 0) {
jbe@85 254 moonbr_io_errmsg();
jbe@85 255 lua_pushnil(L);
jbe@85 256 lua_pushstring(L, errmsg);
jbe@85 257 return 2;
jbe@85 258 }
jbe@105 259 handle->readbufin = bytesread;
jbe@105 260 handle->readbufout = 0;
jbe@86 261 }
jbe@85 262 }
jbe@85 263
jbe@85 264 static int moonbr_io_read(lua_State *L) {
jbe@86 265 return moonbr_io_read_impl(L, 0, 0);
jbe@85 266 }
jbe@85 267
jbe@85 268 static int moonbr_io_read_nb(lua_State *L) {
jbe@86 269 return moonbr_io_read_impl(L, 1, 0);
jbe@86 270 }
jbe@86 271
jbe@86 272 static int moonbr_io_drain(lua_State *L) {
jbe@86 273 return moonbr_io_read_impl(L, 0, 1);
jbe@86 274 }
jbe@86 275
jbe@86 276 static int moonbr_io_drain_nb(lua_State *L) {
jbe@86 277 return moonbr_io_read_impl(L, 1, 1);
jbe@85 278 }
jbe@85 279
jbe@140 280 #if LUA_VERSION_NUM >= 503
jbe@140 281 static int moonbr_io_read_cont(lua_State *L, int status, lua_KContext ctx) {
jbe@140 282 #else
jbe@140 283 static int moonbr_io_read_cont(lua_State *L) {
jbe@140 284 #endif
jbe@140 285 size_t len, remaining;
jbe@140 286 const char *terminatorstr = NULL;
jbe@140 287 size_t terminatorlen = 0;
jbe@140 288 const char *chunk;
jbe@140 289 size_t chunklen;
jbe@140 290 #if !(LUA_VERSION_NUM >= 503)
jbe@140 291 int ctx = 0;
jbe@140 292 lua_getctx(L, &ctx);
jbe@140 293 #endif
jbe@140 294 remaining = lua_tointeger(L, 3);
jbe@140 295 while (1) {
jbe@140 296 lua_pushcfunction(L, moonbr_io_read_nb);
jbe@140 297 lua_pushvalue(L, 1);
jbe@140 298 lua_pushvalue(L, 3);
jbe@140 299 lua_pushvalue(L, 4);
jbe@140 300 lua_call(L, 3, 2);
jbe@140 301 if (lua_isnil(L, -2)) {
jbe@140 302 return 2;
jbe@140 303 } else if (!lua_toboolean(L, -2)) {
jbe@140 304 if (ctx) {
jbe@140 305 lua_pushnil(L);
jbe@140 306 lua_pushliteral(L, "Unexpected EOF");
jbe@140 307 }
jbe@140 308 return 2;
jbe@140 309 }
jbe@140 310 len = lua_rawlen(L, -2);
jbe@140 311 if (!len) {
jbe@140 312 lua_pop(L, 2);
jbe@140 313 } else {
jbe@140 314 lua_pop(L, 1);
jbe@140 315 if (!terminatorstr) {
jbe@140 316 terminatorstr = lua_tolstring(L, 4, &terminatorlen);
jbe@140 317 if (!terminatorstr) terminatorstr = "";
jbe@140 318 }
jbe@140 319 if (terminatorlen) chunk = lua_tolstring(L, -1, &chunklen);
jbe@140 320 if (ctx == 0) {
jbe@140 321 lua_replace(L, 5);
jbe@140 322 ctx = 1;
jbe@140 323 } else if (ctx == 1) {
jbe@140 324 lua_pushvalue(L, 5);
jbe@140 325 lua_newtable(L);
jbe@140 326 lua_replace(L, 5);
jbe@140 327 lua_rawseti(L, 5, 2);
jbe@140 328 lua_rawseti(L, 5, 1);
jbe@140 329 ctx = 2;
jbe@140 330 } else {
jbe@140 331 lua_rawseti(L, 5, lua_rawlen(L, 5) + 1);
jbe@140 332 }
jbe@140 333 if (remaining) {
jbe@140 334 if (len >= remaining) break;
jbe@140 335 remaining -= len;
jbe@140 336 lua_pushinteger(L, remaining);
jbe@140 337 lua_replace(L, 3);
jbe@140 338 }
jbe@140 339 if (terminatorlen && chunk[chunklen-1] == terminatorstr[0]) break;
jbe@140 340 }
jbe@140 341 lua_pushvalue(L, 2);
jbe@140 342 lua_pushvalue(L, 1);
jbe@140 343 lua_pushliteral(L, "r");
jbe@140 344 lua_callk(L, 2, 0, ctx, moonbr_io_read_cont);
jbe@140 345 }
jbe@140 346 if (ctx == 1) {
jbe@140 347 lua_pushvalue(L, 5);
jbe@140 348 } else {
jbe@140 349 luaL_Buffer buf;
jbe@140 350 lua_Integer i, chunkcount;
jbe@140 351 chunkcount = lua_rawlen(L, 5);
jbe@140 352 luaL_buffinit(L, &buf);
jbe@140 353 for (i=1; i<=chunkcount && i>0; i++) {
jbe@140 354 lua_rawgeti(L, 5, i);
jbe@140 355 luaL_addvalue(&buf);
jbe@140 356 }
jbe@140 357 luaL_pushresult(&buf);
jbe@140 358 }
jbe@140 359 return 1;
jbe@140 360 }
jbe@140 361
jbe@140 362 static int moonbr_io_read_call(lua_State *L) {
jbe@140 363 lua_settop(L, 4);
jbe@140 364 lua_pushnil(L);
jbe@140 365 #if LUA_VERSION_NUM >= 503
jbe@140 366 return moonbr_io_read_cont(L, 0, 0);
jbe@140 367 #else
jbe@140 368 return moonbr_io_read_cont(L);
jbe@140 369 #endif
jbe@140 370 }
jbe@140 371
jbe@140 372 static int moonbr_io_read_yield(lua_State *L) {
jbe@140 373 int args;
jbe@140 374 lua_pushcfunction(L, moonbr_io_read_call);
jbe@140 375 lua_insert(L, 1);
jbe@140 376 args = lua_gettop(L);
jbe@140 377 lua_pushcfunction(L, moonbr_io_yield);
jbe@140 378 lua_insert(L, 3);
jbe@140 379 lua_callk(L, args, LUA_MULTRET, 0, moonbr_io_cont_returnall);
jbe@140 380 return lua_gettop(L);
jbe@140 381 }
jbe@140 382
jbe@81 383 static int moonbr_io_write_impl(lua_State *L, int nonblocking, int flush) {
jbe@80 384 moonbr_io_handle_t *handle;
jbe@80 385 int i, top;
jbe@80 386 const char *str;
jbe@92 387 size_t strlen;
jbe@103 388 ssize_t written;
jbe@80 389 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 390 if (handle->closed) luaL_error(L, "Attempt to write to a closed I/O handle");
jbe@94 391 if (handle->finished) luaL_error(L, "Attempt to write to a finished I/O handle");
jbe@81 392 if (handle->writeerr) {
jbe@80 393 lua_pushnil(L);
jbe@80 394 lua_pushliteral(L, "Previous write error");
jbe@80 395 return 2;
jbe@80 396 }
jbe@103 397 handle->writeerr = 1;
jbe@81 398 moonbr_io_handle_set_nonblocking(L, handle, nonblocking);
jbe@84 399 top = lua_gettop(L);
jbe@81 400 lua_getuservalue(L, 1);
jbe@103 401 lua_getfield(L, -1, "writequeue");
jbe@84 402 for (i=2; i<=top; i++) {
jbe@84 403 luaL_checklstring(L, i, &strlen);
jbe@84 404 lua_pushvalue(L, i);
jbe@84 405 lua_rawseti(L, -2, handle->writeqin++);
jbe@84 406 handle->writeleft += strlen;
jbe@81 407 }
jbe@132 408 if (flush) handle->flushedleft = handle->writeleft;
jbe@83 409 while (handle->writeqout != handle->writeqin) {
jbe@83 410 lua_rawgeti(L, -1, handle->writeqout);
jbe@81 411 str = lua_tolstring(L, -1, &strlen);
jbe@92 412 while (handle->writeqoff < strlen) {
jbe@103 413 if (
jbe@132 414 strlen - handle->writeqoff <
jbe@103 415 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 416 ) {
jbe@103 417 memcpy(
jbe@103 418 handle->writebuf + handle->writebufin,
jbe@103 419 str + handle->writeqoff,
jbe@103 420 strlen - handle->writeqoff
jbe@103 421 );
jbe@103 422 handle->writebufin += strlen - handle->writeqoff;
jbe@80 423 break;
jbe@80 424 } else {
jbe@103 425 memcpy(
jbe@103 426 handle->writebuf + handle->writebufin,
jbe@103 427 str + handle->writeqoff,
jbe@103 428 MOONBR_IO_WRITEBUFLEN - handle->writebufin
jbe@103 429 );
jbe@103 430 handle->writeqoff += MOONBR_IO_WRITEBUFLEN - handle->writebufin;
jbe@103 431 while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
jbe@133 432 moonbr_io_handle_set_nopush(L, handle, 1);
jbe@103 433 written = write(
jbe@103 434 handle->fd,
jbe@103 435 handle->writebuf + handle->writebufout,
jbe@103 436 MOONBR_IO_WRITEBUFLEN - handle->writebufout
jbe@103 437 );
jbe@103 438 if (written < 0) {
jbe@81 439 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@81 440 goto moonbr_io_write_impl_block;
jbe@81 441 } else if (errno != EINTR) {
jbe@80 442 moonbr_io_errmsg();
jbe@80 443 lua_pushnil(L);
jbe@80 444 lua_pushstring(L, errmsg);
jbe@80 445 return 2;
jbe@80 446 }
jbe@103 447 } else {
jbe@103 448 handle->writebufout += written;
jbe@103 449 handle->writeleft -= written;
jbe@133 450 if (handle->flushedleft) {
jbe@133 451 if (written >= handle->flushedleft) {
jbe@133 452 handle->flushedleft = 0;
jbe@133 453 moonbr_io_handle_set_nopush(L, handle, 0);
jbe@133 454 } else {
jbe@133 455 handle->flushedleft -= written;
jbe@133 456 }
jbe@133 457 }
jbe@80 458 }
jbe@80 459 }
jbe@103 460 handle->writebufin = 0;
jbe@103 461 handle->writebufout = 0;
jbe@80 462 }
jbe@80 463 }
jbe@81 464 handle->writeqoff = 0;
jbe@81 465 lua_pop(L, 1);
jbe@81 466 lua_pushnil(L);
jbe@83 467 lua_rawseti(L, -2, handle->writeqout++);
jbe@80 468 }
jbe@132 469 while (handle->flushedleft) {
jbe@133 470 moonbr_io_handle_set_nopush(L, handle, 1);
jbe@132 471 written = write(
jbe@132 472 handle->fd,
jbe@132 473 handle->writebuf + handle->writebufout,
jbe@132 474 handle->writebufin - handle->writebufout
jbe@132 475 );
jbe@132 476 if (written < 0) {
jbe@132 477 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@132 478 goto moonbr_io_write_impl_block;
jbe@132 479 } else if (errno != EINTR) {
jbe@132 480 moonbr_io_errmsg();
jbe@132 481 lua_pushnil(L);
jbe@132 482 lua_pushstring(L, errmsg);
jbe@132 483 return 2;
jbe@81 484 }
jbe@132 485 } else {
jbe@132 486 handle->writebufout += written;
jbe@132 487 handle->writeleft -= written;
jbe@133 488 if (handle->flushedleft) {
jbe@133 489 if (written >= handle->flushedleft) {
jbe@133 490 handle->flushedleft = 0;
jbe@133 491 moonbr_io_handle_set_nopush(L, handle, 0);
jbe@133 492 } else {
jbe@133 493 handle->flushedleft -= written;
jbe@133 494 }
jbe@133 495 }
jbe@81 496 }
jbe@132 497 }
jbe@132 498 if (handle->writebufout == handle->writebufin) {
jbe@103 499 handle->writebufin = 0;
jbe@103 500 handle->writebufout = 0;
jbe@81 501 }
jbe@103 502 if (nonblocking) lua_pushinteger(L, 0);
jbe@103 503 else lua_pushvalue(L, 1);
jbe@103 504 handle->writeerr = 0;
jbe@80 505 return 1;
jbe@81 506 moonbr_io_write_impl_block:
jbe@91 507 lua_pushinteger(L, handle->writeleft);
jbe@103 508 handle->writeerr = 0;
jbe@81 509 return 1;
jbe@81 510 }
jbe@81 511
jbe@81 512 static int moonbr_io_write(lua_State *L) {
jbe@81 513 return moonbr_io_write_impl(L, 0, 0);
jbe@81 514 }
jbe@81 515
jbe@81 516 static int moonbr_io_write_nb(lua_State *L) {
jbe@81 517 return moonbr_io_write_impl(L, 1, 0);
jbe@80 518 }
jbe@80 519
jbe@80 520 static int moonbr_io_flush(lua_State *L) {
jbe@81 521 return moonbr_io_write_impl(L, 0, 1);
jbe@81 522 }
jbe@81 523
jbe@81 524 static int moonbr_io_flush_nb(lua_State *L) {
jbe@81 525 return moonbr_io_write_impl(L, 1, 1);
jbe@80 526 }
jbe@80 527
jbe@88 528 static int moonbr_io_finish(lua_State *L) {
jbe@88 529 moonbr_io_handle_t *handle;
jbe@88 530 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 531 if (handle->closed) luaL_error(L, "Attempt to finish a closed I/O handle");
jbe@94 532 if (handle->finished) luaL_error(L, "Attempt to finish a finished I/O handle");
jbe@94 533 if (handle->writeleft) {
jbe@94 534 lua_pushcfunction(L, moonbr_io_flush);
jbe@94 535 lua_pushvalue(L, 1);
jbe@116 536 if (lua_pcall(L, 1, 2, 0)) {
jbe@116 537 handle->finished = 1;
jbe@116 538 lua_error(L);
jbe@116 539 }
jbe@94 540 if (!lua_toboolean(L, -2)) {
jbe@94 541 handle->finished = 1;
jbe@94 542 return 2;
jbe@88 543 }
jbe@94 544 }
jbe@94 545 handle->finished = 1;
jbe@107 546 if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) {
jbe@88 547 if (shutdown(handle->fd, SHUT_WR)) {
jbe@88 548 moonbr_io_errmsg();
jbe@88 549 lua_pushnil(L);
jbe@88 550 lua_pushstring(L, errmsg);
jbe@88 551 return 2;
jbe@88 552 }
jbe@94 553 } else {
jbe@94 554 if (close(handle->fd)) {
jbe@94 555 moonbr_io_errmsg();
jbe@94 556 handle->fd = -1;
jbe@94 557 lua_pushnil(L);
jbe@94 558 lua_pushstring(L, errmsg);
jbe@94 559 return 2;
jbe@94 560 }
jbe@94 561 handle->fd = -1; /* fake EOF on read */
jbe@88 562 }
jbe@88 563 lua_pushboolean(L, 1);
jbe@88 564 return 1;
jbe@88 565 }
jbe@88 566
jbe@94 567 static int moonbr_io_close_impl(lua_State *L, int reset) {
jbe@83 568 moonbr_io_handle_t *handle;
jbe@83 569 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@94 570 if (handle->closed) luaL_error(L, "Attempt to close a closed I/O handle");
jbe@94 571 if (!reset) {
jbe@87 572 if (handle->writeleft) {
jbe@87 573 lua_pushcfunction(L, moonbr_io_flush);
jbe@87 574 lua_pushvalue(L, 1);
jbe@116 575 if (lua_pcall(L, 1, 2, 0)) {
jbe@129 576 handle->closed = 1;
jbe@116 577 close(handle->fd);
jbe@116 578 handle->fd = -1;
jbe@116 579 lua_error(L);
jbe@116 580 }
jbe@129 581 handle->closed = 1;
jbe@87 582 if (!lua_toboolean(L, -2)) {
jbe@87 583 close(handle->fd);
jbe@87 584 handle->fd = -1;
jbe@87 585 return 2;
jbe@87 586 }
jbe@129 587 } else {
jbe@129 588 handle->closed = 1;
jbe@129 589 moonbr_io_handle_set_linger(L, handle, -1);
jbe@83 590 }
jbe@129 591 } else {
jbe@129 592 handle->closed = 1;
jbe@83 593 }
jbe@94 594 if (handle->fd >= 0) {
jbe@94 595 if (close(handle->fd)) {
jbe@94 596 moonbr_io_errmsg();
jbe@94 597 handle->fd = -1;
jbe@94 598 lua_pushnil(L);
jbe@94 599 lua_pushstring(L, errmsg);
jbe@94 600 return 2;
jbe@94 601 }
jbe@104 602 handle->fd = -1;
jbe@83 603 }
jbe@83 604 lua_pushboolean(L, 1);
jbe@83 605 return 1;
jbe@84 606
jbe@83 607 }
jbe@83 608
jbe@94 609 static int moonbr_io_close(lua_State *L) {
jbe@94 610 return moonbr_io_close_impl(L, 0);
jbe@94 611 }
jbe@94 612
jbe@84 613 static int moonbr_io_reset(lua_State *L) {
jbe@94 614 return moonbr_io_close_impl(L, 1);
jbe@84 615 }
jbe@84 616
jbe@108 617 static int moonbr_io_handlegc(lua_State *L) {
jbe@88 618 moonbr_io_handle_t *handle;
jbe@88 619 handle = luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@88 620 if (handle->fd >= 0) {
jbe@88 621 lua_pushcfunction(L, moonbr_io_close);
jbe@88 622 lua_pushvalue(L, 1);
jbe@88 623 lua_pushinteger(L, 0);
jbe@88 624 lua_call(L, 2, 0);
jbe@88 625 }
jbe@88 626 return 0;
jbe@88 627 }
jbe@88 628
jbe@100 629 void moonbr_io_closehandle(lua_State *L, int idx, int reset) {
jbe@88 630 moonbr_io_handle_t *handle;
jbe@88 631 handle = luaL_checkudata(L, idx, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@100 632 if (!handle->closed) {
jbe@100 633 lua_pushcfunction(L, reset ? moonbr_io_reset : moonbr_io_close);
jbe@100 634 lua_pushvalue(L, idx < 0 ? idx-1 : idx);
jbe@100 635 lua_call(L, 1, 0);
jbe@88 636 }
jbe@88 637 }
jbe@88 638
jbe@107 639 void moonbr_io_pushhandle(lua_State *L, int fd) {
jbe@79 640 moonbr_io_handle_t *handle;
jbe@107 641 struct sockaddr addr;
jbe@107 642 socklen_t addrlen;
jbe@79 643 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
jbe@79 644 handle->fd = fd;
jbe@107 645 addrlen = sizeof(addr);
jbe@107 646 if (getsockname(fd, &addr, &addrlen)) {
jbe@107 647 if (errno != ENOTSOCK) {
jbe@107 648 moonbr_io_errmsg();
jbe@107 649 luaL_error(L, "Unexpected error when examining socket: %s", errmsg);
jbe@107 650 }
jbe@107 651 handle->issock = 0;
jbe@107 652 } else {
jbe@107 653 handle->issock = 1;
jbe@107 654 handle->addrfam = addr.sa_family;
jbe@107 655 }
jbe@94 656 handle->finished = 0;
jbe@94 657 handle->closed = 0;
jbe@81 658 handle->nonblocking = -1;
jbe@95 659 handle->nopush = -1;
jbe@85 660 handle->readerr = 0;
jbe@105 661 handle->readbufin = 0;
jbe@105 662 handle->readbufout = 0;
jbe@81 663 handle->writeerr = 0;
jbe@81 664 handle->writeleft = 0;
jbe@132 665 handle->flushedleft = 0;
jbe@83 666 handle->writeqin = 0;
jbe@83 667 handle->writeqout = 0;
jbe@81 668 handle->writeqoff = 0;
jbe@103 669 handle->writebufin = 0;
jbe@103 670 handle->writebufout = 0;
jbe@87 671 moonbr_io_handle_set_linger(L, handle, 0);
jbe@79 672 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 673 lua_setmetatable(L, -2);
jbe@79 674 lua_newtable(L); // uservalue
jbe@81 675 lua_newtable(L);
jbe@103 676 lua_setfield(L, -2, "writequeue");
jbe@79 677 lua_newtable(L); // public
jbe@107 678 if (handle->addrfam == AF_INET6) {
jbe@107 679 struct sockaddr_in6 addr_in6;
jbe@107 680 char addrstrbuf[INET6_ADDRSTRLEN];
jbe@107 681 const char *addrstr;
jbe@107 682 addrlen = sizeof(addr_in6);
jbe@107 683 if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@107 684 moonbr_io_errmsg();
jbe@107 685 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 686 }
jbe@107 687 if (addrlen > sizeof(addr_in6)) {
jbe@107 688 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 689 }
jbe@107 690 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 691 if (!addrstr) {
jbe@107 692 moonbr_io_errmsg();
jbe@107 693 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 694 } else {
jbe@107 695 lua_pushstring(L, addrstr);
jbe@107 696 lua_setfield(L, -2, "local_ip6");
jbe@107 697 }
jbe@107 698 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 699 lua_setfield(L, -2, "local_tcpport");
jbe@107 700 if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) {
jbe@107 701 moonbr_io_errmsg();
jbe@107 702 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 703 }
jbe@107 704 if (addrlen > sizeof(addr_in6)) {
jbe@107 705 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 706 }
jbe@107 707 addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 708 if (!addrstr) {
jbe@107 709 moonbr_io_errmsg();
jbe@107 710 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 711 } else {
jbe@107 712 lua_pushstring(L, addrstr);
jbe@107 713 lua_setfield(L, -2, "remote_ip6");
jbe@107 714 }
jbe@107 715 lua_pushinteger(L, ntohs(addr_in6.sin6_port));
jbe@107 716 lua_setfield(L, -2, "remote_tcpport");
jbe@107 717 } else if (handle->addrfam == AF_INET) {
jbe@107 718 struct sockaddr_in addr_in;
jbe@107 719 char addrstrbuf[INET_ADDRSTRLEN];
jbe@107 720 const char *addrstr;
jbe@107 721 addrlen = sizeof(addr_in);
jbe@107 722 if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@107 723 moonbr_io_errmsg();
jbe@107 724 luaL_error(L, "Could not determine local IP address/port: %s", errmsg);
jbe@107 725 }
jbe@107 726 if (addrlen > sizeof(addr_in)) {
jbe@107 727 luaL_error(L, "Could not determine local IP address/port: buffer size exceeded");
jbe@107 728 }
jbe@107 729 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 730 if (!addrstr) {
jbe@107 731 moonbr_io_errmsg();
jbe@107 732 luaL_error(L, "Could not format local IP address: %s", errmsg);
jbe@107 733 } else {
jbe@107 734 lua_pushstring(L, addrstr);
jbe@107 735 lua_setfield(L, -2, "local_ip4");
jbe@107 736 }
jbe@107 737 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 738 lua_setfield(L, -2, "local_tcpport");
jbe@107 739 if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) {
jbe@107 740 moonbr_io_errmsg();
jbe@107 741 luaL_error(L, "Could not determine remote IP address/port: %s", errmsg);
jbe@107 742 }
jbe@107 743 if (addrlen > sizeof(addr_in)) {
jbe@107 744 luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded");
jbe@107 745 }
jbe@107 746 addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
jbe@107 747 if (!addrstr) {
jbe@107 748 moonbr_io_errmsg();
jbe@107 749 luaL_error(L, "Could not format remote IP address: %s", errmsg);
jbe@107 750 } else {
jbe@107 751 lua_pushstring(L, addrstr);
jbe@107 752 lua_setfield(L, -2, "remote_ip4");
jbe@107 753 }
jbe@107 754 lua_pushinteger(L, ntohs(addr_in.sin_port));
jbe@107 755 lua_setfield(L, -2, "remote_tcpport");
jbe@107 756 }
jbe@79 757 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 758 lua_setmetatable(L, -2);
jbe@79 759 lua_setfield(L, -2, "public");
jbe@79 760 lua_setuservalue(L, -2);
jbe@79 761 }
jbe@79 762
jbe@79 763 static int moonbr_io_handleindex(lua_State *L) {
jbe@80 764 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 765 lua_getuservalue(L, 1);
jbe@79 766 lua_getfield(L, -1, "public");
jbe@79 767 lua_pushvalue(L, 2);
jbe@79 768 lua_gettable(L, -2);
jbe@79 769 return 1;
jbe@79 770 }
jbe@79 771
jbe@79 772 static int moonbr_io_handlenewindex(lua_State *L) {
jbe@80 773 luaL_checkudata(L, 1, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 774 lua_getuservalue(L, 1);
jbe@79 775 lua_getfield(L, -1, "public");
jbe@79 776 lua_pushvalue(L, 2);
jbe@79 777 lua_pushvalue(L, 3);
jbe@79 778 lua_settable(L, -3);
jbe@79 779 return 0;
jbe@79 780 }
jbe@79 781
jbe@111 782 static int moonbr_io_localconnect_impl(lua_State *L, int nonblocking) {
jbe@111 783 const char *path;
jbe@111 784 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@111 785 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@111 786 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@111 787 ) - 1; /* one byte for termination */
jbe@111 788 int sock;
jbe@111 789 path = luaL_checkstring(L, 1);
jbe@111 790 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@111 791 strcpy(sockaddr.sun_path, path);
jbe@111 792 sock = socket(
jbe@111 793 PF_LOCAL,
jbe@111 794 SOCK_STREAM | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@111 795 0
jbe@111 796 );
jbe@111 797 if (sock < 0) {
jbe@111 798 moonbr_io_errmsg();
jbe@111 799 lua_pushnil(L);
jbe@111 800 lua_pushstring(L, errmsg);
jbe@111 801 return 2;
jbe@111 802 }
jbe@111 803 if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@111 804 if (!nonblocking && errno == EINTR) {
jbe@111 805 moonbr_io_errmsg();
jbe@111 806 close(sock);
jbe@111 807 lua_pushnil(L);
jbe@111 808 lua_pushstring(L, errmsg);
jbe@111 809 return 2;
jbe@111 810 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
jbe@111 811 moonbr_io_errmsg();
jbe@111 812 lua_pushnil(L);
jbe@111 813 lua_pushstring(L, errmsg);
jbe@111 814 return 2;
jbe@111 815 }
jbe@111 816 }
jbe@111 817 moonbr_io_pushhandle(L, sock);
jbe@111 818 return 1;
jbe@111 819 }
jbe@111 820
jbe@111 821 static int moonbr_io_localconnect(lua_State *L) {
jbe@111 822 return moonbr_io_localconnect_impl(L, 0);
jbe@111 823 }
jbe@111 824
jbe@111 825 static int moonbr_io_localconnect_nb(lua_State *L) {
jbe@111 826 return moonbr_io_localconnect_impl(L, 1);
jbe@111 827 }
jbe@111 828
jbe@99 829 static int moonbr_io_tcpconnect_impl(lua_State *L, int nonblocking) {
jbe@98 830 const char *host, *port;
jbe@98 831 struct addrinfo hints = { 0, };
jbe@98 832 struct addrinfo *res, *addrinfo;
jbe@98 833 int errcode;
jbe@98 834 int sock;
jbe@98 835 host = luaL_checkstring(L, 1);
jbe@98 836 port = luaL_checkstring(L, 2);
jbe@98 837 hints.ai_family = AF_UNSPEC;
jbe@98 838 hints.ai_socktype = SOCK_STREAM;
jbe@98 839 hints.ai_protocol = IPPROTO_TCP;
jbe@98 840 hints.ai_flags = AI_ADDRCONFIG;
jbe@98 841 errcode = getaddrinfo(host, port, &hints, &res);
jbe@98 842 if (errcode) {
jbe@102 843 freeaddrinfo(res);
jbe@98 844 if (errcode == EAI_SYSTEM) {
jbe@98 845 moonbr_io_errmsg();
jbe@98 846 lua_pushnil(L);
jbe@98 847 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@98 848 } else {
jbe@98 849 lua_pushnil(L);
jbe@98 850 lua_pushstring(L, gai_strerror(errcode));
jbe@98 851 }
jbe@98 852 return 2;
jbe@98 853 }
jbe@98 854 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 855 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@98 856 }
jbe@98 857 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 858 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@98 859 }
jbe@98 860 addrinfo = res;
jbe@98 861 moonbr_io_tcpconnect_found:
jbe@99 862 sock = socket(
jbe@134 863 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@99 864 addrinfo->ai_socktype | SOCK_CLOEXEC | (nonblocking ? SOCK_NONBLOCK : 0),
jbe@99 865 addrinfo->ai_protocol
jbe@99 866 );
jbe@98 867 if (sock < 0) {
jbe@98 868 moonbr_io_errmsg();
jbe@108 869 freeaddrinfo(res);
jbe@98 870 lua_pushnil(L);
jbe@98 871 lua_pushstring(L, errmsg);
jbe@108 872 return 2;
jbe@98 873 }
jbe@98 874 if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@108 875 freeaddrinfo(res);
jbe@99 876 if (!nonblocking && errno == EINTR) {
jbe@99 877 moonbr_io_errmsg();
jbe@99 878 close(sock);
jbe@99 879 lua_pushnil(L);
jbe@99 880 lua_pushstring(L, errmsg);
jbe@99 881 return 2;
jbe@99 882 } else if (!(nonblocking && (errno == EINPROGRESS || errno == EINTR))) {
jbe@99 883 moonbr_io_errmsg();
jbe@99 884 lua_pushnil(L);
jbe@99 885 lua_pushstring(L, errmsg);
jbe@99 886 return 2;
jbe@99 887 }
jbe@108 888 } else {
jbe@108 889 freeaddrinfo(res);
jbe@98 890 }
jbe@107 891 moonbr_io_pushhandle(L, sock);
jbe@98 892 return 1;
jbe@98 893 }
jbe@98 894
jbe@99 895 static int moonbr_io_tcpconnect(lua_State *L) {
jbe@99 896 return moonbr_io_tcpconnect_impl(L, 0);
jbe@99 897 }
jbe@99 898
jbe@99 899 static int moonbr_io_tcpconnect_nb(lua_State *L) {
jbe@99 900 return moonbr_io_tcpconnect_impl(L, 1);
jbe@99 901 }
jbe@99 902
jbe@112 903 static int moonbr_io_locallisten(lua_State *L) {
jbe@112 904 moonbr_io_listener_t *listener;
jbe@112 905 const char *path;
jbe@118 906 struct stat sb;
jbe@112 907 struct sockaddr_un sockaddr = { .sun_family = AF_LOCAL };
jbe@112 908 const int path_maxlen = sizeof(struct sockaddr_un) - (
jbe@112 909 (void *)sockaddr.sun_path - (void *)&sockaddr
jbe@112 910 ) - 1; /* one byte for termination */
jbe@112 911 int sock;
jbe@112 912 path = luaL_checkstring(L, 1);
jbe@112 913 if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen);
jbe@112 914 strcpy(sockaddr.sun_path, path);
jbe@118 915 if (stat(path, &sb) == 0) {
jbe@118 916 if (S_ISSOCK(sb.st_mode)) unlink(path);
jbe@118 917 }
jbe@112 918 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 919 listener->fd = -1;
jbe@112 920 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@112 921 sock = socket(
jbe@112 922 PF_LOCAL,
jbe@112 923 SOCK_STREAM | SOCK_CLOEXEC,
jbe@112 924 0
jbe@112 925 );
jbe@112 926 if (sock < 0) {
jbe@112 927 moonbr_io_errmsg();
jbe@112 928 lua_pushnil(L);
jbe@112 929 lua_pushstring(L, errmsg);
jbe@112 930 return 2;
jbe@112 931 }
jbe@112 932 if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
jbe@112 933 moonbr_io_errmsg();
jbe@112 934 close(sock);
jbe@112 935 lua_pushnil(L);
jbe@112 936 lua_pushstring(L, errmsg);
jbe@112 937 return 2;
jbe@112 938 }
jbe@112 939 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@112 940 moonbr_io_errmsg();
jbe@112 941 close(sock);
jbe@112 942 lua_pushnil(L);
jbe@112 943 lua_pushstring(L, errmsg);
jbe@112 944 return 2;
jbe@112 945 }
jbe@112 946 listener->fd = sock;
jbe@118 947 listener->addrfam = AF_LOCAL;
jbe@112 948 listener->nonblocking = -1;
jbe@112 949 return 1;
jbe@112 950 }
jbe@112 951
jbe@108 952 static int moonbr_io_tcplisten(lua_State *L) {
jbe@108 953 moonbr_io_listener_t *listener;
jbe@108 954 const char *host, *port;
jbe@108 955 struct addrinfo hints = { 0, };
jbe@108 956 struct addrinfo *res, *addrinfo;
jbe@108 957 int errcode;
jbe@108 958 int sock;
jbe@108 959 host = luaL_optstring(L, 1, NULL);
jbe@108 960 port = luaL_checkstring(L, 2);
jbe@108 961 listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t));
jbe@117 962 listener->fd = -1;
jbe@108 963 luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 964 hints.ai_family = AF_UNSPEC;
jbe@108 965 hints.ai_socktype = SOCK_STREAM;
jbe@108 966 hints.ai_protocol = IPPROTO_TCP;
jbe@108 967 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
jbe@108 968 errcode = getaddrinfo(host, port, &hints, &res);
jbe@108 969 if (errcode) {
jbe@108 970 freeaddrinfo(res);
jbe@108 971 if (errcode == EAI_SYSTEM) {
jbe@108 972 moonbr_io_errmsg();
jbe@108 973 lua_pushnil(L);
jbe@108 974 lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg);
jbe@108 975 } else {
jbe@108 976 lua_pushnil(L);
jbe@108 977 lua_pushstring(L, gai_strerror(errcode));
jbe@108 978 }
jbe@108 979 return 2;
jbe@108 980 }
jbe@108 981 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 982 if (addrinfo->ai_family == AF_INET6) goto moonbr_io_tcpconnect_found;
jbe@108 983 }
jbe@108 984 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
jbe@134 985 if (addrinfo->ai_family == AF_INET) goto moonbr_io_tcpconnect_found;
jbe@108 986 }
jbe@108 987 addrinfo = res;
jbe@108 988 moonbr_io_tcpconnect_found:
jbe@118 989 listener->addrfam = addrinfo->ai_family;
jbe@108 990 sock = socket(
jbe@134 991 addrinfo->ai_family, /* NOTE: not correctly using PF_* but AF_* constants here */
jbe@108 992 addrinfo->ai_socktype | SOCK_CLOEXEC,
jbe@108 993 addrinfo->ai_protocol
jbe@108 994 );
jbe@108 995 if (sock < 0) {
jbe@108 996 moonbr_io_errmsg();
jbe@108 997 freeaddrinfo(res);
jbe@108 998 lua_pushnil(L);
jbe@108 999 lua_pushstring(L, errmsg);
jbe@108 1000 return 2;
jbe@108 1001 }
jbe@120 1002 {
jbe@120 1003 static const int reuseval = 1;
jbe@120 1004 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval))) {
jbe@120 1005 moonbr_io_errmsg();
jbe@120 1006 freeaddrinfo(res);
jbe@120 1007 close(sock);
jbe@120 1008 lua_pushnil(L);
jbe@120 1009 lua_pushfstring(L, "Error while setting SO_REUSEADDR with setsockopt: %s", errmsg);
jbe@120 1010 return 2;
jbe@120 1011 }
jbe@120 1012 }
jbe@108 1013 if (bind(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) {
jbe@108 1014 moonbr_io_errmsg();
jbe@108 1015 freeaddrinfo(res);
jbe@108 1016 close(sock);
jbe@108 1017 lua_pushnil(L);
jbe@108 1018 lua_pushstring(L, errmsg);
jbe@108 1019 return 2;
jbe@108 1020 }
jbe@108 1021 freeaddrinfo(res);
jbe@108 1022 if (listen(sock, MOONBR_IO_LISTEN_BACKLOG)) {
jbe@108 1023 moonbr_io_errmsg();
jbe@108 1024 close(sock);
jbe@108 1025 lua_pushnil(L);
jbe@108 1026 lua_pushstring(L, errmsg);
jbe@108 1027 return 2;
jbe@108 1028 }
jbe@108 1029 listener->fd = sock;
jbe@108 1030 listener->nonblocking = -1;
jbe@108 1031 return 1;
jbe@108 1032 }
jbe@108 1033
jbe@108 1034 static int moonbr_io_accept_impl(lua_State *L, int nonblocking) {
jbe@108 1035 moonbr_io_listener_t *listener;
jbe@108 1036 int fd;
jbe@108 1037 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1038 if (listener->fd < 0) luaL_error(L, "Attempt to use a closed listener");
jbe@108 1039 if (listener->nonblocking != nonblocking) {
jbe@108 1040 int flags;
jbe@108 1041 flags = fcntl(listener->fd, F_GETFL, 0);
jbe@108 1042 if (flags == -1) {
jbe@108 1043 moonbr_io_errmsg();
jbe@108 1044 close(listener->fd);
jbe@108 1045 listener->fd = -1;
jbe@108 1046 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1047 }
jbe@108 1048 if (nonblocking) flags |= O_NONBLOCK;
jbe@108 1049 else flags &= ~O_NONBLOCK;
jbe@108 1050 if (fcntl(listener->fd, F_SETFL, flags) == -1) {
jbe@108 1051 moonbr_io_errmsg();
jbe@108 1052 close(listener->fd);
jbe@108 1053 listener->fd = -1;
jbe@108 1054 luaL_error(L, "Unexpected error in fcntl call: %s", errmsg);
jbe@108 1055 }
jbe@108 1056 listener->nonblocking = nonblocking;
jbe@108 1057 }
jbe@108 1058 while (1) {
jbe@108 1059 fd = accept4(listener->fd, NULL, NULL, SOCK_CLOEXEC);
jbe@108 1060 if (fd < 0) {
jbe@108 1061 if (nonblocking && (errno == EAGAIN || errno == EWOULDBLOCK)) {
jbe@108 1062 lua_pushboolean(L, 0);
jbe@108 1063 lua_pushliteral(L, "No incoming connection pending");
jbe@108 1064 return 2;
jbe@108 1065 } else if (errno != EINTR) {
jbe@108 1066 moonbr_io_errmsg();
jbe@108 1067 lua_pushnil(L);
jbe@108 1068 lua_pushstring(L, errmsg);
jbe@108 1069 return 2;
jbe@108 1070 }
jbe@108 1071 } else {
jbe@108 1072 moonbr_io_pushhandle(L, fd);
jbe@108 1073 return 1;
jbe@108 1074 }
jbe@108 1075 }
jbe@108 1076 }
jbe@108 1077
jbe@108 1078 static int moonbr_io_accept(lua_State *L) {
jbe@108 1079 return moonbr_io_accept_impl(L, 0);
jbe@108 1080 }
jbe@108 1081
jbe@108 1082 static int moonbr_io_accept_nb(lua_State *L) {
jbe@108 1083 return moonbr_io_accept_impl(L, 1);
jbe@108 1084 }
jbe@108 1085
jbe@108 1086 static int moonbr_io_unlisten(lua_State *L) {
jbe@108 1087 moonbr_io_listener_t *listener;
jbe@118 1088 struct sockaddr_un addr;
jbe@118 1089 socklen_t addrlen;
jbe@118 1090 struct stat sb;
jbe@108 1091 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1092 if (listener->fd < 0) luaL_error(L, "Attempt to close a closed listener");
jbe@118 1093 addrlen = sizeof(addr);
jbe@118 1094 if (getsockname(listener->fd, (struct sockaddr *)&addr, &addrlen)) addrlen = 0;
jbe@108 1095 if (close(listener->fd)) {
jbe@108 1096 moonbr_io_errmsg();
jbe@108 1097 listener->fd = -1;
jbe@118 1098 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1099 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1100 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1101 }
jbe@118 1102 }
jbe@108 1103 lua_pushnil(L);
jbe@108 1104 lua_pushstring(L, errmsg);
jbe@108 1105 return 2;
jbe@108 1106 }
jbe@108 1107 listener->fd = -1;
jbe@118 1108 if (addrlen && addrlen <= sizeof(addr)) {
jbe@118 1109 if (stat(addr.sun_path, &sb) == 0) {
jbe@118 1110 if (S_ISSOCK(sb.st_mode)) unlink(addr.sun_path);
jbe@118 1111 }
jbe@118 1112 }
jbe@108 1113 lua_pushboolean(L, 1);
jbe@108 1114 return 1;
jbe@108 1115 }
jbe@108 1116
jbe@108 1117 static int moonbr_io_listenergc(lua_State *L) {
jbe@108 1118 moonbr_io_listener_t *listener;
jbe@108 1119 listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@117 1120 if (listener->fd >= 0) close(listener->fd);
jbe@108 1121 listener->fd = -1;
jbe@108 1122 return 0;
jbe@108 1123 }
jbe@108 1124
jbe@106 1125 static int moonbr_io_poll(lua_State *L) {
jbe@106 1126 moonbr_io_handle_t *handle;
jbe@108 1127 moonbr_io_listener_t *listener;
jbe@106 1128 int fd, isnum;
jbe@106 1129 int nfds = 0;
jbe@106 1130 fd_set readfds, writefds, exceptfds;
jbe@106 1131 struct timeval timeout = {0, };
jbe@106 1132 int status;
jbe@106 1133 FD_ZERO(&readfds);
jbe@106 1134 FD_ZERO(&writefds);
jbe@106 1135 FD_ZERO(&exceptfds);
jbe@106 1136 if (!lua_isnoneornil(L, 1)) {
jbe@106 1137 luaL_checktype(L, 1, LUA_TTABLE);
jbe@106 1138 for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) {
jbe@106 1139 if (lua_toboolean(L, -1)) {
jbe@106 1140 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1141 if (handle) {
jbe@109 1142 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@106 1143 fd = handle->fd;
jbe@122 1144 if (
jbe@122 1145 fd < 0 || /* fake EOF to simulate shutdown if fd < 0 */
jbe@122 1146 handle->readbufin != handle->readbufout /* data pending in buffer */
jbe@122 1147 ) {
jbe@109 1148 lua_pushboolean(L, 1);
jbe@109 1149 return 1;
jbe@109 1150 }
jbe@106 1151 } else {
jbe@108 1152 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1153 if (listener) {
jbe@108 1154 fd = listener->fd;
jbe@108 1155 if (fd < 0) luaL_error(L, "Attempt to poll a closed listener");
jbe@108 1156 } else {
jbe@108 1157 fd = lua_tointegerx(L, -2, &isnum);
jbe@108 1158 if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key");
jbe@108 1159 }
jbe@106 1160 }
jbe@138 1161 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1162 FD_SET(fd, &readfds);
jbe@106 1163 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1164 }
jbe@106 1165 }
jbe@106 1166 }
jbe@106 1167 if (!lua_isnoneornil(L, 2)) {
jbe@106 1168 luaL_checktype(L, 2, LUA_TTABLE);
jbe@106 1169 for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
jbe@106 1170 if (lua_toboolean(L, -1)) {
jbe@106 1171 handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@106 1172 if (handle) {
jbe@109 1173 if (handle->closed) luaL_error(L, "Attempt to poll a closed connection");
jbe@109 1174 if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection");
jbe@106 1175 fd = handle->fd;
jbe@106 1176 } else {
jbe@108 1177 listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@109 1178 if (listener) luaL_error(L, "Attempt to write-poll a listener");
jbe@109 1179 fd = lua_tointegerx(L, -2, &isnum);
jbe@109 1180 if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key");
jbe@106 1181 }
jbe@138 1182 if (fd < 0 || fd >= FD_SETSIZE) luaL_error(L, "File descriptor out of valid range");
jbe@106 1183 FD_SET(fd, &writefds);
jbe@106 1184 if (fd+1 > nfds) nfds = fd+1;
jbe@106 1185 }
jbe@106 1186 }
jbe@106 1187 }
jbe@106 1188 if (!lua_isnoneornil(L, 3)) {
jbe@106 1189 lua_Number n;
jbe@106 1190 n = lua_tonumberx(L, 3, &isnum);
jbe@106 1191 if (isnum && n>=0 && n<100000000) {
jbe@106 1192 timeout.tv_sec = n;
jbe@106 1193 timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
jbe@106 1194 } else {
jbe@106 1195 luaL_argcheck(L, 0, 3, "not a valid timeout");
jbe@106 1196 }
jbe@106 1197 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
jbe@106 1198 } else {
jbe@106 1199 status = select(nfds, &readfds, &writefds, &exceptfds, NULL);
jbe@106 1200 }
jbe@106 1201 if (status == -1) {
jbe@106 1202 if (errno == EINTR) {
jbe@106 1203 lua_pushboolean(L, 0);
jbe@106 1204 lua_pushliteral(L, "Signal received while polling file descriptors");
jbe@106 1205 return 2;
jbe@106 1206 } else {
jbe@106 1207 moonbr_io_errmsg();
jbe@106 1208 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
jbe@106 1209 }
jbe@106 1210 } else if (status == 0) {
jbe@106 1211 lua_pushboolean(L, 0);
jbe@106 1212 lua_pushliteral(L, "Timeout while polling file descriptors");
jbe@106 1213 return 2;
jbe@106 1214 } else {
jbe@106 1215 lua_pushboolean(L, 1);
jbe@106 1216 return 1;
jbe@106 1217 }
jbe@106 1218 }
jbe@106 1219
jbe@115 1220 static int moonbr_io_timeref(lua_State *L) {
jbe@115 1221 lua_Number sub;
jbe@115 1222 struct timespec tp;
jbe@115 1223 sub = luaL_optnumber(L, 1, 0);
jbe@115 1224 if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
jbe@115 1225 return luaL_error(L, "Could not access CLOCK_MONOTONIC");
jbe@115 1226 }
jbe@115 1227 lua_pushnumber(L, tp.tv_sec + tp.tv_nsec / 1.0e9 - sub);
jbe@115 1228 return 1;
jbe@115 1229 }
jbe@115 1230
jbe@79 1231 static const struct luaL_Reg moonbr_io_handle_methods[] = {
jbe@85 1232 {"read", moonbr_io_read},
jbe@85 1233 {"read_nb", moonbr_io_read_nb},
jbe@140 1234 {"read_call", moonbr_io_read_call},
jbe@140 1235 {"read_yield", moonbr_io_read_yield},
jbe@86 1236 {"drain", moonbr_io_drain},
jbe@86 1237 {"drain_nb", moonbr_io_drain_nb},
jbe@80 1238 {"write", moonbr_io_write},
jbe@81 1239 {"write_nb", moonbr_io_write_nb},
jbe@80 1240 {"flush", moonbr_io_flush},
jbe@81 1241 {"flush_nb", moonbr_io_flush_nb},
jbe@88 1242 {"finish", moonbr_io_finish},
jbe@87 1243 {"close", moonbr_io_close},
jbe@85 1244 {"reset", moonbr_io_reset},
jbe@79 1245 {NULL, NULL}
jbe@79 1246 };
jbe@79 1247
jbe@79 1248 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
jbe@79 1249 {"__index", moonbr_io_handleindex},
jbe@79 1250 {"__newindex", moonbr_io_handlenewindex},
jbe@108 1251 {"__gc", moonbr_io_handlegc},
jbe@108 1252 {NULL, NULL}
jbe@108 1253 };
jbe@108 1254
jbe@108 1255 static const struct luaL_Reg moonbr_io_listener_methods[] = {
jbe@108 1256 {"accept", moonbr_io_accept},
jbe@108 1257 {"accept_nb", moonbr_io_accept_nb},
jbe@108 1258 {"close", moonbr_io_unlisten},
jbe@108 1259 {NULL, NULL}
jbe@108 1260 };
jbe@108 1261
jbe@108 1262 static const struct luaL_Reg moonbr_io_listener_metamethods[] = {
jbe@108 1263 {"__gc", moonbr_io_listenergc},
jbe@79 1264 {NULL, NULL}
jbe@79 1265 };
jbe@79 1266
jbe@79 1267 static const struct luaL_Reg moonbr_io_module_funcs[] = {
jbe@111 1268 {"localconnect", moonbr_io_localconnect},
jbe@111 1269 {"localconnect_nb", moonbr_io_localconnect_nb},
jbe@98 1270 {"tcpconnect", moonbr_io_tcpconnect},
jbe@99 1271 {"tcpconnect_nb", moonbr_io_tcpconnect_nb},
jbe@112 1272 {"locallisten", moonbr_io_locallisten},
jbe@108 1273 {"tcplisten", moonbr_io_tcplisten},
jbe@106 1274 {"poll", moonbr_io_poll},
jbe@115 1275 {"timeref", moonbr_io_timeref},
jbe@79 1276 {NULL, NULL}
jbe@79 1277 };
jbe@79 1278
jbe@79 1279 int luaopen_moonbridge_io(lua_State *L) {
jbe@79 1280
jbe@113 1281 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
jbe@113 1282
jbe@80 1283 lua_newtable(L); // module
jbe@80 1284
jbe@79 1285 lua_newtable(L); // public metatable
jbe@79 1286 lua_newtable(L); // handle methods
jbe@79 1287 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
jbe@80 1288 lua_pushvalue(L, -1);
jbe@108 1289 lua_setfield(L, -4, "prototype_handle");
jbe@79 1290 lua_setfield(L, -2, "__index");
jbe@79 1291 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
jbe@79 1292
jbe@79 1293 lua_newtable(L); // handle metatable
jbe@79 1294 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
jbe@79 1295 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
jbe@79 1296
jbe@108 1297 lua_newtable(L); // listener metatable
jbe@108 1298 luaL_setfuncs(L, moonbr_io_listener_metamethods, 0);
jbe@108 1299 lua_newtable(L); // listener methods
jbe@108 1300 luaL_setfuncs(L, moonbr_io_listener_methods, 0);
jbe@108 1301 lua_pushvalue(L, -1);
jbe@108 1302 lua_setfield(L, -4, "prototype_listener");
jbe@108 1303 lua_setfield(L, -2, "__index");
jbe@108 1304 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
jbe@108 1305
jbe@79 1306 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
jbe@79 1307 return 1;
jbe@79 1308
jbe@79 1309 }
jbe@79 1310

Impressum / About Us