1 /* 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. 5 */ 6 7 #if defined(LIBC_SCCS) && !defined(lint) 8 static char sccsid[] = "@(#)res_init.c 6.6 (Berkeley) 05/12/87"; 9 #endif LIBC_SCCS and not lint 10 11 #include <sys/types.h> 12 #include <sys/socket.h> 13 #include <netinet/in.h> 14 #include <stdio.h> 15 #include <arpa/nameser.h> 16 #include <resolv.h> 17 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 25 char *conffile = CONFFILE; 26 #else 27 char *conffile = "/etc/resolv.conf"; 28 #endif 29 30 /* 31 * Resolver state default settings 32 */ 33 34 struct state _res = { 35 RES_TIMEOUT, /* retransmition time interval */ 36 4, /* number of times to retransmit */ 37 RES_DEFAULT, /* options flags */ 38 1, /* number of name servers */ 39 }; 40 41 /* 42 * Set up default settings. If the configuration file exist, the values 43 * there will have precedence. Otherwise, the server address is set to 44 * INADDR_ANY and the default domain name comes from the gethostname(). 45 * 46 * The configuration file should only be used if you want to redefine your 47 * domain or run without a server on your machine. 48 * 49 * Return 0 if completes successfully, -1 on error 50 */ 51 res_init() 52 { 53 register FILE *fp; 54 register char *cp, **pp; 55 char buf[BUFSIZ]; 56 extern u_long inet_addr(); 57 extern char *index(); 58 extern char *strcpy(), *strncpy(); 59 extern char *getenv(); 60 int n = 0; /* number of nameserver records read from file */ 61 62 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 63 _res.nsaddr.sin_family = AF_INET; 64 _res.nsaddr.sin_port = htons(NAMESERVER_PORT); 65 _res.nscount = 1; 66 _res.defdname[0] = '\0'; 67 68 if ((fp = fopen(conffile, "r")) != NULL) { 69 /* read the config file */ 70 while (fgets(buf, sizeof(buf), fp) != NULL) { 71 /* read default domain name */ 72 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 73 cp = buf + sizeof("domain") - 1; 74 while (*cp == ' ' || *cp == '\t') 75 cp++; 76 if (*cp == '\0') 77 continue; 78 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); 79 _res.defdname[sizeof(_res.defdname) - 1] = '\0'; 80 if ((cp = index(_res.defdname, '\n')) != NULL) 81 *cp = '\0'; 82 continue; 83 } 84 /* read nameservers to query */ 85 if (!strncmp(buf, "nameserver", 86 sizeof("nameserver") - 1) && (n < MAXNS)) { 87 cp = buf + sizeof("nameserver") - 1; 88 while (*cp == ' ' || *cp == '\t') 89 cp++; 90 if (*cp == '\0') 91 continue; 92 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp); 93 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1) 94 _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY; 95 _res.nsaddr_list[n].sin_family = AF_INET; 96 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT); 97 if ( ++n >= MAXNS) { 98 n = MAXNS; 99 #ifdef DEBUG 100 if ( _res.options & RES_DEBUG ) 101 printf("MAXNS reached, reading resolv.conf\n"); 102 #endif DEBUG 103 } 104 continue; 105 } 106 } 107 if ( n > 1 ) 108 _res.nscount = n; 109 (void) fclose(fp); 110 } 111 if (_res.defdname[0] == 0) { 112 if (gethostname(buf, sizeof(_res.defdname)) == 0 && 113 (cp = index(buf, '.'))) 114 (void)strcpy(_res.defdname, cp + 1); 115 } 116 117 /* Allow user to override the local domain definition */ 118 if ((cp = getenv("LOCALDOMAIN")) != NULL) 119 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); 120 121 /* find components of local domain that might be searched */ 122 pp = _res.dnsrch; 123 *pp++ = _res.defdname; 124 for (cp = _res.defdname, n = 0; *cp; cp++) 125 if (*cp == '.') 126 n++; 127 cp = _res.defdname; 128 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) { 129 cp = index(cp, '.'); 130 *pp++ = ++cp; 131 } 132 _res.options |= RES_INIT; 133 return(0); 134 } 135