arduino-libraries / ArduinoMDNS

mDNS Library for Arduino
39 stars 13 forks source link

Service Discovery returns garbage data #20

Open gigaj0ule opened 1 year ago

gigaj0ule commented 1 year ago

When I use one arduino to generate an MDNS service, and another arduino to listen to the service...

  1. The service name is OK
  2. The IP address is OK, but sometimes, it's 0.0.0.0?
  3. The port is not the same port advertised by the arduino? Sometimes it is a random number.
  4. The text content is arbitrary garbage

It seems like adding the service record is OK, as on my host computer, I don't have issues reading the text data,

...but the service discovery on arduino is very broken?

gigaj0ule commented 1 year ago

ah, the randomness was because our arrays were never initialized

         uint8_t* ptrNames[MDNS_MAX_SERVICES_PER_PACKET] ;
         uint16_t ptrOffsets[MDNS_MAX_SERVICES_PER_PACKET];
         uint16_t ptrPorts[MDNS_MAX_SERVICES_PER_PACKET];

changing them to

         uint8_t* ptrNames[MDNS_MAX_SERVICES_PER_PACKET] = {0};
         uint16_t ptrOffsets[MDNS_MAX_SERVICES_PER_PACKET] = {0};
         uint16_t ptrPorts[MDNS_MAX_SERVICES_PER_PACKET] = {0};

gives me port "0" instead of randomness, so indeed the packet parsing code is broken somewhere...

gigaj0ule commented 1 year ago

I think I found the source of the bug now.

There is simply no code at all that populates ptrPorts or servTxt until we get to the "additional records" part of the packet.

But, port and text are not "additional records". They are "query answers".

This is a pretty big control flow problem and I am not sure how to fix it yet since the code is so hard to understand still.

gigaj0ule commented 1 year ago

After spending 12 hours digging into the code, I don't think there is any way for an arduino to ask for TXT query.

I was watching the raw packet data coming in through the UDP buffer and nothing seemed to me like a txt response.

--wait--, I just saw the strings.

i'll try writing a packet filter to capture it

gigaj0ule commented 1 year ago

I rewrote the entire service browser and now it works

image

Basically i made a whole bunch of packet filters that search for strings just like wireshark.

I also got rid of dynamic memory allocation, as it was too complex for me to follow

There are a lot of changes so I should make a fork maybe