45 #define IPPORT_DOMAIN 53
46 #define MAX_DNS_BUF_SIZE 512
63 #define FORMAT_ERROR 1
74 static uint8 * put16(uint8 *s, uint16 i)
82 static uint16 get16(uint8 *s)
92 static int16 dns_makequery(uint16 op, int8 *name, uint8 *buf, uint16 len)
96 int8 sname[MAX_DNS_BUF_SIZE];
100 static uint16 MSG_ID = 0x1122;
105 cp = put16(cp, MSG_ID);
106 p = (op << 11) | 0x0100;
113 strcpy((
char*)sname, (
char*)name);
115 dlen = strlen((
char*)dname);
119 cp1 = (int8*)strchr((
char*)dname,
'.');
121 if (cp1 != NULL) len = cp1 - dname;
128 strncpy((
char*)cp, (
char*)dname, len);
139 cp = put16(cp, 0x0001);
140 cp = put16(cp, 0x0001);
142 return ((int16)(cp - buf));
145 static int32 parse_name(uint8 *msg, uint8 *compressed, int8 *buf, int16 len)
159 if (!indirect) clen++;
161 if ((slen & 0xc0) == 0xc0)
167 cp = &msg[((slen & 0x3f)<<8) + *cp];
176 if (len < 0)
return -1;
178 if (!indirect) clen += slen;
180 while (slen-- != 0) *buf++ = (int8)*cp++;
198 static uint8 * dns_question(uint8 *msg, uint8 *cp)
201 int8 name[MAX_DNS_BUF_SIZE];
203 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
205 DBGA(
"dns_question : %s", name);
207 if (len == -1)
return 0;
216 static uint8 * dns_answer(uint8 *msg, uint8 *cp, uint8 *pSip)
219 int8 name[MAX_DNS_BUF_SIZE];
221 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
223 if (len == -1)
return 0;
232 DBGA(
"dns_answer : %s : %u : %.4x", name, len, type);
260 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
261 if (len == -1)
return 0;
275 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
276 if (len == -1)
return 0;
282 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
283 if (len == -1)
return 0;
288 len = parse_name(msg, cp, name, MAX_DNS_BUF_SIZE);
289 if (len == -1)
return 0;
310 static int8 parseMSG(
struct dhdr *pdhdr, uint8 *pbuf, uint8 *pSip)
318 memset(pdhdr, 0,
sizeof(pdhdr));
320 pdhdr->id = get16(&msg[0]);
321 tmp = get16(&msg[2]);
322 if (tmp & 0x8000) pdhdr->qr = 1;
324 pdhdr->opcode = (tmp >> 11) & 0xf;
326 if (tmp & 0x0400) pdhdr->aa = 1;
327 if (tmp & 0x0200) pdhdr->tc = 1;
328 if (tmp & 0x0100) pdhdr->rd = 1;
329 if (tmp & 0x0080) pdhdr->ra = 1;
331 pdhdr->rcode = tmp & 0xf;
332 pdhdr->qdcount = get16(&msg[4]);
333 pdhdr->ancount = get16(&msg[6]);
334 pdhdr->nscount = get16(&msg[8]);
335 pdhdr->arcount = get16(&msg[10]);
337 DBGA(
"dhdr->qdcount : %x", (uint16)pdhdr->qdcount);
338 DBGA(
"dhdr->ancount : %x", (uint16)pdhdr->ancount);
339 DBGA(
"dhdr->nscount : %x", (uint16)pdhdr->nscount);
340 DBGA(
"dhdr->arcount : %x", (uint16)pdhdr->arcount);
346 for (i = 0; i < pdhdr->qdcount; i++)
348 cp = dns_question(msg, cp);
352 for (i = 0; i < pdhdr->ancount; i++)
354 cp = dns_answer(msg, cp, pSip);
358 for (i = 0; i < pdhdr->nscount; i++)
364 for (i = 0; i < pdhdr->arcount; i++)
369 if(pdhdr->rcode == 0)
return RET_OK;
387 uint8 ip_tmp[4], i = 0;
391 static uint8 dns_buf[MAX_DNS_BUF_SIZE];
397 DBGA(
"UDPOpen fail (%d)times", i);
404 len = dns_makequery(0, (int8 *)domain, dns_buf, MAX_DNS_BUF_SIZE);
405 while((cnt =
UDPSend(sock, (int8*)dns_buf, len, netinfo.dns, IPPORT_DOMAIN)) < 0) {
407 DBGA(
"UDPSend fail (%d)times", i);
415 if((len =
UDPRecv(sock, (int8*)dns_buf, MAX_DNS_BUF_SIZE, ip_tmp, &port)) > 0) {
426 return parseMSG(&dhp, dns_buf, ip);