2 #include "stdio_impl.h"
6 #include <netinet/in.h>
8 int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz)
11 unsigned char _buf[256];
18 if (search) *search = 0;
20 f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf);
21 if (!f) switch (errno) {
30 while (fgets(line, sizeof line, f)) {
32 if (!strchr(line, '\n') && !feof(f)) {
33 /* Ignore lines that get truncated rather than
34 * potentially misinterpreting them. */
37 while (c != '\n' && c != EOF);
40 if (!strncmp(line, "options", 7) && isspace(line[7])) {
41 p = strstr(line, "ndots:");
42 if (p && isdigit(p[6])) {
44 unsigned long x = strtoul(p, &z, 10);
45 if (z != p) conf->ndots = x > 15 ? 15 : x;
47 p = strstr(line, "attempts:");
48 if (p && isdigit(p[6])) {
50 unsigned long x = strtoul(p, &z, 10);
51 if (z != p) conf->attempts = x > 10 ? 10 : x;
53 p = strstr(line, "timeout:");
54 if (p && (isdigit(p[8]) || p[8]=='.')) {
56 unsigned long x = strtoul(p, &z, 10);
57 if (z != p) conf->timeout = x > 60 ? 60 : x;
61 if (!strncmp(line, "nameserver", 10) && isspace(line[10])) {
62 if (nns >= MAXNS) continue;
63 for (p=line+11; isspace(*p); p++);
64 for (z=p; *z && !isspace(*z); z++);
66 if (__lookup_ipliteral(conf->ns+nns, p, AF_UNSPEC) > 0)
71 if (!search) continue;
72 if ((strncmp(line, "domain", 6) && strncmp(line, "search", 6))
75 for (p=line+7; isspace(*p); p++);
77 /* This can never happen anyway with chosen buffer sizes. */
78 if (l >= search_sz) continue;
79 memcpy(search, p, l+1);
86 __lookup_ipliteral(conf->ns, "127.0.0.1", AF_UNSPEC);