summaryrefslogtreecommitdiffstats
path: root/libnetdata/socket
diff options
context:
space:
mode:
Diffstat (limited to 'libnetdata/socket')
-rw-r--r--libnetdata/socket/socket.c37
-rw-r--r--libnetdata/socket/socket.h1
2 files changed, 37 insertions, 1 deletions
diff --git a/libnetdata/socket/socket.c b/libnetdata/socket/socket.c
index e7d0b4807..67dc4c71c 100644
--- a/libnetdata/socket/socket.c
+++ b/libnetdata/socket/socket.c
@@ -10,6 +10,40 @@
#include "../libnetdata.h"
+bool ip_to_hostname(const char *ip, char *dst, size_t dst_len) {
+ if(!dst || !dst_len)
+ return false;
+
+ struct sockaddr_in sa;
+ struct sockaddr_in6 sa6;
+ struct sockaddr *sa_ptr;
+ int sa_len;
+
+ // Try to convert the IP address to sockaddr_in (IPv4)
+ if (inet_pton(AF_INET, ip, &(sa.sin_addr)) == 1) {
+ sa.sin_family = AF_INET;
+ sa_ptr = (struct sockaddr *)&sa;
+ sa_len = sizeof(sa);
+ }
+ // Try to convert the IP address to sockaddr_in6 (IPv6)
+ else if (inet_pton(AF_INET6, ip, &(sa6.sin6_addr)) == 1) {
+ sa6.sin6_family = AF_INET6;
+ sa_ptr = (struct sockaddr *)&sa6;
+ sa_len = sizeof(sa6);
+ }
+
+ else {
+ dst[0] = '\0';
+ return false;
+ }
+
+ // Perform the reverse lookup
+ int res = getnameinfo(sa_ptr, sa_len, dst, dst_len, NULL, 0, NI_NAMEREQD);
+ if(res != 0)
+ return false;
+
+ return true;
+}
SOCKET_PEERS socket_peers(int sock_fd) {
SOCKET_PEERS peers;
@@ -810,7 +844,7 @@ int connect_to_this_ip46(int protocol, int socktype, const char *host, uint32_t
errno = 0;
if(connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
if(errno == EALREADY || errno == EINPROGRESS) {
- netdata_log_info("Waiting for connection to ip %s port %s to be established", hostBfr, servBfr);
+ internal_error(true, "Waiting for connection to ip %s port %s to be established", hostBfr, servBfr);
// Convert 'struct timeval' to milliseconds for poll():
int timeout_milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
@@ -835,6 +869,7 @@ int connect_to_this_ip46(int protocol, int socktype, const char *host, uint32_t
}
else if (ret == 0) {
// poll() timed out, the connection is not established within the specified timeout.
+ errno = 0;
netdata_log_error("Timed out while connecting to '%s', port '%s'.", hostBfr, servBfr);
close(fd);
fd = -1;
diff --git a/libnetdata/socket/socket.h b/libnetdata/socket/socket.h
index c4bd47360..e4ca08d47 100644
--- a/libnetdata/socket/socket.h
+++ b/libnetdata/socket/socket.h
@@ -243,5 +243,6 @@ typedef struct socket_peers {
} SOCKET_PEERS;
SOCKET_PEERS socket_peers(int sock_fd);
+bool ip_to_hostname(const char *ip, char *dst, size_t dst_len);
#endif //NETDATA_SOCKET_H