1#include <resolv.h>
2#include <string.h>
3#include <time.h>
4#include "libc.h"
5
6int __res_mkquery(int op, const char *dname, int class, int type,
7	const unsigned char *data, int datalen,
8	const unsigned char *newrr, unsigned char *buf, int buflen)
9{
10	int id, i, j;
11	unsigned char q[280];
12	struct timespec ts;
13	size_t l = strnlen(dname, 255);
14	int n;
15
16	if (l && dname[l-1]=='.') l--;
17	n = 17+l+!!l;
18	if (l>253 || buflen<n || op>15u || class>255u || type>255u)
19		return -1;
20
21	/* Construct query template - ID will be filled later */
22	memset(q, 0, n);
23	q[2] = op*8 + 1;
24	q[5] = 1;
25	memcpy((char *)q+13, dname, l);
26	for (i=13; q[i]; i=j+1) {
27		for (j=i; q[j] && q[j] != '.'; j++);
28		if (j-i-1u > 62u) return -1;
29		q[i-1] = j-i;
30	}
31	q[i+1] = type;
32	q[i+3] = class;
33
34	/* Make a reasonably unpredictable id */
35	clock_gettime(CLOCK_REALTIME, &ts);
36	id = ts.tv_nsec + ts.tv_nsec/65536UL & 0xffff;
37	q[0] = id/256;
38	q[1] = id;
39
40	memcpy(buf, q, n);
41	return n;
42}
43
44weak_alias(__res_mkquery, res_mkquery);
45