apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r745763 - in /apr/apr/trunk: ./ build/ include/ include/arch/unix/ network_io/unix/ test/
Date Thu, 19 Feb 2009 07:15:24 GMT
Author: mturk
Date: Thu Feb 19 07:15:23 2009
New Revision: 745763

URL: http://svn.apache.org/viewvc?rev=745763&view=rev
Log:
Enable unix domain (AF_UNIX) sockets if supported by the OS

Modified:
    apr/apr/trunk/CHANGES
    apr/apr/trunk/build/apr_network.m4
    apr/apr/trunk/configure.in
    apr/apr/trunk/include/apr.h.in
    apr/apr/trunk/include/apr.hnw
    apr/apr/trunk/include/apr.hw
    apr/apr/trunk/include/apr_network_io.h
    apr/apr/trunk/include/arch/unix/apr_arch_networkio.h
    apr/apr/trunk/network_io/unix/sockaddr.c
    apr/apr/trunk/network_io/unix/sockets.c
    apr/apr/trunk/network_io/unix/sockopt.c
    apr/apr/trunk/test/sockchild.c
    apr/apr/trunk/test/testsock.c

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/CHANGES [utf-8] (original)
+++ apr/apr/trunk/CHANGES [utf-8] Thu Feb 19 07:15:23 2009
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) Added Unix domain socket support.
+     [Mladen Turk]
+
   *) Win32: Use WSAPoll as default pollset method if supported and
      found inside winsock dll.
      [Mladen Turk]

Modified: apr/apr/trunk/build/apr_network.m4
URL: http://svn.apache.org/viewvc/apr/apr/trunk/build/apr_network.m4?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/build/apr_network.m4 (original)
+++ apr/apr/trunk/build/apr_network.m4 Thu Feb 19 07:15:23 2009
@@ -788,6 +788,36 @@
 fi
 ])
 
+dnl Check for presence of struct sockaddr_un.
+AC_DEFUN([APR_CHECK_SOCKADDR_UN], [
+AC_CACHE_CHECK(for sockaddr_un, ac_cv_define_sockaddr_un,[
+AC_TRY_COMPILE([
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+],[
+struct sockaddr_un sa;
+],[
+    ac_cv_define_sockaddr_un=yes
+],[
+    ac_cv_define_sockaddr_un=no
+])
+])
+
+if test "$ac_cv_define_sockaddr_un" = "yes"; then
+  have_sockaddr_un=1
+else
+  have_sockaddr_un=0
+fi
+AC_SUBST(have_sockaddr_un)
+])
+
 dnl
 dnl APR_H_ERRNO_COMPILE_CHECK
 dnl

Modified: apr/apr/trunk/configure.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/configure.in?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/configure.in (original)
+++ apr/apr/trunk/configure.in Thu Feb 19 07:15:23 2009
@@ -2301,6 +2301,7 @@
 APR_CHECK_WORKING_GETNAMEINFO
 APR_CHECK_SOCKADDR_IN6
 APR_CHECK_SOCKADDR_STORAGE
+APR_CHECK_SOCKADDR_UN
 
 have_ipv6="0"
 if test "$user_disabled_ipv6" = 1; then

Modified: apr/apr/trunk/include/apr.h.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr.h.in?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/include/apr.h.in (original)
+++ apr/apr/trunk/include/apr.h.in Thu Feb 19 07:15:23 2009
@@ -208,6 +208,7 @@
 #define APR_HAVE_INET_ADDR      @have_inet_addr@
 #define APR_HAVE_INET_NETWORK   @have_inet_network@
 #define APR_HAVE_IPV6           @have_ipv6@
+#define APR_HAVE_SOCKADDR_UN    @have_sockaddr_un@
 #define APR_HAVE_MEMMOVE        @have_memmove@
 #define APR_HAVE_SETRLIMIT      @have_setrlimit@
 #define APR_HAVE_SIGACTION      @have_sigaction@

