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