Skip to content

Commit fc279fb

Browse files
committed
dns_conf: check if the IP of bind is valid
1 parent 40dc9ec commit fc279fb

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/dns_conf.c

+23
Original file line numberDiff line numberDiff line change
@@ -2994,6 +2994,24 @@ static int _config_bind_ip_parser_ipset(struct dns_bind_ip *bind_ip, unsigned in
29942994
return -1;
29952995
}
29962996

2997+
static int _bind_is_ip_valid(const char *ip)
2998+
{
2999+
struct sockaddr_storage addr;
3000+
socklen_t addr_len = sizeof(addr);
3001+
char ip_check[MAX_IP_LEN];
3002+
int port_check = 0;
3003+
3004+
if (parse_ip(ip, ip_check, &port_check) != 0) {
3005+
return -1;
3006+
}
3007+
3008+
if (getaddr_by_host(ip_check, (struct sockaddr *)&addr, &addr_len) != 0) {
3009+
return -1;
3010+
}
3011+
3012+
return 0;
3013+
}
3014+
29973015
static int _config_bind_ip(int argc, char *argv[], DNS_BIND_TYPE type)
29983016
{
29993017
int index = dns_conf_bind_ip_num;
@@ -3040,6 +3058,11 @@ static int _config_bind_ip(int argc, char *argv[], DNS_BIND_TYPE type)
30403058
return 0;
30413059
}
30423060

3061+
if (_bind_is_ip_valid(ip) != 0) {
3062+
tlog(TLOG_ERROR, "bind ip address invalid: %s", ip);
3063+
return -1;
3064+
}
3065+
30433066
for (i = 0; i < dns_conf_bind_ip_num; i++) {
30443067
bind_ip = &dns_conf_bind_ip[i];
30453068
if (bind_ip->type != type) {

src/dns_server.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -8705,7 +8705,7 @@ static int _dns_create_socket(const char *host_ip, int type)
87058705
snprintf(port_str, sizeof(port_str), "%d", port);
87068706
gai = _dns_server_getaddr(host, port_str, type, 0);
87078707
if (gai == NULL) {
8708-
tlog(TLOG_ERROR, "get address failed.\n");
8708+
tlog(TLOG_ERROR, "get address failed.");
87098709
goto errout;
87108710
}
87118711

@@ -8771,6 +8771,8 @@ static int _dns_create_socket(const char *host_ip, int type)
87718771
if (gai) {
87728772
freeaddrinfo(gai);
87738773
}
8774+
8775+
tlog(TLOG_ERROR, "add server failed, host-ip: %s, type: %d", host_ip, type);
87748776
return -1;
87758777
}
87768778

@@ -9262,6 +9264,8 @@ int dns_server_init(void)
92629264
INIT_LIST_HEAD(&server.conn_list);
92639265
time(&server.cache_save_time);
92649266
atomic_set(&server.request_num, 0);
9267+
pthread_mutex_init(&server.request_list_lock, NULL);
9268+
INIT_LIST_HEAD(&server.request_list);
92659269

92669270
epollfd = epoll_create1(EPOLL_CLOEXEC);
92679271
if (epollfd < 0) {
@@ -9275,8 +9279,6 @@ int dns_server_init(void)
92759279
goto errout;
92769280
}
92779281

9278-
pthread_mutex_init(&server.request_list_lock, NULL);
9279-
INIT_LIST_HEAD(&server.request_list);
92809282
server.epoll_fd = epollfd;
92819283
atomic_set(&server.run, 1);
92829284

0 commit comments

Comments
 (0)