summaryrefslogtreecommitdiff
path: root/xs_socket.h
diff options
context:
space:
mode:
authorGravatar default2023-03-08 04:28:20 +0100
committerGravatar default2023-03-08 04:28:20 +0100
commit278575c2aab3733d90eb6019d25182f5e442021c (patch)
tree357475cbc5f3fdda798ead2f11053fca1ded2069 /xs_socket.h
parentShow Unicode symbols for replaced control codes. (diff)
downloadsnac2-278575c2aab3733d90eb6019d25182f5e442021c.tar.gz
snac2-278575c2aab3733d90eb6019d25182f5e442021c.tar.xz
snac2-278575c2aab3733d90eb6019d25182f5e442021c.zip
Backport from xs.
Diffstat (limited to 'xs_socket.h')
-rw-r--r--xs_socket.h34
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 @@
7int xs_socket_timeout(int s, double rto, double sto); 7int xs_socket_timeout(int s, double rto, double sto);
8int xs_socket_server(const char *addr, int port); 8int xs_socket_server(const char *addr, int port);
9FILE *xs_socket_accept(int rs); 9FILE *xs_socket_accept(int rs);
10xs_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
19int xs_socket_timeout(int s, double rto, double sto) 21int 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
96xs_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