static int get_nodes (const char *nodename, const struct addrinfo *hints, int port, int protocol, int socktype, struct addrinfo **res) { struct addrinfo *first = NULL; struct addrinfo **current = &first; int family = PF_UNSPEC; int flags = 0; int ret = EAI_NONAME; int error; if (hints != NULL) { family = hints->ai_family; flags = hints->ai_flags; } #ifdef HAVE_IPV6 if (family == PF_INET6 || family == PF_UNSPEC) { struct hostent *he; he = getipnodebyname (nodename, PF_INET6, 0, &error); if (he != NULL) { ret = add_hostent (port, protocol, socktype, ¤t, const_v6, he, &flags); freehostent (he); } } #endif if (family == PF_INET || family == PF_UNSPEC) { struct hostent *he; he = getipnodebyname (nodename, PF_INET, 0, &error); if (he != NULL) { ret = add_hostent (port, protocol, socktype, ¤t, const_v4, he, &flags); freehostent (he); } } *res = first; return ret; }
int mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result) { MonoAddressInfo *addr_info; addr_info = g_new0 (MonoAddressInfo, 1); #ifdef HAVE_GETHOSTBYNAME2 if (flags & MONO_HINT_IPV6 || flags & MONO_HINT_UNSPECIFIED) add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET6)); if (flags & MONO_HINT_IPV4 || flags & MONO_HINT_UNSPECIFIED) add_hostent (addr_info, flags, gethostbyname2 (hostname, AF_INET)); #else add_hostent (addr_info, flags, gethostbyname (hostname)) #endif if (!addr_info->entries) { *result = NULL; mono_free_address_info (addr_info); return 1; } *result = addr_info; return 0; }
int main (void) { const struct hostent *h; const char *host_name = "test-host"; int wait_time; DWORD addr_list [MAX_ADDRESSES+1]; dbug_init(); sock_init(); print_hosts(); wait_time = netdbCacheLife + 1; memset (&addr_list, 0, sizeof(addr_list)); addr_list[0] = htonl (_inet_addr("80.22.33.45")); addr_list[1] = htonl (_inet_addr("222.22.33.46")); addr_list[2] = htonl (_inet_addr("217.22.33.47")); addr_list[3] = htonl (_inet_addr("81.22.33.48")); addr_list[4] = INADDR_NONE; SOCK_DEBUGF (("\nadd_hostent: `%s'", host_name)); add_hostent (NULL, host_name, "some.cname.org", &addr_list[1], addr_list[0], netdbCacheLife); h = gethostbyname (host_name); if (!h) { fprintf (stderr, "gethostbyname() failed!. h_errno = %d\n", h_errno); return (1); } fprintf (stderr, "Waiting for cache-entry to timeout.."); Sleep (wait_time); fprintf (stderr, "gethostbyname() should do a DNS lookup now.\n"); h = gethostbyname (host_name); if (h) fprintf (stderr, "entry didn't timeout!.\n"); #if defined(USE_FORTIFY) Fortify_ListAllMemory(); Fortify_OutputStatistics(); #endif return (0); }
static BOOL gethostbyaddr_internal (const char *addr_name, int len, int type, struct _hostent *ret) { static char name [MAX_HOSTLEN]; struct _hostent *h = NULL; DWORD addr; BOOL rc; time_t now; h_errno = HOST_NOT_FOUND; did_lookup = FALSE; from_where = NULL; _resolve_exit = _resolve_timeout = 0; memset (ret, 0, sizeof(*ret)); if (type != AF_INET || len < SIZEOF(addr)) { h_errno = NO_RECOVERY; return (FALSE); } if (!netdb_init()) { h_errno = NO_RECOVERY; return (FALSE); } addr = *(DWORD*) addr_name; if ((addr == INADDR_ANY || /* 0.0.0.0 -> my_ip_addr */ addr == gethostid()) && gethostname(name,sizeof(name)) == 0) { /** \todo Should return all our addresses if we're multihomed. */ ret->h_num_addr = 1; ret->h_address[0] = gethostid(); ret->h_name = name; from_where = "sethostname"; return (TRUE); } if (addr == INADDR_BROADCAST || /* 255.255.255.255 */ (~ntohl(addr) & ~sin_mask) == 0) /* directed broadcast */ { ret->h_num_addr = 1; ret->h_address[0] = addr; ret->h_name = "broadcast"; return (TRUE); } now = time (NULL); /* If called from getnameinfo() with AI_CANONNAME, we * should not search the host0 list. But do a full reeverse * lookup. */ if (called_from_getai) goto expired; for (h = host0; h; h = h->h_next) { int i; for (i = 0; h->h_address[i] != INADDR_NONE && i < h->h_num_addr; i++) { if (addr == h->h_address[i]) { /* if cached entry expired, do a new reverse lookup */ if (h->h_timeout && now > h->h_timeout) goto expired; *ret = *h; return (TRUE); } } } expired: /* do a reverse ip lookup */ did_lookup = TRUE; rc = reverse_resolve_ip4 (addr, name, sizeof(name)); /* interrupted or timedout */ if (!rc && (_resolve_exit || _resolve_timeout)) return (FALSE); if (rc) /* successfully resolved */ { h = add_hostent (h, name, dom_cname, NULL, addr, dom_ttl); /** \todo should be the new aliases */ return (h ? *ret = *h, TRUE : FALSE); } /* Add the IP to the list even if reverse lookup failed and not * interrupted by _resolve_hook(). Thus the next call to gethostbyxx() * will return immediately. */ add_hostent (h, "*unknown*", NULL, NULL, addr, 0UL); return (FALSE); }
static BOOL gethostbyname_internal (const char *name, const char **alias, struct _hostent *ret) { static char our_name [MAX_HOSTLEN]; struct in_addr addr; struct _hostent *h; time_t now; DWORD ip; h_errno = HOST_NOT_FOUND; did_lookup = FALSE; from_where = NULL; _resolve_exit = _resolve_timeout = 0; memset (ret, 0, sizeof(*ret)); *alias = NULL; if (!netdb_init()) { h_errno = NO_RECOVERY; return (FALSE); } if (inet_aton(name,&addr)) { /** \todo should be canonical name */ ret->h_name = (char*) name; ret->h_address[0] = addr.s_addr; ret->h_num_addr = 1; return (TRUE); } now = time (NULL); for (h = host0; h; h = h->h_next) { int i; if (h->h_name && !stricmp(h->h_name,name)) { /* if cached entry expired, do DNS lookup */ if (h->h_timeout && now > h->h_timeout) goto expired; *ret = *h; return (h->h_address[0] != INADDR_NONE ? TRUE : FALSE); } for (i = 0; h->h_aliases[i] && i < MAX_HOST_ALIASES; i++) if (!stricmp(name,h->h_aliases[i])) { if (h->h_timeout && now > h->h_timeout) goto expired; *alias = h->h_aliases[i]; *ret = *h; return (h->h_address[0] != INADDR_NONE ? TRUE : FALSE); } } /* Not found in linked list (hosts file or cache). Check name * against our own host-name (short-name or FQDN). * \todo Should return all our addresses if we're multihomed. */ if (hostname[0] && !stricmp(name,hostname)) { ret->h_num_addr = 1; ret->h_address[0] = gethostid(); ret->h_name = hostname; from_where = "sethostname"; return (TRUE); } if (!gethostname(our_name,sizeof(our_name)) && !stricmp(name,our_name)) { ret->h_num_addr = 1; ret->h_address[0] = gethostid(); ret->h_name = our_name; from_where = "sethostname"; return (TRUE); } expired: if (called_from_resolve) /* prevent recursion */ return (FALSE); /* Do a full DNS lookup */ called_from_ghbn = TRUE; ip = resolve (name); /* do a normal lookup */ called_from_ghbn = FALSE; did_lookup = TRUE; if (_resolve_exit || /* interrupted or other fail */ _resolve_timeout) /* timed out resolving */ return (FALSE); if (ip) /* successfully resolved */ { h = add_hostent (h, name, dom_cname, dom_a4list, htonl(ip), dom_ttl); return (h ? *ret = *h, TRUE : FALSE); } /* Add the name to the list even if we got a negative DNS reply. * Thus the next call to gethostbyxx() will return immediately. */ add_hostent (h, name, NULL, NULL, INADDR_NONE, netdbCacheLife); return (FALSE); }