# Multicast DNS

# Multicast DNS

This module defines platform APIs for Multicast DNS (mDNS) socket.

## Summary

| ### Typedefs ||
|---|---|
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga320cccfca47ec635d8e04ebf0a35ad32` | typedef `struct https://openthread.io/reference/struct/ot-plat-mdns-address-info` Represents a socket address info. |

| ### Functions ||
|---|---|
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga2205f9ddba9c1ea84315960229143f9d(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, const https://openthread.io/reference/struct/ot-ip6-address *aAddress, bool aAdded, uint32_t aInfraIfIndex)` | `void` Callback to notify OpenThread mDNS module of host address changes. |
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gafe6200c620b6b2209784999c014134ec(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, uint32_t aInfraIfIndex)` | `void` Callback to notify OpenThread mDNS module to remove all previously added host IPv4 and IPv6 addresses. |
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gac7d4d90e728e44a9c606097929cbb56a(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, https://openthread.io/reference/group/api-message#group__api-message_1ga42d128a0a9ad58de0eb79673843c2955 *aMessage, bool aIsUnicast, const https://openthread.io/reference/struct/ot-plat-mdns-address-info *aAddress)` | `void` Callback to notify OpenThread mDNS module of a received message on UDP port 5353. |
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga447ba845ef68c993bc63ab7d028bc777(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, https://openthread.io/reference/group/api-message#group__api-message_1ga42d128a0a9ad58de0eb79673843c2955 *aMessage, uint32_t aInfraIfIndex)` | `void` Sends an mDNS message as multicast. |
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gaf6116ef03350844aacd2b64816d690b2(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, https://openthread.io/reference/group/api-message#group__api-message_1ga42d128a0a9ad58de0eb79673843c2955 *aMessage, const https://openthread.io/reference/struct/ot-plat-mdns-address-info *aAddress)` | `void` Sends an mDNS message as unicast. |
| `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gafe6980cc9675bddc80e52f4bb435d072(https://openthread.io/reference/group/api-instance#group__api-instance_1ga25440551213fb5b40869725378584960 *aInstance, bool aEnable, uint32_t aInfraIfIndex)` | `https://openthread.io/reference/group/api-error#group__api-error_1gabaaa90aaa35da8f9d394a227a23a6a49` Enables or disables listening for mDNS messages sent to mDNS port 5353. |

| ### Structs ||
|---|---|
| [otPlatMdnsAddressInfo](https://openthread.io/reference/struct/ot-plat-mdns-address-info) | Represents a socket address info. |

## Typedefs

### otPlatMdnsAddressInfo

```
struct otPlatMdnsAddressInfo otPlatMdnsAddressInfo
```
Represents a socket address info.

## Functions

### otPlatMdnsHandleHostAddressEvent

```
void otPlatMdnsHandleHostAddressEvent(
  otInstance *aInstance,
  const otIp6Address *aAddress,
  bool aAdded,
  uint32_t aInfraIfIndex
)
```
Callback to notify OpenThread mDNS module of host address changes.

When `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gafe6980cc9675bddc80e52f4bb435d072` enables mDNS listening on an `aInfraIfIndex`, the platform MUST monitor and report ALL IPv4 and IPv6 addresses assigned to this network interface.

When mDNS is enabled:

- The platform MUST retrieve ALL currently assigned IPv4 and IPv6 addresses on the specified interface.
- For each retrieved address, the platform MUST call `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga2205f9ddba9c1ea84315960229143f9d` to add the address.
- The IPv4 addresses are represented using IPv4-mapped IPv6 format.

<br />

Ongoing monitoring (while enabled):

- The platform MUST continuously monitor the specified interface for address changes.
- When the address list changes, the platform MUST notify the OpenThread stack of the change using one of the following methods: A. Call this callback for each affected address, indicating the change (addition or removal using `aAdded`). B. Alternatively, call the `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gafe6200c620b6b2209784999c014134ec` callback once, immediately followed by invoking this callback for every currently assigned IPv4 and IPv6 address on the interface adding them (`aAdded` set to `TRUE`), providing the completed updated address list.
- These two approaches offer flexibility for platforms with varying capabilities, such as different operating systems and network stacks. Some network stacks may provide mechanisms to identify the added or removed addresses, while others may only provide the new list upon a change.When mDNS is disabled:
- The platform MUST cease monitoring for address changes on the interface.
- The platform does NOT need to explicitly signal the removal of addresses upon disable. The OpenThread stack automatically clears its internal address list.
- If address monitoring is re-enabled later, the platform MUST repeat the "enable" steps again, retrieving and reporting ALL current addresses.

<br />

The OpenThread stack maintains an internal list of host addresses. It updates this list automatically upon receiving calls to `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga2205f9ddba9c1ea84315960229143f9d`.

- OpenThread's mDNS implementation uses a short guard time (4 msec) before taking action (e.g., announcing new addresses). This allows multiple changes to be grouped and announced together.
- OpenThread's mDNS implementation also handles transient changes, e.g., an address is removed and then quickly re-added. It ensures that announcements are only made when there is a change to the list (from what was announced before). This simplifies the platform's responsibility as it can simply report all observed changes.

<br />

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpenThread instance. | | `[in] aAddress` | IP Address. IPv4-mapped IPv6 format is used to represent an IPv4 address. | | `[in] aAdded` | Boolean to indicate whether the address added (`TRUE`) or removed (`FALSE`). | | `[in] aInfraIfIndex` | The interface index. | |

### otPlatMdnsHandleHostAddressRemoveAll

```
void otPlatMdnsHandleHostAddressRemoveAll(
  otInstance *aInstance,
  uint32_t aInfraIfIndex
)
```
Callback to notify OpenThread mDNS module to remove all previously added host IPv4 and IPv6 addresses.

See documentation of `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga2205f9ddba9c1ea84315960229143f9d` for how this callback MUST be used.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpenThread instance. | | `[in] aInfraIfIndex` | The interface index. | |

### otPlatMdnsHandleReceive

```
void otPlatMdnsHandleReceive(
  otInstance *aInstance,
  otMessage *aMessage,
  bool aIsUnicast,
  const otPlatMdnsAddressInfo *aAddress
)
```
Callback to notify OpenThread mDNS module of a received message on UDP port 5353.

`aMessage` MUST contain DNS message starting with the DNS header at offset zero. This function passes the ownership of `aMessage` from the platform layer to the OpenThread stack. The OpenThread stack will free the message once processed.

The `aAddress` fields are as follows:


- `mAddress` specifies the sender's address. IPv4-mapped IPv6 format is used to represent an IPv4 destination.
- `mPort` specifies the sender's port.
- `mInfraIndex` specifies the interface index.

<br />

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpenThread instance. | | `[in] aMessage` | The received mDNS message. Ownership is transferred to the OpenThread stack. | | `[in] aIsUnicast` | Indicates whether the received message is unicast or multicast. | | `[in] aAddress` | The sender's address info. | |

### otPlatMdnsSendMulticast

```
void otPlatMdnsSendMulticast(
  otInstance *aInstance,
  otMessage *aMessage,
  uint32_t aInfraIfIndex
)
```
Sends an mDNS message as multicast.

The platform MUST multicast the prepared mDNS message in `aMessage` as a UDP message using the mDNS well-known port number 5353 for both source and destination ports. The message MUST be sent to the mDNS link-local multicast address `224.0.0.251` and/or its IPv6 equivalent `ff02::fb`.

`aMessage` contains the mDNS message starting with DNS header at offset zero. It does not include IP or UDP headers. This function passes the ownership of `aMessage` to the platform layer and platform implementation MUST free `aMessage` once sent and no longer needed.

The platform MUST allow multicast loopback, i.e., the multicast message `aMessage` MUST also be received and passed back to OpenThread stack using `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gac7d4d90e728e44a9c606097929cbb56a` callback. This behavior is essential for the OpenThread mDNS stack to process and potentially respond to its own queries, while allowing other mDNS receivers to also receive the query and its response.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpenThread instance. | | `[in] aMessage` | The mDNS message to multicast. Ownership is transferred to the platform layer. | | `[in] aInfraIfIndex` | The infrastructure network interface index. | |

### otPlatMdnsSendUnicast

```
void otPlatMdnsSendUnicast(
  otInstance *aInstance,
  otMessage *aMessage,
  const otPlatMdnsAddressInfo *aAddress
)
```
Sends an mDNS message as unicast.

The platform MUST send the prepared mDNS message in `aMessage` as a UDP message using source UDP port 5353 to the destination address and port number specified by `aAddress`.

`aMessage` contains the DNS message starting with the DNS header at offset zero. It does not include IP or UDP headers. This function passes the ownership of `aMessage` to the platform layer and platform implementation MUST free `aMessage` once sent and no longer needed.

The `aAddress` fields are as follows:


- `mAddress` specifies the destination address. IPv4-mapped IPv6 format is used to represent an IPv4 destination.
- `mPort` specifies the destination port.
- `mInfraIndex` specifies the interface index.

<br />

If the matches this devices address, the platform MUST ensure to receive and pass the message back to the OpenThread stack using `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gac7d4d90e728e44a9c606097929cbb56a` for processing.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpenThread instance. | | `[in] aMessage` | The mDNS message to multicast. Ownership is transferred to platform layer. | | `[in] aAddress` | The destination address info. | |

### otPlatMdnsSetListeningEnabled

```
otError otPlatMdnsSetListeningEnabled(
  otInstance *aInstance,
  bool aEnable,
  uint32_t aInfraIfIndex
)
```
Enables or disables listening for mDNS messages sent to mDNS port 5353.

When listening is enabled, the platform MUST listen for multicast messages sent to UDP destination port 5353 at the mDNS link-local multicast address `224.0.0.251` and its IPv6 equivalent `ff02::fb`.

The platform SHOULD also listen for any unicast messages sent to UDP destination port 5353. If this is not possible, then OpenThread mDNS module can be configured to not use any "QU" questions requesting unicast response.

While enabled, all received messages MUST be reported back using `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1gac7d4d90e728e44a9c606097929cbb56a` callback.

When enabled, the platform MUST also monitor and report all IPv4 and IPv6 addresses assigned to the network interface using the `https://openthread.io/reference/group/plat-mdns#group__plat-mdns_1ga2205f9ddba9c1ea84315960229143f9d` callback function. Refer to the documentation of this callback for detailed information on the callback's usage and parameters.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `[in] aInstance` | The OpernThread instance. | | `[in] aEnable` | Indicate whether to enable or disable. | | `[in] aInfraInfIndex` | The infrastructure network interface index. | |
| Return Values | |---|---| | `OT_ERROR_NONE` | Successfully enabled/disabled listening for mDNS messages. | | `OT_ERROR_FAILED` | Failed to enable/disable listening for mDNS messages. | |

## Resources

OpenThread API Reference topics originate from the source code, available
on [GitHub](https://github.com/openthread/openthread/tree/main/include/openthread).
For more information, or to contribute to our documentation, refer to
[Resources](https://openthread.io/resources).