home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / ISP / bind.4.8.3.lzh / BIND483 / NAMED / ns_sort.c < prev    next >
Text File  |  1993-08-24  |  4KB  |  143 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)ns_sort.c    4.8 (Berkeley) 6/1/90";
  22. #endif /* not lint */
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #include <sys/socket.h>
  28. #include <sys/file.h>
  29. #include <netinet/in.h>
  30. #include <syslog.h>
  31. #include <arpa/nameser.h>
  32. #include "ns.h"
  33. #include "db.h"
  34.  
  35. extern    char *p_type(), *p_class();
  36.  
  37. extern    int    debug;
  38. extern  FILE    *ddt;
  39.  
  40. struct netinfo* 
  41. local(from)
  42.     struct sockaddr_in *from;
  43. {
  44.     extern struct netinfo *nettab, netloop, **enettab;
  45.     struct netinfo *ntp;
  46.  
  47.     if (from->sin_addr.s_addr == netloop.my_addr.s_addr)
  48.         return( &netloop);
  49.     for (ntp = nettab; ntp != *enettab; ntp = ntp->next) {
  50.         if (ntp->net == (from->sin_addr.s_addr & ntp->mask))
  51.             return(ntp);
  52.     }
  53.     return(NULL);
  54. }
  55.  
  56.  
  57. sort_response(cp, ancount, lp, eom)
  58.     register char *cp;
  59.     register int ancount;
  60.     struct netinfo *lp;
  61.     u_char *eom;
  62. {
  63.     register struct netinfo *ntp;
  64.     extern struct netinfo *nettab;
  65.  
  66. #ifdef DEBUG
  67.     if (debug > 2)
  68.         fprintf(ddt,"sort_response(%d)\n", ancount);
  69. #endif DEBUG
  70.     if (ancount > 1) {
  71.         if (sort_rr(cp, ancount, lp, eom))
  72.             return;
  73.         for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
  74.             if ((ntp->net == lp->net) && (ntp->mask == lp->mask))
  75.                 continue;
  76.             if (sort_rr(cp, ancount, ntp, eom))
  77.                 break;
  78.         }
  79.     }
  80. }
  81.  
  82. int
  83. sort_rr(cp, count, ntp, eom)
  84.     register u_char *cp;
  85.     int count;
  86.     register struct netinfo *ntp;
  87.     u_char *eom;
  88. {
  89.     int type, class, dlen, n, c;
  90.     struct in_addr inaddr;
  91.     u_char *rr1;
  92.  
  93. #ifdef DEBUG
  94.     if (debug > 2) {
  95.         inaddr.s_addr = ntp->net;
  96.         fprintf(ddt,"sort_rr( x%x, %d, %s)\n",cp, count,
  97.         inet_ntoa(inaddr));
  98.     }
  99. #endif DEBUG
  100.     rr1 = NULL;
  101.     for (c = count; c > 0; --c) {
  102.         n = dn_skipname(cp, eom);
  103.         if (n < 0)
  104.         return (1);        /* bogus, stop processing */
  105.         cp += n;
  106.         if (cp + QFIXEDSZ > eom)
  107.         return (1);
  108.         GETSHORT(type, cp);
  109.         GETSHORT(class, cp);
  110.         cp += sizeof(u_long);
  111.         GETSHORT(dlen, cp);
  112.         if (dlen > eom - cp)
  113.         return (1);        /* bogus, stop processing */
  114.         switch (type) {
  115.         case T_A:
  116.             switch (class) {
  117.             case C_IN:
  118.             case C_HS:
  119.                 bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  120.             if (rr1 == NULL)
  121.                 rr1 = cp;
  122.             if ((ntp->mask & inaddr.s_addr) == ntp->net) {
  123. #ifdef DEBUG
  124.                 if (debug > 1) {
  125.                     fprintf(ddt,"net %s best choice\n",
  126.                     inet_ntoa(inaddr));
  127.                 }
  128. #endif DEBUG
  129.                 if (rr1 != cp) {
  130.                         bcopy(rr1, cp, sizeof(inaddr));
  131.                         bcopy((char *)&inaddr, rr1, sizeof(inaddr));
  132.                 }
  133.                 return(1);
  134.             }
  135.                 break;
  136.             }
  137.             break;
  138.         }
  139.         cp += dlen;
  140.     }
  141.     return(0);
  142. }
  143.