djbdnscurve6 53
djbdnscurve6
Loading...
Searching...
No Matches
dns_rcip.c
Go to the documentation of this file.
1#include "taia.h"
2#include "readclose.h"
3#include "byte.h"
4#include "ip.h"
5#include "env.h"
6#include "dns.h"
7#include "socket_if.h"
8
9static stralloc data = {0};
10static stralloc ifname = {0};
11
12static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS])
13{
14 int i;
15 int j;
16 int k = 0;
17 int iplen = 0;
18 char *x;
19 char ip4[4];
20
21/* Read (compactified) IPv4|v6 addresses of resolvers
22 Store them in array IP with fixed length :
23 ip(64) -> 16 IPv4 addresses (not used anymore)
24 ip(QUERY_MAXNS * 16) -> QUERY_MAXNS 16 bit IPv6 addresses (we use IPv4 mapped IPv6 addresses)
25 sid(QUERY_MAXNS) -> the scope for the respective IPv6 or 0
26*/
27 for (i = 0; i < QUERY_MAXNS; ++i) sid[i] = 0;
28
29 x = env_get("DNSCACHEIP");
30 if (x)
31 while (iplen <= 240 && *x != '\0') {
32 if (*x == ' ')
33 ++x;
34 else
35 if ((i = ip6_ifscan(x,ip + iplen,&ifname))) {
36 if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s);
37 iplen += 16; k++;
38 if (*(x += i) == '\0') break;
39 }
40 }
41
42 if (!iplen) {
43 i = openreadclose("/etc/resolv.conf",&data,64);
44 if (i == -1) return DNS_INT;
45 if (i) {
46 if (!stralloc_append(&data,"\n")) return DNS_MEM;
47 i = 0;
48 for (j = 0; j < data.len; ++j)
49 if (data.s[j] == '\n') {
50 if (byte_equal("nameserver ",11,data.s + i) || \
51 byte_equal("nameserver\t",11,data.s + i)) {
52 i += 10;
53 while ((data.s[i] == ' ') || (data.s[i] == '\t'))
54 i++;
55 if (iplen <= 240) {
56 data.s[j] = '\0'; /* ip6_ifscan needs terminated string on input */
57 if (ip4_scan(data.s + i,ip4)) {
58 if (byte_equal(ip4,4,"\0\0\0\0"))
59 byte_copy(ip4,4,"\177\0\0\1");
60 byte_copy(ip + iplen,12,V4mappedprefix);
61 byte_copy(ip + iplen + 12,4,ip4);
62 sid[k] = 0; iplen += 16; k++;
63 } else if (ip6_ifscan(data.s + i,ip + iplen,&ifname)) {
64 if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s);
65 iplen += 16; k++;
66 }
67 }
68 }
69 i = j + 1;
70 }
71 }
72 }
73
74 if (!iplen) {
75 byte_copy(ip,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1");
76 iplen = 16;
77 }
78 byte_zero(ip + iplen,QUERY_MAXIPLEN - iplen);
79 return 0;
80}
81
82static int ok = 0;
83static unsigned int uses;
84static struct taia deadline;
85static char ip[QUERY_MAXIPLEN]; /* defined if ok */
86static uint32 scopes[QUERY_MAXNS];
87
89{
90 struct taia now;
91
92 taia_now(&now);
93 if (taia_less(&deadline,&now)) ok = 0;
94 if (!uses) ok = 0;
95
96 if (!ok) {
97 if (init(ip,scopes) < 0) return DNS_INT;
98 taia_uint(&deadline,600);
99 taia_add(&deadline,&now,&deadline);
100 uses = 10000;
101 ok = 1;
102 }
103
104 --uses;
105 byte_copy(s,QUERY_MAXIPLEN,ip);
106 byte_copy((char *)scope,128,(char *)scopes);
107 return 0;
108}
char data[32767]
Definition: axfrdns.c:133
struct tai now
Definition: axfrdns.c:132
char ip[16]
Definition: axfrdns.c:128
#define QUERY_MAXIPLEN
Definition: dns.h:55
#define DNS_INT
Definition: dns.h:43
#define QUERY_MAXNS
Definition: dns.h:54
#define DNS_MEM
Definition: dns.h:40
int dns_resolvconfip(char s[QUERY_MAXIPLEN], uint32 scope[QUERY_MAXNS])
Definition: dns_rcip.c:88
struct line * x
char ip4[4]
Definition: dnsfilter.c:56