Program Listing for File IPLocator.hpp

Return to documentation for file (include/fastdds/utils/IPLocator.hpp)

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef FASTDDS_UTILS__IPLOCATOR_HPP
#define FASTDDS_UTILS__IPLOCATOR_HPP

#include <fastdds/rtps/common/Types.hpp>
#include <fastdds/dds/log/Log.hpp>

#include <regex>
#include <set>
#include <string>
#include <vector>

namespace eprosima {
namespace fastdds {
namespace rtps {

class Locator_t;

class IPLocator
{
public:

    FASTDDS_EXPORTED_API static void createLocator(
            int32_t kindin,
            const std::string& address,
            uint32_t portin,
            Locator_t& locator);

    FASTDDS_EXPORTED_API static bool setIPv4(
            Locator_t& locator,
            const unsigned char* addr);

    FASTDDS_EXPORTED_API static bool setIPv4(
            Locator_t& locator,
            octet o1,
            octet o2,
            octet o3,
            octet o4);

    FASTDDS_EXPORTED_API static bool setIPv4(
            Locator_t& locator,
            const std::string& ipv4);

    FASTDDS_EXPORTED_API static bool setIPv4(
            Locator_t& destlocator,
            const Locator_t& origlocator);

    FASTDDS_EXPORTED_API static bool setIPv4address(
            Locator_t& destlocator,
            const std::string& lan,
            const std::string& wan,
            const std::string& ipv4);

    FASTDDS_EXPORTED_API static const octet* getIPv4(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool hasIPv4(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static std::string toIPv4string(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool copyIPv4(
            const Locator_t& locator,
            unsigned char* dest);

    // IPv6
    FASTDDS_EXPORTED_API static bool setIPv6(
            Locator_t& locator,
            const unsigned char* addr);

    FASTDDS_EXPORTED_API static bool setIPv6(
            Locator_t& locator,
            uint16_t group0,
            uint16_t group1,
            uint16_t group2,
            uint16_t group3,
            uint16_t group4,
            uint16_t group5,
            uint16_t group6,
            uint16_t group7);

    FASTDDS_EXPORTED_API static bool setIPv6(
            Locator_t& locator,
            const std::string& ipv6);

    FASTDDS_EXPORTED_API static bool setIPv6(
            Locator_t& destlocator,
            const Locator_t& origlocator);

    FASTDDS_EXPORTED_API static const octet* getIPv6(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool hasIPv6(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static std::string toIPv6string(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool copyIPv6(
            const Locator_t& locator,
            unsigned char* dest);

    FASTDDS_EXPORTED_API static bool ip(
            Locator_t& locator,
            const std::string& ip);

    FASTDDS_EXPORTED_API static std::string ip_to_string(
            const Locator_t& locator);

    // TCP
    FASTDDS_EXPORTED_API static bool setLogicalPort(
            Locator_t& locator,
            uint16_t port);

    FASTDDS_EXPORTED_API static uint16_t getLogicalPort(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool setPhysicalPort(
            Locator_t& locator,
            uint16_t port);

    FASTDDS_EXPORTED_API static uint16_t getPhysicalPort(
            const Locator_t& locator);

    // TCPv4
    FASTDDS_EXPORTED_API static bool setWan(
            Locator_t& locator,
            octet o1,
            octet o2,
            octet o3,
            octet o4);

    FASTDDS_EXPORTED_API static bool setWan(
            Locator_t& locator,
            const std::string& wan);

    FASTDDS_EXPORTED_API static const octet* getWan(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool hasWan(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static std::string toWanstring(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static Locator_t WanToLanLocator(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool setLanID(
            Locator_t& locator,
            const std::string& lanId);

    FASTDDS_EXPORTED_API static const octet* getLanID(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static std::string toLanIDstring(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static Locator_t toPhysicalLocator(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool ip_equals_wan(
            const Locator_t& locator);

    // Common
    FASTDDS_EXPORTED_API static bool setPortRTPS(
            Locator_t& locator,
            uint16_t port);

    FASTDDS_EXPORTED_API static uint16_t getPortRTPS(
            Locator_t& locator);

    FASTDDS_EXPORTED_API static bool isLocal(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool isAny(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static bool compareAddress(
            const Locator_t& loc1,
            const Locator_t& loc2,
            bool fullAddress = false);

    FASTDDS_EXPORTED_API static bool compareAddressAndPhysicalPort(
            const Locator_t& loc1,
            const Locator_t& loc2);

    FASTDDS_EXPORTED_API static std::string to_string(
            const Locator_t& locator);

    // UDP
    FASTDDS_EXPORTED_API static bool isMulticast(
            const Locator_t& locator);

    FASTDDS_EXPORTED_API static std::pair<std::set<std::string>, std::set<std::string>> resolveNameDNS(
            const std::string& address_name);

    FASTDDS_EXPORTED_API static bool isIPv4(
            const std::string& address);
    FASTDDS_EXPORTED_API static bool isIPv6(
            const std::string& address);

protected:

    // Checks if the locator address is equal to 0
    // It checks the proper locator address depending on the locator kind
    static bool isEmpty(
            const Locator_t& locator);

    // Checks if the locator address from index till the end is equal to 0
    static bool isEmpty(
            const Locator_t& locator,
            uint16_t index);

    // Checks if a string matches an ipv6 address
    static bool IPv6isCorrect(
            const std::string& ipv6);

private:

    IPLocator() = delete;
    ~IPLocator() = delete;
};

} // namespace rtps
} // namespace fastdds
} // namespace eprosima

#endif // FASTDDS_UTILS__IPLOCATOR_HPP