# HG changeset patch # User jbe # Date 1428458441 -7200 # Node ID acaa85256c4ba0cab4b38034a110b6147e01e63c # Parent 0561abcc68ee95c29167b2926f3a651e8a5b7371 Added moonbridge_io.tcpconnect(...) function diff -r 0561abcc68ee -r acaa85256c4b moonbridge_io.c --- a/moonbridge_io.c Wed Apr 08 01:35:06 2015 +0200 +++ b/moonbridge_io.c Wed Apr 08 04:00:41 2015 +0200 @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -482,6 +484,54 @@ return 0; } +int moonbr_io_tcpconnect(lua_State *L) { + const char *host, *port; + struct addrinfo hints = { 0, }; + struct addrinfo *res, *addrinfo; + int errcode; + int sock; + host = luaL_checkstring(L, 1); + port = luaL_checkstring(L, 2); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_ADDRCONFIG; + errcode = getaddrinfo(host, port, &hints, &res); + if (errcode) { + if (errcode == EAI_SYSTEM) { + moonbr_io_errmsg(); + lua_pushnil(L); + lua_pushfstring(L, "%s: %s", gai_strerror(errcode), errmsg); + } else { + lua_pushnil(L); + lua_pushstring(L, gai_strerror(errcode)); + } + return 2; + } + for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) { + if (addrinfo->ai_family == PF_INET6) goto moonbr_io_tcpconnect_found; + } + for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) { + if (addrinfo->ai_family == PF_INET) goto moonbr_io_tcpconnect_found; + } + addrinfo = res; + moonbr_io_tcpconnect_found: + sock = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol); + if (sock < 0) { + moonbr_io_errmsg(); + lua_pushnil(L); + lua_pushstring(L, errmsg); + } + if (connect(sock, addrinfo->ai_addr, addrinfo->ai_addrlen)) { + moonbr_io_errmsg(); + lua_pushnil(L); + lua_pushstring(L, errmsg); + return 2; + } + moonbr_io_pushhandle(L, sock, 1); + return 1; +} + static const struct luaL_Reg moonbr_io_handle_methods[] = { {"read", moonbr_io_read}, {"read_nb", moonbr_io_read_nb}, @@ -505,6 +555,7 @@ }; static const struct luaL_Reg moonbr_io_module_funcs[] = { + {"tcpconnect", moonbr_io_tcpconnect}, {NULL, NULL} }; diff -r 0561abcc68ee -r acaa85256c4b moonbridge_io.h --- a/moonbridge_io.h Wed Apr 08 01:35:06 2015 +0200 +++ b/moonbridge_io.h Wed Apr 08 04:00:41 2015 +0200 @@ -1,5 +1,6 @@ void moonbr_io_pushhandle(lua_State *L, int fd, int useshutdown); void moonbr_io_closehandle(lua_State *L, int idx, int timeout); +int moonbr_io_tcpconnect(lua_State *L); int luaopen_moonbridge_io(lua_State *L); diff -r 0561abcc68ee -r acaa85256c4b reference.txt --- a/reference.txt Wed Apr 08 01:35:06 2015 +0200 +++ b/reference.txt Wed Apr 08 04:00:41 2015 +0200 @@ -229,6 +229,21 @@ +I/O library +----------- + +The Moonbridge Network Server for Lua Applications comes with its own I/O +library to support blocking as well as nonblocking I/O operations. + + +### moonbridge_io.tcpconnect(hostname, port) + +Tries to open a TCP connection with the given host and TCP port number. Returns +a socket object on success (methods see above), or nil (as first return value) +plus an error message (as second return value) in case of error. + + + HTTP module -----------