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 / RES / res_init.c < prev    next >
Text File  |  1994-09-23  |  6KB  |  188 lines

  1. /*-
  2.  * Copyright (c) 1985, 1989 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. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)res_init.c    6.14 (Berkeley) 6/27/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #ifdef OSK
  25. #include <types.h>
  26. #include <socket.h>
  27. #include <in.h>
  28. #include <stdio.h>
  29. #include <nameser.h>
  30. #include <resolv.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <stdio.h>
  36. #include <arpa/nameser.h>
  37. #include <resolv.h>
  38. #endif
  39.  
  40. /*
  41.  * Resolver state default settings
  42.  */
  43.  
  44. struct state _res = {
  45.     RES_TIMEOUT,                   /* retransmition time interval */
  46.     4,                             /* number of times to retransmit */
  47.     RES_DEFAULT,            /* options flags */
  48.     1,                             /* number of name servers */
  49. };
  50.  
  51. /*
  52.  * Set up default settings.  If the configuration file exist, the values
  53.  * there will have precedence.  Otherwise, the server address is set to
  54.  * INADDR_ANY and the default domain name comes from the gethostname().
  55.  *
  56.  * The configuration file should only be used if you want to redefine your
  57.  * domain or run without a server on your machine.
  58.  *
  59.  * Return 0 if completes successfully, -1 on error
  60.  */
  61. res_init()
  62. {
  63.     register FILE *fp;
  64.     register char *cp, **pp;
  65.     register int n;
  66.     char buf[BUFSIZ];
  67.     extern u_long inet_addr();
  68.     extern char *index();
  69.     extern char *strcpy(), *strncpy();
  70.     extern char *getenv();
  71.     int nserv = 0;    /* number of nameserver records read from file */
  72.     int haveenv = 0;
  73.     int havesearch = 0;
  74.  
  75.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  76.     _res.nsaddr.sin_family = AF_INET;
  77.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  78.     _res.nscount = 1;
  79.  
  80.     /* Allow user to override the local domain definition */
  81.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  82.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  83.         haveenv++;
  84.     }
  85.  
  86.     if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  87.         /* read the config file */
  88.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  89.         /* read default domain name */
  90.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  91.             if (haveenv)    /* skip if have from environ */
  92.                 continue;
  93.             cp = buf + sizeof("domain") - 1;
  94.             while (*cp == ' ' || *cp == '\t')
  95.                 cp++;
  96.             if ((*cp == '\0') || (*cp == '\n'))
  97.                 continue;
  98.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  99.             if ((cp = index(_res.defdname, '\n')) != NULL)
  100.                 *cp = '\0';
  101.             havesearch = 0;
  102.             continue;
  103.         }
  104.         /* set search list */
  105.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  106.             if (haveenv)    /* skip if have from environ */
  107.                 continue;
  108.             cp = buf + sizeof("search") - 1;
  109.             while (*cp == ' ' || *cp == '\t')
  110.                 cp++;
  111.             if ((*cp == '\0') || (*cp == '\n'))
  112.                 continue;
  113.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  114.             if ((cp = index(_res.defdname, '\n')) != NULL)
  115.                 *cp = '\0';
  116.             /*
  117.              * Set search list to be blank-separated strings
  118.              * on rest of line.
  119.              */
  120.             cp = _res.defdname;
  121.             pp = _res.dnsrch;
  122.             *pp++ = cp;
  123.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  124.                 if (*cp == ' ' || *cp == '\t') {
  125.                     *cp = 0;
  126.                     n = 1;
  127.                 } else if (n) {
  128.                     *pp++ = cp;
  129.                     n = 0;
  130.                 }
  131.             }
  132.             /* null terminate last domain if there are excess */
  133.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  134.                 cp++;
  135.             *cp = '\0';
  136.             *pp++ = 0;
  137.             havesearch = 1;
  138.             continue;
  139.         }
  140.         /* read nameservers to query */
  141.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  142.            nserv < MAXNS) {
  143.             cp = buf + sizeof("nameserver") - 1;
  144.             while (*cp == ' ' || *cp == '\t')
  145.                 cp++;
  146.             if ((*cp == '\0') || (*cp == '\n'))
  147.                 continue;
  148.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  149.             inet_addr(cp)) == (unsigned)-1) {
  150.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  151.                 = INADDR_ANY;
  152.                 continue;
  153.             }
  154.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  155.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  156.             nserv++;
  157.             continue;
  158.         }
  159.         }
  160.         if (nserv > 1) 
  161.         _res.nscount = nserv;
  162.         (void) fclose(fp);
  163.     }
  164.     if (_res.defdname[0] == 0) {
  165.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  166.            (cp = index(buf, '.')))
  167.             (void)strcpy(_res.defdname, cp + 1);
  168.     }
  169.  
  170.     /* find components of local domain that might be searched */
  171.     if (havesearch == 0) {
  172.         pp = _res.dnsrch;
  173.         *pp++ = _res.defdname;
  174.         for (cp = _res.defdname, n = 0; *cp; cp++)
  175.             if (*cp == '.')
  176.                 n++;
  177.         cp = _res.defdname;
  178.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  179.             n--) {
  180.             cp = index(cp, '.');
  181.             *pp++ = ++cp;
  182.         }
  183.         *pp++ = 0;
  184.     }
  185.     _res.options |= RES_INIT;
  186.     return (0);
  187. }
  188.