Browse Source

smartdns: some bug fixes

Nick Peng 1 year ago
parent
commit
04972bd243
8 changed files with 48 additions and 5 deletions
  1. 2 1
      ReadMe.md
  2. 1 1
      ReadMe_en.md
  3. 10 1
      src/dns_conf.c
  4. 1 0
      src/dns_conf.h
  5. 1 1
      src/dns_server.c
  6. 4 0
      src/http_parse.c
  7. 25 0
      src/smartdns.c
  8. 4 1
      src/tlog.c

+ 2 - 1
ReadMe.md

@@ -75,7 +75,8 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
 从对比看出,SmartDNS 找到了访问 www.baidu.com 最快的 IP 地址,比阿里 DNS 速度快了 5 倍。
 
 ## 特性
-1. **多虚拟DNS服务器**
+
+1. **多虚拟DNS服务器**  
    支持多个虚拟DNS服务器,不同虚拟DNS服务器不同的端口,规则,客户端。
 
 1. **多 DNS 上游服务器**  

+ 1 - 1
ReadMe_en.md

@@ -75,7 +75,7 @@ From the comparison, smartdns found the fastest IP address to visit www.baidu.co
 
 ## Features
 
-1. **Multiple Virtual DNS server**
+1. **Multiple Virtual DNS server**  
    Support multiple virtual DNS servers with different ports, rules, and clients.
 
 1. **Multiple upstream DNS servers**  

+ 10 - 1
src/dns_conf.c

@@ -163,6 +163,7 @@ char dns_save_fail_packet_dir[DNS_MAX_PATH];
 char dns_resolv_file[DNS_MAX_PATH];
 int dns_no_pidfile;
 int dns_no_daemon;
+int dns_restart_on_crash;
 size_t dns_socket_buff_size;
 
 struct hash_table conf_file_table;
@@ -696,6 +697,10 @@ static int _config_current_group_pop_all(void)
 		_config_current_group_pop();
 	}
 
+	if (dns_conf_default_group_info == NULL) {
+		return 0;
+	}
+
 	list_del(&dns_conf_default_group_info->list);
 	free(dns_conf_default_group_info);
 	dns_conf_default_group_info = NULL;
@@ -5792,6 +5797,7 @@ static struct config_item _config_item[] = {
 	CONF_YESNO("debug-save-fail-packet", &dns_save_fail_packet),
 	CONF_YESNO("no-pidfile", &dns_no_pidfile),
 	CONF_YESNO("no-daemon", &dns_no_daemon),
+	CONF_YESNO("restart-on-crash", &dns_restart_on_crash),
 	CONF_SIZE("socket-buff-size", &dns_socket_buff_size, 0, 1024 * 1024 * 8),
 	CONF_CUSTOM("plugin", _config_plugin, NULL),
 	CONF_STRING("resolv-file", (char *)&dns_resolv_file, sizeof(dns_resolv_file)),
@@ -5978,7 +5984,10 @@ static int _dns_server_load_conf_init(void)
 	hash_init(dns_conf_srv_record_table.srv);
 	hash_init(dns_conf_plugin_table.plugins);
 
-	_config_current_group_push_default();
+	if (_config_current_group_push_default() != 0) {
+		tlog(TLOG_ERROR, "init default group failed.");
+		return -1;
+	}
 
 	return 0;
 }

+ 1 - 0
src/dns_conf.h

@@ -697,6 +697,7 @@ extern char dns_resolv_file[DNS_MAX_PATH];
 
 extern int dns_no_pidfile;
 extern int dns_no_daemon;
+extern int dns_restart_on_crash;
 extern size_t dns_socket_buff_size;
 
 void dns_server_load_exit(void);

+ 1 - 1
src/dns_server.c

@@ -6604,7 +6604,7 @@ static int _dns_server_parser_request(struct dns_request *request, struct dns_pa
 		case DNS_OPT_T_TCP_KEEPALIVE: {
 			unsigned short idle_timeout = 0;
 			ret = dns_get_OPT_TCP_KEEPALIVE(rrs, &idle_timeout);
-			if (idle_timeout == 0) {
+			if (idle_timeout == 0 || ret != 0) {
 				continue;
 			}
 

+ 4 - 0
src/http_parse.c

@@ -328,6 +328,10 @@ static int _http_head_parse_params(struct http_head *http_head, char *url, int u
 	char *field = NULL;
 	char *value = NULL;
 
+	if (url == NULL) {
+		return -1;
+	}
+
 	param_start = strstr(url, "?");
 	if (param_start == NULL) {
 		return 0;

+ 25 - 0
src/smartdns.c

@@ -987,6 +987,17 @@ void smartdns_restart(void)
 	exit_restart = 1;
 }
 
+static int smartdns_enter_monitor_mode(int argc, char *argv[], int no_deamon)
+{
+	setenv("SMARTDNS_RESTART_ON_CRASH", "1", 1);
+	if (no_deamon == 1) {
+		setenv("SMARTDNS_NO_DAEMON", "1", 1);
+	}
+	execv(argv[0], argv);
+	tlog(TLOG_ERROR, "execv failed, %s", strerror(errno));
+	return -1;
+}
+
 #ifdef TEST
 
 static smartdns_post_func _smartdns_post = NULL;
@@ -1096,6 +1107,16 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (getenv("SMARTDNS_RESTART_ON_CRASH") != NULL) {
+		restart_when_crash = 1;
+		unsetenv("SMARTDNS_RESTART_ON_CRASH");
+	}
+
+	if (getenv("SMARTDNS_NO_DAEMON") != NULL) {
+		is_run_as_daemon = 0;
+		unsetenv("SMARTDNS_NO_DAEMON");
+	}
+
 	smartdns_run_monitor_ret init_ret = _smartdns_run_monitor(restart_when_crash, is_run_as_daemon);
 	if (init_ret != SMARTDNS_RUN_MONITOR_OK) {
 		if (init_ret == SMARTDNS_RUN_MONITOR_EXIT) {
@@ -1115,6 +1136,10 @@ int main(int argc, char *argv[])
 		goto errout;
 	}
 
+	if (dns_restart_on_crash && restart_when_crash == 0) {
+		return smartdns_enter_monitor_mode(argc, argv, dns_no_daemon || !is_run_as_daemon);
+	}
+
 	if (dns_no_daemon || restart_when_crash) {
 		is_run_as_daemon = 0;
 	}

+ 4 - 1
src/tlog.c

@@ -1574,8 +1574,11 @@ static void _tlog_work_write(struct tlog_log *log, int log_len, int log_extlen,
             msg += sizeof(struct tlog_segment_log_head);
             log_head->info.level = TLOG_WARN;
         }
+
         int len = snprintf(msg, msg - dropmsg, "[Total Dropped %d Messages]\n", log_dropped);
-        log_head->len = len;
+        if (log_head) {
+            log_head->len = len;
+        }
         _tlog_write_output_func(log, dropmsg, strnlen(dropmsg, sizeof(dropmsg)));
     }
 }