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