Blame resolv/res_mkquery.c

Packit 6c4009
/* Creation of DNS query packets.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1985, 1993
Packit 6c4009
 *    The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies, and that
Packit 6c4009
 * the name of Digital Equipment Corporation not be used in advertising or
Packit 6c4009
 * publicity pertaining to distribution of the document or software without
Packit 6c4009
 * specific, written prior permission.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
Packit 6c4009
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
Packit 6c4009
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
Packit 6c4009
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit 6c4009
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit 6c4009
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit 6c4009
 * SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
Packit 6c4009
 *
Packit 6c4009
 * Permission to use, copy, modify, and distribute this software for any
Packit 6c4009
 * purpose with or without fee is hereby granted, provided that the above
Packit 6c4009
 * copyright notice and this permission notice appear in all copies.
Packit 6c4009
 *
Packit 6c4009
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
Packit 6c4009
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
Packit 6c4009
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
Packit 6c4009
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit 6c4009
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit 6c4009
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit 6c4009
 * SOFTWARE.
Packit 6c4009
 */
Packit 6c4009
Packit Service e24dc2
#include <stdint.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <arpa/nameser.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <resolv/resolv-internal.h>
Packit 6c4009
#include <resolv/resolv_context.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <shlib-compat.h>
Packit Service e24dc2
#include <random-bits.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__res_context_mkquery (struct resolv_context *ctx, int op, const char *dname,
Packit 6c4009
                       int class, int type, const unsigned char *data,
Packit 6c4009
                       unsigned char *buf, int buflen)
