Skip to content

Commit 1998184

Browse files
committed
changes from feedback
1 parent c1ec8b4 commit 1998184

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
@@ -689,6 +689,7 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
689689
int portno, err;
690690
long sockopts = STREAM_SOCKOP_NONE;
691691
long linger = -1;
692+
void *option = NULL;
692693
zval *tmpzval = NULL;
693694

694695
#ifdef AF_UNIX
@@ -750,25 +751,30 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
750751

751752
#ifdef SO_LINGER
752753
if (PHP_STREAM_CONTEXT(stream)
753-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_linger")) != NULL) {
754+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
754755
bool failed;
755756
linger = parse_linger(tmpzval, &failed);
756757

757758
if (failed) {
758759
if (xparam->want_errortext) {
759-
xparam->outputs.error_text = strpprintf(0, "Invalid `so_linger` value");
760+
xparam->outputs.error_text = strpprintf(0, "Invalid `linger` value");
761+
}
762+
if (host) {
763+
efree(host);
760764
}
761765
return -1;
762766
} else {
763767
sockopts |= STREAM_SOCKOP_SO_LINGER;
764768
}
769+
770+
option = &linger;
765771
}
766772
#endif
767773

768774
sock->socket = php_network_bind_socket_to_local_addr(host, portno,
769775
stream->ops == &php_stream_udp_socket_ops ? SOCK_DGRAM : SOCK_STREAM,
770776
sockopts,
771-
linger,
777+
option,
772778
xparam->want_errortext ? &xparam->outputs.error_text : NULL,
773779
&err
774780
);
@@ -790,6 +796,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
790796
zval *tmpzval = NULL;
791797
long sockopts = STREAM_SOCKOP_NONE;
792798
long linger = -1;
799+
void *option = NULL;
793800

794801
#ifdef AF_UNIX
795802
if (stream->ops == &php_stream_unix_socket_ops || stream->ops == &php_stream_unixdg_socket_ops) {
@@ -847,30 +854,38 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
847854

848855
#ifdef SO_LINGER
849856
if (PHP_STREAM_CONTEXT(stream)
850-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "so_linger")) != NULL) {
857+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
851858
bool failed;
852859
linger = parse_linger(tmpzval, &failed);
853860

854861
if (failed) {
855862
if (xparam->want_errortext) {
856-
xparam->outputs.error_text = strpprintf(0, "Invalid `so_linger` value");
863+
xparam->outputs.error_text = strpprintf(0, "Invalid `linger` value");
864+
}
865+
if (host) {
866+
efree(host);
867+
}
868+
if (bindto) {
869+
efree(bindto);
857870
}
858871
return -1;
859872
} else {
860873
sockopts |= STREAM_SOCKOP_SO_LINGER;
861874
}
875+
876+
option = &linger;
862877
}
863878
#endif
864879

865880
if (stream->ops != &php_stream_udp_socket_ops /* TCP_NODELAY is only applicable for TCP */
866881
#ifdef AF_UNIX
867-
&& stream->ops != &php_stream_unix_socket_ops
868-
&& stream->ops != &php_stream_unixdg_socket_ops
882+
&& stream->ops != &php_stream_unix_socket_ops
883+
&& stream->ops != &php_stream_unixdg_socket_ops
869884
#endif
870-
&& PHP_STREAM_CONTEXT(stream)
871-
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_nodelay")) != NULL
872-
&& zend_is_true(tmpzval)
873-
) {
885+
&& PHP_STREAM_CONTEXT(stream)
886+
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_nodelay")) != NULL
887+
&& zend_is_true(tmpzval)
888+
) {
874889
sockopts |= STREAM_SOCKOP_TCP_NODELAY;
875890
}
876891

@@ -887,7 +902,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
887902
bindto,
888903
bindport,
889904
sockopts,
890-
linger
905+
option
891906
);
892907

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

0 commit comments

Comments
 (0)