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 are permitted
   6:  * provided that this notice is preserved and that due credit is given
   7:  * to the University of California at Berkeley. The name of the University
   8:  * may not be used to endorse or promote products derived from this
   9:  * software without specific prior written permission. This software
  10:  * is provided ``as is'' without express or implied warranty.
  11:  */
  12: 
  13: #if defined(LIBC_SCCS) && !defined(lint)
  14: static char sccsid[] = "@(#)res_init.c	6.8 (Berkeley) 3/7/88";
  15: #endif /* LIBC_SCCS and not lint */
  16: 
  17: #include <sys/types.h>
  18: #include <sys/socket.h>
  19: #include <netinet/in.h>
  20: #include <stdio.h>
  21: #include <arpa/nameser.h>
  22: #include <resolv.h>
  23: 
  24: /*
  25:  * Resolver configuration file. Contains the address of the
  26:  * inital name server to query and the default domain for
  27:  * non fully qualified domain names.
  28:  */
  29: 
  30: #ifndef CONFFILE
  31: #define CONFFILE    "/etc/resolv.conf"
  32: #endif
  33: 
  34: /*
  35:  * Resolver state default settings
  36:  */
  37: 
  38: struct state _res = {
  39:     RES_TIMEOUT,                /* retransmition time interval */
  40:     4,                          /* number of times to retransmit */
  41:     RES_DEFAULT,        /* options flags */
  42:     1,                          /* number of name servers */
  43: };
  44: 
  45: /*
  46:  * Set up default settings.  If the configuration file exist, the values
  47:  * there will have precedence.  Otherwise, the server address is set to
  48:  * INADDR_ANY and the default domain name comes from the gethostname().
  49:  *
  50:  * The configuration file should only be used if you want to redefine your
  51:  * domain or run without a server on your machine.
  52:  *
  53:  * Return 0 if completes successfully, -1 on error
  54:  */
  55: res_init()
  56: {
  57:     register FILE *fp;
  58:     register char *cp, **pp;
  59:     char buf[BUFSIZ];
  60:     extern u_long inet_addr();
  61:     extern char *index();
  62:     extern char *strcpy(), *strncpy();
  63:     extern char *getenv();
  64:     int n = 0;    /* number of nameserver records read from file */
  65: 
  66:     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  67:     _res.nsaddr.sin_family = AF_INET;
  68:     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  69:     _res.nscount = 1;
  70:     _res.defdname[0] = '\0';
  71: 
  72:     if ((fp = fopen(CONFFILE, "r")) != NULL) {
  73:         /* read the config file */
  74:         while (fgets(buf, sizeof(buf), fp) != NULL) {
  75:             /* read default domain name */
  76:             if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  77:                 cp = buf + sizeof("domain") - 1;
  78:                 while (*cp == ' ' || *cp == '\t')
  79:                     cp++;
  80:                 if (*cp == '\0')
  81:                     continue;
  82:                 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  83:                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
  84:                 if ((cp = index(_res.defdname, '\n')) != NULL)
  85:                     *cp = '\0';
  86:                 continue;
  87:             }
  88:             /* read nameservers to query */
  89:             if (!strncmp(buf, "nameserver",
  90:                sizeof("nameserver") - 1) && (n < MAXNS)) {
  91:                 cp = buf + sizeof("nameserver") - 1;
  92:                 while (*cp == ' ' || *cp == '\t')
  93:                     cp++;
  94:                 if (*cp == '\0')
  95:                     continue;
  96:                 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
  97:                 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
  98:                     _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
  99:                 _res.nsaddr_list[n].sin_family = AF_INET;
 100:                 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
 101:                 if ( ++n >= MAXNS) {
 102:                     n = MAXNS;
 103: #ifdef DEBUG
 104:                     if ( _res.options & RES_DEBUG )
 105:                         printf("MAXNS reached, reading resolv.conf\n");
 106: #endif DEBUG
 107:                 }
 108:                 continue;
 109:             }
 110:         }
 111:         if ( n > 1 )
 112:             _res.nscount = n;
 113:         (void) fclose(fp);
 114:     }
 115:     if (_res.defdname[0] == 0) {
 116:         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
 117:            (cp = index(buf, '.')))
 118:              (void)strcpy(_res.defdname, cp + 1);
 119:     }
 120: 
 121:     /* Allow user to override the local domain definition */
 122:     if ((cp = getenv("LOCALDOMAIN")) != NULL)
 123:         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
 124: 
 125:     /* find components of local domain that might be searched */
 126:     pp = _res.dnsrch;
 127:     *pp++ = _res.defdname;
 128:     for (cp = _res.defdname, n = 0; *cp; cp++)
 129:     if (*cp == '.')
 130:         n++;
 131:     cp = _res.defdname;
 132:     for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
 133:     cp = index(cp, '.');
 134:     *pp++ = ++cp;
 135:     }
 136:     _res.options |= RES_INIT;
 137:     return(0);
 138: }

Defined functions

Defined variables

_res defined in line 38; used 251 times
sccsid defined in line 14; never used

Defined macros

CONFFILE defined in line 31; used 2 times
Last modified: 1988-09-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3009
Valid CSS Valid XHTML 1.0 Strict