mirror of
https://github.com/CPunch/Laika.git
synced 2026-01-02 19:20:18 +00:00
Added .clang-format, formatted codebase
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
|
||||
void *laikaM_realloc(void *buf, size_t sz) {
|
||||
#include "lerror.h"
|
||||
|
||||
void *laikaM_realloc(void *buf, size_t sz)
|
||||
{
|
||||
void *newBuf;
|
||||
|
||||
/* are we free'ing the buffer? */
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
#include "lpacket.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
const char* laikaD_getPacketName(LAIKAPKT_ID id) {
|
||||
const char *PKTNAMES[] = {
|
||||
"LAIKAPKT_VARPKT",
|
||||
"LAIKAPKT_HANDSHAKE_REQ",
|
||||
"LAIKAPKT_HANDSHAKE_RES",
|
||||
"LAIKAPKT_PINGPONG",
|
||||
"LAIKAPKT_SHELL_OPEN",
|
||||
"LAIKAPKT_SHELL_CLOSE",
|
||||
"LAIKAPKT_SHELL_DATA",
|
||||
"LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ",
|
||||
"LAIKAPKT_AUTHENTICATED_ADD_PEER_RES",
|
||||
"LAIKAPKT_AUTHENTICATED_RMV_PEER_RES",
|
||||
"LAIKAPKT_AUTHENTICATED_SHELL_OPEN_REQ"
|
||||
};
|
||||
const char *laikaD_getPacketName(LAIKAPKT_ID id)
|
||||
{
|
||||
const char *PKTNAMES[] = {"LAIKAPKT_VARPKT",
|
||||
"LAIKAPKT_HANDSHAKE_REQ",
|
||||
"LAIKAPKT_HANDSHAKE_RES",
|
||||
"LAIKAPKT_PINGPONG",
|
||||
"LAIKAPKT_SHELL_OPEN",
|
||||
"LAIKAPKT_SHELL_CLOSE",
|
||||
"LAIKAPKT_SHELL_DATA",
|
||||
"LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ",
|
||||
"LAIKAPKT_AUTHENTICATED_ADD_PEER_RES",
|
||||
"LAIKAPKT_AUTHENTICATED_RMV_PEER_RES",
|
||||
"LAIKAPKT_AUTHENTICATED_SHELL_OPEN_REQ"};
|
||||
|
||||
return id >= LAIKAPKT_MAXNONE ? "LAIKAPKT_UNKNOWN" : PKTNAMES[id];
|
||||
}
|
||||
|
||||
214
lib/src/lpeer.c
214
lib/src/lpeer.c
@@ -1,16 +1,16 @@
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
#include "lpeer.h"
|
||||
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct sLaika_pollList *pList, pollFailEvent onPollFail, void *onPollFailUData, void *uData) {
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl,
|
||||
struct sLaika_pollList *pList, pollFailEvent onPollFail,
|
||||
void *onPollFailUData, void *uData)
|
||||
{
|
||||
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||
|
||||
laikaS_initSocket(&peer->sock,
|
||||
laikaS_handlePeerIn,
|
||||
laikaS_handlePeerOut,
|
||||
onPollFail,
|
||||
onPollFailUData
|
||||
);
|
||||
laikaS_initSocket(&peer->sock, laikaS_handlePeerIn, laikaS_handlePeerOut, onPollFail,
|
||||
onPollFailUData);
|
||||
|
||||
peer->packetTbl = pktTbl;
|
||||
peer->pList = pList;
|
||||
@@ -31,14 +31,16 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct
|
||||
return peer;
|
||||
}
|
||||
|
||||
void laikaS_freePeer(struct sLaika_peer *peer) {
|
||||
void laikaS_freePeer(struct sLaika_peer *peer)
|
||||
{
|
||||
laikaS_cleanSocket(&peer->sock);
|
||||
laikaM_free(peer);
|
||||
}
|
||||
|
||||
/* ===================================[[ Start/End Packets ]]=================================== */
|
||||
|
||||
void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
laikaS_writeByte(sock, id);
|
||||
@@ -47,7 +49,8 @@ void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
||||
}
|
||||
|
||||
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
if (peer->outStart != -1) /* sanity check */
|
||||
@@ -56,27 +59,31 @@ void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
laikaS_writeByte(sock, id);
|
||||
|
||||
peer->outStart = sock->outCount;
|
||||
if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the packet ID */
|
||||
if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the
|
||||
packet ID */
|
||||
uint8_t nonce[crypto_secretbox_NONCEBYTES];
|
||||
randombytes_buf(nonce, crypto_secretbox_NONCEBYTES);
|
||||
laikaS_write(sock, nonce, crypto_secretbox_NONCEBYTES);
|
||||
}
|
||||
}
|
||||
|
||||
int laikaS_endOutPacket(struct sLaika_peer *peer) {
|
||||
int laikaS_endOutPacket(struct sLaika_peer *peer)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
uint8_t *body;
|
||||
size_t sz;
|
||||
|
||||
if (peer->useSecure) {
|
||||
/* make sure we have enough space */
|
||||
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount, sock->outCap);
|
||||
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount,
|
||||
sock->outCap);
|
||||
|
||||
/* 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,
|
||||
&sock->outBuf[peer->outStart], peer->outKey) != 0) {
|
||||
if (crypto_secretbox_easy(body, body,
|
||||
(sock->outCount - peer->outStart) - crypto_secretbox_NONCEBYTES,
|
||||
&sock->outBuf[peer->outStart], peer->outKey) != 0) {
|
||||
LAIKA_ERROR("Failed to encrypt packet!\n");
|
||||
}
|
||||
|
||||
@@ -92,28 +99,33 @@ int laikaS_endOutPacket(struct sLaika_peer *peer) {
|
||||
return sz;
|
||||
}
|
||||
|
||||
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
if (peer->outStart != -1) /* sanity check */
|
||||
LAIKA_ERROR("unended OUT packet!\n");
|
||||
|
||||
laikaS_writeByte(sock, LAIKAPKT_VARPKT);
|
||||
laikaS_zeroWrite(sock, sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */
|
||||
laikaS_zeroWrite(sock,
|
||||
sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */
|
||||
laikaS_startOutPacket(peer, id);
|
||||
}
|
||||
|
||||
int laikaS_endVarPacket(struct sLaika_peer *peer) {
|
||||
int laikaS_endVarPacket(struct sLaika_peer *peer)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
int patchIndx = peer->outStart - (sizeof(LAIKAPKT_SIZE) + sizeof(LAIKAPKT_ID)); /* gets index of packet size */
|
||||
int patchIndx = peer->outStart -
|
||||
(sizeof(LAIKAPKT_SIZE) + sizeof(LAIKAPKT_ID)); /* gets index of packet size */
|
||||
LAIKAPKT_SIZE sz = (LAIKAPKT_SIZE)laikaS_endOutPacket(peer);
|
||||
|
||||
/* patch packet size */
|
||||
memcpy((void*)&sock->outBuf[patchIndx], (void*)&sz, sizeof(LAIKAPKT_SIZE));
|
||||
memcpy((void *)&sock->outBuf[patchIndx], (void *)&sz, sizeof(LAIKAPKT_SIZE));
|
||||
return sz;
|
||||
}
|
||||
|
||||
void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic) {
|
||||
void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
if (peer->inStart != -1) /* sanity check */
|
||||
@@ -126,7 +138,8 @@ void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic) {
|
||||
peer->inStart = sock->inCount;
|
||||
}
|
||||
|
||||
int laikaS_endInPacket(struct sLaika_peer *peer) {
|
||||
int laikaS_endInPacket(struct sLaika_peer *peer)
|
||||
{
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
uint8_t *body;
|
||||
size_t sz = sock->inCount - peer->inStart;
|
||||
@@ -135,7 +148,8 @@ int laikaS_endInPacket(struct sLaika_peer *peer) {
|
||||
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, (sock->inCount - peer->inStart) - crypto_secretbox_NONCEBYTES,
|
||||
&sock->inBuf[peer->inStart], peer->inKey) != 0) {
|
||||
LAIKA_ERROR("Failed to decrypt packet!\n");
|
||||
}
|
||||
@@ -153,113 +167,119 @@ int laikaS_endInPacket(struct sLaika_peer *peer) {
|
||||
return sz;
|
||||
}
|
||||
|
||||
void laikaS_setSecure(struct sLaika_peer *peer, bool flag) {
|
||||
void laikaS_setSecure(struct sLaika_peer *peer, bool flag)
|
||||
{
|
||||
peer->useSecure = flag;
|
||||
}
|
||||
|
||||
/* ==================================[[ Handle Poll Events ]]=================================== */
|
||||
|
||||
bool laikaS_handlePeerIn(struct sLaika_socket *sock) {
|
||||
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
|
||||
bool laikaS_handlePeerIn(struct sLaika_socket *sock)
|
||||
{
|
||||
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
|
||||
int recvd;
|
||||
|
||||
switch (peer->pktID) {
|
||||
case LAIKAPKT_MAXNONE:
|
||||
/* try grabbing pktID */
|
||||
if (laikaS_rawRecv(&peer->sock, sizeof(uint8_t), &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
case LAIKAPKT_MAXNONE:
|
||||
/* try grabbing pktID */
|
||||
if (laikaS_rawRecv(&peer->sock, sizeof(uint8_t), &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
|
||||
/* read packet ID */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
LAIKA_DEBUG("%s\n", laikaD_getPacketName(peer->pktID));
|
||||
/* read packet ID */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
LAIKA_DEBUG("%s\n", laikaD_getPacketName(peer->pktID));
|
||||
|
||||
/* LAIKAPKT_VARPKT's body is unencrypted, and handled by this switch statement. LAIKAPKT_VARPKT is
|
||||
also likely not to be defined in our pktSizeTable. the LAIKAPKT_VARPKT case calls laikaS_startInPacket
|
||||
for itself, so skip all of this */
|
||||
if (peer->pktID == LAIKAPKT_VARPKT)
|
||||
goto _HandlePacketVariadic;
|
||||
/* LAIKAPKT_VARPKT's body is unencrypted, and handled by this switch statement.
|
||||
LAIKAPKT_VARPKT is also likely not to be defined in our pktSizeTable. the LAIKAPKT_VARPKT
|
||||
case calls laikaS_startInPacket for itself, so skip all of this */
|
||||
if (peer->pktID == LAIKAPKT_VARPKT)
|
||||
goto _HandlePacketVariadic;
|
||||
|
||||
/* sanity check pktID, pktID's handler & make sure it's not marked as variadic */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->packetTbl[peer->pktID].handler == NULL || peer->packetTbl[peer->pktID].variadic)
|
||||
LAIKA_ERROR("peer %p doesn't support packet id [%d]!\n", peer, peer->pktID);
|
||||
/* sanity check pktID, pktID's handler & make sure it's not marked as variadic */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->packetTbl[peer->pktID].handler == NULL ||
|
||||
peer->packetTbl[peer->pktID].variadic)
|
||||
LAIKA_ERROR("peer %p doesn't support packet id [%d]!\n", peer, peer->pktID);
|
||||
|
||||
peer->pktSize = peer->packetTbl[peer->pktID].size;
|
||||
peer->pktSize = peer->packetTbl[peer->pktID].size;
|
||||
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer, false);
|
||||
goto _HandlePacketBody;
|
||||
case LAIKAPKT_VARPKT:
|
||||
_HandlePacketVariadic:
|
||||
/* try grabbing pktID & size */
|
||||
if (laikaS_rawRecv(&peer->sock, sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE), &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer, false);
|
||||
goto _HandlePacketBody;
|
||||
case LAIKAPKT_VARPKT:
|
||||
_HandlePacketVariadic:
|
||||
/* try grabbing pktID & size */
|
||||
if (laikaS_rawRecv(&peer->sock, sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE), &recvd) !=
|
||||
RAWSOCK_OK)
|
||||
return false;
|
||||
|
||||
/* not worth queuing & setting pollIn for 3 bytes. if the connection is that slow, it was probably sent maliciously anyways */
|
||||
if (recvd != sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE))
|
||||
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT\n");
|
||||
/* not worth queuing & setting pollIn for 3 bytes. if the connection is that slow, it was
|
||||
* probably sent maliciously anyways */
|
||||
if (recvd != sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE))
|
||||
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT\n");
|
||||
|
||||
/* read packet size */
|
||||
laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
|
||||
/* read packet size */
|
||||
laikaS_readInt(&peer->sock, (void *)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
|
||||
|
||||
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
|
||||
LAIKA_ERROR("variable packet too large!\n");
|
||||
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
|
||||
LAIKA_ERROR("variable packet too large!\n");
|
||||
|
||||
/* read pktID */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
/* read pktID */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* sanity check pktID, check valid handler & make sure it's marked as variadic */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->packetTbl[peer->pktID].handler == NULL || !peer->packetTbl[peer->pktID].variadic)
|
||||
LAIKA_ERROR("requested packet id [%d] is not variadic!\n", peer->pktID);
|
||||
/* sanity check pktID, check valid handler & make sure it's marked as variadic */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->packetTbl[peer->pktID].handler == NULL ||
|
||||
!peer->packetTbl[peer->pktID].variadic)
|
||||
LAIKA_ERROR("requested packet id [%d] is not variadic!\n", peer->pktID);
|
||||
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer, true);
|
||||
goto _HandlePacketBody;
|
||||
default:
|
||||
_HandlePacketBody:
|
||||
/* try grabbing the rest of the packet */
|
||||
if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer, true);
|
||||
goto _HandlePacketBody;
|
||||
default:
|
||||
_HandlePacketBody:
|
||||
/* try grabbing the rest of the packet */
|
||||
if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
|
||||
/* have we received the full packet? */
|
||||
if (peer->pktSize == peer->sock.inCount) {
|
||||
peer->pktSize = laikaS_endInPacket(peer);
|
||||
/* have we received the full packet? */
|
||||
if (peer->pktSize == peer->sock.inCount) {
|
||||
peer->pktSize = laikaS_endInPacket(peer);
|
||||
|
||||
/* dispatch to packet handler */
|
||||
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
|
||||
/* dispatch to packet handler */
|
||||
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
|
||||
|
||||
/* reset */
|
||||
peer->sock.inCount = 0;
|
||||
peer->pktID = LAIKAPKT_MAXNONE;
|
||||
}
|
||||
/* reset */
|
||||
peer->sock.inCount = 0;
|
||||
peer->pktID = LAIKAPKT_MAXNONE;
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
return laikaS_isAlive((&peer->sock));
|
||||
}
|
||||
|
||||
bool laikaS_handlePeerOut(struct sLaika_socket *sock) {
|
||||
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
|
||||
bool laikaS_handlePeerOut(struct sLaika_socket *sock)
|
||||
{
|
||||
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
|
||||
int sent;
|
||||
|
||||
if (peer->sock.outCount == 0) /* sanity check */
|
||||
return true;
|
||||
|
||||
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) {
|
||||
case RAWSOCK_OK: /* we're ok! */
|
||||
/* if POLLOUT was set, unset it */
|
||||
laikaP_rmvPollOut(peer->pList, &peer->sock);
|
||||
case RAWSOCK_OK: /* we're ok! */
|
||||
/* if POLLOUT was set, unset it */
|
||||
laikaP_rmvPollOut(peer->pList, &peer->sock);
|
||||
|
||||
return true;
|
||||
case RAWSOCK_POLL: /* we've been asked to set the POLLOUT flag */
|
||||
/* if POLLOUT wasn't set, set it so we'll be notified whenever the kernel has room :) */
|
||||
laikaP_addPollOut(peer->pList, &peer->sock);
|
||||
return true;
|
||||
case RAWSOCK_POLL: /* we've been asked to set the POLLOUT flag */
|
||||
/* if POLLOUT wasn't set, set it so we'll be notified whenever the kernel has room :) */
|
||||
laikaP_addPollOut(peer->pList, &peer->sock);
|
||||
|
||||
return true;
|
||||
default: /* panic! */
|
||||
case RAWSOCK_CLOSED:
|
||||
case RAWSOCK_ERROR:
|
||||
return false;
|
||||
return true;
|
||||
default: /* panic! */
|
||||
case RAWSOCK_CLOSED:
|
||||
case RAWSOCK_ERROR:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +1,44 @@
|
||||
#include "lpolllist.h"
|
||||
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
#include "lpolllist.h"
|
||||
|
||||
/* ===================================[[ Helper Functions ]]==================================== */
|
||||
|
||||
typedef struct sLaika_hashMapElem {
|
||||
typedef struct sLaika_hashMapElem
|
||||
{
|
||||
SOCKET fd;
|
||||
struct sLaika_socket *sock;
|
||||
} tLaika_hashMapElem;
|
||||
|
||||
int elem_compare(const void *a, const void *b, void *udata) {
|
||||
int elem_compare(const void *a, const void *b, void *udata)
|
||||
{
|
||||
const tLaika_hashMapElem *ua = a;
|
||||
const tLaika_hashMapElem *ub = b;
|
||||
|
||||
return ua->fd != ub->fd;
|
||||
return ua->fd != ub->fd;
|
||||
}
|
||||
|
||||
uint64_t elem_hash(const void *item, uint64_t seed0, uint64_t seed1) {
|
||||
uint64_t elem_hash(const void *item, uint64_t seed0, uint64_t seed1)
|
||||
{
|
||||
const tLaika_hashMapElem *u = item;
|
||||
return (uint64_t)(u->fd);
|
||||
}
|
||||
|
||||
/* =====================================[[ PollList API ]]====================================== */
|
||||
|
||||
void laikaP_initPList(struct sLaika_pollList *pList) {
|
||||
void laikaP_initPList(struct sLaika_pollList *pList)
|
||||
{
|
||||
laikaS_init();
|
||||
|
||||
/* setup hashmap */
|
||||
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash, elem_compare, NULL, NULL);
|
||||
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash,
|
||||
elem_compare, NULL, NULL);
|
||||
pList->revents = NULL; /* laikaP_pollList() will allocate the buffer */
|
||||
pList->reventCap = POLLSTARTCAP/GROW_FACTOR;
|
||||
pList->reventCap = POLLSTARTCAP / GROW_FACTOR;
|
||||
pList->reventCount = 0;
|
||||
pList->outQueue = NULL;
|
||||
pList->outCap = POLLSTARTCAP/GROW_FACTOR;
|
||||
pList->outCap = POLLSTARTCAP / GROW_FACTOR;
|
||||
pList->outCount = 0;
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
@@ -43,12 +49,15 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
|
||||
|
||||
#else
|
||||
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */
|
||||
pList->fdCapacity = POLLSTARTCAP/GROW_FACTOR; /* div by GROW_FACTOR since laikaM_growarray multiplies by GROW_FACTOR */
|
||||
pList->fdCapacity =
|
||||
POLLSTARTCAP /
|
||||
GROW_FACTOR; /* div by GROW_FACTOR since laikaM_growarray multiplies by GROW_FACTOR */
|
||||
pList->fdCount = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaP_cleanPList(struct sLaika_pollList *pList) {
|
||||
void laikaP_cleanPList(struct sLaika_pollList *pList)
|
||||
{
|
||||
laikaM_free(pList->revents);
|
||||
laikaM_free(pList->outQueue);
|
||||
hashmap_free(pList->sockets);
|
||||
@@ -62,13 +71,14 @@ void laikaP_cleanPList(struct sLaika_pollList *pList) {
|
||||
laikaS_cleanUp();
|
||||
}
|
||||
|
||||
void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
||||
{
|
||||
/* add socket to hashmap */
|
||||
hashmap_set(pList->sockets, &(tLaika_hashMapElem){.fd = sock->sock, .sock = sock});
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
pList->ev.events = EPOLLIN;
|
||||
pList->ev.data.ptr = (void*)sock;
|
||||
pList->ev.data.ptr = (void *)sock;
|
||||
|
||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1)
|
||||
LAIKA_ERROR("epoll_ctl [ADD] failed\n");
|
||||
@@ -80,7 +90,8 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* remove socket from hashmap */
|
||||
@@ -88,13 +99,14 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
|
||||
/* make sure peer isn't in outQueue */
|
||||
for (i = 0; i < pList->outCount; i++) {
|
||||
if ((void*)pList->outQueue[i] == (void*)sock) {
|
||||
if ((void *)pList->outQueue[i] == (void *)sock) {
|
||||
laikaM_rmvarray(pList->outQueue, pList->outCount, i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */
|
||||
/* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL
|
||||
* pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */
|
||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_DEL, sock->sock, &pList->ev) == -1) {
|
||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||
LAIKA_WARN("epoll_ctl [DEL] failed\n");
|
||||
@@ -112,13 +124,14 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
||||
{
|
||||
if (sock->setPollOut)
|
||||
return;
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
pList->ev.events = EPOLLIN | EPOLLOUT;
|
||||
pList->ev.data.ptr = (void*)sock;
|
||||
pList->ev.data.ptr = (void *)sock;
|
||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||
LAIKA_WARN("epoll_ctl [MOD] failed\n");
|
||||
@@ -138,13 +151,14 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
||||
sock->setPollOut = true;
|
||||
}
|
||||
|
||||
void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
||||
{
|
||||
if (!sock->setPollOut)
|
||||
return;
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
pList->ev.events = EPOLLIN;
|
||||
pList->ev.data.ptr = (void*)sock;
|
||||
pList->ev.data.ptr = (void *)sock;
|
||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||
LAIKA_WARN("epoll_ctl [MOD] failed\n");
|
||||
@@ -164,7 +178,8 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
||||
sock->setPollOut = false;
|
||||
}
|
||||
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *sock)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* first, check that we don't have this peer in the queue already */
|
||||
@@ -173,15 +188,17 @@ void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *so
|
||||
return; /* found it :) */
|
||||
}
|
||||
|
||||
laikaM_growarray(struct sLaika_socket*, pList->outQueue, 1, pList->outCount, pList->outCap);
|
||||
laikaM_growarray(struct sLaika_socket *, pList->outQueue, 1, pList->outCount, pList->outCap);
|
||||
pList->outQueue[pList->outCount++] = sock;
|
||||
}
|
||||
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList) {
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList)
|
||||
{
|
||||
pList->outCount = 0; /* ez lol */
|
||||
}
|
||||
|
||||
void laikaP_flushOutQueue(struct sLaika_pollList *pList) {
|
||||
void laikaP_flushOutQueue(struct sLaika_pollList *pList)
|
||||
{
|
||||
struct sLaika_socket *sock;
|
||||
int i;
|
||||
|
||||
@@ -195,14 +212,16 @@ void laikaP_flushOutQueue(struct sLaika_pollList *pList) {
|
||||
laikaP_resetOutQueue(pList);
|
||||
}
|
||||
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *_nevents) {
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *_nevents)
|
||||
{
|
||||
int nEvents, i;
|
||||
|
||||
pList->reventCount = 0; /* reset revent array */
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a lookup into our socket hashmap
|
||||
not to mention the various improvements epoll() has over poll() :D
|
||||
*/
|
||||
/* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a
|
||||
lookup into our socket hashmap not to mention the various improvements epoll() has over
|
||||
poll() :D
|
||||
*/
|
||||
nEvents = epoll_wait(pList->epollfd, pList->ep_events, MAX_EPOLL_EVENTS, timeout);
|
||||
|
||||
if (SOCKETERROR(nEvents))
|
||||
@@ -210,15 +229,16 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
|
||||
for (i = 0; i < nEvents; i++) {
|
||||
/* add event to revent array */
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCap);
|
||||
pList->revents[pList->reventCount++] = (struct sLaika_pollEvent){
|
||||
.sock = pList->ep_events[i].data.ptr,
|
||||
.pollIn = pList->ep_events[i].events & EPOLLIN,
|
||||
.pollOut = pList->ep_events[i].events & EPOLLOUT
|
||||
};
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount,
|
||||
pList->reventCap);
|
||||
pList->revents[pList->reventCount++] =
|
||||
(struct sLaika_pollEvent){.sock = pList->ep_events[i].data.ptr,
|
||||
.pollIn = pList->ep_events[i].events & EPOLLIN,
|
||||
.pollOut = pList->ep_events[i].events & EPOLLOUT};
|
||||
}
|
||||
#else
|
||||
nEvents = poll(pList->fds, pList->fdCount, timeout); /* poll returns -1 for error, or the number of events */
|
||||
nEvents = poll(pList->fds, pList->fdCount,
|
||||
timeout); /* poll returns -1 for error, or the number of events */
|
||||
|
||||
if (SOCKETERROR(nEvents))
|
||||
LAIKA_ERROR("poll() failed!\n");
|
||||
@@ -228,15 +248,16 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
PollFD pfd = pList->fds[i];
|
||||
if (pList->fds[i].revents != 0) {
|
||||
/* grab socket from hashmap */
|
||||
tLaika_hashMapElem *elem = (tLaika_hashMapElem*)hashmap_get(pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
|
||||
tLaika_hashMapElem *elem = (tLaika_hashMapElem *)hashmap_get(
|
||||
pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
|
||||
|
||||
/* insert event into revents array */
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCap);
|
||||
pList->revents[pList->reventCount++] = (struct sLaika_pollEvent){
|
||||
.sock = elem->sock,
|
||||
.pollIn = pfd.revents & POLLIN,
|
||||
.pollOut = pfd.revents & POLLOUT
|
||||
};
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount,
|
||||
pList->reventCap);
|
||||
pList->revents[pList->reventCount++] =
|
||||
(struct sLaika_pollEvent){.sock = elem->sock,
|
||||
.pollIn = pfd.revents & POLLIN,
|
||||
.pollOut = pfd.revents & POLLOUT};
|
||||
|
||||
nEvents--;
|
||||
}
|
||||
@@ -249,7 +270,8 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
return pList->revents;
|
||||
}
|
||||
|
||||
bool laikaP_handleEvent(struct sLaika_pollEvent *evnt) {
|
||||
bool laikaP_handleEvent(struct sLaika_pollEvent *evnt)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
/* sanity check */
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
#include "lsocket.h"
|
||||
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
#include "lpacket.h"
|
||||
#include "lpolllist.h"
|
||||
#include "lsodium.h"
|
||||
#include "lsocket.h"
|
||||
#include "lpacket.h"
|
||||
|
||||
static int _LNSetup = 0;
|
||||
|
||||
bool laikaS_isBigEndian(void) {
|
||||
union {
|
||||
bool laikaS_isBigEndian(void)
|
||||
{
|
||||
union
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t c[4];
|
||||
} _indxint = {0xDEADB33F};
|
||||
@@ -16,7 +19,8 @@ bool laikaS_isBigEndian(void) {
|
||||
return _indxint.c[0] == 0xDE;
|
||||
}
|
||||
|
||||
void laikaS_init(void) {
|
||||
void laikaS_init(void)
|
||||
{
|
||||
if (_LNSetup++ > 0)
|
||||
return; /* WSA is already setup! */
|
||||
|
||||
@@ -28,7 +32,8 @@ void laikaS_init(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaS_cleanUp(void) {
|
||||
void laikaS_cleanUp(void)
|
||||
{
|
||||
if (--_LNSetup > 0)
|
||||
return; /* WSA still needs to be up, a socket is still running */
|
||||
|
||||
@@ -37,7 +42,9 @@ void laikaS_cleanUp(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut, pollFailEvent onPollFail, void *uData) {
|
||||
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut,
|
||||
pollFailEvent onPollFail, void *uData)
|
||||
{
|
||||
sock->sock = INVALID_SOCKET;
|
||||
sock->onPollFail = onPollFail;
|
||||
sock->onPollIn = onPollIn;
|
||||
@@ -55,7 +62,8 @@ void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent
|
||||
laikaS_init();
|
||||
}
|
||||
|
||||
void laikaS_cleanSocket(struct sLaika_socket *sock) {
|
||||
void laikaS_cleanSocket(struct sLaika_socket *sock)
|
||||
{
|
||||
/* free in & out arrays */
|
||||
laikaM_free(sock->inBuf);
|
||||
laikaM_free(sock->outBuf);
|
||||
@@ -65,7 +73,8 @@ void laikaS_cleanSocket(struct sLaika_socket *sock) {
|
||||
laikaS_cleanUp();
|
||||
}
|
||||
|
||||
void laikaS_kill(struct sLaika_socket *sock) {
|
||||
void laikaS_kill(struct sLaika_socket *sock)
|
||||
{
|
||||
if (!laikaS_isAlive(sock)) /* sanity check */
|
||||
return;
|
||||
|
||||
@@ -80,7 +89,8 @@ void laikaS_kill(struct sLaika_socket *sock) {
|
||||
sock->sock = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
||||
void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port)
|
||||
{
|
||||
struct addrinfo res, *result, *curr;
|
||||
|
||||
if (!SOCKETINVALID(sock->sock))
|
||||
@@ -95,14 +105,15 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
||||
if (getaddrinfo(ip, port, &res, &result) != 0)
|
||||
LAIKA_ERROR("getaddrinfo() failed!\n");
|
||||
|
||||
/* getaddrinfo returns a list of possible addresses, step through them and try them until we find a valid address */
|
||||
/* getaddrinfo returns a list of possible addresses, step through them and try them until we
|
||||
* find a valid address */
|
||||
for (curr = result; curr != NULL; curr = curr->ai_next) {
|
||||
sock->sock = socket(curr->ai_family, curr->ai_socktype, curr->ai_protocol);
|
||||
|
||||
/* if it failed, try the next sock */
|
||||
if (SOCKETINVALID(sock->sock))
|
||||
continue;
|
||||
|
||||
|
||||
/* if it's not an invalid socket, break and exit the loop, we found a working addr! */
|
||||
if (!SOCKETINVALID(connect(sock->sock, curr->ai_addr, curr->ai_addrlen)))
|
||||
break;
|
||||
@@ -116,7 +127,8 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
||||
LAIKA_ERROR("couldn't connect a valid address handle to socket!\n");
|
||||
}
|
||||
|
||||
void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
||||
void laikaS_bind(struct sLaika_socket *sock, uint16_t port)
|
||||
{
|
||||
socklen_t addressSize;
|
||||
struct sockaddr_in6 address;
|
||||
int opt = 1;
|
||||
@@ -129,9 +141,9 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
||||
if (SOCKETINVALID(sock->sock))
|
||||
LAIKA_ERROR("socket() failed!\n");
|
||||
|
||||
/* allow reuse of local address */
|
||||
/* allow reuse of local address */
|
||||
#ifdef _WIN32
|
||||
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(int)) != 0)
|
||||
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(int)) != 0)
|
||||
#else
|
||||
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0)
|
||||
#endif
|
||||
@@ -144,18 +156,19 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
||||
addressSize = sizeof(address);
|
||||
|
||||
/* bind to the port */
|
||||
if (SOCKETERROR(bind(sock->sock, (struct sockaddr*)&address, addressSize)))
|
||||
if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize)))
|
||||
LAIKA_ERROR("bind() failed!\n");
|
||||
|
||||
if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
|
||||
LAIKA_ERROR("listen() failed!\n");
|
||||
}
|
||||
|
||||
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ip) {
|
||||
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ip)
|
||||
{
|
||||
struct sockaddr_in6 address;
|
||||
socklen_t addressSize = sizeof(address);
|
||||
|
||||
sock->sock = accept(from->sock, (struct sockaddr*)&address, &addressSize);
|
||||
sock->sock = accept(from->sock, (struct sockaddr *)&address, &addressSize);
|
||||
if (SOCKETINVALID(sock->sock))
|
||||
LAIKA_ERROR("accept() failed!\n");
|
||||
|
||||
@@ -166,7 +179,8 @@ void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, c
|
||||
}
|
||||
}
|
||||
|
||||
bool laikaS_setNonBlock(struct sLaika_socket *sock) {
|
||||
bool laikaS_setNonBlock(struct sLaika_socket *sock)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
unsigned long mode = 1;
|
||||
if (ioctlsocket(sock->sock, FIONBIO, &mode) != 0) {
|
||||
@@ -181,11 +195,13 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/* set NULL bytes */
|
||||
@@ -193,12 +209,14 @@ void laikaS_zeroWrite(struct sLaika_socket *sock, size_t sz) {
|
||||
sock->outCount += 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);
|
||||
laikaM_rmvarray(sock->inBuf, sock->inCount, 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 */
|
||||
laikaM_growarray(uint8_t, sock->outBuf, sz, sock->outCount, sock->outCap);
|
||||
|
||||
@@ -207,7 +225,8 @@ void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz) {
|
||||
sock->outCount += 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 */
|
||||
laikaM_growarray(uint8_t, sock->outBuf, LAIKAENC_SIZE(sz), sock->outCount, sock->outCap);
|
||||
|
||||
@@ -218,7 +237,9 @@ void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, ui
|
||||
sock->outCount += LAIKAENC_SIZE(sz);
|
||||
}
|
||||
|
||||
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, uint8_t *priv) {
|
||||
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub,
|
||||
uint8_t *priv)
|
||||
{
|
||||
/* decrypt into buf */
|
||||
if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0)
|
||||
LAIKA_ERROR("Failed to decrypt!\n");
|
||||
@@ -226,12 +247,14 @@ void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uin
|
||||
laikaM_rmvarray(sock->inBuf, sock->inCount, 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);
|
||||
sock->outBuf[sock->outCount++] = data;
|
||||
}
|
||||
|
||||
uint8_t laikaS_readByte(struct sLaika_socket *sock) {
|
||||
uint8_t laikaS_readByte(struct sLaika_socket *sock)
|
||||
{
|
||||
uint8_t tmp = *sock->inBuf;
|
||||
|
||||
/* pop 1 byte */
|
||||
@@ -239,17 +262,18 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock) {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz) {
|
||||
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz)
|
||||
{
|
||||
if (sock->flipEndian) {
|
||||
VLA(uint8_t, tmp, sz); /* allocate tmp buffer to hold data while we switch endianness */
|
||||
int k;
|
||||
|
||||
laikaS_read(sock, (void*)tmp, sz);
|
||||
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];
|
||||
|
||||
*((uint8_t *)buf + k) = tmp[sz - k - 1];
|
||||
|
||||
ENDVLA(tmp);
|
||||
} else {
|
||||
/* just a wrapper for laikaS_read */
|
||||
@@ -257,16 +281,17 @@ void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz) {
|
||||
}
|
||||
}
|
||||
|
||||
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz) {
|
||||
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz)
|
||||
{
|
||||
if (sock->flipEndian) {
|
||||
VLA(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));
|
||||
tmp[k] = *((uint8_t *)buf + (sz - k - 1));
|
||||
|
||||
laikaS_write(sock, (void*)tmp, sz);
|
||||
laikaS_write(sock, (void *)tmp, sz);
|
||||
ENDVLA(tmp);
|
||||
} else {
|
||||
/* just a wrapper for laikaS_write */
|
||||
@@ -274,7 +299,8 @@ 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;
|
||||
int i, rcvd, start = sock->inCount;
|
||||
|
||||
@@ -284,14 +310,16 @@ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed
|
||||
|
||||
/* 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);
|
||||
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[sock->inCount], sz, LN_MSG_NOSIGNAL);
|
||||
|
||||
if (rcvd == 0) {
|
||||
errCode = RAWSOCK_CLOSED;
|
||||
} else if (SOCKETERROR(rcvd) && LN_ERRNO != LN_EWOULD
|
||||
} else if (SOCKETERROR(rcvd) &&
|
||||
LN_ERRNO != LN_EWOULD
|
||||
#ifndef _WIN32
|
||||
/* if it's a posix system, also make sure its not a EAGAIN result (which is a recoverable error, there's just nothing to read lol) */
|
||||
&& LN_ERRNO != EAGAIN
|
||||
/* if it's a posix system, also make sure its not a EAGAIN result (which is a
|
||||
recoverable error, there's just nothing to read lol) */
|
||||
&& LN_ERRNO != EAGAIN
|
||||
#endif
|
||||
) {
|
||||
/* if the socket closed or an error occurred, return the error result */
|
||||
@@ -318,13 +346,15 @@ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed
|
||||
return errCode;
|
||||
}
|
||||
|
||||
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed) {
|
||||
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed)
|
||||
{
|
||||
RAWSOCKCODE errCode = RAWSOCK_OK;
|
||||
int sent, i, sentBytes = 0;
|
||||
|
||||
/* write bytes to the socket until an error occurs or we finish sending */
|
||||
do {
|
||||
sent = send(sock->sock, (buffer_t*)(&sock->outBuf[sentBytes]), sz - sentBytes, LN_MSG_NOSIGNAL);
|
||||
sent = send(sock->sock, (buffer_t *)(&sock->outBuf[sentBytes]), sz - sentBytes,
|
||||
LN_MSG_NOSIGNAL);
|
||||
|
||||
/* check for error result */
|
||||
if (sent == 0) { /* connection closed gracefully */
|
||||
@@ -333,7 +363,8 @@ RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed
|
||||
} else if (SOCKETERROR(sent)) { /* socket error? */
|
||||
if (LN_ERRNO != LN_EWOULD
|
||||
#ifndef _WIN32
|
||||
/* posix also has some platforms which define EAGAIN as a different value than EWOULD, might as well support it. */
|
||||
/* posix also has some platforms which define EAGAIN as a different value than
|
||||
EWOULD, might as well support it. */
|
||||
&& LN_ERRNO != EAGAIN
|
||||
#endif
|
||||
) { /* socket error! */
|
||||
@@ -348,7 +379,7 @@ RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed
|
||||
errCode = RAWSOCK_POLL;
|
||||
goto _rawWriteExit;
|
||||
}
|
||||
} while((sentBytes += sent) < sz);
|
||||
} while ((sentBytes += sent) < sz);
|
||||
|
||||
_rawWriteExit:
|
||||
#if 0
|
||||
|
||||
@@ -2,23 +2,28 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
bool laikaK_loadKeys(uint8_t *outPub, uint8_t *outPriv, const char *inPub, const char *inPriv) {
|
||||
bool laikaK_loadKeys(uint8_t *outPub, uint8_t *outPriv, const char *inPub, const char *inPriv)
|
||||
{
|
||||
size_t _unused;
|
||||
|
||||
if (outPub && sodium_hex2bin(outPub, crypto_kx_PUBLICKEYBYTES, inPub, strlen(inPub), NULL, &_unused, NULL) != 0)
|
||||
return false;
|
||||
|
||||
if (outPriv && sodium_hex2bin(outPriv, crypto_kx_SECRETKEYBYTES, inPriv, strlen(inPriv), NULL, &_unused, NULL) != 0)
|
||||
if (outPub && sodium_hex2bin(outPub, crypto_kx_PUBLICKEYBYTES, inPub, strlen(inPub), NULL,
|
||||
&_unused, NULL) != 0)
|
||||
return false;
|
||||
|
||||
if (outPriv && sodium_hex2bin(outPriv, crypto_kx_SECRETKEYBYTES, inPriv, strlen(inPriv), NULL,
|
||||
&_unused, NULL) != 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool laikaK_genKeys(uint8_t *outPub, uint8_t *outPriv) {
|
||||
bool laikaK_genKeys(uint8_t *outPub, uint8_t *outPriv)
|
||||
{
|
||||
return crypto_kx_keypair(outPub, outPriv) == 0;
|
||||
}
|
||||
|
||||
bool laikaK_checkAuth(uint8_t *pubKey, uint8_t **authKeys, int keys) {
|
||||
bool laikaK_checkAuth(uint8_t *pubKey, uint8_t **authKeys, int keys)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* check if key is in authKey list */
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
#include "lmem.h"
|
||||
#include "ltask.h"
|
||||
|
||||
/* this is the only reason C11 support is needed, i cba to write windows/linux specific stuff to get the current time in ms
|
||||
also side note: microsoft? more like micropenis */
|
||||
long laikaT_getTime() {
|
||||
#include "lmem.h"
|
||||
|
||||
/* this is the only reason C11 support is needed, i cba to write windows/linux specific stuff to get
|
||||
the current time in ms also side note: microsoft? more like micropenis */
|
||||
long laikaT_getTime()
|
||||
{
|
||||
struct timespec ts;
|
||||
timespec_get(&ts, TIME_UTC);
|
||||
return ts.tv_sec*1000 + ts.tv_nsec/1000000; /* convert time (seconds & nanoseconds) to milliseconds */
|
||||
return ts.tv_sec * 1000 +
|
||||
ts.tv_nsec / 1000000; /* convert time (seconds & nanoseconds) to milliseconds */
|
||||
}
|
||||
|
||||
void laikaT_initTaskService(struct sLaika_taskService *service) {
|
||||
void laikaT_initTaskService(struct sLaika_taskService *service)
|
||||
{
|
||||
service->headTask = NULL;
|
||||
}
|
||||
|
||||
void laikaT_cleanTaskService(struct sLaika_taskService *service) {
|
||||
void laikaT_cleanTaskService(struct sLaika_taskService *service)
|
||||
{
|
||||
struct sLaika_task *curr = service->headTask, *last;
|
||||
|
||||
/* walk though tasks, freeing all */
|
||||
@@ -24,7 +29,8 @@ void laikaT_cleanTaskService(struct sLaika_taskService *service) {
|
||||
}
|
||||
}
|
||||
|
||||
void scheduleTask(struct sLaika_taskService *service, struct sLaika_task *task) {
|
||||
void scheduleTask(struct sLaika_taskService *service, struct sLaika_task *task)
|
||||
{
|
||||
struct sLaika_task *curr = service->headTask, *last = NULL;
|
||||
|
||||
task->scheduled = laikaT_getTime() + task->delta;
|
||||
@@ -47,7 +53,8 @@ void scheduleTask(struct sLaika_taskService *service, struct sLaika_task *task)
|
||||
}
|
||||
}
|
||||
|
||||
void unscheduleTask(struct sLaika_taskService *service, struct sLaika_task *task) {
|
||||
void unscheduleTask(struct sLaika_taskService *service, struct sLaika_task *task)
|
||||
{
|
||||
struct sLaika_task *curr = service->headTask, *last = NULL;
|
||||
|
||||
if (task == service->headTask) { /* if task is root, set root to next */
|
||||
@@ -65,7 +72,9 @@ void unscheduleTask(struct sLaika_taskService *service, struct sLaika_task *task
|
||||
}
|
||||
}
|
||||
|
||||
struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta, taskCallback callback, void *uData) {
|
||||
struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta,
|
||||
taskCallback callback, void *uData)
|
||||
{
|
||||
struct sLaika_task *task = laikaM_malloc(sizeof(struct sLaika_task));
|
||||
|
||||
task->callback = callback;
|
||||
@@ -77,17 +86,21 @@ struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta
|
||||
return task;
|
||||
}
|
||||
|
||||
void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task) {
|
||||
void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task)
|
||||
{
|
||||
unscheduleTask(service, task);
|
||||
laikaM_free(task);
|
||||
}
|
||||
|
||||
void laikaT_pollTasks(struct sLaika_taskService *service) {
|
||||
void laikaT_pollTasks(struct sLaika_taskService *service)
|
||||
{
|
||||
struct sLaika_task *last, *curr = service->headTask;
|
||||
clock_t currTick = laikaT_getTime();
|
||||
|
||||
/* run each task, list is already sorted from closest scheduled task to furthest */
|
||||
while (curr != NULL && curr->scheduled <= currTick) { /* if scheduled time is greater than currTime, all events that follow are also not scheduled yet */
|
||||
while (curr != NULL &&
|
||||
curr->scheduled <= currTick) { /* if scheduled time is greater than currTime, all events
|
||||
that follow are also not scheduled yet */
|
||||
/* walk to next task */
|
||||
last = curr;
|
||||
curr = curr->next;
|
||||
@@ -102,7 +115,8 @@ void laikaT_pollTasks(struct sLaika_taskService *service) {
|
||||
}
|
||||
|
||||
/* will return the delta time in ms till the next event. -1 for no tasks scheduled */
|
||||
int laikaT_timeTillTask(struct sLaika_taskService *service) {
|
||||
int laikaT_timeTillTask(struct sLaika_taskService *service)
|
||||
{
|
||||
if (service->headTask) {
|
||||
int pause = service->headTask->scheduled - laikaT_getTime();
|
||||
return (pause > 0) ? pause : 0;
|
||||
|
||||
Reference in New Issue
Block a user