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 in response
  struct mg_addr addr;
  bool is_listing;
  bool is_resp;
  bool is_unicast;
};

Structure pointed to by ev_data on an mDNS event handler function.

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.

mg_mdns_listen()

struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn, void *fn_data);

Create 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.

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.

NOTE: If a connection creates successfully, and an external TCP/IP stack is used rather than our built-in stack, then the first 4 bytes of c->data should have an IPv4 host address.

Usage example:

If an event handler function is passed, this function is called for mDNS requests, 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 traffic triggers the event handler. This can be used, for example, to support several host names on a device.

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: