1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
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.
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.
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/>.
22 #include "resolved-dns-domain.h"
24 int dns_label_unescape(const char **name, char *dest, size_t sz) {
48 if (r >= DNS_LABEL_MAX)
52 /* Escaped character */
60 else if (*n == '\\' || *n == '.') {
61 /* Escaped backslash or dot */
66 } else if (n[0] >= '0' && n[0] <= '9') {
69 /* Escaped literal ASCII character */
71 if (!(n[1] >= '0' && n[1] <= '9') ||
72 !(n[2] >= '0' && n[2] <= '9'))
75 k = ((unsigned) (n[0] - '0') * 100) +
76 ((unsigned) (n[1] - '0') * 10) +
77 ((unsigned) (n[2] - '0'));
79 /* Don't allow CC characters or anything that doesn't fit in 8bit */
80 if (k < ' ' || k > 255 || k == 127)
91 } else if ((uint8_t) *n >= (uint8_t) ' ' && *n != 127) {
93 /* Normal character */
101 /* Empty label that is not at the end? */
112 int dns_label_escape(const char *p, size_t l, char **ret) {
113 _cleanup_free_ char *s = NULL;
120 if (l > DNS_LABEL_MAX)
123 s = malloc(l * 4 + 1);
130 if (*p == '.' || *p == '\\') {
132 /* Dot or backslash */
136 } else if (*p == '_' ||
138 (*p >= '0' && *p <= '9') ||
139 (*p >= 'a' && *p <= 'z') ||
140 (*p >= 'A' && *p <= 'Z')) {
142 /* Proper character */
144 } else if ((uint8_t) *p >= (uint8_t) ' ' && *p != 127) {
146 /* Everything else */
148 *(q++) = '0' + (char) ((uint8_t) *p / 100);
149 *(q++) = '0' + (char) (((uint8_t) *p / 10) % 10);
150 *(q++) = '0' + (char) ((uint8_t) *p % 10);
167 int dns_name_normalize(const char *s, char **_ret) {
168 _cleanup_free_ char *ret = NULL;
169 size_t n = 0, allocated = 0;
177 _cleanup_free_ char *t = NULL;
178 char label[DNS_LABEL_MAX];
180 r = dns_label_unescape(&p, label, sizeof(label));
189 r = dns_label_escape(label, r, &t);
193 if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1))
201 memcpy(ret + n, t, r);
205 if (n > DNS_NAME_MAX)
208 if (!GREEDY_REALLOC(ret, allocated, n + 1))
221 unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]) {
223 unsigned long ul = hash_key[0];
229 char label[DNS_LABEL_MAX+1];
231 r = dns_label_unescape(&p, label, sizeof(label));
236 ascii_strlower(label);
238 ul = ul * hash_key[1] + ul + string_hash_func(label, hash_key);
244 int dns_name_compare_func(const void *a, const void *b) {
245 const char *x = a, *y = b;
252 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
254 if (*x == 0 && *y == 0)
257 r = dns_label_unescape(&x, la, sizeof(la));
258 q = dns_label_unescape(&y, lb, sizeof(lb));
263 r = strcasecmp(la, lb);
269 int dns_name_equal(const char *x, const char *y) {
276 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
278 if (*x == 0 && *y == 0)
281 r = dns_label_unescape(&x, la, sizeof(la));
285 q = dns_label_unescape(&y, lb, sizeof(lb));
290 if (strcasecmp(la, lb))
295 int dns_name_endswith(const char *name, const char *suffix) {
296 const char *n, *s, *saved_n = NULL;
306 char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1];
308 r = dns_label_unescape(&n, ln, sizeof(ln));
315 q = dns_label_unescape(&s, ls, sizeof(ls));
319 if (r == 0 && q == 0)
321 if (r == 0 && saved_n == n)
326 if (r != q || strcasecmp(ln, ls)) {
328 /* Not the same, let's jump back, and try with the next label again */
336 int dns_name_reverse(int family, const union in_addr_union *a, char **ret) {
343 p = (const uint8_t*) a;
345 if (family == AF_INET)
346 r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]);
347 else if (family == AF_INET6)
348 r = asprintf(ret, "%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.ip6.arpa",
349 hexchar(p[15] & 0xF), hexchar(p[15] >> 4), hexchar(p[14] & 0xF), hexchar(p[14] >> 4),
350 hexchar(p[13] & 0xF), hexchar(p[13] >> 4), hexchar(p[12] & 0xF), hexchar(p[12] >> 4),
351 hexchar(p[11] & 0xF), hexchar(p[11] >> 4), hexchar(p[10] & 0xF), hexchar(p[10] >> 4),
352 hexchar(p[ 9] & 0xF), hexchar(p[ 9] >> 4), hexchar(p[ 8] & 0xF), hexchar(p[ 8] >> 4),
353 hexchar(p[ 7] & 0xF), hexchar(p[ 7] >> 4), hexchar(p[ 6] & 0xF), hexchar(p[ 6] >> 4),
354 hexchar(p[ 5] & 0xF), hexchar(p[ 5] >> 4), hexchar(p[ 4] & 0xF), hexchar(p[ 4] >> 4),
355 hexchar(p[ 3] & 0xF), hexchar(p[ 3] >> 4), hexchar(p[ 2] & 0xF), hexchar(p[ 2] >> 4),
356 hexchar(p[ 1] & 0xF), hexchar(p[ 1] >> 4), hexchar(p[ 0] & 0xF), hexchar(p[ 0] >> 4));
358 return -EAFNOSUPPORT;
365 int dns_name_address(const char *p, int *family, union in_addr_union *address) {
372 r = dns_name_endswith(p, "in-addr.arpa");
379 for (i = 0; i < ELEMENTSOF(a); i++) {
380 char label[DNS_LABEL_MAX+1];
382 r = dns_label_unescape(&p, label, sizeof(label));
390 r = safe_atou8(label, &a[i]);
395 r = dns_name_equal(p, "in-addr.arpa");
400 address->in.s_addr = htobe32(((uint32_t) a[3] << 24) |
401 ((uint32_t) a[2] << 16) |
402 ((uint32_t) a[1] << 8) |
408 r = dns_name_endswith(p, "ip6.arpa");
415 for (i = 0; i < ELEMENTSOF(a.s6_addr); i++) {
416 char label[DNS_LABEL_MAX+1];
419 r = dns_label_unescape(&p, label, sizeof(label));
424 x = unhexchar(label[0]);
428 r = dns_label_unescape(&p, label, sizeof(label));
433 y = unhexchar(label[0]);
437 a.s6_addr[ELEMENTSOF(a.s6_addr) - i - 1] = (uint8_t) y << 4 | (uint8_t) x;
440 r = dns_name_equal(p, "ip6.arpa");
452 int dns_name_root(const char *name) {
453 char label[DNS_LABEL_MAX+1];
458 r = dns_label_unescape(&name, label, sizeof(label));
462 return r == 0 && *name == 0;
465 int dns_name_single_label(const char *name) {
466 char label[DNS_LABEL_MAX+1];
471 r = dns_label_unescape(&name, label, sizeof(label));
477 r = dns_label_unescape(&name, label, sizeof(label));
481 return r == 0 && *name == 0;