xref: /csrg-svn/lib/libc/net/res_init.c (revision 34817)
118548Sralph /*
221386Sdist  * Copyright (c) 1985 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
533679Sbostic  * Redistribution and use in source and binary forms are permitted
6*34817Sbostic  * provided that the above copyright notice and this paragraph are
7*34817Sbostic  * duplicated in all such forms and that any documentation,
8*34817Sbostic  * advertising materials, and other materials related to such
9*34817Sbostic  * distribution and use acknowledge that the software was developed
10*34817Sbostic  * by the University of California, Berkeley.  The name of the
11*34817Sbostic  * University may not be used to endorse or promote products derived
12*34817Sbostic  * from this software without specific prior written permission.
13*34817Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34817Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34817Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1618548Sralph  */
1718548Sralph 
1826633Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34817Sbostic static char sccsid[] = "@(#)res_init.c	6.9 (Berkeley) 06/27/88";
2033679Sbostic #endif /* LIBC_SCCS and not lint */
2121386Sdist 
2218142Sralph #include <sys/types.h>
2318142Sralph #include <sys/socket.h>
2418142Sralph #include <netinet/in.h>
2518142Sralph #include <stdio.h>
2624081Skjd #include <arpa/nameser.h>
2726902Skjd #include <resolv.h>
2818142Sralph 
2918142Sralph /*
3024111Skjd  * Resolver configuration file. Contains the address of the
3124111Skjd  * inital name server to query and the default domain for
3224111Skjd  * non fully qualified domain names.
3324111Skjd  */
3424111Skjd 
3532294Sbostic #ifndef	CONFFILE
3632294Sbostic #define	CONFFILE	"/etc/resolv.conf"
3724111Skjd #endif
3824111Skjd 
3924111Skjd /*
4018142Sralph  * Resolver state default settings
4118142Sralph  */
4225242Skjd 
4318142Sralph struct state _res = {
4431112Skarels     RES_TIMEOUT,               	/* retransmition time interval */
4531112Skarels     4,                         	/* number of times to retransmit */
4631112Skarels     RES_DEFAULT,		/* options flags */
4731112Skarels     1,                         	/* number of name servers */
4818142Sralph };
4918142Sralph 
5018142Sralph /*
5124736Sbloom  * Set up default settings.  If the configuration file exist, the values
5224736Sbloom  * there will have precedence.  Otherwise, the server address is set to
5324736Sbloom  * INADDR_ANY and the default domain name comes from the gethostname().
5424736Sbloom  *
5524736Sbloom  * The configuration file should only be used if you want to redefine your
5624736Sbloom  * domain or run without a server on your machine.
5724736Sbloom  *
5824736Sbloom  * Return 0 if completes successfully, -1 on error
5918142Sralph  */
6018142Sralph res_init()
6118142Sralph {
6225242Skjd     register FILE *fp;
6331112Skarels     register char *cp, **pp;
6431112Skarels     char buf[BUFSIZ];
6525242Skjd     extern u_long inet_addr();
6625242Skjd     extern char *index();
6725242Skjd     extern char *strcpy(), *strncpy();
6825242Skjd     extern char *getenv();
6925242Skjd     int n = 0;    /* number of nameserver records read from file */
7018142Sralph 
7125242Skjd     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
7225242Skjd     _res.nsaddr.sin_family = AF_INET;
7325242Skjd     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
7425242Skjd     _res.nscount = 1;
7525242Skjd     _res.defdname[0] = '\0';
7618142Sralph 
7732294Sbostic     if ((fp = fopen(CONFFILE, "r")) != NULL) {
7825242Skjd         /* read the config file */
7925242Skjd         while (fgets(buf, sizeof(buf), fp) != NULL) {
8025242Skjd             /* read default domain name */
8125242Skjd             if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
8225242Skjd                 cp = buf + sizeof("domain") - 1;
8325242Skjd                 while (*cp == ' ' || *cp == '\t')
8425242Skjd                     cp++;
8525242Skjd                 if (*cp == '\0')
8625242Skjd                     continue;
8725242Skjd                 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
8825242Skjd                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
8925242Skjd                 if ((cp = index(_res.defdname, '\n')) != NULL)
9025242Skjd                     *cp = '\0';
9125242Skjd                 continue;
9225242Skjd             }
9325242Skjd             /* read nameservers to query */
9425242Skjd             if (!strncmp(buf, "nameserver",
9525242Skjd                sizeof("nameserver") - 1) && (n < MAXNS)) {
9625242Skjd                 cp = buf + sizeof("nameserver") - 1;
9725242Skjd                 while (*cp == ' ' || *cp == '\t')
9825242Skjd                     cp++;
9925242Skjd                 if (*cp == '\0')
10025242Skjd                     continue;
10125242Skjd                 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
10225242Skjd                 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
10325242Skjd                     _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
10426902Skjd                 _res.nsaddr_list[n].sin_family = AF_INET;
10526902Skjd                 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
10626902Skjd                 if ( ++n >= MAXNS) {
10726902Skjd                     n = MAXNS;
10825242Skjd #ifdef DEBUG
10926902Skjd                     if ( _res.options & RES_DEBUG )
11026902Skjd                         printf("MAXNS reached, reading resolv.conf\n");
11125242Skjd #endif DEBUG
11225242Skjd                 }
11325242Skjd                 continue;
11425242Skjd             }
11525242Skjd         }
11625242Skjd         if ( n > 1 )
11725242Skjd             _res.nscount = n;
11825242Skjd         (void) fclose(fp);
11925242Skjd     }
12025242Skjd     if (_res.defdname[0] == 0) {
12125242Skjd         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
12225242Skjd            (cp = index(buf, '.')))
12325242Skjd              (void)strcpy(_res.defdname, cp + 1);
12425242Skjd     }
12518142Sralph 
12625242Skjd     /* Allow user to override the local domain definition */
12725242Skjd     if ((cp = getenv("LOCALDOMAIN")) != NULL)
12825242Skjd         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
12931112Skarels 
13031112Skarels     /* find components of local domain that might be searched */
13131112Skarels     pp = _res.dnsrch;
13231112Skarels     *pp++ = _res.defdname;
13331112Skarels     for (cp = _res.defdname, n = 0; *cp; cp++)
13431112Skarels 	if (*cp == '.')
13531112Skarels 	    n++;
13631112Skarels     cp = _res.defdname;
13731112Skarels     for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
13831112Skarels 	cp = index(cp, '.');
13931112Skarels 	*pp++ = ++cp;
14031112Skarels     }
14125242Skjd     _res.options |= RES_INIT;
14225242Skjd     return(0);
14318142Sralph }
144