diff options
author | default <nobody@localhost> | 2023-03-08 04:28:20 +0100 |
---|---|---|
committer | default <nobody@localhost> | 2023-03-08 04:28:20 +0100 |
commit | 278575c2aab3733d90eb6019d25182f5e442021c (patch) | |
tree | 357475cbc5f3fdda798ead2f11053fca1ded2069 /xs_socket.h | |
parent | 158bc127a35ef48e8a3c1dc510aababb8abdb676 (diff) |
Backport from xs.
Diffstat (limited to 'xs_socket.h')
-rw-r--r-- | xs_socket.h | 34 |
1 files changed, 34 insertions, 0 deletions
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 <sys/socket.h> #include <netdb.h> #include <netinet/in.h> +#include <arpa/inet.h> 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 |