]> granicus.if.org Git - apache/commitdiff
Add a new mod_proxy_fdpass module to pass a client connection off to a separate daemon.
authorPaul Querna <pquerna@apache.org>
Fri, 12 Dec 2008 08:04:47 +0000 (08:04 +0000)
committerPaul Querna <pquerna@apache.org>
Fri, 12 Dec 2008 08:04:47 +0000 (08:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@725940 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
modules/proxy/config.m4
modules/proxy/mod_proxy_fdpass.c [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 05e3fbf1b6aaf6f52e6965503331699953f942ba..450e2e4134a618777c1065357b3dfb512203baba 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,9 @@
 Changes with Apache 2.3.1
 [ When backported to 2.2.x, remove entry from this file ]
 
+  *) mod_proxy_fdpass: New module to pass a client connection over to a separate
+     process that is reading from a unix daemon socket.
+
   *) mod_ssl: Improve environment variable extraction to be more
      efficient and to correctly handle DNs with duplicate tags.
      PR 45975.  [Joe Orton]
index 7b71523664c44d5f488099a2bb1ae3dbc4f9870d..9967f6456f60d01edf0d9eb63aefbf0e1525bf74 100644 (file)
@@ -17,6 +17,7 @@ proxy_connect_objs="mod_proxy_connect.lo"
 proxy_ftp_objs="mod_proxy_ftp.lo"
 proxy_http_objs="mod_proxy_http.lo"
 proxy_fcgi_objs="mod_proxy_fcgi.lo"
+proxy_fdpass_objs="mod_proxy_fdpass.lo"
 proxy_ajp_objs="mod_proxy_ajp.lo ajp_header.lo ajp_link.lo ajp_msg.lo ajp_utils.lo"
 proxy_balancer_objs="mod_proxy_balancer.lo"
 
@@ -28,6 +29,7 @@ case "$host" in
     proxy_ftp_objs="$proxy_ftp_objs mod_proxy.la"
     proxy_http_objs="$proxy_http_objs mod_proxy.la"
     proxy_fcgi_objs="$proxy_fcgi_objs mod_proxy.la"
+    proxy_fdpass_objs="$proxy_fdpass_objs mod_proxy.la"
     proxy_ajp_objs="$proxy_ajp_objs mod_proxy.la"
     proxy_balancer_objs="$proxy_balancer_objs mod_proxy.la"
     ;;
