resolved-dns-domain.c revision 74b2466e14a1961bf3ac0e8a60cfaceec705bd59
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "resolved-dns-domain.h"
const char *n;
char *d;
int r = 0;
n = *name;
d = dest;
for (;;) {
if (*n == '.') {
n++;
break;
}
if (*n == 0)
break;
if (sz <= 0)
return -ENOSPC;
if (*n == '\\') {
/* Escaped character */
n++;
if (*n == 0)
/* Ending NUL */
return -EINVAL;
else if (*n == '\\' || *n == '.') {
/* Escaped backslash or dot */
*(d++) = *(n++);
sz--;
r++;
} else if (n[0] >= '0' && n[0] <= '9') {
unsigned k;
/* Escaped literal ASCII character */
if (!(n[1] >= '0' && n[1] <= '9') ||
!(n[2] >= '0' && n[2] <= '9'))
return -EINVAL;
k = ((unsigned) (n[0] - '0') * 100) +
((unsigned) (n[1] - '0') * 10) +
((unsigned) (n[2] - '0'));
/* Don't allow CC characters or anything that doesn't fit in 8bit */
if (k < ' ' || k > 255 || k == 127)
return -EINVAL;
*(d++) = (char) k;
sz--;
r++;
n += 3;
} else
return -EINVAL;
} else if (*n >= ' ' && *n != 127) {
/* Normal character */
*(d++) = *(n++);
sz--;
r++;
} else
return -EINVAL;
}
/* Empty label that is not at the end? */
if (r == 0 && *n)
return -EINVAL;
if (sz >= 1)
*d = 0;
*name = n;
return r;
}
_cleanup_free_ char *s = NULL;
char *q;
int r;
assert(p);
if (!s)
return -ENOMEM;
q = s;
while (l > 0) {
if (*p == '.' || *p == '\\') {
/* Dot or backslash */
*(q++) = '\\';
*(q++) = *p;
} else if (*p == '_' ||
*p == '-' ||
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')) {
/* Proper character */
*(q++) = *p;
} else if (*p >= ' ' && *p != 127) {
/* Everything else */
*(q++) = '\\';
*(q++) = '0' + (char) ((unsigned) *p / 100);
*(q++) = '0' + (char) (((unsigned) *p / 10) % 10);
*(q++) = '0' + (char) ((unsigned) *p % 10);
} else
return -EINVAL;
p++;
l--;
}
*q = 0;
*ret = s;
r = q - s;
s = NULL;
return r;
}
int dns_name_normalize(const char *s, char **_ret) {
const char *p = s;
bool first = true;
int r;
assert(s);
for (;;) {
_cleanup_free_ char *t = NULL;
char label[DNS_LABEL_MAX];
if (r < 0)
return r;
if (r == 0) {
if (*p != 0)
return -EINVAL;
break;
}
r = dns_label_escape(label, r, &t);
if (r < 0)
return r;
return -ENOMEM;
if (!first)
ret[n++] = '.';
else
first = false;
n += r;
}
return -ENOMEM;
ret[n] = 0;
return 0;
}
const char *p = s;
unsigned long ul = 0;
int r;
assert(p);
while (*p) {
if (r < 0)
break;
label[r] = 0;
}
return ul;
}
int dns_name_compare_func(const void *a, const void *b) {
const char *x = a, *y = b;
int r, q;
assert(a);
assert(b);
for (;;) {
if (*x == 0 && *y == 0)
return 0;
if (r < 0 || q < 0)
return r - q;
if (r != 0)
return r;
}
}
int dns_name_equal(const char *x, const char *y) {
int r, q;
assert(x);
assert(y);
for (;;) {
if (*x == 0 && *y == 0)
return true;
if (r < 0)
return r;
if (q < 0)
return q;
return false;
}
}
int r, q;
n = name;
s = suffix;
for (;;) {
if (r < 0)
return r;
if (!saved_n)
saved_n = n;
if (r < 0)
return r;
if (r == 0 && q == 0)
return true;
if (r == 0 && saved_n == n)
return false;
/* Not the same, let's jump back, and try with the next label again */
s = suffix;
n = saved_n;
}
}
}
const uint8_t *p;
int r;
assert(a);
p = (const uint8_t*) a;
else
return -EAFNOSUPPORT;
if (r < 0)
return -ENOMEM;
return 0;
}
int dns_name_root(const char *name) {
int r;
if (r < 0)
return r;
return r == 0 && *name == 0;
}
int dns_name_single_label(const char *name) {
int r;
if (r < 0)
return r;
if (r == 0)
return 0;
if (r < 0)
return r;
return r == 0 && *name == 0;
}