1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

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).
This commit is contained in:
llmII 2021-09-06 17:35:49 -05:00
parent 02640812af
commit 65a6945ea5
No known key found for this signature in database
GPG Key ID: E3AD2E259F58A9A0

View File

@ -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,