home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / named / ns_sort.c < prev    next >
C/C++ Source or Header  |  1994-07-23  |  5KB  |  170 lines

  1. #if !defined(lint) && !defined(SABER)
  2. static char sccsid[] = "@(#)ns_sort.c    4.10 (Berkeley) 3/3/91";
  3. static char rcsid[] = "$Id: ns_sort.c,v 4.9.1.6 1994/07/23 23:23:56 vixie Exp $";
  4. #endif /* not lint */
  5.  
  6. /*
  7.  * ++Copyright++ 1986, 1990
  8.  * -
  9.  * Copyright (c) 1986, 1990
  10.  *    The Regents of the University of California.  All rights reserved.
  11.  * 
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *     This product includes software developed by the University of
  23.  *     California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  * -
  40.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41.  * 
  42.  * Permission to use, copy, modify, and distribute this software for any
  43.  * purpose with or without fee is hereby granted, provided that the above
  44.  * copyright notice and this permission notice appear in all copies, and that
  45.  * the name of Digital Equipment Corporation not be used in advertising or
  46.  * publicity pertaining to distribution of the document or software without
  47.  * specific, written prior permission.
  48.  * 
  49.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  52.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56.  * SOFTWARE.
  57.  * -
  58.  * --Copyright--
  59.  */
  60.  
  61. #include <sys/param.h>
  62. #include <sys/types.h>
  63. #include <sys/socket.h>
  64. #include <sys/file.h>
  65. #include <netinet/in.h>
  66. #include <arpa/nameser.h>
  67. #include <arpa/inet.h>
  68. #include <stdio.h>
  69. #include <syslog.h>
  70. #include <resolv.h>
  71.  
  72. #include "named.h"
  73.  
  74. static int sort_rr __P((u_char *cp, int count, struct netinfo *ntp, u_char *eom));
  75.  
  76. struct netinfo *
  77. local(from)
  78.     struct sockaddr_in *from;
  79. {
  80.     struct netinfo *ntp;
  81.  
  82.     if (from->sin_addr.s_addr == netloop.my_addr.s_addr)
  83.         return (&netloop);
  84.     for (ntp = nettab; ntp != *enettab; ntp = ntp->next) {
  85.         if (ntp->addr == (from->sin_addr.s_addr & ntp->mask))
  86.             return (ntp);
  87.     }
  88.     return (NULL);
  89. }
  90.  
  91. void
  92. sort_response(cp, ancount, lp, eom)
  93.     register u_char *cp;
  94.     register int ancount;
  95.     struct netinfo *lp;
  96.     u_char *eom;
  97. {
  98.     register struct netinfo *ntp;
  99.  
  100.     dprintf(3, (ddt, "sort_response(%d)\n", ancount));
  101.     if (ancount > 1) {
  102.         if (sort_rr(cp, ancount, lp, eom))
  103.             return;
  104.         for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
  105.             if ((ntp->addr == lp->addr) && (ntp->mask == lp->mask))
  106.                 continue;
  107.             if (sort_rr(cp, ancount, ntp, eom))
  108.                 break;
  109.         }
  110.     }
  111. }
  112.  
  113. static int
  114. sort_rr(cp, count, ntp, eom)
  115.     register u_char *cp;
  116.     int count;
  117.     register struct netinfo *ntp;
  118.     u_char *eom;
  119. {
  120.     int type, class, dlen, n, c;
  121.     struct in_addr inaddr;
  122.     u_char *rr1;
  123.  
  124. #ifdef DEBUG
  125.     if (debug > 2) {
  126.         inaddr.s_addr = ntp->addr;
  127.         fprintf(ddt, "sort_rr(x%x, %d, [%s])\n",
  128.             cp, count, inet_ntoa(inaddr));
  129.     }
  130. #endif
  131.     rr1 = NULL;
  132.     for (c = count; c > 0; --c) {
  133.         n = dn_skipname(cp, eom);
  134.         if (n < 0)
  135.         return (1);        /* bogus, stop processing */
  136.         cp += n;
  137.         if (cp + QFIXEDSZ > eom)
  138.         return (1);
  139.         GETSHORT(type, cp);
  140.         GETSHORT(class, cp);
  141.         cp += INT32SZ;
  142.         GETSHORT(dlen, cp);
  143.         if (dlen > eom - cp)
  144.         return (1);        /* bogus, stop processing */
  145.         switch (type) {
  146.         case T_A:
  147.             switch (class) {
  148.             case C_IN:
  149.             case C_HS:
  150.                 bcopy(cp, (char *)&inaddr, INADDRSZ);
  151.             if (rr1 == NULL)
  152.                 rr1 = cp;
  153.             if ((ntp->mask & inaddr.s_addr) == ntp->addr) {
  154.                 dprintf(2, (ddt,"net [%s] best choice\n",
  155.                     inet_ntoa(inaddr)));
  156.                 if (rr1 != cp) {
  157.                         bcopy(rr1, cp, INADDRSZ);
  158.                         bcopy((char *)&inaddr, rr1, INADDRSZ);
  159.                 }
  160.                 return (1);
  161.             }
  162.                 break;
  163.             }
  164.             break;
  165.         }
  166.         cp += dlen;
  167.     }
  168.     return (0);
  169. }
  170.