mirror of
https://github.com/apache/httpd.git
synced 2025-08-08 15:02:10 +03:00
mod_proxy: Handle backend address renewal with address_ttl= parameter.
Define a new proxy_address struct holding the current/latest sockaddr in use by each proxy worker and conn. Since backend addresses can be updated when their TTL expires and while connections are being processed, each address is refcounted and freed only when the last worker (or conn) using it grabs the new one. The lifetime of the addresses is handled at a single place by the new ap_proxy_determine_address() function. It guarantees to bind the current/latest backend address to the passed in conn (or do nothing if it's up to date already). The function is called indirectly by ap_proxy_determine_connection() for the proxy modules that use it, or directly by mod_proxy_ftp and mod_proxy_hcheck. It also is called eventually by ap_proxy_connect_backend() when connect()ing all the current addresses fails, to check (PROXY_DETERMINE_ADDRESS_CHECK) if some new addrs are available. This commit is also a rework of the lifetime of conn->addr, conn->hostname and conn->forward, using the conn->uds_pool and conn->fwd_pool for the cases where the backend is connected through a UDS socket and a remote CONNECT proxy respectively. * include/ap_mmn.h: Minor bump for new function/fields. * modules/proxy/mod_proxy.h (struct proxy_address, ap_proxy_determine_addresss()): Declare ap_proxy_determine_addresss() and opaque struct proxy_address, new fields to structs proxy_conn_rec/proxy_worker_shared/proxy_worker. * modules/proxy/mod_proxy.c (set_worker_param): Parse/set the new worker->address_ttl parameter. * modules/proxy/proxy_util.c (proxy_util_register_hooks(), ap_proxy_initialize_worker(), ap_proxy_connection_reusable(), ap_proxyerror(), proxyerror_core(), init_conn_pool(), make_conn_subpool(), connection_make(), connection_cleanup(), connection_constructor()): Initialize *proxy_start_time in proxy_util_register_hooks() as the epoch from which expiration times are relative (i.e. seconds stored in an uint32_t for atomic changes). Make sure worker->s->is_address_reusable and worker->s->disablereuse are consistant in ap_proxy_initialize_worker(), thus no need to check for both in ap_proxy_connection_reusable(). New proxyerror_core() helper taking an apr_status_t to log, wrap in ap_proxyerror(). New make_conn_subpool() to create worker->cp->{pool,dns} with their own allocator. New connection_make() helper to factorize code in connection_cleanup() and connection_constructor(). * modules/proxy/proxy_util.c (proxy_address_inc(), proxy_address_dec(), proxy_address_cleanup(), proxy_address_set_expired(), worker_address_get(), worker_address_set(), worker_address_resolve(), proxy_addrs_equal(), ap_proxy_determine_address(), ap_proxy_determine_connection(), ap_proxy_connect_backend()): Implement ap_proxy_determine_address() using the above helpers for atomic changes, and call it from ap_proxy_determine_connection() and ap_proxy_connect_backend(). * modules/proxy/mod_proxy_ftp.c (proxy_ftp_handler): Use ap_proxy_determine_address() and use the returned backend->addr. * modules/proxy/mod_proxy_hcheck.c (hc_determine_connection, hc_get_backend, hc_init_worker, hc_watchdog_callback): Use ap_proxy_determine_address() in hc_determine_connection() and call the latter from hc_get_backend(), replace hc_init_worker() by hc_init_baton() which now calls hc_get_hcworker() and hc_get_backend() to resolve the first address at init time. * modules/proxy/mod_proxy_http.c (proxy_http_handler): Use backend->addr and ->hostname instead of worker->cp->addr and worker->s->hostname_ex respectively. * modules/proxy/mod_proxy_ajp.c (ap_proxy_ajp_request): Use backend->addr and ->hostname instead of worker->cp->addr and worker->s->hostname_ex respectively. Closes #367 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1912459 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -975,13 +975,8 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
conn_rec *c = r->connection;
|
||||
proxy_conn_rec *backend;
|
||||
apr_socket_t *sock, *local_sock, *data_sock = NULL;
|
||||
apr_sockaddr_t *connect_addr = NULL;
|
||||
apr_status_t rv;
|
||||
conn_rec *origin, *data = NULL;
|
||||
apr_status_t err = APR_SUCCESS;
|
||||
#if APR_HAS_THREADS
|
||||
apr_status_t uerr = APR_SUCCESS;
|
||||
#endif
|
||||
apr_bucket_brigade *bb;
|
||||
char *buf, *connectname;
|
||||
apr_port_t connectport;
|
||||
@@ -1005,8 +1000,8 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
/* stuff for PASV mode */
|
||||
int connect = 0, use_port = 0;
|
||||
char dates[APR_RFC822_DATE_LEN];
|
||||
apr_status_t rv;
|
||||
int status;
|
||||
apr_pool_t *address_pool;
|
||||
|
||||
/* is this for us? */
|
||||
if (proxyhost) {
|
||||
@@ -1120,53 +1115,8 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01036)
|
||||
"connecting %s to %s:%d", url, connectname, connectport);
|
||||
|
||||
if (worker->s->is_address_reusable) {
|
||||
if (!worker->cp->addr) {
|
||||
#if APR_HAS_THREADS
|
||||
if ((err = PROXY_THREAD_LOCK(worker)) != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, err, r, APLOGNO(01037) "lock");
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
connect_addr = AP_VOLATILIZE_T(apr_sockaddr_t *, worker->cp->addr);
|
||||
address_pool = worker->cp->dns_pool;
|
||||
}
|
||||
else
|
||||
address_pool = r->pool;
|
||||
|
||||
/* do a DNS lookup for the destination host */
|
||||
if (!connect_addr)
|
||||
err = apr_sockaddr_info_get(&(connect_addr),
|
||||
connectname, APR_UNSPEC,
|
||||
connectport, 0,
|
||||
address_pool);
|
||||
if (worker->s->is_address_reusable && !worker->cp->addr) {
|
||||
worker->cp->addr = connect_addr;
|
||||
#if APR_HAS_THREADS
|
||||
if ((uerr = PROXY_THREAD_UNLOCK(worker)) != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, uerr, r, APLOGNO(01038) "unlock");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
/*
|
||||
* get all the possible IP addresses for the destname and loop through
|
||||
* them until we get a successful connection
|
||||
*/
|
||||
if (APR_SUCCESS != err) {
|
||||
return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
|
||||
"DNS lookup failure for: ",
|
||||
connectname, NULL));
|
||||
}
|
||||
|
||||
/* check if ProxyBlock directive on this host */
|
||||
if (OK != ap_proxy_checkproxyblock(r, conf, connectname, connect_addr)) {
|
||||
return ap_proxyerror(r, HTTP_FORBIDDEN,
|
||||
"Connect to remote machine blocked");
|
||||
}
|
||||
|
||||
/* create space for state information */
|
||||
backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config, &proxy_ftp_module);
|
||||
backend = ap_get_module_config(c->conn_config, &proxy_ftp_module);
|
||||
if (!backend) {
|
||||
status = ap_proxy_acquire_connection("FTP", &backend, worker, r->server);
|
||||
if (status != OK) {
|
||||
@@ -1176,11 +1126,26 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
}
|
||||
return status;
|
||||
}
|
||||
/* TODO: see if ftp could use determine_connection */
|
||||
backend->addr = connect_addr;
|
||||
ap_set_module_config(c->conn_config, &proxy_ftp_module, backend);
|
||||
}
|
||||
|
||||
/*
|
||||
* get all the possible IP addresses for the destname and loop through
|
||||
* them until we get a successful connection
|
||||
*/
|
||||
err = ap_proxy_determine_address("FTP", backend, connectname, connectport,
|
||||
0, r, r->server);
|
||||
if (APR_SUCCESS != err) {
|
||||
return ftp_proxyerror(r, backend, HTTP_BAD_GATEWAY,
|
||||
"Error resolving backend address");
|
||||
}
|
||||
|
||||
/* check if ProxyBlock directive on this host */
|
||||
if (OK != ap_proxy_checkproxyblock(r, conf, connectname, backend->addr)) {
|
||||
return ftp_proxyerror(r, backend, HTTP_FORBIDDEN,
|
||||
"Connect to remote machine blocked");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* II: Make the Connection -----------------------
|
||||
@@ -1188,11 +1153,7 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
* We have determined who to connect to. Now make the connection.
|
||||
*/
|
||||
|
||||
|
||||
if (ap_proxy_connect_backend("FTP", backend, worker, r->server)) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01039)
|
||||
"an error occurred creating a new connection to %pI (%s)",
|
||||
connect_addr, connectname);
|
||||
proxy_ftp_cleanup(r, backend);
|
||||
return HTTP_SERVICE_UNAVAILABLE;
|
||||
}
|
||||
@@ -1536,7 +1497,8 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
"PASV contacting host %d.%d.%d.%d:%d",
|
||||
h3, h2, h1, h0, pasvport);
|
||||
|
||||
if ((rv = apr_socket_create(&data_sock, connect_addr->family, SOCK_STREAM, 0, r->pool)) != APR_SUCCESS) {
|
||||
if ((rv = apr_socket_create(&data_sock, backend->addr->family,
|
||||
SOCK_STREAM, 0, r->pool)) != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01045)
|
||||
"error creating PASV socket");
|
||||
proxy_ftp_cleanup(r, backend);
|
||||
@@ -1558,11 +1520,13 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
}
|
||||
|
||||
/* make the connection */
|
||||
err = apr_sockaddr_info_get(&pasv_addr, apr_psprintf(p, "%d.%d.%d.%d", h3, h2, h1, h0), connect_addr->family, pasvport, 0, p);
|
||||
err = apr_sockaddr_info_get(&pasv_addr, apr_psprintf(p, "%d.%d.%d.%d",
|
||||
h3, h2, h1, h0),
|
||||
backend->addr->family, pasvport, 0, p);
|
||||
if (APR_SUCCESS != err) {
|
||||
return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
|
||||
"DNS lookup failure for: ",
|
||||
connectname, NULL));
|
||||
return ftp_proxyerror(r, backend, HTTP_BAD_GATEWAY,
|
||||
apr_pstrcat(p, "DNS lookup failure for: ",
|
||||
connectname, NULL));
|
||||
}
|
||||
rv = apr_socket_connect(data_sock, pasv_addr);
|
||||
if (rv != APR_SUCCESS) {
|
||||
@@ -1586,7 +1550,8 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
apr_port_t local_port;
|
||||
unsigned int h0, h1, h2, h3, p0, p1;
|
||||
|
||||
if ((rv = apr_socket_create(&local_sock, connect_addr->family, SOCK_STREAM, 0, r->pool)) != APR_SUCCESS) {
|
||||
if ((rv = apr_socket_create(&local_sock, backend->addr->family,
|
||||
SOCK_STREAM, 0, r->pool)) != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01049)
|
||||
"error creating local socket");
|
||||
proxy_ftp_cleanup(r, backend);
|
||||
@@ -1608,9 +1573,9 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
|
||||
|
||||
err = apr_sockaddr_info_get(&local_addr, local_ip, APR_UNSPEC, local_port, 0, r->pool);
|
||||
if (APR_SUCCESS != err) {
|
||||
return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
|
||||
"DNS lookup failure for: ",
|
||||
connectname, NULL));
|
||||
return ftp_proxyerror(r, backend, HTTP_BAD_GATEWAY,
|
||||
apr_pstrcat(p, "DNS lookup failure for: ",
|
||||
connectname, NULL));
|
||||
}
|
||||
|
||||
if ((rv = apr_socket_bind(local_sock, local_addr)) != APR_SUCCESS) {
|
||||
|
Reference in New Issue
Block a user