Exemple #1
0
/*
 * Function:	get_proxy_port_admin
 * Description:	Retrieves proxy setting from admin file
 * Parameters:	proxy - where to store resulting proxy (host:port or URL)
 *		port - Where to store resulting proxy port
 * Returns:	B_TRUE - admin file had a valid proxy setting,
 *		and it is stored in "proxy".
 *		B_FALSE - no proxy setting in admin file, or
 *		invalid setting in admin file.
 */
boolean_t
get_proxy_port_admin(char **proxy, ushort_t *port)
{
	extern unsigned short strip_port(char *proxy);
	if (ADMSET(proxy) && !path_valid(adm.proxy)) {
		/* admin file has bad keystore */
		return (B_FALSE);
	} else if (ADMSET(proxy)) {
		*proxy = strdup(adm.proxy);
		*port = strip_port(adm.proxy);
	}
	return (B_TRUE);
}
status_t
BNetworkAddressResolver::SetTo(int family, const char* host,
	const char* service, uint32 flags)
{
	Unset();

	// Check if the address contains a port

	BString hostString(host);

	BString portString;
	if (!strip_port(hostString, portString) && service != NULL)
		portString = service;

	// Resolve address

	addrinfo hint = {0};
	hint.ai_family = family;
	if ((flags & B_NO_ADDRESS_RESOLUTION) != 0)
		hint.ai_flags |= AI_NUMERICHOST;
	else if ((flags & B_UNCONFIGURED_ADDRESS_FAMILIES) == 0)
		hint.ai_flags |= AI_ADDRCONFIG;

	if (host == NULL && portString.Length() == 0) {
		portString = "0";
		hint.ai_flags |= AI_PASSIVE;
	}

	int status = getaddrinfo(host != NULL ? hostString.String() : NULL,
		portString.Length() != 0 ? portString.String() : NULL, &hint, &fInfo);
	if (status == 0)
		return fStatus = B_OK;

	// Map errors
	// TODO: improve error reporting, maybe add specific error codes?

	switch (status) {
		case EAI_ADDRFAMILY:
		case EAI_BADFLAGS:
		case EAI_PROTOCOL:
		case EAI_BADHINTS:
		case EAI_SOCKTYPE:
		case EAI_SERVICE:
		case EAI_NONAME:
		case EAI_FAMILY:
			fStatus = B_BAD_VALUE;
			break;

		case EAI_SYSTEM:
			fStatus = errno;
			break;

		case EAI_OVERFLOW:
		case EAI_MEMORY:
			fStatus = B_NO_MEMORY;
			break;

		case EAI_AGAIN:
			// TODO: better error code to denote temporary failure?
			fStatus = B_TIMED_OUT;
			break;

		default:
			fStatus = B_ERROR;
			break;
	}

	return fStatus;
}