Skip to content

Commit

Permalink
Merge branch 'pymumu:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
PikuZheng authored Jul 31, 2022
2 parents 56d090a + ac042e8 commit 09a0207
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 56 deletions.
42 changes: 1 addition & 41 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ static int _dns_decode_an(struct dns_context *context, dns_rr_type type)
}

if (context->ptr - opt_start != rr_len) {
tlog(TLOG_ERROR, "opt length mismatch, %s\n", domain);
tlog(TLOG_DEBUG, "opt length mismatch, %s\n", domain);
return -1;
}

Expand Down Expand Up @@ -2223,43 +2223,3 @@ int dns_packet_update(unsigned char *data, int size, struct dns_update_param *pa

return 0;
}

#if 0
void dns_debug(void)
{
unsigned char data[1024];
ssize_t len;
char buff[4096];

int fd = open("dns.bin", O_RDWR);
if (fd < 0) {
return;
}
len = read(fd, data, 1024);
close(fd);
if (len < 0) {
return;
}

struct dns_packet *packet = (struct dns_packet *)buff;
if (dns_decode(packet, 4096, data, len) != 0) {
tlog(TLOG_ERROR, "decode failed.\n");
}

memset(data, 0, sizeof(data));
len = dns_encode(data, 1024, packet);
if (len < 0) {
tlog(TLOG_ERROR, "encode failed.");
}

fd = open("dns-cmp.bin", O_CREAT | O_TRUNC | O_RDWR, 0660);
write(fd, data, len);
close(fd);

packet = (struct dns_packet *)buff;
if (dns_decode(packet, 4096, data, len) != 0) {
tlog(TLOG_ERROR, "decode failed.\n");
}

}
#endif
16 changes: 10 additions & 6 deletions src/dns_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ static int _dns_client_add_to_group(const char *group_name, struct dns_server_in
return -1;
}

static int _dns_client_add_to_pending_group(const char *group_name, char *server_ip, int port, dns_server_type_t server_type)
static int _dns_client_add_to_pending_group(const char *group_name, char *server_ip, int port,
dns_server_type_t server_type)
{
struct dns_server_pending *item = NULL;
struct dns_server_pending *tmp = NULL;
Expand Down Expand Up @@ -621,8 +622,8 @@ static int _dns_client_add_to_pending_group(const char *group_name, char *server
}

/* add server to group */
static int _dns_client_add_to_group_pending(const char *group_name, char *server_ip, int port, dns_server_type_t server_type,
int ispending)
static int _dns_client_add_to_group_pending(const char *group_name, char *server_ip, int port,
dns_server_type_t server_type, int ispending)
{
struct dns_server_info *server_info = NULL;

Expand Down Expand Up @@ -1591,8 +1592,11 @@ static int _dns_client_recv(struct dns_server_info *server_info, unsigned char *
len = dns_decode(packet, DNS_PACKSIZE, inpacket, inpacket_len);
if (len != 0) {
char host_name[DNS_MAX_CNAME_LEN];
tlog(TLOG_WARN, "decode failed, packet len = %d, tc = %d, id = %d, from = %s\n", inpacket_len, packet->head.tc,
tlog(TLOG_INFO, "decode failed, packet len = %d, tc = %d, id = %d, from = %s\n", inpacket_len, packet->head.tc,
packet->head.id, gethost_by_addr(host_name, sizeof(host_name), from));
if (dns_save_fail_packet) {
dns_packet_save(dns_save_fail_packet_dir, "client", host_name, inpacket, inpacket_len);
}
return -1;
}

Expand Down Expand Up @@ -1721,7 +1725,7 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)

fd = socket(server_info->ai_family, SOCK_STREAM, 0);
if (fd < 0) {
tlog(TLOG_ERROR, "create socket failed.");
tlog(TLOG_ERROR, "create socket failed, %s", strerror(errno));
goto errout;
}

Expand All @@ -1732,7 +1736,7 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)

/* enable tcp fast open */
if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &yes, sizeof(yes)) != 0) {
tlog(TLOG_DEBUG, "enable TCP fast open failed.");
tlog(TLOG_DEBUG, "enable TCP fast open failed, %s", strerror(errno));
}

setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
Expand Down
7 changes: 7 additions & 0 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ int dns_conf_ipset_timeout_enable;

char dns_conf_user[DNS_CONF_USRNAME_LEN];

