From 278575c2aab3733d90eb6019d25182f5e442021c Mon Sep 17 00:00:00 2001 From: default Date: Wed, 8 Mar 2023 04:28:20 +0100 Subject: [PATCH] Backport from xs. --- xs_socket.h | 34 ++++++++++++++++++++++++++++++++++ xs_version.h | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/xs_socket.h b/xs_socket.h index 24b97f1..870a7ec 100644 --- a/xs_socket.h +++ b/xs_socket.h @@ -7,6 +7,7 @@ int xs_socket_timeout(int s, double rto, double sto); int xs_socket_server(const char *addr, int port); FILE *xs_socket_accept(int rs); +xs_str *xs_socket_peername(int s); #ifdef XS_IMPLEMENTATION @@ -14,6 +15,7 @@ FILE *xs_socket_accept(int rs); #include #include #include +#include int xs_socket_timeout(int s, double rto, double sto) @@ -90,6 +92,38 @@ FILE *xs_socket_accept(int rs) return cs == -1 ? NULL : fdopen(cs, "r+"); } + +xs_str *xs_socket_peername(int s) +/* returns the remote address as a string */ +{ + xs_str *ip = NULL; + struct sockaddr_storage addr; + socklen_t slen = sizeof(addr); + + if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) { + char buf[1024]; + const char *p = NULL; + + if (addr.ss_family == AF_INET) { + struct sockaddr_in *sa = (struct sockaddr_in *)&addr; + + p = inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof(buf)); + } + else + if (addr.ss_family == AF_INET6) { + struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr; + + p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, sizeof(buf)); + } + + if (p != NULL) + ip = xs_str_new(p); + } + + return ip; +} + + #endif /* XS_IMPLEMENTATION */ #endif /* _XS_SOCKET_H */ \ No newline at end of file diff --git a/xs_version.h b/xs_version.h index 5aecdbd..4338f70 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 576198d1ed0f3309a9d09cb674c7ce1bf5c12737 */ +/* ee3466736d01d22d67ec800dbd4d3b0ccc4251b1 */