diff options
author | neilbrown <neilbrown> | 2003-05-21 05:49:00 +0000 |
---|---|---|
committer | neilbrown <neilbrown> | 2003-05-21 05:49:00 +0000 |
commit | 77c32fa565d6af59cbeb8601e08efa9af6ef3d81 (patch) | |
tree | b998029eb2a4dfe05c7aaa16b3cfcaa27524b98c /support/export/hostname.c | |
parent | 36b091949d42e2dfe125343b19961aaeb67b2322 (diff) | |
download | nfs-utils-77c32fa565d6af59cbeb8601e08efa9af6ef3d81.tar.gz nfs-utils-77c32fa565d6af59cbeb8601e08efa9af6ef3d81.tar.xz nfs-utils-77c32fa565d6af59cbeb8601e08efa9af6ef3d81.zip |
Define and use get_reliable_hostbyname
Diffstat (limited to 'support/export/hostname.c')
-rw-r--r-- | support/export/hostname.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/support/export/hostname.c b/support/export/hostname.c index 299fe99..f88e0a4 100644 --- a/support/export/hostname.c +++ b/support/export/hostname.c @@ -16,6 +16,7 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> +#include <xlog.h> #ifdef TEST #define xmalloc malloc #else @@ -216,6 +217,57 @@ matchhostname (const char *h1, const char *h2) return status; } + +/* Map IP to hostname, and then map back to addr to make sure it is a + * reliable hostname + */ +struct hostent * +get_reliable_hostbyaddr(const char *addr, int len, int type) +{ + struct hostent *hp; + + char **sp; + struct hostent *forward = NULL; + char *tmpname; + + hp = gethostbyaddr(addr, len , type); + if (!hp) + return hp; + + /* must make sure the hostent is authorative. */ + + hp = hostent_dup (hp); + tmpname = xstrdup((hp)->h_name); + if (tmpname) { + forward = gethostbyname(tmpname); + free(tmpname); + } + if (forward) { + /* now make sure the "addr" is in the list */ + for (sp = forward->h_addr_list ; *sp ; sp++) { + if (memcmp(*sp, addr, forward->h_length)==0) + break; + } + + if (!*sp) { + /* it was a FAKE */ + xlog(L_WARNING, "Fake hostname %s for %s - forward lookup doesn't match reverse", + forward->h_name, inet_ntoa(*(struct in_addr*)addr)); + return NULL; + } + free (hp); + hp = hostent_dup (forward); + } + else { + /* never heard of it. misconfigured DNS? */ + xlog(L_WARNING, "Fake hostname %s for %s - forward lookup doesn't exist", + forward->h_name, inet_ntoa(*(struct in_addr*)addr)); + return NULL; + } + return hp; +} + + #ifdef TEST void print_host (struct hostent *hp) |