home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / other / irc / ircd / res_mkquery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-14  |  5.2 KB  |  193 lines

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