From ed3efdaf112ce48363a348c505b878d6f36853ef Mon Sep 17 00:00:00 2001 From: CPunch Date: Mon, 24 Jan 2022 20:50:14 -0600 Subject: [PATCH] Added laikaS_readInt() & laikaS_writeInt() - new member for the sLaika_sock struct, 'flipEndian' --- lib/include/lmem.h | 1 - lib/include/lpeer.h | 6 +++--- lib/include/lsocket.h | 13 ++++++++++++- lib/src/lpeer.c | 2 +- lib/src/lsocket.c | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/include/lmem.h b/lib/include/lmem.h index 021bd52..a39a12a 100644 --- a/lib/include/lmem.h +++ b/lib/include/lmem.h @@ -7,7 +7,6 @@ #define laikaM_malloc(sz) laikaM_realloc(NULL, sz) #define laikaM_free(buf) laikaM_realloc(buf, 0) -#define laikaM_free(buf) laikaM_realloc(buf, 0) #define laikaM_growarray(type, buf, count, capacity) \ if (count >= capacity || buf == NULL) { \ diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 14c7196..8297a8a 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -7,16 +7,16 @@ #include "lpolllist.h" struct sLaika_peer { - struct sLaika_socket sock; + struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */ struct sLaika_pollList *pList; /* pollList we're active in */ void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id); size_t pktSize; /* current pkt size */ LAIKAPKT_ID pktID; /* current pkt ID */ size_t *pktSizeTable; /* const table to pull pkt size data from */ - bool setPollOut; /* if 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, LAIKAPKT_ID id), struct sLaika_pollList *pList, size_t *pktSizeTable); +struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), size_t *pktSizeTable, struct sLaika_pollList *pList); void laikaS_freePeer(struct sLaika_peer *peer); bool laikaS_handlePeerIn(struct sLaika_peer *peer); diff --git a/lib/include/lsocket.h b/lib/include/lsocket.h index 6a55dff..e5071fd 100644 --- a/lib/include/lsocket.h +++ b/lib/include/lsocket.h @@ -62,10 +62,20 @@ struct sLaika_socket { int inCount; int outCap; int inCap; + bool flipEndian }; #define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET) +inline 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_cleanUp(void); @@ -79,9 +89,10 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock); void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */ void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */ - void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data); uint8_t laikaS_readByte(struct sLaika_socket *sock); +void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz); /* reads INT, respecting endianness */ +void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz); /* writes INT, respecting endianness */ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed); RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed); \ No newline at end of file diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index 0b0199a..133027e 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, LAIKAPKT_ID id), struct sLaika_pollList *pList, size_t *pktSizeTable) { +struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), size_t *pktSizeTable, struct sLaika_pollList *pList) { struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer)); laikaS_initSocket(&peer->sock); diff --git a/lib/src/lsocket.c b/lib/src/lsocket.c index 16a33c0..70c6a27 100644 --- a/lib/src/lsocket.c +++ b/lib/src/lsocket.c @@ -1,3 +1,5 @@ +#include + #include "lerror.h" #include "lmem.h" #include "lpolllist.h" @@ -34,6 +36,7 @@ void laikaS_initSocket(struct sLaika_socket *sock) { sock->outBuf = NULL; sock->outCap = ARRAY_START; sock->outCount = 0; + sock->flipEndian = false; laikaS_init(); return sock; @@ -186,6 +189,38 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock) { return tmp; } +void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz) { + if (sock->flipEndian) { + uint8_t tmp[sz]; /* allocate tmp buffer to hold data while we switch endianness */ + int k; + + laikaS_read(sock, (void*)tmp, sz); + + /* copy tmp buffer to user buffer, flipping endianness */ + for (k = 0; k < sz; k++) + *(uint8_t*)(buf + k) = tmp[sz - k - 1]; + } else { + /* just a wrapper for laikaS_read */ + laikaS_read(sock, buf, sz); + } +} + +void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz) { + if (sock->flipEndian) { + uint8_t tmp[sz]; /* allocate tmp buffer to hold data while we switch endianness */ + int k; + + /* copy user buffer to tmp buffer, flipping endianness */ + for (k = 0; k < sz; k++) + tmp[k] = *(uint8_t*)(buf + (sz - k - 1)); + + laikaS_write(sock, (void*)tmp, sz); + } else { + /* just a wrapper for laikaS_write */ + laikaS_write(sock, buf, sz); + } +} + RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed) { RAWSOCKCODE errCode = RAWSOCK_OK; int rcvd, start = sock->inCount;