You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by jo...@apache.org on 2016/03/03 13:00:20 UTC

svn commit: r1733451 - in /apr/apr/trunk: include/apr_network_io.h network_io/unix/sockopt.c test/testsock.c

Author: jorton
Date: Thu Mar  3 12:00:20 2016
New Revision: 1733451

URL: http://svn.apache.org/viewvc?rev=1733451&view=rev
Log:
* include/apr_network_io.h (APR_SO_FREEBIND): Add option.

* network_io/unix/sockopt.c (apr_socket_opt_set): Implement
  APR_SO_FREEBIND on Linux with IP_FREEBIND

* test/testsock.c (test_freebind): Add test case.

Submitted by: Ashley GC, jkaluza, jorton

Modified:
    apr/apr/trunk/include/apr_network_io.h
    apr/apr/trunk/network_io/unix/sockopt.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=1733451&r1=1733450&r2=1733451&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_network_io.h (original)
+++ apr/apr/trunk/include/apr_network_io.h Thu Mar  3 12:00:20 2016
@@ -105,6 +105,9 @@ extern "C" {
                                     */
 #define APR_SO_BROADCAST     65536 /**< Allow broadcast
                                     */
+#define APR_SO_FREEBIND     131072 /**< Allow binding to addresses not owned
+                                    * by any interface
+                                    */
 
 /** @} */
 

Modified: apr/apr/trunk/network_io/unix/sockopt.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockopt.c?rev=1733451&r1=1733450&r2=1733451&view=diff
==============================================================================
--- apr/apr/trunk/network_io/unix/sockopt.c (original)
+++ apr/apr/trunk/network_io/unix/sockopt.c Thu Mar  3 12:00:20 2016
@@ -330,6 +330,20 @@ apr_status_t apr_socket_opt_set(apr_sock
         return APR_ENOTIMPL;
 #endif
         break;
+    case APR_SO_FREEBIND:
+#if defined(IP_FREEBIND)
+        if (setsockopt(sock->socketdes, SOL_IP, IP_FREEBIND,
+                       (void *)&one, sizeof(int)) == -1) {
+            return errno;
+        }
+        apr_set_option(sock, APR_SO_FREEBIND, on);
+#elif defined(IP_BINDANY)
+        /* TODO: insert FreeBSD support here, note family specific
+         * options, IP_BINDANY vs IPV6_BINDANY */
+#else
+        return APR_ENOTIMPL;
+#endif
+        break;
     default:
         return APR_EINVAL;
     }

Modified: apr/apr/trunk/test/testsock.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/test/testsock.c?rev=1733451&r1=1733450&r2=1733451&view=diff
==============================================================================
--- apr/apr/trunk/test/testsock.c (original)
+++ apr/apr/trunk/test/testsock.c Thu Mar  3 12:00:20 2016
@@ -607,6 +607,39 @@ static void test_nonblock_inheritance(ab
     APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
 }
 
+static void test_freebind(abts_case *tc, void *data)
+{
+#ifdef IP_FREEBIND
+    apr_status_t rv;
+    apr_socket_t *sock;
+    apr_sockaddr_t *sa;
+    apr_int32_t on;
+    
+    /* RFC 5737 address */
+    rv = apr_sockaddr_info_get(&sa, "192.0.2.1", APR_INET, 8080, 0, p);
+    APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
+    
+    rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, APR_PROTO_TCP, p);
+    APR_ASSERT_SUCCESS(tc, "Problem creating socket", rv);
+
+    rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
+    APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
+
+    rv = apr_socket_opt_set(sock, APR_SO_FREEBIND, 1);
+    APR_ASSERT_SUCCESS(tc, "Could not enable FREEBIND option", rv);
+    
+    rv = apr_socket_opt_get(sock, APR_SO_FREEBIND, &on);
+    APR_ASSERT_SUCCESS(tc, "Could not retrieve FREEBIND option", rv);
+    ABTS_INT_EQUAL(tc, 1, on);
+    
+    rv = apr_socket_bind(sock, sa);
+    APR_ASSERT_SUCCESS(tc, "Problem binding to port with FREEBIND", rv);
+    
+    rv = apr_socket_close(sock);
+    APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv);
+#endif
+}
+
 abts_suite *testsock(abts_suite *suite)
 {
     suite = ADD_SUITE(suite)
@@ -623,6 +656,7 @@ abts_suite *testsock(abts_suite *suite)
     abts_run_test(suite, test_get_addr, NULL);
     abts_run_test(suite, test_wait, NULL);
     abts_run_test(suite, test_nonblock_inheritance, NULL);
+    abts_run_test(suite, test_freebind, NULL);
 #if APR_HAVE_SOCKADDR_UN
     socket_name = UNIX_SOCKET_NAME;
     socket_type = APR_UNIX;



Re: svn commit: r1733451 - in /apr/apr/trunk: include/apr_network_io.h network_io/unix/sockopt.c test/testsock.c

Posted by Joe Orton <jo...@redhat.com>.
On Thu, Mar 03, 2016 at 04:24:07PM +0100, Yann Ylavic wrote:
> Don't we want to return APR_ENOTIMPL here until the FreeBSD implementation?

Good point.  Done in r1733594 & r1733595

Re: svn commit: r1733451 - in /apr/apr/trunk: include/apr_network_io.h network_io/unix/sockopt.c test/testsock.c

Posted by Yann Ylavic <yl...@gmail.com>.
On Thu, Mar 3, 2016 at 1:00 PM,  <jo...@apache.org> wrote:
> Author: jorton
> Date: Thu Mar  3 12:00:20 2016
> New Revision: 1733451
>
> URL: http://svn.apache.org/viewvc?rev=1733451&view=rev
> Log:
> * include/apr_network_io.h (APR_SO_FREEBIND): Add option.
>
> * network_io/unix/sockopt.c (apr_socket_opt_set): Implement
>   APR_SO_FREEBIND on Linux with IP_FREEBIND
>
[]
>
> Modified: apr/apr/trunk/network_io/unix/sockopt.c
> URL: http://svn.apache.org/viewvc/apr/apr/trunk/network_io/unix/sockopt.c?rev=1733451&r1=1733450&r2=1733451&view=diff
> ==============================================================================
> --- apr/apr/trunk/network_io/unix/sockopt.c (original)
> +++ apr/apr/trunk/network_io/unix/sockopt.c Thu Mar  3 12:00:20 2016
> @@ -330,6 +330,20 @@ apr_status_t apr_socket_opt_set(apr_sock
>          return APR_ENOTIMPL;
>  #endif
>          break;
> +    case APR_SO_FREEBIND:
> +#if defined(IP_FREEBIND)
> +        if (setsockopt(sock->socketdes, SOL_IP, IP_FREEBIND,
> +                       (void *)&one, sizeof(int)) == -1) {
> +            return errno;
> +        }
> +        apr_set_option(sock, APR_SO_FREEBIND, on);
> +#elif defined(IP_BINDANY)
> +        /* TODO: insert FreeBSD support here, note family specific
> +         * options, IP_BINDANY vs IPV6_BINDANY */

Don't we want to return APR_ENOTIMPL here until the FreeBSD implementation?

> +#else
> +        return APR_ENOTIMPL;
> +#endif
> +        break;
>      default:
>          return APR_EINVAL;
>      }

Regards,
Yann.