apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jor...@apache.org
Subject svn commit: r279566 - in /apr/apr/trunk: CHANGES include/apr_network_io.h network_io/unix/sockaddr.c strings/apr_snprintf.c test/testsock.c
Date Thu, 08 Sep 2005 14:05:47 GMT
Author: jorton
Date: Thu Sep  8 07:05:40 2005
New Revision: 279566

URL: http://svn.apache.org/viewcvs?rev=279566&view=rev
Log:
Fix handling of %pI with psprintf; the psprintf implementation assumes
that the vformatter code will not palloc.

* include/apr_network_io.h (apr_sockaddr_ip_getbuf): Declare new
function.

* network_io/unix/sockaddr.c (apr_sockaddr_ip_getbuf): Rewrite of
apr_sockaddr_ip_get, taking a buffer argument.
(apr_sockaddr_ip_get): Reimplement using apr_sockaddr_ip_getbuf.

* strings/apr_snprintf.c (conv_apr_sockaddr): Use
apr_sockaddr_ip_getbuf to avoid use of pool.

* test/testsock.c (test_print_addr): New test case.

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

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewcvs/apr/apr/trunk/CHANGES?rev=279566&r1=279565&r2=279566&view=diff
==============================================================================
--- apr/apr/trunk/CHANGES (original)
+++ apr/apr/trunk/CHANGES Thu Sep  8 07:05:40 2005
@@ -1,5 +1,9 @@
 Changes for APR 1.3.0
 
+  *) Add apr_sockaddr_ip_getbuf() function. [Joe Orton]
+
+  *) Fix handling of %pI in apr_psprintf. [Joe Orton]
+
   *) Provide APR_VERSION_AT_LEAST() macro for applications which 
      want to enable features based on a required level of APR.
      [Jeff Trawick]

Modified: apr/apr/trunk/include/apr_network_io.h
URL: http://svn.apache.org/viewcvs/apr/apr/trunk/include/apr_network_io.h?rev=279566&r1=279565&r2=279566&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_network_io.h (original)
+++ apr/apr/trunk/include/apr_network_io.h Thu Sep  8 07:05:40 2005
@@ -669,6 +669,15 @@
                                               apr_sockaddr_t *sockaddr);
 
 /**
+ * Write the IP address (in numeric address string format) of the APR
+ * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
+ * @param addr The IP address.
+ * @param sockaddr The socket address to reference.
+ */
+APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
+                                                 apr_sockaddr_t *sockaddr);
+
+/**
  * See if the IP addresses in two APR socket addresses are
  * equivalent.  Appropriate logic is present for comparing
  * IPv4-mapped IPv6 addresses with IPv4 addresses.

Modified: apr/apr/trunk/network_io/unix/sockaddr.c
URL: http://svn.apache.org/viewcvs/apr/apr/trunk/network_io/unix/sockaddr.c?rev=279566&r1=279565&r2=279566&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockaddr.c (original)
+++ apr/apr/trunk/network_io/unix/sockaddr.c Thu Sep  8 07:05:40 2005
@@ -98,25 +98,35 @@
     }
 }
 
-APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
-                                         apr_sockaddr_t *sockaddr)
+APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
+                                                 apr_sockaddr_t *sockaddr)
 {
-    *addr = apr_palloc(sockaddr->pool, sockaddr->addr_str_len);
-    apr_inet_ntop(sockaddr->family,
-                  sockaddr->ipaddr_ptr,
-                  *addr,
-                  sockaddr->addr_str_len);
+    if (!apr_inet_ntop(sockaddr->family, sockaddr->ipaddr_ptr, buf, buflen)) {
+        return APR_ENOSPC;
+    }
+
 #if APR_HAVE_IPV6
-    if (sockaddr->family == AF_INET6 &&
-        IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sockaddr->ipaddr_ptr)) {
+    if (sockaddr->family == AF_INET6 
+        && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sockaddr->ipaddr_ptr)
+        && buflen > strlen("::ffff:")) {
         /* This is an IPv4-mapped IPv6 address; drop the leading
          * part of the address string so we're left with the familiar
          * IPv4 format.
          */
-        *addr += strlen("::ffff:");
+        memmove(buf, buf + strlen("::ffff:"),
+                strlen(buf + strlen("::ffff:")));
     }
 #endif
+    /* ensure NUL termination if the buffer is too short */
+    buf[buflen-1] = '\0';
     return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
+                                              apr_sockaddr_t *sockaddr)
+{
+    *addr = apr_palloc(sockaddr->pool, sockaddr->addr_str_len);
+    return apr_sockaddr_ip_getbuf(*addr, sockaddr->addr_str_len, sockaddr);
 }
 
 void apr_sockaddr_vars_set(apr_sockaddr_t *addr, int family, apr_port_t port)

Modified: apr/apr/trunk/strings/apr_snprintf.c
URL: http://svn.apache.org/viewcvs/apr/apr/trunk/strings/apr_snprintf.c?rev=279566&r1=279565&r2=279566&view=diff
==============================================================================
--- apr/apr/trunk/strings/apr_snprintf.c (original)
+++ apr/apr/trunk/strings/apr_snprintf.c Thu Sep  8 07:05:40 2005
@@ -464,7 +464,8 @@
 }
 
 
-
+/* Must be passed a buffer of size NUM_BUF_SIZE where buf_end points
+ * to 1 byte past the end of the buffer. */
 static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
 {
     char *p = buf_end;
@@ -474,7 +475,14 @@
 
     p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
     *--p = ':';
-    apr_sockaddr_ip_get(&ipaddr_str, sa);
+    ipaddr_str = buf_end - NUM_BUF_SIZE;
+    if (apr_sockaddr_ip_getbuf(ipaddr_str, sa->addr_str_len, sa)) {
+        /* Should only fail if the buffer is too small, which it
+         * should not be; but fail safe anyway: */
+        *--p = '?';
+        *len = buf_end - p;
+        return p;
+    }
     sub_len = strlen(ipaddr_str);
 #if APR_HAVE_IPV6
     if (sa->family == APR_INET6 &&

Modified: apr/apr/trunk/test/testsock.c
URL: http://svn.apache.org/viewcvs/apr/apr/trunk/test/testsock.c?rev=279566&r1=279565&r2=279566&view=diff
==============================================================================
--- apr/apr/trunk/test/testsock.c (original)
+++ apr/apr/trunk/test/testsock.c Thu Sep  8 07:05:40 2005
@@ -203,6 +203,19 @@
     APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
 }
 
+static void test_print_addr(abts_case *tc, void *data)
+{
+    apr_sockaddr_t *sa;
+    char *s;
+
+    APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr",
+                       apr_sockaddr_info_get(&sa, "0.0.0.0", APR_INET, 80, 0, p));
+
+    s = apr_psprintf(p, "foo %pI bar", sa);
+
+    ABTS_STR_EQUAL(tc, "foo 0.0.0.0:80 bar", s);
+}
+
 abts_suite *testsock(abts_suite *suite)
 {
     suite = ADD_SUITE(suite)
@@ -212,6 +225,7 @@
     abts_run_test(suite, test_send, NULL);
     abts_run_test(suite, test_recv, NULL);
     abts_run_test(suite, test_timeout, NULL);
+    abts_run_test(suite, test_print_addr, NULL);
 
     return suite;
 }



Mime
View raw message