home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / named / tools / nslookup / skip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-24  |  5.1 KB  |  188 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, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)skip.c    5.12 (Berkeley) 3/21/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  *******************************************************************************
  40.  *
  41.  *  skip.c --
  42.  *
  43.  *    Routines to skip over portions of a query buffer.
  44.  *
  45.  *    Note: this file has been submitted for inclusion in
  46.  *    BIND resolver library. When this has been done, this file
  47.  *    is no longer necessary (assuming there haven't been any
  48.  *    changes).
  49.  *
  50.  *    Adapted from 4.3BSD BIND res_debug.c
  51.  *
  52.  *******************************************************************************
  53.  */
  54.  
  55. #include <sys/param.h>
  56. #include <netinet/in.h>
  57. #include <arpa/nameser.h>
  58. #include <resolv.h>
  59. #include <stdio.h>
  60.  
  61. char *res_skip_rr();
  62.  
  63.  
  64. /*
  65.  *******************************************************************************
  66.  *
  67.  *  res_skip --
  68.  *
  69.  *     Skip the contents of a query.
  70.  *
  71.  *     Interpretation of numFieldsToSkip argument:
  72.  *            res_skip returns pointer to:
  73.  *        1 ->  start of question records.
  74.  *        2 ->  start of authoritative answer records.
  75.  *        3 ->  start of additional records.
  76.  *        4 ->  first byte after end of additional records.
  77.  *
  78.  *   Results:
  79.  *    (address)    - success operation.
  80.  *      NULL         - a resource record had an incorrect format.
  81.  *
  82.  *******************************************************************************
  83.  */
  84.  
  85. char *
  86. res_skip(msg, numFieldsToSkip, eom)
  87.     char *msg;
  88.     int numFieldsToSkip;
  89.     char *eom;
  90. {
  91.     register char *cp;
  92.     register HEADER *hp;
  93.     register int tmp;
  94.     register int n;
  95.  
  96.     /*
  97.      * Skip the header fields.
  98.      */
  99.     hp = (HEADER *)msg;
  100.     cp = msg + sizeof(HEADER);
  101.  
  102.     /*
  103.      * skip question records.
  104.      */
  105.     if (n = ntohs(hp->qdcount) ) {
  106.         while (--n >= 0 && cp < eom) {
  107.             tmp = dn_skipname((u_char *)cp, (u_char *)eom);
  108.             if (tmp == -1) return(NULL);
  109.             cp += tmp;
  110.             cp += sizeof(u_short);    /* type     */
  111.             cp += sizeof(u_short);    /* class     */
  112.         }
  113.     }
  114.     if (--numFieldsToSkip <= 0) return(cp);
  115.  
  116.     /*
  117.      * skip authoritative answer records
  118.      */
  119.     if (n = ntohs(hp->ancount)) {
  120.         while (--n >= 0 && cp < eom) {
  121.             cp = res_skip_rr(cp, eom);
  122.             if (cp == NULL) return(NULL);
  123.         }
  124.     }
  125.     if (--numFieldsToSkip == 0) return(cp);
  126.  
  127.     /*
  128.      * skip name server records
  129.      */
  130.     if (n = ntohs(hp->nscount)) {
  131.         while (--n >= 0 && cp < eom) {
  132.             cp = res_skip_rr(cp, eom);
  133.             if (cp == NULL) return(NULL);
  134.         }
  135.     }
  136.     if (--numFieldsToSkip == 0) return(cp);
  137.  
  138.     /*
  139.      * skip additional records
  140.      */
  141.     if (n = ntohs(hp->arcount)) {
  142.         while (--n >= 0 && cp < eom) {
  143.             cp = res_skip_rr(cp, eom);
  144.             if (cp == NULL) return(NULL);
  145.         }
  146.     }
  147.  
  148.     return(cp);
  149. }
  150.  
  151.  
  152. /*
  153.  *******************************************************************************
  154.  *
  155.  *  res_skip_rr --
  156.  *
  157.  *     Skip over resource record fields.
  158.  *
  159.  *   Results:
  160.  *    (address)    - success operation.
  161.  *      NULL         - a resource record had an incorrect format.
  162.  *******************************************************************************
  163.  */
  164.  
  165. char *
  166. res_skip_rr(cp, eom)
  167.     char *cp;
  168.     char *eom;
  169. {
  170.     int tmp;
  171.     int dlen;
  172.  
  173.     if ((tmp = dn_skipname((u_char *)cp, (u_char *)eom)) == -1)
  174.         return (NULL);            /* compression error */
  175.     cp += tmp;
  176.     if ((cp + RRFIXEDSZ) > eom)
  177.         return (NULL);
  178.     cp += sizeof(u_short);    /*     type     */
  179.     cp += sizeof(u_short);    /*     class     */
  180.     cp += sizeof(u_long);    /*     ttl     */
  181.     dlen = _getshort(cp);
  182.     cp += sizeof(u_short);    /*     dlen     */
  183.     cp += dlen;
  184.     if (cp > eom)
  185.         return (NULL);
  186.     return (cp);
  187. }
  188.