Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/include/fastdds/utils/IPFinder.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2016 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 IPFinder.hpp
17
 *
18
 */
19
20
#ifndef FASTDDS_UTILS__IPFINDER_HPP
21
#define FASTDDS_UTILS__IPFINDER_HPP
22
23
#include <string>
24
#include <vector>
25
26
#include <fastdds/rtps/common/Locator.hpp>
27
#include <fastdds/rtps/common/LocatorList.hpp>
28
#include <fastdds/rtps/common/LocatorWithMask.hpp>
29
30
namespace eprosima {
31
namespace fastdds {
32
namespace rtps {
33
/**
34
 * Class IPFinder, to determine the IP of the NICs.
35
 * @ingroup UTILITIES_MODULE
36
 */
37
class IPFinder
38
{
39
public:
40
41
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
42
    /**
43
     * Enum IPTYPE, to define the type of IP obtained from the NICs.
44
     */
45
    enum IPTYPE
46
    {
47
        IP4,          //!< IP4
48
        IP6,          //!< IP6
49
        IP4_LOCAL,    //!< IP4_LOCAL
50
        IP6_LOCAL     //!< IP6_LOCAL
51
    };
52
    /**
53
     * Structure info_IP with information about a specific IP obtained from a NIC.
54
     */
55
    typedef struct info_IP
56
    {
57
        IPTYPE type;
58
        std::string name;
59
        std::string dev;
60
        Locator_t locator;
61
        fastdds::rtps::LocatorWithMask masked_locator;
62
    }info_IP;
63
64
    /**
65
     * Structure info_MAC with information about a specific MAC obtained from a NIC.
66
     */
67
    typedef struct info_MAC
68
    {
69
        unsigned char address[6];
70
71
        bool operator == (
72
                const info_MAC& other)
73
0
        {
74
0
            return memcmp(address, other.address, 6) == 0;
75
0
        }
76
77
    }info_MAC;
78
79
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
80
    IPFinder();
81
    virtual ~IPFinder();
82
83
    FASTDDS_EXPORTED_API static bool getIPs(
84
            std::vector<info_IP>* vec_name,
85
            bool return_loopback = false);
86
87
    /**
88
     * Get the IP4Adresses in all interfaces.
89
     * @param [out] locators List of locators to be populated with the IP4 addresses.
90
     */
91
    FASTDDS_EXPORTED_API static bool getIP4Address(
92
            LocatorList_t* locators);
93
    /**
94
     * Get the IP6Adresses in all interfaces.
95
     * @param [out] locators List of locators to be populated with the IP6 addresses.
96
     */
97
    FASTDDS_EXPORTED_API static bool getIP6Address(
98
            LocatorList_t* locators);
99
    /**
100
     * Get all IP Adresses in all interfaces.
101
     * @param [out] locators List of locators to be populated with the addresses.
102
     */
103
    FASTDDS_EXPORTED_API static bool getAllIPAddress(
104
            LocatorList_t* locators);
105
    /**
106
     * Parses an IP4 string, populating a info_IP with its value.
107
     * @param [out] info info_IP to populate.
108
     * */
109
    FASTDDS_EXPORTED_API static bool parseIP4(
110
            info_IP& info);
111
    /**
112
     * Parses an IP6 string, populating a info_IP with its value.
113
     * @param [out] info info_IP to populate.
114
     * */
115
    FASTDDS_EXPORTED_API static bool parseIP6(
116
            info_IP& info);
117
118
    FASTDDS_EXPORTED_API static std::string getIPv4Address(
119
            const std::string& name);
120
    FASTDDS_EXPORTED_API static std::string getIPv6Address(
121
            const std::string& name);
122
123
    /**
124
     * Get all MAC Adresses of all interfaces.
125
     * Will return all unique MAC addresses for eadh of the interfaces returned by getAllIPAddress
126
     * @param [out] macs List of MAC addresses.
127
     */
128
    FASTDDS_EXPORTED_API static bool getAllMACAddress(
129
            std::vector<info_MAC>* macs);
130
};
131
132
} // namespace rtps
133
} // namespace fastdds
134
} // namespace eprosima
135
136
#endif // FASTDDS_UTILS__IPFINDER_HPP