Skip to content

Commit d780f30

Browse files
committed
Optimize DNS API, Optimize core-tests code
1 parent a7cf5c0 commit d780f30

File tree

7 files changed

+51
-38
lines changed

7 files changed

+51
-38
lines changed

core-tests/src/coroutine/async.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ TEST(coroutine_async, gethostbyname) {
5555

5656
TEST(coroutine_async, error) {
5757
coroutine::run([](void *arg) {
58-
int retval;
58+
int retval = 0x7009501;
5959
const char *test_file = "/tmp/swoole_core_test_file_not_exists";
6060
swoole::coroutine::async([&](void) { retval = open(test_file, O_RDONLY); }, -1);
6161
ASSERT_EQ(retval, -1);

core-tests/src/network/dns.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@ TEST(dns, getaddrinfo) {
9090
ASSERT_TRUE(swoole::network::Address::verify_ip(AF_INET, ip));
9191
}
9292
}
93+
94+
TEST(dns, load_resolv_conf) {
95+
ASSERT_TRUE(swoole_load_resolv_conf());
96+
auto dns_server = swoole_get_dns_server();
97+
ASSERT_FALSE(dns_server.first.empty());
98+
ASSERT_NE(dns_server.second, 0);
99+
}

include/swoole.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ SW_API void swoole_throw_error(int code);
700700
SW_API void swoole_set_log_level(int level);
701701
SW_API void swoole_set_trace_flags(int flags);
702702
SW_API void swoole_set_dns_server(const std::string server);
703+
SW_API std::pair<std::string, int> swoole_get_dns_server();
704+
SW_API bool swoole_load_resolv_conf();
703705

704706
//-----------------------------------------------
705707
static sw_inline void sw_spinlock(sw_atomic_t *lock) {

include/swoole_config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@
191191

192192
#define SW_DNS_HOST_BUFFER_SIZE 16
193193
#define SW_DNS_SERVER_PORT 53
194-
#define SW_DNS_DEFAULT_SERVER "8.8.8.8"
195194
#define SW_DNS_RESOLV_CONF "/etc/resolv.conf"
196195

197196
#define SW_Z_BEST_SPEED 1

include/swoole_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define SWOOLE_EXTRA_VERSION "dev"
2525
#define SWOOLE_VERSION "4.7.0-dev"
2626
#define SWOOLE_VERSION_ID 40700
27-
#define SWOOLE_API_VERSION_ID 0x202012a
27+
#define SWOOLE_API_VERSION_ID 0x202107a
2828

2929
#define SWOOLE_BUG_REPORT \
3030
"A bug occurred in Swoole-v" SWOOLE_VERSION ", please report it.\n" \

src/core/base.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ SW_API void swoole_set_dns_server(const std::string server) {
262262
SwooleG.dns_server_port = dns_server_port;
263263
}
264264

265+
SW_API std::pair<std::string, int> swoole_get_dns_server() {
266+
std::pair<std::string, int> result;
267+
if (SwooleG.dns_server_host.empty()) {
268+
result.first = "";
269+
result.second = 0;
270+
} else {
271+
result.first = SwooleG.dns_server_host;
272+
result.second = SwooleG.dns_server_port;
273+
}
274+
return result;
275+
}
276+
265277
bool swoole_set_task_tmpdir(const std::string &dir) {
266278
if (dir.at(0) != '/') {
267279
swWarn("wrong absolute path '%s'", dir.c_str());

src/network/dns.cc

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,31 @@
2525
#include <ares.h>
2626
#endif
2727

28-
#define SW_DNS_SERVER_NUM 2
28+
bool swoole_load_resolv_conf() {
29+
FILE *fp;
30+
char line[100];
31+
char buf[16] = {};
32+
33+
if ((fp = fopen(SwooleG.dns_resolvconf_path.c_str(), "rt")) == nullptr) {
34+
swSysWarn("fopen(%s) failed", SwooleG.dns_resolvconf_path.c_str());
35+
return false;
36+
}
37+
38+
while (fgets(line, 100, fp)) {
39+
if (strncmp(line, "nameserver", 10) == 0) {
40+
strcpy(buf, strtok(line, " "));
41+
strcpy(buf, strtok(nullptr, "\n"));
42+
break;
43+
}
44+
}
45+
fclose(fp);
46+
47+
if (strlen(buf) == 0) {
48+
return false;
49+
}
50+
swoole_set_dns_server(buf);
51+
return true;
52+
}
2953

3054
namespace swoole {
3155
namespace coroutine {
@@ -77,37 +101,8 @@ static uint16_t dns_request_id = 1;
77101

78102
static int domain_encode(const char *src, int n, char *dest);
79103
static void domain_decode(char *str);
80-
static int get_dns_server();
81104
static std::string parse_ip_address(void *vaddr, int type);
82105

83-
static int get_dns_server() {
84-
FILE *fp;
85-
char line[100];
86-
char buf[16] = {};
87-
88-
if ((fp = fopen(SwooleG.dns_resolvconf_path.c_str(), "rt")) == nullptr) {
89-
swSysWarn("fopen(%s) failed", SwooleG.dns_resolvconf_path.c_str());
90-
return SW_ERR;
91-
}
92-
93-
while (fgets(line, 100, fp)) {
94-
if (strncmp(line, "nameserver", 10) == 0) {
95-
strcpy(buf, strtok(line, " "));
96-
strcpy(buf, strtok(nullptr, "\n"));
97-
break;
98-
}
99-
}
100-
fclose(fp);
101-
102-
if (strlen(buf) == 0) {
103-
swoole_set_dns_server(SW_DNS_DEFAULT_SERVER);
104-
} else {
105-
swoole_set_dns_server(buf);
106-
}
107-
108-
return SW_OK;
109-
}
110-
111106
static std::string parse_ip_address(void *vaddr, int type) {
112107
auto addr = reinterpret_cast<unsigned char *>(vaddr);
113108
std::string ip_addr;
@@ -138,11 +133,9 @@ std::vector<std::string> dns_lookup_impl_with_socket(const char *domain, int fam
138133
int steps = 0;
139134
std::vector<std::string> result;
140135

141-
if (SwooleG.dns_server_host.empty()) {
142-
if (get_dns_server() < 0) {
143-
swoole_set_last_error(SW_ERROR_DNSLOOKUP_NO_SERVER);
144-
return result;
145-
}
136+
if (SwooleG.dns_server_host.empty() && !swoole_load_resolv_conf()) {
137+
swoole_set_last_error(SW_ERROR_DNSLOOKUP_NO_SERVER);
138+
return result;
146139
}
147140

148141
header = (RecordHeader *) packet;

0 commit comments

Comments
 (0)