lsocket.[ch] & lpeer.c: migrated to new vector API

This commit is contained in:
CPunch 2022-09-01 19:05:56 -05:00
parent af09e74263
commit 13398dbdf6
4 changed files with 50 additions and 53 deletions

View File

@ -3,8 +3,8 @@
#include "hashmap.h" #include "hashmap.h"
#include "laika.h" #include "laika.h"
#include "lsocket.h"
#include "lmem.h" #include "lmem.h"
#include "lsocket.h"
#include <stdbool.h> #include <stdbool.h>
@ -21,7 +21,8 @@ struct sLaika_pollEvent
struct sLaika_pollList struct sLaika_pollList
{ {
struct hashmap *sockets; struct hashmap *sockets;
laikaM_newVector(struct sLaika_socket *, outQueue); /* holds sockets which have data needed to be sent */ /* holds sockets which have data needed to be sent */
laikaM_newVector(struct sLaika_socket *, outQueue);
laikaM_newVector(struct sLaika_pollEvent, revents); laikaM_newVector(struct sLaika_pollEvent, revents);
#ifdef LAIKA_USE_EPOLL #ifdef LAIKA_USE_EPOLL
/* epoll */ /* epoll */

View File

@ -55,6 +55,7 @@ typedef void buffer_t;
#endif #endif
#include "laika.h" #include "laika.h"
#include "lsodium.h" #include "lsodium.h"
#include "lmem.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
@ -79,12 +80,8 @@ struct sLaika_socket
pollEvent onPollIn; pollEvent onPollIn;
pollEvent onPollOut; pollEvent onPollOut;
void *uData; /* passed to onPollFail */ void *uData; /* passed to onPollFail */
uint8_t *outBuf; /* raw data to be sent() */ laikaM_newVector(uint8_t, outBuf); /* raw data to be sent() */
uint8_t *inBuf; /* raw data we recv()'d */ laikaM_newVector(uint8_t, inBuf); /* raw data we recv()'d */
int outCount;
int inCount;
int outCap;
int inCap;
bool flipEndian; bool flipEndian;
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */ bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
}; };

View File

