@@ -453,43 +453,41 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po
453
453
continue ;
454
454
}
455
455
456
- if (sa ) {
457
- /* attempt to bind */
456
+ /* attempt to bind */
458
457
459
458
#ifdef SO_REUSEADDR
460
- setsockopt (sock , SOL_SOCKET , SO_REUSEADDR , (char * )& sockoptval , sizeof (sockoptval ));
459
+ setsockopt (sock , SOL_SOCKET , SO_REUSEADDR , (char * )& sockoptval , sizeof (sockoptval ));
461
460
#endif
462
461
#ifdef IPV6_V6ONLY
463
- if (sockopts & STREAM_SOCKOP_IPV6_V6ONLY ) {
464
- int ipv6_val = !!(sockopts & STREAM_SOCKOP_IPV6_V6ONLY_ENABLED );
465
- setsockopt (sock , IPPROTO_IPV6 , IPV6_V6ONLY , (char * )& ipv6_val , sizeof (sockoptval ));
466
- }
462
+ if (sockopts & STREAM_SOCKOP_IPV6_V6ONLY ) {
463
+ int ipv6_val = !!(sockopts & STREAM_SOCKOP_IPV6_V6ONLY_ENABLED );
464
+ setsockopt (sock , IPPROTO_IPV6 , IPV6_V6ONLY , (char * )& ipv6_val , sizeof (sockoptval ));
465
+ }
467
466
#endif
468
467
#ifdef SO_REUSEPORT
469
- if (sockopts & STREAM_SOCKOP_SO_REUSEPORT ) {
470
- setsockopt (sock , SOL_SOCKET , SO_REUSEPORT , (char * )& sockoptval , sizeof (sockoptval ));
471
- }
468
+ if (sockopts & STREAM_SOCKOP_SO_REUSEPORT ) {
469
+ setsockopt (sock , SOL_SOCKET , SO_REUSEPORT , (char * )& sockoptval , sizeof (sockoptval ));
470
+ }
472
471
#endif
473
472
#ifdef SO_BROADCAST
474
- if (sockopts & STREAM_SOCKOP_SO_BROADCAST ) {
475
- setsockopt (sock , SOL_SOCKET , SO_BROADCAST , (char * )& sockoptval , sizeof (sockoptval ));
476
- }
473
+ if (sockopts & STREAM_SOCKOP_SO_BROADCAST ) {
474
+ setsockopt (sock , SOL_SOCKET , SO_BROADCAST , (char * )& sockoptval , sizeof (sockoptval ));
475
+ }
477
476
#endif
478
477
#ifdef TCP_NODELAY
479
- if (sockopts & STREAM_SOCKOP_TCP_NODELAY ) {
480
- setsockopt (sock , IPPROTO_TCP , TCP_NODELAY , (char * )& sockoptval , sizeof (sockoptval ));
481
- }
478
+ if (sockopts & STREAM_SOCKOP_TCP_NODELAY ) {
479
+ setsockopt (sock , IPPROTO_TCP , TCP_NODELAY , (char * )& sockoptval , sizeof (sockoptval ));
480
+ }
482
481
#endif
483
482
484
- n = bind (sock , sa , socklen );
485
-
486
- if (n != SOCK_CONN_ERR ) {
487
- goto bound ;
488
- }
483
+ n = bind (sock , sa , socklen );
489
484
490
- err = php_socket_errno ();
485
+ if (n != SOCK_CONN_ERR ) {
486
+ goto bound ;
491
487
}
492
488
489
+ err = php_socket_errno ();
490
+
493
491
closesocket (sock );
494
492
}
495
493
sock = -1 ;
@@ -854,108 +852,103 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
854
852
continue ;
855
853
}
856
854
857
- if (sa ) {
858
855
/* make a connection attempt */
859
856
860
- if (bindto ) {
861
- struct {
862
- int len ;
863
- union {
864
- struct sockaddr common ;
865
- struct sockaddr_in in4 ;
857
+ if (bindto ) {
858
+ union {
859
+ struct sockaddr common ;
860
+ struct sockaddr_in in4 ;
866
861
#if HAVE_IPV6 && HAVE_INET_PTON
867
- struct sockaddr_in6 in6 ;
862
+ struct sockaddr_in6 in6 ;
868
863
#endif
869
- } ;
870
- } local_address ;
864
+ } local_address ;
865
+ int local_address_len = 0 ;
871
866
872
- local_address .len = 0 ;
873
- if (sa -> sa_family == AF_INET ) {
867
+ if (sa -> sa_family == AF_INET ) {
874
868
#ifdef HAVE_INET_PTON
875
- if (inet_pton (AF_INET , bindto , & local_address .in4 .sin_addr ) == 1 ) {
869
+ if (inet_pton (AF_INET , bindto , & local_address .in4 .sin_addr ) == 1 ) {
876
870
#else
877
- if (inet_aton (bindto , & local_address .in4 .sin_addr )) {
871
+ if (inet_aton (bindto , & local_address .in4 .sin_addr )) {
878
872
#endif
879
- local_address .len = sizeof (struct sockaddr_in );
880
- local_address .in4 .sin_family = sa -> sa_family ;
881
- local_address .in4 .sin_port = htons (bindport );
882
- memset (& (local_address .in4 .sin_zero ), 0 , sizeof (local_address .in4 .sin_zero ));
883
- }
873
+ local_address_len = sizeof (struct sockaddr_in );
874
+ local_address .in4 .sin_family = sa -> sa_family ;
875
+ local_address .in4 .sin_port = htons (bindport );
876
+ memset (& (local_address .in4 .sin_zero ), 0 , sizeof (local_address .in4 .sin_zero ));
884
877
}
878
+ }
885
879
#if HAVE_IPV6 && HAVE_INET_PTON
886
- else { /* IPV6 */
887
- if (inet_pton (AF_INET6 , bindto , & local_address .in6 .sin6_addr ) == 1 ) {
888
- local_address .len = sizeof (struct sockaddr_in6 );
889
- local_address .in6 .sin6_family = sa -> sa_family ;
890
- local_address .in6 .sin6_port = htons (bindport );
891
- }
892
- }
893
- #endif
894
- if (local_address .len == 0 ) {
895
- php_error_docref (NULL , E_WARNING , "Invalid IP Address: %s" , bindto );
896
- } else if (bind (sock , & local_address .common , local_address .len )) {
897
- php_error_docref (NULL , E_WARNING , "Failed to bind to '%s:%d', system said: %s" , bindto , bindport , strerror (errno ));
880
+ else { /* IPV6 */
881
+ if (inet_pton (AF_INET6 , bindto , & local_address .in6 .sin6_addr ) == 1 ) {
882
+ local_address_len = sizeof (struct sockaddr_in6 );
883
+ local_address .in6 .sin6_family = sa -> sa_family ;
884
+ local_address .in6 .sin6_port = htons (bindport );
898
885
}
899
886
}
900
- /* free error string received during previous iteration (if any) */
901
- if (error_string && * error_string ) {
902
- zend_string_release_ex (* error_string , 0 );
903
- * error_string = NULL ;
887
+ #endif
888
+ if (local_address_len == 0 ) {
889
+ php_error_docref (NULL , E_WARNING , "Invalid IP Address: %s" , bindto );
890
+ } else if (bind (sock , & local_address .common , local_address_len )) {
891
+ php_error_docref (NULL , E_WARNING , "Failed to bind to '%s:%d', system said: %s" , bindto , bindport , strerror (errno ));
904
892
}
893
+ }
894
+ /* free error string received during previous iteration (if any) */
895
+ if (error_string && * error_string ) {
896
+ zend_string_release_ex (* error_string , 0 );
897
+ * error_string = NULL ;
898
+ }
905
899
906
900
#ifdef SO_BROADCAST
907
- {
908
- int val = 1 ;
909
- if (sockopts & STREAM_SOCKOP_SO_BROADCAST ) {
910
- setsockopt (sock , SOL_SOCKET , SO_BROADCAST , (char * )& val , sizeof (val ));
911
- }
901
+ {
902
+ int val = 1 ;
903
+ if (sockopts & STREAM_SOCKOP_SO_BROADCAST ) {
904
+ setsockopt (sock , SOL_SOCKET , SO_BROADCAST , (char * )& val , sizeof (val ));
912
905
}
906
+ }
913
907
#endif
914
908
915
909
#ifdef TCP_NODELAY
916
- {
917
- int val = 1 ;
918
- if (sockopts & STREAM_SOCKOP_TCP_NODELAY ) {
919
- setsockopt (sock , IPPROTO_TCP , TCP_NODELAY , (char * )& val , sizeof (val ));
920
- }
910
+ {
911
+ int val = 1 ;
912
+ if (sockopts & STREAM_SOCKOP_TCP_NODELAY ) {
913
+ setsockopt (sock , IPPROTO_TCP , TCP_NODELAY , (char * )& val , sizeof (val ));
921
914
}
915
+ }
922
916
#endif
923
- n = php_network_connect_socket (sock , sa , socklen , asynchronous ,
924
- timeout ? & working_timeout : NULL ,
925
- error_string , error_code );
917
+ n = php_network_connect_socket (sock , sa , socklen , asynchronous ,
918
+ timeout ? & working_timeout : NULL ,
919
+ error_string , error_code );
926
920
927
- if (n != -1 ) {
928
- goto connected ;
929
- }
921
+ if (n != -1 ) {
922
+ goto connected ;
923
+ }
930
924
931
- /* adjust timeout for next attempt */
925
+ /* adjust timeout for next attempt */
932
926
#if HAVE_GETTIMEOFDAY
933
- if (timeout ) {
934
- gettimeofday (& time_now , NULL );
927
+ if (timeout ) {
928
+ gettimeofday (& time_now , NULL );
935
929
936
- if (!timercmp (& time_now , & limit_time , < )) {
937
- /* time limit expired; don't attempt any further connections */
938
- fatal = 1 ;
939
- } else {
940
- /* work out remaining time */
941
- sub_times (limit_time , time_now , & working_timeout );
942
- }
943
- }
944
- #else
945
- if (error_code && * error_code == PHP_TIMEOUT_ERROR_VALUE ) {
946
- /* Don't even bother trying to connect to the next alternative;
947
- * we have no way to determine how long we have already taken
948
- * and it is quite likely that the next attempt will fail too. */
930
+ if (!timercmp (& time_now , & limit_time , < )) {
931
+ /* time limit expired; don't attempt any further connections */
949
932
fatal = 1 ;
950
933
} else {
951
- /* re-use the same initial timeout.
952
- * Not the best thing, but in practice it should be good-enough */
953
- if (timeout ) {
954
- memcpy (& working_timeout , timeout , sizeof (working_timeout ));
955
- }
934
+ /* work out remaining time */
935
+ sub_times (limit_time , time_now , & working_timeout );
956
936
}
957
- #endif
958
937
}
938
+ #else
939
+ if (error_code && * error_code == PHP_TIMEOUT_ERROR_VALUE ) {
940
+ /* Don't even bother trying to connect to the next alternative;
941
+ * we have no way to determine how long we have already taken
942
+ * and it is quite likely that the next attempt will fail too. */
943
+ fatal = 1 ;
944
+ } else {
945
+ /* re-use the same initial timeout.
946
+ * Not the best thing, but in practice it should be good-enough */
947
+ if (timeout ) {
948
+ memcpy (& working_timeout , timeout , sizeof (working_timeout ));
949
+ }
950
+ }
951
+ #endif
959
952
960
953
closesocket (sock );
961
954
}
0 commit comments