summaryrefslogtreecommitdiff
path: root/src/l_tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/l_tcp.c')
-rw-r--r--src/l_tcp.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/l_tcp.c b/src/l_tcp.c
index 9418ea5..d96742c 100644
--- a/src/l_tcp.c
+++ b/src/l_tcp.c
@@ -30,13 +30,15 @@
* along with gcsd. If not, see <http://www.gnu.org/licenses/>.
*/
+#define _GNU_SOURCE
+#include <stdio.h>
+
#include <lua.h>
#include <lauxlib.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
-#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
@@ -59,7 +61,7 @@ typedef struct {
static tcp_endpoint_t* newtcpendudata(lua_State *L)
{
tcp_endpoint_t* end = (tcp_endpoint_t*)lua_newuserdata(L, sizeof(tcp_endpoint_t));
- memset(end, 0, sizeof(end));
+ memset(end, 0, sizeof(*end));
luaL_newmetatable(L, LUA_TCP_UDATA_NAME);
lua_setmetatable(L, -2);
return end;
@@ -67,7 +69,6 @@ static tcp_endpoint_t* newtcpendudata(lua_State *L)
static char* tcp_endpoint_to_string(tcp_endpoint_t* e)
{
- size_t addrstr_len = 0;
char addrstr[INET6_ADDRSTRLEN + 1], portstr[6], *ret;
char addrport_sep = ':';
@@ -76,12 +77,13 @@ static char* tcp_endpoint_to_string(tcp_endpoint_t* e)
case AF_INET: addrport_sep = ':'; break;
case AF_INET6: addrport_sep = '.'; break;
case AF_UNSPEC: return NULL;
- default: asprintf(&ret, "unknown address type"); return ret;
+ default: return strdup("unknown address type");
}
int errcode = getnameinfo((struct sockaddr *)&(e->addr_), e->len_, addrstr, sizeof(addrstr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV);
if (errcode != 0) return NULL;
- asprintf(&ret, "%s%c%s", addrstr, addrport_sep ,portstr);
+ int len = asprintf(&ret, "%s%c%s", addrstr, addrport_sep ,portstr);
+ if(len == -1) return NULL;
return ret;
}
@@ -325,7 +327,7 @@ static int l_tcp_connect(lua_State *L)
{
int fd = luaL_checkint(L, 1);
int error = 0;
- int len = sizeof(error);
+ socklen_t len = sizeof(error);
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)==-1) {
log_printf(ERROR, "error on getsockopt(): %s", strerror(errno));
lua_pushnil(L);