Skip to content

Commit 26ef413

Browse files
committed
ext/sockets: inet family conversions internal changes.
1 parent caafa04 commit 26ef413

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

ext/sockets/conversions.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,11 @@ static void to_zval_read_uid_t(const char *data, zval *zv, res_context *ctx)
550550
/* CONVERSIONS for sockaddr */
551551
static void from_zval_write_sin_addr(const zval *zaddr_str, char *inaddr, ser_context *ctx)
552552
{
553-
int res;
554553
struct sockaddr_in saddr = {0};
555554
zend_string *addr_str, *tmp_addr_str;
556555

557556
addr_str = zval_get_tmp_string((zval *) zaddr_str, &tmp_addr_str);
558-
res = php_set_inet_addr(&saddr, ZSTR_VAL(addr_str), ctx->sock);
559-
if (res) {
557+
if (php_set_inet_addr(&saddr, ZSTR_VAL(addr_str), ctx->sock) == SUCCESS) {
560558
memcpy(inaddr, &saddr.sin_addr, sizeof saddr.sin_addr);
561559
} else {
562560
/* error already emitted, but let's emit another more relevant */
@@ -600,13 +598,11 @@ static void to_zval_read_sockaddr_in(const char *data, zval *zv, res_context *ct
600598
#ifdef HAVE_IPV6
601599
static void from_zval_write_sin6_addr(const zval *zaddr_str, char *addr6, ser_context *ctx)
602600
{
603-
int res;
604601
struct sockaddr_in6 saddr6 = {0};
605602
zend_string *addr_str, *tmp_addr_str;
606603

607604
addr_str = zval_get_tmp_string((zval *) zaddr_str, &tmp_addr_str);
608-
res = php_set_inet6_addr(&saddr6, ZSTR_VAL(addr_str), ctx->sock);
609-
if (res) {
605+
if (php_set_inet6_addr(&saddr6, ZSTR_VAL(addr_str), ctx->sock) == SUCCESS) {
610606
memcpy(addr6, &saddr6.sin6_addr, sizeof saddr6.sin6_addr);
611607
} else {
612608
/* error already emitted, but let's emit another more relevant */

ext/sockets/multicast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static zend_result php_get_address_from_array(const HashTable *ht, const char *k
128128
return FAILURE;
129129
}
130130
str = zval_get_tmp_string(val, &tmp_str);
131-
if (!php_set_inet46_addr(ss, ss_len, ZSTR_VAL(str), sock)) {
131+
if (php_set_inet46_addr(ss, ss_len, ZSTR_VAL(str), sock) == FAILURE) {
132132
zend_tmp_string_release(tmp_str);
133133
return FAILURE;
134134
}

ext/sockets/sockaddr_conv.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern zend_result php_string_to_if_index(const char *val, unsigned *out);
1313

1414
#ifdef HAVE_IPV6
1515
/* Sets addr by hostname, or by ip in string form (AF_INET6) */
16-
int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock) /* {{{ */
16+
zend_result php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock) /* {{{ */
1717
{
1818
struct in6_addr tmp;
1919
#ifdef HAVE_GETADDRINFO
@@ -41,12 +41,12 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
4141
#else
4242
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
4343
#endif
44-
return 0;
44+
return FAILURE;
4545
}
4646
if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
4747
php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
4848
freeaddrinfo(addrinfo);
49-
return 0;
49+
return FAILURE;
5050
}
5151

5252
memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
@@ -55,36 +55,43 @@ int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_
5555
#else
5656
/* No IPv6 specific hostname resolution is available on this system? */
5757
php_error_docref(NULL, E_WARNING, "Host lookup failed: getaddrinfo() not available on this system");
58-
return 0;
58+
return FAILURE;
5959
#endif
6060

6161
}
6262

6363
if (scope) {
6464
zend_long lval = 0;
6565
double dval = 0;
66-
unsigned scope_id = 0;
67-
66+
uint32_t scope_id = 0;
6867
scope++;
6968

69+
if (*scope == '\0') {
70+
zend_value_error("scope cannot be empty");
71+
return FAILURE;
72+
}
73+
74+
7075
if (IS_LONG == is_numeric_string(scope, strlen(scope), &lval, &dval, 0)) {
71-
if (lval > 0 && (zend_ulong)lval <= UINT_MAX) {
72-
scope_id = lval;
76+
if (lval <= 0 || (zend_ulong)lval > UINT_MAX) {
77+
zend_value_error("scope must be between 1 and %u", UINT_MAX);
78+
return FAILURE;
7379
}
80+
scope_id = lval;
7481
} else {
7582
php_string_to_if_index(scope, &scope_id);
7683
}
7784

7885
sin6->sin6_scope_id = scope_id;
7986
}
8087

81-
return 1;
88+
return SUCCESS;
8289
}
8390
/* }}} */
8491
#endif
8592

8693
/* Sets addr by hostname, or by ip in string form (AF_INET) */
87-
int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock) /* {{{ */
94+
zend_result php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock) /* {{{ */
8895
{
8996
struct in_addr tmp;
9097
struct hostent *host_entry;
@@ -99,46 +106,46 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_soc
99106
#else
100107
PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
101108
#endif
102-
return 0;
109+
return FAILURE;
103110
}
104111
if (host_entry->h_addrtype != AF_INET) {
105112
php_error_docref(NULL, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
106-
return 0;
113+
return FAILURE;
107114
}
108115
memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
109116
}
110117

111-
return 1;
118+
return SUCCESS;
112119
}
113120
/* }}} */
114121

115122
/* Sets addr by hostname or by ip in string form (AF_INET or AF_INET6,
116123
* depending on the socket) */
117-
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock) /* {{{ */
124+
zend_result php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock) /* {{{ */
118125
{
119126
if (php_sock->type == AF_INET) {
120127
struct sockaddr_in t = {0};
121-
if (php_set_inet_addr(&t, string, php_sock)) {
128+
if (php_set_inet_addr(&t, string, php_sock) == SUCCESS) {
122129
memcpy(ss, &t, sizeof t);
123130
ss->ss_family = AF_INET;
124131
*ss_len = sizeof(t);
125-
return 1;
132+
return SUCCESS;
126133
}
127134
}
128135
#ifdef HAVE_IPV6
129136
else if (php_sock->type == AF_INET6) {
130137
struct sockaddr_in6 t = {0};
131-
if (php_set_inet6_addr(&t, string, php_sock)) {
138+
if (php_set_inet6_addr(&t, string, php_sock) == SUCCESS) {
132139
memcpy(ss, &t, sizeof t);
133140
ss->ss_family = AF_INET6;
134141
*ss_len = sizeof(t);
135-
return 1;
142+
return SUCCESS;
136143
}
137144
}
138145
#endif
139146
else {
140147
php_error_docref(NULL, E_WARNING,
141148
"IP address used in the context of an unexpected type of socket");
142149
}
143-
return 0;
150+
return FAILURE;
144151
}

ext/sockets/sockaddr_conv.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
* The IPv6 literal can be a IPv4 mapped address (like ::ffff:127.0.0.1).
1717
* If the hostname yields no IPv6 addresses, a mapped IPv4 address may be returned (AI_V4MAPPED)
1818
*/
19-
int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock);
19+
zend_result php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock);
2020