Modified: apr/apr/trunk/include/apr.hnw
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr.hnw?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/include/apr.hnw (original)
+++ apr/apr/trunk/include/apr.hnw Thu Feb 19 07:15:23 2009
@@ -178,6 +178,7 @@
 #else
 #define APR_HAVE_IPV6           0
 #endif
+#define APR_HAVE_SOCKADDR_UN    0
 #define APR_HAVE_MEMCHR         1
 #define APR_HAVE_MEMMOVE        1
 #define APR_HAVE_SETRLIMIT      0

Modified: apr/apr/trunk/include/apr.hw
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr.hw?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/include/apr.hw (original)
+++ apr/apr/trunk/include/apr.hw Thu Feb 19 07:15:23 2009
@@ -278,6 +278,7 @@
 #define APR_HAVE_INET_ADDR      1
 #define APR_HAVE_INET_NETWORK   0
 #define APR_HAVE_IPV6           0
+#define APR_HAVE_SOCKADDR_UN    0
 #define APR_HAVE_MEMMOVE        1
 #define APR_HAVE_SETRLIMIT      0
 #define APR_HAVE_SIGACTION      0

Modified: apr/apr/trunk/include/apr_network_io.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_network_io.h?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_network_io.h (original)
+++ apr/apr/trunk/include/apr_network_io.h Thu Feb 19 07:15:23 2009
@@ -26,10 +26,14 @@
 #include "apr_file_io.h"
 #include "apr_errno.h"
 #include "apr_inherit.h" 
+#include "apr_perms_set.h"
 
 #if APR_HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
+#if APR_HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -154,6 +158,25 @@
 #define APR_INET6    AF_INET6
 #endif
 
+#if APR_HAVE_SOCKADDR_UN
+#if defined (AF_UNIX)
+#define APR_UNIX    AF_UNIX
+#elif defined(AF_LOCAL)
+#define APR_UNIX    AF_LOCAL
+#else
+#error "Neither AF_UNIX nor AF_LOCAL is defined"
+#endif
+#else /* !APR_HAVE_SOCKADDR_UN */
+#if defined (AF_UNIX)
+#define APR_UNIX    AF_UNIX
+#elif defined(AF_LOCAL)
+#define APR_UNIX    AF_LOCAL
+#else
+/* TODO: Use a smarter way to detect unique APR_UNIX value */
+#define APR_UNIX    1234
+#endif
+#endif
+
 /**
  * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
  * @{
@@ -245,6 +268,10 @@
          * dependent on whether APR_HAVE_IPV6 is defined. */
         struct sockaddr_storage sas;
 #endif
+#if APR_HAVE_SOCKADDR_UN
+        /** Unix domain socket sockaddr structure */
+        struct sockaddr_un unx;
+#endif
     } sa;
 };
 
@@ -352,6 +379,7 @@
  * @param sa The new apr_sockaddr_t.
  * @param hostname The hostname or numeric address string to resolve/parse, or
  *               NULL to build an address that corresponds to 0.0.0.0 or ::
+ *               or in case of APR_UNIX family it is absolute socket filename.
  * @param family The address family to use, or APR_UNSPEC if the system should 
  *               decide.
  * @param port The port number.
@@ -377,6 +405,7 @@
                                           apr_int32_t flags,
                                           apr_pool_t *p);
 
+
 /**
  * Look up the host name from an apr_sockaddr_t.
  * @param hostname The hostname.
@@ -769,6 +798,11 @@
 APR_DECLARE_INHERIT_UNSET(socket);
 
 /**
+ * Set socket permissions.
+ */
+APR_PERMS_SET_IMPLEMENT(global_mutex);
+
+/**
  * @defgroup apr_mcast IP Multicast
  * @{
  */

Modified: apr/apr/trunk/include/arch/unix/apr_arch_networkio.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/arch/unix/apr_arch_networkio.h?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/include/arch/unix/apr_arch_networkio.h (original)
+++ apr/apr/trunk/include/arch/unix/apr_arch_networkio.h Thu Feb 19 07:15:23 2009
@@ -111,6 +111,9 @@
 #ifndef HAVE_POLL
     int connected;
 #endif
