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.7 (Berkeley) 09/15/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 struct state _res = { 34 10, 35 4, 36 RES_RECURSE|RES_DEFNAMES, 37 }; 38 39 /* 40 * Set up default settings. If the configuration file exist, the values 41 * there will have precedence. Otherwise, the server address is set to 42 * INADDR_ANY and the default domain name comes from the gethostname(). 43 * 44 * The configuration file should only be used if you want to redefine your 45 * domain or run without a server on your machine. 46 * 47 * Return 0 if completes successfully, -1 on error 48 */ 49 res_init() 50 { 51 FILE *fp; 52 char buf[BUFSIZ], *cp; 53 extern u_long inet_addr(); 54 extern char *index(); 55 extern char *strcpy(), *strncpy(); 56 #ifdef DEBUG 57 extern char *getenv(); 58 #endif 59 60 _res.nsaddr.sin_family = AF_INET; 61 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 62 _res.defdname[0] = '\0'; 63 _res.nsaddr.sin_port = htons(NAMESERVER_PORT); 64 65 if ((fp = fopen(conffile, "r")) != NULL) { 66 while (fgets(buf, sizeof(buf), fp) != NULL) { 67 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 68 cp = buf + sizeof("domain") - 1; 69 while (*cp == ' ' || *cp == '\t') 70 cp++; 71 if (*cp == '\0') 72 continue; 73 (void)strncpy(_res.defdname, cp, 74 sizeof(_res.defdname)); 75 _res.defdname[sizeof(_res.defdname) - 1] = '\0'; 76 if ((cp = index(_res.defdname, '\n')) != NULL) 77 *cp = '\0'; 78 continue; 79 } 80 if (!strncmp(buf, "resolver", sizeof("resolver") - 1)) { 81 cp = buf + sizeof("resolver") - 1; 82 while (*cp == ' ' || *cp == '\t') 83 cp++; 84 if (*cp == '\0') 85 continue; 86 _res.nsaddr.sin_addr.s_addr = inet_addr(cp); 87 if (_res.nsaddr.sin_addr.s_addr == (unsigned)-1) 88 _res.nsaddr.sin_addr.s_addr = 89 INADDR_ANY; 90 continue; 91 } 92 } 93 (void) fclose(fp); 94 } 95 if (_res.defdname[0] == 0) { 96 if (gethostname(buf, sizeof(_res.defdname)) == -1) 97 return(-1); 98 if ((cp = index(buf, '.')) == NULL) 99 return(-1); 100 (void)strcpy(_res.defdname, cp + 1); 101 } 102 103 #ifdef DEBUG 104 /* Allow user to override the local domain definition */ 105 if ((cp = getenv("LOCALDOMAIN")) != NULL) 106 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname)); 107 #endif 108 _res.options |= RES_INIT; 109 return(0); 110 } 111