SockAddr sk_namelookup(const char *host, char **canonicalname)
SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
SockAddr sk_namelookup(const char *host, char **canonicalname){    SockAddr ret = snew(struct SockAddr_tag);#ifdef IPV6    struct addrinfo hints;    int err;#else    unsigned long a;    struct hostent *h = NULL;#endif    char realhost[8192];    /* Clear the structure and default to IPv4. */    memset(ret, 0, sizeof(struct SockAddr_tag));    ret->family = 0;		       /* We set this one when we have resolved the host. */    *realhost = '\0';    ret->error = NULL;#ifdef IPV6    hints.ai_flags = AI_CANONNAME;    hints.ai_family = AF_UNSPEC;    hints.ai_socktype = 0;    hints.ai_protocol = 0;    hints.ai_addrlen = 0;    hints.ai_addr = NULL;    hints.ai_canonname = NULL;    hints.ai_next = NULL;    err = getaddrinfo(host, NULL, NULL, &ret->ai);    if (err != 0) {	ret->error = gai_strerror(err);	return ret;    }    ret->family = ret->ai->ai_family;    *realhost = '\0';    if (ret->ai->ai_canonname != NULL)	strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);    else	strncat(realhost, host, sizeof(realhost) - 1);#else    if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {	/*	 * Otherwise use the IPv4-only gethostbyname... (NOTE:	 * we don't use gethostbyname as a fallback!)	 */	if (ret->family == 0) {		/*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */		if ( (h = gethostbyname(host)) )			ret->family = AF_INET;	}	if (ret->family == 0) {		ret->error = (h_errno == HOST_NOT_FOUND ||		    h_errno == NO_DATA ||		    h_errno == NO_ADDRESS ? "Host does not exist" :		    h_errno == TRY_AGAIN ?		    "Temporary name service failure" :		    "gethostbyname: unknown error");		return ret;	}	memcpy(&a, h->h_addr, sizeof(a));	/* This way we are always sure the h->h_name is valid :) */	strncpy(realhost, h->h_name, sizeof(realhost));    } else {	/*	 * This must be a numeric IPv4 address because it caused a	 * success return from inet_addr.	 */	ret->family = AF_INET;	strncpy(realhost, host, sizeof(realhost));    }    ret->address = ntohl(a);#endif    realhost[lenof(realhost)-1] = '\0';    *canonicalname = snewn(1+strlen(realhost), char);    strcpy(*canonicalname, realhost);    return ret;}