diff --git a/src/address/SocketAddress.cpp b/src/address/SocketAddress.cpp index aa96521..95e10a2 100644 --- a/src/address/SocketAddress.cpp +++ b/src/address/SocketAddress.cpp @@ -1,9 +1,50 @@ #include "SocketAddress.h" +#include +#include + namespace kt { - kt::InternetProtocolVersion getInternetProtocolVersion(kt::SocketAddress& address) + kt::InternetProtocolVersion getInternetProtocolVersion(const kt::SocketAddress& address) { return static_cast(address.address.sa_family); } + + long getPortNumber(const kt::SocketAddress& address) + { + kt::InternetProtocolVersion version = getInternetProtocolVersion(address); + if (version == kt::InternetProtocolVersion::IPV6) + { + return htonl(address.ipv6.sin6_port); + } + // I believe the address is in the same position for ipv4 and ipv6 structs, so it doesn't really matter. + // Doing the checks anway to make sure its fine + return htonl(address.ipv4.sin_port); + } + + std::optional resolveToAddress(const kt::SocketAddress& address) + { + const kt::InternetProtocolVersion protocolVersion = getInternetProtocolVersion(address); + const size_t addressLength = protocolVersion == kt::InternetProtocolVersion::IPV6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN; + std::string asString; + asString.resize(addressLength); + + if (protocolVersion == kt::InternetProtocolVersion::IPV6) + { + inet_ntop(static_cast(protocolVersion), &address.ipv6.sin6_addr, &asString[0], addressLength); + } + else + { + inet_ntop(static_cast(protocolVersion), &address.ipv4.sin_addr, &asString[0], addressLength); + } + + // Removing trailing \0 bytes + const size_t delimiterIndex = asString.find_first_of('\0'); + if (delimiterIndex != std::string::npos) + { + asString = asString.substr(0, delimiterIndex); + } + // Since we zero out the address, we need to check its not default initialised + return !asString.empty() && asString != "0.0.0.0" && asString != "::" ? std::optional{asString} : std::nullopt; + } } diff --git a/src/address/SocketAddress.h b/src/address/SocketAddress.h index a76e8bd..373addf 100644 --- a/src/address/SocketAddress.h +++ b/src/address/SocketAddress.h @@ -3,6 +3,9 @@ #include "../enums/InternetProtocolVersion.h" +#include +#include + #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN @@ -36,5 +39,9 @@ namespace kt sockaddr_in6 ipv6; } SocketAddress; - kt::InternetProtocolVersion getInternetProtocolVersion(kt::SocketAddress&); + kt::InternetProtocolVersion getInternetProtocolVersion(const kt::SocketAddress&); + + long getPortNumber(const kt::SocketAddress&); + + std::optional resolveToAddress(const kt::SocketAddress&); } diff --git a/src/serversocket/ServerSocket.cpp b/src/serversocket/ServerSocket.cpp index 7c505a2..7f9cfc9 100644 --- a/src/serversocket/ServerSocket.cpp +++ b/src/serversocket/ServerSocket.cpp @@ -366,7 +366,7 @@ namespace kt } unsigned int portNum = this->getInternetProtocolVersion() == kt::InternetProtocolVersion::IPV6 ? htons(acceptedAddress.ipv6.sin6_port) : htons(acceptedAddress.ipv4.sin_port); - std::optional hostname = kt::resolveToAddress(&acceptedAddress, this->getInternetProtocolVersion()); + std::optional hostname = kt::resolveToAddress(acceptedAddress); if (!hostname.has_value()) { throw kt::SocketException("Unable to resolve accepted hostname from accepted socket."); diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 2b06154..a891a00 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -560,7 +560,7 @@ namespace kt { if (this->protocol == kt::SocketProtocol::UDP) { - return kt::resolveToAddress(&this->receiveAddress, this->getInternetProtocolVersion()); + return kt::resolveToAddress(this->receiveAddress); } return std::nullopt; @@ -577,31 +577,6 @@ namespace kt return std::nullopt; } - std::optional resolveToAddress(const kt::SocketAddress* address, const kt::InternetProtocolVersion protocolVersion) - { - const size_t addressLength = protocolVersion == kt::InternetProtocolVersion::IPV6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN; - std::string asString; - asString.resize(addressLength); - - if (protocolVersion == kt::InternetProtocolVersion::IPV6) - { - inet_ntop(static_cast(protocolVersion), &address->ipv6.sin6_addr, &asString[0], addressLength); - } - else - { - inet_ntop(static_cast(protocolVersion), &address->ipv4.sin_addr, &asString[0], addressLength); - } - - // Removing trailing \0 bytes - const size_t delimiterIndex = asString.find_first_of('\0'); - if (delimiterIndex != std::string::npos) - { - asString = asString.substr(0, delimiterIndex); - } - // Since we zero out the address, we need to check its not default initialised - return !asString.empty() && asString != "0.0.0.0" && asString != "::" ? std::optional{asString} : std::nullopt; - } - /** * @return the hostname configured for this socket. */ diff --git a/src/socket/Socket.h b/src/socket/Socket.h index a1dd892..23c81f6 100644 --- a/src/socket/Socket.h +++ b/src/socket/Socket.h @@ -107,7 +107,6 @@ namespace kt static std::optional getLocalMACAddress(); }; - std::optional resolveToAddress(const kt::SocketAddress*, const kt::InternetProtocolVersion); int pollSocket(const SOCKET& socketDescriptor, const long& timeout, timeval* timeOutVal = nullptr); } // End namespace kt