resolve: support port specifier in DNS= setting

This commit is contained in:
Yu Watanabe 2020-07-13 08:58:02 +09:00
parent 395aa34023
commit da9de7385a
8 changed files with 35 additions and 14 deletions

View File

@ -28,15 +28,16 @@ static const char* const dns_stub_listener_mode_table[_DNS_STUB_LISTENER_MODE_MA
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dns_stub_listener_mode, DnsStubListenerMode, DNS_STUB_LISTENER_YES);
static int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) {
_cleanup_free_ char *server_name = NULL;
union in_addr_union address;
int family, r, ifindex = 0;
uint16_t port;
DnsServer *s;
_cleanup_free_ char *server_name = NULL;
assert(m);
assert(word);
r = in_addr_ifindex_name_from_string_auto(word, &family, &address, &ifindex, &server_name);
r = in_addr_port_ifindex_name_from_string_auto(word, &family, &address, &port, &ifindex, &server_name);
if (r < 0)
return r;
@ -44,6 +45,11 @@ static int manager_add_dns_server_by_string(Manager *m, DnsServerType type, cons
if (!dns_server_address_valid(family, &address))
return 0;
/* By default, the port number is determined with the transaction feature level.
* See dns_transaction_port() and dns_server_port(). */
if (IN_SET(port, 53, 853))
port = 0;
/* Filter out duplicates */
s = dns_server_find(manager_get_first_dns_server(m, type), family, &address, ifindex);
if (s) {
@ -57,7 +63,7 @@ static int manager_add_dns_server_by_string(Manager *m, DnsServerType type, cons
return 0;
}
return dns_server_new(m, NULL, type, NULL, family, &address, ifindex, server_name);
return dns_server_new(m, NULL, type, NULL, family, &address, port, ifindex, server_name);
}
int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string) {

View File

@ -447,8 +447,8 @@ static int dns_scope_socket(
return TAKE_FD(fd);
}
int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port, NULL);
int dns_scope_socket_udp(DnsScope *s, DnsServer *server) {
return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, dns_server_port(server), NULL);
}
int dns_scope_socket_tcp(DnsScope *s, int family, const union in_addr_union *address, DnsServer *server, uint16_t port, union sockaddr_union *ret_socket_address) {

View File

@ -75,7 +75,7 @@ void dns_scope_packet_lost(DnsScope *s, usec_t usec);
int dns_scope_emit_udp(DnsScope *s, int fd, DnsPacket *p);
int dns_scope_socket_tcp(DnsScope *s, int family, const union in_addr_union *address, DnsServer *server, uint16_t port, union sockaddr_union *ret_socket_address);
int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port);
int dns_scope_socket_udp(DnsScope *s, DnsServer *server);
DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain);
bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key);

View File

@ -26,6 +26,7 @@ int dns_server_new(
Link *l,
int family,
const union in_addr_union *in_addr,
uint16_t port,
int ifindex,
const char *server_name) {
@ -63,6 +64,7 @@ int dns_server_new(
.type = type,
.family = family,
.address = *in_addr,
.port = port,
.ifindex = ifindex,
.server_name = TAKE_PTR(name),
};
@ -548,6 +550,15 @@ int dns_server_ifindex(const DnsServer *s) {
return 0;
}
uint16_t dns_server_port(const DnsServer *s) {
assert(s);
if (s->port > 0)
return s->port;
return 53;
}
const char *dns_server_string(DnsServer *server) {
assert(server);

View File

@ -56,11 +56,11 @@ struct DnsServer {
int family;
union in_addr_union address;
int ifindex; /* for IPv6 link-local DNS servers */
uint16_t port;
char *server_name;
char *server_string;
char *server_name;
/* The long-lived stream towards this server. */
DnsStream *stream;
@ -102,6 +102,7 @@ int dns_server_new(
Link *link,
int family,
const union in_addr_union *address,
uint16_t port,
int ifindex,
const char *server_string);
@ -124,6 +125,7 @@ int dns_server_adjust_opt(DnsServer *server, DnsPacket *packet, DnsServerFeature
const char *dns_server_string(DnsServer *server);
int dns_server_ifindex(const DnsServer *s);
uint16_t dns_server_port(const DnsServer *s);
bool dns_server_dnssec_supported(DnsServer *server);

View File

@ -544,8 +544,10 @@ static int on_stream_packet(DnsStream *s) {
return 0;
}
static uint16_t dns_port_for_feature_level(DnsServerFeatureLevel level) {
return DNS_SERVER_FEATURE_LEVEL_IS_TLS(level) ? 853 : 53;
static uint16_t dns_transaction_port(DnsTransaction *t) {
if (t->server->port > 0)
return t->server->port;
return DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) ? 853 : 53;
}
static int dns_transaction_emit_tcp(DnsTransaction *t) {
@ -576,7 +578,7 @@ static int dns_transaction_emit_tcp(DnsTransaction *t) {
if (t->server->stream && (DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) == t->server->stream->encrypted))
s = dns_stream_ref(t->server->stream);
else
fd = dns_scope_socket_tcp(t->scope, AF_UNSPEC, NULL, t->server, dns_port_for_feature_level(t->current_feature_level), &sa);
fd = dns_scope_socket_tcp(t->scope, AF_UNSPEC, NULL, t->server, dns_transaction_port(t), &sa);
type = DNS_STREAM_LOOKUP;
break;
@ -1243,7 +1245,7 @@ static int dns_transaction_emit_udp(DnsTransaction *t) {
dns_transaction_close_connection(t);
fd = dns_scope_socket_udp(t->scope, t->server, 53);
fd = dns_scope_socket_udp(t->scope, t->server);
if (fd < 0)
return fd;

View File

@ -285,7 +285,7 @@ int bus_link_method_set_dns_servers(sd_bus_message *message, void *userdata, sd_
if (s)
dns_server_move_back_and_unmark(s);
else {
r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, dns[i].family, &dns[i].address, 0, NULL);
r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, dns[i].family, &dns[i].address, 0, 0, NULL);
if (r < 0)
goto clear;
}

View File

@ -269,7 +269,7 @@ static int link_update_dns_server_one(Link *l, const char *name) {
return 0;
}
return dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a, 0, NULL);
return dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a, 0, 0, NULL);
}
static int link_update_dns_servers(Link *l) {