Skip to content

Commit 3118ca3

Browse files
committed
changes from feedback
1 parent 233e9d7 commit 3118ca3

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

ext/ftp/ftp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ ftp_open(const char *host, short port, zend_long timeout_sec)
129129

130130
ftp->fd = php_network_connect_socket_to_host(host,
131131
(unsigned short) (port ? port : 21), SOCK_STREAM,
132-
0, &tv, NULL, NULL, NULL, 0, STREAM_SOCKOP_NONE, -1);
132+
0, &tv, NULL, NULL, NULL, 0, STREAM_SOCKOP_NONE, NULL);
133133
if (ftp->fd == -1) {
134134
goto bail;
135135
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
--TEST--
2-
Testing so_linger `socket` option.
2+
Testing linger `socket` option.
33
--SKIPIF--
44
<?php
5-
if (getenv("SKIP_ONLINE_TESTS")) die('skip online test');
5+
//if (getenv("SKIP_ONLINE_TESTS")) die('skip online test');
66
if (!in_array('https', stream_get_wrappers())) die('skip: https wrapper is required');
77
?>
88
--FILE--
99
<?php
10-
$context = stream_context_create(['socket' => ['so_linger' => false]]);
10+
$context = stream_context_create(['socket' => ['linger' => false]]);
1111
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
12-
$context = stream_context_create(['socket' => ['so_linger' => PHP_INT_MAX + 1]]);
12+
$context = stream_context_create(['socket' => ['linger' => PHP_INT_MAX + 1]]);
1313
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
14-
$context = stream_context_create(['socket' => ['so_linger' => 3]]);
14+
$context = stream_context_create(['socket' => ['linger' => 3]]);
1515
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
1616
?>
1717
--EXPECTF--
18-
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `so_linger` value in %s on line %d
18+
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `linger` value in %s on line %d
1919
bool(false)
2020

21-
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `so_linger` value in %s on line %d
21+
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `linger` value in %s on line %d
2222
bool(false)
2323
bool(true)

main/network.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static inline void sub_times(struct timeval a, struct timeval b, struct timeval
402402
* */
403403
/* {{{ php_network_bind_socket_to_local_addr */
404404
php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
405-
int socktype, long sockopts, long linger, zend_string **error_string, int *error_code
405+
int socktype, long sockopts, void *option, zend_string **error_string, int *error_code
406406
)
407407
{
408408
int num_addrs, n, err = 0;
@@ -472,6 +472,8 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po
472472
#endif
473473
#ifdef SO_LINGER
474474
if (sockopts & STREAM_SOCKOP_SO_LINGER) {
475+
ZEND_ASSERT(option != NULL);
476+
long linger = *(long *)option;
475477
struct linger val = {
476478
.l_onoff = (linger > 0),
477479
.l_linger = (int)linger
@@ -776,7 +778,7 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
776778
php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
777779
int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
778780
int *error_code, const char *bindto, unsigned short bindport, long sockopts,
779-
long linger
781+
void *option
780782
)
781783
{
782784
int num_addrs, n, fatal = 0;
@@ -910,6 +912,8 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
910912
#ifdef SO_LINGER
911913
{
912914
if (sockopts & STREAM_SOCKOP_SO_LINGER) {
915+
ZEND_ASSERT(option != NULL);
916+
long linger = *(long *)option;
913917
struct linger val = {
914918
.l_onoff = linger > 0,
915919
.l_linger = (int)linger

main/php_network.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
267267
PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
268268
int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
269269
int *error_code, const char *bindto, unsigned short bindport, long sockopts,
270-
long linger
270+
void *option
271271
);
272272

273273
PHPAPI int php_network_connect_socket(php_socket_t sockfd,
@@ -282,7 +282,7 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd,
282282
php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
283283

284284
PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
285-
int socktype, long sockopts, long linger, zend_string **error_string, int *error_code
285+
int socktype, long sockopts, void *option, zend_string **error_string, int *error_code
286286
);
287287

288288
PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,

main/streams/xp_socket.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
694694
int portno, err;
695695
long sockopts = STREAM_SOCKOP_NONE;
696696
long linger = -1;
697+
void *option = NULL;
697698
zval *tmpzval = NULL;
698699

699700
#ifdef AF_UNIX
@@ -755,25 +756,30 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
755756

756757
#ifdef SO_LINGER
757758
if (PHP_STREAM_CONTEXT(stream)
758-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_linger")) != NULL) {
759+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
759760
bool failed;
760761
linger = parse_linger(tmpzval, &failed);
761762

762763
if (failed) {
763764
if (xparam->want_errortext) {
764-
xparam->outputs.error_text = strpprintf(0, "Invalid `so_linger` value");
765+
xparam->outputs.error_text = strpprintf(0, "Invalid `linger` value");
766+
}
767+
if (host) {
768+
efree(host);
765769
}
766770
return -1;
767771
} else {
768772
sockopts |= STREAM_SOCKOP_SO_LINGER;
769773
}
774+
775+
option = &linger;
770776
}
771777
#endif
772778

773779
sock->socket = php_network_bind_socket_to_local_addr(host, portno,
774780
stream->ops == &php_stream_udp_socket_ops ? SOCK_DGRAM : SOCK_STREAM,
775781
sockopts,
776-
linger,
782+
option,
777783
xparam->want_errortext ? &xparam->outputs.error_text : NULL,
778784
&err
779785
);
@@ -795,6 +801,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
795801
zval *tmpzval = NULL;
796802
long sockopts = STREAM_SOCKOP_NONE;
797803
long linger = -1;
804+
void *option = NULL;
798805

799806
#ifdef AF_UNIX
800807
if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
@@ -852,30 +859,38 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
852859

853860
#ifdef SO_LINGER
854861
if (PHP_STREAM_CONTEXT(stream)
855-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_linger")) != NULL) {
862+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
856863
bool failed;
857864
linger = parse_linger(tmpzval, &failed);
858865

859866
if (failed) {
860867
if (xparam->want_errortext) {
861-
xparam->outputs.error_text = strpprintf(0, "Invalid `so_linger` value");
868+
xparam->outputs.error_text = strpprintf(0, "Invalid `linger` value");
869+
}
870+
if (host) {
871+
efree(host);
872+
}
873+
if (bindto) {
874+
efree(bindto);
862875
}
863876
return -1;
864877
} else {
865878
sockopts |= STREAM_SOCKOP_SO_LINGER;
866879
}
880+
881+
option = &linger;
867882
}
868883
#endif
869884

870885
if (stream->ops != &php_stream_udp_socket_ops /* TCP_NODELAY is only applicable for TCP */
871886
#ifdef AF_UNIX
872-
&& stream->ops != &php_stream_unix_socket_ops
873-
&& stream->ops != &php_stream_unixdg_socket_ops
887+
&& stream->ops != &php_stream_unix_socket_ops
888+
&& stream->ops != &php_stream_unixdg_socket_ops
874889
#endif
875-
&& PHP_STREAM_CONTEXT(stream)
876-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_nodelay")) != NULL
877-
&& zend_is_true(tmpzval)
878-
) {
890+
&& PHP_STREAM_CONTEXT(stream)
891+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_nodelay")) != NULL
892+
&& zend_is_true(tmpzval)
893+
) {
879894
sockopts |= STREAM_SOCKOP_TCP_NODELAY;
880895
}
881896

@@ -892,7 +907,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
892907
bindto,
893908
bindport,
894909
sockopts,
895-
linger
910+
option
896911
);
897912

898913
ret = sock->socket == -1 ? -1 : 0;

0 commit comments

Comments
 (0)