resolved-dns-packet.c revision 322345fdb9865ef2477fba8e4bdde0e1183ef505
/*-*- 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 "utf8.h"
#include "util.h"
#include "resolved-dns-domain.h"
#include "resolved-dns-packet.h"
DnsPacket *p;
size_t a;
if (mtu <= 0)
else
a = mtu;
if (a < DNS_PACKET_HEADER_SIZE)
/* round up to next page size */
/* make sure we never allocate more than useful */
if (a > DNS_PACKET_SIZE_MAX)
a = DNS_PACKET_SIZE_MAX;
if (!p)
return -ENOMEM;
p->allocated = a;
p->n_ref = 1;
*ret = p;
return 0;
}
DnsPacket *p;
DnsPacketHeader *h;
int r;
r = dns_packet_new(&p, mtu);
if (r < 0)
return r;
h = DNS_PACKET_HEADER(p);
*ret = p;
return 0;
}
if (!p)
return NULL;
p->n_ref++;
return p;
}
static void dns_packet_free(DnsPacket *p) {
char *s;
assert(p);
if (p->rrs)
while ((s = hashmap_steal_first_key(p->names)))
free(s);
hashmap_free(p->names);
free(p);
}
if (!p)
return NULL;
if (p->n_ref == 1)
dns_packet_free(p);
else
p->n_ref--;
return NULL;
}
int dns_packet_validate(DnsPacket *p) {
assert(p);
if (p->size < DNS_PACKET_HEADER_SIZE)
return -EBADMSG;
if (p->size > DNS_PACKET_SIZE_MAX)
return -EBADMSG;
return 0;
}
int dns_packet_validate_reply(DnsPacket *p) {
int r;
assert(p);
r = dns_packet_validate(p);
if (r < 0)
return r;
if (DNS_PACKET_QR(p) == 0)
return -EBADMSG;
if (DNS_PACKET_OPCODE(p) != 0)
return -EBADMSG;
return 0;
}
assert(p);
size_t a;
if (a > DNS_PACKET_SIZE_MAX)
a = DNS_PACKET_SIZE_MAX;
return -EMSGSIZE;
if (p->data) {
void *d;
if (!d)
return -ENOMEM;
p->data = d;
} else {
if (!p->data)
return -ENOMEM;
}
p->allocated = a;
}
if (start)
if (ret)
return 0;
}
Iterator i;
char *s;
void *n;
assert(p);
return;
HASHMAP_FOREACH_KEY(s, n, p->names, i) {
if (PTR_TO_SIZE(n) < sz)
continue;
hashmap_remove(p->names, s);
free(s);
}
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
((uint8_t*) d)[0] = v;
return 0;
}
void *d;
int r;
assert(p);
if (r < 0)
return r;
return 0;
}
void *d;
size_t l;
int r;
assert(p);
assert(s);
l = strlen(s);
if (l > 255)
return -E2BIG;
if (r < 0)
return r;
return 0;
}
void *w;
int r;
assert(p);
assert(d);
if (l > DNS_LABEL_MAX)
return -E2BIG;
if (r < 0)
return r;
return 0;
}
int r;
assert(p);
saved_size = p->size;
while (*name) {
_cleanup_free_ char *s = NULL;
char label[DNS_LABEL_MAX];
size_t n;
if (n > 0) {
if (n < 0x4000) {
if (r < 0)
goto fail;
goto done;
}
}
if (!s) {
r = -ENOMEM;
goto fail;
}
if (r < 0)
goto fail;
r = dns_packet_append_label(p, label, r, &n);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
s = NULL;
}
r = dns_packet_append_uint8(p, 0, NULL);
if (r < 0)
return r;
done:
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
int r;
assert(p);
assert(k);
saved_size = p->size;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (start)
*start = saved_size;
return 0;
fail:
return r;
}
assert(p);
return -EMSGSIZE;
if (ret)
if (start)
return 0;
}
assert(p);
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
return 0;
}
const void *d;
int r;
assert(p);
if (r < 0)
return r;
return 0;
}
const void *d;
char *t;
uint8_t c;
int r;
assert(p);
saved_rindex = p->rindex;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
r = dns_packet_read(p, c, &d, NULL);
if (r < 0)
goto fail;
if (memchr(d, 0, c)) {
r = -EBADMSG;
goto fail;
}
t = strndup(d, c);
if (!t) {
r = -ENOMEM;
goto fail;
}
if (!utf8_is_valid(t)) {
free(t);
r = -EBADMSG;
goto fail;
}
*ret = t;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
bool first = true;
int r;
assert(p);
saved_rindex = p->rindex;
for (;;) {
uint8_t c, d;
r = dns_packet_read_uint8(p, &c, NULL);
if (r < 0)
goto fail;
if (c == 0)
/* End of name */
break;
else if (c <= 63) {
_cleanup_free_ char *t = NULL;
const char *label;
/* Literal label */
if (r < 0)
goto fail;
r = dns_label_escape(label, c, &t);
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
if (!first)
ret[n++] = '.';
else
first = false;
n += r;
continue;
} else if ((c & 0xc0) == 0xc0) {
/* Pointer */
r = dns_packet_read_uint8(p, &d, NULL);
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (after_rindex == 0)
after_rindex = p->rindex;
} else
goto fail;
}
r = -ENOMEM;
goto fail;
}
ret[n] = 0;
if (after_rindex != 0)
p->rindex= after_rindex;
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int r;
assert(p);
saved_rindex = p->rindex;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
*ret = k;
zero(k);
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
const void *d;
int r;
assert(p);
if (!rr)
return -ENOMEM;
saved_rindex = p->rindex;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
break;
case DNS_TYPE_HINFO:
if (r < 0)
goto fail;
break;
case DNS_TYPE_A:
if (r < 0)
goto fail;
break;
case DNS_TYPE_AAAA:
if (r < 0)
goto fail;
break;
default:
if (r < 0)
goto fail;
r = -ENOMEM;
goto fail;
}
break;
}
if (r < 0)
goto fail;
r = -EBADMSG;
goto fail;
}
if (start)
*start = saved_rindex;
return 0;
fail:
return r;
}
int dns_packet_skip_question(DnsPacket *p) {
unsigned i, n;
int r;
assert(p);
n = DNS_PACKET_QDCOUNT(p);
for (i = 0; i < n; i++) {
if (r < 0)
return r;
}
return 0;
}
int dns_packet_extract_rrs(DnsPacket *p) {
unsigned n, added = 0;
int r;
if (p->rrs)
return (int) DNS_PACKET_RRCOUNT(p);
saved_rindex = p->rindex;
r = dns_packet_skip_question(p);
if (r < 0)
goto finish;
n = DNS_PACKET_RRCOUNT(p);
if (n <= 0) {
r = 0;
goto finish;
}
if (!rrs) {
r = -ENOMEM;
goto finish;
}
if (r < 0) {
goto finish;
}
}
r = (int) n;
p->rindex = saved_rindex;
return r;
}
static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = {
[DNS_RCODE_SUCCESS] = "SUCCESS",
[DNS_RCODE_FORMERR] = "FORMERR",
[DNS_RCODE_SERVFAIL] = "SERVFAIL",
[DNS_RCODE_NXDOMAIN] = "NXDOMAIN",
[DNS_RCODE_NOTIMP] = "NOTIMP",
[DNS_RCODE_REFUSED] = "REFUSED",
[DNS_RCODE_YXDOMAIN] = "YXDOMAIN",
[DNS_RCODE_YXRRSET] = "YRRSET",
[DNS_RCODE_NXRRSET] = "NXRRSET",
[DNS_RCODE_NOTAUTH] = "NOTAUTH",
[DNS_RCODE_NOTZONE] = "NOTZONE",
[DNS_RCODE_BADVERS] = "BADVERS",
[DNS_RCODE_BADKEY] = "BADKEY",
[DNS_RCODE_BADTIME] = "BADTIME",
[DNS_RCODE_BADMODE] = "BADMODE",
[DNS_RCODE_BADNAME] = "BADNAME",
[DNS_RCODE_BADALG] = "BADALG",
[DNS_RCODE_BADTRUNC] = "BADTRUNC",
};