1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
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.
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.
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/>.
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);
25 int is_name_synthetic(int flags, char *name, union all_addr *addr)
28 struct cond_domain *c = NULL;
29 int prot = (flags & F_IPV6) ? AF_INET6 : AF_INET;
31 for (c = daemon->synth_domains; c; c = c->next)
36 for (tail = name, pref = c->prefix; *tail != 0 && pref && *pref != 0; tail++, pref++)
38 unsigned int c1 = (unsigned char) *pref;
39 unsigned int c2 = (unsigned char) *tail;
41 if (c1 >= 'A' && c1 <= 'Z')
43 if (c2 >= 'A' && c2 <= 'Z')
50 if (pref && *pref != 0)
51 continue; /* prefix match fail */
55 for (p = tail; *p; p++)
59 if (c < '0' || c > '9')
68 if (hostname_isequal(c->domain, p+1))
72 unsigned int index = atoi(tail);
75 index <= ntohl(c->end.s_addr) - ntohl(c->start.s_addr))
77 addr->addr4.s_addr = htonl(ntohl(c->start.s_addr) + index);
83 u64 index = atoll(tail);
86 index <= addr6part(&c->end6) - addr6part(&c->start6))
88 u64 start = addr6part(&c->start6);
89 addr->addr6 = c->start6;
90 setaddr6part(&addr->addr6, start + index);
98 /* NB, must not alter name if we return zero */
99 for (p = tail; *p; p++)
103 if ((c >='0' && c <= '9') || c == '-')
106 if (prot == AF_INET6 && ((c >='A' && c <= 'F') || (c >='a' && c <= 'f')))
117 if (prot == AF_INET6 && strstr(tail, "--ffff-") == tail)
119 /* special hack for v4-mapped. */
120 memcpy(tail, "::ffff:", 7);
121 for (p = tail + 7; *p; p++)
127 /* swap . or : for - */
128 for (p = tail; *p; p++)
138 if (hostname_isequal(c->domain, p+1) && inet_pton(prot, tail, addr))
139 found = (prot == AF_INET) ? match_domain(addr->addr4, c) : match_domain6(&addr->addr6, c);
143 for (p = tail; *p; p++)
144 if (*p == '.' || *p == ':')
158 int is_rev_synth(int flag, union all_addr *addr, char *name)
160 struct cond_domain *c;
162 if (flag & F_IPV4 && (c = search_domain(addr->addr4, daemon->synth_domains)))
169 unsigned int index = ntohl(addr->addr4.s_addr) - ntohl(c->start.s_addr);
170 snprintf(name, MAXDNAME, "%s%u", c->prefix ? c->prefix : "", index);
175 strncpy(name, c->prefix, MAXDNAME - ADDRSTRLEN);
177 inet_ntop(AF_INET, &addr->addr4, name + strlen(name), ADDRSTRLEN);
178 for (p = name; *p; p++)
183 strncat(name, ".", MAXDNAME);
184 strncat(name, c->domain, MAXDNAME);
189 if ((flag & F_IPV6) && (c = search_domain6(&addr->addr6, daemon->synth_domains)))
196 u64 index = addr6part(&addr->addr6) - addr6part(&c->start6);
197 snprintf(name, MAXDNAME, "%s%llu", c->prefix ? c->prefix : "", index);
202 strncpy(name, c->prefix, MAXDNAME - ADDRSTRLEN);
204 inet_ntop(AF_INET6, &addr->addr6, name + strlen(name), ADDRSTRLEN);
206 /* IPv6 presentation address can start with ":", but valid domain names
207 cannot start with "-" so prepend a zero in that case. */
208 if (!c->prefix && *name == ':')
211 inet_ntop(AF_INET6, &addr->addr6, name+1, ADDRSTRLEN);
214 /* V4-mapped have periods.... */
215 for (p = name; *p; p++)
216 if (*p == ':' || *p == '.')
221 strncat(name, ".", MAXDNAME);
222 strncat(name, c->domain, MAXDNAME);
231 static int match_domain(struct in_addr addr, struct cond_domain *c)
236 for (al = c->al; al; al = al->next)
237 if (!(al->flags & ADDRLIST_IPV6) &&
238 is_same_net_prefix(addr, al->addr.addr4, al->prefixlen))
242 ntohl(addr.s_addr) >= ntohl(c->start.s_addr) &&
243 ntohl(addr.s_addr) <= ntohl(c->end.s_addr))
249 static struct cond_domain *search_domain(struct in_addr addr, struct cond_domain *c)
251 for (; c; c = c->next)
252 if (match_domain(addr, c))
258 char *get_domain(struct in_addr addr)
260 struct cond_domain *c;
262 if ((c = search_domain(addr, daemon->cond_domain)))
265 return daemon->domain_suffix;
268 static int match_domain6(struct in6_addr *addr, struct cond_domain *c)
271 /* subnet from interface address. */
275 for (al = c->al; al; al = al->next)
276 if (al->flags & ADDRLIST_IPV6 &&
277 is_same_net6(addr, &al->addr.addr6, al->prefixlen))
282 if (c->prefixlen >= 64)
284 u64 addrpart = addr6part(addr);
285 if (is_same_net6(addr, &c->start6, 64) &&
286 addrpart >= addr6part(&c->start6) &&
287 addrpart <= addr6part(&c->end6))
290 else if (is_same_net6(addr, &c->start6, c->prefixlen))
297 static struct cond_domain *search_domain6(struct in6_addr *addr, struct cond_domain *c)
299 for (; c; c = c->next)
300 if (match_domain6(addr, c))
306 char *get_domain6(struct in6_addr *addr)
308 struct cond_domain *c;
310 if (addr && (c = search_domain6(addr, daemon->cond_domain)))
313 return daemon->domain_suffix;