Skip to content

Commit 9a49ca4

Browse files
authored
Merge pull request #534 from aregtech/feature/533-check-localhost-address
533 check localhost address - added checkup of local host; - added validator of IPv4 address as a string - modified SocketAddress and added possibility to keep IP address and host name.
2 parents 14309e8 + 1c4a08e commit 9a49ca4

File tree

6 files changed

+283
-47
lines changed

6 files changed

+283
-47
lines changed

framework/areg/base/NESocket.hpp

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,25 @@ namespace NESocket
158158
**/
159159
bool resolveSocket( SOCKETHANDLE hSocket );
160160

161+
/**
162+
* \brief Compares the stored IPv4 address or host name and port number with the given values.
163+
*
164+
* \param host The IPv4 address or host name to compare.
165+
* \param port The 16-bit port number to compare.
166+
* \return Returns true if both the host (IPv4 address or name) and port match the stored values.
167+
*/
168+
bool isEqualAddress(const String& host, uint16_t port) const;
169+
161170
/**
162171
* \brief Returns IP address of host as readable string.
163172
**/
164173
inline const String & getHostAddress( void ) const;
165174

175+
/**
176+
* \brief Returns the name of the host as a readable string.
177+
**/
178+
inline const String & getHostName( void ) const;
179+
166180
/**
167181
* \brief Returns port number of host.
168182
**/
@@ -186,6 +200,10 @@ namespace NESocket
186200
* \brief The string containing human readable numeric IP-address.
187201
**/
188202
String mIpAddr;
203+
/**
204+
* \brief The string contains human readable host name.
205+
**/
206+
String mHostName;
189207
/**
190208
* \brief The port number of socket to connect.
191209
**/
@@ -581,6 +599,48 @@ namespace NESocket
581599
**/
582600
AREG_API const String & getHostname(void);
583601

602+
/**
603+
* \brief NESocket::isLocalAddress
604+
* Checks whether passed IP-address is local address.
605+
* The local address is either "localhost" or "127.0.0.1".
606+
**/
607+
inline bool isLocalAddress(const String& ipaddress);
608+
609+
/**
610+
* \brief Checks whether the given string contains an IPv4 address.
611+
* \param ipaddress String to check.
612+
* \return Returns true if given string is an IPv4 address like "127.0.0.1". Otherwise, returns false.
613+
**/
614+
AREG_API bool isIpAddress(const String& ipaddress);
615+
616+
/**
617+
* \brief Converts the host name to the IPv4 address or returns the `hostName` string if failed to convert.
618+
* \param hostName The human readable string to convert.
619+
* \return Returns appropriate IPv4 address or the same `hostName` string if could not convert.
620+
**/
621+
AREG_API String convertHostNameToIpAddress(const String& hostName);
622+
623+
/**
624+
* \brief Converts the IPv4 address to the host name or returns the `ipAddress` string if failed to convert.
625+
* \param ipAddress The human readable IPv4 address to convert.
626+
* \return Returns appropriate host name or the same `ipAddress` string if could not convert.
627+
**/
628+
AREG_API String convertIpAddressToHostName(const String& ipAddress);
629+
630+
/**
631+
* \brief Extracts the IPv4 address from the given `sockaddr_in` structure.
632+
* \param addrHost The `sockaddr_in` structure which contains the information of the IP address.
633+
* \return Returns human readable IP address.
634+
**/
635+
AREG_API String extractIpAddress(const struct sockaddr_in& addrHost);
636+
637+
/**
638+
* \brief Extracts the port number from the given `sockaddr_in` structure.
639+
* \param addrHost The `sockaddr_in` structure which contains the information of the port number.
640+
* \return Returns port number.
641+
**/
642+
AREG_API uint16_t extractPortNumber(const struct sockaddr_in& addrHost);
643+
584644
} // namespace NESocket end
585645

586646
//////////////////////////////////////////////////////////////////////////
@@ -597,14 +657,20 @@ inline const String & NESocket::SocketAddress::getHostAddress( void ) const
597657
return mIpAddr;
598658
}
599659

660+
inline const String& NESocket::SocketAddress::getHostName(void) const
661+
{
662+
return mHostName;
663+
}
664+
600665
inline unsigned short NESocket::SocketAddress::getHostPort( void ) const
601666
{
602667
return mPortNr;
603668
}
604669

605670
inline void NESocket::SocketAddress::resetAddress( void )
606671
{
607-
mIpAddr = String::EmptyString;
672+
mIpAddr.clear();
673+
mHostName.clear();
608674
mPortNr = NESocket::InvalidPort;
609675
}
610676

@@ -613,4 +679,9 @@ inline bool NESocket::isSocketHandleValid(SOCKETHANDLE hSocket)
613679
return ((hSocket != NESocket::InvalidSocketHandle) && (hSocket != NESocket::FailedSocketHandle));
614680
}
615681

682+
inline bool NESocket::isLocalAddress(const String& ipaddress)
683+
{
684+
return (ipaddress == NESocket::LocalHost) || (ipaddress == NESocket::LocalAddress);
685+
}
686+
616687
#endif // AREG_BASE_NESOCKET_HPP

0 commit comments

Comments
 (0)