From 02c3176bc455664984f42b4fb9a02128d506395a Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 18 Feb 2022 19:32:16 -0600 Subject: [PATCH] Added laikaS_startVarPacket() & laikaS_endVarPacket() --- lib/include/lpacket.h | 2 +- lib/include/lpeer.h | 4 +++- lib/src/lpeer.c | 36 +++++++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 64eea02..de9476c 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -56,8 +56,8 @@ enum { */ LAIKAPKT_VARPKT_REQ, /* layout of LAIKAPKT_VARPKT_REQ: - * LAIKAPKT_ID pktID; * LAIKAPKT_SIZE pktSize; + * LAIKAPKT_ID pktID; */ LAIKAPKT_MAXNONE }; diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 14642aa..f7c7f75 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -39,8 +39,10 @@ struct sLaika_peer *laikaS_newPeer(PeerPktHandler *handlers, LAIKAPKT_SIZE *pktS void laikaS_freePeer(struct sLaika_peer *peer); void laikaS_setSecure(struct sLaika_peer *peer, bool flag); -void laikaS_startOutPacket(struct sLaika_peer *peer, uint8_t id); +void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id); int laikaS_endOutPacket(struct sLaika_peer *peer); +void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id); +int laikaS_endVarPacket(struct sLaika_peer *peer); bool laikaS_handlePeerIn(struct sLaika_peer *peer); bool laikaS_handlePeerOut(struct sLaika_peer *peer); diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index 5972931..2fd9525 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -28,7 +28,7 @@ void laikaS_freePeer(struct sLaika_peer *peer) { laikaM_free(peer); } -void laikaS_startOutPacket(struct sLaika_peer *peer, uint8_t id) { +void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) { struct sLaika_socket *sock = &peer->sock; if (peer->outStart != -1) { /* sanity check */ @@ -72,6 +72,28 @@ int laikaS_endOutPacket(struct sLaika_peer *peer) { return sz; } +void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) { + struct sLaika_socket *sock = &peer->sock; + + if (peer->outStart != -1) { /* sanity check */ + LAIKA_ERROR("unended OUT packet!\n") + } + + laikaS_writeByte(sock, LAIKAPKT_VARPKT_REQ); + laikaS_zeroWrite(sock, sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */ + laikaS_startOutPacket(peer, id); +} + +int laikaS_endVarPacket(struct sLaika_peer *peer) { + struct sLaika_socket *sock = &peer->sock; + int patchIndx = peer->outStart - 3; /* gets index of packet size */ + LAIKAPKT_SIZE sz = (LAIKAPKT_SIZE)laikaS_endOutPacket(peer); + + /* patch packet size */ + memcpy((void*)&sock->outBuf[patchIndx], (void*)&sz, sizeof(LAIKAPKT_SIZE)); + return sz; +} + void laikaS_startInPacket(struct sLaika_peer *peer) { struct sLaika_socket *sock = &peer->sock; @@ -151,6 +173,12 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) { if (recvd != sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE)) LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT_REQ") + /* read packet size */ + laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE)); + + if (peer->pktSize > LAIKA_MAX_PKTSIZE) + LAIKA_ERROR("variable packet too large!") + /* read pktID */ peer->pktID = laikaS_readByte(&peer->sock); @@ -158,12 +186,6 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) { if (peer->pktID >= LAIKAPKT_MAXNONE || peer->pktSizeTable[peer->pktID] != 0 || peer->handlers[peer->pktID] == NULL) LAIKA_ERROR("requested packet id [%d] is not variadic!", peer->pktID) - /* read packet size */ - laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE)); - - if (peer->pktSize > LAIKA_MAX_PKTSIZE) - LAIKA_ERROR("variable packet too large!") - /* if peer->useSecure is true, body is encrypted */ laikaS_startInPacket(peer); break;