mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-21 12:40:04 +00:00
lsocket.[ch] & lpeer.c: migrated to new vector API
This commit is contained in:
parent
af09e74263
commit
13398dbdf6
@ -3,8 +3,8 @@
|
||||
|
||||
#include "hashmap.h"
|
||||
#include "laika.h"
|
||||
#include "lsocket.h"
|
||||
#include "lmem.h"
|
||||
#include "lsocket.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
@ -21,7 +21,8 @@ struct sLaika_pollEvent
|
||||
struct sLaika_pollList
|
||||
{
|
||||
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);
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* epoll */
|
||||
|
@ -55,6 +55,7 @@ typedef void buffer_t;
|
||||
#endif
|
||||
#include "laika.h"
|
||||
#include "lsodium.h"
|
||||
#include "lmem.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
@ -79,12 +80,8 @@ struct sLaika_socket
|
||||
pollEvent onPollIn;
|
||||
pollEvent onPollOut;
|
||||
void *uData; /* passed to onPollFail */
|
||||
uint8_t *outBuf; /* raw data to be sent() */
|
||||
uint8_t *inBuf; /* raw data we recv()'d */
|
||||
int outCount;
|
||||
int inCount;
|
||||
int outCap;
|
||||
int inCap;
|
||||
laikaM_newVector(uint8_t, outBuf); /* raw data to be sent() */
|
||||
laikaM_newVector(uint8_t, inBuf); /* raw data we recv()'d */
|
||||
bool flipEndian;
|
||||
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
||||
};
|
||||
|
@ -71,7 +71,7 @@ void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID 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
|
||||
packet ID */
|
||||
uint8_t nonce[crypto_secretbox_NONCEBYTES];
|
||||
@ -88,26 +88,26 @@ int laikaS_endOutPacket(struct sLaika_peer *peer)
|
||||
|
||||
if (peer->useSecure) {
|
||||
/* make sure we have enough space */
|
||||
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount,
|
||||
sock->outCap);
|
||||
laikaM_growVector(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES);
|
||||
|
||||
/* packet body starts after the id & nonce */
|
||||
body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES];
|
||||
/* encrypt packet body in-place */
|
||||
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) {
|
||||
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 */
|
||||
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
||||
|
||||
/* return packet size and prepare for next outPacket */
|
||||
sz = sock->outCount - peer->outStart;
|
||||
sz = laikaM_countVector(sock->outBuf) - peer->outStart;
|
||||
peer->outStart = -1;
|
||||
return sz;
|
||||
}
|
||||
@ -148,30 +148,31 @@ void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic)
|
||||
if (peer->useSecure && !variadic && peer->pktSize != 0)
|
||||
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)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
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) {
|
||||
body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES];
|
||||
|
||||
/* decrypt packet body in-place */
|
||||
if (crypto_secretbox_open_easy(
|
||||
body, body, (sock->inCount - peer->inStart) - crypto_secretbox_NONCEBYTES,
|
||||
if (crypto_secretbox_open_easy(body, body,
|
||||
(laikaM_countVector(sock->inBuf) - peer->inStart) -
|
||||
crypto_secretbox_NONCEBYTES,
|
||||
&sock->inBuf[peer->inStart], peer->inKey) != 0) {
|
||||
LAIKA_ERROR("Failed to decrypt packet!\n");
|
||||
}
|
||||
|
||||
/* decrypted message is smaller now */
|
||||
sock->inCount -= crypto_secretbox_MACBYTES;
|
||||
laikaM_countVector(sock->inBuf) -= crypto_secretbox_MACBYTES;
|
||||
|
||||
/* 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;
|
||||
}
|
||||
@ -254,18 +255,19 @@ bool laikaS_handlePeerIn(struct sLaika_socket *sock)
|
||||
default:
|
||||
_HandlePacketBody:
|
||||
/* 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;
|
||||
|
||||
/* 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);
|
||||
|
||||
/* dispatch to packet handler */
|
||||
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
|
||||
|
||||
/* reset */
|
||||
peer->sock.inCount = 0;
|
||||
laikaM_countVector(peer->sock.inBuf) = 0;
|
||||
peer->pktID = LAIKAPKT_MAXNONE;
|
||||
}
|
||||
|
||||
@ -280,10 +282,10 @@ bool laikaS_handlePeerOut(struct sLaika_socket *sock)
|
||||
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
|
||||
int sent;
|
||||
|
||||
if (peer->sock.outCount == 0) /* sanity check */
|
||||
if (laikaM_countVector(peer->sock.outBuf) == 0) /* sanity check */
|
||||
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! */
|
||||
/* if POLLOUT was set, unset it */
|
||||
laikaP_rmvPollOut(peer->pList, &peer->sock);
|
||||
|
@ -50,12 +50,8 @@ void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent
|
||||
sock->onPollIn = onPollIn;
|
||||
sock->onPollOut = onPollOut;
|
||||
sock->uData = uData;
|
||||
sock->inBuf = NULL;
|
||||
sock->inCap = 8;
|
||||
sock->inCount = 0;
|
||||
sock->outBuf = NULL;
|
||||
sock->outCap = 8;
|
||||
sock->outCount = 0;
|
||||
laikaM_initVector(sock->inBuf, 8);
|
||||
laikaM_initVector(sock->outBuf, 8);
|
||||
sock->flipEndian = 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)
|
||||
{
|
||||
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz);
|
||||
laikaM_rmvVector(sock->inBuf, 0, 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 */
|
||||
memset(&sock->outBuf[sock->outCount], 0, sz);
|
||||
sock->outCount += sz;
|
||||
memset(&sock->outBuf[laikaM_countVector(sock->outBuf)], 0, sz);
|
||||
laikaM_countVector(sock->outBuf) += sz;
|
||||
}
|
||||
|
||||
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t 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)
|
||||
{
|
||||
/* 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 */
|
||||
memcpy(&sock->outBuf[sock->outCount], buf, sz);
|
||||
sock->outCount += sz;
|
||||
memcpy(&sock->outBuf[laikaM_countVector(sock->outBuf)], buf, sz);
|
||||
laikaM_countVector(sock->outBuf) += sz;
|
||||
}
|
||||
|
||||
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 */
|
||||
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 */
|
||||
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");
|
||||
|
||||
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,
|
||||
@ -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)
|
||||
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)
|
||||
{
|
||||
laikaM_growarray(uint8_t, sock->outBuf, 1, sock->outCount, sock->outCap);
|
||||
sock->outBuf[sock->outCount++] = data;
|
||||
laikaM_growVector(uint8_t, sock->outBuf, 1);
|
||||
sock->outBuf[laikaM_countVector(sock->outBuf)++] = data;
|
||||
}
|
||||
|
||||
uint8_t laikaS_readByte(struct sLaika_socket *sock)
|
||||
{
|
||||
uint8_t tmp = *sock->inBuf;
|
||||
|
||||
/* pop 1 byte */
|
||||
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, 1);
|
||||
/* consume 1 byte */
|
||||
laikaM_rmvVector(sock->inBuf, 0, 1);
|
||||
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 errCode = RAWSOCK_OK;
|
||||
int i, rcvd, start = sock->inCount;
|
||||
int i, rcvd, start = laikaM_countVector(sock->inBuf);
|
||||
|
||||
/* sanity check */
|
||||
if (sz == 0)
|
||||
return RAWSOCK_OK;
|
||||
|
||||
/* make sure we have enough space to recv */
|
||||
laikaM_growarray(uint8_t, sock->inBuf, sz, sock->inCount, sock->inCap);
|
||||
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[sock->inCount], sz, LN_MSG_NOSIGNAL);
|
||||
laikaM_growVector(uint8_t, sock->inBuf, sz);
|
||||
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[laikaM_countVector(sock->inBuf)], sz,
|
||||
LN_MSG_NOSIGNAL);
|
||||
|
||||
if (rcvd == 0) {
|
||||
errCode = RAWSOCK_CLOSED;
|
||||
@ -340,7 +337,7 @@ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed
|
||||
#endif
|
||||
|
||||
/* recv() worked, add rcvd to inCount */
|
||||
sock->inCount += rcvd;
|
||||
laikaM_countVector(sock->inBuf) += rcvd;
|
||||
}
|
||||
*processed = rcvd;
|
||||
return errCode;
|
||||
@ -397,7 +394,7 @@ _rawWriteExit:
|
||||
#endif
|
||||
|
||||
/* trim sent data from outBuf */
|
||||
laikaM_rmvarray(sock->outBuf, sock->outCount, 0, sentBytes);
|
||||
laikaM_rmvVector(sock->outBuf, 0, sentBytes);
|
||||
|
||||
*processed = sentBytes;
|
||||
return errCode;
|
||||
|
Loading…
Reference in New Issue
Block a user