adafruit / Adafruit_CircuitPython_Wiznet5k

Pure-Python interface for WIZNET 5k Ethernet modules
Other
15 stars 37 forks source link

DNS server raises TypeError exception in C Python although it runs in CP #71

Closed BiffoBear closed 2 years ago

BiffoBear commented 2 years ago

In _build_dns_question() the line self._pkt_buf += host[i] concatenates a str onto a bytearray which raises a TypeError exception in C Python but works in CircuitPython (Thanks to @Neradoc for checking my sanity).

One simple fix is self._pkt_buf += bytes(host[i], "utf-8")

Code snippet with bug below.

adafruit_wiznet5k/adafruit_wiznet5k_dns.py

class DNS:
    """W5K DNS implementation."""
    def __init__(
        self,
        iface: WIZNET5K,
        dns_address: Union[str, Tuple[int, int, int, int]],
        debug: bool = False,
    ) -> None:

        ...
        self._pkt_buf = bytearray()

        ...

    def _build_dns_question(self) -> None:
        """Build a DNS query."""
        host = self._host.decode("utf-8")
        host = host.split(".")
        # write out each section of host
        for i, _ in enumerate(host):
            # append the sz of the section
            self._pkt_buf.append(len(host[i]))
            # append the section data
            self._pkt_buf += host[i]
        # end of the name
        self._pkt_buf.append(0x00)
        # Type A record
        self._pkt_buf.append(htons(TYPE_A) & 0xFF)
        self._pkt_buf.append(htons(TYPE_A) >> 8)
        # Class IN
        self._pkt_buf.append(htons(CLASS_IN) & 0xFF)
        self._pkt_buf.append(htons(CLASS_IN) >> 8)