int dns_save_fail_packet;
char dns_save_fail_packet_dir[DNS_MAX_PATH];

/* ECS */
struct dns_edns_client_subnet dns_conf_ipv4_ecs;
struct dns_edns_client_subnet dns_conf_ipv6_ecs;
Expand Down Expand Up @@ -1920,6 +1923,8 @@ static struct config_item _config_item[] = {
CONF_STRING("ca-file", (char *)&dns_conf_ca_file, DNS_MAX_PATH),
CONF_STRING("ca-path", (char *)&dns_conf_ca_path, DNS_MAX_PATH),
CONF_STRING("user", (char *)&dns_conf_user, sizeof(dns_conf_user)),
CONF_YESNO("debug-save-fail-packet", &dns_save_fail_packet),
CONF_STRING("debug-save-fail-packet-dir", (char *)&dns_save_fail_packet_dir, sizeof(dns_save_fail_packet_dir)),
CONF_CUSTOM("conf-file", config_addtional_file, NULL),
CONF_END(),
};
Expand Down Expand Up @@ -2068,6 +2073,8 @@ static int _dns_conf_load_pre(void)

_dns_ping_cap_check();

safe_strncpy(dns_save_fail_packet_dir, SMARTDNS_DEBUG_DIR, sizeof(dns_save_fail_packet_dir));

return 0;

errout:
Expand Down
4 changes: 4 additions & 0 deletions src/dns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern "C" {
#define SMARTDNS_LOG_FILE "/var/log/smartdns/smartdns.log"
#define SMARTDNS_AUDIT_FILE "/var/log/smartdns/smartdns-audit.log"
#define SMARTDNS_CACHE_FILE "/tmp/smartdns.cache"
#define SMARTDNS_DEBUG_DIR "/tmp/smartdns"

enum domain_rule {
DOMAIN_RULE_FLAGS = 0,
Expand Down Expand Up @@ -321,6 +322,9 @@ extern struct dns_edns_client_subnet dns_conf_ipv6_ecs;

extern char dns_conf_sni_proxy_ip[DNS_MAX_IPLEN];

extern int dns_save_fail_packet;
extern char dns_save_fail_packet_dir[DNS_MAX_PATH];

void dns_server_load_exit(void);

int dns_server_load_conf(const char *file);
Expand Down
8 changes: 6 additions & 2 deletions src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ static void _dns_server_audit_log(struct dns_server_post_context *context)
tm.min, tm.sec, tm.usec / 1000);

tlog_printf(dns_audit, "%s %s query %s, type %d, time %lums, speed: %.1fms, result %s\n", req_time, req_host,
request->domain, request->qtype, get_tick_count() - request->send_tick, ((float)request->ping_time) / 10,
req_result);
request->domain, request->qtype, get_tick_count() - request->send_tick,
((float)request->ping_time) / 10, req_result);
}

