Skip to content

Commit b262146

Browse files
authored
get rid of inet_ntoa and inet_aton calls (#4199)
* get rid of inet_ntoa and inet_aton calls * get rid of inet_aton in thirdparty
1 parent b1d1833 commit b262146

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

ext-src/swoole_client.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,12 @@ static PHP_METHOD(swoole_client, getsockname) {
12101210
}
12111211
} else {
12121212
add_assoc_long(return_value, "port", ntohs(cli->socket->info.addr.inet_v4.sin_port));
1213-
add_assoc_string(return_value, "host", inet_ntoa(cli->socket->info.addr.inet_v4.sin_addr));
1213+
char tmp[INET_ADDRSTRLEN];
1214+
if (inet_ntop(AF_INET, &cli->socket->info.addr.inet_v4.sin_addr, tmp, sizeof(tmp))) {
1215+
add_assoc_string(return_value, "host", tmp);
1216+
} else {
1217+
php_swoole_fatal_error(E_WARNING, "inet_ntop() failed");
1218+
}
12141219
}
12151220
}
12161221

@@ -1248,7 +1253,13 @@ static PHP_METHOD(swoole_client, getpeername) {
12481253
if (cli->socket->socket_type == SW_SOCK_UDP) {
12491254
array_init(return_value);
12501255
add_assoc_long(return_value, "port", ntohs(cli->remote_addr.addr.inet_v4.sin_port));
1251-
add_assoc_string(return_value, "host", inet_ntoa(cli->remote_addr.addr.inet_v4.sin_addr));
1256+
char tmp[INET_ADDRSTRLEN];
1257+
1258+
if (inet_ntop(AF_INET, &cli->remote_addr.addr.inet_v4.sin_addr, tmp, sizeof(tmp))) {
1259+
add_assoc_string(return_value, "host", tmp);
1260+
} else {
1261+
php_swoole_fatal_error(E_WARNING, "inet_ntop() failed");
1262+
}
12521263
} else if (cli->socket->socket_type == SW_SOCK_UDP6) {
12531264
array_init(return_value);
12541265
add_assoc_long(return_value, "port", ntohs(cli->remote_addr.addr.inet_v6.sin6_port));

src/coroutine/socket.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ ssize_t Socket::sendto(const std::string &host, int port, const void *__buf, siz
13661366

13671367
for (size_t i = 0; i < 2; i++) {
13681368
if (type == SW_SOCK_UDP) {
1369-
if (::inet_aton(ip.c_str(), &addr.in.sin_addr) == 0) {
1369+
if (::inet_pton(AF_INET, ip.c_str(), &addr.in.sin_addr) == 0) {
13701370
read_co = write_co = Coroutine::get_current_safe();
13711371
ip = System::gethostbyname(host, sock_domain, dns_timeout);
13721372
read_co = write_co = nullptr;

src/network/address.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ static thread_local char tmp_address[INET6_ADDRSTRLEN];
2323

2424
const char *Address::get_addr() {
2525
if (type == SW_SOCK_TCP || type == SW_SOCK_UDP) {
26-
return inet_ntoa(addr.inet_v4.sin_addr);
26+
if (inet_ntop(AF_INET, &addr.inet_v4.sin_addr, tmp_address, sizeof(tmp_address))) {
27+
return tmp_address;
28+
}
2729
} else if (type == SW_SOCK_TCP6 || type == SW_SOCK_UDP6) {
2830
if (inet_ntop(AF_INET6, &addr.inet_v6.sin6_addr, tmp_address, sizeof(tmp_address))) {
2931
return tmp_address;

thirdparty/php/sockets/sockaddr_conv.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int php_set_inet_addr(struct sockaddr_in *sin, char *string, Socket *php_sock) /
7777
struct in_addr tmp;
7878
struct hostent *host_entry;
7979

80-
if (inet_aton(string, &tmp)) {
80+
if (inet_pton(AF_INET, string, &tmp)) {
8181
sin->sin_addr.s_addr = tmp.s_addr;
8282
} else {
8383
#if PHP_VERSION_ID >= 70006

0 commit comments

Comments
 (0)