mjansson / mdns

Public domain mDNS/DNS-SD library in C
The Unlicense
459 stars 120 forks source link

Howto receive discovery and query answers at the same time? #70

Closed stefbon closed 1 year ago

stefbon commented 1 year ago

Hi,

I'm using the library in my software, and so far it works very fine. I've added the network socket to send and receive the mdns messages with to an eventloop, and it works very good.

Now howto deal with a situation that one host has send a discovery answer and your server/host has send a query, and another host hasn't even send an answer to the discovery message. In my eventloop, to process a reply, for one message the function mdns_query_recv has to be used, for the other mdns_discovery_recv. How to know what to use (since you cannot know what the message is...)?

Use a sequence number per remote fromaddress (starting at zero), and when it's zero, it must be a discovery reply, in all other cases it's "normal" query?

Stef

mjansson commented 1 year ago

It depends on what type of message you send in the first place, if you send mdns_discovery_send to do DNS-SD (service discovery, i.e query the _services._dns-sd._udp.local. address) you should use mdns_discovery_recv to get all the answers with PTR records to available services, like PTR _http._tcp.local. rclass 0x1 ttl 10 length 8.

If you send a query for a service or a specific record with mdns_query_send you should read the answers with mdns_query_recv, for example sending a query for the PTR record for _http._tcp.local. you will get back the associated SRV, A, AAA, TXT and other records for each instance of that service on the local network.

The difference between the functions are small, the mdns_discovery_recv is a convenience function as it does filtering to only feed service discovery answers back to the callback, while if you would use mdns_query_recv for reading service discovery answers you would have to do that yourself in the callback.

stefbon commented 1 year ago

Thanks a lot. I already started using mdns_query_recv in all cases, also after a discovery is send. Indeed by looking ar the name section (in the query_callback entrystr) it is possible to find out what this answer is for.

Stef