Skip to content

Commit 4b548ef

Browse files
committed
resolved: move DNS class utilities to dns-type.c and add more helpers
Let's make DNS class helpers more like DNS type helpers, let's move them from resolved-dns-rr.[ch] into dns-type.[ch]. This also adds two new calls dns_class_is_pseudo() and dns_class_is_valid_rr() which operate similar to dns_type_is_pseudo() and dns_type_is_valid_rr() but for classes instead of types. This should hopefully make handling of DNS classes and DNS types more alike.
1 parent 3e92a71 commit 4b548ef

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

src/resolve-host/resolve-host.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
static int arg_family = AF_UNSPEC;
4040
static int arg_ifindex = 0;
41-
static int arg_type = 0;
41+
static uint16_t arg_type = 0;
4242
static uint16_t arg_class = 0;
4343
static bool arg_legend = true;
4444
static uint64_t arg_flags = 0;
@@ -347,7 +347,6 @@ static int resolve_record(sd_bus *bus, const char *name) {
347347
if (r < 0)
348348
return bus_log_create_error(r);
349349

350-
assert((uint16_t) arg_type == arg_type);
351350
r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, arg_class, arg_type, arg_flags);
352351
if (r < 0)
353352
return bus_log_create_error(r);
@@ -758,12 +757,13 @@ static int parse_argv(int argc, char *argv[]) {
758757
return 0;
759758
}
760759

761-
arg_type = dns_type_from_string(optarg);
762-
if (arg_type < 0) {
760+
r = dns_type_from_string(optarg);
761+
if (r < 0) {
763762
log_error("Failed to parse RR record type %s", optarg);
764-
return arg_type;
763+
return r;
765764
}
766-
assert(arg_type > 0 && (uint16_t) arg_type == arg_type);
765+
arg_type = (uint16_t) r;
766+
assert((int) arg_type == r);
767767

768768
break;
769769

@@ -773,11 +773,13 @@ static int parse_argv(int argc, char *argv[]) {
773773
return 0;
774774
}
775775

776-
r = dns_class_from_string(optarg, &arg_class);
776+
r = dns_class_from_string(optarg);
777777
if (r < 0) {
778778
log_error("Failed to parse RR record class %s", optarg);
779779
return r;
780780
}
781+
arg_class = (uint16_t) r;
782+
assert((int) arg_class == r);
781783

782784
break;
783785

src/resolve/dns-type.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
***/
2121

2222
#include "dns-type.h"
23+
#include "string-util.h"
2324

2425
typedef const struct {
2526
uint16_t type;
@@ -64,6 +65,10 @@ bool dns_type_is_pseudo(uint16_t type) {
6465
);
6566
}
6667

68+
bool dns_class_is_pseudo(uint16_t class) {
69+
return class == DNS_TYPE_ANY;
70+
}
71+
6772
bool dns_type_is_valid_query(uint16_t type) {
6873

6974
/* The types valid as questions in packets */
@@ -85,3 +90,34 @@ bool dns_type_is_valid_rr(uint16_t type) {
8590
DNS_TYPE_AXFR,
8691
DNS_TYPE_IXFR);
8792
}
93+
94+
bool dns_class_is_valid_rr(uint16_t class) {
95+
return class != DNS_CLASS_ANY;
96+
}
97+
98+
const char *dns_class_to_string(uint16_t class) {
99+
100+
switch (class) {
101+
102+
case DNS_CLASS_IN:
103+
return "IN";
104+
105+
case DNS_CLASS_ANY:
106+
return "ANY";
107+
}
108+
109+
return NULL;
110+
}
111+
112+
int dns_class_from_string(const char *s) {
113+
114+
if (!s)
115+
return _DNS_CLASS_INVALID;
116+
117+
if (strcaseeq(s, "IN"))
118+
return DNS_CLASS_IN;
119+
else if (strcaseeq(s, "ANY"))
120+
return DNS_CLASS_ANY;
121+
122+
return _DNS_CLASS_INVALID;
123+
}

src/resolve/dns-type.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,17 @@ enum {
122122
assert_cc(DNS_TYPE_SSHFP == 44);
123123
assert_cc(DNS_TYPE_TLSA == 52);
124124
assert_cc(DNS_TYPE_ANY == 255);
125+
126+
/* DNS record classes, see RFC 1035 */
127+
enum {
128+
DNS_CLASS_IN = 0x01,
129+
DNS_CLASS_ANY = 0xFF,
130+
_DNS_CLASS_MAX,
131+
_DNS_CLASS_INVALID = -1
132+
};
133+
134+
bool dns_class_is_pseudo(uint16_t class);
135+
bool dns_class_is_valid_rr(uint16_t class);
136+
137+
const char *dns_class_to_string(uint16_t type);
138+
int dns_class_from_string(const char *name);

src/resolve/resolved-dns-rr.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,34 +1074,6 @@ int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
10741074
return 0;
10751075
}
10761076

1077-
const char *dns_class_to_string(uint16_t class) {
1078-
1079-
switch (class) {
1080-
1081-
case DNS_CLASS_IN:
1082-
return "IN";
1083-
1084-
case DNS_CLASS_ANY:
1085-
return "ANY";
1086-
}
1087-
1088-
return NULL;
1089-
}
1090-
1091-
int dns_class_from_string(const char *s, uint16_t *class) {
1092-
assert(s);
1093-
assert(class);
1094-
1095-
if (strcaseeq(s, "IN"))
1096-
*class = DNS_CLASS_IN;
1097-
else if (strcaseeq(s, "ANY"))
1098-
*class = DNS_CLASS_ANY;
1099-
else
1100-
return -EINVAL;
1101-
1102-
return 0;
1103-
}
1104-
11051077
DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i) {
11061078
DnsTxtItem *n;
11071079

src/resolve/resolved-dns-rr.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ typedef struct DnsResourceKey DnsResourceKey;
3333
typedef struct DnsResourceRecord DnsResourceRecord;
3434
typedef struct DnsTxtItem DnsTxtItem;
3535

36-
/* DNS record classes, see RFC 1035 */
37-
enum {
38-
DNS_CLASS_IN = 0x01,
39-
DNS_CLASS_ANY = 0xFF,
40-
_DNS_CLASS_MAX,
41-
_DNS_CLASS_INVALID = -1
42-
};
43-
4436
/* DNSKEY RR flags */
4537
#define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8)
4638
#define DNSKEY_FLAG_SEP (UINT16_C(1) << 0)
@@ -270,9 +262,6 @@ int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical);
270262
DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i);
271263
bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b);
272264

273-
const char *dns_class_to_string(uint16_t type);
274-
int dns_class_from_string(const char *name, uint16_t *class);
275-
276265
extern const struct hash_ops dns_resource_key_hash_ops;
277266

278267
const char* dnssec_algorithm_to_string(int i) _const_;

0 commit comments

Comments
 (0)