Coverage Report

Created: 2026-04-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Fast-DDS/src/cpp/rtps/common/LocatorWithMask.cpp
Line
Count
Source
1
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/*!
16
 * @file LocatorWithMask.cpp
17
 */
18
19
#include <cassert>
20
#include <string>
21
22
#include <fastdds/rtps/common/Locator.hpp>
23
#include <fastdds/rtps/common/Types.hpp>
24
#include <rtps/network/utils/network.hpp>
25
26
#include <fastdds/rtps/common/LocatorWithMask.hpp>
27
28
namespace eprosima {
29
namespace fastdds {
30
namespace rtps {
31
32
uint8_t LocatorWithMask::mask() const
33
0
{
34
0
    return mask_;
35
0
}
36
37
void LocatorWithMask::mask(
38
        uint8_t mask)
39
0
{
40
0
    mask_ = mask;
41
0
}
42
43
bool LocatorWithMask::matches(
44
        const Locator& loc) const
45
0
{
46
0
    if (kind == loc.kind)
47
0
    {
48
0
        switch (kind)
49
0
        {
50
0
            case LOCATOR_KIND_UDPv4:
51
0
            case LOCATOR_KIND_TCPv4:
52
                // IPv4 address is in the last 4 octets of the 16-octet array
53
0
                assert(32 >= mask());
54
0
                return network::address_matches(loc.address + 12, address + 12, mask());
55
56
0
            case LOCATOR_KIND_UDPv6:
57
0
            case LOCATOR_KIND_TCPv6:
58
0
            case LOCATOR_KIND_SHM:
59
                // IPv6 and SHM address use the full 16-octet array
60
0
                assert(128 >= mask());
61
0
                return network::address_matches(loc.address, address, mask());
62
63
0
            case LOCATOR_KIND_ETHERNET:
64
                // Ethernet locators match independently of the MAC address (mask 0)
65
0
                assert(0 == mask());
66
0
                return true;
67
68
0
            default:
69
                // Other kinds of locators would come from custom transports, so we let them always match (mask 0)
70
0
                return true;
71
0
        }
72
0
    }
73
74
0
    return false;
75
0
}
76
77
LocatorWithMask& LocatorWithMask::operator =(
78
        const Locator& loc)
79
0
{
80
0
    kind = loc.kind;
81
0
    port = loc.port;
82
0
    std::memcpy(address, loc.address, 16 * sizeof(octet));
83
0
    return *this;
84
0
}
85
86
std::ostream& operator <<(
87
        std::ostream& output,
88
        const LocatorWithMask& loc)
89
0
{
90
0
    std::stringstream ss;
91
0
    operator <<(ss, static_cast<const Locator&>(loc));
92
0
    std::string loc_str = ss.str();
93
94
0
    if (loc.kind == LOCATOR_KIND_UDPv4 || loc.kind == LOCATOR_KIND_UDPv6 || loc.kind == LOCATOR_KIND_TCPv4 ||
95
0
            loc.kind == LOCATOR_KIND_TCPv6)
96
0
    {
97
0
        size_t ip_end = loc_str.find("]");
98
0
        if (ip_end != std::string::npos)
99
0
        {
100
0
            std::string netmask_suffix = "/" + std::to_string(loc.mask());
101
0
            loc_str.insert(ip_end, netmask_suffix);
102
0
        }
103
0
    }
104
0
    return output << loc_str;
105
0
}
106
107
} // namsepace rtps
108
} // namespace fastdds
109
} // namespace eprosima