resolved: use switch-case statements for protocol details
With more protocols to come, switch repetitive if-else blocks with a switch-case statements.
This commit is contained in:
@@ -64,7 +64,7 @@ int dns_packet_new(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
|
|||||||
int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
|
int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
|
||||||
DnsPacket *p;
|
DnsPacket *p;
|
||||||
DnsPacketHeader *h;
|
DnsPacketHeader *h;
|
||||||
int r;
|
int r, rd;
|
||||||
|
|
||||||
assert(ret);
|
assert(ret);
|
||||||
|
|
||||||
@@ -74,26 +74,27 @@ int dns_packet_new_query(DnsPacket **ret, DnsProtocol protocol, size_t mtu) {
|
|||||||
|
|
||||||
h = DNS_PACKET_HEADER(p);
|
h = DNS_PACKET_HEADER(p);
|
||||||
|
|
||||||
if (protocol == DNS_PROTOCOL_LLMNR)
|
switch (protocol) {
|
||||||
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
|
case DNS_PROTOCOL_LLMNR:
|
||||||
0 /* opcode */,
|
/* no recursion for link-local resolving protocols */
|
||||||
0 /* c */,
|
rd = 0;
|
||||||
0 /* tc */,
|
break;
|
||||||
0 /* t */,
|
|
||||||
0 /* ra */,
|
default:
|
||||||
0 /* ad */,
|
/* ask for recursion */
|
||||||
0 /* cd */,
|
rd = 1;
|
||||||
0 /* rcode */));
|
break;
|
||||||
else
|
}
|
||||||
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
|
|
||||||
0 /* opcode */,
|
h->flags = htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
|
||||||
0 /* aa */,
|
0 /* opcode */,
|
||||||
0 /* tc */,
|
0 /* aa */,
|
||||||
1 /* rd (ask for recursion) */,
|
0 /* tc */,
|
||||||
0 /* ra */,
|
rd /* rd */,
|
||||||
0 /* ad */,
|
0 /* ra */,
|
||||||
0 /* cd */,
|
0 /* ad */,
|
||||||
0 /* rcode */));
|
0 /* cd */,
|
||||||
|
0 /* rcode */));
|
||||||
|
|
||||||
*ret = p;
|
*ret = p;
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -239,11 +239,16 @@ static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family)
|
|||||||
|
|
||||||
/* Converts a protocol + family into a flags field as used in queries */
|
/* Converts a protocol + family into a flags field as used in queries */
|
||||||
|
|
||||||
if (protocol == DNS_PROTOCOL_DNS)
|
switch (protocol) {
|
||||||
|
case DNS_PROTOCOL_DNS:
|
||||||
return SD_RESOLVED_DNS;
|
return SD_RESOLVED_DNS;
|
||||||
|
|
||||||
if (protocol == DNS_PROTOCOL_LLMNR)
|
case DNS_PROTOCOL_LLMNR:
|
||||||
return family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4;
|
return family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,7 +166,8 @@ int dns_scope_emit(DnsScope *s, int fd, DnsPacket *p) {
|
|||||||
} else
|
} else
|
||||||
mtu = manager_find_mtu(s->manager);
|
mtu = manager_find_mtu(s->manager);
|
||||||
|
|
||||||
if (s->protocol == DNS_PROTOCOL_DNS) {
|
switch (s->protocol) {
|
||||||
|
case DNS_PROTOCOL_DNS:
|
||||||
if (DNS_PACKET_QDCOUNT(p) > 1)
|
if (DNS_PACKET_QDCOUNT(p) > 1)
|
||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
|
|
||||||
@@ -180,8 +181,9 @@ int dns_scope_emit(DnsScope *s, int fd, DnsPacket *p) {
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
} else if (s->protocol == DNS_PROTOCOL_LLMNR) {
|
break;
|
||||||
|
|
||||||
|
case DNS_PROTOCOL_LLMNR:
|
||||||
if (DNS_PACKET_QDCOUNT(p) > 1)
|
if (DNS_PACKET_QDCOUNT(p) > 1)
|
||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
|
|
||||||
@@ -205,8 +207,12 @@ int dns_scope_emit(DnsScope *s, int fd, DnsPacket *p) {
|
|||||||
r = manager_send(s->manager, fd, ifindex, family, &addr, port, p);
|
r = manager_send(s->manager, fd, ifindex, family, &addr, port, p);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
} else
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
return -EAFNOSUPPORT;
|
return -EAFNOSUPPORT;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -341,27 +347,25 @@ DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, co
|
|||||||
if (dns_name_endswith(domain, *i) > 0)
|
if (dns_name_endswith(domain, *i) > 0)
|
||||||
return DNS_SCOPE_YES;
|
return DNS_SCOPE_YES;
|
||||||
|
|
||||||
if (s->protocol == DNS_PROTOCOL_DNS) {
|
switch (s->protocol) {
|
||||||
|
case DNS_PROTOCOL_DNS:
|
||||||
if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
|
if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
|
||||||
dns_name_endswith(domain, "0.8.e.f.ip6.arpa") == 0 &&
|
dns_name_endswith(domain, "0.8.e.f.ip6.arpa") == 0 &&
|
||||||
dns_name_single_label(domain) == 0)
|
dns_name_single_label(domain) == 0)
|
||||||
return DNS_SCOPE_MAYBE;
|
return DNS_SCOPE_MAYBE;
|
||||||
|
|
||||||
return DNS_SCOPE_NO;
|
return DNS_SCOPE_NO;
|
||||||
}
|
|
||||||
|
|
||||||
if (s->protocol == DNS_PROTOCOL_MDNS) {
|
case DNS_PROTOCOL_MDNS:
|
||||||
if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
|
if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
|
||||||
(s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
|
(s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
|
||||||
(dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */
|
(dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */
|
||||||
dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */
|
dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */
|
||||||
manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */
|
manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */
|
||||||
return DNS_SCOPE_MAYBE;
|
|
||||||
|
|
||||||
return DNS_SCOPE_NO;
|
return DNS_SCOPE_NO;
|
||||||
}
|
|
||||||
|
|
||||||
if (s->protocol == DNS_PROTOCOL_LLMNR) {
|
case DNS_PROTOCOL_LLMNR:
|
||||||
if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
|
if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
|
||||||
(s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
|
(s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
|
||||||
(dns_name_single_label(domain) > 0 && /* only resolve single label names via LLMNR */
|
(dns_name_single_label(domain) > 0 && /* only resolve single label names via LLMNR */
|
||||||
@@ -370,9 +374,10 @@ DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, co
|
|||||||
return DNS_SCOPE_MAYBE;
|
return DNS_SCOPE_MAYBE;
|
||||||
|
|
||||||
return DNS_SCOPE_NO;
|
return DNS_SCOPE_NO;
|
||||||
}
|
|
||||||
|
|
||||||
assert_not_reached("Unknown scope protocol");
|
default:
|
||||||
|
assert_not_reached("Unknown scope protocol");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int dns_scope_good_key(DnsScope *s, DnsResourceKey *key) {
|
int dns_scope_good_key(DnsScope *s, DnsResourceKey *key) {
|
||||||
|
|||||||
@@ -260,10 +260,12 @@ static int dns_transaction_open_tcp(DnsTransaction *t) {
|
|||||||
if (t->stream)
|
if (t->stream)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (t->scope->protocol == DNS_PROTOCOL_DNS)
|
switch (t->scope->protocol) {
|
||||||
|
case DNS_PROTOCOL_DNS:
|
||||||
fd = dns_scope_tcp_socket(t->scope, AF_UNSPEC, NULL, 53, &server);
|
fd = dns_scope_tcp_socket(t->scope, AF_UNSPEC, NULL, 53, &server);
|
||||||
else if (t->scope->protocol == DNS_PROTOCOL_LLMNR) {
|
break;
|
||||||
|
|
||||||
|
case DNS_PROTOCOL_LLMNR:
|
||||||
/* When we already received a reply to this (but it was truncated), send to its sender address */
|
/* When we already received a reply to this (but it was truncated), send to its sender address */
|
||||||
if (t->received)
|
if (t->received)
|
||||||
fd = dns_scope_tcp_socket(t->scope, t->received->family, &t->received->sender, t->received->sender_port, NULL);
|
fd = dns_scope_tcp_socket(t->scope, t->received->family, &t->received->sender, t->received->sender_port, NULL);
|
||||||
@@ -285,8 +287,12 @@ static int dns_transaction_open_tcp(DnsTransaction *t) {
|
|||||||
|
|
||||||
fd = dns_scope_tcp_socket(t->scope, family, &address, LLMNR_PORT, NULL);
|
fd = dns_scope_tcp_socket(t->scope, family, &address, LLMNR_PORT, NULL);
|
||||||
}
|
}
|
||||||
} else
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
return -EAFNOSUPPORT;
|
return -EAFNOSUPPORT;
|
||||||
|
}
|
||||||
|
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return fd;
|
return fd;
|
||||||
@@ -342,7 +348,8 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) {
|
|||||||
* should hence not attempt to access the query or transaction
|
* should hence not attempt to access the query or transaction
|
||||||
* after calling this function. */
|
* after calling this function. */
|
||||||
|
|
||||||
if (t->scope->protocol == DNS_PROTOCOL_LLMNR) {
|
switch (t->scope->protocol) {
|
||||||
|
case DNS_PROTOCOL_LLMNR:
|
||||||
assert(t->scope->link);
|
assert(t->scope->link);
|
||||||
|
|
||||||
/* For LLMNR we will not accept any packets from other
|
/* For LLMNR we will not accept any packets from other
|
||||||
@@ -361,6 +368,14 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) {
|
|||||||
dns_transaction_tentative(t, p);
|
dns_transaction_tentative(t, p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DNS_PROTOCOL_DNS:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t->received != p) {
|
if (t->received != p) {
|
||||||
|
|||||||
Reference in New Issue
Block a user