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 (*n >= ' ' && *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 (*p >= ' ' && *p != 127) {
146 /* Everything else */
148 *(q++) = '0' + (char) ((unsigned) *p / 100);
149 *(q++) = '0' + (char) (((unsigned) *p / 10) % 10);
150 *(q++) = '0' + (char) ((unsigned) *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;
178 _cleanup_free_ char *t = NULL;
179 char label[DNS_LABEL_MAX];
181 r = dns_label_unescape(&p, label, sizeof(label));
190 r = dns_label_escape(label, r, &t);
194 if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1))
202 memcpy(ret + n, t, r);
206 if (!GREEDY_REALLOC(ret, allocated, n + 1))
216 unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]) {
218 unsigned long ul = 0;
224 char label[DNS_LABEL_MAX+1];
226 r = dns_label_unescape(&p, label, sizeof(label));
231 ascii_strlower(label);
233 ul = hash_key[0] * ul + ul + string_hash_func(label, hash_key);
239 int dns_name_compare_func(const void *a, const void *b) {
240 const char *x = a, *y = b;
247 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
249 if (*x == 0 && *y == 0)
252 r = dns_label_unescape(&x, la, sizeof(la));
253 q = dns_label_unescape(&y, lb, sizeof(lb));
258 r = strcasecmp(la, lb);
264 int dns_name_equal(const char *x, const char *y) {
271 char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
273 if (*x == 0 && *y == 0)
276 r = dns_label_unescape(&x, la, sizeof(la));
280 q = dns_label_unescape(&y, lb, sizeof(lb));
285 if (strcasecmp(la, lb))
290 int dns_name_endswith(const char *name, const char *suffix) {
291 const char *n, *s, *saved_n = NULL;
301 char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1];
303 r = dns_label_unescape(&n, ln, sizeof(ln));
310 q = dns_label_unescape(&s, ls, sizeof(ls));
314 if (r == 0 && q == 0)
316 if (r == 0 && saved_n == n)
321 if (r != q || strcasecmp(ln, ls)) {
323 /* Not the same, let's jump back, and try with the next label again */
331 int dns_name_reverse(int family, const union in_addr_union *a, char **ret) {
338 p = (const uint8_t*) a;
340 if (family == AF_INET)
341 r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]);
342 else if (family == AF_INET6)
343 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",
344 hexchar(p[15] & 0xF), hexchar(p[15] >> 4), hexchar(p[14] & 0xF), hexchar(p[14] >> 4),
345 hexchar(p[13] & 0xF), hexchar(p[13] >> 4), hexchar(p[12] & 0xF), hexchar(p[12] >> 4),
346 hexchar(p[11] & 0xF), hexchar(p[11] >> 4), hexchar(p[10] & 0xF), hexchar(p[10] >> 4),
347 hexchar(p[ 9] & 0xF), hexchar(p[ 9] >> 4), hexchar(p[ 8] & 0xF), hexchar(p[ 8] >> 4),
348 hexchar(p[ 7] & 0xF), hexchar(p[ 7] >> 4), hexchar(p[ 6] & 0xF), hexchar(p[ 6] >> 4),
349 hexchar(p[ 5] & 0xF), hexchar(p[ 5] >> 4), hexchar(p[ 4] & 0xF), hexchar(p[ 4] >> 4),
350 hexchar(p[ 3] & 0xF), hexchar(p[ 3] >> 4), hexchar(p[ 2] & 0xF), hexchar(p[ 2] >> 4),
351 hexchar(p[ 1] & 0xF), hexchar(p[ 1] >> 4), hexchar(p[ 0] & 0xF), hexchar(p[ 0] >> 4));
353 return -EAFNOSUPPORT;
360 int dns_name_root(const char *name) {
361 char label[DNS_LABEL_MAX+1];
366 r = dns_label_unescape(&name, label, sizeof(label));
370 return r == 0 && *name == 0;
373 int dns_name_single_label(const char *name) {
374 char label[DNS_LABEL_MAX+1];
379 r = dns_label_unescape(&name, label, sizeof(label));
386 r = dns_label_unescape(&name, label, sizeof(label));
390 return r == 0 && *name == 0;