void libnet_addr2name6_r(struct libnet_in6_addr addr, u_int8_t use_name, char *host_name, int host_name_len) { struct hostent *host_ent = NULL; if (use_name == LIBNET_RESOLVE) { #ifdef HAVE_SOLARIS #ifdef HAVE_SOLARIS_IPV6 host_ent = getipnodebyaddr((int8_t *)&addr, sizeof(struct in_addr), AF_INET6, NULL); #else /* XXX - Gah! Can't report error! */ host_ent = NULL; #endif #else host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr), AF_INET6); #endif } if (!host_ent) { #if !defined(__WIN32__) /* Silence Win32 warning */ inet_ntop(AF_INET6, &addr, host_name, host_name_len); #endif } else { strncpy(host_name, host_ent->h_name, host_name_len -1); host_name[sizeof(host_name) - 1] = '\0'; } }
char * SamrftGetHostByAddr( void *addr, int af) { struct hostent *host_ent; char *host_name = NULL; char buffer[32]; int h_err = TRUE; int size; switch (af) { case AF_INET: size = sizeof (struct in_addr); break; case AF_INET6: size = sizeof (struct in6_addr); break; } host_ent = getipnodebyaddr((char *)addr, size, af, &h_err); if (host_ent != NULL) { SamStrdup(host_name, host_ent->h_name); freehostent(host_ent); } else { Trace(TR_ERR, "Samrft af %d address %s not found", af, inet_ntop(af, (char *)addr, buffer, 32)); } return (host_name); }
static char * myGetHostByAddr (char *addr, int len, int type) { struct hostent *hostEnt; char *result; #if HAVE_GETIPNODEBYADDR int error; hostEnt = getipnodebyaddr (addr, len, type, &error); #else hostEnt = gethostbyaddr (addr, len, type); #endif if (hostEnt) { result = malloc (128); /* out of a hat */ strncpy (result, hostEnt->h_name, 128); #if HAVE_GETIPNODEBYADDR freehostent (hostEnt); #endif } else result = NULL; return (result); }
/* * smb_gethostbyaddr * * Looks up a host by the given IP address. The host entry can come * from any of the sources for hosts specified in the * /etc/nsswitch.conf and the NetBIOS cache. * * XXX Invokes nbt API to resolve name by IP once the NBTD is integrated * to look in the NetBIOS cache if getipnodebyaddr fails. * * Caller should invoke freehostent to free the returned hostent. */ struct hostent * smb_gethostbyaddr(const char *addr, int len, int type, int *err_num) { struct hostent *h; h = getipnodebyaddr(addr, len, type, err_num); return (h); }
char * one_addr(krb5_address *a) { static char retstr[256]; struct hostent *h; int no_resolve = 1; retstr[0] = '\0'; if ((a->addrtype == ADDRTYPE_INET && a->length == 4) #ifdef AF_INET6 || (a->addrtype == ADDRTYPE_INET6 && a->length == 16) #endif ) { int af = AF_INET; #ifdef AF_INET6 if (a->addrtype == ADDRTYPE_INET6) af = AF_INET6; #endif if (!no_resolve) { #ifdef HAVE_GETIPNODEBYADDR int err; h = getipnodebyaddr(a->contents, a->length, af, &err); if (h) { wsprintf(retstr, "%s", h->h_name); freehostent(h); } #else h = gethostbyaddr(a->contents, a->length, af); if (h) { wsprintf(retstr,"%s", h->h_name); } #endif if (h) return(retstr); } if (no_resolve || !h) { #ifdef HAVE_INET_NTOP char buf[46]; const char *name = inet_ntop(a->addrtype, a->contents, buf, sizeof(buf)); if (name) { wsprintf(retstr,"%s", name); return; } #else if (a->addrtype == ADDRTYPE_INET) { wsprintf(retstr,"%d.%d.%d.%d", a->contents[0], a->contents[1], a->contents[2], a->contents[3]); return(retstr); } #endif } } wsprintf(retstr,"unknown addr type %d", a->addrtype); return(retstr); }
void testValues() { f = 2; char buf[10]; int err; struct hostent* result = getipnodebyaddr(buf, 10, anyint(), &err); //@ assert result == \null || \valid_read(result->h_name); //@ assert f == 2; //@ assert vacuous: \false; }
static struct hostent * __gethostbyaddr(const void *addr, socklen_t len, int af) { struct hostent *he; int error; if (use_ipnode_functions) { error = 0; he = getipnodebyaddr(addr, len, af, &error); if (he == NULL) errno = error; } else he = gethostbyaddr(addr, len, af); return (he); }
struct hostent * tds_gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buffer, int buflen, int *h_errnop) { #if defined(NETDB_REENTRANT) return gethostbyaddr(addr, len, type); #elif defined(HAVE_GETIPNODEBYADDR) struct hostent *he = getipnodebyaddr(addr, len, type, h_errnop); if (!he) return NULL; if (tds_copy_hostent(he, result, buffer, buflen)) { errno = ENOMEM; if (h_errnop) *h_errnop = NETDB_INTERNAL; freehostent(he); return NULL; } freehostent(he); return result; #elif defined(HAVE_FUNC_GETHOSTBYADDR_R_8) if (gethostbyaddr_r(addr, len, type, result, buffer, buflen, &result, h_errnop)) return NULL; return result; #elif defined(HAVE_FUNC_GETHOSTBYADDR_R_7) result = gethostbyaddr_r(addr, len, type, result, buffer, buflen, h_errnop); return result; #elif defined(HAVE_FUNC_GETHOSTBYADDR_R_5) struct hostent_data *data = (struct hostent_data *) buffer; memset(buffer, 0, buflen); if (gethostbyaddr_r(addr, len, type, result, data)) { *h_errnop = 0; result = NULL; } return result; #elif defined(TDS_NO_THREADSAFE) return gethostbyaddr(addr, len, type); #else #error gethostbyaddr_r style unknown #endif }
static int add_hostent (int port, int protocol, int socktype, struct addrinfo ***current, int (*func)(struct addrinfo *, void *data, int port), struct hostent *he, int *flags) { int ret; char *canonname = NULL; char **h; if (*flags & AI_CANONNAME) { struct hostent *he2 = NULL; const char *tmp_canon; tmp_canon = hostent_find_fqdn (he); if (strchr (tmp_canon, '.') == NULL) { int error; he2 = getipnodebyaddr (he->h_addr_list[0], he->h_length, he->h_addrtype, &error); if (he2 != NULL) { const char *tmp = hostent_find_fqdn (he2); if (strchr (tmp, '.') != NULL) tmp_canon = tmp; } } canonname = strdup (tmp_canon); if (he2 != NULL) freehostent (he2); if (canonname == NULL) return EAI_MEMORY; } for (h = he->h_addr_list; *h != NULL; ++h) { ret = add_one (port, protocol, socktype, current, func, *h, canonname); if (ret) return ret; if (*flags & AI_CANONNAME) { *flags &= ~AI_CANONNAME; canonname = NULL; } } return 0; }
static void test_getipnodebyaddr(const char *address, int af, const char *name) { struct hostent *hp; char addrbuf[16]; int len, ret; int error_num; if (af == AF_INET) len = 4; else len = 16; ret = inet_pton(af, address, addrbuf); assert(ret == 1); hp = getipnodebyaddr(addrbuf, len, af, &error_num); if (hp == NULL) { if (name == NULL && error_num == HOST_NOT_FOUND) return; else if (error_num != HOST_NOT_FOUND) { printf("I:getipnodebyaddr(%s) failed: %d\n", address, error_num); fails++; return; } else { printf("I:getipnodebyaddr(%s) returned not found\n", address); fails++; return; } } else { if (name != NULL && strcmp(hp->h_name, name) != 0) { printf("I:getipnodebyaddr(%s) returned %s, " "expected %s\n", address, hp->h_name, name); freehostent(hp); fails++; return; } freehostent(hp); } }
void printaddress (krb5_address address) { int af; char buf[46]; const char *addr_string = NULL; switch (address.addrtype) { case ADDRTYPE_INET: af = AF_INET; break; case ADDRTYPE_INET6: af = AF_INET6; break; default: printmsg ("unknown address type %d", address.addrtype); return; } if (!no_reverse_resolve_addresses) { struct hostent *h = NULL; int err; h = getipnodebyaddr (address.contents, address.length, af, &err); if (h != NULL) { printmsg ("%s", h->h_name); freehostent (h); return; } } /* either we aren't resolving addresses or we failed to do so */ addr_string = inet_ntop(af, address.contents, buf, sizeof(buf)); if (addr_string != NULL) { printmsg ("%s", addr_string); } }
int main(int argc, char **argv) { struct hostent *he; int error; struct in_addr in_addr; struct in6_addr in6_addr; void *addr; int af; size_t len; (void)argc; while (argv[1] != NULL) { if (inet_pton(AF_INET, argv[1], &in_addr) == 1) { af = AF_INET; addr = &in_addr; len = sizeof(in_addr); } else if (inet_pton(AF_INET6, argv[1], &in6_addr) == 1) { af = AF_INET6; addr = &in6_addr; len = sizeof(in6_addr); } else { printf("unable to convert \"%s\" to an address\n", argv[1]); argv++; continue; } he = gethostbyaddr(addr, len, af); print_he(he, h_errno, "gethostbyaddr", argv[1]); he = getipnodebyaddr(addr, len, af, &error); print_he(he, error, "getipnodebyaddr", argv[1]); if (he != NULL) freehostent(he); argv++; } return (0); }
void runFailure1() { char buf[10]; int err; getipnodebyaddr(buf, 10, anyint(), NULL); }
struct hostent * getnodebyaddr(const void *src, size_t len, int af) { return getipnodebyaddr(src, len, af, &h_errno); }
void runFailure2() { char buf[10]; int err; getipnodebyaddr(buf, 20, anyint(), &err); }
void runSuccess() { char buf[10]; int err; getipnodebyaddr(buf, 10, anyint(), &err); }
/* * An instance name is formed using either the host name in the fully * qualified domain name form (FQDN) which should map to a specific IP address * or using INADDR_ANY which means all IP addresses. * * We do a lookup or reverse lookup to get the host name. It is assumed that * the returned name is in the FQDN form. i.e. DNS is used. */ char * create_instance_name(const char *arg, char **inaddr_any_name, boolean_t is_create) { int len; uint16_t port; char *cname; char *instance_name; const char *prefix = "kssl-"; char *first_space; first_space = strchr(arg, ' '); if (first_space == NULL) { /* No host name. Use INADDR_ANY. */ if (get_portnum(arg, &port) == 0) { (void) fprintf(stderr, gettext("Error: Invalid port value -- %s\n"), arg); return (NULL); } KSSL_DEBUG("port=%d\n", port); if ((cname = strdup(ANY_ADDR)) == NULL) return (NULL); } else { char *temp_str; char *ptr; struct hostent *hp; boolean_t do_warn; int error_num; in_addr_t v4addr; in6_addr_t v6addr; if (get_portnum(first_space + 1, &port) == 0) { (void) fprintf(stderr, gettext("Error: Invalid port value -- %s\n"), first_space + 1); return (NULL); } KSSL_DEBUG("port=%d\n", port); if ((temp_str = strdup(arg)) == NULL) return (NULL); *(strchr(temp_str, ' ')) = '\0'; if (inet_pton(AF_INET6, temp_str, &v6addr) == 1) { /* Do a reverse lookup for the IPv6 address */ hp = getipnodebyaddr(&v6addr, sizeof (v6addr), AF_INET6, &error_num); } else if (inet_pton(AF_INET, temp_str, &v4addr) == 1) { /* Do a reverse lookup for the IPv4 address */ hp = getipnodebyaddr(&v4addr, sizeof (v4addr), AF_INET, &error_num); } else { /* Do a lookup for the host name */ hp = getipnodebyname(temp_str, AF_INET6, AI_DEFAULT, &error_num); } if (hp == NULL) { (void) fprintf(stderr, gettext("Error: Unknown host -- %s\n"), temp_str); free(temp_str); return (NULL); } if ((ptr = cname = strdup(hp->h_name)) == NULL) { freehostent(hp); free(temp_str); return (NULL); } freehostent(hp); do_warn = B_TRUE; /* "s/./-/g" */ while ((ptr = strchr(ptr, '.')) != NULL) { if (do_warn) do_warn = B_FALSE; *ptr = '-'; ptr++; } if (do_warn && is_create) { (void) fprintf(stderr, gettext("Warning: %s does not appear to have a" " registered DNS name.\n"), temp_str); } free(temp_str); } KSSL_DEBUG("Cannonical host name =%s\n", cname); len = strlen(prefix) + strlen(cname) + 10; if ((instance_name = malloc(len)) == NULL) { (void) fprintf(stderr, gettext("Error: memory allocation failure.\n")); return (NULL); } (void) snprintf(instance_name, len, "%s%s-%d", prefix, cname, port); if (is_create) { len = strlen(prefix) + strlen(ANY_ADDR) + 10; if ((*inaddr_any_name = malloc(len)) == NULL) { (void) fprintf(stderr, gettext("Error: memory allocation failure.\n")); free(instance_name); free(cname); return (NULL); } (void) snprintf(*inaddr_any_name, len, "%s%s-%d", prefix, ANY_ADDR, port); } free(cname); KSSL_DEBUG("instance_name=%s\n", instance_name); return (instance_name); }
void runFailure() { char buf[10]; int err; getipnodebyaddr(NULL, anyint(), anyint(), &err); }