This patch adds a forward lookup of any hostnames listed in the
"hosts allow" or "hosts deny" daemon config options.  Based on
a patch by Paul Williamson.

To use this patch, run these commands for a successful build:

    patch -p1 <patches/daemon-forward-lookup.diff
    ./configure                         (optional if already run)
    make

diff --git a/access.c b/access.c
--- a/access.c
+++ b/access.c
@@ -210,6 +210,43 @@ static int match_address(char *addr, char *tok)
 	return ret;
 }
 
+static int match_hostlookup(char *addr, char *tok)
+{
+	struct hostent *hp;
+	unsigned int i, len;
+	int failed;
+	char *p;
+
+	if ((p = strchr(tok,'/')) != NULL) {
+		*p = '\0';
+		len = p - tok;
+	} else
+		len = strlen(tok);
+
+	/* Fail quietly if tok is an address (not a hostname) */
+	failed = strspn(tok, ".0123456789") == len;
+#ifdef INET6
+	failed ||= strchr(tok, ':') != NULL;
+#endif
+
+	if (!failed && (hp = gethostbyname(tok)) == NULL)
+		failed = 1;
+
+	if (p)
+		*p = '/';
+
+	if (failed)
+		return 0;
+
+	for (i = 0; hp->h_addr_list[i] != NULL; i++) {
+		tok = inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]));
+		if (match_address(addr, tok))
+			return 1;
+	}
+
+	return 0;
+}
+
 static int access_match(char *list, char *addr, char *host)
 {
 	char *tok;
@@ -223,7 +260,7 @@ static int access_match(char *list, char *addr, char *host)
 		strlower(host);
 
 	for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
-		if (match_hostname(host, tok) || match_address(addr, tok)) {
+		if (match_hostname(host, tok) || match_address(addr, tok) || match_hostlookup(addr, tok)) {
 			free(list2);
 			return 1;
 		}
