Removed unused content streams

This commit is contained in:
CPunch 2022-06-26 19:02:21 -05:00
parent 43ef603301
commit ca0543fe90
9 changed files with 0 additions and 350 deletions

View File

@ -52,11 +52,4 @@ void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
/* content stream has finished */
void laikaC_contentRecvEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content);
/* request to open a content stream */
bool laikaC_contentNewEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content);
/* error happened on a stream */
void laikaC_contentErrEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content, CONTENT_ERRCODE err);
#endif

View File

@ -157,20 +157,3 @@ void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uD
laikaS_write(&shell->auth->sock, buf, sz-sizeof(uint32_t));
laikaS_endVarPacket(shell->auth);
}
/* ============================================[[ Content Handlers ]]============================================ */
/* content stream has finished */
void laikaC_contentRecvEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content) {
}
/* request to open a content stream */
bool laikaC_contentNewEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content) {
}
/* error happened on a stream */
void laikaC_contentErrEvent(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content, CONTENT_ERRCODE err) {
}

View File

@ -35,7 +35,5 @@ struct sLaika_socket;
struct sLaika_pollList;
struct sLaika_task;
struct sLaika_taskService;
struct sLaika_content;
struct sLaika_contentContext;
#endif

View File

@ -1,65 +0,0 @@
#ifndef LAIKA_CONTENT_H
#define LAIKA_CONTENT_H
#include "laika.h"
#include "lpacket.h"
#include <stdio.h>
#include <inttypes.h>
enum {
CONTENT_IN = true, /* being recv'd from peer */
CONTENT_OUT = false /* being sent to peer */
};
enum {
CONTENT_TYPE_FILE
};
enum {
CONTENT_ERR_ID_IN_USE,
CONTENT_ERR_INVALID_ID,
CONTENT_ERR_REJECTED
};
typedef uint8_t CONTENT_TYPE;
typedef uint8_t CONTENT_ERRCODE;
typedef uint16_t CONTENT_ID;
typedef void (*contentRecvEvent)(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content);
typedef bool (*contentNewEvent)(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content);
typedef void (*contentErrorEvent)(struct sLaika_peer *peer, struct sLaika_contentContext *context, struct sLaika_content *content, CONTENT_ERRCODE err);
struct sLaika_content {
struct sLaika_content *next;
FILE *fd;
uint32_t sz; /* content size */
uint32_t processed;
CONTENT_ID id;
CONTENT_TYPE type;
bool direction;
};
struct sLaika_contentContext {
struct sLaika_content *head;
CONTENT_ID nextID;
contentRecvEvent onReceived;
contentNewEvent onNew;
contentErrorEvent onError;
};
void laikaF_initContext(struct sLaika_contentContext *context);
void laikaF_cleanContext(struct sLaika_contentContext *context);
void laikaF_setupEvents(struct sLaika_contentContext *context, contentRecvEvent onRecv, contentNewEvent onNew, contentErrorEvent onError);
int laikaF_nextID(struct sLaika_peer *peer); /* returns the id that will be assigned to the next sent content */
int laikaF_sendContent(struct sLaika_peer *peer, FILE *fd, CONTENT_TYPE type);
void laikaF_pollContent(struct sLaika_peer *peer);
void laikaF_handleContentNew(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaF_handleContentError(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
void laikaF_handleContentChunk(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
#endif

View File

@ -73,22 +73,6 @@ enum {
* uint32_t id;
* char buf[VAR_PACKET_LENGTH-sizeof(uint32_t)];
*/
LAIKAPKT_CONTENT_NEW,
/* layout of LAIKAPKT_CONTENT_NEW:
* uint16_t id;
* uint32_t sz;
* uint8_t type;
*/
LAIKAPKT_CONTENT_ERROR,
/* layout of LAIKAPKT_CONTENT_ERROR:
* uint16_t id;
* uint8_t errCode;
*/
LAIKAPKT_CONTENT_CHUNK, /* variadic */
/* layout of LAIKAPKT_CONTENT_CHUNK:
* uint16_t id;
* uint8_t buf[VAR_PACKET_LENGTH-sizeof(uint16_t)];
*/
/* ==================================================[[ Auth ]]================================================== */
LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ, /* second packet sent by authenticated peers (panel). there is no response packet */
/* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ

View File

@ -6,7 +6,6 @@
#include "lpacket.h"
#include "lpolllist.h"
#include "lsodium.h"
#include "lcontent.h"
typedef enum {
PEER_UNKNWN,
@ -43,7 +42,6 @@ struct sLaika_peerPacketInfo {
struct sLaika_peer {
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_contentContext context;
uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES];
char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipStr[LAIKA_IPSTR_LEN];

View File

@ -1,235 +0,0 @@
#include "laika.h"
#include "lcontent.h"
#include "lmem.h"
#include "lerror.h"
#include "lsocket.h"
#include "lpeer.h"
#define CONTENTCHUNK_MAX_BODY (LAIKA_MAX_PKTSIZE-sizeof(CONTENT_ID))
/* ===========================================[[ Helper Functions ]]============================================= */
size_t getSize(FILE *fd) {
size_t sz;
fseek(fd, 0L, SEEK_END);
sz = ftell(fd);
fseek(fd, 0L, SEEK_SET);
return sz;
}
struct sLaika_content* getContentByID(struct sLaika_contentContext *context, CONTENT_ID id) {
struct sLaika_content *curr = context->head;
while (curr) {
if (curr->id == id)
return curr;
curr = curr->next;
}
return NULL;
}
void freeContent(struct sLaika_content *content) {
fclose(content->fd);
laikaM_free(content);
}
void rmvContent(struct sLaika_contentContext *context, struct sLaika_content *content) {
struct sLaika_content *last = NULL, *curr = context->head;
while (curr) {
/* if found, remove it! */
if (curr == content) {
if (last)
last->next = curr->next;
else
context->head = curr->next;
freeContent(curr);
break;
}
last = curr;
curr = curr->next;
}
}
void sendContentError(struct sLaika_peer *peer, CONTENT_ID id, CONTENT_ERRCODE err) {
laikaS_startOutPacket(peer, LAIKAPKT_CONTENT_ERROR);
laikaS_writeInt(&peer->sock, &id, sizeof(CONTENT_ID));
laikaS_writeByte(&peer->sock, err);
laikaS_endOutPacket(peer);
}
/* ==============================================[[ Content API ]]=============================================== */
void laikaF_initContext(struct sLaika_contentContext *context) {
context->head = NULL;
context->nextID = 0;
context->onReceived = NULL;
context->onNew = NULL;
context->onError = NULL;
}
void laikaF_cleanContext(struct sLaika_contentContext *context) {
struct sLaika_content *tmp, *curr;
/* free content list */
curr = context->head;
while (curr) {
tmp = curr->next;
freeContent(curr);
curr = tmp;
}
}
void laikaF_setupEvents(struct sLaika_contentContext *context, contentRecvEvent onRecv, contentNewEvent onNew, contentErrorEvent onError) {
context->onReceived = onRecv;
context->onNew = onNew;
context->onError = onError;
}
struct sLaika_content* laikaF_newContent(struct sLaika_contentContext *context, FILE *fd, size_t sz, CONTENT_ID id, CONTENT_TYPE type, bool direction) {
struct sLaika_content *content = (struct sLaika_content*)laikaM_malloc(sizeof(struct sLaika_content));
/* init content struct */
content->fd = fd;
content->sz = sz;
content->processed = 0;
content->id = id;
content->type = type;
content->direction = direction;
/* add to list */
content->next = context->head;
context->head = content;
return content;
}
int laikaF_nextID(struct sLaika_peer *peer) {
return peer->context.nextID + 1;
}
int laikaF_sendContent(struct sLaika_peer *peer, FILE *fd, CONTENT_TYPE type) {
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *content = laikaF_newContent(context, fd, getSize(fd), context->nextID++, type, CONTENT_OUT);
/* let the peer know we're sending them some content */
laikaS_startOutPacket(peer, LAIKAPKT_CONTENT_NEW);
laikaS_writeInt(&peer->sock, &content->id, sizeof(CONTENT_ID));
laikaS_writeInt(&peer->sock, &content->sz, sizeof(uint32_t));
laikaS_writeByte(&peer->sock, type);
laikaS_endOutPacket(peer);
return content->id;
}
/* new content we're recieving from a peer */
struct sLaika_content* laikaF_recvContent(struct sLaika_peer *peer, CONTENT_ID id, uint32_t sz, CONTENT_TYPE type) {
struct sLaika_contentContext *context = &peer->context;
if (getContentByID(context, id)) {
sendContentError(peer, id, CONTENT_ERR_ID_IN_USE);
LAIKA_ERROR("ID [%d] is in use!\n", id);
}
return laikaF_newContent(context, tmpfile(), sz, id, type, CONTENT_IN);
}
void laikaF_pollContent(struct sLaika_peer *peer) {
uint8_t buff[CONTENTCHUNK_MAX_BODY];
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *tmp, *curr = context->head;
int rd;
/* traverse our out content, sending each chunk */
while (curr) {
if (curr->direction == CONTENT_OUT) {
/* if we've reached the end of the file stream, remove it! */
if (rd = fread(buff, sizeof(uint8_t), MIN(curr->sz - curr->processed, CONTENTCHUNK_MAX_BODY), curr->fd) == 0) {
tmp = curr->next;
rmvContent(context, curr);
curr = tmp;
continue;
}
/* send chunk */
laikaS_startVarPacket(peer, LAIKAPKT_CONTENT_CHUNK);
laikaS_writeInt(&peer->sock, &curr->id, sizeof(CONTENT_ID));
laikaS_write(&peer->sock, buff, rd);
laikaS_endVarPacket(peer);
curr->processed += rd;
}
curr = curr->next;
}
}
/* ============================================[[ Packet Handlers ]]============================================= */
void laikaF_handleContentNew(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *content;
uint32_t contentSize;
CONTENT_ID contentID;
CONTENT_TYPE contentType;
laikaS_readInt(&peer->sock, &contentID, sizeof(CONTENT_ID));
laikaS_readInt(&peer->sock, &contentSize, sizeof(uint32_t));
contentType = laikaS_readByte(&peer->sock);
content = laikaF_recvContent(peer, contentID, contentSize, contentType);
if (context->onNew && !context->onNew(peer, context, content)) {
sendContentError(peer, contentID, CONTENT_ERR_REJECTED);
rmvContent(context, content);
}
}
void laikaF_handleContentError(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *content;
CONTENT_ID contentID;
uint8_t errCode;
laikaS_readInt(&peer->sock, &contentID, sizeof(CONTENT_ID));
errCode = laikaS_readByte(&peer->sock);
if ((content = getContentByID(context, contentID)) == NULL)
LAIKA_ERROR("Received error for non-existant id %d!\n", contentID);
LAIKA_DEBUG("We received an errcode for id %d, err: %d\n", contentID, errCode);
if (context->onError) /* check if event exists! */
context->onError(peer, context, content, errCode);
rmvContent(context, content);
}
void laikaF_handleContentChunk(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
uint8_t buff[CONTENTCHUNK_MAX_BODY];
struct sLaika_contentContext *context = &peer->context;
struct sLaika_content *content;
CONTENT_ID id;
size_t bodySz = sz-sizeof(CONTENT_ID);
if (sz <= sizeof(CONTENT_ID))
LAIKA_ERROR("malformed chunk packet!\n");
/* read and sanity check id */
laikaS_readInt(&peer->sock, &id, sizeof(CONTENT_ID));
if ((content = getContentByID(context, id)) == NULL || content->direction != CONTENT_IN)
LAIKA_ERROR("chunk recieved with invalid id! [%d]\n", id);
/* read data & write to file */
laikaS_read(&peer->sock, buff, bodySz);
if (fwrite(buff, sizeof(uint8_t), bodySz, content->fd) != bodySz) {
rmvContent(context, content);
} else if ((content->processed += bodySz) == content->sz) {
if (context->onReceived) /* check if event exists! */
context->onReceived(peer, context, content);
}
}

View File

@ -10,9 +10,6 @@ const char* laikaD_getPacketName(LAIKAPKT_ID id) {
"LAIKAPKT_SHELL_OPEN",
"LAIKAPKT_SHELL_CLOSE",
"LAIKAPKT_SHELL_DATA",
"LAIKAPKT_CONTENT_NEW",
"LAIKAPKT_CONTENT_ERROR",
"LAIKAPKT_CONTENT_CHUNK",
"LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ",
"LAIKAPKT_AUTHENTICATED_ADD_PEER_RES",
"LAIKAPKT_AUTHENTICATED_RMV_PEER_RES",

View File

@ -28,13 +28,10 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct
memset(peer->inet, 0, LAIKA_INET_LEN);
memset(peer->ipStr, 0, LAIKA_IPSTR_LEN);
/* init content context */
laikaF_initContext(&peer->context);
return peer;
}
void laikaS_freePeer(struct sLaika_peer *peer) {
laikaF_cleanContext(&peer->context);
laikaS_cleanSocket(&peer->sock);
laikaM_free(peer);
}