2022-06-27 23:57:00 +00:00
|
|
|
#include "lpeer.h"
|
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl,
|
|
|
|
struct sLaika_pollList *pList, pollFailEvent onPollFail,
|
|
|
|
void *onPollFailUData, void *uData)
|
|
|
|
{
|
2022-01-24 03:28:16 +00:00
|
|
|
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
laikaS_initSocket(&peer->sock, laikaS_handlePeerIn, laikaS_handlePeerOut, onPollFail,
|
|
|
|
onPollFailUData);
|
2022-03-28 20:49:50 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
peer->packetTbl = pktTbl;
|
2022-01-25 02:57:54 +00:00
|
|
|
peer->pList = pList;
|
|
|
|
peer->uData = uData;
|
2022-01-24 03:28:16 +00:00
|
|
|
peer->pktSize = 0;
|
2022-03-18 04:05:18 +00:00
|
|
|
peer->type = PEER_UNKNWN;
|
|
|
|
peer->osType = OS_UNKNWN;
|
2022-01-24 03:28:16 +00:00
|
|
|
peer->pktID = LAIKAPKT_MAXNONE;
|
2022-02-04 02:51:32 +00:00
|
|
|
peer->outStart = -1;
|
|
|
|
peer->inStart = -1;
|
|
|
|
peer->useSecure = false;
|
2022-02-15 22:57:21 +00:00
|
|
|
|
2022-03-05 02:17:03 +00:00
|
|
|
/* zero-out peer info */
|
2022-02-15 22:57:21 +00:00
|
|
|
memset(peer->hostname, 0, LAIKA_HOSTNAME_LEN);
|
2022-03-05 02:17:03 +00:00
|
|
|
memset(peer->inet, 0, LAIKA_INET_LEN);
|
2022-06-04 15:11:28 +00:00
|
|
|
memset(peer->ipStr, 0, LAIKA_IPSTR_LEN);
|
2022-05-17 15:37:58 +00:00
|
|
|
|
2022-06-30 01:31:22 +00:00
|
|
|
/* generate peer's salt */
|
|
|
|
laikaS_genSalt(peer);
|
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_freePeer(struct sLaika_peer *peer)
|
|
|
|
{
|
2022-01-24 03:28:16 +00:00
|
|
|
laikaS_cleanSocket(&peer->sock);
|
|
|
|
laikaM_free(peer);
|
|
|
|
}
|
|
|
|
|
2022-06-30 01:31:22 +00:00
|
|
|
void laikaS_setSalt(struct sLaika_peer *peer, uint8_t *salt)
|
|
|
|
{
|
|
|
|
memcpy(peer->salt, salt, LAIKA_HANDSHAKE_SALT_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaS_genSalt(struct sLaika_peer *peer)
|
|
|
|
{
|
|
|
|
randombytes_buf(peer->salt, LAIKA_HANDSHAKE_SALT_LEN);
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:20:23 +00:00
|
|
|
/* ===================================[[ Start/End Packets ]]=================================== */
|
2022-05-18 17:04:19 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
|
|
|
{
|
2022-02-28 22:27:55 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
|
|
|
|
laikaS_writeByte(sock, id);
|
|
|
|
|
|
|
|
/* add to pollList's out queue */
|
2022-03-24 15:26:06 +00:00
|
|
|
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
2022-02-28 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
|
|
|
{
|
2022-02-04 02:51:32 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
if (peer->outStart != -1) /* sanity check */
|
|
|
|
LAIKA_ERROR("unended OUT packet!\n");
|
|
|
|
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_writeByte(sock, id);
|
|
|
|
|
|
|
|
peer->outStart = sock->outCount;
|
2022-06-27 23:57:00 +00:00
|
|
|
if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the
|
|
|
|
packet ID */
|
2022-02-04 02:51:32 +00:00
|
|
|
uint8_t nonce[crypto_secretbox_NONCEBYTES];
|
|
|
|
randombytes_buf(nonce, crypto_secretbox_NONCEBYTES);
|
|
|
|
laikaS_write(sock, nonce, crypto_secretbox_NONCEBYTES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
int laikaS_endOutPacket(struct sLaika_peer *peer)
|
|
|
|
{
|
2022-02-04 02:51:32 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
uint8_t *body;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
if (peer->useSecure) {
|
|
|
|
/* make sure we have enough space */
|
2022-06-27 23:57:00 +00:00
|
|
|
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount,
|
|
|
|
sock->outCap);
|
2022-02-04 02:51:32 +00:00
|
|
|
|
|
|
|
/* packet body starts after the id & nonce */
|
|
|
|
body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES];
|
|
|
|
/* encrypt packet body in-place */
|
2022-06-27 23:57:00 +00:00
|
|
|
if (crypto_secretbox_easy(body, body,
|
|
|
|
(sock->outCount - peer->outStart) - crypto_secretbox_NONCEBYTES,
|
|
|
|
&sock->outBuf[peer->outStart], peer->outKey) != 0) {
|
2022-02-28 22:27:55 +00:00
|
|
|
LAIKA_ERROR("Failed to encrypt packet!\n");
|
2022-02-04 02:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sock->outCount += crypto_secretbox_MACBYTES;
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:55:30 +00:00
|
|
|
/* add to pollList's out queue */
|
2022-03-24 15:26:06 +00:00
|
|
|
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
2022-02-14 05:55:30 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* return packet size and prepare for next outPacket */
|
2022-02-04 02:51:32 +00:00
|
|
|
sz = sock->outCount - peer->outStart;
|
|
|
|
peer->outStart = -1;
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
|
|
|
|
{
|
2022-02-19 01:32:16 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
if (peer->outStart != -1) /* sanity check */
|
|
|
|
LAIKA_ERROR("unended OUT packet!\n");
|
2022-02-19 01:32:16 +00:00
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
laikaS_writeByte(sock, LAIKAPKT_VARPKT);
|
2022-06-27 23:57:00 +00:00
|
|
|
laikaS_zeroWrite(sock,
|
|
|
|
sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */
|
2022-02-19 01:32:16 +00:00
|
|
|
laikaS_startOutPacket(peer, id);
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
int laikaS_endVarPacket(struct sLaika_peer *peer)
|
|
|
|
{
|
2022-02-19 01:32:16 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
2022-06-27 23:57:00 +00:00
|
|
|
int patchIndx = peer->outStart -
|
|
|
|
(sizeof(LAIKAPKT_SIZE) + sizeof(LAIKAPKT_ID)); /* gets index of packet size */
|
2022-02-19 01:32:16 +00:00
|
|
|
LAIKAPKT_SIZE sz = (LAIKAPKT_SIZE)laikaS_endOutPacket(peer);
|
|
|
|
|
|
|
|
/* patch packet size */
|
2022-06-27 23:57:00 +00:00
|
|
|
memcpy((void *)&sock->outBuf[patchIndx], (void *)&sz, sizeof(LAIKAPKT_SIZE));
|
2022-02-19 01:32:16 +00:00
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic)
|
|
|
|
{
|
2022-02-04 02:51:32 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
if (peer->inStart != -1) /* sanity check */
|
|
|
|
LAIKA_ERROR("unended IN packet!\n");
|
2022-02-04 02:51:32 +00:00
|
|
|
|
2022-02-16 23:58:07 +00:00
|
|
|
/* if we're encrypting/decrypting all packets, make sure to make the packetsize reflect this */
|
2022-02-28 22:27:55 +00:00
|
|
|
if (peer->useSecure && !variadic && peer->pktSize != 0)
|
2022-02-16 23:58:07 +00:00
|
|
|
peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
|
|
|
|
|
2022-02-04 02:51:32 +00:00
|
|
|
peer->inStart = sock->inCount;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
int laikaS_endInPacket(struct sLaika_peer *peer)
|
|
|
|
{
|
2022-02-04 02:51:32 +00:00
|
|
|
struct sLaika_socket *sock = &peer->sock;
|
|
|
|
uint8_t *body;
|
|
|
|
size_t sz = sock->inCount - peer->inStart;
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
if (peer->useSecure && sz > crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) {
|
2022-02-04 02:51:32 +00:00
|
|
|
body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES];
|
|
|
|
|
|
|
|
/* decrypt packet body in-place */
|
2022-06-27 23:57:00 +00:00
|
|
|
if (crypto_secretbox_open_easy(
|
|
|
|
body, body, (sock->inCount - peer->inStart) - crypto_secretbox_NONCEBYTES,
|
2022-02-04 02:51:32 +00:00
|
|
|
&sock->inBuf[peer->inStart], peer->inKey) != 0) {
|
2022-02-28 22:27:55 +00:00
|
|
|
LAIKA_ERROR("Failed to decrypt packet!\n");
|
2022-02-04 02:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* decrypted message is smaller now */
|
|
|
|
sock->inCount -= crypto_secretbox_MACBYTES;
|
|
|
|
|
|
|
|
/* remove nonce */
|
|
|
|
laikaM_rmvarray(sock->inBuf, sock->inCount, peer->inStart, crypto_secretbox_NONCEBYTES);
|
|
|
|
|
|
|
|
sz -= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->inStart = -1;
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_setSecure(struct sLaika_peer *peer, bool flag)
|
|
|
|
{
|
2022-02-04 02:51:32 +00:00
|
|
|
peer->useSecure = flag;
|
|
|
|
}
|
|
|
|
|
2022-06-27 23:20:23 +00:00
|
|
|
/* ==================================[[ Handle Poll Events ]]=================================== */
|
2022-05-18 17:04:19 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
bool laikaS_handlePeerIn(struct sLaika_socket *sock)
|
|
|
|
{
|
|
|
|
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
|
2022-01-24 03:28:16 +00:00
|
|
|
int recvd;
|
|
|
|
|
|
|
|
switch (peer->pktID) {
|
2022-06-27 23:57:00 +00:00
|
|
|
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));
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
|
|
|
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
|
|
|
|
LAIKA_ERROR("variable packet too large!\n");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
/* dispatch to packet handler */
|
|
|
|
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
peer->sock.inCount = 0;
|
|
|
|
peer->pktID = LAIKAPKT_MAXNONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-01-24 03:28:16 +00:00
|
|
|
}
|
2022-01-24 16:34:30 +00:00
|
|
|
|
|
|
|
return laikaS_isAlive((&peer->sock));
|
2022-01-24 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
bool laikaS_handlePeerOut(struct sLaika_socket *sock)
|
|
|
|
{
|
|
|
|
struct sLaika_peer *peer = (struct sLaika_peer *)sock;
|
2022-01-24 03:28:16 +00:00
|
|
|
int sent;
|
|
|
|
|
|
|
|
if (peer->sock.outCount == 0) /* sanity check */
|
2022-01-25 03:46:29 +00:00
|
|
|
return true;
|
2022-01-24 03:28:16 +00:00
|
|
|
|
|
|
|
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) {
|
2022-06-27 23:57:00 +00:00
|
|
|
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);
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
return true;
|
|
|
|
default: /* panic! */
|
|
|
|
case RAWSOCK_CLOSED:
|
|
|
|
case RAWSOCK_ERROR:
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-24 03:28:16 +00:00
|
|
|
}
|