1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-24 21:41:04 +00:00

Added LAIKAPKT_HANDSHAKE_REQ support to cnc.c

- minor refactoring
- fixed CMakeLists.txt for cnc & bot
This commit is contained in:
CPunch 2022-01-25 12:13:04 -06:00
parent 04f02b4371
commit 2a0e34dd5a
6 changed files with 46 additions and 19 deletions

View File

@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(BOT_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) 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 # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)

View File

@ -11,6 +11,8 @@ void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) {
case LAIKAPKT_HANDSHAKE_RES: { case LAIKAPKT_HANDSHAKE_RES: {
uint8_t endianness = laikaS_readByte(&peer->sock); uint8_t endianness = laikaS_readByte(&peer->sock);
peer->sock.flipEndian = endianness != laikaS_isBigEndian(); peer->sock.flipEndian = endianness != laikaS_isBigEndian();
LAIKA_DEBUG("handshake accepted by cnc!\n")
break; break;
} }
default: default:
@ -68,15 +70,15 @@ bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
LAIKA_TRY LAIKA_TRY
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer)) if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
goto _BKill; goto _BOTKILL;
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer)) if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
goto _BKill; goto _BOTKILL;
if (!evnt->pollIn && !evnt->pollOut) if (!evnt->pollIn && !evnt->pollOut)
goto _BKill; goto _BOTKILL;
LAIKA_CATCH LAIKA_CATCH
_BKill: _BOTKILL:
laikaS_kill(&bot->peer->sock); laikaS_kill(&bot->peer->sock);
LAIKA_TRYEND LAIKA_TRYEND

View File

@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CNC_INCLUDEDIR ${CMAKE_CURRENT_SOURCE_DIR}/include) 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 # Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)

View File

@ -1,4 +1,5 @@
#include "lmem.h" #include "lmem.h"
#include "lsocket.h"
#include "lerror.h" #include "lerror.h"
#include "cnc.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) { 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) { 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) { void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
printf("peer %x killed!\n", peer); printf("peer %x killed!\n", peer);
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)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) { bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
@ -76,15 +98,16 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
LAIKA_TRY LAIKA_TRY
if (evnts[i].pollIn && !laikaS_handlePeerIn(peer)) if (evnts[i].pollIn && !laikaS_handlePeerIn(peer))
laikaC_killPeer(cnc, peer); goto _CNCKILL;
if (evnts[i].pollOut && !laikaS_handlePeerOut(peer)) if (evnts[i].pollOut && !laikaS_handlePeerOut(peer))
laikaC_killPeer(cnc, peer); goto _CNCKILL;
if (!evnts[i].pollIn && !evnts[i].pollOut) if (!evnts[i].pollIn && !evnts[i].pollOut)
laikaC_killPeer(cnc, peer); goto _CNCKILL;
LAIKA_CATCH LAIKA_CATCH
_CNCKILL:
laikaC_killPeer(cnc, peer); laikaC_killPeer(cnc, peer);
LAIKA_TRYEND LAIKA_TRYEND
} }

View File

@ -71,14 +71,7 @@ struct sLaika_socket {
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET) #define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
inline bool laikaS_isBigEndian(void) { bool laikaS_isBigEndian(void);
union {
uint32_t i;
uint8_t c[4];
} _indxint = {0xDEADB33F};
return _indxint.c[0] == 0xDE;
}
void laikaS_init(void); void laikaS_init(void);
void laikaS_cleanUp(void); void laikaS_cleanUp(void);

View File

@ -7,6 +7,15 @@
static int _LNSetup = 0; 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) { void laikaS_init(void) {
if (_LNSetup++ > 0) if (_LNSetup++ > 0)
return; /* WSA is already setup! */ return; /* WSA is already setup! */