From 65a6945ea5944c13070ed804ac735bb10f7a9f70 Mon Sep 17 00:00:00 2001 From: llmII Date: Mon, 6 Sep 2021 17:35:49 -0500 Subject: [PATCH] Finalize peername and localname Both now do the right thing and give back all information (host and port) when possible as a tuple of (host port). --- src/core/net.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/core/net.c b/src/core/net.c index a1d0f08a..7f367837 100644 --- a/src/core/net.c +++ b/src/core/net.c @@ -720,7 +720,7 @@ static inline void *sa_aton_(void *dst, size_t lim, const char *src) { } */ -static JanetString janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen) { +static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen) { uint8_t *hn = NULL; uint16_t hp = 0; size_t plen = SA_ADDRSTRLEN; @@ -760,7 +760,15 @@ static JanetString janet_so_getname(const struct sockaddr_storage *ss, socklen_t break; } - return janet_string(hn, plen + 1); + Janet name[2]; + int32_t len = 1; + name[0] = janet_wrap_string(janet_cstring((const char *)hn)); + if (hp > 0) { + len++; + name[1] = janet_wrap_integer(hp); + } + + return janet_wrap_tuple(janet_tuple_n(name, len)); } JANET_CORE_FN(cfun_net_getsockname, @@ -776,7 +784,7 @@ JANET_CORE_FN(cfun_net_getsockname, if(0 != (error = getsockname(js->handle, (struct sockaddr *) &ss, &slen))) janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr()); - return janet_wrap_string(janet_so_getname(&ss, slen)); + return janet_so_getname(&ss, slen); } @@ -794,7 +802,7 @@ JANET_CORE_FN(cfun_net_getpeername, janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr()); } - return janet_wrap_string(janet_so_getname(&ss, slen)); + return janet_so_getname(&ss, slen); } JANET_CORE_FN(cfun_stream_accept_loop,