2016-12-31 21:06:16 +08:00
|
|
|
/* Convert a DNS packet to a human-readable representation.
|
2020-01-01 08:14:33 +08:00
|
|
|
Copyright (C) 2016-2020 Free Software Foundation, Inc.
|
2016-12-31 21:06:16 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it 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.
|
|
|
|
|
|
|
|
The GNU C Library 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 the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2016-12-31 21:06:16 +08:00
|
|
|
|
|
|
|
#include <support/format_nss.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <resolv.h>
|
2017-12-14 20:48:56 +08:00
|
|
|
#include <stdbool.h>
|
2016-12-31 21:06:16 +08:00
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/support.h>
|
|
|
|
#include <support/xmemstream.h>
|
|
|
|
|
|
|
|
struct in_buffer
|
|
|
|
{
|
|
|
|
const unsigned char *data;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
extract_8 (struct in_buffer *in, unsigned char *value)
|
|
|
|
{
|
|
|
|
if (in->size == 0)
|
|
|
|
return false;
|
|
|
|
*value = in->data[0];
|
|
|
|
++in->data;
|
|
|
|
--in->size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
extract_16 (struct in_buffer *in, unsigned short *value)
|
|
|
|
{
|
|
|
|
if (in->size < 2)
|
|
|
|
return false;
|
|
|
|
*value = (in->data[0] << 8) | in->data[1];
|
|
|
|
in->data += 2;
|
|
|
|
in->size -= 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
extract_32 (struct in_buffer *in, unsigned *value)
|
|
|
|
{
|
|
|
|
if (in->size < 4)
|
|
|
|
return false;
|
|
|
|
unsigned a = in->data[0];
|
|
|
|
unsigned b = in->data[1];
|
|
|
|
unsigned c = in->data[2];
|
|
|
|
unsigned d = in->data[3];
|
|
|
|
*value = (a << 24) | (b << 16) | (c << 8) | d;
|
|
|
|
in->data += 4;
|
|
|
|
in->size -= 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
extract_bytes (struct in_buffer *in, size_t length, struct in_buffer *value)
|
|
|
|
{
|
|
|
|
if (in->size < length)
|
|
|
|
return false;
|
|
|
|
*value = (struct in_buffer) {in->data, length};
|
|
|
|
in->data += length;
|
|
|
|
in->size -= length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dname
|
|
|
|
{
|
|
|
|
char name[MAXDNAME + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
extract_name (struct in_buffer full, struct in_buffer *in, struct dname *value)
|
|
|
|
{
|
|
|
|
const unsigned char *full_end = full.data + full.size;
|
|
|
|
/* Sanity checks; these indicate buffer misuse. */
|
|
|
|
TEST_VERIFY_EXIT
|
|
|
|
(!(in->data < full.data || in->data > full_end
|
|
|
|
|| in->size > (size_t) (full_end - in->data)));
|
|
|
|
int ret = dn_expand (full.data, full_end, in->data,
|
|
|
|
value->name, sizeof (value->name));
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
in->data += ret;
|
|
|
|
in->size -= ret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
support_format_dns_packet (const unsigned char *buffer, size_t length)
|
|
|
|
{
|
|
|
|
struct in_buffer full = { buffer, length };
|
|
|
|
struct in_buffer in = full;
|
|
|
|
struct xmemstream mem;
|
|
|
|
xopen_memstream (&mem);
|
|
|
|
|
|
|
|
unsigned short txnid;
|
|
|
|
unsigned short flags;
|
|
|
|
unsigned short qdcount;
|
|
|
|
unsigned short ancount;
|
|
|
|
unsigned short nscount;
|
|
|
|
unsigned short adcount;
|
|
|
|
if (!(extract_16 (&in, &txnid)
|
|
|
|
&& extract_16 (&in, &flags)
|
|
|
|
&& extract_16 (&in, &qdcount)
|
|
|
|
&& extract_16 (&in, &ancount)
|
|
|
|
&& extract_16 (&in, &nscount)
|
|
|
|
&& extract_16 (&in, &adcount)))
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: could not parse DNS header\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (qdcount != 1)
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: question count is %d, not 1\n", qdcount);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
struct dname qname;
|
|
|
|
if (!extract_name (full, &in, &qname))
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: malformed QNAME\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
unsigned short qtype;
|
|
|
|
unsigned short qclass;
|
|
|
|
if (!(extract_16 (&in, &qtype)
|
|
|
|
&& extract_16 (&in, &qclass)))
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: malformed question\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (qtype != T_A && qtype != T_AAAA && qtype != T_PTR)
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: unsupported QTYPE %d\n", qtype);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (mem.out, "name: %s\n", qname.name);
|
|
|
|
|
|
|
|
for (int i = 0; i < ancount; ++i)
|
|
|
|
{
|
|
|
|
struct dname rname;
|
|
|
|
if (!extract_name (full, &in, &rname))
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: malformed record name\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
unsigned short rtype;
|
|
|
|
unsigned short rclass;
|
|
|
|
unsigned ttl;
|
|
|
|
unsigned short rdlen;
|
|
|
|
struct in_buffer rdata;
|
|
|
|
if (!(extract_16 (&in, &rtype)
|
|
|
|
&& extract_16 (&in, &rclass)
|
|
|
|
&& extract_32 (&in, &ttl)
|
|
|
|
&& extract_16 (&in, &rdlen)
|
|
|
|
&& extract_bytes (&in, rdlen, &rdata)))
|
|
|
|
{
|
|
|
|
fprintf (mem.out, "error: malformed record header\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Skip non-matching record types. */
|
2017-03-15 19:57:12 +08:00
|
|
|
if ((rtype != qtype && rtype != T_CNAME) || rclass != qclass)
|
2016-12-31 21:06:16 +08:00
|
|
|
continue;
|
|
|
|
switch (rtype)
|
|
|
|
{
|
|
|
|
case T_A:
|
|
|
|
if (rdlen == 4)
|
|
|
|
fprintf (mem.out, "address: %d.%d.%d.%d\n",
|
|
|
|
rdata.data[0],
|
|
|
|
rdata.data[1],
|
|
|
|
rdata.data[2],
|
|
|
|
rdata.data[3]);
|
|
|
|
else
|
2017-03-15 19:57:12 +08:00
|
|
|
fprintf (mem.out, "error: A record of size %d: %s\n",
|
|
|
|
rdlen, rname.name);
|
2016-12-31 21:06:16 +08:00
|
|
|
break;
|
|
|
|
case T_AAAA:
|
|
|
|
{
|
2017-03-15 19:57:12 +08:00
|
|
|
if (rdlen == 16)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
if (inet_ntop (AF_INET6, rdata.data, buf, sizeof (buf)) == NULL)
|
|
|
|
fprintf (mem.out, "error: AAAA record decoding failed: %m\n");
|
|
|
|
else
|
|
|
|
fprintf (mem.out, "address: %s\n", buf);
|
|
|
|
}
|
2016-12-31 21:06:16 +08:00
|
|
|
else
|
2017-03-15 19:57:12 +08:00
|
|
|
fprintf (mem.out, "error: AAAA record of size %d: %s\n",
|
|
|
|
rdlen, rname.name);
|
2016-12-31 21:06:16 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_CNAME:
|
|
|
|
case T_PTR:
|
|
|
|
{
|
|
|
|
struct dname name;
|
2017-03-15 19:57:12 +08:00
|
|
|
if (extract_name (full, &rdata, &name))
|
2016-12-31 21:06:16 +08:00
|
|
|
fprintf (mem.out, "name: %s\n", name.name);
|
|
|
|
else
|
|
|
|
fprintf (mem.out, "error: malformed CNAME/PTR record\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
xfclose_memstream (&mem);
|
|
|
|
return mem.buffer;
|
|
|
|
}
|