EtherLib/packet_registry.c

53 lines
1.7 KiB
C

#include "packet_registry.h"
#include "global_state.h"
#include "dynmem.h"
#include "utils.h"
#include "packet_sieve.h"
#include <stddef.h>
#include <memory.h>
#define PCKTREG_INITIAL_RESERVED_CNT (6)
PcktRegistry * packreg_new() {
PcktRegistry * packReg = (PcktRegistry *)dynmem_alloc(sizeof(PcktRegistry));
ASSERT_NULL(packReg);
packReg->reservedCnt = PCKTREG_INITIAL_RESERVED_CNT;
packReg->ents = (PcktClassDesc *) dynmem_alloc(sizeof(PcktClassDesc) * packReg->reservedCnt);
ASSERT_NULL(packReg->ents);
packReg->entCnt = 0;
return packReg;
}
void packreg_add_class(PcktRegistry * packReg, const PcktClassDesc * classDesc) {
// if buffer is full and reallocation and resize is needed
if (packReg->reservedCnt == packReg->entCnt) {
PcktClassDesc * newEnts = (PcktClassDesc *) dynmem_alloc(packReg->reservedCnt * 2 * sizeof(PcktClassDesc));
ASSERT_NULL(newEnts);
memcpy(newEnts, packReg->ents, packReg->reservedCnt * sizeof(PcktClassDesc));
PcktClassDesc * oldEnts = packReg->ents;
packReg->ents = newEnts;
dynmem_free(oldEnts);
packReg->reservedCnt *= 2;
}
// append new item
PcktClassDesc * newEntry = packReg->ents + packReg->entCnt;
*newEntry = *classDesc;
newEntry->cacheSize = 0; // initialize cache
newEntry->cacheArea = NULL;
packReg->entCnt++;
}
PcktClassDesc * packreg_get_by_class(PcktRegistry * packReg, uint16_t ownClass, uint16_t containerClass) {
uint32_t i;
for (i = 0; i < packReg->entCnt; i++) {
PcktClassDesc * cdesc = packReg->ents + i;
if ((cdesc->class == ownClass) && ((cdesc->containerClass == containerClass) || (containerClass == 0))) {
return cdesc;
}
}
return NULL;
}