Skip to content

Commit b566d20

Browse files
committed
Optimize Client_inet_addr
1 parent 8e6c3b9 commit b566d20

File tree

3 files changed

+15
-35
lines changed

3 files changed

+15
-35
lines changed

include/swoole_socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct Address {
9898
socklen_t len;
9999
SocketType type;
100100

101-
bool assign(SocketType _type, const std::string &_host, int _port);
101+
bool assign(SocketType _type, const std::string &_host, int _port, bool resolve = true);
102102
bool assign(const std::string &url);
103103

104104
const char *get_ip() {

src/network/address.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int Address::get_port() {
5252
}
5353
}
5454

55-
bool Address::assign(SocketType _type, const std::string &_host, int _port) {
55+
bool Address::assign(SocketType _type, const std::string &_host, int _port, bool resolve) {
5656
type = _type;
5757
const char *host = _host.c_str();
5858

@@ -66,6 +66,10 @@ bool Address::assign(SocketType _type, const std::string &_host, int _port) {
6666
addr.inet_v4.sin_port = htons(_port);
6767
len = sizeof(addr.inet_v4);
6868
if (inet_pton(AF_INET, host, &addr.inet_v4.sin_addr.s_addr) != 1) {
69+
if (!resolve) {
70+
swoole_set_last_error(SW_ERROR_BAD_HOST_ADDR);
71+
return false;
72+
}
6973
if (gethostbyname(AF_INET, host, (char *) &addr.inet_v4.sin_addr) < 0) {
7074
swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
7175
return false;
@@ -76,6 +80,10 @@ bool Address::assign(SocketType _type, const std::string &_host, int _port) {
7680
addr.inet_v6.sin6_port = htons(_port);
7781
len = sizeof(addr.inet_v6);
7882
if (inet_pton(AF_INET6, host, addr.inet_v6.sin6_addr.s6_addr) != 1) {
83+
if (!resolve) {
84+
swoole_set_last_error(SW_ERROR_BAD_HOST_ADDR);
85+
return false;
86+
}
7987
if (gethostbyname(AF_INET6, host, (char *) &addr.inet_v6.sin6_addr) < 0) {
8088
swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
8189
return false;

src/network/client.cc

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -358,42 +358,14 @@ static int Client_inet_addr(Client *cli, const char *host, int port) {
358358
cli->server_host = host;
359359
cli->server_port = port;
360360

361-
void *addr = nullptr;
362-
if (cli->socket->is_inet4()) {
363-
cli->server_addr.addr.inet_v4.sin_family = AF_INET;
364-
cli->server_addr.addr.inet_v4.sin_port = htons(port);
365-
cli->server_addr.len = sizeof(cli->server_addr.addr.inet_v4);
366-
addr = &cli->server_addr.addr.inet_v4.sin_addr.s_addr;
367-
368-
if (inet_pton(AF_INET, host, addr)) {
369-
return SW_OK;
370-
}
371-
} else if (cli->socket->is_inet6()) {
372-
cli->server_addr.addr.inet_v6.sin6_family = AF_INET6;
373-
cli->server_addr.addr.inet_v6.sin6_port = htons(port);
374-
cli->server_addr.len = sizeof(cli->server_addr.addr.inet_v6);
375-
addr = cli->server_addr.addr.inet_v6.sin6_addr.s6_addr;
376-
377-
if (inet_pton(AF_INET6, host, addr)) {
378-
return SW_OK;
379-
}
380-
} else if (cli->socket->is_local()) {
381-
cli->server_addr.addr.un.sun_family = AF_UNIX;
382-
swoole_strlcpy(cli->server_addr.addr.un.sun_path, host, sizeof(cli->server_addr.addr.un.sun_path));
383-
cli->server_addr.addr.un.sun_path[sizeof(cli->server_addr.addr.un.sun_path) - 1] = 0;
384-
cli->server_addr.len = sizeof(cli->server_addr.addr.un.sun_path);
385-
return SW_OK;
386-
} else {
387-
return SW_ERR;
388-
}
389-
if (!cli->async) {
390-
if (swoole::network::gethostbyname(cli->_sock_domain, host, (char *) addr) < 0) {
391-
swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
361+
if (!cli->server_addr.assign(cli->socket->socket_type, host, port, !cli->async)) {
362+
if (swoole_get_last_error() == SW_ERROR_BAD_HOST_ADDR) {
363+
cli->wait_dns = 1;
364+
} else {
392365
return SW_ERR;
393366
}
394-
} else {
395-
cli->wait_dns = 1;
396367
}
368+
397369
return SW_OK;
398370
}
399371

0 commit comments

Comments
 (0)