@@ -4,21 +4,15 @@ $socket = null;
4
4
$ errno = 0 ;
5
5
$ context = stream_context_create (array ('ssl ' => array ('local_cert ' => dirname (__FILE__ ).'/cert.pem ' )));
6
6
7
- for ($ i =0 ; $ i <10 && !$ socket ; ++$ i ) {
8
- $ port = rand (50000 , 65535 );
9
-
10
- $ socket = @stream_socket_server ("tcp://127.0.0.1: $ port " , $ errno , $ errstr , STREAM_SERVER_BIND |STREAM_SERVER_LISTEN , $ context );
11
- }
12
- //set anther random port that is not the same as $port
13
- do {
14
- $ pasv_port = rand (50000 , 65535 );
15
- }while ($ pasv_port == $ port );
16
-
7
+ $ socket = stream_socket_server ("tcp://127.0.0.1:0 " , $ errno , $ errstr , STREAM_SERVER_BIND |STREAM_SERVER_LISTEN , $ context );
17
8
if (!$ socket ) {
18
9
echo "$ errstr ( $ errno) \n" ;
19
10
die ("could not start/bind the ftp server \n" );
20
11
}
21
12
13
+ $ socket_name = stream_socket_get_name ($ socket , false );
14
+ $ port = (int ) substr ($ socket_name , strrpos ($ socket_name , ': ' ) + 1 );
15
+
22
16
$ pid = pcntl_fork ();
23
17
24
18
if ($ pid ) {
@@ -402,25 +396,20 @@ if ($pid) {
402
396
$ i =0 ;
403
397
404
398
if (empty ($ bug73457 )) {
405
- do {
406
- if (!empty ($ ssl )) {
407
- $ soc = @stream_socket_server ("tcp://127.0.0.1: $ pasv_port " , $ errno , $ errstr , STREAM_SERVER_BIND |STREAM_SERVER_LISTEN , $ context );
408
- } else {
409
- $ soc = @stream_socket_server ("tcp://127.0.0.1: $ pasv_port " );
410
- }
411
- /* Could bind port, Try another port */
412
- if (!$ soc ) {
413
- $ pasv_port = rand (50000 , 65535 );
414
- }
415
- $ i ++;
416
- } while ($ i <10 && !$ soc );
417
-
399
+ if (!empty ($ ssl )) {
400
+ $ soc = stream_socket_server ("tcp://127.0.0.1:0 " , $ errno , $ errstr , STREAM_SERVER_BIND |STREAM_SERVER_LISTEN , $ context );
401
+ } else {
402
+ $ soc = stream_socket_server ("tcp://127.0.0.1:0 " );
403
+ }
418
404
if (!$ soc ) {
419
405
echo "$ errstr ( $ errno) \n" ;
420
406
die ("could not bind passive port \n" );
421
407
}
408
+
409
+ $ soc_name = stream_socket_get_name ($ soc , false );
410
+ $ pasv_port = (int ) substr ($ socket_name , strrpos ($ soc_name , ': ' ) + 1 );
422
411
} else {
423
- $ pasv_port= 1234 ;
412
+ $ pasv_port = 1234 ;
424
413
}
425
414
426
415
$ p2 = $ pasv_port % ((int ) 1 << 8 );
0 commit comments