+#if APR_HAVE_SOCKADDR_UN
+    int bound;
+#endif
     int local_port_unknown;
     int local_interface_unknown;
     int remote_addr_unknown;

Modified: apr/apr/trunk/network_io/unix/sockaddr.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockaddr.c?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockaddr.c (original)
+++ apr/apr/trunk/network_io/unix/sockaddr.c Thu Feb 19 07:15:23 2009
@@ -153,6 +153,14 @@
         addr->ipaddr_len = sizeof(struct in6_addr);
     }
 #endif
+#if APR_HAVE_SOCKADDR_UN
+    else if (family == APR_UNIX) {
+        addr->salen = sizeof(struct sockaddr_un);
+        addr->addr_str_len = sizeof(addr->sa.unx.sun_path);;
+        addr->ipaddr_ptr = &(addr->sa.unx.sun_path);
+        addr->ipaddr_len = addr->addr_str_len;
+    }
+#endif
 }
 
 APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
@@ -564,6 +572,32 @@
         }
 #endif
     }
+    if (family == APR_UNSPEC && hostname && *hostname == '/')
+        family = APR_UNIX;
+    if (family == APR_UNIX) {
+#if APR_HAVE_SOCKADDR_UN
+        if (hostname && *hostname == '/') {
+            *sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
+            (*sa)->pool = p;
+            apr_cpystrn((*sa)->sa.unx.sun_path, hostname,
+                        sizeof((*sa)->sa.unx.sun_path));
+            (*sa)->hostname = apr_pstrdup(p, hostname);
+            (*sa)->family = APR_UNIX;
+            (*sa)->sa.unx.sun_family = APR_UNIX;
+            (*sa)->salen = sizeof(struct sockaddr_un);
+            (*sa)->addr_str_len = sizeof((*sa)->sa.unx.sun_path);
+            (*sa)->ipaddr_ptr = &((*sa)->sa.unx.sun_path);
+            (*sa)->ipaddr_len = (*sa)->addr_str_len;
+
+            return APR_SUCCESS;
+        }
+        else
+#endif
+        {
+            *sa = NULL;
+            return APR_ENOTIMPL;
+        }
+    }
 #if !APR_HAVE_IPV6
     /* What may happen is that APR is not IPv6-enabled, but we're still
      * going to call getaddrinfo(), so we have to tell the OS we only
@@ -616,6 +650,12 @@
                          tmphostname, sizeof(tmphostname), NULL, 0,
                          flags != 0 ? flags : NI_NAMEREQD);
     }
+#if APR_HAVE_SOCKADDR_UN
+    else if (sockaddr->family == APR_UNIX) {
+        *hostname = sockaddr->hostname;
+        return APR_SUCCESS;
+    }
+#endif
     else
 #endif
     rc = getnameinfo((const struct sockaddr *)&sockaddr->sa, sockaddr->salen,

Modified: apr/apr/trunk/network_io/unix/sockets.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockets.c?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockets.c (original)
+++ apr/apr/trunk/network_io/unix/sockets.c Thu Feb 19 07:15:23 2009
@@ -26,12 +26,37 @@
 #define close closesocket
 #endif /* BEOS_R5 */
 
