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