resolved-dns-domain.c revision 7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cd
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/***
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright 2014 Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen ***/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "resolved-dns-domain.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_label_unescape(const char **name, char *dest, size_t sz) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen const char *n;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering char *d;
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering int r = 0;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering assert(name);
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek assert(*name);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering assert(dest);
c004493cdefc1f43a3956ca529e8070f8d70be56Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering n = *name;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering d = dest;
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering for (;;) {
3df3e884ae1237ef0d4d23b0e80f4ffda95ac135Ronny Chevalier if (*n == '.') {
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering n++;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering break;
5f402ae84bbc08fe8de5682e371b3f66c387da52Daniel Mack }
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (*n == 0)
bc7702b098b63031767174206f5df14a8ccb45cfDaniel Mack break;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering if (sz <= 0)
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return -ENOSPC;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r >= DNS_LABEL_MAX)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (*n == '\\') {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Escaped character */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*n == 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Ending NUL */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else if (*n == '\\' || *n == '.') {
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen /* Escaped backslash or dot */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(d++) = *(n++);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sz--;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (n[0] >= '0' && n[0] <= '9') {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Escaped literal ASCII character */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!(n[1] >= '0' && n[1] <= '9') ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering !(n[2] >= '0' && n[2] <= '9'))
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering return -EINVAL;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = ((unsigned) (n[0] - '0') * 100) +
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering ((unsigned) (n[1] - '0') * 10) +
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ((unsigned) (n[2] - '0'));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Don't allow CC characters or anything that doesn't fit in 8bit */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (k < ' ' || k > 255 || k == 127)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(d++) = (char) k;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sz--;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r++;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering n += 3;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering } else
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poettering return -EINVAL;
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering } else if ((uint8_t) *n >= (uint8_t) ' ' && *n != 127) {
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Normal character */
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering *(d++) = *(n++);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sz--;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Empty label that is not at the end? */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r == 0 && *n)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (sz >= 1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *d = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt *name = n;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersenint dns_label_escape(const char *p, size_t l, char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *s = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *q;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(ret);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (l > DNS_LABEL_MAX)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = malloc(l * 4 + 1);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (!s)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering q = s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (l > 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*p == '.' || *p == '\\') {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Dot or backslash */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = '\\';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = *p;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (*p == '_' ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *p == '-' ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (*p >= '0' && *p <= '9') ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (*p >= 'a' && *p <= 'z') ||
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (*p >= 'A' && *p <= 'Z')) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Proper character */
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen *(q++) = *p;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if ((uint8_t) *p >= (uint8_t) ' ' && *p != 127) {
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Everything else */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = '\\';
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = '0' + (char) ((uint8_t) *p / 100);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = '0' + (char) (((uint8_t) *p / 10) % 10);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(q++) = '0' + (char) ((uint8_t) *p % 10);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen p++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering l--;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *q = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = q - s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_name_normalize(const char *s, char **_ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *ret = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size_t n = 0, allocated = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *p = s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool first = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(s);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *t = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char label[DNS_LABEL_MAX];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_unescape(&p, label, sizeof(label));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*p != 0)
3e044c492e3ebe64f4e3175c94f9db8a62557b82Markus Elfring return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_escape(label, r, &t);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!GREEDY_REALLOC(ret, allocated, n + !first + strlen(t) + 1))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!first)
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering ret[n++] = '.';
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering first = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcpy(ret + n, t, r);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n += r;
cc98b3025eeb89addb76a27390cb2baca4eab8b9Torstein Husebø }
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (n > DNS_NAME_MAX)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (!GREEDY_REALLOC(ret, allocated, n + 1))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ret[n] = 0;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *_ret = ret;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ret = NULL;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringunsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *p = s;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen unsigned long ul = hash_key[0];
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (*p) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char label[DNS_LABEL_MAX+1];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_unescape(&p, label, sizeof(label));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering label[r] = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ascii_strlower(label);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ul = ul * hash_key[1] + ul + string_hash_func(label, hash_key);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return ul;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_name_compare_func(const void *a, const void *b) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *x = a, *y = b;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, q;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen assert(a);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(b);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*x == 0 && *y == 0)
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_unescape(&x, la, sizeof(la));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering q = dns_label_unescape(&y, lb, sizeof(lb));
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (r < 0 || q < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r - q;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering la[r] = lb[q] = 0;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen r = strcasecmp(la, lb);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r != 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_name_equal(const char *x, const char *y) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, q;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(x);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(y);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char la[DNS_LABEL_MAX+1], lb[DNS_LABEL_MAX+1];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*x == 0 && *y == 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_unescape(&x, la, sizeof(la));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt q = dns_label_unescape(&y, lb, sizeof(lb));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (q < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return q;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering la[r] = lb[q] = 0;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (strcasecmp(la, lb))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_name_endswith(const char *name, const char *suffix) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *n, *s, *saved_n = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, q;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(name);
0014a4ad505d119c7ac4346d9d774c3f17f663a5Lennart Poettering assert(suffix);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = name;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = suffix;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char ln[DNS_LABEL_MAX+1], ls[DNS_LABEL_MAX+1];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_label_unescape(&n, ln, sizeof(ln));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!saved_n)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering saved_n = n;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering q = dns_label_unescape(&s, ls, sizeof(ls));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r == 0 && q == 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return true;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r == 0 && saved_n == n)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return false;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering ln[r] = ls[q] = 0;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r != q || strcasecmp(ln, ls)) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* Not the same, let's jump back, and try with the next label again */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering s = suffix;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering n = saved_n;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering saved_n = NULL;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering }
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering }
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringint dns_name_reverse(int family, const union in_addr_union *a, char **ret) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering const uint8_t *p;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(a);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(ret);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering p = (const uint8_t*) a;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (family == AF_INET)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = asprintf(ret, "%u.%u.%u.%u.in-addr.arpa", p[3], p[2], p[1], p[0]);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering else if (family == AF_INET6)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering 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",
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering hexchar(p[15] & 0xF), hexchar(p[15] >> 4), hexchar(p[14] & 0xF), hexchar(p[14] >> 4),
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering hexchar(p[13] & 0xF), hexchar(p[13] >> 4), hexchar(p[12] & 0xF), hexchar(p[12] >> 4),
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering hexchar(p[11] & 0xF), hexchar(p[11] >> 4), hexchar(p[10] & 0xF), hexchar(p[10] >> 4),
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering hexchar(p[ 9] & 0xF), hexchar(p[ 9] >> 4), hexchar(p[ 8] & 0xF), hexchar(p[ 8] >> 4),
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering hexchar(p[ 7] & 0xF), hexchar(p[ 7] >> 4), hexchar(p[ 6] & 0xF), hexchar(p[ 6] >> 4),
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering hexchar(p[ 5] & 0xF), hexchar(p[ 5] >> 4), hexchar(p[ 4] & 0xF), hexchar(p[ 4] >> 4),
422baca0f230913158078fddf884e06c8c64a316Lennart Poettering hexchar(p[ 3] & 0xF), hexchar(p[ 3] >> 4), hexchar(p[ 2] & 0xF), hexchar(p[ 2] >> 4),
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering hexchar(p[ 1] & 0xF), hexchar(p[ 1] >> 4), hexchar(p[ 0] & 0xF), hexchar(p[ 0] >> 4));
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering else
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -EAFNOSUPPORT;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -ENOMEM;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return 0;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringint dns_name_address(const char *p, int *family, union in_addr_union *address) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(p);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering assert(family);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering assert(address);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = dns_name_endswith(p, "in-addr.arpa");
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r > 0) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering uint8_t a[4];
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering unsigned i;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering for (i = 0; i < ELEMENTSOF(a); i++) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering char label[DNS_LABEL_MAX+1];
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = dns_label_unescape(&p, label, sizeof(label));
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r == 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return -EINVAL;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r > 3)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -EINVAL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = safe_atou8(label, &a[i]);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = dns_name_equal(p, "in-addr.arpa");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r <= 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering *family = AF_INET;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering address->in.s_addr = htobe32(((uint32_t) a[3] << 24) |
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering ((uint32_t) a[2] << 16) |
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering ((uint32_t) a[1] << 8) |
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering (uint32_t) a[0]);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return 1;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering }
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = dns_name_endswith(p, "ip6.arpa");
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r < 0)
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r > 0) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering struct in6_addr a;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering unsigned i;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering for (i = 0; i < ELEMENTSOF(a.s6_addr); i++) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering char label[DNS_LABEL_MAX+1];
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering int x, y;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
8d3d7072e609ef0e0fb37e1d19a29307d58146c3Michal Schmidt r = dns_label_unescape(&p, label, sizeof(label));
8d3d7072e609ef0e0fb37e1d19a29307d58146c3Michal Schmidt if (r <= 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return r;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (r != 1)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -EINVAL;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering x = unhexchar(label[0]);
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (x < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -EINVAL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = dns_label_unescape(&p, label, sizeof(label));
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r <= 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r != 1)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return -EINVAL;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering y = unhexchar(label[0]);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (y < 0)
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering return -EINVAL;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering a.s6_addr[ELEMENTSOF(a.s6_addr) - i - 1] = (uint8_t) y << 4 | (uint8_t) x;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering }
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering r = dns_name_equal(p, "ip6.arpa");
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering if (r <= 0)
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering return r;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering *family = AF_INET6;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering address->in6 = a;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering return 1;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering }
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering return 0;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering}
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poetteringint dns_name_root(const char *name) {
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering char label[DNS_LABEL_MAX+1];
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering int r;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering assert(name);
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering r = dns_label_unescape(&name, label, sizeof(label));
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering if (r < 0)
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering return r;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering return r == 0 && *name == 0;
4d506d6bb757af3b99e0876234c465e6898c5ea4Lennart Poettering}
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_name_single_label(const char *name) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen char label[DNS_LABEL_MAX+1];
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen int r;
c92e531c82a9815ec349aa1bf31236b86b2d5311Lennart Poettering
c92e531c82a9815ec349aa1bf31236b86b2d5311Lennart Poettering assert(name);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = dns_label_unescape(&name, label, sizeof(label));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0)
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return r;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r == 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
bc7702b098b63031767174206f5df14a8ccb45cfDaniel Mack
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering r = dns_label_unescape(&name, label, sizeof(label));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r < 0)
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering return r;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering
00fa60ae3b2823036cb3e7734f16bce30cb7441dLennart Poettering return r == 0 && *name == 0;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}
0d2cd47617b423f37d7425be7a56ae2fca8ff9f6Lennart Poettering