Skip to content

Commit

Permalink
smartdns: some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Jan 26, 2024
1 parent 40d859e commit 42921ce
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rtt min/avg/max/mdev = 5.954/6.133/6.313/0.195 ms
从对比看出,SmartDNS 找到了访问 www.baidu.com 最快的 IP 地址,比阿里 DNS 速度快了 5 倍。

## 特性

1. **多虚拟DNS服务器**
支持多个虚拟DNS服务器,不同虚拟DNS服务器不同的端口,规则,客户端。

Expand Down
11 changes: 10 additions & 1 deletion src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/http_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/smartdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion src/tlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
Expand Down

0 comments on commit 42921ce

Please sign in to comment.