From 92cbb3408551a7616d2c02f07d524e34fbdd843f Mon Sep 17 00:00:00 2001 From: Assar Westerlund Date: Sun, 18 Aug 1996 23:23:50 +0000 Subject: [PATCH] New function `inaddr2str' to convert an IP address into a verified hostname or a string of the form x.y.z.a git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@656 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/roken/inaddr2str.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/roken/roken.h | 2 ++ lib/roken/roken.h.in | 2 ++ 3 files changed, 46 insertions(+) create mode 100644 lib/roken/inaddr2str.c diff --git a/lib/roken/inaddr2str.c b/lib/roken/inaddr2str.c new file mode 100644 index 000000000..39f0054bd --- /dev/null +++ b/lib/roken/inaddr2str.c @@ -0,0 +1,42 @@ +#ifdef HAVE_CONFIG_H +#include +RCSID("$Id$"); +#endif + +#include +#include +#include +#include +#include +#ifdef HAVE_ARPA_INET_H +#include +#endif +#include +#include "roken.h" + +/* + * Get a verified name for `addr'. + * If unable to find it in the DNS, return x.y.z.a + */ + +void +inaddr2str(struct in_addr addr, char *s, size_t len) +{ + struct hostent *h; + char *p; + + h = gethostbyaddr ((const char *)&addr, sizeof(addr), AF_INET); + if (h) { + h = gethostbyname (h->h_name); + if(h) + while ((p = *(h->h_addr_list)++)) + if (memcmp (p, &addr, h->h_length) == 0) { + strncpy (s, h->h_name, len); + s[len - 1] = '\0'; + return; + } + } + strncpy (s, inet_ntoa (addr), len); + s[len - 1] = '\0'; + return; +} diff --git a/lib/roken/roken.h b/lib/roken/roken.h index 2b338bcbc..1bd3fc854 100644 --- a/lib/roken/roken.h +++ b/lib/roken/roken.h @@ -79,4 +79,6 @@ time_t tm2time (struct tm tm, int local); int verify_unix_user(char *user, char *password); +void inaddr2str(struct in_addr addr, char *s, size_t len); + #endif /* __ROKEN_H__ */ diff --git a/lib/roken/roken.h.in b/lib/roken/roken.h.in index 2b338bcbc..1bd3fc854 100644 --- a/lib/roken/roken.h.in +++ b/lib/roken/roken.h.in @@ -79,4 +79,6 @@ time_t tm2time (struct tm tm, int local); int verify_unix_user(char *user, char *password); +void inaddr2str(struct in_addr addr, char *s, size_t len); + #endif /* __ROKEN_H__ */