apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r1590729 - in /apr/apr/branches/1.6.x: ./ CHANGES include/apr_network_io.h network_io/unix/sockaddr.c test/testsock.c
Date Mon, 28 Apr 2014 18:12:01 GMT
Author: jim
Date: Mon Apr 28 18:12:01 2014
New Revision: 1590729

URL: http://svn.apache.org/r1590729
Log:
Merge r1587045, r1587543 from trunk:

Add apr_sockaddr_info_copy(), for making a deep copy of an
apr_sockaddr_t into a specified pool.

Submitted by: Yann Ylavic <ylavic.dev gmail.com>
Reviewed by: trawick


Add testcase for apr_sockaddr_info_copy()

Submitted by: Yann Ylavic <ylavic.dev gmail.com>
Reviewed by: trawick

Submitted by: trawick
Reviewed/backported by: jim

Modified:
    apr/apr/branches/1.6.x/   (props changed)
    apr/apr/branches/1.6.x/CHANGES
    apr/apr/branches/1.6.x/include/apr_network_io.h
    apr/apr/branches/1.6.x/network_io/unix/sockaddr.c
    apr/apr/branches/1.6.x/test/testsock.c

Propchange: apr/apr/branches/1.6.x/
------------------------------------------------------------------------------
  Merged /apr/apr/trunk:r1587045,1587543

Modified: apr/apr/branches/1.6.x/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/CHANGES?rev=1590729&r1=1590728&r2=1590729&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/CHANGES [utf-8] (original)
+++ apr/apr/branches/1.6.x/CHANGES [utf-8] Mon Apr 28 18:12:01 2014
@@ -1,6 +1,10 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 1.6.0
 
+  *) Add apr_sockaddr_info_copy(), for making a deep copy of an
+     apr_sockaddr_t into a specified pool.  [Yann Ylavic
+     <ylavic.dev gmail.com>]
+
   *) Added Unix domain socket support.
      [Mladen Turk]
 

Modified: apr/apr/branches/1.6.x/include/apr_network_io.h
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/include/apr_network_io.h?rev=1590729&r1=1590728&r2=1590729&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/include/apr_network_io.h (original)
+++ apr/apr/branches/1.6.x/include/apr_network_io.h Mon Apr 28 18:12:01 2014
@@ -429,6 +429,16 @@ APR_DECLARE(apr_status_t) apr_sockaddr_i
                                           apr_pool_t *p);
 
 /**
+ * Copy apr_sockaddr_t src to dst on pool p.
+ * @param dst The destination apr_sockaddr_t.
+ * @param src The source apr_sockaddr_t.
+ * @param p The pool for the apr_sockaddr_t and associated storage.
+ */
+APR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst,
+                                                 const apr_sockaddr_t *src,
+                                                 apr_pool_t *p);
+
+/**
  * Look up the host name from an apr_sockaddr_t.
  * @param hostname The hostname.
  * @param sa The apr_sockaddr_t.

Modified: apr/apr/branches/1.6.x/network_io/unix/sockaddr.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/network_io/unix/sockaddr.c?rev=1590729&r1=1590728&r2=1590729&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/network_io/unix/sockaddr.c (original)
+++ apr/apr/branches/1.6.x/network_io/unix/sockaddr.c Mon Apr 28 18:12:01 2014
@@ -661,6 +661,42 @@ APR_DECLARE(apr_status_t) apr_sockaddr_i
     return find_addresses(sa, hostname, family, port, flags, p);
 }
 
+APR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst,
+                                                 const apr_sockaddr_t *src,
+                                                 apr_pool_t *p)
+{
+    apr_sockaddr_t *d;
+    const apr_sockaddr_t *s;
+
+    for (*dst = d = NULL, s = src; s; s = s->next) {
+        if (!d) {
+            *dst = d = apr_pmemdup(p, s, sizeof *s);
+        }
+        else {
+            d = d->next = apr_pmemdup(p, s, sizeof *s);
+        }
+        if (s->hostname) {
+            if (s == src || s->hostname != src->hostname) {
+                d->hostname = apr_pstrdup(p, s->hostname);
+            }
+            else {
+                d->hostname = (*dst)->hostname;
+            }
+        }
+        if (s->servname) {
+            if (s == src || s->servname != src->servname) {
+                d->servname = apr_pstrdup(p, s->servname);
+            }
+            else {
+                d->servname = (*dst)->servname;
+            }
+        }
+        d->pool = p;
+        apr_sockaddr_vars_set(d, s->family, s->port);
+    }
+    return APR_SUCCESS;
+}
+
 APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
                                           apr_sockaddr_t *sockaddr,
                                           apr_int32_t flags)

Modified: apr/apr/branches/1.6.x/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/test/testsock.c?rev=1590729&r1=1590728&r2=1590729&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/test/testsock.c (original)
+++ apr/apr/branches/1.6.x/test/testsock.c Mon Apr 28 18:12:01 2014
@@ -97,6 +97,56 @@ static void test_addr_info(abts_case *tc
     ABTS_INT_EQUAL(tc, 0, ntohs(sa->sa.sin.sin_port));
 }
 
+static void test_addr_copy(abts_case *tc, void *data)
+{
+    apr_status_t rv;
+    apr_sockaddr_t *sa1, *sa2;
+    int rc;
+    const char *hosts[] = {
+        "127.0.0.1",
+#if APR_HAVE_IPV6
+        "::1",
+#endif
+        NULL
+    }, **host = hosts;
+
+    /* Loop up to and including NULL */
+    do {
+        rv = apr_sockaddr_info_get(&sa1, *host, APR_UNSPEC, 80, 0, p);
+        APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
+
+        rv = apr_sockaddr_info_copy(&sa2, sa1, p);
+        APR_ASSERT_SUCCESS(tc, "Problem copying sockaddr", rv);
+
+        ABTS_PTR_NOTNULL(tc, sa1);
+        do {
+            ABTS_PTR_NOTNULL(tc, sa2);
+
+            rc = apr_sockaddr_equal(sa2, sa1);
+            ABTS_INT_NEQUAL(tc, 0, rc);
+            ABTS_INT_EQUAL(tc, 80, sa1->port);
+            ABTS_INT_EQUAL(tc, sa2->port, sa1->port);
+            ABTS_INT_EQUAL(tc, 80, ntohs(sa1->sa.sin.sin_port));
+            ABTS_INT_EQUAL(tc, ntohs(sa2->sa.sin.sin_port), ntohs(sa1->sa.sin.sin_port));
+
+            if (*host) {
+                ABTS_PTR_NOTNULL(tc, sa1->hostname);
+                ABTS_PTR_NOTNULL(tc, sa2->hostname);
+                ABTS_STR_EQUAL(tc, *host, sa1->hostname);
+                ABTS_STR_EQUAL(tc, sa1->hostname, sa2->hostname);
+                ABTS_TRUE(tc, sa1->hostname != sa2->hostname);
+            }
+            else {
+                ABTS_PTR_EQUAL(tc, NULL, sa1->hostname);
+                ABTS_PTR_EQUAL(tc, NULL, sa2->hostname);
+            }
+
+        } while ((sa2 = sa2->next, sa1 = sa1->next));
+        ABTS_PTR_EQUAL(tc, NULL, sa2);
+
+    } while (*host++);
+}
+
 static void test_serv_by_name(abts_case *tc, void *data)
 {
     apr_status_t rv;
@@ -499,6 +549,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_addr_copy, NULL);
     abts_run_test(suite, test_serv_by_name, NULL);
     abts_run_test(suite, test_create_bind_listen, NULL);
     abts_run_test(suite, test_send, NULL);



Mime
View raw message