static void _dns_rrs_result_log(struct dns_server_post_context *context, struct dns_ip_address *addr_map)
Expand Down Expand Up @@ -3533,6 +3533,7 @@ static int _dns_server_process_cache_packet(struct dns_request *request, struct
request->ping_time = dns_cache->info.speed;

if (dns_decode(context.packet, context.packet_maxlen, cache_packet->data, cache_packet->head.size) != 0) {
tlog(TLOG_ERROR, "decode cache failed, %d, %d", context.packet_maxlen, context.inpacket_len);
return -1;
}

Expand Down Expand Up @@ -4159,6 +4160,9 @@ static int _dns_server_recv(struct dns_server_conn_head *conn, unsigned char *in
if (decode_len < 0) {
tlog(TLOG_DEBUG, "decode failed.\n");
ret = RECV_ERROR_INVALID_PACKET;
if (dns_save_fail_packet) {
dns_packet_save(dns_save_fail_packet_dir, "server", name, inpacket, inpacket_len);
}
goto errout;
}

Expand Down
6 changes: 5 additions & 1 deletion src/smartdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ int main(int argc, char *argv[])
sigemptyset(&empty_sigblock);
sigprocmask(SIG_SETMASK, &empty_sigblock, NULL);

while ((opt = getopt(argc, argv, "fhc:p:Svx")) != -1) {
while ((opt = getopt(argc, argv, "fhc:p:SvxN:")) != -1) {
switch (opt) {
case 'f':
is_forground = 1;
Expand All @@ -545,6 +545,10 @@ int main(int argc, char *argv[])
_show_version();
return 0;
break;
#ifdef DEBUG
case 'N':
return dns_packet_debug(optarg);
#endif
case 'h':
_help();
return 1;
Expand Down
43 changes: 40 additions & 3 deletions src/tlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct tlog_log {
int zip_pid;
int multi_log;
int logscreen;
int no_write_log;
int segment_log;
int max_line_size;

Expand Down Expand Up @@ -216,7 +217,6 @@ static int _tlog_mkdir(const char *path)
}

if (mkdir(path_c, 0750) != 0) {
fprintf(stderr, "create directory %s failed, %s\n", path_c, strerror(errno));
return -1;
}

Expand Down Expand Up @@ -1130,6 +1130,10 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
unused = write(STDOUT_FILENO, buff, bufflen);
}

if (log->no_write_log) {
return 0;
}

/* if log file size exceeds threshold, start to compress */
if (log->multi_log && log->fd > 0) {
log->filesize = lseek(log->fd, 0, SEEK_END);
Expand Down Expand Up @@ -1160,7 +1164,15 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)

char logfile[PATH_MAX * 2];
if (_tlog_mkdir(log->logdir) != 0) {
fprintf(stderr, "create log dir %s failed.\n", log->logdir);
if (print_errmsg == 0) {
return -1;
}
print_errmsg = 0;
fprintf(stderr, "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");
tlog_logscreen_only(log, 1);
}
return -1;
}
snprintf(logfile, sizeof(logfile), "%s/%s", log->logdir, log->logname);
Expand Down Expand Up @@ -1574,11 +1586,26 @@ static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
log->logscreen = (enable != 0) ? 1 : 0;
}

static void _tlog_log_setlogscreen_only(struct tlog_log *log, int enable)
{
if (log == NULL) {
return;
}

log->logscreen = (enable != 0) ? 1 : 0;
log->no_write_log = (enable != 0) ? 1 : 0;
}

void tlog_setlogscreen(int enable)
{
_tlog_log_setlogscreen(tlog.root, enable);
}

void tlog_setlogscreen_only(int enable)
{
_tlog_log_setlogscreen_only(tlog.root, enable);
}

int tlog_write_log(char *buff, int bufflen)
{
if (unlikely(tlog.root == NULL)) {
Expand All @@ -1597,6 +1624,15 @@ void tlog_logscreen(tlog_log *log, int enable)
_tlog_log_setlogscreen(log, enable);
}

void tlog_logscreen_only(tlog_log *log, int enable)
{
if (log == NULL) {
return;
}

_tlog_log_setlogscreen_only(log, enable);
}

int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
{
if (log == NULL) {
Expand Down Expand Up @@ -1830,13 +1866,13 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
}
tlog_reg_output_func(log, _tlog_root_write_log);

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));
goto errout;
}

tlog.root = log;
if (flag & TLOG_SUPPORT_FORK) {
pthread_atfork(&tlog_fork_prepare, &tlog_fork_parent, &tlog_fork_child);
}
Expand All @@ -1852,6 +1888,7 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
pthread_cond_destroy(&tlog.cond);
pthread_mutex_destroy(&tlog.lock);
tlog.run = 0;
tlog.root = NULL;

_tlog_close(log, 1);

Expand Down
6 changes: 6 additions & 0 deletions src/tlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ extern void tlog_set_logfile(const char *logfile);
/* enalbe log to screen */
extern void tlog_setlogscreen(int enable);

/* output log to screen only */
extern void tlog_setlogscreen_only(int enable);

/* enalbe early log to screen */
extern void tlog_set_early_printf(int enable);

Expand Down Expand Up @@ -184,6 +187,9 @@ extern int tlog_vprintf(tlog_log *log, const char *format, va_list ap);
/* enalbe log to screen */
extern void tlog_logscreen(tlog_log *log, int enable);

/* enalbe log to screen only*/
extern void tlog_logscreen_only(tlog_log *log, int enable);

/* register output callback */
typedef int (*tlog_output_func)(struct tlog_log *log, const char *buff, int bufflen);
extern int tlog_reg_output_func(tlog_log *log, tlog_output_func output);
Expand Down
Loading

0 comments on commit 09a0207

Please sign in to comment.