3 @@ -880,6 +880,507 @@ void restell(char *s)
9 +static const char digits[] = "0123456789";
10 +#define __set_errno(e) (errno = (e))
12 +#define NS_PUT16(s, cp) do { \
13 + register u_int16_t t_s = (u_int16_t)(s); \
14 + register u_char *t_cp = (u_char *)(cp); \
15 + *t_cp++ = t_s >> 8; \
17 + (cp) += NS_INT16SZ; \
22 +#define NS_PUT32(l, cp) do { \
23 + register u_int32_t t_l = (u_int32_t)(l); \
24 + register u_char *t_cp = (u_char *)(cp); \
25 + *t_cp++ = t_l >> 24; \
26 + *t_cp++ = t_l >> 16; \
27 + *t_cp++ = t_l >> 8; \
29 + (cp) += NS_INT32SZ; \
34 +ns_put16(u_int src, u_char *dst) {
39 +ns_put32(u_long src, u_char *dst) {
43 +void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
44 +void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
48 + if (ch >= 0x41 && ch <= 0x5A)
55 +dn_find(const u_char *domain, const u_char *msg,
56 + const u_char * const *dnptrs,
57 + const u_char * const *lastdnptr)
59 + const u_char *dn, *cp, *sp;
60 + const u_char * const *cpp;
63 + for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
66 + * terminate search on:
68 + * compression pointer
71 + while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
72 + (sp - msg) < 0x4000) {
75 + while ((n = *cp++) != 0) {
77 + * check for indirection
79 + switch (n & NS_CMPRSFLGS) {
80 + case 0: /* normal case, n == len */
83 + for ((void)NULL; n > 0; n--)
84 + if (mklower(*dn++) !=
87 + /* Is next root for both ? */
88 + if (*dn == '\0' && *cp == '\0')
94 + case NS_CMPRSFLGS: /* indirection */
95 + cp = msg + (((n & 0x3f) << 8) | *cp);
98 + default: /* illegal type */
99 + __set_errno (EMSGSIZE);
107 + __set_errno (ENOENT);
113 +ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
114 + const u_char **dnptrs, const u_char **lastdnptr)
117 + const u_char **cpp, **lpp, *eob, *msg;
118 + const u_char *srcp;
119 + int n, l, first = 1;
123 + eob = dstp + dstsiz;
125 + if (dnptrs != NULL) {
126 + if ((msg = *dnptrs++) != NULL) {
127 + for (cpp = dnptrs; *cpp != NULL; cpp++)
129 + lpp = cpp; /* end of list to search */
134 + /* make sure the domain we are about to add is legal */
138 + if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
139 + __set_errno (EMSGSIZE);
145 + if (l > MAXCDNAME) {
146 + __set_errno (EMSGSIZE);
152 + /* from here on we need to reset compression pointer array on error */
155 + /* Look to see if we can use pointers. */
157 + if (n != 0 && n != 0x41 && msg != NULL) {
158 + l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
159 + (const u_char * const *)lpp);
161 + if (dstp + 1 >= eob) {
164 + *dstp++ = (l >> 8) | NS_CMPRSFLGS;
166 + return (dstp - dst);
168 + /* Not found, save it. */
169 + if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
170 + (dstp - msg) < 0x4000 && first) {
176 + /* copy label to buffer */
177 + if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) { /* Should not happen. */
182 + if (dstp + 1 >= eob)
186 + if (dstp + 1 + n >= eob) {
189 + memcpy(dstp, srcp, n + 1);
198 + __set_errno (EMSGSIZE);
201 + return (dstp - dst);
206 +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
207 + u_char *label, *bp, *eom;
213 + eom = dst + dstsiz;
216 + while ((c = *src++) != 0) {
218 + if ((cp = strchr(digits, c)) != NULL) {
219 + n = (cp - digits) * 100;
220 + if ((c = *src++) == 0 ||
221 + (cp = strchr(digits, c)) == NULL) {
222 + __set_errno (EMSGSIZE);
225 + n += (cp - digits) * 10;
226 + if ((c = *src++) == 0 ||
227 + (cp = strchr(digits, c)) == NULL) {
228 + __set_errno (EMSGSIZE);
231 + n += (cp - digits);
233 + __set_errno (EMSGSIZE);
237 + } else if (c == '[' && label == bp - 1 && *src == 'x') {
238 + /* Theoretically we would have to handle \[o
239 + as well but we do not since we do not need
244 + while (isxdigit (*src)) {
245 + n = *src > '9' ? *src - 'a' + 10 : *src - '0';
247 + if (! isxdigit(*src)) {
248 + __set_errno (EMSGSIZE);
252 + n += *src > '9' ? *src - 'a' + 10 : *src - '0';
253 + if (bp + 1 >= eom) {
254 + __set_errno (EMSGSIZE);
260 + *label = (bp - label - 1) * 8;
261 + if (*src++ != ']' || *src++ != '.') {
262 + __set_errno (EMSGSIZE);
268 + __set_errno (EMSGSIZE);
274 + } else if (c == '\\') {
277 + } else if (c == '.') {
278 + c = (bp - label - 1);
279 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
280 + __set_errno (EMSGSIZE);
283 + if (label >= eom) {
284 + __set_errno (EMSGSIZE);
288 + /* Fully qualified ? */
289 + if (*src == '\0') {
292 + __set_errno (EMSGSIZE);
297 + if ((bp - dst) > MAXCDNAME) {
298 + __set_errno (EMSGSIZE);
303 + if (c == 0 || *src == '.') {
304 + __set_errno (EMSGSIZE);
311 + __set_errno (EMSGSIZE);
316 + c = (bp - label - 1);
317 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
318 + __set_errno (EMSGSIZE);
321 + if (label >= eom) {
322 + __set_errno (EMSGSIZE);
328 + __set_errno (EMSGSIZE);
333 + if ((bp - dst) > MAXCDNAME) { /* src too big */
334 + __set_errno (EMSGSIZE);
343 +ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
344 + const u_char **dnptrs, const u_char **lastdnptr)
346 + u_char tmp[NS_MAXCDNAME];
348 + if (ns_name_pton(src, tmp, sizeof tmp) == -1)
350 + return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
355 +dn_comp(const char *src, u_char *dst, int dstsiz,
356 + u_char **dnptrs, u_char **lastdnptr)
358 + return (ns_name_compress(src, dst, (size_t)dstsiz,
359 + (const u_char **)dnptrs,
360 + (const u_char **)lastdnptr));
367 +res_nmkquery(res_state statp,
368 + int op, /* opcode of query */
369 + const char *dname, /* domain name */
370 + int class, int type, /* class and type of query */
371 + const u_char *data, /* resource record data */
372 + int datalen, /* length of data */
373 + const u_char *newrr_in, /* new rr for modify or append */
374 + u_char *buf, /* buffer to put query */
375 + int buflen) /* size of buffer */
377 + register HEADER *hp;
378 + register u_char *cp;
380 + u_char *dnptrs[20], **dpp, **lastdnptr;
383 + if (statp->options & RES_DEBUG)
384 + printf(";; res_nmkquery(%s, %s, %s, %s)\n",
385 + _res_opcodes[op], dname, p_class(class), p_type(type));
388 + * Initialize header fields.
390 + if ((buf == NULL) || (buflen < HFIXEDSZ))
392 + memset(buf, 0, HFIXEDSZ);
393 + hp = (HEADER *) buf;
394 + /* We randomize the IDs every time. The old code just
395 + incremented by one after the initial randomization which
396 + still predictable if the application does multiple
399 + hp->id = htons(++statp->id);
401 + hp->id = htons(statp->id);
406 + RANDOM_BITS (randombits);
409 + gettimeofday (&tv, NULL);
410 + randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
413 + while ((randombits & 0xffff) == 0);
414 + statp->id = (statp->id + randombits) & 0xffff;
417 + hp->rd = (statp->options & RES_RECURSE) != 0;
418 + hp->rcode = NOERROR;
419 + cp = buf + HFIXEDSZ;
420 + buflen -= HFIXEDSZ;
424 + lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
426 + * perform opcode specific processing
429 + case QUERY: /*FALLTHROUGH*/
431 + if ((buflen -= QFIXEDSZ) < 0)
433 + if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
437 + __putshort(type, cp);
439 + __putshort(class, cp);
441 + hp->qdcount = htons(1);
442 + if (op == QUERY || data == NULL)
445 + * Make an additional record for completion domain.
447 + buflen -= RRFIXEDSZ;
448 + n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
453 + __putshort(T_NULL, cp);
455 + __putshort(class, cp);
461 + hp->arcount = htons(1);
466 + * Initialize answer section
468 + if (buflen < 1 + RRFIXEDSZ + datalen)
470 + *cp++ = '\0'; /* no domain name */
471 + __putshort(type, cp);
473 + __putshort(class, cp);
477 + __putshort(datalen, cp);
480 + memcpy(cp, data, datalen);
483 + hp->ancount = htons(1);
493 +res_mkquery(int op, /* opcode of query */
494 + const char *dname, /* domain name */
495 + int class, int type, /* class and type of query */
496 + const u_char *data, /* resource record data */
497 + int datalen, /* length of data */
498 + const u_char *newrr_in, /* new rr for modify or append */
499 + u_char *buf, /* buffer to put query */
500 + int buflen) /* size of buffer */
502 + return (res_nmkquery(&_res, op, dname, class, type,
504 + newrr_in, buf, buflen));
509 void dorequest(char *s,int type,word id)