Ver Fonte

dns_conf: check if the IP of bind is valid

Nick Peng há 1 ano atrás
pai
commit
fc279fbcc7
2 ficheiros alterados com 28 adições e 3 exclusões
  1. 23 0
      src/dns_conf.c
  2. 5 3
      src/dns_server.c

+ 23 - 0
src/dns_conf.c

@@ -2994,6 +2994,24 @@ errout:
 	return -1;
 }
 
+static int _bind_is_ip_valid(const char *ip)
+{
+	struct sockaddr_storage addr;
+	socklen_t addr_len = sizeof(addr);
+	char ip_check[MAX_IP_LEN];
+	int port_check = 0;
+
+	if (parse_ip(ip, ip_check, &port_check) != 0) {
+		return -1;
+	}
+
+	if (getaddr_by_host(ip_check, (struct sockaddr *)&addr, &addr_len) != 0) {
+		return -1;
+	}
+
+	return 0;
+}
+
 static int _config_bind_ip(int argc, char *argv[], DNS_BIND_TYPE type)
 {
 	int index = dns_conf_bind_ip_num;
@@ -3040,6 +3058,11 @@ static int _config_bind_ip(int argc, char *argv[], DNS_BIND_TYPE type)
 		return 0;
 	}
 
+	if (_bind_is_ip_valid(ip) != 0) {
+		tlog(TLOG_ERROR, "bind ip address invalid: %s", ip);
+		return -1;
+	}
+
 	for (i = 0; i < dns_conf_bind_ip_num; i++) {
 		bind_ip = &dns_conf_bind_ip[i];
 		if (bind_ip->type != type) {

+ 5 - 3
src/dns_server.c

@@ -8705,7 +8705,7 @@ static int _dns_create_socket(const char *host_ip, int type)
 	snprintf(port_str, sizeof(port_str), "%d", port);
 	gai = _dns_server_getaddr(host, port_str, type, 0);
 	if (gai == NULL) {
-		tlog(TLOG_ERROR, "get address failed.\n");
+		tlog(TLOG_ERROR, "get address failed.");
 		goto errout;
 	}
 
@@ -8771,6 +8771,8 @@ errout:
 	if (gai) {
 		freeaddrinfo(gai);
 	}
+
+	tlog(TLOG_ERROR, "add server failed, host-ip: %s, type: %d", host_ip, type);
 	return -1;
 }
 
@@ -9262,6 +9264,8 @@ int dns_server_init(void)
 	INIT_LIST_HEAD(&server.conn_list);
 	time(&server.cache_save_time);
 	atomic_set(&server.request_num, 0);
+	pthread_mutex_init(&server.request_list_lock, NULL);
+	INIT_LIST_HEAD(&server.request_list);
 
 	epollfd = epoll_create1(EPOLL_CLOEXEC);
 	if (epollfd < 0) {
@@ -9275,8 +9279,6 @@ int dns_server_init(void)
 		goto errout;
 	}
 
-	pthread_mutex_init(&server.request_list_lock, NULL);
-	INIT_LIST_HEAD(&server.request_list);
 	server.epoll_fd = epollfd;
 	atomic_set(&server.run, 1);