usched: Implement LWP lazy migration support.
[dragonfly.git] / lib / libc / nameser / ns_samedomain.c
blobee890090b8d944a849ffb422933ae396c57c69b7
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp $";
20 #endif
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <arpa/nameser.h>
26 #include <errno.h>
27 #include <string.h>
29 #include "port_after.h"
31 /*%
32 * Check whether a name belongs to a domain.
34 * Inputs:
35 *\li a - the domain whose ancestory is being verified
36 *\li b - the potential ancestor we're checking against
38 * Return:
39 *\li boolean - is a at or below b?
41 * Notes:
42 *\li Trailing dots are first removed from name and domain.
43 * Always compare complete subdomains, not only whether the
44 * domain name is the trailing string of the given name.
46 *\li "host.foobar.top" lies in "foobar.top" and in "top" and in ""
47 * but NOT in "bar.top"
50 int
51 ns_samedomain(const char *a, const char *b) {
52 size_t la, lb;
53 int diff, i, escaped;
54 const char *cp;
56 la = strlen(a);
57 lb = strlen(b);
59 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
60 if (la != 0U && a[la - 1] == '.') {
61 escaped = 0;
62 /* Note this loop doesn't get executed if la==1. */
63 for (i = la - 2; i >= 0; i--)
64 if (a[i] == '\\') {
65 if (escaped)
66 escaped = 0;
67 else
68 escaped = 1;
69 } else
70 break;
71 if (!escaped)
72 la--;
75 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
76 if (lb != 0U && b[lb - 1] == '.') {
77 escaped = 0;
78 /* note this loop doesn't get executed if lb==1 */
79 for (i = lb - 2; i >= 0; i--)
80 if (b[i] == '\\') {
81 if (escaped)
82 escaped = 0;
83 else
84 escaped = 1;
85 } else
86 break;
87 if (!escaped)
88 lb--;
91 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
92 if (lb == 0U)
93 return (1);
95 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
96 if (lb > la)
97 return (0);
99 /* 'a' and 'b' being equal at this point indicates sameness. */
100 if (lb == la)
101 return (strncasecmp(a, b, lb) == 0);
103 /* Ok, we know la > lb. */
105 diff = la - lb;
108 * If 'a' is only 1 character longer than 'b', then it can't be
109 * a subdomain of 'b' (because of the need for the '.' label
110 * separator).
112 if (diff < 2)
113 return (0);
116 * If the character before the last 'lb' characters of 'b'
117 * isn't '.', then it can't be a match (this lets us avoid
118 * having "foobar.com" match "bar.com").
120 if (a[diff - 1] != '.')
121 return (0);
124 * We're not sure about that '.', however. It could be escaped
125 * and thus not a really a label separator.
127 escaped = 0;
128 for (i = diff - 2; i >= 0; i--)
129 if (a[i] == '\\') {
130 if (escaped)
131 escaped = 0;
132 else
133 escaped = 1;
134 } else
135 break;
136 if (escaped)
137 return (0);
139 /* Now compare aligned trailing substring. */
140 cp = a + diff;
141 return (strncasecmp(cp, b, lb) == 0);
144 #ifndef _LIBC
146 * int
147 * ns_subdomain(a, b)
148 * is "a" a subdomain of "b"?
151 ns_subdomain(const char *a, const char *b) {
152 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
154 #endif
157 * make a canonical copy of domain name "src"
159 * notes:
160 * \code
161 * foo -> foo.
162 * foo. -> foo.
163 * foo.. -> foo.
164 * foo\. -> foo\..
165 * foo\\. -> foo\\.
166 * \endcode
170 ns_makecanon(const char *src, char *dst, size_t dstsize) {
171 size_t n = strlen(src);
173 if (n + sizeof "." > dstsize) { /*%< Note: sizeof == 2 */
174 errno = EMSGSIZE;
175 return (-1);
177 strcpy(dst, src);
178 while (n >= 1U && dst[n - 1] == '.') /*%< Ends in "." */
179 if (n >= 2U && dst[n - 2] == '\\' && /*%< Ends in "\." */
180 (n < 3U || dst[n - 3] != '\\')) /*%< But not "\\." */
181 break;
182 else
183 dst[--n] = '\0';
184 dst[n++] = '.';
185 dst[n] = '\0';
186 return (0);
190 * determine whether domain name "a" is the same as domain name "b"
192 * return:
193 *\li -1 on error
194 *\li 0 if names differ
195 *\li 1 if names are the same
199 ns_samename(const char *a, const char *b) {
200 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
202 if (ns_makecanon(a, ta, sizeof ta) < 0 ||
203 ns_makecanon(b, tb, sizeof tb) < 0)
204 return (-1);
205 if (strcasecmp(ta, tb) == 0)
206 return (1);
207 else
208 return (0);
211 /*! \file */