mDNS / DNS-SD

struct mg_mdns_req

// mDNS request
struct mg_mdns_req {
  struct mg_dns_rr *rr;
  struct mg_dnssd_record *r;
  struct mg_str reqname;   // requested name in RR
  struct mg_str respname;  // actual name to use in response
  struct mg_addr *addr;    // actual address to use in response
  bool is_listing;
  bool is_resp;
  bool is_unicast;
};

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;
  struct mg_str name;
  struct mg_addr addr;
};

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 use
  • fn - The event handler function, if any; can be NULL
  • fn_data - an arbitrary pointer, which will be stored in the connection structure as c->fn_data, so the event handler can use it when called; can be NULL

Return value: mDNS connection.

Server usage example:

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_data is 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:

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 listener
  • name - The host or service name to query
  • rtype - The type of record (MG_DNS_RTYPE_A, PTR, SRV, TXT) to request

Return value: success/failure

Usage example:

NOTE: currently only MG_DNS_RTYPE_A records are supported

struct mg_dnssd_record

// DNS-SD response record
struct mg_dnssd_record {
  struct mg_str srvcproto;  // service.proto, service name
  struct mg_str txt;        // TXT record contents
  uint16_t port;            // SRV record port
};

Structure used to tell mDNS how to answer to PTR, TXT and SRV record requests, so implementing a DNS-SD service

Usage example: