Changeset View
Changeset View
Standalone View
Standalone View
source/network/StunClient.cpp
Show First 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | bool GetFromBuffer(const std::vector<u8>& buffer, u32& offset, T& result) | ||||
while (a--) | while (a--) | ||||
{ | { | ||||
result <<= 8; | result <<= 8; | ||||
result += buffer[offset - 1 - a]; | result += buffer[offset - 1 - a]; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool GetByteFromBuffer(const std::vector<u8>& buffer, u32& offset, u8& result) | |||||
leper: I guess one could provide a specialization of the other function so someone calling that with… | |||||
elexisAuthorUnsubmitted Not Done Inline ActionsSince n is a deduced argument, it could only be template<u8> bool GetFromBuffer(const std::vector<u8>& buffer, u32& offset, u8& result) But then I still have the clang compile warning. Since this function copies one line, we can just keep that one function and add that n=1 special case to that function if we can't resolve the specialization however. I believe it might be possible to do something with template constraints http://en.cppreference.com/w/cpp/language/constraints (search for size on that page), but that looks alien and might even end up more complicated and longer. elexis: Since `n` is a deduced argument, it could only be
```
template<u8>
bool GetFromBuffer(const std… | |||||
{ | |||||
if (offset + 1 > buffer.size()) | |||||
return false; | |||||
result = buffer[offset++]; | |||||
return true; | |||||
} | |||||
/** | /** | ||||
* Creates a STUN request and sends it to a STUN server. | * Creates a STUN request and sends it to a STUN server. | ||||
* The request is sent through transactionHost, from which the answer | * The request is sent through transactionHost, from which the answer | ||||
* will be retrieved by ReceiveStunResponse and interpreted by ParseStunResponse. | * will be retrieved by ReceiveStunResponse and interpreted by ParseStunResponse. | ||||
*/ | */ | ||||
bool CreateStunRequest(ENetHost* transactionHost) | bool CreateStunRequest(ENetHost* transactionHost) | ||||
{ | { | ||||
ENSURE(transactionHost); | ENSURE(transactionHost); | ||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | bool ParseStunResponse(const std::vector<u8>& buffer) | ||||
{ | { | ||||
LOGERROR("STUN response doesn't contain the magic cookie"); | LOGERROR("STUN response doesn't contain the magic cookie"); | ||||
return false; | return false; | ||||
} | } | ||||
for (std::size_t i = 0; i < sizeof(m_TransactionID); ++i) | for (std::size_t i = 0; i < sizeof(m_TransactionID); ++i) | ||||
{ | { | ||||
u8 transactionChar = 0; | u8 transactionChar = 0; | ||||
if (!GetFromBuffer<u8, 1>(buffer, offset, transactionChar) || transactionChar != m_TransactionID[i]) | if (!GetByteFromBuffer(buffer, offset, transactionChar) || transactionChar != m_TransactionID[i]) | ||||
{ | { | ||||
LOGERROR("STUN response doesn't contain the transaction ID"); | LOGERROR("STUN response doesn't contain the transaction ID"); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
while (offset < buffer.size()) | while (offset < buffer.size()) | ||||
{ | { | ||||
Show All 19 Lines | case m_AttrTypeXORMappedAddress: | ||||
LOGERROR("Invalid STUN Mapped Address length"); | LOGERROR("Invalid STUN Mapped Address length"); | ||||
return false; | return false; | ||||
} | } | ||||
// Ignore the first byte as mentioned in Section 15.1 of RFC 5389. | // Ignore the first byte as mentioned in Section 15.1 of RFC 5389. | ||||
++offset; | ++offset; | ||||
u8 ipFamily = 0; | u8 ipFamily = 0; | ||||
if (!GetFromBuffer<u8, 1>(buffer, offset, ipFamily) || ipFamily != m_IPAddressFamilyIPv4) | if (!GetByteFromBuffer(buffer, offset, ipFamily) || ipFamily != m_IPAddressFamilyIPv4) | ||||
{ | { | ||||
LOGERROR("Unsupported address family, IPv4 is expected"); | LOGERROR("Unsupported address family, IPv4 is expected"); | ||||
return false; | return false; | ||||
} | } | ||||
u16 port = 0; | u16 port = 0; | ||||
u32 ip = 0; | u32 ip = 0; | ||||
if (!GetFromBuffer<u16, 2>(buffer, offset, port) || | if (!GetFromBuffer<u16, 2>(buffer, offset, port) || | ||||
▲ Show 20 Lines • Show All 111 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
I guess one could provide a specialization of the other function so someone calling that with u8 does not run into the same issue again.