mrdunk / esp8266_mdns

mDNS queries and responses on esp8266
MIT License
77 stars 16 forks source link

How to see all hosts on network immediately? #1

Closed probonopd closed 8 years ago

probonopd commented 9 years ago

Is there a way to "ping" (not literally ping but the mdns-sd equivalent, it it exists) all services on the network, so that they are displayed immediately (rather than when they proactively send information about themselves)?

mrdunk commented 9 years ago

yes. this is an mDNS thing rather than anything to do with my code.

to find all services available on your network query for a PTR record with the special name "_services._dns-sd._udp.local". when you send such a query you should see all services on your network announce them selves.

the service query is actually already written but commented out in one of the examples: https://github.com/mrdunk/esp8266_mdns/blob/master/examples/mdns_test/mdns_test.ino search for the line "// Query for all service types on network."

note that the records returned from this query only tell you about the service, not the host that provides the service so you will then need to query again for each service you are interested in to get the host information. (run the service query and watch the results using WireShark if that sentence doesn't make sense.)

probonopd commented 9 years ago

Thanks mdunk. I get a lot of " Did not find "xxx._tcp.local" in hosts buffer" messages. Sorry if this is a stupid question, but is there an example sketch that resolves the IP address and/or provides information about the host as well?

mrdunk commented 9 years ago

in my answerCallback() function the " Did not find "xxx._tcp.local" in hosts buffer" messages will occur whenever you get a type "A" DNS record before you got a type "PTR" DNS record about the service name you are searching for.

this makes sense in my code because it presumes you are only interested in replies to queries for a specific service. you don't want that though. if you want the full map of all services provided by all hosts you'll need to write your own answerCallback() function.

try something like this for your answerCallback() to see just the available services:

void answerCallback(const mdns::Answer* answer) {
  if (answer->rrtype == MDNS_TYPE_PTR and 
      strstr(answer->name_buffer, "_services._dns-sd._udp.local") != 0){
    Serial.print(" Service name: ");
    Serial.printls(answer->rdata_buffer);
  }
}

once you make a list of services you then need to do what i did in the mdns_test example for each service.

mrdunk commented 9 years ago

question: do you actually need all the service on all the hosts? if it's just a list of hosts you need it may be simpler to advertise your own service type on all hosts and just query for that.

probonopd commented 9 years ago

Ah, got it! Thanks. Still I have the impression I have to wait for a long time until all services are "seen" by the sketch.

mrdunk commented 9 years ago

so from memory it only took a second or two for the service records to come in. on my network there were only a few hosts responding to mDNS queries when i tried this. (<10 hosts.)

it's worth using WireShark to confirm whether

  1. the "_services._dns-sd._udp.local" record is reaching the network.
  2. there are mDNS responses on the network.
probonopd commented 8 years ago

It appears to be working for me now, using the simple.ino sketch it looks like I am seeing the data flowing by in real time.