diff options
| author | 2023-03-08 04:28:20 +0100 | |
|---|---|---|
| committer | 2023-03-08 04:28:20 +0100 | |
| commit | 278575c2aab3733d90eb6019d25182f5e442021c (patch) | |
| tree | 357475cbc5f3fdda798ead2f11053fca1ded2069 /xs_socket.h | |
| parent | Show Unicode symbols for replaced control codes. (diff) | |
| download | penes-snac2-278575c2aab3733d90eb6019d25182f5e442021c.tar.gz penes-snac2-278575c2aab3733d90eb6019d25182f5e442021c.tar.xz penes-snac2-278575c2aab3733d90eb6019d25182f5e442021c.zip | |
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 @@ | |||
| 7 | int xs_socket_timeout(int s, double rto, double sto); | 7 | int xs_socket_timeout(int s, double rto, double sto); |
| 8 | int xs_socket_server(const char *addr, int port); | 8 | int xs_socket_server(const char *addr, int port); |
| 9 | FILE *xs_socket_accept(int rs); | 9 | FILE *xs_socket_accept(int rs); |
| 10 | xs_str *xs_socket_peername(int s); | ||
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | #ifdef XS_IMPLEMENTATION | 13 | #ifdef XS_IMPLEMENTATION |
| @@ -14,6 +15,7 @@ FILE *xs_socket_accept(int rs); | |||
| 14 | #include <sys/socket.h> | 15 | #include <sys/socket.h> |
| 15 | #include <netdb.h> | 16 | #include <netdb.h> |
| 16 | #include <netinet/in.h> | 17 | #include <netinet/in.h> |
| 18 | #include <arpa/inet.h> | ||
| 17 | 19 | ||
| 18 | 20 | ||
| 19 | int xs_socket_timeout(int s, double rto, double sto) | 21 | int xs_socket_timeout(int s, double rto, double sto) |
| @@ -90,6 +92,38 @@ FILE *xs_socket_accept(int rs) | |||
| 90 | return cs == -1 ? NULL : fdopen(cs, "r+"); | 92 | return cs == -1 ? NULL : fdopen(cs, "r+"); |
| 91 | } | 93 | } |
| 92 | 94 | ||
| 95 | |||
| 96 | xs_str *xs_socket_peername(int s) | ||
| 97 | /* returns the remote address as a string */ | ||
| 98 | { | ||
| 99 | xs_str *ip = NULL; | ||
| 100 | struct sockaddr_storage addr; | ||
| 101 | socklen_t slen = sizeof(addr); | ||
| 102 | |||
| 103 | if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) { | ||
| 104 | char buf[1024]; | ||
| 105 | const char *p = NULL; | ||
| 106 | |||
| 107 | if (addr.ss_family == AF_INET) { | ||
| 108 | struct sockaddr_in *sa = (struct sockaddr_in *)&addr; | ||
| 109 | |||
| 110 | p = inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof(buf)); | ||
| 111 | } | ||
| 112 | else | ||
| 113 | if (addr.ss_family == AF_INET6) { | ||
| 114 | struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr; | ||
| 115 | |||
| 116 | p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, sizeof(buf)); | ||
| 117 | } | ||
| 118 | |||
| 119 | if (p != NULL) | ||
| 120 | ip = xs_str_new(p); | ||
| 121 | } | ||
| 122 | |||
| 123 | return ip; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 93 | #endif /* XS_IMPLEMENTATION */ | 127 | #endif /* XS_IMPLEMENTATION */ |
| 94 | 128 | ||
| 95 | #endif /* _XS_SOCKET_H */ \ No newline at end of file | 129 | #endif /* _XS_SOCKET_H */ \ No newline at end of file |