diff options
-rw-r--r-- | hbeat.c | 16 |
1 files changed, 15 insertions, 1 deletions
@@ -27,6 +27,8 @@ #include <sys/types.h> #include <time.h> #include <stdio.h> +#include <netdb.h> +#include <arpa/inet.h> #include "btime.h" #include "hbeat.h" @@ -54,13 +56,25 @@ hbeat_t hbeat_init(const char *host, int max_timeout) { struct hbeat_s *hbeatp; + struct hostent *hostent; + struct in_addr tmpaddr; hbeatp = malloc(sizeof *hbeatp); if (!hbeatp) { return NULL; } - hbeatp->host = strdup(host); + /* Store the dotted quad instead of the hostname so we + * don't have to look it up ever time we hbeat. */ + if (inet_addr(host) == INADDR_NONE) { + if ((hostent = gethostbyname(host)) == NULL) { + return NULL; + } + memcpy(&tmpaddr, hostent->h_addr, hostent->h_length); + hbeatp->host = strdup(inet_ntoa(tmpaddr)); + } else { + hbeatp->host = strdup(host); + } hbeatp->max_timeout = max_timeout; hbeatp->rhost_state = HOST_ALIVE; hbeatp->last_rhost_btime = 0; |