| 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.2 (Berkeley) %G%"; |
| 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 <nameser.h> |
| 16 | #include <resolv.h> |
| 17 | |
| 18 | /* |
| 19 | * Resolver state default settings |
| 20 | */ |
| 21 | struct state _res = { |
| 22 | 90, |
| 23 | 2, |
| 24 | RES_RECURSE|RES_DEFNAMES, |
| 25 | }; |
| 26 | |
| 27 | /* |
| 28 | * Read the configuration file for default settings. |
| 29 | * Return true if the name server address is initialized. |
| 30 | */ |
| 31 | res_init() |
| 32 | { |
| 33 | FILE *fp; |
| 34 | char buf[BUFSIZ], *cp; |
| 35 | int n; |
| 36 | extern u_long inet_addr(); |
| 37 | extern char *index(), *getenv(); |
| 38 | |
| 39 | _res.options |= RES_INIT; |
| 40 | _res.nsaddr.sin_family = AF_INET; |
| 41 | _res.nsaddr.sin_addr.s_addr = INADDR_ANY; |
| 42 | _res.nsaddr.sin_port = htons(NAMESERVER_PORT); |
| 43 | |
| 44 | /* first try reading the config file */ |
| 45 | if ((fp = fopen(CONFFILE, "r")) != NULL) { |
| 46 | if (fgets(_res.defdname, sizeof(_res.defdname), fp) == NULL) |
| 47 | _res.defdname[0] = '\0'; |
| 48 | else if ((cp = index(_res.defdname, '\n')) != NULL) |
| 49 | *cp = '\0'; |
| 50 | if (fgets(buf, sizeof (buf), fp) != NULL) |
| 51 | _res.nsaddr.sin_addr.s_addr = inet_addr(buf); |
| 52 | (void) fclose(fp); |
| 53 | } |
| 54 | |
| 55 | /* Allow user to override the local domain definition */ |
| 56 | if ((cp = getenv("LOCALDOMAIN")) != NULL) |
| 57 | strncpy(_res.defdname, cp, sizeof(_res.defdname)); |
| 58 | } |