page_type | languages | products | name | urlFragment | extendedZipContent | description | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
|
Azure Sphere – DNS service discovery |
DNSServiceDiscovery |
|
Demonstrates how to perform service discovery on the local network by using multicast DNS (mDNS). |
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. |
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. |
The sample requires the following hardware:
-
Ensure that your Azure Sphere device is connected to your computer, and your computer is connected to the internet.
-
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. -
Ensure that the Azure CLI is installed. At a minimum, the Azure CLI version must be 2.45.0 or later.
-
Install the Azure Sphere extension.
-
Connect your Azure Sphere device to the same local network as the DNS service.
-
Enable application development, if you have not already done so, by entering the
az sphere device enable-development
command in the command prompt. -
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.
-
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
-
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.
By default, this sample runs over a Wi-Fi connection to the internet. To use Ethernet instead, complete the following steps:
-
Follow the Ethernet setup instructions.
-
Ensure that the global constant networkInterface is set to "eth0". Find the following line of code in
main.c
and replacewlan0
witheth0
:char networkInterface[] = "wlan0";
To build and run this sample, follow the instructions in Build a sample application.
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:
- Install IIS on the same computer as the DNS service.
- 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.
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.
-
Make the following changes in the sample:
-
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"
. -
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";
-
-
Follow the instructions in the Build and run the sample section of this README.
- For an overview of Azure Sphere, see What is Azure Sphere.
- To learn more about Azure Sphere application development, see Overview of Azure Sphere applications.
- For network troubleshooting, see Troubleshoot network problems.