From f8950fa08e82b70a98517418a643e565179ac32d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 4 Mar 2019 19:05:07 +0900 Subject: [PATCH] Fix endianness of addr, minor fixes --- src/UDPC_Defines.h | 3 ++- src/UDPConnection.c | 11 ++++++----- src/test/UDPC_UnitTest.c | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/UDPC_Defines.h b/src/UDPC_Defines.h index 68ded16..650f14a 100644 --- a/src/UDPC_Defines.h +++ b/src/UDPC_Defines.h @@ -37,7 +37,8 @@ static const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread"; #define UDPC_BAD_MODE_SEND_INTERVAL (1.0f/10.0f) #define UDPC_TIMEOUT_SECONDS 10.0f #define UDPC_HEARTBEAT_PKT_INTERVAL (15.0f/100.0f) -#define UDPC_INIT_PKT_INTERVAL 5.0f +#define UDPC_INIT_PKT_INTERVAL 5 +#define UDPC_INIT_PKT_INTERVAL_F ((float)UDPC_INIT_PKT_INTERVAL) #define UDPC_PKT_PROTOCOL_ID 1357924680 #define UDPC_ID_CONNECT 0x80000000 diff --git a/src/UDPConnection.c b/src/UDPConnection.c index 827a30e..653c238 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -320,7 +320,8 @@ void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t }; timespec_get(&cd.received, TIME_UTC); - // only set "received" to now, since "sent" will be set after sending packet + cd.sent = cd.received; + cd.sent.tv_sec -= UDPC_INIT_PKT_INTERVAL + 1; UDPC_HashMap_insert(ctx->conMap, addr, &cd); } @@ -778,7 +779,7 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data) if((cd->flags & 0x8) != 0) { // initiate connection to server - if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL) + if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL_F) { return; } @@ -1235,7 +1236,7 @@ char* UDPC_INTERNAL_atostr(UDPC_Context *ctx, uint32_t addr) int index = 0; for(int x = 0; x < 4; ++x) { - unsigned char temp = (addr >> (24 - x * 8)) & 0xFF; + unsigned char temp = (addr >> (x * 8)) & 0xFF; if(temp >= 100) { @@ -1293,7 +1294,7 @@ uint32_t UDPC_strtoa(const char *addrStr) } else if(*addrStr == '.' && temp <= 0xFF && index < 3) { - addr |= (temp << (24 - 8 * index++)); + addr |= (temp << (8 * index++)); temp = 0; } else @@ -1305,7 +1306,7 @@ uint32_t UDPC_strtoa(const char *addrStr) if(index == 3 && temp <= 0xFF) { - addr |= temp; + addr |= temp << 24; return addr; } else diff --git a/src/test/UDPC_UnitTest.c b/src/test/UDPC_UnitTest.c index 2229ec8..353c9bd 100644 --- a/src/test/UDPC_UnitTest.c +++ b/src/test/UDPC_UnitTest.c @@ -246,7 +246,7 @@ void TEST_ATOSTR() { UDPC_Context ctx; ASSERT_EQ_MEM( - UDPC_INTERNAL_atostr(&ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF), + UDPC_INTERNAL_atostr(&ctx, (0xFF << 24) | (0x1 << 16) | (0x1E << 8) | 0xAC), "172.30.1.255", 13); UNITTEST_REPORT(ATOSTR); @@ -387,9 +387,9 @@ void TEST_HASHMAP() void TEST_STRTOA() { - ASSERT_EQ(0x01020304, UDPC_strtoa("1.2.3.4")); - ASSERT_EQ(0x7F000001, UDPC_strtoa("127.0.0.1")); - ASSERT_EQ(0xC0A801FF, UDPC_strtoa("192.168.1.255")); + ASSERT_EQ(0x04030201, UDPC_strtoa("1.2.3.4")); + ASSERT_EQ(0x0100007F, UDPC_strtoa("127.0.0.1")); + ASSERT_EQ(0xFF01A8C0, UDPC_strtoa("192.168.1.255")); ASSERT_EQ(0, UDPC_strtoa("1.2.3.4.5")); ASSERT_EQ(0, UDPC_strtoa("100.20.30")); ASSERT_EQ(0, UDPC_strtoa("200.400.30.50"));