dns_server: optimize result callback and update tlog.
This commit is contained in:
@@ -2820,7 +2820,7 @@ static int _dns_client_verify_common_name(struct dns_server_info *server_info, X
|
||||
|
||||
tlog(TLOG_DEBUG, "peer SAN: %s", dns->data);
|
||||
if (_dns_client_tls_matchName(tls_host_verify, (char *)dns->data, dns->length) == 0) {
|
||||
tlog(TLOG_INFO, "peer SAN match: %s", dns->data);
|
||||
tlog(TLOG_DEBUG, "peer SAN match: %s", dns->data);
|
||||
return 0;
|
||||
}
|
||||
} break;
|
||||
@@ -3896,13 +3896,13 @@ static int _dns_client_pending_server_resolve(const struct dns_result *result, v
|
||||
struct dns_server_pending *pending = user_ptr;
|
||||
int ret = 0;
|
||||
|
||||
if (result->rtcode == DNS_RC_NXDOMAIN) {
|
||||
if (result->rtcode == DNS_RC_NXDOMAIN || result->ip_num == 0 || result->has_soa == 1) {
|
||||
pending->has_soa = 1;
|
||||
}
|
||||
|
||||
if (result->addr_type == DNS_T_A) {
|
||||
pending->ping_time_v4 = -1;
|
||||
if (result->rtcode == DNS_RC_NOERROR) {
|
||||
if (result->rtcode == DNS_RC_NOERROR && result->ip_num > 0) {
|
||||
pending->has_v4 = 1;
|
||||
pending->ping_time_v4 = result->ping_time;
|
||||
pending->has_soa = 0;
|
||||
@@ -3910,7 +3910,7 @@ static int _dns_client_pending_server_resolve(const struct dns_result *result, v
|
||||
}
|
||||
} else if (result->addr_type == DNS_T_AAAA) {
|
||||
pending->ping_time_v6 = -1;
|
||||
if (result->rtcode == DNS_RC_NOERROR) {
|
||||
if (result->rtcode == DNS_RC_NOERROR && result->ip_num > 0) {
|
||||
pending->has_v6 = 1;
|
||||
pending->ping_time_v6 = result->ping_time;
|
||||
pending->has_soa = 0;
|
||||
|
||||
@@ -1489,28 +1489,6 @@ errout:
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int _dns_result_callback_nxdomain(struct dns_request *request)
|
||||
{
|
||||
char ip[DNS_MAX_CNAME_LEN];
|
||||
unsigned int ping_time = -1;
|
||||
|
||||
ip[0] = 0;
|
||||
if (request->result_callback == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct dns_result result;
|
||||
result.domain = request->domain;
|
||||
result.rtcode = request->rcode;
|
||||
result.addr_type = request->qtype;
|
||||
result.ip = ip;
|
||||
result.ping_time = ping_time;
|
||||
memset(&result.ip_addr, 0, sizeof(result.ip_addr));
|
||||
result.ip_num = 0;
|
||||
|
||||
return request->result_callback(&result, request->user_ptr);
|
||||
}
|
||||
|
||||
static int _dns_result_callback(struct dns_server_post_context *context)
|
||||
{
|
||||
struct dns_result result;
|
||||
@@ -1526,14 +1504,6 @@ static int _dns_result_callback(struct dns_server_post_context *context)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (request->has_soa || context->do_force_soa || context->ip_num == 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (request->has_ip == 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ip[0] = 0;
|
||||
memset(&result, 0, sizeof(result));
|
||||
ping_time = request->ping_time;
|
||||
@@ -1541,32 +1511,29 @@ static int _dns_result_callback(struct dns_server_post_context *context)
|
||||
result.rtcode = request->rcode;
|
||||
result.addr_type = request->qtype;
|
||||
result.ip = ip;
|
||||
result.has_soa = request->has_soa | context->do_force_soa;
|
||||
result.ping_time = ping_time;
|
||||
result.ip_num = 0;
|
||||
for (int i = 0; i < context->ip_num && i < MAX_IP_NUM; i++) {
|
||||
result.ip_addr[i] = context->ip_addr[i];
|
||||
result.ip_num++;
|
||||
|
||||
if (request->has_ip != 0 && context->do_force_soa == 0) {
|
||||
for (int i = 0; i < context->ip_num && i < MAX_IP_NUM; i++) {
|
||||
result.ip_addr[i] = context->ip_addr[i];
|
||||
result.ip_num++;
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
sprintf(ip, "%d.%d.%d.%d", request->ip_addr[0], request->ip_addr[1], request->ip_addr[2],
|
||||
request->ip_addr[3]);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
sprintf(ip, "%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x", request->ip_addr[0],
|
||||
request->ip_addr[1], request->ip_addr[2], request->ip_addr[3], request->ip_addr[4],
|
||||
request->ip_addr[5], request->ip_addr[6], request->ip_addr[7], request->ip_addr[8],
|
||||
request->ip_addr[9], request->ip_addr[10], request->ip_addr[11], request->ip_addr[12],
|
||||
request->ip_addr[13], request->ip_addr[14], request->ip_addr[15]);
|
||||
}
|
||||
}
|
||||
|
||||
if (request->qtype == DNS_T_A) {
|
||||
sprintf(ip, "%d.%d.%d.%d", request->ip_addr[0], request->ip_addr[1], request->ip_addr[2], request->ip_addr[3]);
|
||||
return request->result_callback(&result, request->user_ptr);
|
||||
} else if (request->qtype == DNS_T_AAAA) {
|
||||
sprintf(ip, "%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x", request->ip_addr[0],
|
||||
request->ip_addr[1], request->ip_addr[2], request->ip_addr[3], request->ip_addr[4], request->ip_addr[5],
|
||||
request->ip_addr[6], request->ip_addr[7], request->ip_addr[8], request->ip_addr[9],
|
||||
request->ip_addr[10], request->ip_addr[11], request->ip_addr[12], request->ip_addr[13],
|
||||
request->ip_addr[14], request->ip_addr[15]);
|
||||
return request->result_callback(&result, request->user_ptr);
|
||||
}
|
||||
|
||||
_dns_result_callback_nxdomain(request);
|
||||
|
||||
return 0;
|
||||
out:
|
||||
|
||||
_dns_result_callback_nxdomain(request);
|
||||
return 0;
|
||||
return request->result_callback(&result, request->user_ptr);
|
||||
}
|
||||
|
||||
static int _dns_cache_specify_packet(struct dns_server_post_context *context)
|
||||
|
||||
@@ -58,6 +58,7 @@ struct dns_result {
|
||||
const char *ip;
|
||||
const unsigned char *ip_addr[MAX_IP_NUM];
|
||||
int ip_num;
|
||||
int has_soa;
|
||||
unsigned int ping_time;
|
||||
};
|
||||
|
||||
|
||||
36
src/tlog.c
36
src/tlog.c
@@ -748,7 +748,7 @@ static int _tlog_list_dir(const char *path, list_callback callback, void *userpt
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir == NULL) {
|
||||
fprintf(stderr, "open directory failed, %s\n", strerror(errno));
|
||||
fprintf(stderr, "tlog: open directory failed, %s\n", strerror(errno));
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -859,7 +859,7 @@ static int _tlog_remove_oldlog(struct tlog_log *log)
|
||||
|
||||
/* get total log file number */
|
||||
if (_tlog_list_dir(log->logdir, _tlog_count_log_callback, &count_log) != 0) {
|
||||
fprintf(stderr, "get log file count failed.\n");
|
||||
fprintf(stderr, "tlog: get log file count failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -896,7 +896,7 @@ static int _tlog_log_lock(struct tlog_log *log)
|
||||
snprintf(lock_file, sizeof(lock_file), "%s/%s.lock", log->logdir, log->logname);
|
||||
fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "create pid file failed, %s", strerror(errno));
|
||||
fprintf(stderr, "tlog: create lock file failed, %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1061,8 +1061,14 @@ static int _tlog_archive_log_compressed(struct tlog_log *log)
|
||||
if (pid == 0) {
|
||||
_tlog_close_all_fd();
|
||||
execl(tlog.gzip_cmd, tlog.gzip_cmd, "-1", pending_file, NULL);
|
||||
fprintf(stderr, "tlog: execl gzip failed, no compress\n");
|
||||
log->nocompress = 1;
|
||||
_exit(1);
|
||||
} else if (pid < 0) {
|
||||
if (errno == EPERM || errno == EACCES) {
|
||||
fprintf(stderr, "tlog: vfork failed, errno: %d, no compress\n", errno);
|
||||
log->nocompress = 1;
|
||||
}
|
||||
goto errout;
|
||||
}
|
||||
log->zip_pid = pid;
|
||||
@@ -1195,9 +1201,9 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
return -1;
|
||||
}
|
||||
log->print_errmsg = 0;
|
||||
fprintf(stderr, "create log dir %s failed, %s\n", log->logdir, strerror(errno));
|
||||
fprintf(stderr, "tlog: create log dir %s failed, %s\n", log->logdir, strerror(errno));
|
||||
if (errno == EACCES && log->logscreen == 0) {
|
||||
fprintf(stderr, "no permission to write log file, output log to console\n");
|
||||
fprintf(stderr, "tlog: no permission to write log file, output log to console\n");
|
||||
tlog_logscreen(log, 1);
|
||||
tlog_logcount(log, 0);
|
||||
}
|
||||
@@ -1211,7 +1217,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "open log file %s failed, %s\n", logfile, strerror(errno));
|
||||
fprintf(stderr, "tlog: open log file %s failed, %s\n", logfile, strerror(errno));
|
||||
log->print_errmsg = 0;
|
||||
return -1;
|
||||
}
|
||||
@@ -1752,13 +1758,13 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
|
||||
struct tlog_log *log = NULL;
|
||||
|
||||
if (tlog.run == 0) {
|
||||
fprintf(stderr, "tlog is not initialized.");
|
||||
fprintf(stderr, "tlog: tlog is not initialized.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
log = (struct tlog_log *)malloc(sizeof(*log));
|
||||
if (log == NULL) {
|
||||
fprintf(stderr, "malloc log failed.");
|
||||
fprintf(stderr, "tlog: malloc log failed.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1800,7 +1806,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
|
||||
|
||||
log->buff = (char *)malloc(log->buffsize);
|
||||
if (log->buff == NULL) {
|
||||
fprintf(stderr, "malloc log buffer failed, %s\n", strerror(errno));
|
||||
fprintf(stderr, "tlog: malloc log buffer failed, %s\n", strerror(errno));
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -1888,7 +1894,7 @@ static void tlog_fork_child(void)
|
||||
pthread_attr_init(&attr);
|
||||
int ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "create tlog work thread failed, %s\n", strerror(errno));
|
||||
fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -1910,12 +1916,12 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
|
||||
struct tlog_log *log = NULL;
|
||||
|
||||
if (tlog_format != NULL) {
|
||||
fprintf(stderr, "tlog already initialized.\n");
|
||||
fprintf(stderr, "tlog: already initialized.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (buffsize > 0 && buffsize < TLOG_MAX_LINE_SIZE_SET * 2) {
|
||||
fprintf(stderr, "buffer size is invalid.\n");
|
||||
fprintf(stderr, "tlog: buffer size is invalid.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1932,19 +1938,19 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
|
||||
|
||||
log = tlog_open(logfile, maxlogsize, maxlogcount, buffsize, flag);
|
||||
if (log == NULL) {
|
||||
fprintf(stderr, "init tlog root failed.\n");
|
||||
fprintf(stderr, "tlog: init tlog root failed.\n");
|
||||
goto errout;
|
||||
}
|
||||
tlog_reg_output_func(log, _tlog_root_write_log);
|
||||
|
||||
if ((flag & TLOG_NOCOMPRESS) == 0 && tlog.gzip_cmd[0] == '\0') {
|
||||
fprintf(stderr, "can not find gzip command, disable compress.\n");
|
||||
fprintf(stderr, "tlog: can not find gzip command, disable compress.\n");
|
||||
}
|
||||
|
||||
tlog.root = log;
|
||||
ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "create tlog work thread failed, %s\n", strerror(errno));
|
||||
fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
|
||||
goto errout;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* tinylog
|
||||
* Copyright (C) 2018-2021 Ruilin Peng (Nick) <pymumu@gmail.com>
|
||||
* Copyright (C) 2018-2023 Ruilin Peng (Nick) <pymumu@gmail.com>
|
||||
* https://github.com/pymumu/tinylog
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user