diff options
Diffstat (limited to 'src/l_tcp.c')
-rw-r--r-- | src/l_tcp.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/l_tcp.c b/src/l_tcp.c index da452d4..158a8ba 100644 --- a/src/l_tcp.c +++ b/src/l_tcp.c @@ -36,6 +36,7 @@ #include <errno.h> #include <string.h> #include <stdlib.h> +#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> @@ -85,14 +86,14 @@ static int init_listener(tcp_endpoint_t* end) { int fd = socket(end->addr_.ss_family, SOCK_STREAM, 0); if(fd < 0) { - log_printf(ERROR, "tcp: Error on opening socket: %s", strerror(errno)); + log_printf(ERROR, "tcp: error on opening socket: %s", strerror(errno)); return -1; } int on = 1; int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if(ret) { - log_printf(ERROR, "tcp: Error on setsockopt(): %s", strerror(errno)); + log_printf(ERROR, "tcp: error on setsockopt(): %s", strerror(errno)); return -1; } if(end->addr_.ss_family == AF_INET6) { @@ -103,7 +104,7 @@ static int init_listener(tcp_endpoint_t* end) char* ls = tcp_endpoint_to_string(end); ret = bind(fd, (struct sockaddr *)&(end->addr_), end->len_); if(ret) { - log_printf(ERROR, "tcp: Error on bind(%s): %s", ls ? ls:"", strerror(errno)); + log_printf(ERROR, "tcp: error on bind(%s): %s", ls ? ls:"", strerror(errno)); if(ls) free(ls); return -1; } @@ -111,7 +112,7 @@ static int init_listener(tcp_endpoint_t* end) ret = listen(fd, 0); if(ret) { - log_printf(ERROR, "tcp: Error on listen(): %s", strerror(errno)); + log_printf(ERROR, "tcp: error on listen(): %s", strerror(errno)); if(ls) free(ls); return -1; } @@ -138,14 +139,14 @@ static int l_tcp_server(lua_State *L) } int errcode = getaddrinfo(addr, port, &hints, &res); - // TODO: better error handling (no lua error) - if(errcode != 0) - luaL_error(L, "tcp: resolver error: %s", gai_strerror(errcode)); - if(!res) - luaL_error(L, "tcp: no address found!"); + if(errcode != 0 || !res) { + log_printf(ERROR, "tcp: resolver error: %s", errcode ? gai_strerror(errcode):"no address found!"); + lua_pushnil(L); + lua_pushstring(L, "error at tcp-server initialization"); + return 2; + } lua_newtable(L); - int idx = 1; struct addrinfo* r = res; while(r) { @@ -161,8 +162,9 @@ static int l_tcp_server(lua_State *L) int fd = init_listener(end); if(fd < 0) { freeaddrinfo(res); - // TODO: better error handling (no lua error) - luaL_error(L, "tcp: Error at server init"); + lua_pushnil(L); + lua_pushstring(L, "error at tcp-server initialization"); + return 2; } lua_pushliteral(L, "fd"); |