apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From traw...@apache.org
Subject svn commit: r1541061 - in /apr/apr/trunk: include/apr_network_io.h network_io/unix/sockaddr.c test/testsock.c
Date Tue, 12 Nov 2013 13:48:16 GMT
Author: trawick
Date: Tue Nov 12 13:48:16 2013
New Revision: 1541061

URL: http://svn.apache.org/r1541061
Log:
add apr_sockaddr_is_wildcard() API

Modified:
    apr/apr/trunk/include/apr_network_io.h
    apr/apr/trunk/network_io/unix/sockaddr.c
    apr/apr/trunk/test/testsock.c

Modified: apr/apr/trunk/include/apr_network_io.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_network_io.h?rev=1541061&r1=1541060&r2=1541061&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_network_io.h (original)
+++ apr/apr/trunk/include/apr_network_io.h Tue Nov 12 13:48:16 2013
@@ -760,6 +760,16 @@ APR_DECLARE(int) apr_sockaddr_equal(cons
                                     const apr_sockaddr_t *addr2);
 
 /**
+ * See if the IP address in an APR socket address refers to the wildcard
+ * address for the protocol family (e.g., INADDR_ANY for IPv4).
+ *
+ * @param addr The APR socket address to examine.
+ * @remark The return value will be non-zero if the address is
+ * initialized and is the wildcard address.
+ */
+APR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr);
+
+/**
 * Return the type of the socket.
 * @param sock The socket to query.
 * @param type The returned type (e.g., SOCK_STREAM).

Modified: apr/apr/trunk/network_io/unix/sockaddr.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockaddr.c?rev=1541061&r1=1541060&r2=1541061&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockaddr.c (original)
+++ apr/apr/trunk/network_io/unix/sockaddr.c Tue Nov 12 13:48:16 2013
@@ -879,6 +879,35 @@ APR_DECLARE(int) apr_sockaddr_equal(cons
     return 0; /* not equal */
 }
 
+APR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr)
+{
+    static const char inaddr_any[
+#if APR_HAVE_IPV6
+        sizeof(struct in6_addr)
+#else
+        sizeof(struct in_addr)
+#endif
+    ] = {0};
+
+    if (addr->ipaddr_ptr /* IP address initialized */
+        && addr->ipaddr_len <= sizeof inaddr_any) { /* else bug elsewhere?
*/
+        if (!memcmp(inaddr_any, addr->ipaddr_ptr, addr->ipaddr_len)) {
+            return 1;
+        }
+#if APR_HAVE_IPV6
+    if (addr->family == AF_INET6
+        && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr->ipaddr_ptr)) {
+        struct in_addr *v4 = (struct in_addr *)&((apr_uint32_t *)addr->ipaddr_ptr)[3];
+
+        if (!memcmp(inaddr_any, v4, sizeof *v4)) {
+            return 1;
+        }
+    }
+#endif
+    }
+    return 0;
+}
+
 static apr_status_t parse_network(apr_ipsubnet_t *ipsub, const char *network)
 {
     /* legacy syntax for ip addrs: a.b.c. ==> a.b.c.0/24 for example */

Modified: apr/apr/trunk/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/testsock.c?rev=1541061&r1=1541060&r2=1541061&view=diff
==============================================================================
--- apr/apr/trunk/test/testsock.c (original)
+++ apr/apr/trunk/test/testsock.c Tue Nov 12 13:48:16 2013
@@ -73,14 +73,21 @@ static void test_addr_info(abts_case *tc
 {
     apr_status_t rv;
     apr_sockaddr_t *sa;
+    int rc;
 
     rv = apr_sockaddr_info_get(&sa, NULL, APR_UNSPEC, 80, 0, p);
     APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
 
+    rc = apr_sockaddr_is_wildcard(sa);
+    ABTS_INT_NEQUAL(tc, 0, rc);
+
     rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_UNSPEC, 80, 0, p);
     APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
     ABTS_STR_EQUAL(tc, "127.0.0.1", sa->hostname);
 
+    rc = apr_sockaddr_is_wildcard(sa);
+    ABTS_INT_EQUAL(tc, 0, rc);
+
     rv = apr_sockaddr_info_get(&sa, "127.0.0.1", APR_UNSPEC, 0, 0, p);
     APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
     ABTS_STR_EQUAL(tc, "127.0.0.1", sa->hostname);
@@ -324,6 +331,10 @@ static void test_print_addr(abts_case *t
     if (rv == APR_SUCCESS && sa) {
         /* sa should now be a v4-mapped IPv6 address. */
         char buf[128];
+        int rc;
+
+        rc = apr_sockaddr_is_wildcard(sa);
+        ABTS_INT_NEQUAL(tc, 0, rc);
 
         memset(buf, 'z', sizeof buf);
         



Mime
View raw message