Files

269 lines
8.6 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Windows/Ip/IpManager.h
*
* @brief Header file for base ip manager class.
*/
#if (!defined ACTIV_BASE_IP_WINDOWS_IP_MANAGER_H)
#define ACTIV_BASE_IP_WINDOWS_IP_MANAGER_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/Ip/IpAddress.h"
#include "ActivMiddleware/ActivBase/Ip/SocketWrapper.h"
#include "ActivMiddleware/ActivBase/Windows/IoCompletionManager.h"
#include "ActivMiddleware/ActivBase/IniFileParser.h"
#include "ActivMiddleware/ActivBase/FieldTypes/Boolean.h"
#include "ActivMiddleware/ActivBase/FieldTypes/UInt.h"
#include "ActivMiddleware/Misc/MacAddress.h"
#include <vector>
//! Activ namespace.
namespace Activ
{
/**
* @brief Ip manager base class.
*/
class IpManager : public IoCompletionManager
{
public:
/**
* @brief Settings.
*/
class Settings : public IoCompletionManager::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);
bool m_enableWindowsOverlappedOperations; ///< Default true.
bool m_shouldUseEpoll; ///< Ignored on Windows systems.
UInt m_maxIoBytes; ///< Max. bytes to read/write from one socket before checking others. Default 50000, ignored on Windows.
};
/**
* @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 (NetLuidIndex).
UInt m_vlanId;
InterfaceState m_interfaceState; ///< The interface state.
};
typedef std::vector<InterfaceInfo> InterfaceInfoList; ///< Type of list of InterfaceInfo objects.
/**
* @brief Default constructor.
*
* @param settings reference to a Settings object.
*
* @throw Exception
*/
ACTIV_BASE_API IpManager(const Settings &settings);
/**
* @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 address of a winsock extension function.
*
* @param socket the socket to find the extension function for.
* @param fnStorage pointer to place to store the function pointer.
* @param fnStorageSize the size of the function pointer storage.
* @param fnGuid pointer to the guid for the function to find.
* @param fnGuidSize size of the guid.
*
* @throw Exception
*/
ACTIV_BASE_API static void GetExtensionFunction(const SOCKET socket, void *fnStorage, const DWORD fnStorageSize, void *fnGuid,
const DWORD fnGuidSize);
/**
* @brief Create a non-inheritable socket.
*
* @param ipAddress describing type of socket to create.
* @param socketWrapper to reveive the socket.
*
* @throw Exception.
*/
ACTIV_BASE_API static void CreateNonInheritableSocket(const IpAddress &ipAddress, SocketWrapper &socketWrapper);
/**
* @brief Get the last socket error code.
*
* @return Socket error.
*/
ACTIV_BASE_API static int GetLastSocketError();
private:
const Settings m_settings; ///< Settings as passed to constructor.
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_IP_WINDOWS_IP_MANAGER_H)