Example Responder

The tests/mdns_example.c source file included in the package provides a complete, simple, real-world example of the mDNS Responder usage. It registers two services, a dummy (port 9) service _workstation._tcp.local and a HTTP daemon using service _http._tcp.local on port 80.

Normally the first task after the lwIP networking has been initialised is to call the C function:

cyg_bool cyg_mdns_init(void);

This initialises the core of the mDNS support. After the mDNS Responder has been initialised, services can be registered, and the network interfaces enabled as required.

The following is a very simple example of declaring a <hostname>._http._tcp.local service for the <hostname>.local address, where <hostname> is the default as configured by the CYGDAT_NET_MDNS_HOSTNAME CDL option.

static const cyg_uint8 label_http[] = { 0x05,'_','h','t','t','p' };

static const cyg_mdns_service_identity httpd80 = {
  .service = label_http,
  .proto = cyg_mdns_label_tcp,
  .port = 80
};

static const struct cyg_mdns_service httpd80_service = {
  .id = &httpd80,
  .txt_vector = NULL,
  .txt_count = 0,
};

...

if (cyg_mdns_init()) {
  if (cyg_mdns_service_register(1,(struct mdns_service *)&httpd80_service)) {
    if (cyg_mdns_enable()) {
      // other application processing as needed ...
    } else {
      (void)cyg_mdns_service_unregister(1,(struct mdns_service *)&httpd80_service);
      // report error
    }
  } else {
    cyg_mdns_terminate();
    // report error
  }
} else {
  // report error
}
    

2017-02-09
Documentation license for this page: eCosPro License