home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / netlib / lib / rmkquery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-27  |  6.5 KB  |  214 lines

  1. /*
  2.  * Adopted to Mint-Net 1994, Kay Roemer
  3.  */
  4.  
  5. /*
  6.  * Copyright (c) 1985 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)res_mkquery.c    6.16 (Berkeley) 3/6/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. #include "socklib.h"
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/nameser.h>
  46. #include <resolv.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. /*
  51.  * Form all types of queries.
  52.  * Returns the size of the result or -1.
  53.  */
  54. int
  55. res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
  56.     int op;            /* opcode of query */
  57.     const char *dname;        /* domain name */
  58.     int class, type;    /* class and type of query */
  59.     char *data;        /* resource record data */
  60.     int datalen;        /* length of data */
  61.     struct rrec *newrr;    /* new rr for modify or append */
  62.     char *buf;        /* buffer to put query */
  63.     int buflen;        /* size of buffer */
  64. {
  65.     register HEADER *hp;
  66.     register char *cp;
  67.     register int n;
  68.     char *dnptrs[10], **dpp, **lastdnptr;
  69.  
  70. #ifdef DEBUG
  71.     if (_res.options & RES_DEBUG)
  72.         printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
  73. #endif DEBUG
  74.     /*
  75.      * Initialize header fields.
  76.      */
  77.     if ((buf == NULL) || (buflen < sizeof(HEADER)))
  78.         return(-1);
  79.     bzero(buf, sizeof(HEADER));
  80.     hp = (HEADER *) buf;
  81.     hp->id = htons(++_res.id);
  82.     hp->opcode = op;
  83.     hp->pr = (_res.options & RES_PRIMARY) != 0;
  84.     hp->rd = (_res.options & RES_RECURSE) != 0;
  85.     hp->rcode = NOERROR;
  86.     cp = buf + sizeof(HEADER);
  87.     buflen -= sizeof(HEADER);
  88.     dpp = dnptrs;
  89.     *dpp++ = buf;
  90.     *dpp++ = NULL;
  91.     lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
  92.     /*
  93.      * perform opcode specific processing
  94.      */
  95.     switch (op) {
  96.     case QUERY:
  97.         if ((buflen -= QFIXEDSZ) < 0)
  98.             return(-1);
  99.         if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen,
  100.             (u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
  101.             return (-1);
  102.         cp += n;
  103.         buflen -= n;
  104.         __putshort(type, (u_char *)cp);
  105.         cp += sizeof(u_short);
  106.         __putshort(class, (u_char *)cp);
  107.         cp += sizeof(u_short);
  108.         hp->qdcount = htons(1);
  109.         if (op == QUERY || data == NULL)
  110.             break;
  111.         /*
  112.          * Make an additional record for completion domain.
  113.          */
  114.         buflen -= RRFIXEDSZ;
  115.         if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen,
  116.             (u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
  117.             return (-1);
  118.         cp += n;
  119.         buflen -= n;
  120.         __putshort(T_NULL, (u_char *)cp);
  121.         cp += sizeof(u_short);
  122.         __putshort(class, (u_char *)cp);
  123.         cp += sizeof(u_short);
  124.         __putlong(0, (u_char *)cp);
  125.         cp += sizeof(u_long);
  126.         __putshort(0, (u_char *)cp);
  127.         cp += sizeof(u_short);
  128.         hp->arcount = htons(1);
  129.         break;
  130.  
  131.     case IQUERY:
  132.         /*
  133.          * Initialize answer section
  134.          */
  135.         if (buflen < 1 + RRFIXEDSZ + datalen)
  136.             return (-1);
  137.         *cp++ = '\0';    /* no domain name */
  138.         __putshort(type, (u_char *)cp);
  139.         cp += sizeof(u_short);
  140.         __putshort(class, (u_char *)cp);
  141.         cp += sizeof(u_short);
  142.         __putlong(0, (u_char *)cp);
  143.         cp += sizeof(u_long);
  144.         __putshort(datalen, (u_char *)cp);
  145.         cp += sizeof(u_short);
  146.         if (datalen) {
  147.             bcopy(data, cp, datalen);
  148.             cp += datalen;
  149.         }
  150.         hp->ancount = htons(1);
  151.         break;
  152.  
  153. #ifdef ALLOW_UPDATES
  154.     /*
  155.      * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
  156.      * (Record to be modified is followed by its replacement in msg.)
  157.      */
  158.     case UPDATEM:
  159.     case UPDATEMA:
  160.  
  161.     case UPDATED:
  162.         /*
  163.          * The res code for UPDATED and UPDATEDA is the same; user
  164.          * calls them differently: specifies data for UPDATED; server
  165.          * ignores data if specified for UPDATEDA.
  166.          */
  167.     case UPDATEDA:
  168.         buflen -= RRFIXEDSZ + datalen;
  169.         if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
  170.             return (-1);
  171.         cp += n;
  172.         __putshort(type, cp);
  173.                 cp += sizeof(u_short);
  174.                 __putshort(class, cp);
  175.                 cp += sizeof(u_short);
  176.         __putlong(0, cp);
  177.         cp += sizeof(u_long);
  178.         __putshort(datalen, cp);
  179.                 cp += sizeof(u_short);
  180.         if (datalen) {
  181.             bcopy(data, cp, datalen);
  182.             cp += datalen;
  183.         }
  184.         if ( (op == UPDATED) || (op == UPDATEDA) ) {
  185.             hp->ancount = htons(0);
  186.             break;
  187.         }
  188.         /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
  189.  
  190.     case UPDATEA:    /* Add new resource record */
  191.         buflen -= RRFIXEDSZ + datalen;
  192.         if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
  193.             return (-1);
  194.         cp += n;
  195.         __putshort(newrr->r_type, cp);
  196.                 cp += sizeof(u_short);
  197.                 __putshort(newrr->r_class, cp);
  198.                 cp += sizeof(u_short);
  199.         __putlong(0, cp);
  200.         cp += sizeof(u_long);
  201.         __putshort(newrr->r_size, cp);
  202.                 cp += sizeof(u_short);
  203.         if (newrr->r_size) {
  204.             bcopy(newrr->r_data, cp, newrr->r_size);
  205.             cp += newrr->r_size;
  206.         }
  207.         hp->ancount = htons(0);
  208.         break;
  209.  
  210. #endif ALLOW_UPDATES
  211.     }
  212.     return (cp - buf);
  213. }
  214.