apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yann Ylavic <ylavic....@gmail.com>
Subject New apr_sockaddr_info_copy() function
Date Fri, 24 Jan 2014 00:24:47 GMT
Hi,

I'd like to propose the following/attached patch which adds the
apr_sockaddr_info_copy() function to network_io.

It can be useful when one wants an existing apr_sockaddr_t with a different
lifetime (pool) without a DNS lookup, ie. a redundant/costly call to
apr_sockaddr_info_get().

I hope it can be useful.

Regards,
Yann.


Index: include/apr_network_io.h
===================================================================
--- include/apr_network_io.h    (revision 1560855)
+++ include/apr_network_io.h    (working copy)
@@ -428,6 +428,15 @@ APR_DECLARE(apr_status_t) apr_sockaddr_info_get(ap
                                           apr_int32_t flags,
                                           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.
Index: network_io/unix/sockaddr.c
===================================================================
--- network_io/unix/sockaddr.c    (revision 1560855)
+++ network_io/unix/sockaddr.c    (working copy)
@@ -660,6 +660,41 @@ APR_DECLARE(apr_status_t) apr_sockaddr_info_get(ap
     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)
[EOS]

Mime
View raw message