diff options
| author | 2023-12-27 12:54:38 +0100 | |
|---|---|---|
| committer | 2023-12-27 12:54:38 +0100 | |
| commit | bf435af788d387b3d97fd744e3b1f6a73795beb8 (patch) | |
| tree | 6d193edd88ef3818bffd9278ddab0248e1108ef3 /xs_socket.h | |
| parent | Also log the job_fifo len in status.txt. (diff) | |
| download | penes-snac2-bf435af788d387b3d97fd744e3b1f6a73795beb8.tar.gz penes-snac2-bf435af788d387b3d97fd744e3b1f6a73795beb8.tar.xz penes-snac2-bf435af788d387b3d97fd744e3b1f6a73795beb8.zip | |
Backport from xs.
Diffstat (limited to 'xs_socket.h')
| -rw-r--r-- | xs_socket.h | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/xs_socket.h b/xs_socket.h index eea2f2d..ab67a6b 100644 --- a/xs_socket.h +++ b/xs_socket.h | |||
| @@ -7,9 +7,13 @@ | |||
| 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, const char *serv); | 8 | int xs_socket_server(const char *addr, const char *serv); |
| 9 | FILE *xs_socket_accept(int rs); | 9 | FILE *xs_socket_accept(int rs); |
| 10 | xs_str *xs_socket_peername(int s); | 10 | int _xs_socket_peername(int s, char *buf, int buf_size); |
| 11 | int xs_socket_connect(const char *addr, const char *serv); | 11 | int xs_socket_connect(const char *addr, const char *serv); |
| 12 | 12 | ||
| 13 | #ifdef _XS_H | ||
| 14 | xs_str *xs_socket_peername(int s); | ||
| 15 | #endif | ||
| 16 | |||
| 13 | 17 | ||
| 14 | #ifdef XS_IMPLEMENTATION | 18 | #ifdef XS_IMPLEMENTATION |
| 15 | 19 | ||
| @@ -17,6 +21,9 @@ int xs_socket_connect(const char *addr, const char *serv); | |||
| 17 | #include <netdb.h> | 21 | #include <netdb.h> |
| 18 | #include <netinet/in.h> | 22 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> | 23 | #include <arpa/inet.h> |
| 24 | #include <string.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <unistd.h> | ||
| 20 | 27 | ||
| 21 | 28 | ||
| 22 | int xs_socket_timeout(int s, double rto, double sto) | 29 | int xs_socket_timeout(int s, double rto, double sto) |
| @@ -100,34 +107,28 @@ FILE *xs_socket_accept(int rs) | |||
| 100 | } | 107 | } |
| 101 | 108 | ||
| 102 | 109 | ||
| 103 | xs_str *xs_socket_peername(int s) | 110 | int _xs_socket_peername(int s, char *buf, int buf_size) |
| 104 | /* returns the remote address as a string */ | 111 | /* fill the buffer with the socket peername */ |
| 105 | { | 112 | { |
| 106 | xs_str *ip = NULL; | ||
| 107 | struct sockaddr_storage addr; | 113 | struct sockaddr_storage addr; |
| 108 | socklen_t slen = sizeof(addr); | 114 | socklen_t slen = sizeof(addr); |
| 115 | const char *p = NULL; | ||
| 109 | 116 | ||
| 110 | if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) { | 117 | if (getpeername(s, (struct sockaddr *)&addr, &slen) != -1) { |
| 111 | char buf[1024]; | ||
| 112 | const char *p = NULL; | ||
| 113 | |||
| 114 | if (addr.ss_family == AF_INET) { | 118 | if (addr.ss_family == AF_INET) { |
| 115 | struct sockaddr_in *sa = (struct sockaddr_in *)&addr; | 119 | struct sockaddr_in *sa = (struct sockaddr_in *)&addr; |
| 116 | 120 | ||
| 117 | p = inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof(buf)); | 121 | p = inet_ntop(AF_INET, &sa->sin_addr, buf, buf_size); |
| 118 | } | 122 | } |
| 119 | else | 123 | else |
| 120 | if (addr.ss_family == AF_INET6) { | 124 | if (addr.ss_family == AF_INET6) { |
| 121 | struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr; | 125 | struct sockaddr_in6 *sa = (struct sockaddr_in6 *)&addr; |
| 122 | 126 | ||
| 123 | p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, sizeof(buf)); | 127 | p = inet_ntop(AF_INET6, &sa->sin6_addr, buf, buf_size); |
| 124 | } | 128 | } |
| 125 | |||
| 126 | if (p != NULL) | ||
| 127 | ip = xs_str_new(p); | ||
| 128 | } | 129 | } |
| 129 | 130 | ||
| 130 | return ip; | 131 | return p != NULL; |
| 131 | } | 132 | } |
| 132 | 133 | ||
| 133 | 134 | ||
| @@ -195,6 +196,22 @@ int xs_socket_connect(const char *addr, const char *serv) | |||
| 195 | } | 196 | } |
| 196 | 197 | ||
| 197 | 198 | ||
| 199 | #ifdef _XS_H | ||
| 200 | |||
| 201 | xs_str *xs_socket_peername(int s) | ||
| 202 | /* returns the remote address as a string */ | ||
| 203 | { | ||
| 204 | char buf[2028]; | ||
| 205 | xs_str *p = NULL; | ||
| 206 | |||
| 207 | if (_xs_socket_peername(s, buf, sizeof(buf))) | ||
| 208 | p = xs_str_new(buf); | ||
| 209 | |||
| 210 | return p; | ||
| 211 | } | ||
| 212 | |||
| 213 | #endif /* _XS_H */ | ||
| 214 | |||
| 198 | #endif /* XS_IMPLEMENTATION */ | 215 | #endif /* XS_IMPLEMENTATION */ |
| 199 | 216 | ||
| 200 | #endif /* _XS_SOCKET_H */ | 217 | #endif /* _XS_SOCKET_H */ |