Skip to content

Commit

Permalink
Move resolveToAddress() to SocketAddress.cpp and update signature.
Browse files Browse the repository at this point in the history
Add getPortNumber() for SocketAddresses.
  • Loading branch information
Kilemonn committed Jun 23, 2024
1 parent fcf0943 commit 6d7684f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
43 changes: 42 additions & 1 deletion src/address/SocketAddress.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
#include "SocketAddress.h"

#include <optional>
#include <string>

namespace kt
{
kt::InternetProtocolVersion getInternetProtocolVersion(kt::SocketAddress& address)
kt::InternetProtocolVersion getInternetProtocolVersion(const kt::SocketAddress& address)
{
return static_cast<kt::InternetProtocolVersion>(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<std::string> 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<int>(protocolVersion), &address.ipv6.sin6_addr, &asString[0], addressLength);
}
else
{
inet_ntop(static_cast<int>(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<std::string>{asString} : std::nullopt;
}
}
9 changes: 8 additions & 1 deletion src/address/SocketAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include "../enums/InternetProtocolVersion.h"

#include <string>
#include <optional>

#ifdef _WIN32

#ifndef WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -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<std::string> resolveToAddress(const kt::SocketAddress&);
}
2 changes: 1 addition & 1 deletion src/serversocket/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> hostname = kt::resolveToAddress(&acceptedAddress, this->getInternetProtocolVersion());
std::optional<std::string> hostname = kt::resolveToAddress(acceptedAddress);
if (!hostname.has_value())
{
throw kt::SocketException("Unable to resolve accepted hostname from accepted socket.");
Expand Down
27 changes: 1 addition & 26 deletions src/socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -577,31 +577,6 @@ namespace kt
return std::nullopt;
}

std::optional<std::string> 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<int>(protocolVersion), &address->ipv6.sin6_addr, &asString[0], addressLength);
}
else
{
inet_ntop(static_cast<int>(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<std::string>{asString} : std::nullopt;
}

/**
* @return the hostname configured for this socket.
*/
Expand Down
1 change: 0 additions & 1 deletion src/socket/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ namespace kt
static std::optional<std::string> getLocalMACAddress();
};

std::optional<std::string> resolveToAddress(const kt::SocketAddress*, const kt::InternetProtocolVersion);
int pollSocket(const SOCKET& socketDescriptor, const long& timeout, timeval* timeOutVal = nullptr);

} // End namespace kt

0 comments on commit 6d7684f

Please sign in to comment.