Blame ares_create_query.c

Packit 514978
Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#ifdef HAVE_NETINET_IN_H
Packit 514978
#  include <netinet/in.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_H
Packit 514978
#  include <arpa/nameser.h>
Packit 514978
#else
Packit 514978
#  include "nameser.h"
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
Packit 514978
#  include <arpa/nameser_compat.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_dns.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
#ifndef T_OPT
Packit 514978
#  define T_OPT  41 /* EDNS0 option (meta-RR) */
Packit 514978
#endif
Packit 514978
Packit 514978
/* Header format, from RFC 1035:
Packit 514978
 *                                  1  1  1  1  1  1
Packit 514978
 *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                      ID                       |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                    QDCOUNT                    |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                    ANCOUNT                    |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                    NSCOUNT                    |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                    ARCOUNT                    |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *
Packit 514978
 * AA, TC, RA, and RCODE are only set in responses.  Brief description
Packit 514978
 * of the remaining fields:
Packit 514978
 *      ID      Identifier to match responses with queries
Packit 514978
 *      QR      Query (0) or response (1)
Packit 514978
 *      Opcode  For our purposes, always QUERY
Packit 514978
 *      RD      Recursion desired
Packit 514978
 *      Z       Reserved (zero)
Packit 514978
 *      QDCOUNT Number of queries
Packit 514978
 *      ANCOUNT Number of answers
Packit 514978
 *      NSCOUNT Number of name server records
Packit 514978
 *      ARCOUNT Number of additional records
Packit 514978
 *
Packit 514978
 * Question format, from RFC 1035:
Packit 514978
 *                                  1  1  1  1  1  1
Packit 514978
 *    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                                               |
Packit 514978
 *  /                     QNAME                     /
Packit 514978
 *  /                                               /
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                     QTYPE                     |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *  |                     QCLASS                    |
Packit 514978
 *  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Packit 514978
 *
Packit 514978
 * The query name is encoded as a series of labels, each represented
Packit 514978
 * as a one-byte length (maximum 63) followed by the text of the
Packit 514978
 * label.  The list is terminated by a label of length zero (which can
Packit 514978
 * be thought of as the root domain).
Packit 514978
 */
Packit 514978
Packit 514978
int ares_create_query(const char *name, int dnsclass, int type,
Packit 514978
                      unsigned short id, int rd, unsigned char **bufp,
Packit 514978
                      int *buflenp, int max_udp_size)
Packit 514978
{
Packit 514978
  size_t len;
Packit 514978
  unsigned char *q;
Packit 514978
  const char *p;
Packit 514978
  size_t buflen;
Packit 514978
  unsigned char *buf;
Packit 514978
Packit 514978
  /* Set our results early, in case we bail out early with an error. */
Packit 514978
  *buflenp = 0;
Packit 514978
  *bufp = NULL;
Packit 514978
Packit 514978
  /* Allocate a memory area for the maximum size this packet might need. +2
Packit 514978
   * is for the length byte and zero termination if no dots or ecscaping is
Packit 514978
   * used.
Packit 514978
   */
Packit 514978
  len = strlen(name) + 2 + HFIXEDSZ + QFIXEDSZ +
Packit 514978
    (max_udp_size ? EDNSFIXEDSZ : 0);
Packit 514978
  buf = ares_malloc(len);
Packit 514978
  if (!buf)
Packit 514978
    return ARES_ENOMEM;
Packit 514978
Packit 514978
  /* Set up the header. */
Packit 514978
  q = buf;
Packit 514978
  memset(q, 0, HFIXEDSZ);
Packit 514978
  DNS_HEADER_SET_QID(q, id);
Packit 514978
  DNS_HEADER_SET_OPCODE(q, QUERY);
Packit 514978
  if (rd) {
Packit 514978
    DNS_HEADER_SET_RD(q, 1);
Packit 514978
  }
Packit 514978
  else {
Packit 514978
    DNS_HEADER_SET_RD(q, 0);
Packit 514978
  }
Packit 514978
  DNS_HEADER_SET_QDCOUNT(q, 1);
Packit 514978
Packit 514978
  if (max_udp_size) {
Packit 514978
      DNS_HEADER_SET_ARCOUNT(q, 1);
Packit 514978
  }
Packit 514978
Packit 514978
  /* A name of "." is a screw case for the loop below, so adjust it. */
Packit 514978
  if (strcmp(name, ".") == 0)
Packit 514978
    name++;
Packit 514978
Packit 514978
  /* Start writing out the name after the header. */
Packit 514978
  q += HFIXEDSZ;
Packit 514978
  while (*name)
Packit 514978
    {
Packit 514978
      if (*name == '.') {
Packit 514978
        ares_free (buf);
Packit 514978
        return ARES_EBADNAME;
Packit 514978
      }
Packit 514978
Packit 514978
      /* Count the number of bytes in this label. */
Packit 514978
      len = 0;
Packit 514978
      for (p = name; *p && *p != '.'; p++)
Packit 514978
        {
Packit 514978
          if (*p == '\\' && *(p + 1) != 0)
Packit 514978
            p++;
Packit 514978
          len++;
Packit 514978
        }
Packit 514978
      if (len > MAXLABEL) {
Packit 514978
        ares_free (buf);
Packit 514978
        return ARES_EBADNAME;
Packit 514978
      }
Packit 514978
Packit 514978
      /* Encode the length and copy the data. */
Packit 514978
      *q++ = (unsigned char)len;
Packit 514978
      for (p = name; *p && *p != '.'; p++)
Packit 514978
        {
Packit 514978
          if (*p == '\\' && *(p + 1) != 0)
Packit 514978
            p++;
Packit 514978
          *q++ = *p;
Packit 514978
        }
Packit 514978
Packit 514978
      /* Go to the next label and repeat, unless we hit the end. */
Packit 514978
      if (!*p)
Packit 514978
        break;
Packit 514978
      name = p + 1;
Packit 514978
    }
Packit 514978
Packit 514978
  /* Add the zero-length label at the end. */
Packit 514978
  *q++ = 0;
Packit 514978
Packit 514978
  /* Finish off the question with the type and class. */
Packit 514978
  DNS_QUESTION_SET_TYPE(q, type);
Packit 514978
  DNS_QUESTION_SET_CLASS(q, dnsclass);
Packit 514978
Packit 514978
  q += QFIXEDSZ;
Packit 514978
  if (max_udp_size)
Packit 514978
  {
Packit 514978
      memset(q, 0, EDNSFIXEDSZ);
Packit 514978
      q++;
Packit 514978
      DNS_RR_SET_TYPE(q, T_OPT);
Packit 514978
      DNS_RR_SET_CLASS(q, max_udp_size);
Packit 514978
      q += (EDNSFIXEDSZ-1);
Packit 514978
  }
Packit 514978
  buflen = (q - buf);
Packit 514978
Packit 514978
  /* Reject names that are longer than the maximum of 255 bytes that's
Packit 514978
   * specified in RFC 1035 ("To simplify implementations, the total length of
Packit 514978
   * a domain name (i.e., label octets and label length octets) is restricted
Packit 514978
   * to 255 octets or less."). */
Packit 514978
  if (buflen > (MAXCDNAME + HFIXEDSZ + QFIXEDSZ +
Packit 514978
                (max_udp_size ? EDNSFIXEDSZ : 0))) {
Packit 514978
    ares_free (buf);
Packit 514978
    return ARES_EBADNAME;
Packit 514978
  }
Packit 514978
Packit 514978
  /* we know this fits in an int at this point */
Packit 514978
  *buflenp = (int) buflen;
Packit 514978
  *bufp = buf;
Packit 514978
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}