xref: /csrg-svn/lib/libc/net/res_init.c (revision 32294)
118548Sralph /*
221386Sdist  * Copyright (c) 1985 Regents of the University of California.
321386Sdist  * All rights reserved.  The Berkeley software License Agreement
421386Sdist  * specifies the terms and conditions for redistribution.
518548Sralph  */
618548Sralph 
726633Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*32294Sbostic static char sccsid[] = "@(#)res_init.c	6.7 (Berkeley) 09/30/87";
926633Sdonn #endif LIBC_SCCS and not lint
1021386Sdist 
1118142Sralph #include <sys/types.h>
1218142Sralph #include <sys/socket.h>
1318142Sralph #include <netinet/in.h>
1418142Sralph #include <stdio.h>
1524081Skjd #include <arpa/nameser.h>
1626902Skjd #include <resolv.h>
1718142Sralph 
1818142Sralph /*
1924111Skjd  * Resolver configuration file. Contains the address of the
2024111Skjd  * inital name server to query and the default domain for
2124111Skjd  * non fully qualified domain names.
2224111Skjd  */
2324111Skjd 
24*32294Sbostic #ifndef	CONFFILE
25*32294Sbostic #define	CONFFILE	"/etc/resolv.conf"
2624111Skjd #endif
2724111Skjd 
2824111Skjd /*
2918142Sralph  * Resolver state default settings
3018142Sralph  */
3125242Skjd 
3218142Sralph struct state _res = {
3331112Skarels     RES_TIMEOUT,               	/* retransmition time interval */
3431112Skarels     4,                         	/* number of times to retransmit */
3531112Skarels     RES_DEFAULT,		/* options flags */
3631112Skarels     1,                         	/* number of name servers */
3718142Sralph };
3818142Sralph 
3918142Sralph /*
4024736Sbloom  * Set up default settings.  If the configuration file exist, the values
4124736Sbloom  * there will have precedence.  Otherwise, the server address is set to
4224736Sbloom  * INADDR_ANY and the default domain name comes from the gethostname().
4324736Sbloom  *
4424736Sbloom  * The configuration file should only be used if you want to redefine your
4524736Sbloom  * domain or run without a server on your machine.
4624736Sbloom  *
4724736Sbloom  * Return 0 if completes successfully, -1 on error
4818142Sralph  */
4918142Sralph res_init()
5018142Sralph {
5125242Skjd     register FILE *fp;
5231112Skarels     register char *cp, **pp;
5331112Skarels     char buf[BUFSIZ];
5425242Skjd     extern u_long inet_addr();
5525242Skjd     extern char *index();
5625242Skjd     extern char *strcpy(), *strncpy();
5725242Skjd     extern char *getenv();
5825242Skjd     int n = 0;    /* number of nameserver records read from file */
5918142Sralph 
6025242Skjd     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
6125242Skjd     _res.nsaddr.sin_family = AF_INET;
6225242Skjd     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
6325242Skjd     _res.nscount = 1;
6425242Skjd     _res.defdname[0] = '\0';
6518142Sralph 
66*32294Sbostic     if ((fp = fopen(CONFFILE, "r")) != NULL) {
6725242Skjd         /* read the config file */
6825242Skjd         while (fgets(buf, sizeof(buf), fp) != NULL) {
6925242Skjd             /* read default domain name */
7025242Skjd             if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
7125242Skjd                 cp = buf + sizeof("domain") - 1;
7225242Skjd                 while (*cp == ' ' || *cp == '\t')
7325242Skjd                     cp++;
7425242Skjd                 if (*cp == '\0')
7525242Skjd                     continue;
7625242Skjd                 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
7725242Skjd                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
7825242Skjd                 if ((cp = index(_res.defdname, '\n')) != NULL)
7925242Skjd                     *cp = '\0';
8025242Skjd                 continue;
8125242Skjd             }
8225242Skjd             /* read nameservers to query */
8325242Skjd             if (!strncmp(buf, "nameserver",
8425242Skjd                sizeof("nameserver") - 1) && (n < MAXNS)) {
8525242Skjd                 cp = buf + sizeof("nameserver") - 1;
8625242Skjd                 while (*cp == ' ' || *cp == '\t')
8725242Skjd                     cp++;
8825242Skjd                 if (*cp == '\0')
8925242Skjd                     continue;
9025242Skjd                 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
9125242Skjd                 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
9225242Skjd                     _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
9326902Skjd                 _res.nsaddr_list[n].sin_family = AF_INET;
9426902Skjd                 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
9526902Skjd                 if ( ++n >= MAXNS) {
9626902Skjd                     n = MAXNS;
9725242Skjd #ifdef DEBUG
9826902Skjd                     if ( _res.options & RES_DEBUG )
9926902Skjd                         printf("MAXNS reached, reading resolv.conf\n");
10025242Skjd #endif DEBUG
10125242Skjd                 }
10225242Skjd                 continue;
10325242Skjd             }
10425242Skjd         }
10525242Skjd         if ( n > 1 )
10625242Skjd             _res.nscount = n;
10725242Skjd         (void) fclose(fp);
10825242Skjd     }
10925242Skjd     if (_res.defdname[0] == 0) {
11025242Skjd         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
11125242Skjd            (cp = index(buf, '.')))
11225242Skjd              (void)strcpy(_res.defdname, cp + 1);
11325242Skjd     }
11418142Sralph 
11525242Skjd     /* Allow user to override the local domain definition */
11625242Skjd     if ((cp = getenv("LOCALDOMAIN")) != NULL)
11725242Skjd         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
11831112Skarels 
11931112Skarels     /* find components of local domain that might be searched */
12031112Skarels     pp = _res.dnsrch;
12131112Skarels     *pp++ = _res.defdname;
12231112Skarels     for (cp = _res.defdname, n = 0; *cp; cp++)
12331112Skarels 	if (*cp == '.')
12431112Skarels 	    n++;
12531112Skarels     cp = _res.defdname;
12631112Skarels     for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
12731112Skarels 	cp = index(cp, '.');
12831112Skarels 	*pp++ = ++cp;
12931112Skarels     }
13025242Skjd     _res.options |= RES_INIT;
13125242Skjd     return(0);
13218142Sralph }
133