diff --git a/src/libckpool.c b/src/libckpool.c index aa2ab7c4..913f8d3e 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -451,6 +451,23 @@ bool addrinfo_from_url(const char *url, const char *port, struct addrinfo *addri return true; } +/* Convert a socket into a url and port. URL should be a string of + * INET6_ADDRSTRLEN size, port at least a string of 6 bytes */ +bool url_from_socket(const int sockd, char *url, char *port) +{ + socklen_t addrlen = sizeof(struct sockaddr); + struct sockaddr addr; + + if (sockd < 1) + return false; + if (getsockname(sockd, &addr, &addrlen)) + return false; + if (!url_from_sockaddr(&addr, url, port)) + return false; + return true; +} + + void keep_sockalive(int fd) { const int tcp_one = 1; diff --git a/src/libckpool.h b/src/libckpool.h index 26994aa7..9b1b3a98 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -425,6 +425,8 @@ static inline bool sock_timeout(void) bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port); bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port); bool addrinfo_from_url(const char *url, const char *port, struct addrinfo *addrinfo); +bool url_from_socket(const int sockd, char *url, char *port); + void keep_sockalive(int fd); void nolinger_socket(int fd); void noblock_socket(int fd);