moonbridge

annotate moonbridge_io.c @ 113:113185a57b06

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

Impressum / About Us