-static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */
+#if APR_HAVE_SOCKADDR_UN
+#define GENERIC_INADDR_ANY_LEN  sizeof(struct sockaddr_un)
+#else
+#define GENERIC_INADDR_ANY_LEN  16
+#endif
+
+/* big enough for IPv4, IPv6 and optionaly sun_path */
+static char generic_inaddr_any[GENERIC_INADDR_ANY_LEN] = {0};
 
 static apr_status_t socket_cleanup(void *sock)
 {
     apr_socket_t *thesocket = sock;
 
+#if APR_HAVE_SOCKADDR_UN
+    if (thesocket->bound && thesocket->local_addr->family == APR_UNIX) {
+        /* XXX: Check for return values ? */
+        unlink(thesocket->local_addr->hostname);
+    }
+#endif        
+    if (close(thesocket->socketdes) == 0) {
+        thesocket->socketdes = -1;
+        return APR_SUCCESS;
+    }
+    else {
+        return errno;
+    }
+}
+
+static apr_status_t socket_child_cleanup(void *sock)
+{
+    apr_socket_t *thesocket = sock;
     if (close(thesocket->socketdes) == 0) {
         thesocket->socketdes = -1;
         return APR_SUCCESS;
@@ -84,6 +109,7 @@
                                int protocol, apr_pool_t *cont)
 {
     int family = ofamily;
+    int oprotocol = protocol;
 
     if (family == APR_UNSPEC) {
 #if APR_HAVE_IPV6
@@ -92,7 +118,11 @@
         family = APR_INET;
 #endif
     }
-
+#if APR_HAVE_SOCKADDR_UN
+    if (family == APR_UNIX) {
+        protocol = 0;
+    }
+#endif
     alloc_socket(new, cont);
 
 #ifndef BEOS_R5
@@ -128,12 +158,12 @@
     if ((*new)->socketdes < 0) {
         return errno;
     }
-    set_socket_vars(*new, family, type, protocol);
+    set_socket_vars(*new, family, type, oprotocol);
 
     (*new)->timeout = -1;
     (*new)->inherit = 0;
     apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup,
-                              socket_cleanup);
+                              socket_child_cleanup);
 
     return APR_SUCCESS;
 } 
@@ -158,6 +188,13 @@
     else {
         sock->local_addr = sa;
         /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
+#if APR_HAVE_SOCKADDR_UN
+        if (sock->local_addr->family == APR_UNIX) {
+            sock->bound = 1;
+            sock->local_port_unknown = 1;
+        }
+        else
+#endif
         if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing
w/ 0 */
             sock->local_port_unknown = 1; /* kernel got us an ephemeral port */
         }
@@ -225,6 +262,14 @@
         (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr;
     }
 #endif