2121
/*
2222
* Convert an IPv4 literal or a hostname into a sockaddr_in.
2323
*/
24-
int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock);
24+
zend_result php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock);
2525

2626
/*
2727
* Calls either php_set_inet6_addr() or php_set_inet_addr(), depending on the type of the socket.
2828
*/
29-
int php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock);
29+
zend_result php_set_inet46_addr(php_sockaddr_storage *ss, socklen_t *ss_len, char *string, php_socket *php_sock);
3030

3131
#endif

ext/sockets/sockets.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ PHP_FUNCTION(socket_connect)
11921192
sin6.sin6_family = AF_INET6;
11931193
sin6.sin6_port = htons((unsigned short int)port);
11941194

1195-
if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1195+
if (php_set_inet6_addr(&sin6, addr, php_sock) == FAILURE) {
11961196
RETURN_FALSE;
11971197
}
11981198

@@ -1211,7 +1211,7 @@ PHP_FUNCTION(socket_connect)
12111211
sin.sin_family = AF_INET;
12121212
sin.sin_port = htons((unsigned short int)port);
12131213

1214-
if (! php_set_inet_addr(&sin, addr, php_sock)) {
1214+
if (php_set_inet_addr(&sin, addr, php_sock) == FAILURE) {
12151215
RETURN_FALSE;
12161216
}
12171217

@@ -1318,7 +1318,7 @@ PHP_FUNCTION(socket_bind)
13181318
sa->sin_family = AF_INET;
13191319
sa->sin_port = htons((unsigned short) port);
13201320

1321-
if (! php_set_inet_addr(sa, addr, php_sock)) {
1321+
if (php_set_inet_addr(sa, addr, php_sock) == FAILURE) {
13221322
RETURN_FALSE;
13231323
}
13241324

@@ -1333,7 +1333,7 @@ PHP_FUNCTION(socket_bind)
13331333
sa->sin6_family = AF_INET6;
13341334
sa->sin6_port = htons((unsigned short) port);
13351335

1336-
if (! php_set_inet6_addr(sa, addr, php_sock)) {
1336+
if (php_set_inet6_addr(sa, addr, php_sock) == FAILURE) {
13371337
RETURN_FALSE;
13381338
}
13391339

@@ -1610,7 +1610,7 @@ PHP_FUNCTION(socket_sendto)
16101610
sin.sin_family = AF_INET;
16111611
sin.sin_port = htons((unsigned short) port);
16121612

1613-
if (! php_set_inet_addr(&sin, addr, php_sock)) {
1613+
if (php_set_inet_addr(&sin, addr, php_sock) == FAILURE) {
16141614
RETURN_FALSE;
16151615
}
16161616

@@ -1627,7 +1627,7 @@ PHP_FUNCTION(socket_sendto)
16271627
sin6.sin6_family = AF_INET6;
16281628
sin6.sin6_port = htons((unsigned short) port);
16291629

1630-
if (! php_set_inet6_addr(&sin6, addr, php_sock)) {
1630+
if (php_set_inet6_addr(&sin6, addr, php_sock) == FAILURE) {
16311631
RETURN_FALSE;
16321632
}
16331633

0 commit comments

Comments
 (0)