1 diff -Naur mtr-0.69.old/dns.c mtr-0.69.new/dns.c
2 --- mtr-0.69.old/dns.c 2005-01-11 09:32:42.000000000 +0100
3 +++ mtr-0.69.new/dns.c 2005-10-03 21:31:27.000000000 +0200
10 +static const char digits[] = "0123456789";
11 +#define __set_errno(e) (errno = (e))
13 +#define NS_PUT16(s, cp) do { \
14 + register u_int16_t t_s = (u_int16_t)(s); \
15 + register u_char *t_cp = (u_char *)(cp); \
16 + *t_cp++ = t_s >> 8; \
18 + (cp) += NS_INT16SZ; \
23 +#define NS_PUT32(l, cp) do { \
24 + register u_int32_t t_l = (u_int32_t)(l); \
25 + register u_char *t_cp = (u_char *)(cp); \
26 + *t_cp++ = t_l >> 24; \
27 + *t_cp++ = t_l >> 16; \
28 + *t_cp++ = t_l >> 8; \
30 + (cp) += NS_INT32SZ; \
35 +ns_put16(u_int src, u_char *dst) {
40 +ns_put32(u_long src, u_char *dst) {
44 +void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
45 +void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
49 + if (ch >= 0x41 && ch <= 0x5A)
56 +dn_find(const u_char *domain, const u_char *msg,
57 + const u_char * const *dnptrs,
58 + const u_char * const *lastdnptr)
60 + const u_char *dn, *cp, *sp;
61 + const u_char * const *cpp;
64 + for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
67 + * terminate search on:
69 + * compression pointer
72 + while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
73 + (sp - msg) < 0x4000) {
76 + while ((n = *cp++) != 0) {
78 + * check for indirection
80 + switch (n & NS_CMPRSFLGS) {
81 + case 0: /* normal case, n == len */
84 + for ((void)NULL; n > 0; n--)
85 + if (mklower(*dn++) !=
88 + /* Is next root for both ? */
89 + if (*dn == '\0' && *cp == '\0')
95 + case NS_CMPRSFLGS: /* indirection */
96 + cp = msg + (((n & 0x3f) << 8) | *cp);
99 + default: /* illegal type */
100 + __set_errno (EMSGSIZE);
108 + __set_errno (ENOENT);
114 +ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
115 + const u_char **dnptrs, const u_char **lastdnptr)
118 + const u_char **cpp, **lpp, *eob, *msg;
119 + const u_char *srcp;
120 + int n, l, first = 1;
124 + eob = dstp + dstsiz;
126 + if (dnptrs != NULL) {
127 + if ((msg = *dnptrs++) != NULL) {
128 + for (cpp = dnptrs; *cpp != NULL; cpp++)
130 + lpp = cpp; /* end of list to search */
135 + /* make sure the domain we are about to add is legal */
139 + if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
140 + __set_errno (EMSGSIZE);
146 + if (l > MAXCDNAME) {
147 + __set_errno (EMSGSIZE);
153 + /* from here on we need to reset compression pointer array on error */
156 + /* Look to see if we can use pointers. */
158 + if (n != 0 && n != 0x41 && msg != NULL) {
159 + l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
160 + (const u_char * const *)lpp);
162 + if (dstp + 1 >= eob) {
165 + *dstp++ = (l >> 8) | NS_CMPRSFLGS;
167 + return (dstp - dst);
169 + /* Not found, save it. */
170 + if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
171 + (dstp - msg) < 0x4000 && first) {
177 + /* copy label to buffer */
178 + if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) { /* Should not happen. */
183 + if (dstp + 1 >= eob)
187 + if (dstp + 1 + n >= eob) {
190 + memcpy(dstp, srcp, n + 1);
199 + __set_errno (EMSGSIZE);
202 + return (dstp - dst);
207 +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
208 + u_char *label, *bp, *eom;
214 + eom = dst + dstsiz;
217 + while ((c = *src++) != 0) {
219 + if ((cp = strchr(digits, c)) != NULL) {
220 + n = (cp - digits) * 100;
221 + if ((c = *src++) == 0 ||
222 + (cp = strchr(digits, c)) == NULL) {
223 + __set_errno (EMSGSIZE);
226 + n += (cp - digits) * 10;
227 + if ((c = *src++) == 0 ||
228 + (cp = strchr(digits, c)) == NULL) {
229 + __set_errno (EMSGSIZE);
232 + n += (cp - digits);
234 + __set_errno (EMSGSIZE);
238 + } else if (c == '[' && label == bp - 1 && *src == 'x') {
239 + /* Theoretically we would have to handle \[o
240 + as well but we do not since we do not need
245 + while (isxdigit (*src)) {
246 + n = *src > '9' ? *src - 'a' + 10 : *src - '0';
248 + if (! isxdigit(*src)) {
249 + __set_errno (EMSGSIZE);
253 + n += *src > '9' ? *src - 'a' + 10 : *src - '0';
254 + if (bp + 1 >= eom) {
255 + __set_errno (EMSGSIZE);
261 + *label = (bp - label - 1) * 8;
262 + if (*src++ != ']' || *src++ != '.') {
263 + __set_errno (EMSGSIZE);
269 + __set_errno (EMSGSIZE);
275 + } else if (c == '\\') {
278 + } else if (c == '.') {
279 + c = (bp - label - 1);
280 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
281 + __set_errno (EMSGSIZE);
284 + if (label >= eom) {
285 + __set_errno (EMSGSIZE);
289 + /* Fully qualified ? */
290 + if (*src == '\0') {
293 + __set_errno (EMSGSIZE);
298 + if ((bp - dst) > MAXCDNAME) {
299 + __set_errno (EMSGSIZE);
304 + if (c == 0 || *src == '.') {
305 + __set_errno (EMSGSIZE);
312 + __set_errno (EMSGSIZE);
317 + c = (bp - label - 1);
318 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
319 + __set_errno (EMSGSIZE);
322 + if (label >= eom) {
323 + __set_errno (EMSGSIZE);
329 + __set_errno (EMSGSIZE);
334 + if ((bp - dst) > MAXCDNAME) { /* src too big */
335 + __set_errno (EMSGSIZE);
344 +ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
345 + const u_char **dnptrs, const u_char **lastdnptr)
347 + u_char tmp[NS_MAXCDNAME];
349 + if (ns_name_pton(src, tmp, sizeof tmp) == -1)
351 + return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
356 +dn_comp(const char *src, u_char *dst, int dstsiz,
357 + u_char **dnptrs, u_char **lastdnptr)
359 + return (ns_name_compress(src, dst, (size_t)dstsiz,
360 + (const u_char **)dnptrs,
361 + (const u_char **)lastdnptr));
368 +res_nmkquery(res_state statp,
369 + int op, /* opcode of query */
370 + const char *dname, /* domain name */
371 + int class, int type, /* class and type of query */
372 + const u_char *data, /* resource record data */
373 + int datalen, /* length of data */
374 + const u_char *newrr_in, /* new rr for modify or append */
375 + u_char *buf, /* buffer to put query */
376 + int buflen) /* size of buffer */
378 + register HEADER *hp;
379 + register u_char *cp;
381 + u_char *dnptrs[20], **dpp, **lastdnptr;
384 + if (statp->options & RES_DEBUG)
385 + printf(";; res_nmkquery(%s, %s, %s, %s)\n",
386 + _res_opcodes[op], dname, p_class(class), p_type(type));
389 + * Initialize header fields.
391 + if ((buf == NULL) || (buflen < HFIXEDSZ))
393 + memset(buf, 0, HFIXEDSZ);
394 + hp = (HEADER *) buf;
395 + /* We randomize the IDs every time. The old code just
396 + incremented by one after the initial randomization which
397 + still predictable if the application does multiple
400 + hp->id = htons(++statp->id);
402 + hp->id = htons(statp->id);
407 + RANDOM_BITS (randombits);
410 + gettimeofday (&tv, NULL);
411 + randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
414 + while ((randombits & 0xffff) == 0);
415 + statp->id = (statp->id + randombits) & 0xffff;
418 + hp->rd = (statp->options & RES_RECURSE) != 0;
419 + hp->rcode = NOERROR;
420 + cp = buf + HFIXEDSZ;
421 + buflen -= HFIXEDSZ;
425 + lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
427 + * perform opcode specific processing
430 + case QUERY: /*FALLTHROUGH*/
432 + if ((buflen -= QFIXEDSZ) < 0)
434 + if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
438 + __putshort(type, cp);
440 + __putshort(class, cp);
442 + hp->qdcount = htons(1);
443 + if (op == QUERY || data == NULL)
446 + * Make an additional record for completion domain.
448 + buflen -= RRFIXEDSZ;
449 + n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
454 + __putshort(T_NULL, cp);
456 + __putshort(class, cp);
462 + hp->arcount = htons(1);
467 + * Initialize answer section
469 + if (buflen < 1 + RRFIXEDSZ + datalen)
471 + *cp++ = '\0'; /* no domain name */
472 + __putshort(type, cp);
474 + __putshort(class, cp);
478 + __putshort(datalen, cp);
481 + memcpy(cp, data, datalen);
484 + hp->ancount = htons(1);
494 +res_mkquery(int op, /* opcode of query */
495 + const char *dname, /* domain name */
496 + int class, int type, /* class and type of query */
497 + const u_char *data, /* resource record data */
498 + int datalen, /* length of data */
499 + const u_char *newrr_in, /* new rr for modify or append */
500 + u_char *buf, /* buffer to put query */
501 + int buflen) /* size of buffer */
503 + return (res_nmkquery(&_res, op, dname, class, type,
505 + newrr_in, buf, buflen));
510 void dorequest(char *s,int type,word id)