Ejemplo n.º 1
0
static ret_t
connect_to (cherokee_downloader_t *downloader,
            cherokee_buffer_t     *host,
            cuint_t                port,
            int                    protocol)
{
	ret_t              ret;
	cherokee_socket_t *sock = &downloader->socket;

	TRACE(ENTRIES, "host=%s port=%d proto=%d\n", host->buf, port, protocol);

	/* Create the socket
	 */
	ret = cherokee_socket_create_fd (sock, AF_INET);
	if (unlikely(ret != ret_ok)) return ret_error;

	/* Set the port
	 */
	SOCKET_SIN_PORT(sock) = htons(port);

	/* Supposing it's an IP: convert it.
	 */
	ret = cherokee_socket_pton (sock, host);
	if (ret != ret_ok) {

		/* Oops! It might be a hostname. Try to resolve it.
		 */
		ret = cherokee_socket_gethostbyname (sock, host);
		if (unlikely(ret != ret_ok)) return ret_error;
	}

	/* Connect to server
	 */
	ret = cherokee_socket_connect (sock);
	TRACE(ENTRIES, "socket=%p ret=%d\n", sock, ret);

	if (unlikely(ret != ret_ok))
		return ret;

	/* Is this connection TLS?
	 */
	if ((protocol == https) &&
	    (sock->cryptor != NULL))
	{
		ret = cherokee_socket_init_client_tls (sock, host);
		if (ret != ret_ok)
			return ret;
	}

	TRACE(ENTRIES, "Exits ok; socket=%p\n", sock);
	return ret_ok;
}
ret_t
cherokee_proxy_util_init_socket (cherokee_socket_t *socket,
				 cherokee_source_t *src)
{
	ret_t                    ret;
	cherokee_resolv_cache_t *resolv;

	TRACE (ENTRIES, "Initializing proxy socket: %s\n",
	       cherokee_string_is_ipv6 (&src->host) ? "IPv6": "IPv4");

	/* Family */
	if (cherokee_string_is_ipv6 (&src->host)) {
		ret = cherokee_socket_set_client (socket, AF_INET6);
	} else {
		ret = cherokee_socket_set_client (socket, AF_INET);
	}

        if (unlikely(ret != ret_ok))
		return ret_error;

	/* TCP port */
        SOCKET_SIN_PORT(socket) = htons (src->port);

        /* IP host */
	ret = cherokee_resolv_cache_get_default (&resolv);
	if (unlikely (ret != ret_ok)) {
		return ret_error;
	}

	ret = cherokee_resolv_cache_get_host (resolv, &src->host, socket);
	if (ret != ret_ok) {
		return ret_error;
	}

	/* Set a few properties */
	cherokee_fd_set_closexec    (socket->socket);
	cherokee_fd_set_nonblocking (socket->socket, true);
	cherokee_fd_set_nodelay     (socket->socket, true);

	return ret_ok;
}