+#if APR_HAVE_SOCKADDR_UN
+    else if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
+        *(*new)->remote_addr = *sock->local_addr;
+        (*new)->local_addr->ipaddr_ptr = &((*new)->local_addr->sa.unx.sun_path);
+        (*new)->remote_addr->ipaddr_ptr = &((*new)->remote_addr->sa.unx.sun_path);
+    }
+    if (sock->local_addr->sa.sin.sin_family != AF_UNIX)
+#endif
     (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
     if (sock->local_port_unknown) {
         /* not likely for a listening socket, but theoretically possible :) */
@@ -299,7 +344,6 @@
     if (rc == -1 && errno != EISCONN) {
         return errno;
     }
-
     if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) {
         /* A real remote address was passed in.  If the unspecified
          * address was used, the actual remote addr will have to be
@@ -314,6 +358,13 @@
         /* connect() got us an ephemeral port */
         sock->local_port_unknown = 1;
     }
+#if APR_HAVE_SOCKADDR_UN
+    if (sock->local_addr->sa.sin.sin_family == AF_UNIX) {
+        /* Assign connect address as local. */
+        sock->local_addr = sa;
+    }
+    else
+#endif
     if (!memcmp(sock->local_addr->ipaddr_ptr,
                 generic_inaddr_any,
                 sock->local_addr->ipaddr_len)) {

Modified: apr/apr/trunk/network_io/unix/sockopt.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockopt.c?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockopt.c (original)
+++ apr/apr/trunk/network_io/unix/sockopt.c Thu Feb 19 07:15:23 2009
@@ -395,3 +395,24 @@
     return APR_SUCCESS;
 }
 #endif
+
+APR_PERMS_SET_IMPLEMENT(socket)
+{
+#if APR_HAVE_SOCKADDR_UN
+    apr_status_t rv = APR_SUCCESS;
+    apr_socket_t *socket = (apr_socket_t *)thesocket;
+
+    if (socket->local_addr->family == APR_UNIX) {
+        if (!(perms & APR_FPROT_GSETID))
+            gid = -1;
+        if (fchown(socket->socketdes, uid, gid) < 0) {
+            rv = errno;
+        }
+    }
+    else
+        rv = APR_EINVAL;
+    return rv;
+#else
+    return APR_ENOTIMPL;
+#endif
+}

Modified: apr/apr/trunk/test/sockchild.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/sockchild.c?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/test/sockchild.c (original)
+++ apr/apr/trunk/test/sockchild.c Thu Feb 19 07:15:23 2009
@@ -30,11 +30,11 @@
     atexit(apr_terminate);
     apr_pool_create(&p, NULL);
 
-    if (argc < 2) {
+    if (argc < 3) {
         exit(-1);
     }
 
-    rv = apr_sockaddr_info_get(&remote_sa, "127.0.0.1", APR_UNSPEC, 8021, 0, p);
+    rv = apr_sockaddr_info_get(&remote_sa, argv[2], APR_UNSPEC, 8021, 0, p);
     if (rv != APR_SUCCESS) {
         exit(-1);
     }

Modified: apr/apr/trunk/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/testsock.c?rev=745763&r1=745762&r2=745763&view=diff
==============================================================================
--- apr/apr/trunk/test/testsock.c (original)
+++ apr/apr/trunk/test/testsock.c Thu Feb 19 07:15:23 2009
@@ -24,10 +24,15 @@
 #include "apr_strings.h"
 #include "apr_poll.h"
 
+#define UNIX_SOCKET_NAME    "/tmp/apr-socket"
+#define IPV4_SOCKET_NAME    "127.0.0.1"
+static char *socket_name = NULL;
+static int   socket_type = APR_INET;
+
 static void launch_child(abts_case *tc, apr_proc_t *proc, const char *arg1, apr_pool_t *p)
 {
     apr_procattr_t *procattr;
-    const char *args[3];
+    const char *args[4];
     apr_status_t rv;
 
     rv = apr_procattr_create(&procattr, p);
@@ -42,7 +47,8 @@
 
     args[0] = "sockchild" EXTENSION;
     args[1] = arg1;
-    args[2] = NULL;
+    args[2] = socket_name;
+    args[3] = NULL;
     rv = apr_proc_create(proc, TESTBINPATH "sockchild" EXTENSION, args, NULL,
                          procattr, p);
     APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
@@ -99,7 +105,7 @@
     apr_sockaddr_t *sa;
     apr_socket_t *sock;
 
-    rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_INET, 8021, 0, p);
+    rv = apr_sockaddr_info_get(&sa, socket_name, socket_type, 8021, 0, p);
     APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
 
     rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, APR_PROTO_TCP, p);
@@ -336,7 +342,7 @@
 abts_suite *testsock(abts_suite *suite)
 {
     suite = ADD_SUITE(suite)
-
+    socket_name = IPV4_SOCKET_NAME;
     abts_run_test(suite, test_addr_info, NULL);
     abts_run_test(suite, test_serv_by_name, NULL);
     abts_run_test(suite, test_create_bind_listen, NULL);
@@ -345,7 +351,14 @@
     abts_run_test(suite, test_timeout, NULL);
     abts_run_test(suite, test_print_addr, NULL);
     abts_run_test(suite, test_get_addr, NULL);
-
+#if APR_HAVE_SOCKADDR_UN
+    socket_name = UNIX_SOCKET_NAME;
+    socket_type = APR_UNIX;
+    abts_run_test(suite, test_create_bind_listen, NULL);
+    abts_run_test(suite, test_send, NULL);
+    abts_run_test(suite, test_recv, NULL);
+    abts_run_test(suite, test_timeout, NULL);
+#endif
     return suite;
 }
 



Mime
View raw message