diff options
author | deraadt <> | 1995-10-18 08:42:23 +0000 |
---|---|---|
committer | deraadt <> | 1995-10-18 08:42:23 +0000 |
commit | 0527d29da443886d92e9a418180c5b25a5f8d270 (patch) | |
tree | 86b3a64928451a669cefa27900e5884036b4e349 /src/lib/libc/net/res_comp.c | |
download | openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.gz openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.bz2 openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.zip |
initial import of NetBSD tree
Diffstat (limited to 'src/lib/libc/net/res_comp.c')
-rw-r--r-- | src/lib/libc/net/res_comp.c | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..9d7bbecdda --- /dev/null +++ b/src/lib/libc/net/res_comp.c | |||
@@ -0,0 +1,364 @@ | |||
1 | /* $NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1993 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <arpa/nameser.h> | ||
67 | #include <netinet/in.h> | ||
68 | #include <resolv.h> | ||
69 | #include <stdio.h> | ||
70 | |||
71 | static int dn_find(); | ||
72 | |||
73 | /* | ||
74 | * Expand compressed domain name 'comp_dn' to full domain name. | ||
75 | * 'msg' is a pointer to the begining of the message, | ||
76 | * 'eomorig' points to the first location after the message, | ||
77 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | ||
78 | * Return size of compressed name or -1 if there was an error. | ||
79 | */ | ||
80 | dn_expand(msg, eomorig, comp_dn, exp_dn, length) | ||
81 | const u_char *msg, *eomorig, *comp_dn; | ||
82 | u_char *exp_dn; | ||
83 | int length; | ||
84 | { | ||
85 | register u_char *cp, *dn; | ||
86 | register int n, c; | ||
87 | u_char *eom; | ||
88 | int len = -1, checked = 0; | ||
89 | |||
90 | dn = exp_dn; | ||
91 | cp = (u_char *)comp_dn; | ||
92 | eom = exp_dn + length; | ||
93 | /* | ||
94 | * fetch next label in domain name | ||
95 | */ | ||
96 | while (n = *cp++) { | ||
97 | /* | ||
98 | * Check for indirection | ||
99 | */ | ||
100 | switch (n & INDIR_MASK) { | ||
101 | case 0: | ||
102 | if (dn != exp_dn) { | ||
103 | if (dn >= eom) | ||
104 | return (-1); | ||
105 | *dn++ = '.'; | ||
106 | } | ||
107 | if (dn+n >= eom) | ||
108 | return (-1); | ||
109 | checked += n + 1; | ||
110 | while (--n >= 0) { | ||
111 | if ((c = *cp++) == '.') { | ||
112 | if (dn + n + 2 >= eom) | ||
113 | return (-1); | ||
114 | *dn++ = '\\'; | ||
115 | } | ||
116 | *dn++ = c; | ||
117 | if (cp >= eomorig) /* out of range */ | ||
118 | return(-1); | ||
119 | } | ||
120 | break; | ||
121 | |||
122 | case INDIR_MASK: | ||
123 | if (len < 0) | ||
124 | len = cp - comp_dn + 1; | ||
125 | cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); | ||
126 | if (cp < msg || cp >= eomorig) /* out of range */ | ||
127 | return(-1); | ||
128 | checked += 2; | ||
129 | /* | ||
130 | * Check for loops in the compressed name; | ||
131 | * if we've looked at the whole message, | ||
132 | * there must be a loop. | ||
133 | */ | ||
134 | if (checked >= eomorig - msg) | ||
135 | return (-1); | ||
136 | break; | ||
137 | |||
138 | default: | ||
139 | return (-1); /* flag error */ | ||
140 | } | ||
141 | } | ||
142 | *dn = '\0'; | ||
143 | if (len < 0) | ||
144 | len = cp - comp_dn; | ||
145 | return (len); | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * Compress domain name 'exp_dn' into 'comp_dn'. | ||
150 | * Return the size of the compressed name or -1. | ||
151 | * 'length' is the size of the array pointed to by 'comp_dn'. | ||
152 | * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] | ||
153 | * is a pointer to the beginning of the message. The list ends with NULL. | ||
154 | * 'lastdnptr' is a pointer to the end of the arrary pointed to | ||
155 | * by 'dnptrs'. Side effect is to update the list of pointers for | ||
156 | * labels inserted into the message as we compress the name. | ||
157 | * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' | ||
158 | * is NULL, we don't update the list. | ||
159 | */ | ||
160 | dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) | ||
161 | const u_char *exp_dn; | ||
162 | u_char *comp_dn, **dnptrs, **lastdnptr; | ||
163 | int length; | ||
164 | { | ||
165 | register u_char *cp, *dn; | ||
166 | register int c, l; | ||
167 | u_char **cpp, **lpp, *sp, *eob; | ||
168 | u_char *msg; | ||
169 | |||
170 | dn = (u_char *)exp_dn; | ||
171 | cp = comp_dn; | ||
172 | eob = cp + length; | ||
173 | if (dnptrs != NULL) { | ||
174 | if ((msg = *dnptrs++) != NULL) { | ||
175 | for (cpp = dnptrs; *cpp != NULL; cpp++) | ||
176 | ; | ||
177 | lpp = cpp; /* end of list to search */ | ||
178 | } | ||
179 | } else | ||
180 | msg = NULL; | ||
181 | for (c = *dn++; c != '\0'; ) { | ||
182 | /* look to see if we can use pointers */ | ||
183 | if (msg != NULL) { | ||
184 | if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { | ||
185 | if (cp+1 >= eob) | ||
186 | return (-1); | ||
187 | *cp++ = (l >> 8) | INDIR_MASK; | ||
188 | *cp++ = l % 256; | ||
189 | return (cp - comp_dn); | ||
190 | } | ||
191 | /* not found, save it */ | ||
192 | if (lastdnptr != NULL && cpp < lastdnptr-1) { | ||
193 | *cpp++ = cp; | ||
194 | *cpp = NULL; | ||
195 | } | ||
196 | } | ||
197 | sp = cp++; /* save ptr to length byte */ | ||
198 | do { | ||
199 | if (c == '.') { | ||
200 | c = *dn++; | ||
201 | break; | ||
202 | } | ||
203 | if (c == '\\') { | ||
204 | if ((c = *dn++) == '\0') | ||
205 | break; | ||
206 | } | ||
207 | if (cp >= eob) { | ||
208 | if (msg != NULL) | ||
209 | *lpp = NULL; | ||
210 | return (-1); | ||
211 | } | ||
212 | *cp++ = c; | ||
213 | } while ((c = *dn++) != '\0'); | ||
214 | /* catch trailing '.'s but not '..' */ | ||
215 | if ((l = cp - sp - 1) == 0 && c == '\0') { | ||
216 | cp--; | ||
217 | break; | ||
218 | } | ||
219 | if (l <= 0 || l > MAXLABEL) { | ||
220 | if (msg != NULL) | ||
221 | *lpp = NULL; | ||
222 | return (-1); | ||
223 | } | ||
224 | *sp = l; | ||
225 | } | ||
226 | if (cp >= eob) { | ||
227 | if (msg != NULL) | ||
228 | *lpp = NULL; | ||
229 | return (-1); | ||
230 | } | ||
231 | *cp++ = '\0'; | ||
232 | return (cp - comp_dn); | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * Skip over a compressed domain name. Return the size or -1. | ||
237 | */ | ||
238 | __dn_skipname(comp_dn, eom) | ||
239 | const u_char *comp_dn, *eom; | ||
240 | { | ||
241 | register u_char *cp; | ||
242 | register int n; | ||
243 | |||
244 | cp = (u_char *)comp_dn; | ||
245 | while (cp < eom && (n = *cp++)) { | ||
246 | /* | ||
247 | * check for indirection | ||
248 | */ | ||
249 | switch (n & INDIR_MASK) { | ||
250 | case 0: /* normal case, n == len */ | ||
251 | cp += n; | ||
252 | continue; | ||
253 | case INDIR_MASK: /* indirection */ | ||
254 | cp++; | ||
255 | break; | ||
256 | default: /* illegal type */ | ||
257 | return (-1); | ||
258 | } | ||
259 | break; | ||
260 | } | ||
261 | if (cp > eom) | ||
262 | return -1; | ||
263 | return (cp - comp_dn); | ||
264 | } | ||
265 | |||
266 | /* | ||
267 | * Search for expanded name from a list of previously compressed names. | ||
268 | * Return the offset from msg if found or -1. | ||
269 | * dnptrs is the pointer to the first name on the list, | ||
270 | * not the pointer to the start of the message. | ||
271 | */ | ||
272 | static int | ||
273 | dn_find(exp_dn, msg, dnptrs, lastdnptr) | ||
274 | u_char *exp_dn, *msg; | ||
275 | u_char **dnptrs, **lastdnptr; | ||
276 | { | ||
277 | register u_char *dn, *cp, **cpp; | ||
278 | register int n; | ||
279 | u_char *sp; | ||
280 | |||
281 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | ||
282 | dn = exp_dn; | ||
283 | sp = cp = *cpp; | ||
284 | while (n = *cp++) { | ||
285 | /* | ||
286 | * check for indirection | ||
287 | */ | ||
288 | switch (n & INDIR_MASK) { | ||
289 | case 0: /* normal case, n == len */ | ||
290 | while (--n >= 0) { | ||
291 | if (*dn == '.') | ||
292 | goto next; | ||
293 | if (*dn == '\\') | ||
294 | dn++; | ||
295 | if (*dn++ != *cp++) | ||
296 | goto next; | ||
297 | } | ||
298 | if ((n = *dn++) == '\0' && *cp == '\0') | ||
299 | return (sp - msg); | ||
300 | if (n == '.') | ||
301 | continue; | ||
302 | goto next; | ||
303 | |||
304 | default: /* illegal type */ | ||
305 | return (-1); | ||
306 | |||
307 | case INDIR_MASK: /* indirection */ | ||
308 | cp = msg + (((n & 0x3f) << 8) | *cp); | ||
309 | } | ||
310 | } | ||
311 | if (*dn == '\0') | ||
312 | return (sp - msg); | ||
313 | next: ; | ||
314 | } | ||
315 | return (-1); | ||
316 | } | ||
317 | |||
318 | /* | ||
319 | * Routines to insert/extract short/long's. Must account for byte | ||
320 | * order and non-alignment problems. This code at least has the | ||
321 | * advantage of being portable. | ||
322 | * | ||
323 | * used by sendmail. | ||
324 | */ | ||
325 | |||
326 | u_short | ||
327 | _getshort(msgp) | ||
328 | register u_char *msgp; | ||
329 | { | ||
330 | register u_int16_t u; | ||
331 | |||
332 | GETSHORT(u, msgp); | ||
333 | return (u); | ||
334 | } | ||
335 | |||
336 | u_int32_t | ||
337 | _getlong(msgp) | ||
338 | register u_char *msgp; | ||
339 | { | ||
340 | register u_int32_t u; | ||
341 | |||
342 | GETLONG(u, msgp); | ||
343 | return (u); | ||
344 | } | ||
345 | |||
346 | void | ||
347 | #if defined(__STDC__) || defined(__cplusplus) | ||
348 | __putshort(register u_int16_t s, register u_char *msgp) | ||
349 | #else | ||
350 | __putshort(s, msgp) | ||
351 | register u_int16_t s; | ||
352 | register u_char *msgp; | ||
353 | #endif | ||
354 | { | ||
355 | PUTSHORT(s, msgp); | ||
356 | } | ||
357 | |||
358 | void | ||
359 | __putlong(l, msgp) | ||
360 | register u_int32_t l; | ||
361 | register u_char *msgp; | ||
362 | { | ||
363 | PUTLONG(l, msgp); | ||
364 | } | ||