/* * Return a 'struct hostent *' for name. */ struct hostent * W32_CALL gethostbyname (const char *name) { struct _hostent h; const char *alias; SOCK_DEBUGF (("\ngethostbyname: `%s'", name)); if (gethostbyname_internal(name, &alias, &h)) { #if defined(USE_DEBUG) int i; for (i = 0; i < h.h_num_addr; i++) SOCK_DEBUGF ((" %s,", inet_ntoa(*(struct in_addr*)&h.h_address[i]))); if (!did_lookup) SOCK_DEBUGF ((" %s", h.h_timeout ? "cached" : from_where ? from_where : "hosts-file")); if (alias) SOCK_DEBUGF ((" (alias %s)", alias)); #endif return fill_hostent (&h); } if (called_from_resolve) SOCK_DEBUGF ((", unknown (called from resolve)")); else SOCK_DEBUGF ((", failed (%s)", did_lookup ? dom_strerror(dom_errno) : hstrerror(h_errno))); return (NULL); }
/* * Return a 'struct hostent *' for an address. */ struct hostent *W32_CALL gethostbyaddr (const char *addr_name, int len, int type) { struct _hostent h; SOCK_DEBUGF (("\ngethostbyaddr: %s", (type == AF_INET && addr_name) ? inet_ntoa(*(struct in_addr*)addr_name) : (type == AF_INET6 && addr_name) ? _inet6_ntoa(addr_name) : "")); #if defined(USE_IPV6) if (type == AF_INET6 && len == sizeof(struct in6_addr)) { struct hostent *he; SOCK_ENTER_SCOPE(); he = gethostbyaddr6 (addr_name); SOCK_LEAVE_SCOPE(); return (he); } #endif if (gethostbyaddr_internal (addr_name, len, type, &h)) { SOCK_DEBUGF ((" `%s'", h.h_name)); if (!did_lookup) SOCK_DEBUGF ((", %s", h.h_timeout ? "cached" : "hosts-file")); return fill_hostent (&h); } SOCK_DEBUGF ((" failed (%s) ", did_lookup ? dom_strerror(dom_errno) : hstrerror(h_errno))); return (NULL); }
struct hostent *gethostbyname2(const char *name, int af) { struct addrinfo hints; struct addrinfo *rv; int err; /* Set up a query to getaddrinfo(). */ memset(&hints, 0, sizeof(hints)); hints.ai_family = af; err = getaddrinfo(name, NULL, &hints, &rv); switch(err) { case 0: /* Success. XXXX: Do something with the data. */ break; case EAI_FAIL: h_errno = NO_RECOVERY; return NULL; case EAI_MEMORY: case EAI_AGAIN: case EAI_SYSTEM: h_errno = TRY_AGAIN; return NULL; case EAI_NONAME: h_errno = HOST_NOT_FOUND; return NULL; default: /* None of the other errors should happen, so if they do, then it is a bad thing. Assume the worst. */ h_errno = NO_RECOVERY; return NULL; } /* Clean up any old entry in the static hostent. */ cleanup_hostent(); /* Fill in the hostent with the information we got from getaddrinfo() and clean up. */ err = fill_hostent(name, rv); freeaddrinfo(rv); if(err) { h_errno = err; return NULL; } return &he; }
/** * Return the next (non-commented) line from the host-file. * Format is: * ip-address host-name [alias..] {\n | # ..} */ struct hostent * W32_CALL gethostent (void) { struct _hostent h; char *tok, *ip, *name, *alias; char buf [2*MAX_HOSTLEN]; int i; if (!netdb_init() || !hostFile) { h_errno = NO_RECOVERY; return (NULL); } while (1) { if (!fgets(buf,sizeof(buf),hostFile)) return (NULL); tok = strltrim (buf); if (*tok == '#' || *tok == ';' || *tok == '\n') continue; ip = strtok (tok, " \t"); name = strtok (NULL, " \t\n"); if (ip && name && isaddr(ip)) break; } if (hostClose) endhostent(); memset (&h, 0, sizeof(h)); if (!strcmp(ip,"0.0.0.0")) /* inet_addr() maps 0 -> INADDR_NONE */ h.h_address[0] = INADDR_ANY; else h.h_address[0] = inet_addr (ip); h.h_num_addr = 1; h.h_name = name; alias = strtok (NULL, " \t\n"); for (i = 0; alias && i < MAX_HOST_ALIASES; i++) { static char aliases [MAX_NETENT_ALIASES][MAX_HOSTLEN]; if (*alias == '#' || *alias == ';') break; h.h_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i])); alias = strtok (NULL, " \t\n"); } return fill_hostent (&h); }