Add check for endianness, fix strtoa for ipv4

This commit is contained in:
Stephen Seo 2019-07-07 14:44:37 +09:00
parent f10c53d92c
commit 132508d5cc
2 changed files with 30 additions and 2 deletions

View file

@ -27,6 +27,8 @@ struct Context {
bool VerifyContext(void *ctx);
bool isBigEndian();
} // namespace UDPC
#endif

View file

@ -1,6 +1,9 @@
#include "UDPC_Defines.hpp"
#include "UDPConnection.h"
#include <optional>
UDPC::Context::Context(bool isThreaded)
: _contextIdentifier(UDPC_CONTEXT_IDENTIFIER), flags(),
isAcceptNewConnections(true), protocolID(UDPC_DEFAULT_PROTOCOL_ID),
@ -29,6 +32,21 @@ bool UDPC::VerifyContext(void *ctx) {
}
}
bool UDPC::isBigEndian() {
static std::optional<bool> isBigEndian = {};
if(isBigEndian) {
return *isBigEndian;
}
union {
uint32_t i;
char c[4];
} bint = { 0x01020304 };
isBigEndian = (bint.c[0] == 1);
return *isBigEndian;
}
void *UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) {
UDPC::Context *ctx = new UDPC::Context(false);
@ -156,7 +174,11 @@ uint32_t UDPC_strtoa(const char *addrStr) {
temp *= 10;
temp += *addrStr - '0';
} else if (*addrStr == '.' && temp <= 0xFF && index < 3) {
addr |= (temp << (8 * index++));
if(UDPC::isBigEndian()) {
addr |= (temp << (24 - 8 * index++));
} else {
addr |= (temp << (8 * index++));
}
temp = 0;
} else {
return 0;
@ -165,7 +187,11 @@ uint32_t UDPC_strtoa(const char *addrStr) {
}
if (index == 3 && temp <= 0xFF) {
addr |= temp << 24;
if(UDPC::isBigEndian()) {
addr |= temp;
} else {
addr |= temp << 24;
}
return addr;
} else {
return 0;