Packit 6c4009
{
Packit 6c4009
  HEADER *hp;
Packit 6c4009
  unsigned char *cp;
Packit 6c4009
  int n;
Packit 6c4009
  unsigned char *dnptrs[20], **dpp, **lastdnptr;
Packit 6c4009
Packit 6c4009
  if (class < 0 || class > 65535 || type < 0 || type > 65535)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Initialize header fields.  */
Packit 6c4009
  if ((buf == NULL) || (buflen < HFIXEDSZ))
Packit 6c4009
    return -1;
Packit 6c4009
  memset (buf, 0, HFIXEDSZ);
Packit 6c4009
  hp = (HEADER *) buf;
Packit 6c4009
  /* We randomize the IDs every time.  The old code just incremented
Packit 6c4009
     by one after the initial randomization which still predictable if
Packit 6c4009
     the application does multiple requests.  */
Packit Service e24dc2
  hp->id = random_bits ();
Packit 6c4009
  hp->opcode = op;
Packit 6c4009
  hp->rd = (ctx->resp->options & RES_RECURSE) != 0;
Packit 6c4009
  hp->rcode = NOERROR;
Packit 6c4009
  cp = buf + HFIXEDSZ;
Packit 6c4009
  buflen -= HFIXEDSZ;
Packit 6c4009
  dpp = dnptrs;
Packit 6c4009
  *dpp++ = buf;
Packit 6c4009
  *dpp++ = NULL;
Packit 6c4009
  lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
Packit 6c4009
Packit 6c4009
  /* Perform opcode specific processing.  */
Packit 6c4009
  switch (op)
Packit 6c4009
    {
Packit 6c4009
    case NS_NOTIFY_OP:
Packit 6c4009
      if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0)
Packit 6c4009
        return -1;
Packit 6c4009
      goto compose;
Packit 6c4009
Packit 6c4009
    case QUERY:
Packit 6c4009
      if ((buflen -= QFIXEDSZ) < 0)
Packit 6c4009
        return -1;
Packit 6c4009
    compose:
Packit 6c4009
      n = ns_name_compress (dname, cp, buflen,
Packit 6c4009
                            (const unsigned char **) dnptrs,
Packit 6c4009
                            (const unsigned char **) lastdnptr);
Packit 6c4009
      if (n < 0)
Packit 6c4009
        return -1;
Packit 6c4009
      cp += n;
Packit 6c4009
      buflen -= n;
Packit 6c4009
      NS_PUT16 (type, cp);
Packit 6c4009
      NS_PUT16 (class, cp);
Packit 6c4009
      hp->qdcount = htons (1);
Packit 6c4009
      if (op == QUERY || data == NULL)
Packit 6c4009
        break;
Packit 6c4009
Packit 6c4009
      /* Make an additional record for completion domain.  */
Packit 6c4009
      n = ns_name_compress ((char *)data, cp, buflen,
Packit 6c4009
                            (const unsigned char **) dnptrs,
Packit 6c4009
                            (const unsigned char **) lastdnptr);
Packit 6c4009
      if (__glibc_unlikely (n < 0))
Packit 6c4009
        return -1;
Packit 6c4009
      cp += n;
Packit 6c4009
      buflen -= n;
Packit 6c4009
      NS_PUT16 (T_NULL, cp);
Packit 6c4009
      NS_PUT16 (class, cp);
Packit 6c4009
      NS_PUT32 (0, cp);
Packit 6c4009
      NS_PUT16 (0, cp);
Packit 6c4009
      hp->arcount = htons (1);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
  return cp - buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Common part of res_nmkquery and res_mkquery.  */
Packit 6c4009
static int
Packit 6c4009
context_mkquery_common (struct resolv_context *ctx,
Packit 6c4009
                        int op, const char *dname, int class, int type,
Packit 6c4009
                        const unsigned char *data,
Packit 6c4009
                        unsigned char *buf, int buflen)
Packit 6c4009
{
Packit 6c4009
  if (ctx == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
  int result = __res_context_mkquery
Packit 6c4009
    (ctx, op, dname, class, type, data, buf, buflen);
Packit 6c4009
  if (result >= 2)
Packit 6c4009
    memcpy (&ctx->resp->id, buf, 2);
Packit 6c4009
  __resolv_context_put (ctx);
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Form all types of queries.  Returns the size of the result or -1 on
Packit 6c4009
   error.
Packit 6c4009
Packit 6c4009
   STATP points to an initialized resolver state.  OP is the opcode of
Packit 6c4009
   the query.  DNAME is the domain.  CLASS and TYPE are the DNS query
Packit 6c4009
   class and type.  DATA can be NULL; otherwise, it is a pointer to a
Packit 6c4009
   domain name which is included in the generated packet (if op ==
Packit 6c4009
   NS_NOTIFY_OP).  BUF must point to the out buffer of BUFLEN bytes.
Packit 6c4009
Packit 6c4009
   DATALEN and NEWRR_IN are currently ignored.  */
Packit 6c4009
int
Packit 6c4009
res_nmkquery (res_state statp, int op, const char *dname,
Packit 6c4009
              int class, int type,
Packit 6c4009
              const unsigned char *data, int datalen,
Packit 6c4009
              const unsigned char *newrr_in,
Packit 6c4009
              unsigned char *buf, int buflen)
Packit 6c4009
{
Packit 6c4009
  return context_mkquery_common
Packit 6c4009
    (__resolv_context_get_override (statp),
Packit 6c4009
     op, dname, class, type, data, buf, buflen);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
res_mkquery (int op, const char *dname, int class, int type,
Packit 6c4009
             const unsigned char *data, int datalen,
Packit 6c4009
             const unsigned char *newrr_in,
Packit 6c4009
             unsigned char *buf, int buflen)
Packit 6c4009
{
Packit 6c4009
  return context_mkquery_common
Packit 6c4009
    (__resolv_context_get_preinit (),
Packit 6c4009
     op, dname, class, type, data, buf, buflen);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Create an OPT resource record.  Return the length of the final
Packit 6c4009
   packet, or -1 on error.
Packit 6c4009
Packit 6c4009
   STATP must be an initialized resolver state.  N0 is the current
Packit 6c4009
   number of bytes of the packet (already written to BUF by the
Packit 6c4009
   aller).  BUF is the packet being constructed.  The array it
Packit 6c4009
   pointers to must be BUFLEN bytes long.  ANSLEN is the advertised
Packit 6c4009
   EDNS buffer size (to be included in the OPT resource record).  */
Packit 6c4009
int
Packit 6c4009
__res_nopt (struct resolv_context *ctx,
Packit 6c4009
            int n0, unsigned char *buf, int buflen, int anslen)
Packit 6c4009
{
Packit 6c4009
  uint16_t flags = 0;
Packit 6c4009
  HEADER *hp = (HEADER *) buf;
Packit 6c4009
  unsigned char *cp = buf + n0;
Packit 6c4009
  unsigned char *ep = buf + buflen;
Packit 6c4009
Packit 6c4009
  if ((ep - cp) < 1 + RRFIXEDSZ)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Add the root label.  */
Packit 6c4009
  *cp++ = 0;
Packit 6c4009
Packit 6c4009
  NS_PUT16 (T_OPT, cp);         /* Record type.  */
Packit 6c4009
Packit 6c4009
  /* Lowering the advertised buffer size based on the actual
Packit 6c4009
     answer buffer size is desirable because the server will
Packit 6c4009
     minimize the reply to fit into the UDP packet (and A
Packit 6c4009
     non-minimal response might not fit the buffer).
Packit 6c4009
Packit 6c4009
     The RESOLV_EDNS_BUFFER_SIZE limit could still result in TCP
Packit 6c4009
     fallback and a non-minimal response which has to be
Packit 6c4009
     hard-truncated in the stub resolver, but this is price to
Packit 6c4009
     pay for avoiding fragmentation.  (This issue does not
Packit 6c4009
     affect the nss_dns functions because they use the stub
Packit 6c4009
     resolver in such a way that it allocates a properly sized
Packit 6c4009
     response buffer.)  */
Packit 6c4009
  {
Packit 6c4009
    uint16_t buffer_size;
Packit 6c4009
    if (anslen < 512)
Packit 6c4009
      buffer_size = 512;
Packit 6c4009
    else if (anslen > RESOLV_EDNS_BUFFER_SIZE)
Packit 6c4009
      buffer_size = RESOLV_EDNS_BUFFER_SIZE;
Packit 6c4009
    else
Packit 6c4009
      buffer_size = anslen;
Packit 6c4009
    NS_PUT16 (buffer_size, cp);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  *cp++ = NOERROR;              /* Extended RCODE.  */
Packit 6c4009
  *cp++ = 0;                    /* EDNS version.  */
Packit 6c4009
Packit 6c4009
  if (ctx->resp->options & RES_USE_DNSSEC)
Packit 6c4009
    flags |= NS_OPT_DNSSEC_OK;
Packit 6c4009
Packit 6c4009
  NS_PUT16 (flags, cp);
Packit 6c4009
  NS_PUT16 (0, cp);       /* RDATA length (no options are preent).  */
Packit 6c4009
  hp->arcount = htons (ntohs (hp->arcount) + 1);
Packit 6c4009
Packit 6c4009
  return cp - buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_2)
Packit 6c4009
# undef res_mkquery
Packit 6c4009
weak_alias (__res_mkquery, res_mkquery);
Packit 6c4009
#endif