Debian Codegraph @

src/domain.c

raw
1 /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "dnsmasq.h"
18
19
20 static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c);
21 static int match_domain(struct in_addr addr, struct cond_domain *c);
22 static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c);
23 static int match_domain6(struct in6_addr *addr, struct cond_domain *c);
24
25 int is_name_synthetic(int flags, char *name, union all_addr *addrp)
26 {
27 char *p;
28 struct cond_domain *c = NULL;
29 int prot = (flags & F_IPV6) ? AF_INET6 : AF_INET;
30 union all_addr addr;
31
32 for (c = daemon->synth_domains; c; c = c->next)
33 {
34 int found = 0;
35 char *tail, *pref;
36
37 for (tail = name, pref = c->prefix; *tail != 0 && pref && *pref != 0; tail++, pref++)
38 {
39 unsigned int c1 = (unsigned char) *pref;
40 unsigned int c2 = (unsigned char) *tail;
41
42 if (c1 >= 'A' && c1 <= 'Z')
43 c1 += 'a' - 'A';
44 if (c2 >= 'A' && c2 <= 'Z')
45 c2 += 'a' - 'A';
46
47 if (c1 != c2)
48 break;
49 }
50
51 if (pref && *pref != 0)
52 continue; /* prefix match fail */
53
54 if (c->indexed)
55 {
56 for (p = tail; *p; p++)
57 {
58 char c = *p;
59
60 if (c < '0' || c > '9')
61 break;
62 }
63
64 if (*p != '.')
65 continue;
66
67 *p = 0;
68
69 if (hostname_isequal(c->domain, p+1))
70 {
71 if (prot == AF_INET)
72 {
73 unsigned int index = atoi(tail);
74
75 if (!c->is6 &&
76 index <= ntohl(c->end.s_addr) - ntohl(c->start.s_addr))
77 {
78 addr.addr4.s_addr = htonl(ntohl(c->start.s_addr) + index);
79 found = 1;
80 }
81 }
82 else
83 {
84 u64 index = atoll(tail);
85
86 if (c->is6 &&
87 index <= addr6part(&c->end6) - addr6part(&c->start6))
88 {
89 u64 start = addr6part(&c->start6);
90 addr.addr6 = c->start6;
91 setaddr6part(&addr.addr6, start + index);
92 found = 1;
93 }
94 }
95 }
96 }
97 else
98 {
99 /* NB, must not alter name if we return zero */
100 for (p = tail; *p; p++)
101 {
102 char c = *p;
103
104 if ((c >='0' && c <= '9') || c == '-')
105 continue;
106
107 if (prot == AF_INET6 && ((c >='A' && c <= 'F') || (c >='a' && c <= 'f')))
108 continue;
109
110 break;
111 }
112
113 if (*p != '.')
114 continue;
115
116 *p = 0;
117
118 /* swap . or : for - */
119 for (p = tail; *p; p++)
120 if (*p == '-')
121 {
122 if (prot == AF_INET)
123 *p = '.';
124 else
125 *p = ':';
126 }
127
128 if (hostname_isequal(c->domain, p+1) && inet_pton(prot, tail, &addr))
129 found = (prot == AF_INET) ? match_domain(addr.addr4, c) : match_domain6(&addr.addr6, c);
130 }
131
132 /* restore name */
133 for (p = tail; *p; p++)
134 if (*p == '.' || *p == ':')
135 *p = '-';
136
137 *p = '.';
138
139
140 if (found)
141 {
142 if (addrp)
143 *addrp = addr;
144
145 return 1;
146 }
147 }
148
149 return 0;
150 }
151
152
153 int is_rev_synth(int flag, union all_addr *addr, char *name)
154 {
155 struct cond_domain *c;
156
157 if (flag & F_IPV4 && (c = search_domain(addr->addr4, daemon->synth_domains)))
158 {
159 char *p;
160
161 *name = 0;
162 if (c->indexed)
163 {
164 unsigned int index = ntohl(addr->addr4.s_addr) - ntohl(c->start.s_addr);
165 snprintf(name, MAXDNAMESTR, "%s%u", c->prefix ? c->prefix : "", index);
166 }
167 else
168 {
169 if (c->prefix)
170 strncpy(name, c->prefix, MAXDNAMESTR - ADDRSTRLEN);
171
172 inet_ntop(AF_INET, &addr->addr4, name + strlen(name), ADDRSTRLEN);
173 for (p = name; *p; p++)
174 if (*p == '.')
175 *p = '-';
176 }
177
178 strncat(name, ".", MAXDNAMESTR - strlen(name) - 1);
179 strncat(name, c->domain, MAXDNAMESTR - strlen(name) - 1);
180
181 return 1;
182 }
183
184 if ((flag & F_IPV6) && (c = search_domain6(&addr->addr6, daemon->synth_domains)))
185 {
186 *name = 0;
187
188 if (c->indexed)
189 {
190 u64 index = addr6part(&addr->addr6) - addr6part(&c->start6);
191 snprintf(name, MAXDNAMESTR, "%s%llu", c->prefix ? c->prefix : "", index);
192 }
193 else
194 {
195 int i;
196 char frag[6];
197
198 if (c->prefix)
199 strncpy(name, c->prefix, MAXDNAMESTR);
200
201 for (i = 0; i < 16; i += 2)
202 {
203 sprintf(frag, "%s%02x%02x", i == 0 ? "" : "-", addr->addr6.s6_addr[i], addr->addr6.s6_addr[i+1]);
204 strncat(name, frag, MAXDNAMESTR - strlen(name) - 1);
205 }
206 }
207
208 strncat(name, ".", MAXDNAMESTR - strlen(name) - 1);
209 strncat(name, c->domain, MAXDNAMESTR - strlen(name) - 1);
210
211 return 1;
212 }
213
214 return 0;
215 }
216
217
218 static int match_domain(struct in_addr addr, struct cond_domain *c)
219 {
220 if (c->interface)
221 {
222 struct addrlist *al;
223 for (al = c->al; al; al = al->next)
224 if (!(al->flags & ADDRLIST_IPV6) &&
225 is_same_net_prefix(addr, al->addr.addr4, al->prefixlen))
226 return 1;
227 }
228 else if (!c->is6 &&
229 ntohl(addr.s_addr) >= ntohl(c->start.s_addr) &&
230 ntohl(addr.s_addr) <= ntohl(c->end.s_addr))
231 return 1;
232
233 return 0;
234 }
235
236 static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c)
237 {
238 for (; c; c = c->next)
239 if (match_domain(addr, c))
240 return c;
241
242 return NULL;
243 }
244
245 char *get_domain(struct in_addr addr)
246 {
247 struct cond_domain *c;
248
249 if ((c = search_domain(addr, daemon->cond_domain)))
250 return c->domain;
251
252 return daemon->domain_suffix;
253 }
254
255 static int match_domain6(struct in6_addr *addr, struct cond_domain *c)
256 {
257
258 /* subnet from interface address. */
259 if (c->interface)
260 {
261 struct addrlist *al;
262 for (al = c->al; al; al = al->next)
263 if (al->flags & ADDRLIST_IPV6 &&
264 is_same_net6(addr, &al->addr.addr6, al->prefixlen))
265 return 1;
266 }
267 else if (c->is6)
268 {
269 if (c->prefixlen >= 64)
270 {
271 u64 addrpart = addr6part(addr);
272 if (is_same_net6(addr, &c->start6, 64) &&
273 addrpart >= addr6part(&c->start6) &&
274 addrpart <= addr6part(&c->end6))
275 return 1;
276 }
277 else if (is_same_net6(addr, &c->start6, c->prefixlen))
278 return 1;
279 }
280
281 return 0;
282 }
283
284 static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c)
285 {
286 for (; c; c = c->next)
287 if (match_domain6(addr, c))
288 return c;
289
290 return NULL;
291 }
292
293 char *get_domain6(struct in6_addr *addr)
294 {
295 struct cond_domain *c;
296
297 if (addr && (c = search_domain6(addr, daemon->cond_domain)))
298 return c->domain;
299
300 return daemon->domain_suffix;
301 }