Annotation of 43BSDReno/lib/libc/net/res_init.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.