]>
Commit | Line | Data |
---|---|---|
7263f724 ZJS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Zbigniew Jędrzejewski-Szmek | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include "dns-type.h" | |
4b548ef3 | 23 | #include "string-util.h" |
7263f724 ZJS |
24 | |
25 | typedef const struct { | |
26 | uint16_t type; | |
27 | const char *name; | |
28 | } dns_type; | |
29 | ||
30 | static const struct dns_type_name * | |
31 | lookup_dns_type (register const char *str, register unsigned int len); | |
32 | ||
33 | #include "dns_type-from-name.h" | |
34 | #include "dns_type-to-name.h" | |
35 | ||
de292aa1 | 36 | int dns_type_from_string(const char *s) { |
7263f724 ZJS |
37 | const struct dns_type_name *sc; |
38 | ||
39 | assert(s); | |
7263f724 ZJS |
40 | |
41 | sc = lookup_dns_type(s, strlen(s)); | |
42 | if (!sc) | |
de292aa1 | 43 | return _DNS_TYPE_INVALID; |
7263f724 | 44 | |
de292aa1 | 45 | return sc->id; |
7263f724 | 46 | } |
8e6edc49 | 47 | |
bea4c76f LP |
48 | bool dns_type_is_pseudo(uint16_t type) { |
49 | ||
50 | /* Checks whether the specified type is a "pseudo-type". What | |
51 | * a "pseudo-type" precisely is, is defined only very weakly, | |
52 | * but apparently entails all RR types that are not actually | |
53 | * stored as RRs on the server and should hence also not be | |
54 | * cached. We use this list primarily to validate NSEC type | |
c33be4a6 | 55 | * bitfields, and to verify what to cache. */ |
bea4c76f LP |
56 | |
57 | return IN_SET(type, | |
58 | 0, /* A Pseudo RR type, according to RFC 2931 */ | |
59 | DNS_TYPE_ANY, | |
60 | DNS_TYPE_AXFR, | |
61 | DNS_TYPE_IXFR, | |
62 | DNS_TYPE_OPT, | |
63 | DNS_TYPE_TSIG, | |
64 | DNS_TYPE_TKEY | |
65 | ); | |
8e6edc49 | 66 | } |
c463eb78 | 67 | |
4b548ef3 LP |
68 | bool dns_class_is_pseudo(uint16_t class) { |
69 | return class == DNS_TYPE_ANY; | |
70 | } | |
71 | ||
c463eb78 LP |
72 | bool dns_type_is_valid_query(uint16_t type) { |
73 | ||
74 | /* The types valid as questions in packets */ | |
75 | ||
76 | return !IN_SET(type, | |
77 | 0, | |
78 | DNS_TYPE_OPT, | |
79 | DNS_TYPE_TSIG, | |
80 | DNS_TYPE_TKEY); | |
81 | } | |
82 | ||
83 | bool dns_type_is_valid_rr(uint16_t type) { | |
84 | ||
85 | /* The types valid as RR in packets (but not necessarily | |
86 | * stored on servers). */ | |
87 | ||
88 | return !IN_SET(type, | |
89 | DNS_TYPE_ANY, | |
90 | DNS_TYPE_AXFR, | |
91 | DNS_TYPE_IXFR); | |
92 | } | |
4b548ef3 LP |
93 | |
94 | bool dns_class_is_valid_rr(uint16_t class) { | |
95 | return class != DNS_CLASS_ANY; | |
96 | } | |
97 | ||
d3c7e913 LP |
98 | bool dns_type_may_redirect(uint16_t type) { |
99 | /* The following record types should never be redirected using | |
100 | * CNAME/DNAME RRs. See | |
101 | * <https://tools.ietf.org/html/rfc4035#section-2.5>. */ | |
102 | ||
103 | if (dns_type_is_pseudo(type)) | |
104 | return false; | |
105 | ||
106 | return !IN_SET(type, | |
107 | DNS_TYPE_CNAME, | |
108 | DNS_TYPE_DNAME, | |
109 | DNS_TYPE_NSEC3, | |
110 | DNS_TYPE_NSEC, | |
111 | DNS_TYPE_RRSIG, | |
112 | DNS_TYPE_NXT, | |
113 | DNS_TYPE_SIG, | |
114 | DNS_TYPE_KEY); | |
115 | } | |
116 | ||
4b548ef3 LP |
117 | const char *dns_class_to_string(uint16_t class) { |
118 | ||
119 | switch (class) { | |
120 | ||
121 | case DNS_CLASS_IN: | |
122 | return "IN"; | |
123 | ||
124 | case DNS_CLASS_ANY: | |
125 | return "ANY"; | |
126 | } | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | ||
131 | int dns_class_from_string(const char *s) { | |
132 | ||
133 | if (!s) | |
134 | return _DNS_CLASS_INVALID; | |
135 | ||
136 | if (strcaseeq(s, "IN")) | |
137 | return DNS_CLASS_IN; | |
138 | else if (strcaseeq(s, "ANY")) | |
139 | return DNS_CLASS_ANY; | |
140 | ||
141 | return _DNS_CLASS_INVALID; | |
142 | } |