source: trunk/minix/lib/ip/res_comp.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 7.6 KB
Line 
1/*
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted provided
6 * that: (1) source distributions retain this entire copyright notice and
7 * comment, and (2) distributions including binaries display the following
8 * acknowledgement: ``This product includes software developed by the
9 * University of California, Berkeley and its contributors'' in the
10 * documentation or other materials provided with the distribution and in
11 * all advertising materials mentioning features or use of this software.
12 * Neither the name of the University nor the names of its contributors may
13 * be used to endorse or promote products derived from this software without
14 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21static char sccsid[] = "@(#)res_comp.c 6.18 (Berkeley) 6/27/90";
22#endif /* LIBC_SCCS and not lint */
23
24#if _MINIX
25#include <sys/types.h>
26#include <stdlib.h>
27
28#include <net/gen/in.h>
29#include <net/gen/nameser.h>
30#include <net/gen/resolv.h>
31
32static int dn_find _ARGS(( const u_char *exp_dn, const u_char *msg,
33 u_char **dnptrs, u_char **lastdnptr ));
34int dn_skipname _ARGS(( const u_char *comp_dn, const u_char *eom ));
35
36#define getshort _getshort
37#define getlong _getlong
38#define putshort __putshort
39#define putlong __putlong
40#else
41#include <sys/types.h>
42#include <stdio.h>
43#include <arpa/nameser.h>
44
45static dn_find();
46#endif
47
48#ifdef __STDC__
49#define CONST const
50#else
51#define CONST
52#endif
53
54/*
55 * Expand compressed domain name 'comp_dn' to full domain name.
56 * 'msg' is a pointer to the begining of the message,
57 * 'eomorig' points to the first location after the message,
58 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
59 * Return size of compressed name or -1 if there was an error.
60 */
61dn_expand(msg, eomorig, comp_dn, exp_dn, length)
62 CONST u_char *msg, *eomorig, *comp_dn;
63 u_char *exp_dn;
64 int length;
65{
66 register CONST u_char *cp;
67 register u_char *dn;
68 register int n, c;
69 CONST u_char *eom;
70 int len = -1, checked = 0;
71
72 dn = exp_dn;
73 cp = comp_dn;
74 eom = exp_dn + length;
75 /*
76 * fetch next label in domain name
77 */
78 while (n = *cp++) {
79 /*
80 * Check for indirection
81 */
82 switch (n & INDIR_MASK) {
83 case 0:
84 if (dn != exp_dn) {
85 if (dn >= eom)
86 return (-1);
87 *dn++ = '.';
88 }
89 if (dn+n >= eom)
90 return (-1);
91 checked += n + 1;
92 while (--n >= 0) {
93 if ((c = *cp++) == '.') {
94 if (dn + n + 2 >= eom)
95 return (-1);
96 *dn++ = '\\';
97 }
98 *dn++ = c;
99 if (cp >= eomorig) /* out of range */
100 return(-1);
101 }
102 break;
103
104 case INDIR_MASK:
105 if (len < 0)
106 len = cp - comp_dn + 1;
107 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
108 if (cp < msg || cp >= eomorig) /* out of range */
109 return(-1);
110 checked += 2;
111 /*
112 * Check for loops in the compressed name;
113 * if we've looked at the whole message,
114 * there must be a loop.
115 */
116 if (checked >= eomorig - msg)
117 return (-1);
118 break;
119
120 default:
121 return (-1); /* flag error */
122 }
123 }
124 *dn = '\0';
125 if (len < 0)
126 len = cp - comp_dn;
127 return (len);
128}
129
130/*
131 * Compress domain name 'exp_dn' into 'comp_dn'.
132 * Return the size of the compressed name or -1.
133 * 'length' is the size of the array pointed to by 'comp_dn'.
134 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
135 * is a pointer to the beginning of the message. The list ends with NULL.
136 * 'lastdnptr' is a pointer to the end of the arrary pointed to
137 * by 'dnptrs'. Side effect is to update the list of pointers for
138 * labels inserted into the message as we compress the name.
139 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
140 * is NULL, we don't update the list.
141 */
142int
143dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
144 CONST u_char *exp_dn;
145 u_char *comp_dn;
146 int length;
147 u_char **dnptrs, **lastdnptr;
148{
149 register u_char *cp;
150 register CONST u_char *dn;
151 register int c, l;
152 u_char **cpp, **lpp, *sp, *eob;
153 u_char *msg;
154
155 dn = exp_dn;
156 cp = comp_dn;
157 eob = cp + length;
158 if (dnptrs != NULL) {
159 if ((msg = *dnptrs++) != NULL) {
160 for (cpp = dnptrs; *cpp != NULL; cpp++)
161 ;
162 lpp = cpp; /* end of list to search */
163 }
164 } else
165 msg = NULL;
166 for (c = *dn++; c != '\0'; ) {
167 /* look to see if we can use pointers */
168 if (msg != NULL) {
169 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
170 if (cp+1 >= eob)
171 return (-1);
172 *cp++ = (l >> 8) | INDIR_MASK;
173 *cp++ = l % 256;
174 return (cp - comp_dn);
175 }
176 /* not found, save it */
177 if (lastdnptr != NULL && cpp < lastdnptr-1) {
178 *cpp++ = cp;
179 *cpp = NULL;
180 }
181 }
182 sp = cp++; /* save ptr to length byte */
183 do {
184 if (c == '.') {
185 c = *dn++;
186 break;
187 }
188 if (c == '\\') {
189 if ((c = *dn++) == '\0')
190 break;
191 }
192 if (cp >= eob) {
193 if (msg != NULL)
194 *lpp = NULL;
195 return (-1);
196 }
197 *cp++ = c;
198 } while ((c = *dn++) != '\0');
199 /* catch trailing '.'s but not '..' */
200 if ((l = cp - sp - 1) == 0 && c == '\0') {
201 cp--;
202 break;
203 }
204 if (l <= 0 || l > MAXLABEL) {
205 if (msg != NULL)
206 *lpp = NULL;
207 return (-1);
208 }
209 *sp = l;
210 }
211 if (cp >= eob) {
212 if (msg != NULL)
213 *lpp = NULL;
214 return (-1);
215 }
216 *cp++ = '\0';
217 return (cp - comp_dn);
218}
219
220/*
221 * Skip over a compressed domain name. Return the size or -1.
222 */
223dn_skipname(comp_dn, eom)
224 CONST u_char *comp_dn, *eom;
225{
226 register CONST u_char *cp;
227 register int n;
228
229 cp = comp_dn;
230 while (cp < eom && (n = *cp++)) {
231 /*
232 * check for indirection
233 */
234 switch (n & INDIR_MASK) {
235 case 0: /* normal case, n == len */
236 cp += n;
237 continue;
238 default: /* illegal type */
239 return (-1);
240 case INDIR_MASK: /* indirection */
241 cp++;
242 }
243 break;
244 }
245 return (cp - comp_dn);
246}
247
248/*
249 * Search for expanded name from a list of previously compressed names.
250 * Return the offset from msg if found or -1.
251 * dnptrs is the pointer to the first name on the list,
252 * not the pointer to the start of the message.
253 */
254static int
255dn_find(exp_dn, msg, dnptrs, lastdnptr)
256 CONST u_char *exp_dn, *msg;
257 u_char **dnptrs, **lastdnptr;
258{
259 CONST register u_char *dn, *cp;
260 register u_char **cpp;
261 register int n;
262 CONST u_char *sp;
263
264 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
265 dn = exp_dn;
266 sp = cp = *cpp;
267 while (n = *cp++) {
268 /*
269 * check for indirection
270 */
271 switch (n & INDIR_MASK) {
272 case 0: /* normal case, n == len */
273 while (--n >= 0) {
274 if (*dn == '.')
275 goto next;
276 if (*dn == '\\')
277 dn++;
278 if (*dn++ != *cp++)
279 goto next;
280 }
281 if ((n = *dn++) == '\0' && *cp == '\0')
282 return (sp - msg);
283 if (n == '.')
284 continue;
285 goto next;
286
287 default: /* illegal type */
288 return (-1);
289
290 case INDIR_MASK: /* indirection */
291 cp = msg + (((n & 0x3f) << 8) | *cp);
292 }
293 }
294 if (*dn == '\0')
295 return (sp - msg);
296 next: ;
297 }
298 return (-1);
299}
300
301/*
302 * Routines to insert/extract short/long's. Must account for byte
303 * order and non-alignment problems. This code at least has the
304 * advantage of being portable.
305 *
306 * used by sendmail.
307 */
308
309u16_t
310getshort(msgp)
311 CONST u8_t *msgp;
312{
313 return ((msgp[0] << 8) | (msgp[1] << 0));
314}
315
316u32_t
317getlong(msgp)
318 CONST u8_t *msgp;
319{
320 return ( ((u32_t) msgp[0] << 24)
321 | ((u32_t) msgp[1] << 16)
322 | ((u32_t) msgp[2] << 8)
323 | ((u32_t) msgp[3] << 0));
324}
325
326
327void
328putshort(s, msgp)
329 register U16_t s;
330 register u8_t *msgp;
331{
332
333 msgp[1] = s;
334 msgp[0] = s >> 8;
335}
336
337void
338putlong(l, msgp)
339 register u32_t l;
340 register u8_t *msgp;
341{
342
343 msgp[3] = l;
344 msgp[2] = (l >>= 8);
345 msgp[1] = (l >>= 8);
346 msgp[0] = l >> 8;
347}
Note: See TracBrowser for help on using the repository browser.