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 #ifndef lint 8 static char sccsid[] = "@(#)res_init.c 6.1 (Berkeley) 10/31/85"; 9 #endif 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 <arpa/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 #ifndef RES_TIMEOUT 35 #define RES_TIMEOUT 10 36 #endif 37 38 struct state _res = { 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 */ 43 }; 44 45 /* 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 54 */ 55 res_init() 56 { 57 register FILE *fp; 58 char buf[BUFSIZ], *cp; 59 extern u_long inet_addr(); 60 extern char *index(); 61 extern char *strcpy(), *strncpy(); 62 #ifdef DEBUG 63 extern char *getenv(); 64 #endif DEBUG 65 int n = 0; /* number of nameserver records read from file */ 66 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'; 72 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; 100 _res.nsaddr_list[n].sin_family = AF_INET; 101 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT); 102 #ifdef DEBUG 103 if ( _res.options & RES_DEBUG ) 104 printf("Server #%d address = %s\n", n+1, 105 inet_ntoa(_res.nsaddr_list[n].sin_addr.s_addr)); 106 #endif DEBUG 107 if ( ++n >= MAXNS) { 108 n = MAXNS; 109 #ifdef DEBUG 110 if ( _res.options & RES_DEBUG ) 111 printf("MAXNS reached\n"); 112 #endif DEBUG 113 } 114 continue; 115 } 116 } 117 if ( n > 1 ) 118 _res.nscount = n; 119 (void) fclose(fp); 120 } 121 if (_res.defdname[0] == 0) { 122 if (gethostname(buf, sizeof(_res.defdname)) == 0 && 123 (cp = index(buf, '.'))) 124 (void)strcpy(_res.defdname, cp + 1); 125 } 126 127 #ifdef DEBUG 128 /* Allow user to override the local domain definition */ 129 if ((cp = getenv("LOCALDOMAIN")) != NULL) 130 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); 131 #endif DEBUG 132 _res.options |= RES_INIT; 133 return(0); 134 } 135