1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-12-27 05:10:06 +00:00

Added LAIKAPKT_VAR_REQ, enabling variable packets

- additional minor refactoring
This commit is contained in:
CPunch 2022-01-25 13:53:17 -06:00
parent 2a0e34dd5a
commit 22df7e3702
5 changed files with 35 additions and 9 deletions

View File

@ -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);

View File

@ -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];

View File

@ -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

View File

@ -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);

View File

@ -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)