Network/HostnameInfo: Replace family/socketType raw value by engine enumeration

Former-commit-id: 40adb7654b5a741c5fa12c1b866e3b427b1be5e9
This commit is contained in:
Lynix 2015-11-14 02:12:10 +01:00
parent 1b4ac70ac3
commit e8aa649cfc
10 changed files with 70 additions and 18 deletions

View File

@ -30,8 +30,9 @@ namespace Nz
NetProtocol_Any,
NetProtocol_IPv4,
NetProtocol_IPv6,
NetProtocol_Unknown,
NetProtocol_Max = NetProtocol_IPv6
NetProtocol_Max = NetProtocol_Unknown
};
enum SocketError
@ -71,8 +72,9 @@ namespace Nz
SocketType_Raw,
SocketType_TCP,
SocketType_UDP,
SocketType_Unknown,
SocketType_Max = SocketType_UDP
SocketType_Max = SocketType_Unknown
};
}

View File

@ -89,10 +89,9 @@ namespace Nz
struct HostnameInfo
{
IpAddress address;
NetProtocol protocol;
SocketType socketType;
String canonicalName;
int flags;
int family; //< TODO: NetProtocol
int socketType; //< TODO: SocketType
};
}

View File

@ -118,6 +118,7 @@ namespace Nz
switch (first.m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;
case NetProtocol_IPv4:
@ -167,6 +168,7 @@ namespace Nz
switch (first.m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;
case NetProtocol_IPv4:

View File

@ -31,11 +31,13 @@ namespace Nz
inline SocketState TcpServer::Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize)
{
NazaraAssert(protocol != NetProtocol_Any, "Any protocol not supported for Listen"); //< TODO
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
IpAddress any;
switch (protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected;

View File

@ -30,6 +30,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected;
@ -48,6 +49,8 @@ namespace Nz
bool UdpSocket::Create(NetProtocol protocol)
{
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
return Open(protocol);
}

View File

@ -57,6 +57,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;
case NetProtocol_IPv4:
@ -80,6 +81,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;
case NetProtocol_IPv4:
@ -151,6 +153,8 @@ namespace Nz
String IpAddress::ResolveAddress(const IpAddress& address, String* service, ResolveError* error)
{
NazaraAssert(address.IsValid(), "Invalid address");
String hostname;
IpAddressImpl::ResolveAddress(address, &hostname, service, error);
@ -159,6 +163,8 @@ namespace Nz
std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const String& hostname, const String& service, ResolveError* error)
{
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
return IpAddressImpl::ResolveHostname(protocol, hostname, service, error);
}

View File

@ -57,10 +57,12 @@ namespace Nz
IpAddress hostnameAddress;
for (const HostnameInfo& result : results)
{
//TODO: Check PF_ type (TCP)
if (!result.address)
continue;
if (result.socketType != SocketType_TCP)
continue;
hostnameAddress = result.address;
break; //< Take first valid address
}

View File

@ -21,12 +21,12 @@ namespace Nz
return GetAddrInfoW(hostname.GetWideString().c_str(), service.GetWideString().c_str(), &hints, &servinfo);
}
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service)
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
{
std::array<wchar_t, NI_MAXHOST> hostnameBuffer;
std::array<wchar_t, NI_MAXSERV> serviceBuffer;
int result = GetNameInfoW(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), NI_NUMERICSERV);
int result = GetNameInfoW(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), flags);
if (result == 0)
{
if (hostname)
@ -51,12 +51,12 @@ namespace Nz
return getaddrinfo(hostname.GetConstBuffer(), service.GetConstBuffer(), hints, results);
}
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service)
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
{
std::array<char, NI_MAXHOST> hostnameBuffer;
std::array<char, NI_MAXSERV> serviceBuffer;
int result = getnameinfo(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), NI_NUMERICSERV);
int result = getnameinfo(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), flags);
if (result == 0)
{
if (hostname)
@ -153,7 +153,7 @@ namespace Nz
SockAddrBuffer socketAddress;
socklen_t socketAddressLen = ToSockAddr(ipAddress, socketAddress.data());
if (Detail::GetHostnameInfo(reinterpret_cast<sockaddr*>(socketAddress.data()), socketAddressLen, hostname, service) != 0)
if (Detail::GetHostnameInfo(reinterpret_cast<sockaddr*>(socketAddress.data()), socketAddressLen, hostname, service, NI_NUMERICSERV) != 0)
{
if (error)
*error = TranslateWSAErrorToResolveError(WSAGetLastError());
@ -196,9 +196,8 @@ namespace Nz
HostnameInfo result;
result.address = FromAddrinfo(p);
result.canonicalName = String::Unicode(p->ai_canonname);
result.family = p->ai_family;
result.flags = p->ai_flags;
result.socketType = p->ai_socktype;
result.protocol = TranslatePFToNetProtocol(p->ai_family);
result.socketType = TranslateSockToNetProtocol(p->ai_socktype);
results.push_back(result);
}
@ -252,6 +251,39 @@ namespace Nz
return 0;
}
NetProtocol IpAddressImpl::TranslatePFToNetProtocol(int family)
{
switch (family)
{
case PF_INET:
return NetProtocol_IPv4;
case PF_INET6:
return NetProtocol_IPv6;
default:
return NetProtocol_Unknown;
}
}
SocketType IpAddressImpl::TranslateSockToNetProtocol(int socketType)
{
switch (socketType)
{
case SOCK_STREAM:
return SocketType_TCP;
case SOCK_DGRAM:
return SocketType_UDP;
case SOCK_RAW:
return SocketType_Raw;
default:
return SocketType_Unknown;
}
}
ResolveError IpAddressImpl::TranslateWSAErrorToResolveError(int error)
{
switch (error)

View File

@ -26,6 +26,8 @@ namespace Nz
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error);
static socklen_t ToSockAddr(const IpAddress& ipAddress, void* buffer);
static NetProtocol TranslatePFToNetProtocol(int family);
static SocketType TranslateSockToNetProtocol(int socketType);
static ResolveError TranslateWSAErrorToResolveError(int error);
};
}

View File

@ -646,7 +646,8 @@ namespace Nz
static int addressFamily[] = {
AF_UNSPEC, //< NetProtocol_Any
AF_INET, //< NetProtocol_IPv4
AF_INET6 //< NetProtocol_IPv6
AF_INET6, //< NetProtocol_IPv6
-1 //< NetProtocol_Unknown
};
static_assert(sizeof(addressFamily) / sizeof(int) == NetProtocol_Max + 1, "Address family array is incomplete");
@ -658,9 +659,10 @@ namespace Nz
NazaraAssert(type <= SocketType_Max, "Socket type has value out of enum");
static int socketType[] = {
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM //< SocketType_UDP
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM, //< SocketType_UDP
-1 //< SocketType_Unknown
};
static_assert(sizeof(socketType) / sizeof(int) == SocketType_Max + 1, "Socket type array is incomplete");