separate reading of file into its own routine, lpd needed it
[unix-history] / usr / src / lib / libc / net / res_init.c
CommitLineData
b423e985 1/*
8ea4199d
DF
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
b423e985
RC
5 */
6
2ce81398 7#if defined(LIBC_SCCS) && !defined(lint)
c8337dc9 8static char sccsid[] = "@(#)res_init.c 6.5 (Berkeley) %G%";
2ce81398 9#endif LIBC_SCCS and not lint
8ea4199d 10
b53e7108
RC
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <stdio.h>
56d853b7 15#include <arpa/nameser.h>
a36af7fe 16#include <resolv.h>
b53e7108 17
7dfc5eb4
KD
18/*
19 * Resolver configuration file. Contains the address of the
20 * inital name server to query and the default domain for
21 * non fully qualified domain names.
22 */
23
24#ifdef CONFFILE
dc795f66 25char *conffile = CONFFILE;
7dfc5eb4 26#else
dc795f66 27char *conffile = "/etc/resolv.conf";
7dfc5eb4
KD
28#endif
29
b53e7108
RC
30/*
31 * Resolver state default settings
32 */
dc795f66
KD
33
34#ifndef RES_TIMEOUT
c8337dc9 35#define RES_TIMEOUT 4
dc795f66
KD
36#endif
37
b53e7108 38struct state _res = {
dc795f66
KD
39 RES_TIMEOUT, /* retransmition time interval */
40 4, /* number of times to retransmit */
41 RES_RECURSE|RES_DEFNAMES, /* options flags */
42 1, /* number of name servers */
b53e7108
RC
43};
44
45/*
98959623
JB
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
b53e7108
RC
54 */
55res_init()
56{
dc795f66
KD
57 register FILE *fp;
58 char buf[BUFSIZ], *cp;
59 extern u_long inet_addr();
60 extern char *index();
61 extern char *strcpy(), *strncpy();
98959623 62#ifdef DEBUG
dc795f66
KD
63 extern char *getenv();
64#endif DEBUG
65 int n = 0; /* number of nameserver records read from file */
b53e7108 66
dc795f66
KD
67 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
68 _res.nsaddr.sin_family = AF_INET;
69 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
70 _res.nscount = 1;
71 _res.defdname[0] = '\0';
b53e7108 72
dc795f66
KD
73 if ((fp = fopen(conffile, "r")) != NULL) {
74 /* read the config file */
75 while (fgets(buf, sizeof(buf), fp) != NULL) {
76 /* read default domain name */
77 if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
78 cp = buf + sizeof("domain") - 1;
79 while (*cp == ' ' || *cp == '\t')
80 cp++;
81 if (*cp == '\0')
82 continue;
83 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
84 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
85 if ((cp = index(_res.defdname, '\n')) != NULL)
86 *cp = '\0';
87 continue;
88 }
89 /* read nameservers to query */
90 if (!strncmp(buf, "nameserver",
91 sizeof("nameserver") - 1) && (n < MAXNS)) {
92 cp = buf + sizeof("nameserver") - 1;
93 while (*cp == ' ' || *cp == '\t')
94 cp++;
95 if (*cp == '\0')
96 continue;
97 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
98 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1)
99 _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
a36af7fe
KD
100 _res.nsaddr_list[n].sin_family = AF_INET;
101 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
102 if ( ++n >= MAXNS) {
103 n = MAXNS;
dc795f66 104#ifdef DEBUG
a36af7fe
KD
105 if ( _res.options & RES_DEBUG )
106 printf("MAXNS reached, reading resolv.conf\n");
dc795f66
KD
107#endif DEBUG
108 }
109 continue;
110 }
111 }
112 if ( n > 1 )
113 _res.nscount = n;
114 (void) fclose(fp);
115 }
116 if (_res.defdname[0] == 0) {
117 if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
118 (cp = index(buf, '.')))
119 (void)strcpy(_res.defdname, cp + 1);
120 }
b53e7108 121
98959623 122#ifdef DEBUG
dc795f66
KD
123 /* Allow user to override the local domain definition */
124 if ((cp = getenv("LOCALDOMAIN")) != NULL)
125 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
126#endif DEBUG
127 _res.options |= RES_INIT;
128 return(0);
b53e7108 129}