moonbridge

annotate moonbridge_io.c @ 112:e99265c3372d

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

Impressum / About Us