]> granicus.if.org Git - php/commitdiff
Implements feature Bug #63472 ability to set SO_BINDTODEVICE on socket.
authorDamjan Cvetko <damjan.cvetko@gmail.com>
Mon, 10 Jun 2013 10:11:14 +0000 (12:11 +0200)
committerStanislav Malyshev <stas@php.net>
Sun, 23 Jun 2013 22:20:54 +0000 (15:20 -0700)
NEWS
ext/sockets/sockets.c
ext/sockets/tests/socket_set_option_bindtodevice.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index be774f84f75f42d3bc32cd91a046b65c3747219e..5c6f6b92be85597a95f097e7e17e568a95739be9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ PHP                                                                        NEWS
   . Fixed bug #65066 (Cli server not responsive when responding with 422 http
     status code). (Adam)
 
+- Sockets:
+  . Implemented FR #63472 (Setting SO_BINDTODEVICE with socket_set_option). 
+    (Damjan Cvetko)
+
 ?? ??? 2013, PHP 5.4.17
 
 - Core:
index b3bcbf26005bacc79040dc46a6c3d1dc7c43c714..0c0380861ace46554a8dde6e23bcc48614b4ba3a 100644 (file)
@@ -843,6 +843,9 @@ PHP_MINIT_FUNCTION(sockets)
        REGISTER_LONG_CONSTANT("SO_RCVTIMEO",   SO_RCVTIMEO,    CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("SO_TYPE",               SO_TYPE,                CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("SO_ERROR",              SO_ERROR,               CONST_CS | CONST_PERSISTENT);
+#ifdef SO_BINDTODEVICE
+       REGISTER_LONG_CONSTANT("SO_BINDTODEVICE",       SO_BINDTODEVICE,        CONST_CS | CONST_PERSISTENT);
+#endif
        REGISTER_LONG_CONSTANT("SOL_SOCKET",    SOL_SOCKET,             CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("SOMAXCONN",             SOMAXCONN,              CONST_CS | CONST_PERSISTENT);
 #ifdef TCP_NODELAY
@@ -2355,7 +2358,19 @@ ipv6_loop_hops:
 #endif
                        break;
                }
-               
+#ifdef SO_BINDTODEVICE
+               case SO_BINDTODEVICE: {
+                       if (Z_TYPE_PP(arg4) == IS_STRING) {
+                               opt_ptr = Z_STRVAL_PP(arg4);
+                               optlen = Z_STRLEN_PP(arg4);
+                       } else {
+                               opt_ptr = "";
+                               optlen = 0;
+                       }
+                       break;
+               }
+#endif
+
                default:
                        convert_to_long_ex(arg4);
                        ov = Z_LVAL_PP(arg4);
diff --git a/ext/sockets/tests/socket_set_option_bindtodevice.phpt b/ext/sockets/tests/socket_set_option_bindtodevice.phpt
new file mode 100644 (file)
index 0000000..05d718c
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+Test if socket_set_option() works, option:SO_BINDTODEVICE
+--DESCRIPTION--
+-Bind to loopback 'lo' device (should exist)
+-Bind to unexisting device
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+        die('SKIP sockets extension not available.');
+}
+if (!defined("SO_BINDTODEVICE")) {
+       die('SKIP SO_BINDTODEVICE not supported on this platform.');
+}
+if (!function_exists("posix_getuid") || posix_getuid() != 0) {
+       die('SKIP SO_BINDTODEVICE requires root permissions.');
+}
+?>
+--FILE--
+<?php
+$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+if (!$socket) {
+        die('Unable to create AF_INET socket [socket]');
+}
+// wrong params
+$retval_1 = socket_set_option( $socket, SOL_SOCKET, SO_BINDTODEVICE, "lo");
+var_dump($retval_1);
+$retval_2 = socket_set_option( $socket, SOL_SOCKET, SO_BINDTODEVICE, "ethIDONOTEXIST");
+var_dump($retval_2);
+
+socket_close($socket);
+?>
+
+--EXPECTF--
+bool(true)
+
+Warning: socket_set_option(): unable to set socket option [19]: No such device in %s on line %d
+bool(false)
+--CREDITS--
+Damjan Cvetko, foreach.org