fix: wiznet bloatlib

This commit is contained in:
2024-10-11 20:08:06 +06:00
parent 0a6948ad59
commit 259d63197e
6 changed files with 397 additions and 100 deletions

View File

@@ -59,8 +59,13 @@
#include <stdio.h>
#endif
// DNS protocol constants
#define INITRTT 2000L /* Initial smoothed response time */
#define MAXCNAME (MAX_DOMAIN_NAME + (MAX_DOMAIN_NAME>>1)) /* Maximum amount of cname recursion */
#define DNS_HEADER_LEN 12 /* Length of the DNS header in bytes */
#define QTYPE_A 0x0001 /* Query type for IPv4 address resolution (A record) */
#define QCLASS_IN 0x0001 /* Class for internet (IN) queries, indicating the DNS class of the query */
#define MAX_LABEL_LENGTH 63 /* Maximum length of a DNS label (part of a domain name) in bytes */
#define TYPE_A 1 /* Host address */
#define TYPE_NS 2 /* Name server */
@@ -123,20 +128,17 @@ uint32_t dns_1s_tick; // for timout of DNS processing
static uint8_t retry_count;
/* converts uint16_t from network buffer to a host byte order integer. */
uint16_t get16(uint8_t * s)
uint16_t get16(const uint8_t *s)
{
uint16_t i;
i = *s++ << 8;
i = i + *s;
return i;
return (uint16_t)s[0] << 8 | s[1];
}
/* copies uint16_t to the network buffer with network byte order. */
uint8_t * put16(uint8_t * s, uint16_t i)
uint8_t *put16(uint8_t *s, uint16_t i)
{
*s++ = i >> 8;
*s++ = i;
return s;
s[0] = (uint8_t)(i >> 8);
s[1] = (uint8_t)(i & 0xFF);
return s + 2;
}
@@ -332,71 +334,65 @@ uint8_t * dns_answer(uint8_t * msg, uint8_t * cp, uint8_t * ip_from_dns)
* 0 - Fail (Timout or parse error)
* 1 - Success,
*/
int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
int8_t parseDNSMSG(struct dhdr *pdhdr, uint8_t *pbuf, uint8_t *ip_from_dns)
{
uint16_t tmp;
uint16_t i;
uint8_t * msg;
uint8_t * cp;
uint16_t flags;
uint8_t *msg = pbuf;
uint8_t *cp;
msg = pbuf;
memset(pdhdr, 0, sizeof(*pdhdr));
pdhdr->id = get16(&msg[0]);
tmp = get16(&msg[2]);
if (tmp & 0x8000) pdhdr->qr = 1;
flags = get16(&msg[2]);
pdhdr->opcode = (tmp >> 11) & 0xf;
pdhdr->qr = (flags & 0x8000) != 0;
pdhdr->opcode = (flags >> 11) & 0xF;
pdhdr->aa = (flags & 0x0400) != 0;
pdhdr->tc = (flags & 0x0200) != 0;
pdhdr->rd = (flags & 0x0100) != 0;
pdhdr->ra = (flags & 0x0080) != 0;
pdhdr->rcode = flags & 0xF;
if (tmp & 0x0400) pdhdr->aa = 1;
if (tmp & 0x0200) pdhdr->tc = 1;
if (tmp & 0x0100) pdhdr->rd = 1;
if (tmp & 0x0080) pdhdr->ra = 1;
pdhdr->rcode = tmp & 0xf;
pdhdr->qdcount = get16(&msg[4]);
pdhdr->ancount = get16(&msg[6]);
pdhdr->nscount = get16(&msg[8]);
pdhdr->arcount = get16(&msg[10]);
/* Now parse the variable length sections */
cp = &msg[12];
/* Question section */
for (i = 0; i < pdhdr->qdcount; i++)
{
// Question section
for (uint16_t i = 0; i < pdhdr->qdcount; i++) {
cp = dns_question(msg, cp);
#ifdef _DNS_DEBUG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
#endif
if(!cp) return -1;
if (!cp) {
#ifdef _DNS_DEBUG_
printf("Error: Question section parsing failed.\n");
#endif
return -1;
}
}
/* Answer section */
for (i = 0; i < pdhdr->ancount; i++)
{
// Answer section
for (uint16_t i = 0; i < pdhdr->ancount; i++) {
cp = dns_answer(msg, cp, ip_from_dns);
#ifdef _DNS_DEBUG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
#endif
if(!cp) return -1;
if (!cp) {
#ifdef _DNS_DEBUG_
printf("Error: Answer section parsing failed.\n");
#endif
return -1;
}
}
/* Name server (authority) section */
for (i = 0; i < pdhdr->nscount; i++)
{
;
// Name server (authority) section
for (uint16_t i = 0; i < pdhdr->nscount; i++) {
// Process name server records if needed
}
/* Additional section */
for (i = 0; i < pdhdr->arcount; i++)
{
;
// Additional section
for (uint16_t i = 0; i < pdhdr->arcount; i++) {
// Process additional records if needed
}
if(pdhdr->rcode == 0) return 1; // No error
else return 0;
return (pdhdr->rcode == 0) ? 1 : 0;
}
/*
@@ -411,69 +407,71 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
*/
// #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;
// Ensuring name fits into sname
if (strlen(name) >= MAXCNAME) {
// Handle error - name too long
int16_t dns_makequery(uint16_t op, const char *name, uint8_t *buf, uint16_t len) {
// buffer and length check.
if (strlen(name) >= MAXCNAME || len < DNS_HEADER_LEN) {
#ifdef _DNS_DEBUG_
printf("Error: Name too long.\n");
printf("Error: Name too long or buffer too small.\n");
#endif
return -1;
return -1;
}
uint8_t *cp = buf;
uint16_t total_length = 0;
// increment message ID
DNS_MSGID++;
cp = put16(cp, DNS_MSGID);
p = (op << 11) | 0x0100; // Recursion desired
cp = put16(cp, p);
cp = put16(cp, 1);
cp = put16(cp, 0);
cp = put16(cp, 0);
cp = put16(cp, 0);
cp = put16(cp, (op << 11) | 0x0100); // flags with recursion desired
cp = put16(cp, 1); // questions count
cp = put16(cp, 0); // answer RRs
cp = put16(cp, 0); // authority RRs
cp = put16(cp, 0); // additional RRs
total_length += DNS_HEADER_LEN;
strcpy(sname, name);
dname = sname;
dlen = strlen(dname);
const char* ptr = name;
while (*ptr) {
const char* dot = strchr(ptr, '.');
uint16_t label_len = dot ? (dot - ptr) : (int)strlen(ptr);
for (;;) {
cp1 = strchr(dname, '.'); // Look for next dot
uint16_t component_len = cp1 != NULL ? cp1 - dname : dlen;
*cp++ = component_len; // Write length of component
if (component_len == 0) break; // Handle empty component
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 += component_len + 1; // Move past current component and dot
dlen -= component_len + 1;
// Ensures dlen is still valid
if (dlen <= 0) {
// check label length and buffer capacity
if (label_len > MAX_LABEL_LENGTH || total_length + label_len + 2 > len) {
#ifdef _DNS_DEBUG_
printf("Error: dlen is non-positive.\n");
printf("Error: Domain part too long or buffer too small.\n");
#endif
return -1;
}
*cp++ = (uint8_t)label_len;
memcpy(cp, ptr, label_len);
cp += label_len;
total_length += label_len + 1;
// move past current label
ptr += label_len;
if (*ptr == '.') {
ptr++;
}
}
cp = put16(cp, 0x0001); // type
cp = put16(cp, 0x0001); // class
// null terminate the domain name section
*cp++ = 0;
total_length++;
return (int16_t)((uint32_t)(cp) - (uint32_t)(buf));
// add QTYPE and QCLASS to the buffer
if (total_length + 4 > len) {
#ifdef _DNS_DEBUG_
printf("Error: Buffer too small for QTYPE and QCLASS.\n");
#endif
return -1;
}
cp = put16(cp, QTYPE_A); // QTYPE for A record
cp = put16(cp, QCLASS_IN); // QCLASS for IN (Internet)
total_length += 4;
return total_length;
}
// #pragma GCC pop_options
/*
* CHECK DNS TIMEOUT

View File

@@ -60,7 +60,7 @@ extern "C" {
* @brief Define it for Debug & Monitor DNS processing.
* @note If defined, it dependens on <stdio.h>
*/
// #define _DNS_DEBUG_
#define _DNS_DEBUG_
#define MAX_DNS_BUF_SIZE 256 ///< maximum size of DNS buffer. */
/*