From 3358797987483f861c46fdccc7f57869dabc26de Mon Sep 17 00:00:00 2001 From: Nate Straz Date: Mon, 7 Apr 2008 15:57:00 +0000 Subject: Try to reduce the number of times we look up a hostname by storing the address in the hbeat struct instead of the name. --- hbeat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hbeat.c b/hbeat.c index ab0fb33..46b8ac1 100644 --- a/hbeat.c +++ b/hbeat.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #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; -- cgit