63 #define MAXCNAME (MAX_DOMAIN_NAME + (MAX_DOMAIN_NAME>>1))
107 #define FORMAT_ERROR 1
108 #define SERVER_FAIL 2
124 static uint8_t retry_count;
136 uint8_t *
put16(uint8_t * s, uint16_t i)
154 int parse_name(uint8_t * msg, uint8_t * compressed,
char * buf, int16_t len)
168 if (!indirect) clen++;
170 if ((slen & 0xc0) == 0xc0)
176 cp = &msg[((slen & 0x3f)<<8) + *cp];
185 if (len < 0)
return -1;
187 if (!indirect) clen += slen;
189 while (slen-- != 0) *buf++ = (char)*cp++;
223 if (len == -1)
return 0;
241 uint8_t *
dns_answer(uint8_t * msg, uint8_t * cp, uint8_t * ip_from_dns)
248 if (len == -1)
return 0;
262 ip_from_dns[0] = *cp++;
263 ip_from_dns[1] = *cp++;
264 ip_from_dns[2] = *cp++;
265 ip_from_dns[3] = *cp++;
268 ip_from_dns[0] = *cp++;
269 ip_from_dns[1] = *cp++;
270 ip_from_dns[2] = *cp++;
271 ip_from_dns[3] = *cp++;
272 ip_from_dns[4] = *cp++;
273 ip_from_dns[5] = *cp++;
274 ip_from_dns[6] = *cp++;
275 ip_from_dns[7] = *cp++;
276 ip_from_dns[8] = *cp++;
277 ip_from_dns[9] = *cp++;
278 ip_from_dns[10] = *cp++;
279 ip_from_dns[11] = *cp++;
280 ip_from_dns[12] = *cp++;
281 ip_from_dns[13] = *cp++;
282 ip_from_dns[14] = *cp++;
283 ip_from_dns[15] = *cp++;
294 if (len == -1)
return 0;
309 if (len == -1)
return 0;
316 if (len == -1)
return 0;
322 if (len == -1)
return 0;
363 memset(pdhdr, 0,
sizeof(*pdhdr));
366 tmp =
get16(&msg[2]);
367 if (tmp & 0x8000) pdhdr->
qr = 1;
369 pdhdr->
opcode = (tmp >> 11) & 0xf;
371 if (tmp & 0x0400) pdhdr->
aa = 1;
372 if (tmp & 0x0200) pdhdr->
tc = 1;
373 if (tmp & 0x0100) pdhdr->
rd = 1;
374 if (tmp & 0x0080) pdhdr->
ra = 1;
376 pdhdr->
rcode = tmp & 0xf;
387 for (i = 0; i < pdhdr->
qdcount; i++)
391 printf(
"MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
397 for (i = 0; i < pdhdr->
ancount; i++)
401 printf(
"MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
407 for (i = 0; i < pdhdr->
nscount; i++)
413 for (i = 0; i < pdhdr->
arcount; i++)
418 if(pdhdr->
rcode == 0)
return 1;
433 int16_t
dns_makequery(uint16_t op,
char * name, uint8_t * buf, uint16_t len,uint8_t type)
446 p = (op << 11) | 0x0100;
455 dlen = strlen(dname);
459 cp1 = strchr(dname,
'.');
461 if (cp1 != NULL) len = cp1 - dname;
468 strncpy((
char *)cp, dname, len);
481 cp =
put16(cp, type);
483 cp =
put16(cp, 0x0001);
486 return ((int16_t)((uint32_t)(cp) - (uint32_t)(buf)));
526 int8_t
DNS_run(uint8_t s,uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns,uint8_t mode)
533 int8_t ret_check_timeout;
550 printf(
"> DNS Query to DNS Server : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]);
564 printf(
"> Receive DNS message from %d.%d.%d.%d(%d). len = %d\r\n", ip[0], ip[1], ip[2], ip[3],port,len);
571 if (ret_check_timeout < 0) {
574 printf(
"> DNS Server is not responding : %d.%d.%d.%d\r\n", dns_ip[0], dns_ip[1], dns_ip[2], dns_ip[3]);
578 else if (ret_check_timeout == 0) {
581 printf(
"> DNS Timeout\r\n");