Back to index

glibc  2.9
res_mkquery.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1985, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies, and that
00036  * the name of Digital Equipment Corporation not be used in advertising or
00037  * publicity pertaining to distribution of the document or software without
00038  * specific, written prior permission.
00039  *
00040  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00041  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00042  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00043  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00044  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00045  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00046  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047  * SOFTWARE.
00048  */
00049 
00050 /*
00051  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00052  *
00053  * Permission to use, copy, modify, and distribute this software for any
00054  * purpose with or without fee is hereby granted, provided that the above
00055  * copyright notice and this permission notice appear in all copies.
00056  *
00057  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00058  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00059  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00060  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00061  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00062  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00063  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00064  * SOFTWARE.
00065  */
00066 
00067 #if defined(LIBC_SCCS) && !defined(lint)
00068 static const char sccsid[] = "@(#)res_mkquery.c  8.1 (Berkeley) 6/4/93";
00069 static const char rcsid[] = "$BINDId: res_mkquery.c,v 8.12 1999/10/13 16:39:40 vixie Exp $";
00070 #endif /* LIBC_SCCS and not lint */
00071 
00072 #include <sys/types.h>
00073 #include <sys/param.h>
00074 #include <netinet/in.h>
00075 #include <arpa/nameser.h>
00076 #include <netdb.h>
00077 #include <resolv.h>
00078 #include <stdio.h>
00079 #include <string.h>
00080 
00081 /* Options.  Leave them on. */
00082 /* #define DEBUG */
00083 
00084 #ifdef _LIBC
00085 # include <hp-timing.h>
00086 # if HP_TIMING_AVAIL
00087 #  define RANDOM_BITS(Var) { uint64_t v64; HP_TIMING_NOW (v64); Var = v64; }
00088 # endif
00089 #endif
00090 
00091 /*
00092  * Form all types of queries.
00093  * Returns the size of the result or -1.
00094  */
00095 int
00096 res_nmkquery(res_state statp,
00097             int op,                /* opcode of query */
00098             const char *dname,            /* domain name */
00099             int class, int type,   /* class and type of query */
00100             const u_char *data,    /* resource record data */
00101             int datalen,           /* length of data */
00102             const u_char *newrr_in,       /* new rr for modify or append */
00103             u_char *buf,           /* buffer to put query */
00104             int buflen)            /* size of buffer */
00105 {
00106        register HEADER *hp;
00107        register u_char *cp;
00108        register int n;
00109        u_char *dnptrs[20], **dpp, **lastdnptr;
00110 
00111 #ifdef DEBUG
00112        if (statp->options & RES_DEBUG)
00113               printf(";; res_nmkquery(%s, %s, %s, %s)\n",
00114                      _res_opcodes[op], dname, p_class(class), p_type(type));
00115 #endif
00116        /*
00117         * Initialize header fields.
00118         */
00119        if ((buf == NULL) || (buflen < HFIXEDSZ))
00120               return (-1);
00121        memset(buf, 0, HFIXEDSZ);
00122        hp = (HEADER *) buf;
00123        /* We randomize the IDs every time.  The old code just
00124           incremented by one after the initial randomization which
00125           still predictable if the application does multiple
00126           requests.  */
00127        int randombits;
00128        do
00129          {
00130 #ifdef RANDOM_BITS
00131            RANDOM_BITS (randombits);
00132 #else
00133            struct timeval tv;
00134            __gettimeofday (&tv, NULL);
00135            randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
00136 #endif
00137          }
00138        while ((randombits & 0xffff) == 0);
00139        statp->id = (statp->id + randombits) & 0xffff;
00140        hp->id = statp->id;
00141        hp->opcode = op;
00142        hp->rd = (statp->options & RES_RECURSE) != 0;
00143        hp->rcode = NOERROR;
00144        cp = buf + HFIXEDSZ;
00145        buflen -= HFIXEDSZ;
00146        dpp = dnptrs;
00147        *dpp++ = buf;
00148        *dpp++ = NULL;
00149        lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
00150        /*
00151         * perform opcode specific processing
00152         */
00153        switch (op) {
00154        case NS_NOTIFY_OP:
00155               if ((buflen -= QFIXEDSZ + (data == NULL ? 0 : RRFIXEDSZ)) < 0)
00156                      return (-1);
00157               goto compose;
00158 
00159        case QUERY:
00160               if ((buflen -= QFIXEDSZ) < 0)
00161                      return (-1);
00162        compose:
00163               n = ns_name_compress(dname, cp, buflen,
00164                                  (const u_char **) dnptrs,
00165                                  (const u_char **) lastdnptr);
00166               if (n < 0)
00167                      return (-1);
00168               cp += n;
00169               buflen -= n;
00170               NS_PUT16 (type, cp);
00171               NS_PUT16 (class, cp);
00172               hp->qdcount = htons(1);
00173               if (op == QUERY || data == NULL)
00174                      break;
00175               /*
00176                * Make an additional record for completion domain.
00177                */
00178               n = ns_name_compress((char *)data, cp, buflen,
00179                                  (const u_char **) dnptrs,
00180                                  (const u_char **) lastdnptr);
00181               if (__builtin_expect (n < 0, 0))
00182                      return (-1);
00183               cp += n;
00184               buflen -= n;
00185               NS_PUT16 (T_NULL, cp);
00186               NS_PUT16 (class, cp);
00187               NS_PUT32 (0, cp);
00188               NS_PUT16 (0, cp);
00189               hp->arcount = htons(1);
00190               break;
00191 
00192        case IQUERY:
00193               /*
00194                * Initialize answer section
00195                */
00196               if (__builtin_expect (buflen < 1 + RRFIXEDSZ + datalen, 0))
00197                      return (-1);
00198               *cp++ = '\0'; /* no domain name */
00199               NS_PUT16 (type, cp);
00200               NS_PUT16 (class, cp);
00201               NS_PUT32 (0, cp);
00202               NS_PUT16 (datalen, cp);
00203               if (datalen) {
00204                      memcpy(cp, data, datalen);
00205                      cp += datalen;
00206               }
00207               hp->ancount = htons(1);
00208               break;
00209 
00210        default:
00211               return (-1);
00212        }
00213        return (cp - buf);
00214 }
00215 libresolv_hidden_def (res_nmkquery)
00216 
00217 
00218 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
00219 #ifndef T_OPT
00220 #define T_OPT   41
00221 #endif
00222 
00223 int
00224 __res_nopt(res_state statp,
00225           int n0,                /* current offset in buffer */
00226           u_char *buf,           /* buffer to put query */
00227           int buflen,            /* size of buffer */
00228           int anslen)            /* UDP answer buffer size */
00229 {
00230        u_int16_t flags = 0;
00231 
00232 #ifdef DEBUG
00233        if ((statp->options & RES_DEBUG) != 0U)
00234               printf(";; res_nopt()\n");
00235 #endif
00236 
00237        HEADER *hp = (HEADER *) buf;
00238        u_char *cp = buf + n0;
00239        u_char *ep = buf + buflen;
00240 
00241        if ((ep - cp) < 1 + RRFIXEDSZ)
00242               return -1;
00243 
00244        *cp++ = 0;    /* "." */
00245 
00246        NS_PUT16(T_OPT, cp); /* TYPE */
00247        NS_PUT16(anslen & 0xffff, cp);     /* CLASS = UDP payload size */
00248        *cp++ = NOERROR;     /* extended RCODE */
00249        *cp++ = 0;           /* EDNS version */
00250        /* XXX Once we support DNSSEC we change the flag value here.  */
00251        NS_PUT16(flags, cp);
00252        NS_PUT16(0, cp);     /* RDLEN */
00253        hp->arcount = htons(ntohs(hp->arcount) + 1);
00254 
00255        return cp - buf;
00256 }
00257 libresolv_hidden_def (__res_nopt)