# 2.11BSD#431 - /usr/src/lib/libc/net/res_init.c

##### Functions

    res_init          X

##### Variables

    _res              X

##### Macros

    CONFFILE          X

       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

res_init defined in line 55; used 7 times

- in /usr/src/lib/libc/net/res_mkquery.c line 75
- in /usr/src/lib/libc/net/res_query.c line 57, 131
- in /usr/src/lib/libc/net/res_send.c line 74
- in /usr/src/usr.sbin/named/tools/nslookup/main.c line 152
- in /usr/src/usr.sbin/named/tools/nslookup/send.c line 127
- in /usr/src/usr.sbin/named/tools/nstest.c line 55

#### Defined variables

_res defined in line 38; used 251 times

- in line 66-70(5), 82-84(5), 96-104(6), 112-136(12)
- in /usr/src/lib/libc/net/named/gethnamadr.c line 151, 181, 233, 265
- in /usr/src/lib/libc/net/named/sethostent.c line 19-24(2)
- in /usr/src/lib/libc/net/res_init.c line 66-70(5), 82-84(5), 96-104(6), 112-136(12)
- in /usr/src/lib/libc/net/res_mkquery.c line 45, 52-56(3), 72-78(4)
- in /usr/src/lib/libc/net/res_query.c line 57-60(2), 68, 77, 87, 131, 142-143(2), 166, 194
- in /usr/src/lib/libc/net/res_send.c line 68-87(7), 100-109(3), 128, 148, 158, 173, 203(2), 210-213(2), 222, 229-232(2), 241-242(3), 253, 263, 270, 280, 291, 298-303(2), 312, 318, 328
- in /usr/src/libexec/popper/pop_init.c line 125-130(2)
- in /usr/src/libexec/rlogind/rlogind.c line 177
- in /usr/src/libexec/rshd/rshd.c line 240
- in /usr/src/usr.sbin/named/named/ns_main.c line 167
- in /usr/src/usr.sbin/named/tools/nslookup/debug.c line 88, 240-242(2), 436
- in /usr/src/usr.sbin/named/tools/nslookup/getinfo.c line 142, 269, 557-559(3), 579, 633, 689
- in /usr/src/usr.sbin/named/tools/nslookup/list.c line 228, 587, 599
- in /usr/src/usr.sbin/named/tools/nslookup/main.c line 211-212(2), 222-230(4), 237-254(7), 265, 276-279(3), 824-845(9), 853-855(2), 871-873(2), 879, 888-890(2), 901-907(3), 925-936(6), 947-948(3), 954-957(3), 995-1012(11)
- in /usr/src/usr.sbin/named/tools/nslookup/send.c line 112, 121-142(7), 153, 159, 167-172(2), 191, 211, 234, 249, 281, 296, 303, 310, 333, 340-343(2), 352, 369-372(2), 385-388(2), 400, 410, 421, 432, 438, 446, 452, 462-463(2), 476, 490-491(2)
- in /usr/src/usr.sbin/named/tools/nsquery.c line 40-44(2), 61
- in /usr/src/usr.sbin/named/tools/nstest.c line 54, 68-76(3), 87-92(5)
- in /usr/src/usr.sbin/sendmail.MX/src/deliver.c line 86-87(2), 379, 464
- in /usr/src/usr.sbin/sendmail.MX/src/domain.c line 91
- in /usr/src/usr.sbin/sendmail.MX/src/main.c line 329

sccsid defined in line 14; never used

#### Defined macros

CONFFILE defined in line 31; used 2 times

- in line 30, 72

Last modified: 1988-09-17 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 755 |