Coverage Report

Created: 2026-01-17 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
27.6k
{
44
27.6k
    return AsNode(aInstance).mMdns.SetListeningEnabled(AsCoreType(aInstance), aEnable, aInfraIfIndex);
45
27.6k
}
46
47
void otPlatMdnsSendMulticast(otInstance *aInstance, otMessage *aMessage, uint32_t aInfraIfIndex)
48
527k
{
49
527k
    AsNode(aInstance).mMdns.SendMulticast(AsCoreType(aMessage), aInfraIfIndex);
50
527k
}
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
27.3k
    : mEnabled(false)
64
27.3k
{
65
27.3k
    Ip6::Address             address;
66
27.3k
    Ip6::InterfaceIdentifier iid;
67
68
27.3k
    iid.GenerateRandom();
69
27.3k
    address.SetToLinkLocalAddress(iid);
70
71
27.3k
    SuccessOrQuit(mIfAddresses.PushBack(address));
72
27.3k
}
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
27.6k
{
82
27.6k
    Error error = kErrorNone;
83
84
27.6k
    VerifyOrExit(aInfraIfIndex == kInfraIfIndex, error = kErrorFailed);
85
27.5k
    VerifyOrExit(mEnabled != aEnable);
86
27.5k
    mEnabled = aEnable;
87
88
27.5k
    if (mEnabled)
89
27.3k
    {
90
27.3k
        SignalIfAddresses(aInstance);
91
27.3k
    }
92
93
27.6k
exit:
94
27.6k
    return error;
95
27.5k
}
96
97
void Mdns::SendMulticast(Message &aMessage, uint32_t aInfraIfIndex)
98
527k
{
99
527k
    PendingTx *pendingTx;
100
101
527k
    if (aInfraIfIndex != kInfraIfIndex)
102
997
    {
103
997
        aMessage.Free();
104
997
        ExitNow();
105
997
    }
106
107
526k
    pendingTx = PendingTx::Allocate();
108
526k
    VerifyOrQuit(pendingTx != nullptr);
109
110
526k
    pendingTx->mMessage.Reset(&aMessage);
111
526k
    pendingTx->mIsUnicast = false;
112
113
526k
    mPendingTxList.PushAfterTail(*pendingTx);
114
115
527k
exit:
116
527k
    return;
117
526k
}
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
27.3k
{
144
27.3k
    otPlatMdnsHandleHostAddressRemoveAll(&aInstance, kInfraIfIndex);
145
146
27.3k
    for (const Ip6::Address &address : mIfAddresses)
147
27.3k
    {
148
27.3k
        otPlatMdnsHandleHostAddressEvent(&aInstance, &address, /* aAdded */ true, kInfraIfIndex);
149
27.3k
    }
150
27.3k
}
151
152
void Mdns::Receive(Instance &aInstance, const PendingTx &aPendingTx, const AddressInfo &aSenderAddress)
153
526k
{
154
526k
    Message *message;
155
156
526k
    VerifyOrExit(mEnabled);
157
158
526k
    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
526k
    message = aPendingTx.mMessage->Clone();
166
526k
    VerifyOrQuit(message != nullptr);
167
168
526k
    otPlatMdnsHandleReceive(&aInstance, message, aPendingTx.mIsUnicast, &aSenderAddress);
169
170
526k
exit:
171
526k
    return;
172
526k
}
173
174
void Mdns::GetAddress(AddressInfo &aAddress) const
175
16.7M
{
176
16.7M
    ClearAllBytes(aAddress);
177
16.7M
    aAddress.mAddress      = mIfAddresses[0];
178
16.7M
    aAddress.mPort         = kUdpPort;
179
16.7M
    aAddress.mInfraIfIndex = kInfraIfIndex;
180
16.7M
}
181
182
} // namespace Nexus
183
} // namespace ot