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 5.6 (Berkeley) 09/14/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 <netdb.h> 16 #include <arpa/nameser.h> 17 #include <arpa/resolv.h> 18 19 /* 20 * Resolver configuration file. Contains the address of the 21 * inital name server to query and the default domain for 22 * non fully qualified domain names. 23 */ 24 25 #ifdef CONFFILE 26 char *conffile = CONFFILE; 27 #else 28 char *conffile = "/etc/resolv.conf"; 29 #endif 30 31 /* 32 * Resolver state default settings 33 */ 34 struct state _res = { 35 10, 36 4, 37 RES_RECURSE|RES_DEFNAMES, 38 }; 39 40 /* 41 * Set up default settings. If the configuration file exist, the values 42 * there will have precedence. Otherwise, the server address is set to 43 * INADDR_ANY and the default domain name comes from the gethostname(). 44 * 45 * The configuration file should only be used if you want to redefine your 46 * domain or run without a server on your machine. 47 * 48 * Return 0 if completes successfully, -1 on error 49 */ 50 res_init() 51 { 52 FILE *fp; 53 char buf[BUFSIZ], *cp; 54 extern u_long inet_addr(); 55 extern char *index(); 56 extern char *strcpy(), *strncpy(); 57 struct servent *serv; 58 #ifdef DEBUG 59 extern char *getenv(); 60 #endif 61 62 _res.nsaddr.sin_family = AF_INET; 63 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 64 _res.defdname[0] = '\0'; 65 if ((serv = getservbyname(NAMESERVER_SNAME, "tcp")) == NULL) 66 return(-1); 67 _res.nsaddr.sin_port = (u_short)serv->s_port; 68 69 if ((fp = fopen(conffile, "r")) != NULL) { 70 while (fgets(buf, sizeof(buf), fp) != NULL) { 71 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 72 cp = buf + sizeof("domain") - 1; 73 while (*cp == ' ' || *cp == '\t') 74 cp++; 75 if (*cp == '\0') 76 continue; 77 (void)strncpy(_res.defdname, cp, 78 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 if (!strncmp(buf, "resolver", sizeof("resolver") - 1)) { 85 cp = buf + sizeof("resolver") - 1; 86 while (*cp == ' ' || *cp == '\t') 87 cp++; 88 if (*cp == '\0') 89 continue; 90 _res.nsaddr.sin_addr.s_addr = inet_addr(cp); 91 if (_res.nsaddr.sin_addr.s_addr == (unsigned)-1) 92 _res.nsaddr.sin_addr.s_addr = 93 INADDR_ANY; 94 continue; 95 } 96 } 97 (void) fclose(fp); 98 } 99 if (_res.defdname[0] == 0) { 100 if (gethostname(buf, sizeof(_res.defdname)) == -1) 101 return(-1); 102 if ((cp = index(buf, '.')) == NULL) 103 return(-1); 104 (void)strcpy(_res.defdname, cp + 1); 105 } 106 107 #ifdef DEBUG 108 /* Allow user to override the local domain definition */ 109 if ((cp = getenv("LOCALDOMAIN")) != NULL) 110 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); 111 #endif 112 _res.options |= RES_INIT; 113 return(0); 114 } 115