2006-07-22 17:37:10 +02:00
|
|
|
/* $Id$ */
|
2006-07-22 17:21:13 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2004-07-23 00:18:45 +02:00
|
|
|
#include "setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-09-15 19:14:29 +02:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <netinet/in.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser.h>
|
|
|
|
#else
|
|
|
|
# include "nameser.h"
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
2005-04-07 00:27:42 +02:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser_compat.h>
|
2003-10-07 23:54:04 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
2006-07-22 17:21:13 +02:00
|
|
|
#include "ares_private.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Header format, from RFC 1035:
|
|
|
|
* 1 1 1 1 1 1
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | ID |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | QDCOUNT |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | ANCOUNT |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | NSCOUNT |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | ARCOUNT |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
*
|
|
|
|
* AA, TC, RA, and RCODE are only set in responses. Brief description
|
|
|
|
* of the remaining fields:
|
2004-10-06 09:50:18 +02:00
|
|
|
* ID Identifier to match responses with queries
|
|
|
|
* QR Query (0) or response (1)
|
|
|
|
* Opcode For our purposes, always QUERY
|
|
|
|
* RD Recursion desired
|
|
|
|
* Z Reserved (zero)
|
|
|
|
* QDCOUNT Number of queries
|
|
|
|
* ANCOUNT Number of answers
|
|
|
|
* NSCOUNT Number of name server records
|
|
|
|
* ARCOUNT Number of additional records
|
2003-10-07 23:54:04 +02:00
|
|
|
*
|
|
|
|
* Question format, from RFC 1035:
|
|
|
|
* 1 1 1 1 1 1
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | |
|
|
|
|
* / QNAME /
|
|
|
|
* / /
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | QTYPE |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
* | QCLASS |
|
|
|
|
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|
|
|
*
|
|
|
|
* The query name is encoded as a series of labels, each represented
|
|
|
|
* as a one-byte length (maximum 63) followed by the text of the
|
|
|
|
* label. The list is terminated by a label of length zero (which can
|
|
|
|
* be thought of as the root domain).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
|
2004-10-06 09:50:18 +02:00
|
|
|
int rd, unsigned char **buf, int *buflen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
unsigned char *q;
|
|
|
|
const char *p;
|
|
|
|
|
2007-09-29 15:56:36 +02:00
|
|
|
/* Set our results early, in case we bail out early with an error. */
|
|
|
|
*buflen = 0;
|
|
|
|
*buf = NULL;
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Compute the length of the encoded name so we can check buflen.
|
|
|
|
* Start counting at 1 for the zero-length label at the end. */
|
|
|
|
len = 1;
|
|
|
|
for (p = name; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == '\\' && *(p + 1) != 0)
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
2003-10-07 23:54:04 +02:00
|
|
|
len++;
|
|
|
|
}
|
|
|
|
/* If there are n periods in the name, there are n + 1 labels, and
|
|
|
|
* thus n + 1 length fields, unless the name is empty or ends with a
|
|
|
|
* period. So add 1 unless name is empty or ends with a period.
|
|
|
|
*/
|
|
|
|
if (*name && *(p - 1) != '.')
|
|
|
|
len++;
|
|
|
|
|
2007-09-29 15:58:23 +02:00
|
|
|
/* Immediately reject names that are longer than the maximum of 255
|
|
|
|
* bytes that's specified in RFC 1035 ("To simplify implementations,
|
|
|
|
* the total length of a domain name (i.e., label octets and label
|
|
|
|
* length octets) is restricted to 255 octets or less."). We aren't
|
|
|
|
* doing this just to be a stickler about RFCs. For names that are
|
|
|
|
* too long, 'dnscache' closes its TCP connection to us immediately
|
|
|
|
* (when using TCP) and ignores the request when using UDP, and
|
|
|
|
* BIND's named returns ServFail (TCP or UDP). Sending a request
|
|
|
|
* that we know will cause 'dnscache' to close the TCP connection is
|
|
|
|
* painful, since that makes any other outstanding requests on that
|
|
|
|
* connection fail. And sending a UDP request that we know
|
|
|
|
* 'dnscache' will ignore is bad because resources will be tied up
|
|
|
|
* until we time-out the request.
|
|
|
|
*/
|
|
|
|
if (len > MAXCDNAME)
|
|
|
|
return ARES_EBADNAME;
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
*buflen = len + HFIXEDSZ + QFIXEDSZ;
|
|
|
|
*buf = malloc(*buflen);
|
|
|
|
if (!*buf)
|
|
|
|
return ARES_ENOMEM;
|
|
|
|
|
|
|
|
/* Set up the header. */
|
|
|
|
q = *buf;
|
|
|
|
memset(q, 0, HFIXEDSZ);
|
|
|
|
DNS_HEADER_SET_QID(q, id);
|
|
|
|
DNS_HEADER_SET_OPCODE(q, QUERY);
|
2007-02-19 03:29:35 +01:00
|
|
|
if (rd) {
|
|
|
|
DNS_HEADER_SET_RD(q, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DNS_HEADER_SET_RD(q, 0);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
DNS_HEADER_SET_QDCOUNT(q, 1);
|
|
|
|
|
|
|
|
/* A name of "." is a screw case for the loop below, so adjust it. */
|
|
|
|
if (strcmp(name, ".") == 0)
|
|
|
|
name++;
|
|
|
|
|
|
|
|
/* Start writing out the name after the header. */
|
|
|
|
q += HFIXEDSZ;
|
|
|
|
while (*name)
|
|
|
|
{
|
|
|
|
if (*name == '.')
|
2004-10-06 09:50:18 +02:00
|
|
|
return ARES_EBADNAME;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Count the number of bytes in this label. */
|
|
|
|
len = 0;
|
|
|
|
for (p = name; *p && *p != '.'; p++)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (*p == '\\' && *(p + 1) != 0)
|
|
|
|
p++;
|
|
|
|
len++;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
if (len > MAXLABEL)
|
2004-10-06 09:50:18 +02:00
|
|
|
return ARES_EBADNAME;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Encode the length and copy the data. */
|
2007-02-01 02:42:13 +01:00
|
|
|
*q++ = (unsigned char)len;
|
2003-10-07 23:54:04 +02:00
|
|
|
for (p = name; *p && *p != '.'; p++)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (*p == '\\' && *(p + 1) != 0)
|
|
|
|
p++;
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Go to the next label and repeat, unless we hit the end. */
|
|
|
|
if (!*p)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
name = p + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the zero-length label at the end. */
|
|
|
|
*q++ = 0;
|
|
|
|
|
|
|
|
/* Finish off the question with the type and class. */
|
|
|
|
DNS_QUESTION_SET_TYPE(q, type);
|
|
|
|
DNS_QUESTION_SET_CLASS(q, dnsclass);
|
|
|
|
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|