Skip to content

Commit 4aa74e6

Browse files
committed
Apply suggestions
1 parent 36e1406 commit 4aa74e6

File tree

1 file changed

+88
-95
lines changed

1 file changed

+88
-95
lines changed

main/network.c

Lines changed: 88 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -453,43 +453,41 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po
453453
continue;
454454
}
455455

456-
if (sa) {
457-
/* attempt to bind */
456+
/* attempt to bind */
458457

459458
#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));
461460
#endif
462461
#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+
}
467466
#endif
468467
#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+
}
472471
#endif
473472
#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+
}
477476
#endif
478477
#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+
}
482481
#endif
483482

484-
n = bind(sock, sa, socklen);
485-
486-
if (n != SOCK_CONN_ERR) {
487-
goto bound;
488-
}
483+
n = bind(sock, sa, socklen);
489484

490-
err = php_socket_errno();
485+
if (n != SOCK_CONN_ERR) {
486+
goto bound;
491487
}
492488

489+
err = php_socket_errno();
490+
493491
closesocket(sock);
494492
}
495493
sock = -1;
@@ -854,108 +852,103 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
854852
continue;
855853
}
856854

857-
if (sa) {
858855
/* make a connection attempt */
859856

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;
866861
#if HAVE_IPV6 && HAVE_INET_PTON
867-
struct sockaddr_in6 in6;
862+
struct sockaddr_in6 in6;
868863
#endif
869-
};
870-
} local_address;
864+
} local_address;
865+
int local_address_len = 0;
871866

872-
local_address.len = 0;
873-
if (sa->sa_family == AF_INET) {
867+
if (sa->sa_family == AF_INET) {
874868
#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) {
876870
#else
877-
if (inet_aton(bindto, &local_address.in4.sin_addr)) {
871+
if (inet_aton(bindto, &local_address.in4.sin_addr)) {
878872
#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));
884877
}
878+
}
885879
#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);
898885
}
899886
}
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));
904892
}
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+
}
905899

906900
#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));
912905
}
906+
}
913907
#endif
914908

915909
#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));
921914
}
915+
}
922916
#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);
926920

927-
if (n != -1) {
928-
goto connected;
929-
}
921+
if (n != -1) {
922+
goto connected;
923+
}
930924

931-
/* adjust timeout for next attempt */
925+
/* adjust timeout for next attempt */
932926
#if HAVE_GETTIMEOFDAY
933-
if (timeout) {
934-
gettimeofday(&time_now, NULL);
927+
if (timeout) {
928+
gettimeofday(&time_now, NULL);
935929

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 */
949932
fatal = 1;
950933
} 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);
956936
}
957-
#endif
958937
}
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
959952

960953
closesocket(sock);
961954
}

0 commit comments

Comments
 (0)