Skip to content

Commit d02bcea

Browse files
committed
dns_conf: fix server-https options issue.
1 parent f1debd0 commit d02bcea

File tree

6 files changed

+44
-34
lines changed

6 files changed

+44
-34
lines changed

.clang-tidy

+2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Checks: >
1818
-bugprone-suspicious-memory-comparison,
1919
-bugprone-not-null-terminated-result,
2020
-bugprone-signal-handler,
21+
-bugprone-assignment-in-if-condition,
2122
-concurrency-mt-unsafe,
23+
-modernize-macro-to-enum,
2224
-misc-unused-parameters,
2325
-misc-misplaced-widening-cast,
2426
-misc-no-recursion,

src/dns_client.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct dns_server_info {
9393
/* server ping handle */
9494
struct ping_host_struct *ping_host;
9595

96-
char ip[DNS_HOSTNAME_LEN];
96+
char ip[DNS_MAX_HOSTNAME];
9797
int port;
9898
char proxy_name[DNS_HOSTNAME_LEN];
9999
/* server type */
@@ -3327,6 +3327,7 @@ static int _dns_client_send_https(struct dns_server_info *server_info, void *pac
33273327
http_len = snprintf((char *)inpacket, DNS_IN_PACKSIZE,
33283328
"POST %s HTTP/1.1\r\n"
33293329
"Host: %s\r\n"
3330+
"User-Agent: smartdns\r\n"
33303331
"content-type: application/dns-message\r\n"
33313332
"Content-Length: %d\r\n"
33323333
"\r\n",

src/dns_conf.c

+34-27
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
504504
unsigned char *spki = NULL;
505505
int drop_packet_latency_ms = 0;
506506
int is_bootstrap_dns = 0;
507-
int is_hostip_set = 0;
507+
char host_ip[DNS_MAX_IPLEN] = {0};
508+
int no_tls_host_name = 0;
509+
int no_tls_host_verify = 0;
508510

509511
int ttl = 0;
510512
/* clang-format off */
@@ -576,14 +578,6 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
576578
}
577579
}
578580

579-
if (type == DNS_SERVER_HTTPS) {
580-
safe_strncpy(server->hostname, server->server, sizeof(server->hostname));
581-
safe_strncpy(server->httphost, server->server, sizeof(server->httphost));
582-
if (server->path[0] == 0) {
583-
safe_strncpy(server->path, "/", sizeof(server->path));
584-
}
585-
}
586-
587581
/* if port is not defined, set port to default 53 */
588582
if (port == PORT_NOT_DEFINED) {
589583
port = default_port;
@@ -624,6 +618,7 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
624618

625619
case 'k': {
626620
server->skip_check_cert = 1;
621+
no_tls_host_verify = 1;
627622
break;
628623
}
629624
case 'b': {
@@ -655,10 +650,10 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
655650
break;
656651
}
657652
case 258: {
658-
if (check_is_ipaddr(server->server) != 0) {
659-
_conf_domain_rule_address(server->server, optarg);
660-
is_hostip_set = 1;
653+
if (check_is_ipaddr(optarg) != 0) {
654+
goto errout;
661655
}
656+
safe_strncpy(host_ip, optarg, DNS_MAX_IPLEN);
662657
break;
663658
}
664659
case 259: {
@@ -669,6 +664,7 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
669664
safe_strncpy(server->hostname, optarg, DNS_MAX_CNAME_LEN);
670665
if (strncmp(server->hostname, "-", 2) == 0) {
671666
server->hostname[0] = '\0';
667+
no_tls_host_name = 1;
672668
}
673669
break;
674670
}
@@ -678,6 +674,10 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
678674
}
679675
case 262: {
680676
safe_strncpy(server->tls_host_verify, optarg, DNS_MAX_CNAME_LEN);
677+
if (strncmp(server->tls_host_verify, "-", 2) == 0) {
678+
server->tls_host_verify[0] = '\0';
679+
no_tls_host_verify = 1;
680+
}
681681
break;
682682
}
683683
default:
@@ -686,23 +686,30 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
686686
}
687687
}
688688

