/src/openthread/tests/nexus/platform/nexus_mdns.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2025, The OpenThread Authors. |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions are met: |
7 | | * 1. Redistributions of source code must retain the above copyright |
8 | | * notice, this list of conditions and the following disclaimer. |
9 | | * 2. Redistributions in binary form must reproduce the above copyright |
10 | | * notice, this list of conditions and the following disclaimer in the |
11 | | * documentation and/or other materials provided with the distribution. |
12 | | * 3. Neither the name of the copyright holder nor the |
13 | | * names of its contributors may be used to endorse or promote products |
14 | | * derived from this software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
20 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | | * POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #include <openthread/platform/mdns_socket.h> |
30 | | |
31 | | #include "nexus_core.hpp" |
32 | | #include "nexus_node.hpp" |
33 | | |
34 | | namespace ot { |
35 | | namespace Nexus { |
36 | | |
37 | | //--------------------------------------------------------------------------------------------------------------------- |
38 | | // otPlatMdns APIs |
39 | | |
40 | | extern "C" { |
41 | | |
42 | | otError otPlatMdnsSetListeningEnabled(otInstance *aInstance, bool aEnable, uint32_t aInfraIfIndex) |
43 | 28.4k | { |
44 | 28.4k | return AsNode(aInstance).mMdns.SetListeningEnabled(AsCoreType(aInstance), aEnable, aInfraIfIndex); |
45 | 28.4k | } |
46 | | |
47 | | void otPlatMdnsSendMulticast(otInstance *aInstance, otMessage *aMessage, uint32_t aInfraIfIndex) |
48 | 541k | { |
49 | 541k | AsNode(aInstance).mMdns.SendMulticast(AsCoreType(aMessage), aInfraIfIndex); |
50 | 541k | } |
51 | | |
52 | | void otPlatMdnsSendUnicast(otInstance *aInstance, otMessage *aMessage, const otPlatMdnsAddressInfo *aAddress) |
53 | 0 | { |
54 | 0 | AsNode(aInstance).mMdns.SendUnicast(AsCoreType(aMessage), *aAddress); |
55 | 0 | } |
56 | | |
57 | | } // extern "C" |
58 | | |
59 | | //--------------------------------------------------------------------------------------------------------------------- |
60 | | // Mdns |
61 | | |
62 | | Mdns::Mdns(void) |
63 | 28.1k | : mEnabled(false) |
64 | 28.1k | { |
65 | 28.1k | Ip6::Address address; |
66 | 28.1k | Ip6::InterfaceIdentifier iid; |
67 | | |
68 | 28.1k | iid.GenerateRandom(); |
69 | 28.1k | address.SetToLinkLocalAddress(iid); |
70 | | |
71 | 28.1k | SuccessOrQuit(mIfAddresses.PushBack(address)); |
72 | 28.1k | } |
73 | | |
74 | | void Mdns::Reset(void) |
75 | 0 | { |
76 | 0 | mEnabled = false; |
77 | 0 | mPendingTxList.Free(); |
78 | 0 | } |
79 | | |
80 | | Error Mdns::SetListeningEnabled(Instance &aInstance, bool aEnable, uint32_t aInfraIfIndex) |
81 | 28.4k | { |
82 | 28.4k | Error error = kErrorNone; |
83 | | |
84 | 28.4k | VerifyOrExit(aInfraIfIndex == kInfraIfIndex, error = kErrorFailed); |
85 | 28.2k | VerifyOrExit(mEnabled != aEnable); |
86 | 28.2k | mEnabled = aEnable; |
87 | | |
88 | 28.2k | if (mEnabled) |
89 | 28.1k | { |
90 | 28.1k | SignalIfAddresses(aInstance); |
91 | 28.1k | } |
92 | | |
93 | 28.4k | exit: |
94 | 28.4k | return error; |
95 | 28.2k | } |
96 | | |
97 | | void Mdns::SendMulticast(Message &aMessage, uint32_t aInfraIfIndex) |
98 | 541k | { |
99 | 541k | PendingTx *pendingTx; |
100 | | |
101 | 541k | if (aInfraIfIndex != kInfraIfIndex) |
102 | 961 | { |
103 | 961 | aMessage.Free(); |
104 | 961 | ExitNow(); |
105 | 961 | } |
106 | | |
107 | 540k | pendingTx = PendingTx::Allocate(); |
108 | 540k | VerifyOrQuit(pendingTx != nullptr); |
109 | | |
110 | 540k | pendingTx->mMessage.Reset(&aMessage); |
111 | 540k | pendingTx->mIsUnicast = false; |
112 | | |
113 | 540k | mPendingTxList.PushAfterTail(*pendingTx); |
114 | | |
115 | 541k | exit: |
116 | 541k | return; |
117 | 540k | } |
118 | | |
119 | | void Mdns::SendUnicast(Message &aMessage, const AddressInfo &aAddress) |
120 | 0 | { |
121 | 0 | PendingTx *pendingTx; |
122 | |
|
123 | 0 | if (aAddress.mInfraIfIndex != kInfraIfIndex) |
124 | 0 | { |
125 | 0 | aMessage.Free(); |
126 | 0 | ExitNow(); |
127 | 0 | } |
128 | | |
129 | 0 | pendingTx = PendingTx::Allocate(); |
130 | 0 | VerifyOrQuit(pendingTx != nullptr); |
131 | | |
132 | 0 | pendingTx->mMessage.Reset(&aMessage); |
133 | 0 | pendingTx->mIsUnicast = true; |
134 | 0 | pendingTx->mAddress = aAddress; |
135 | |
|
136 | 0 | mPendingTxList.PushAfterTail(*pendingTx); |
137 | |
|
138 | 0 | exit: |
139 | 0 | return; |
140 | 0 | } |
141 | | |
142 | | void Mdns::SignalIfAddresses(Instance &aInstance) |
143 | 28.1k | { |
144 | 28.1k | otPlatMdnsHandleHostAddressRemoveAll(&aInstance, kInfraIfIndex); |
145 | | |
146 | 28.1k | for (const Ip6::Address &address : mIfAddresses) |
147 | 28.1k | { |
148 | 28.1k | otPlatMdnsHandleHostAddressEvent(&aInstance, &address, /* aAdded */ true, kInfraIfIndex); |
149 | 28.1k | } |
150 | 28.1k | } |
151 | | |
152 | | void Mdns::Receive(Instance &aInstance, const PendingTx &aPendingTx, const AddressInfo &aSenderAddress) |
153 | 540k | { |
154 | 540k | Message *message; |
155 | | |
156 | 540k | VerifyOrExit(mEnabled); |
157 | | |
158 | 540k | if (aPendingTx.mIsUnicast) |
159 | 0 | { |
160 | 0 | VerifyOrExit(aPendingTx.mAddress.mInfraIfIndex == kInfraIfIndex); |
161 | 0 | VerifyOrExit(aPendingTx.mAddress.mPort == kUdpPort); |
162 | 0 | VerifyOrExit(mIfAddresses.Contains(AsCoreType(&aPendingTx.mAddress.mAddress))); |
163 | 0 | } |
164 | | |
165 | 540k | message = aPendingTx.mMessage->Clone(); |
166 | 540k | VerifyOrQuit(message != nullptr); |
167 | | |
168 | 540k | otPlatMdnsHandleReceive(&aInstance, message, aPendingTx.mIsUnicast, &aSenderAddress); |
169 | | |
170 | 540k | exit: |
171 | 540k | return; |
172 | 540k | } |
173 | | |
174 | | void Mdns::GetAddress(AddressInfo &aAddress) const |
175 | 17.0M | { |
176 | 17.0M | ClearAllBytes(aAddress); |
177 | 17.0M | aAddress.mAddress = mIfAddresses[0]; |
178 | 17.0M | aAddress.mPort = kUdpPort; |
179 | 17.0M | aAddress.mInfraIfIndex = kInfraIfIndex; |
180 | 17.0M | } |
181 | | |
182 | | } // namespace Nexus |
183 | | } // namespace ot |