mDNS / DNS-SD
struct mg_mdns_req
// mDNS request
struct mg_mdns_req {
struct mg_dns_rr *rr; // Parsed resource record from the incoming query
struct mg_dnssd_record *r; // User-supplied service record to include in the response
struct mg_str reqname; // Queried hostname, without the .local suffix
struct mg_str respname; // Hostname to use in response; defaults to fn_data if empty
struct mg_addr *addr; // IP address for A record; uses local interface if NULL
bool is_listing; // True if this is a service-discovery listing (_services._dns-sd._udp)
bool is_resp; // Set to true in the handler to trigger a response
bool is_unicast; // True if the client requested a unicast (QU) response
};
Structure pointed to by ev_data on an mDNS event handler function for an MG_EV_MDNS_REQ event.
DNS-SD users will store a valid pointer to a struct mg_dnssd_record in the r field, based on the requested name available in reqname.
struct mg_mdns_resp
struct mg_mdns_resp {
struct mg_dns_rr *rr; // Resource record from the response (1st in chain)
struct mg_str name; // Resolved hostname, without the .local suffix
struct mg_addr addr; // Resolved IP address
struct mg_dnssd_record sd; // Service Discovery data
};
Structure pointed to by ev_data on an mDNS event handler function for an MG_EV_MDNS_RESP event.
mg_mdns_listen()
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data);
Create an mDNS listener/responder [for the given hostname].
Parameters:
c- Connection to usefn- The event handler function, if any; can be NULLfn_data- an arbitrary pointer, which will be stored in the connection structure asc->fn_data, so the event handler can use it when called; can be NULL
Return value: mDNS connection.
Server usage example:
- see mdns-server
If fn_data is passed, then it is a pointer to a buffer that holds a NUL-terminated hostname string. Must be valid during the connection lifetime.
Mongoose accepts only name queries for that host name, answering by itself; regardless of an event handler function.
If an event handler function is passed, this function is called for mDNS requests with an MG_EV_MDNS_REQ event, as follows:
- If
fn_datais passed, only those requests (name or service queries) for that host name are processed, as described above. This can be used to implement a DNS-SD listener/responder for a typical host, using mDNS. - Otherwise, all mDNS requests trigger the event handler. This can be used, for example, to support several host names on a device.
Client usage example:
- see mdns-client
An event handler function is not always required. If passed, this function will be called for mDNS responses, with an MG_EV_MDNS_RESP event. Users will then compare the received information to what they're looking for. However, if you just want to have a listener so Mongoose resolver can resolve '.local' address via mDNS, you will pass NULL as argument.
mg_mdns_query()
bool mg_mdns_query(struct mg_connection *c, const char *name, unsigned int rtype);
Issue an mDNS request of type rtype for the given name. Any responses will be handled by an event handler registered at opening a listener, by calling mg_mdns_listen.
Parameters:
c- Connection to use, returned when opening the listenername- The host or service name to queryrtype- The type of record (MG_DNS_RTYPE_A, PTR, SRV, TXT) to request
Return value: success/failure
Usage example:
- see mdns-client
struct mg_dnssd_record
// DNS-SD response record
struct mg_dnssd_record {
struct mg_str srvcproto; // Service and protocol label, e.g. "_http._tcp"
struct mg_str txt; // TXT record contents, verbatim
uint16_t port; // Port number for the SRV record
};
Structure used to tell mDNS how to answer to PTR, TXT and SRV record requests, so implementing a DNS-SD service. It is also present on a response to a DNS-SD query
Server usage example:
- see mdns-sd-server
Client usage example:
- see mdns-sd-client