Skip to content

Latest commit

 

History

History

DNSServiceDiscovery

page_type languages products name urlFragment extendedZipContent description
sample
c
azure
azure-sphere
Azure Sphere – DNS service discovery
DNSServiceDiscovery
path target
.clang-format
.clang-format
path target
BUILD_INSTRUCTIONS.md
BUILD_INSTRUCTIONS.md
path target
Samples/SECURITY.md
SECURITY.md
path target
Samples/troubleshooting.md
troubleshooting.md
Demonstrates how to perform service discovery on the local network by using multicast DNS (mDNS).

Sample: DNS service discovery

This sample demonstrates how to perform DNS service discovery by sending DNS-SD queries to the local network using multicast DNS (mDNS).

The application queries the local network for PTR records that identify all instances of the _sample-service._tcp service. The application then queries the network for the SRV, TXT, and A records that contain the DNS details for each service instance. After service discovery is performed, the Azure Sphere firewall allows the application to connect to the discovered host names.

The sample uses the following Azure Sphere libraries.

Library Purpose
eventloop Invokes handlers for timer events.
log Displays messages in the Device Output window during debugging.
networking Manages network connectivity.

Contents

File/folder Description
app_manifest.json Application manifest file, which describes the resources.
CMakeLists.txt CMake configuration file, which Contains the project information and is required for all builds.
CMakePresets.json CMake presets file, which contains the information to configure the CMake project.
launch.vs.json JSON file that tells Visual Studio how to deploy and debug the application.
LICENSE.txt The license for this sample application.
main.c Main C source code file.
README.md This README file.
.vscode Folder containing the JSON files that configure Visual Studio Code for deploying and debugging the application.

Prerequisites

The sample requires the following hardware:

Setup

  1. Ensure that your Azure Sphere device is connected to your computer, and your computer is connected to the internet.

  2. Ensure that you have Azure Sphere SDK version 24.03 or above. At the command prompt, run az sphere show-sdk-version to check. Upgrade the Azure Sphere SDK for Windows or Linux as needed.

  3. Ensure that the Azure CLI is installed. At a minimum, the Azure CLI version must be 2.45.0 or later.

  4. Install the Azure Sphere extension.

  5. Connect your Azure Sphere device to the same local network as the DNS service.

  6. Enable application development, if you have not already done so, by entering the az sphere device enable-development command in the command prompt.

  7. Clone the Azure Sphere samples repository and find the DNSServiceDiscovery sample in the DNSServiceDiscovery folder or download the zip file from the Microsoft samples browser.

  8. Set up a DNS service. This sample requires that you run a DNS service instance that is discoverable on the same local network as the Azure Sphere device. You can use the DNS-SD tool from Apple Bonjour to set up the service.

    The following dns-sd command registers an instance of a DNS responder service with the default service configuration used by the sample:

    dns-sd -R SampleInstanceName _sample-service._tcp local 1234 SampleTxtData
    

    The command registers a service instance with the following configuration:

    • service instance name: SampleInstanceName
    • DNS server type: _sample-service._tcp
    • DNS server domain: local
    • port: 1234
    • TXT record: SampleTxtData
  9. You may want to modify the sample to use unicast queries if you don't need to use multicast queries. You can use unicast queries by calling the res_send POSIX API to query the DNS server and process the response in a single blocking call. This may simplify the application, especially if it doesn't need to perform other activities while waiting for the response.

Use Ethernet instead of Wi-Fi

By default, this sample runs over a Wi-Fi connection to the internet. To use Ethernet instead, complete the following steps:

  1. Follow the Ethernet setup instructions.

  2. Ensure that the global constant networkInterface is set to "eth0". Find the following line of code in main.c and replace wlan0 with eth0:

    char networkInterface[] = "wlan0";

Build and run the sample

To build and run this sample, follow the instructions in Build a sample application.

Test the sample

When you run the sample, every 10 seconds the application will send a DNS query. When it receives a response, it displays the name, host, IPv4 address, port, and TXT data from the query response. The application should then be able to connect to the host names returned by the response.

You can verify the connection by setting up a local web server on the same computer as the DNS service, and then making requests to the service from the application.

To set up an Internet Information Services (IIS) web server, complete the following steps:

  1. Install IIS on the same computer as the DNS service.
  2. If you set up a site binding for a default website with a port other than 80 or 443, you must add an inbound rule that allows the port.

To send requests to the web server, you can incorporate code from the HTTPS_Curl_Easy sample into the application. Requests to the web server should fail before the DNS-SD responses are received but should succeed afterwards.

Rebuild the sample to query a different DNS server

By default, this sample queries the _sample-service._tcp.local DNS server address. To rebuild the sample to query a different DNS server, complete the following steps.

  1. Make the following changes in the sample:

    1. In the app_manifest.json file, change the value of the AllowedConnections field from "_sample-service._tcp.local" to the new DNS server address, such as "_http._tcp.local".

    2. In main.c, find the following line of code and replace _sample-service._tcp.local with the new DNS server address:

      static const char DnsServiceDiscoveryServer[] = "_sample-service._tcp.local";
  2. Follow the instructions in the Build and run the sample section of this README.

Next steps