@ -71,7 +71,7 @@ void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
laikaS_writeByte(sock, id); laikaS_writeByte(sock, id);
peer->outStart = sock->outCount; peer->outStart = laikaM_countVector(sock->outBuf);
if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the
packet ID */ packet ID */
uint8_t nonce[crypto_secretbox_NONCEBYTES]; uint8_t nonce[crypto_secretbox_NONCEBYTES];
@ -88,26 +88,26 @@ int laikaS_endOutPacket(struct sLaika_peer *peer)
if (peer->useSecure) { if (peer->useSecure) {
/* make sure we have enough space */ /* make sure we have enough space */
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount, laikaM_growVector(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES);
sock->outCap);
/* packet body starts after the id & nonce */ /* packet body starts after the id & nonce */
body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES]; body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES];
/* encrypt packet body in-place */ /* encrypt packet body in-place */
if (crypto_secretbox_easy(body, body, if (crypto_secretbox_easy(body, body,
(sock->outCount - peer->outStart) - crypto_secretbox_NONCEBYTES, (laikaM_countVector(sock->outBuf) - peer->outStart) -
crypto_secretbox_NONCEBYTES,
&sock->outBuf[peer->outStart], peer->outKey) != 0) { &sock->outBuf[peer->outStart], peer->outKey) != 0) {
LAIKA_ERROR("Failed to encrypt packet!\n"); LAIKA_ERROR("Failed to encrypt packet!\n");
} }
sock->outCount += crypto_secretbox_MACBYTES; laikaM_countVector(sock->outBuf) += crypto_secretbox_MACBYTES;
} }
/* add to pollList's out queue */ /* add to pollList's out queue */
laikaP_pushOutQueue(peer->pList, &peer->sock); laikaP_pushOutQueue(peer->pList, &peer->sock);
/* return packet size and prepare for next outPacket */ /* return packet size and prepare for next outPacket */
sz = sock->outCount - peer->outStart; sz = laikaM_countVector(sock->outBuf) - peer->outStart;
peer->outStart = -1; peer->outStart = -1;
return sz; return sz;
} }
@ -148,30 +148,31 @@ void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic)
if (peer->useSecure && !variadic && peer->pktSize != 0) if (peer->useSecure && !variadic && peer->pktSize != 0)
peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES; peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
peer->inStart = sock->inCount; peer->inStart = laikaM_countVector(sock->inBuf);
} }
int laikaS_endInPacket(struct sLaika_peer *peer) int laikaS_endInPacket(struct sLaika_peer *peer)
{ {
struct sLaika_socket *sock = &peer->sock; struct sLaika_socket *sock = &peer->sock;
uint8_t *body; uint8_t *body;
size_t sz = sock->inCount - peer->inStart; size_t sz = laikaM_countVector(sock->inBuf) - peer->inStart;
if (peer->useSecure && sz > crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) { if (peer->useSecure && sz > crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) {
body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES]; body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES];
/* decrypt packet body in-place */ /* decrypt packet body in-place */
if (crypto_secretbox_open_easy( if (crypto_secretbox_open_easy(body, body,
body, body, (sock->inCount - peer->inStart) - crypto_secretbox_NONCEBYTES, (laikaM_countVector(sock->inBuf) - peer->inStart) -
&sock->inBuf[peer->inStart], peer->inKey) != 0) { crypto_secretbox_NONCEBYTES,
&sock->inBuf[peer->inStart], peer->inKey) != 0) {
LAIKA_ERROR("Failed to decrypt packet!\n"); LAIKA_ERROR("Failed to decrypt packet!\n");
} }
/* decrypted message is smaller now */ /* decrypted message is smaller now */
sock->inCount -= crypto_secretbox_MACBYTES; laikaM_countVector(sock->inBuf) -= crypto_secretbox_MACBYTES;
/* remove nonce */ /* remove nonce */
laikaM_rmvarray(sock->inBuf, sock->inCount, peer->inStart, crypto_secretbox_NONCEBYTES); laikaM_rmvVector(sock->inBuf, peer->inStart, crypto_secretbox_NONCEBYTES);
sz -= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES; sz -= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
} }
@ -254,18 +255,19 @@ bool laikaS_handlePeerIn(struct sLaika_socket *sock)
default: default:
_HandlePacketBody: _HandlePacketBody:
/* 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 - laikaM_countVector(peer->sock.inBuf),
&recvd) != RAWSOCK_OK)
return false; return false;
/* have we received the full packet? */ /* have we received the full packet? */
if (peer->pktSize == peer->sock.inCount) { if (peer->pktSize == laikaM_countVector(peer->sock.inBuf)) {
peer->pktSize = laikaS_endInPacket(peer); peer->pktSize = laikaS_endInPacket(peer);
/* dispatch to packet handler */ /* dispatch to packet handler */
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData); peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
/* reset */ /* reset */
peer->sock.inCount = 0; laikaM_countVector(peer->sock.inBuf) = 0;
peer->pktID = LAIKAPKT_MAXNONE; peer->pktID = LAIKAPKT_MAXNONE;
} }
@ -280,10 +282,10 @@ bool laikaS_handlePeerOut(struct sLaika_socket *sock)
struct sLaika_peer *peer = (struct sLaika_peer *)sock; struct sLaika_peer *peer = (struct sLaika_peer *)sock;
int sent; int sent;
if (peer->sock.outCount == 0) /* sanity check */ if (laikaM_countVector(peer->sock.outBuf) == 0) /* sanity check */
return true; return true;
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) { switch (laikaS_rawSend(&peer->sock, laikaM_countVector(peer->sock.outBuf), &sent)) {
case RAWSOCK_OK: /* we're ok! */ case RAWSOCK_OK: /* we're ok! */
/* if POLLOUT was set, unset it */ /* if POLLOUT was set, unset it */
laikaP_rmvPollOut(peer->pList, &peer->sock); laikaP_rmvPollOut(peer->pList, &peer->sock);

View File

@ -50,12 +50,8 @@ void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent
sock->onPollIn = onPollIn; sock->onPollIn = onPollIn;
sock->onPollOut = onPollOut; sock->onPollOut = onPollOut;
sock->uData = uData; sock->uData = uData;
sock->inBuf = NULL; laikaM_initVector(sock->inBuf, 8);
sock->inCap = 8; laikaM_initVector(sock->outBuf, 8);
sock->inCount = 0;
sock->outBuf = NULL;
sock->outCap = 8;
sock->outCount = 0;
sock->flipEndian = false; sock->flipEndian = false;
sock->setPollOut = false; sock->setPollOut = false;
@ -197,44 +193,44 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock)
void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz) void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz)
{ {
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz); laikaM_rmvVector(sock->inBuf, 0, sz);
} }
void laikaS_zeroWrite(struct sLaika_socket *sock, size_t sz) void laikaS_zeroWrite(struct sLaika_socket *sock, size_t sz)
{ {
laikaM_growarray(uint8_t, sock->outBuf, sz, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, sz);
/* set NULL bytes */ /* set NULL bytes */
memset(&sock->outBuf[sock->outCount], 0, sz); memset(&sock->outBuf[laikaM_countVector(sock->outBuf)], 0, sz);
sock->outCount += sz; laikaM_countVector(sock->outBuf) += sz;
} }
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz) void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz)
{ {
memcpy(buf, sock->inBuf, sz); memcpy(buf, sock->inBuf, sz);
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz); laikaM_rmvVector(sock->inBuf, 0, sz);
} }
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz) void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz)
{ {
/* make sure we have enough space to copy the buffer */ /* make sure we have enough space to copy the buffer */
laikaM_growarray(uint8_t, sock->outBuf, sz, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, sz);
/* copy the buffer, then increment outCount */ /* copy the buffer, then increment outCount */
memcpy(&sock->outBuf[sock->outCount], buf, sz); memcpy(&sock->outBuf[laikaM_countVector(sock->outBuf)], buf, sz);
sock->outCount += sz; laikaM_countVector(sock->outBuf) += sz;
} }
void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub) void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub)
{ {
/* make sure we have enough space to encrypt the buffer */ /* make sure we have enough space to encrypt the buffer */
laikaM_growarray(uint8_t, sock->outBuf, LAIKAENC_SIZE(sz), sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, LAIKAENC_SIZE(sz));
/* encrypt the buffer into outBuf */ /* encrypt the buffer into outBuf */
if (crypto_box_seal(&sock->outBuf[sock->outCount], buf, sz, pub) != 0) if (crypto_box_seal(&sock->outBuf[laikaM_countVector(sock->outBuf)], buf, sz, pub) != 0)
LAIKA_ERROR("Failed to encrypt!\n"); LAIKA_ERROR("Failed to encrypt!\n");
sock->outCount += LAIKAENC_SIZE(sz); laikaM_countVector(sock->outBuf) += LAIKAENC_SIZE(sz);
} }
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub,
@ -244,21 +240,21 @@ void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uin
if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0) if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0)
LAIKA_ERROR("Failed to decrypt!\n"); LAIKA_ERROR("Failed to decrypt!\n");
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, LAIKAENC_SIZE(sz)); laikaM_rmvVector(sock->inBuf, 0, LAIKAENC_SIZE(sz));
} }
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data) void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data)
{ {
laikaM_growarray(uint8_t, sock->outBuf, 1, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, 1);
sock->outBuf[sock->outCount++] = data; sock->outBuf[laikaM_countVector(sock->outBuf)++] = data;
} }
uint8_t laikaS_readByte(struct sLaika_socket *sock) uint8_t laikaS_readByte(struct sLaika_socket *sock)
{ {
uint8_t tmp = *sock->inBuf; uint8_t tmp = *sock->inBuf;
/* pop 1 byte */ /* consume 1 byte */
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, 1); laikaM_rmvVector(sock->inBuf, 0, 1);
return tmp; return tmp;
} }
@ -302,15 +298,16 @@ void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz)
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed) RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed)
{ {
RAWSOCKCODE errCode = RAWSOCK_OK; RAWSOCKCODE errCode = RAWSOCK_OK;
int i, rcvd, start = sock->inCount; int i, rcvd, start = laikaM_countVector(sock->inBuf);
/* sanity check */ /* sanity check */
if (sz == 0) if (sz == 0)
return RAWSOCK_OK; return RAWSOCK_OK;
/* make sure we have enough space to recv */ /* make sure we have enough space to recv */
laikaM_growarray(uint8_t, sock->inBuf, sz, sock->inCount, sock->inCap); laikaM_growVector(uint8_t, sock->inBuf, sz);
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[sock->inCount], sz, LN_MSG_NOSIGNAL); rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[laikaM_countVector(sock->inBuf)], sz,
LN_MSG_NOSIGNAL);
if (rcvd == 0) { if (rcvd == 0) {
errCode = RAWSOCK_CLOSED; errCode = RAWSOCK_CLOSED;
@ -340,7 +337,7 @@ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed
#endif #endif
/* recv() worked, add rcvd to inCount */ /* recv() worked, add rcvd to inCount */
sock->inCount += rcvd; laikaM_countVector(sock->inBuf) += rcvd;
} }
*processed = rcvd; *processed = rcvd;
return errCode; return errCode;
@ -397,7 +394,7 @@ _rawWriteExit:
#endif #endif
/* trim sent data from outBuf */ /* trim sent data from outBuf */
laikaM_rmvarray(sock->outBuf, sock->outCount, 0, sentBytes); laikaM_rmvVector(sock->outBuf, 0, sentBytes);
*processed = sentBytes; *processed = sentBytes;
return errCode; return errCode;