689-
/* if server is domain name, then verify domain */
690-
if (server->tls_host_verify[0] == '\0' && check_is_ipaddr(server->server) != 0) {
691-
safe_strncpy(server->tls_host_verify, server->server, DNS_MAX_CNAME_LEN);
692-
}
693-
694-
/* update address rules for host-ip */
695-
if (is_hostip_set == 1) {
696-
struct dns_domain_rule *rule = _config_domain_rule_get(server->server);
697-
if (rule) {
698-
if (rule->rules[DOMAIN_RULE_ADDRESS_IPV4] != NULL && rule->rules[DOMAIN_RULE_ADDRESS_IPV6] == NULL) {
699-
_conf_domain_rule_address(server->server, "#6");
700-
} else if (rule->rules[DOMAIN_RULE_ADDRESS_IPV4] == NULL && rule->rules[DOMAIN_RULE_ADDRESS_IPV6] != NULL) {
701-
_conf_domain_rule_address(server->server, "#4");
702-
}
689+
if (check_is_ipaddr(server->server) != 0) {
690+
/* if server is domain name, then verify domain */
691+
if (server->tls_host_verify[0] == '\0' && no_tls_host_verify == 0) {
692+
safe_strncpy(server->tls_host_verify, server->server, DNS_MAX_CNAME_LEN);
693+
}
694+
695+
if (server->hostname[0] == '\0' && no_tls_host_name == 0) {
696+
safe_strncpy(server->hostname, server->server, DNS_MAX_CNAME_LEN);
697+
}
698+
699+
if (server->httphost[0] == '\0') {
700+
safe_strncpy(server->httphost, server->server, DNS_MAX_CNAME_LEN);
701+
}
702+
703+
if (host_ip[0] != '\0') {
704+
safe_strncpy(server->server, host_ip, DNS_MAX_IPLEN);
703705
}
704706
}
705707

708+
/* if server is domain name, then verify domain */
709+
if (server->tls_host_verify[0] == '\0' && server->hostname[0] != '\0' && no_tls_host_verify == 0) {
710+
safe_strncpy(server->tls_host_verify, server->hostname, DNS_MAX_CNAME_LEN);
711+
}
712+
706713
/* add new server */
707714
server->type = type;
708715
server->port = port;
@@ -915,7 +922,7 @@ static int _config_setup_domain_key(const char *domain, char *domain_key, int do
915922
return 0;
916923
}
917924

918-
static struct dns_domain_rule *_config_domain_rule_get(const char *domain)
925+
static __attribute__((unused)) struct dns_domain_rule *_config_domain_rule_get(const char *domain)
919926
{
920927
char domain_key[DNS_MAX_CONF_CNAME_LEN];
921928
int len = 0;

src/dns_conf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ struct dns_edns_client_subnet {
323323
};
324324

325325
struct dns_servers {
326-
char server[DNS_MAX_IPLEN];
326+
char server[DNS_MAX_CNAME_LEN];
327327
unsigned short port;
328328
unsigned int result_flag;
329329
unsigned int server_flag;

src/dns_server.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2887,7 +2887,7 @@ static int _dns_server_process_ip_alias(struct dns_request *request, struct dns_
28872887
{
28882888
int addr_num = 0;
28892889

2890-
if (alias == 0) {
2890+
if (alias == NULL) {
28912891
return 0;
28922892
}
28932893

src/smartdns.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include "hashtable.h"
2929
#include "list.h"
3030
#include "rbtree.h"
31+
#include "timer.h"
3132
#include "tlog.h"
3233
#include "util.h"
33-
#include "timer.h"
3434
#include <errno.h>
3535
#include <fcntl.h>
3636
#include <getopt.h>
@@ -157,7 +157,7 @@ static void _help(void)
157157
" -v display version.\n"
158158
" -h show this help message.\n"
159159

160-
"Online help: http://pymumu.github.io/smartdns\n"
160+
"Online help: https://pymumu.github.io/smartdns\n"
161161
"Copyright (C) Nick Peng <[email protected]>\n"
162162
;
163163
/* clang-format on */
@@ -773,7 +773,7 @@ int main(int argc, char *argv[])
773773
struct stat sb;
774774

775775
static struct option long_options[] = {
776-
{"cache-print", required_argument, 0, 256}, {"help", no_argument, 0, 'h'}, {NULL, 0, 0, 0}};
776+
{"cache-print", required_argument, NULL, 256}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}};
777777

778778
safe_strncpy(config_file, SMARTDNS_CONF_FILE, MAX_LINE_LEN);
779779

@@ -788,7 +788,7 @@ int main(int argc, char *argv[])
788788
sigprocmask(SIG_SETMASK, &empty_sigblock, NULL);
789789
smartdns_close_allfds();
790790

791-
while ((opt = getopt_long(argc, argv, "fhc:p:SvxN:", long_options, 0)) != -1) {
791+
while ((opt = getopt_long(argc, argv, "fhc:p:SvxN:", long_options, NULL)) != -1) {
792792
switch (opt) {
793793
case 'f':
794794
is_run_as_daemon = 0;

0 commit comments

Comments
 (0)