Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ static PHP_MINIT_FUNCTION(sockets)
REGISTER_LONG_CONSTANT("SO_FAMILY", SO_FAMILY, CONST_CS | CONST_PERSISTENT);
#endif
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
Expand Down Expand Up @@ -2038,6 +2041,18 @@ PHP_FUNCTION(socket_set_option)
#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:
default_case:
Expand Down
37 changes: 37 additions & 0 deletions ext/sockets/tests/socket_set_option_bindtodevice.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test if socket_set_option() works, option:SO_BINDTODEVICE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would only work for root

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you need root for SO_BINDTODEVICE, that's it nature. Should I just skip the test if current context is not root? posix_getuid() != 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably.

--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.');
}
?>
--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 /root/zobo/php-sockets/php-src/ext/sockets/tests/socket_set_option_bindtodevice.php on line 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test shouldn't include your private paths, it will fail for everybody else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have used %s %d, will fix.

bool(false)
--CREDITS--
Damjan Cvetko, foreach.org