This commit is contained in:
2024-10-11 06:10:24 +06:00
parent 8d0606462d
commit 555abf9503
31 changed files with 59 additions and 16267 deletions

View File

@@ -117,7 +117,7 @@ struct dhdr
uint8_t* pDNSMSG; // DNS message buffer
uint8_t DNS_SOCKET; // SOCKET number for DNS
uint16_t DNS_MSGID; // DNS message ID
volatile uint16_t DNS_MSGID; // DNS message ID
uint32_t dns_1s_tick; // for timout of DNS processing
static uint8_t retry_count;
@@ -368,7 +368,7 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
{
cp = dns_question(msg, cp);
#ifdef _DNS_DEUBG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
#endif
if(!cp) return -1;
}
@@ -378,7 +378,7 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
{
cp = dns_answer(msg, cp, ip_from_dns);
#ifdef _DNS_DEUBG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
#endif
if(!cp) return -1;
}
@@ -399,7 +399,6 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
else return 0;
}
/*
* MAKE DNS QUERY MESSAGE
*
@@ -410,20 +409,26 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
* len - is the MAX. size of buffer.
* Returns : the pointer to the DNS message.
*/
int16_t dns_makequery(uint16_t op, char * name, uint8_t * buf, uint16_t len)
{
uint8_t *cp;
// #pragma GCC push_options
// #pragma GCC optimize("O0")
int16_t dns_makequery(uint16_t op, char *name, uint8_t *buf, uint16_t len) {
uint8_t *cp = buf;
char *cp1;
char sname[MAXCNAME];
char *dname;
uint16_t p;
uint16_t dlen;
cp = buf;
// Ensuring name fits into sname
if (strlen(name) >= MAXCNAME) {
// Handle error - name too long
printf("Error: Name too long.\n");
return -1;
}
DNS_MSGID++;
cp = put16(cp, DNS_MSGID);
p = (op << 11) | 0x0100; /* Recursion desired */
p = (op << 11) | 0x0100; // Recursion desired
cp = put16(cp, p);
cp = put16(cp, 1);
cp = put16(cp, 0);
@@ -433,34 +438,38 @@ int16_t dns_makequery(uint16_t op, char * name, uint8_t * buf, uint16_t len)
strcpy(sname, name);
dname = sname;
dlen = strlen(dname);
for (;;)
{
/* Look for next dot */
cp1 = strchr(dname, '.');
if (cp1 != NULL) len = cp1 - dname; /* More to come */
else len = dlen; /* Last component */
for (;;) {
cp1 = strchr(dname, '.'); // Look for next dot
uint16_t component_len = cp1 != NULL ? cp1 - dname : dlen;
*cp++ = len; /* Write length of component */
if (len == 0) break;
*cp++ = component_len; // Write length of component
if (component_len == 0) break; // Handle empty component
/* Copy component up to (but not including) dot */
strncpy((char *)cp, dname, len);
cp += len;
if (cp1 == NULL)
{
*cp++ = 0; /* Last one; write null and finish */
memcpy(cp, dname, component_len); // Copy component up to (but not including) dot
cp += component_len;
if (cp1 == NULL) {
*cp++ = 0; // Last one; write null terminator
break;
}
dname += len+1;
dlen -= len+1;
dname += component_len + 1; // Move past current component and dot
dlen -= component_len + 1;
// Ensures dlen is still valid
if (dlen <= 0) {
printf("Error: dlen is non-positive.\n");
return -1;
}
}
cp = put16(cp, 0x0001); /* type */
cp = put16(cp, 0x0001); /* class */
cp = put16(cp, 0x0001); // type
cp = put16(cp, 0x0001); // class
return ((int16_t)((uint32_t)(cp) - (uint32_t)(buf)));
return (int16_t)((uint32_t)(cp) - (uint32_t)(buf));
}
// #pragma GCC pop_options
/*
* CHECK DNS TIMEOUT
@@ -526,17 +535,17 @@ int8_t DNS_run(uint8_t * dns_ip, uint8_t * name, uint8_t * ip_from_dns)
{
if (len > MAX_DNS_BUF_SIZE) len = MAX_DNS_BUF_SIZE;
len = recvfrom(DNS_SOCKET, pDNSMSG, len, ip, &port);
#ifdef _DNS_DEBUG_
printf("> Receive DNS message from %d.%d.%d.%d(%d). len = %d\r\n", ip[0], ip[1], ip[2], ip[3],port,len);
#ifdef _DNS_DEBUG_
printf("> Receive DNS message from %d.%d.%d.%d(%d). len = %d\r\n", ip[0], ip[1], ip[2], ip[3],port,len);
// Print the partial DNS message
printf("> Partial DNS message: ");
for (int i = 0; i < len; i++) {
printf("%02X ", pDNSMSG[i]); // Print each byte in hexadecimal format
}
printf("\r\n");
#endif
ret = parseDNSMSG(&dhp, pDNSMSG, ip_from_dns);
printf("> Partial DNS message: ");
for (int i = 0; i < len; i++) {
printf("%02X ", pDNSMSG[i]); // Print each byte in hexadecimal format
}
printf("\r\n");
#endif
ret = parseDNSMSG(&dhp, pDNSMSG, ip_from_dns);
break;
}
// Check Timeout