Files

326 lines
10 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Posix/Ip/IpManager.h
*
* @brief Header file for base ip manager class.
*/
#if (!defined ACTIV_BASE_POSIX_IP_IP_MANAGER_H)
#define ACTIV_BASE_POSIX_IP_IP_MANAGER_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/IniFileParser.h"
#include "ActivMiddleware/ActivBase/ProcessorAffinity.h"
#include "ActivMiddleware/ActivBase/FieldTypes/Boolean.h"
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
#include "ActivMiddleware/CxxYZ/memory.h"
#include "ActivMiddleware/Misc/MacAddress.h"
#include <vector>
#include <netdb.h>
//! Activ namespace.
namespace Activ
{
class EventManager;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Ip manager base class.
*/
class IpManager : private Boost::noncopyable
{
public:
/**
* @brief Settings.
*/
class Settings
{
public:
/**
* @brief Default constructor.
*/
ACTIV_BASE_API Settings();
/**
* @brief Read settings from an ini file.
*
* @param sectionIterator iterator to an ini file section.
*
* @throw Exception
*/
ACTIV_BASE_API void Read(const IniFileParser::ConstSectionIterator &sectionIterator);
size_t m_numberOfIoThreads; ///< Default: 1. Non-0 creates 1 thread for processing the EventManager. 0: don't create any threads.
ProcessorAffinity m_ioThreadProcessorAffinity; ///< For setting affinity of io threads. Empty to not set, ignored on non-Windows.
bool m_enableWindowsOverlappedOperations; ///< Ignored on Posix systems.
bool m_shouldUseEpoll; ///< Use epoll if available? Default true.
UInt m_maxIoBytes; ///< Max. bytes to read/write from one socket before checking others. Default 50000, ignored on Windows.
};
typedef std::vector<IpAddressMask> IpAddressMaskList;
/**
* @brief Information on a network interface.
*/
class InterfaceInfo
{
public:
enum InterfaceState
{
INTERFACE_STATE_DOWN,
INTERFACE_STATE_UP
};
/**
* @brief Default constructor.
*/
ACTIV_BASE_API InterfaceInfo();
String m_name;
String m_description;
String m_driverName;
String m_driverVersion;
String m_firmwareVersion;
String m_busInfo;
UInt m_numaNode;
std::vector<uint16_t> m_localCpuList; ///< List of cpus local to the interface.
std::vector<uint16_t> m_socketLocalCpuList; ///< List of cpus in socket local to the interface (m_localCpuList may be limited to a numa node).
UInt m_rxRingSize;
UInt m_txRingSize;
Boolean m_useAdaptiveRxCoalescence;
UInt m_rxUsecsCoalescence;
MacAddress m_macAddress;
size_t m_bitsPerSecond; ///< 0 is unknown.
size_t m_mtu;
IpAddressMaskList m_ipAddressList;
size_t m_index; ///< Interface index of the card; 0 is unknown.
size_t m_physicalInterfaceIndex; ///< Index of the physical interface.
UInt m_vlanId;
InterfaceState m_interfaceState; ///< The interface state.
};
typedef std::vector<InterfaceInfo> InterfaceInfoList; ///< Type of list of InterfaceInfo objects.
/**
* @brief Constructor.
*
* Creates an EventManager.
*
* @param settings reference to a Settings object.
*
* @throw Exception
*/
ACTIV_BASE_API IpManager(const Settings &settings);
/**
* @brief Constructor.
*
* @param settings reference to a Settings object.
* @param eventManager the pre-existing EventManager to use.
*
* @throw Exception
*/
ACTIV_BASE_API IpManager(const Settings &settings, EventManager &eventManager);
/**
* @brief Destructor.
*/
ACTIV_BASE_API ~IpManager();
/**
* @brief Get reference to settings.
*
* @return Reference to settings as passed to constructor.
*/
ACTIV_BASE_API const Settings &GetSettings() const;
/**
* @brief Resolve a string into an ip address.
*
* The string can be a hostname or ip address.
*
* @param nodeName the name of the node.
* @param portNumber the port number.
* @param ipAddress reference to an IpAddress object to store the ip address.
* @param flags AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST.
* @param timeoutPeriod how long to wait for the call to complete, in ms. Will throw on timeout.
*
* @throw Exception
*/
ACTIV_BASE_API static void GetIpAddress(const String &nodeName, const uint16_t portNumber, IpAddress &ipAddress,
const int flags = 0, const TimeoutPeriod timeoutPeriod = ACTIV_WAIT_INFINITE);
/**
* @brief Get a list of local address.
*
* @param localAddress the address to resolve. If \a localAddress is empty or the hostname, all configured interfaces
* are returned.
* If \a localAddress is one of the "any" addresses (i.e. "0.0.0.0" or "::"), all configured interfaces
* for that protocol (i.e. ipv4 or ipv6) are returned.
* \a localaddress can also be one of the configured interface addresses (eg "172.16.16.2") then only that
* address will be returned.
* @param portNumber optional port to give the returned addresses. Can be 0.
* @param ipAddressList to receive the results.
* @param timeoutPeriod how long to wait for the call to complete, in ms.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_TIMEOUT
* @retval ...
*/
ACTIV_BASE_API static StatusCode GetLocalIpAddressList(const String &localAddress, const uint16_t portNumber,
IpAddressList &ipAddressList, const TimeoutPeriod timeoutPeriod = ACTIV_WAIT_INFINITE);
/**
* @brief Get a list of information for network interfaces in the machine.
*
* @param family AF_INET, AF_INET6 or AF_UNSPEC to not filter results.
* @param interfaceInfoList to receive the list of interface information.
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
ACTIV_BASE_API static StatusCode GetInterfaceInfoList(const int family, InterfaceInfoList &interfaceInfoList);
/**
* @brief Get the network information for an interface that has a specific ip address configured.
*
* @param interfaceAddress the ip address to find.
* @param interfaceInfo to receive the interface information.
*
* @retval STATUS_CODE_SUCCESS or an error status code.
*/
ACTIV_BASE_API static StatusCode GetInterfaceInfo(const IpAddress &interfaceAddress, InterfaceInfo &interfaceInfo);
/**
* @brief Get the hostname and port from a string of the form "host:port".
*
* If there is no "port" portion of the input string, portNumber is reset.
*
* @param hostAndPort a string of the form "host:port".
* @param host reference to a string to receive the host portion of the input string.
* @param portNumber reference to a variable to receive the optional port number portion of the input string.
*
* @retval true.
* @retval false hostAndPort could not be parsed.
*/
ACTIV_BASE_API static bool GetHostAndPort(const String &hostAndPort, String &host, Boost::optional<uint16_t> &portNumber);
/**
* @brief Get the hostname and port from a string of the form "host:port".
*
* If there is no "port" portion of the input string, portNumber is set to 0.
*
* @param hostAndPort a string of the form "host:port".
* @param host reference to a string to receive the host portion of the input string.
* @param portNumber reference to a variable to receive the port number portion of the input string.
*
* @retval true.
* @retval false hostAndPort could not be parsed.
*/
ACTIV_BASE_API static bool GetHostAndPort(const String &hostAndPort, String &host, uint16_t &portNumber);
/**
* @brief Resolve a string into an ip address mask.
*
* The string should be a numeric ip address mask using slash notation.
*
* @param ipAddressMaskString the string form of the ipAddress mask.
* @param ipAddressMask reference to an IpAddressMask object to store the ip address mask.
*
* @throw Exception
*/
ACTIV_BASE_API static void GetIpAddressMask(const String &ipAddressMaskString, IpAddressMask &ipAddressMask);
/**
* @brief Get the hostname.
*
* Note hostname changes will not be picked up by this function.
*
* @return The hostname.
*/
ACTIV_BASE_API static const String &GetHostName();
/**
* @brief Build a list of all the lines from all hosts files.
*
* @param result
*/
ACTIV_BASE_API static void GetHostsFileEntries(std::vector<String> &result);
/**
* @brief Get the last socket error code.
*
* @return Socket error.
*/
ACTIV_BASE_API static int GetLastSocketError();
/**
* @brief Get reference to event manager.
*
* @return Event manager.
*/
EventManager &GetEventManager();
/**
* @brief Get max io vecs allowed to sendmsg() and recvmsg().
*
* @return Max value.
*/
size_t GetMaxIoVecs() const;
private:
class IoThread;
//! Common construction.
void CommonCtor();
/**
* @brief Get a list of information for network interfaces in the machine.
*
* @param family AF_INET, AF_INET6 or AF_UNSPEC to not filter results.
* @param interfaceInfoList to receive the list of interface information.
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
static StatusCode InternalGetInterfaceInfoList(const int family, InterfaceInfoList &interfaceInfoList);
/**
* @brief Shutdown io threads.
*
* @param shutdownTimeout how long to wait for the threads to die, in ms.
*/
void ShutdownIoThreads(const TimeoutPeriod shutdownTimeout);
/**
* @brief Io worker thread entry point.
*/
void IoThreadProc();
const Settings m_settings; ///< Settings as passed to constructor.
CxxYZ::unique_ptr<IoThread> m_pIoThread;
const CxxYZ::unique_ptr<EventManager> m_pEventManager;
EventManager & m_eventManager;
size_t m_maxIoVecs; ///< Max io vecs to send/recvmsg().
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline EventManager &IpManager::GetEventManager()
{
return m_eventManager;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t IpManager::GetMaxIoVecs() const
{
return m_maxIoVecs;
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_POSIX_IP_IP_MANAGER_H)