debug("Trying to reverse map address %.100s.", ntop);
debug3("Trying to reverse map address %.100s.", ntop);
get_remote_hostname(int socket, int reverse_mapping_check){	struct sockaddr_storage from;	int i;	socklen_t fromlen;	struct addrinfo hints, *ai, *aitop;	char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];	/* Get IP address of client. */	fromlen = sizeof(from);	memset(&from, 0, sizeof(from));	if (getpeername(socket, (struct sockaddr *) &from, &fromlen) < 0) {		debug("getpeername failed: %.100s", strerror(errno));		fatal_cleanup();	}	if (from.ss_family == AF_INET)		check_ip_options(socket, ntop);	if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),	     NULL, 0, NI_NUMERICHOST) != 0)		fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");	debug("Trying to reverse map address %.100s.", ntop);	/* Map the IP address to a host name. */	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),	     NULL, 0, NI_NAMEREQD) != 0) {		/* Host name not found.  Use ip address. */		log("Could not reverse map address %.100s.", ntop);		return xstrdup(ntop);	}	/* Got host name. */	name[sizeof(name) - 1] = '\0';	/*	 * Convert it to all lowercase (which is expected by the rest	 * of this software).	 */	for (i = 0; name[i]; i++)		if (isupper(name[i]))			name[i] = tolower(name[i]);	if (!reverse_mapping_check)		return xstrdup(name);	/*	 * Map it back to an IP address and check that the given	 * address actually is an address of this host.  This is	 * necessary because anyone with access to a name server can	 * define arbitrary names for an IP address. Mapping from	 * name to IP address can be trusted better (but can still be	 * fooled if the intruder has access to the name server of	 * the domain).	 */	memset(&hints, 0, sizeof(hints));	hints.ai_family = from.ss_family;	hints.ai_socktype = SOCK_STREAM;	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {		log("reverse mapping checking getaddrinfo for %.700s "		    "failed - POSSIBLE BREAKIN ATTEMPT!", name);		return xstrdup(ntop);	}	/* Look for the address from the list of addresses. */	for (ai = aitop; ai; ai = ai->ai_next) {		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&		    (strcmp(ntop, ntop2) == 0))				break;	}	freeaddrinfo(aitop);	/* If we reached the end of the list, the address was not there. */	if (!ai) {		/* Address not found for the host name. */		log("Address %.100s maps to %.600s, but this does not "		    "map back to the address - POSSIBLE BREAKIN ATTEMPT!",		    ntop, name);		return xstrdup(ntop);	}	return xstrdup(name);}