Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/src/cpp/rtps/common/LocatorWithMask.cpp
Line
Count
Source (jump to first uncovered line)
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
0
                assert(32 >= mask());
53
0
                return network::address_matches(loc.address + 12, address + 12, mask());
54
55
0
            case LOCATOR_KIND_UDPv6:
56
0
            case LOCATOR_KIND_TCPv6:
57
0
            case LOCATOR_KIND_SHM:
58
0
                assert(128 >= mask());
59
0
                return network::address_matches(loc.address, address, mask());
60
0
        }
61
0
    }
62
63
0
    return false;
64
0
}
65
66
LocatorWithMask& LocatorWithMask::operator =(
67
        const Locator& loc)
68
0
{
69
0
    kind = loc.kind;
70
0
    port = loc.port;
71
0
    std::memcpy(address, loc.address, 16 * sizeof(octet));
72
0
    return *this;
73
0
}
74
75
std::ostream& operator <<(
76
        std::ostream& output,
77
        const LocatorWithMask& loc)
78
0
{
79
0
    std::stringstream ss;
80
0
    operator <<(ss, static_cast<const Locator&>(loc));
81
0
    std::string loc_str = ss.str();
82
83
0
    if (loc.kind == LOCATOR_KIND_UDPv4 || loc.kind == LOCATOR_KIND_UDPv6 || loc.kind == LOCATOR_KIND_TCPv4 ||
84
0
            loc.kind == LOCATOR_KIND_TCPv6)
85
0
    {
86
0
        size_t ip_end = loc_str.find("]");
87
0
        if (ip_end != std::string::npos)
88
0
        {
89
0
            std::string netmask_suffix = "/" + std::to_string(loc.mask());
90
0
            loc_str.insert(ip_end, netmask_suffix);
91
0
        }
92
0
    }
93
0
    return output << loc_str;
94
0
}
95
96
} // namsepace rtps
97
} // namespace fastdds
98
} // namespace eprosima