Hi,
Think this is valuable addition.
It doesn't change the API except
new APR_UNIX socket family mode.
In case of APR_UNSPEC it forces the
AF_UNIX if hostname starts with '/'
Changes do not touch the existing code
except adding few compares for family == APR_UNIX
I've extended the test suite as well,
but tested only on linux and darwin.
One possible addition would be
automatic umask on bind() or using
the new APR_PERMS_SET macros.
Comments?
Regards
--
^(TM)
Index: test/sockchild.c
===================================================================
--- test/sockchild.c (revision 745502)
+++ test/sockchild.c (working copy)
@@ -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);
}
Index: test/testsock.c
===================================================================
--- test/testsock.c (revision 745502)
+++ test/testsock.c (working copy)
@@ -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;
}
Index: network_io/unix/sockaddr.c
===================================================================
--- network_io/unix/sockaddr.c (revision 745121)
+++ network_io/unix/sockaddr.c (working copy)
@@ -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,
Index: network_io/unix/sockets.c
===================================================================
--- network_io/unix/sockets.c (revision 745121)
+++ network_io/unix/sockets.c (working copy)
@@ -26,12 +26,25 @@
#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;
@@ -41,6 +54,18 @@
}
}
+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;
+ }
+ else {
+ return errno;
+ }
+}
+
static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol)
{
sock->type = type;
@@ -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)) {
Index: include/arch/unix/apr_arch_networkio.h
===================================================================
--- include/arch/unix/apr_arch_networkio.h (revision 745121)
+++ include/arch/unix/apr_arch_networkio.h (working copy)
@@ -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;
Index: include/apr.h.in
===================================================================
--- include/apr.h.in (revision 745121)
+++ include/apr.h.in (working copy)
@@ -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@
Index: include/apr.hw
===================================================================
--- include/apr.hw (revision 745121)
+++ include/apr.hw (working copy)
@@ -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
Index: include/apr_network_io.h
===================================================================
--- include/apr_network_io.h (revision 745121)
+++ include/apr_network_io.h (working copy)
@@ -30,6 +30,9 @@
#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 +157,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 +267,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 +378,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 +404,7 @@
apr_int32_t flags,
apr_pool_t *p);
+
/**
* Look up the host name from an apr_sockaddr_t.
* @param hostname The hostname.
Index: include/apr.hnw
===================================================================
--- include/apr.hnw (revision 745121)
+++ include/apr.hnw (working copy)
@@ -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
Index: configure.in
===================================================================
--- configure.in (revision 745127)
+++ configure.in (working copy)
@@ -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
Index: build/apr_network.m4
===================================================================
--- build/apr_network.m4 (revision 745121)
+++ build/apr_network.m4 (working copy)
@@ -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