@@ -37,6 +39,7 @@ APACHE_MODULE(proxy_connect, Apache proxy CONNECT module, $proxy_connect_objs, ,
 APACHE_MODULE(proxy_ftp, Apache proxy FTP module, $proxy_ftp_objs, , $proxy_mods_enable)
 APACHE_MODULE(proxy_http, Apache proxy HTTP module, $proxy_http_objs, , $proxy_mods_enable)
 APACHE_MODULE(proxy_fcgi, Apache proxy FastCGI module, $proxy_fcgi_objs, , $proxy_mods_enable)
+APACHE_MODULE(proxy_fdpass, Apache proxy to Unix Daemon Socket module, $proxy_fdpass_objs, , $proxy_mods_enable)
 APACHE_MODULE(proxy_ajp, Apache proxy AJP module, $proxy_ajp_objs, , $proxy_mods_enable)
 APACHE_MODULE(proxy_balancer, Apache proxy BALANCER module, $proxy_balancer_objs, , $proxy_mods_enable)
 
diff --git a/modules/proxy/mod_proxy_fdpass.c b/modules/proxy/mod_proxy_fdpass.c
new file mode 100644 (file)
index 0000000..f156694
--- /dev/null
@@ -0,0 +1,261 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mod_proxy.h"
+
+#ifndef CMSG_DATA
+#error This module only works on unix platforms with the correct OS support
+#endif
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+/* for apr_wait_for_io_or_timeout */
+#include "apr_support.h"
+
+module AP_MODULE_DECLARE_DATA proxy_fdpass_module;
+
+static int proxy_fdpass_canon(request_rec *r, char *url)
+{
+    const char *path;
+
+    if (strncasecmp(url, "fd://", 5) == 0) {
+        url += 5;
+    }
+    else {
+        return DECLINED;
+    }
+    
+    path = ap_server_root_relative(r->pool, url);
+
+    r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
+
+    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
+                  "proxy: FD: set r->filename to %s", r->filename);
+    return OK;
+}
+
+/* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
+static apr_status_t socket_connect_un(apr_socket_t *sock,
+                                      struct sockaddr_un *sa)
+{
+    apr_status_t rv;
+    apr_os_sock_t rawsock;
+    apr_interval_time_t t;
+
+    rv = apr_os_sock_get(&rawsock, sock);
+    if (rv) {
+        return rv;
+    }
+
+    rv = apr_socket_timeout_get(sock, &t);
+    if (rv) {
+        return rv;
+    }
+
+    do {
+        rv = connect(rawsock, (struct sockaddr*)sa, sizeof(*sa) + strlen(sa->sun_path));
+    } while (rv == -1 && errno == EINTR);
+
+    if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
+        && (t > 0)) {
+        rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
+        if (rv != APR_SUCCESS) {
+            return rv;
+        }
+    }
+    
+    if (rv == -1 && errno != EISCONN) {
+        return errno;
+    }
+
+    return APR_SUCCESS;
+}
+
+static apr_status_t get_socket_from_path(apr_pool_t *p,
+                                         const char* path,
+                                         apr_socket_t **out_sock)
+{
+    struct sockaddr_un sa;
+    apr_socket_t *s;
+    apr_status_t rv;
+    *out_sock = NULL;
+
+    rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
+
+    if (rv) {
+        return rv;
+    }
+
+    sa.sun_family = AF_UNIX;
+    strncpy(sa.sun_path, path, sizeof(sa.sun_path));
+
+    rv = socket_connect_un(s, &sa);
+    if (rv) {
+        return rv;
+    }
+
+    *out_sock = s;
+
+    return APR_SUCCESS;
+}
+
+
+static apr_status_t send_socket(apr_pool_t *p,
+                                apr_socket_t *s,
+                                apr_socket_t *outbound)
+{
+    apr_status_t rv;
+    apr_os_sock_t rawsock;
+    apr_os_sock_t srawsock;
+    struct msghdr msg;
+    struct cmsghdr *cmsg;
+       struct iovec iov;
+    char b = '\0';
+    
+    rv = apr_os_sock_get(&rawsock, outbound);
+    if (rv) {
+        return rv;
+    }
+
+    rv = apr_os_sock_get(&srawsock, s);
+    if (rv) {
+        return rv;
+    }
+    
+    memset(&msg, 0, sizeof(msg));
+
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+
+       iov.iov_base = &b;
+       iov.iov_len = 1;
+
+    cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
+    cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+
+    memcpy(CMSG_DATA(cmsg), &rawsock, sizeof(rawsock));
+
+    msg.msg_control = cmsg;
+    msg.msg_controllen = cmsg->cmsg_len;
+
+    rv = sendmsg(srawsock, &msg, 0);
+
+    if (rv == -1) {
+        return errno;
+    }
+
+    
+    return APR_SUCCESS;
+}
+
+static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
+                              proxy_server_conf *conf,
+                              char *url, const char *proxyname,
+                              apr_port_t proxyport)
+{
+    apr_status_t rv;
+    apr_socket_t *sock;
+    apr_socket_t *clientsock;
+
+    if (strncasecmp(url, "fd://", 5) == 0) {
+        url += 5;
+    }
+    else {
+        return DECLINED;
+    }
+
+    rv = get_socket_from_path(r->pool, url, &sock);
+
+    if (rv) {
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
+                      "proxy: FD: Failed to connect to '%s'",
+                      url);
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    r->connection->keepalive = AP_CONN_CLOSE;
+
+    /* TODO: Make this part a provider, so you can send a custom body / headers,
+     * before passing the client off to the socket. 
+     */
+    {
+        int status;
+        apr_bucket_brigade *bb;
+        apr_bucket *e;
+
+        bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
+        e = apr_bucket_flush_create(r->connection->bucket_alloc);
+
+        APR_BRIGADE_INSERT_TAIL(bb, e);
+
+        status = ap_pass_brigade(r->output_filters, bb);
+        
+        if (status != OK) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
+                          "proxy: FD: ap_pass_brigade failed:");
+            return status;
+        }
+    }
+
+    /* XXXXX: THIS IS AN EVIL HACK */
+    /* There should really be a (documented) public API for this ! */
+    clientsock = ap_get_module_config(r->connection->conn_config, &core_module);
+
+    rv = send_socket(r->pool, sock, clientsock);
+    if (rv) {
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
+                      "proxy: FD: send_socket failed:");
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    {
+        apr_socket_t *dummy;
+        /* Create a dummy unconnected socket, and set it as the one we were 
+         * connected to, so that when the core closes it, it doesn't close 
+         * the tcp connection to the client.
+         */
+        rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP, r->connection->pool);
+        if (rv) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
+                          "proxy: FD: failed to create dummy socket");
+            return HTTP_INTERNAL_SERVER_ERROR;
+        }
+        ap_set_module_config(r->connection->conn_config, &core_module, dummy);
+    }
+    
+    
+    return OK;
+}
+
+static void register_hooks(apr_pool_t *p)
+{
+    proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
+    proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
+}
+
+module AP_MODULE_DECLARE_DATA proxy_fdpass_module = {
+    STANDARD20_MODULE_STUFF,
+    NULL,                       /* create per-directory config structure */
+    NULL,                       /* merge per-directory config structures */
+    NULL,                       /* create per-server config structure */
+    NULL,                       /* merge per-server config structures */
+    NULL,                       /* command apr_table_t */
+    register_hooks              /* register hooks */
+};