diff --git a/global_state.c b/global_state.c index 89b763e..aa40c09 100644 --- a/global_state.c +++ b/global_state.c @@ -36,10 +36,17 @@ void ethlib_init() { register_packet_parsers(); // register packet parsers } +typedef struct PcktHeaderElement_ { + struct PcktHeaderElement_ * next, * prev; ///< Next and previous header in the linked list + PcktProps props; ///< Properties (allocated to appropriate size) +} PcktHeaderElement; + +#define ETH_PCKT_HEADER_ELEMENT_HEAD_SIZE (2 * sizeof(PcktHeaderElement *)) + void process_raw_packet(const uint8_t * data, uint32_t size) { uint16_t ownClass = 0, containerClass = 0; // Ethernet... uint16_t offset = 0; - PcktProps * props = NULL; + PcktHeaderElement * lastHeader = NULL; do { // get packet descriptor const PcktClassDesc * cdesc = packreg_get_by_class(E.pcktReg, ownClass, containerClass); @@ -48,17 +55,24 @@ void process_raw_packet(const uint8_t * data, uint32_t size) { } // allocate property object - props = (PcktProps *) dynmem_alloc(cdesc->propertySize); + PcktHeaderElement * header = (PcktHeaderElement *) dynmem_alloc(ETH_PCKT_HEADER_ELEMENT_HEAD_SIZE + cdesc->propertySize); + header->props.ownPacketClass = ownClass; + header->props.propSize = cdesc->propertySize; + header->prev = lastHeader; + if (lastHeader) { + lastHeader->next = header; + } // call parsing function - cdesc->procFun(data + offset, size - offset, props); - uint16_t containedClass = props->containedPacketClass; + cdesc->procFun(data + offset, size - offset, &header->props); + uint16_t containedClass = header->props.containedPacketClass; if (containedClass != 0) { containerClass = ownClass; - offset += props->headerSize; - dynmem_free(props); + offset += header->props.headerSize; + //dynmem_free(props); } ownClass = containedClass; + lastHeader = header; } while (ownClass != 0); // TODO process innermost packet diff --git a/packet_registry.h b/packet_registry.h index 05e0cf2..0196f96 100644 --- a/packet_registry.h +++ b/packet_registry.h @@ -18,9 +18,13 @@ typedef uint8_t bool8_t; * as well. (The size of the packet is divisible by 4.) */ +struct PcktClassDesc_; + #define PcktPropsHeader \ + uint16_t propSize; /* Size of this property object */ \ uint16_t headerSize; /*< Header size in bytes */ \ - uint16_t containedPacketClass; /*< ID of contained packet. Zero if no packet contained. */ \ + uint16_t containedPacketClass; /*< Class of contained packet. Zero if no packet contained. */ \ + uint16_t ownPacketClass; /* Our own packet class */ \ bool8_t validityOK; /*< Indicates that checksum is OK. */ \ @@ -46,7 +50,7 @@ typedef int (*PcktProcFn)(const uint8_t *hdr, uint32_t size, PcktProps *props); * Pckt class descriptor. Pckt parsers can be registered * using PcktClassDesc assignments. */ -typedef struct { +typedef struct PcktClassDesc_ { uint16_t class; ///< Type identification of the packet 'class' (unique!) uint16_t containerClass; ///< Type of container packet packet (e.g.: IPv4 in case of UDP) PcktProcFn procFun; ///< Pckt processing function @@ -58,7 +62,7 @@ typedef struct { * Packet registry sturcture. */ typedef struct { - PcktClassDesc * ents; ///< Packet registry entries + PcktClassDesc *ents; ///< Packet registry entries uint16_t entCnt; ///< Number of registry entries uint16_t reservedCnt; ///< Buffer reservation in elements. } PcktRegistry; @@ -68,13 +72,13 @@ typedef struct { /** * Create new packet registry */ -PcktRegistry * packreg_new(); +PcktRegistry *packreg_new(); /** * Add new packet class. (e.g. UDP, IP...) * @param classDesc Packet class descriptor */ -void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc); +void packreg_add(PcktRegistry *packReg, const PcktClassDesc *classDesc); /** * Get packet descriptor by own class and container class. @@ -83,6 +87,6 @@ void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc); * @param containerClass container packet's class (if 0, then search by container class is not considered) * @return pointer to descriptor on success or NULL on failure */ -const PcktClassDesc * packreg_get_by_class(const PcktRegistry * packReg, uint16_t ownClass, uint16_t containerClass); +const PcktClassDesc *packreg_get_by_class(const PcktRegistry *packReg, uint16_t ownClass, uint16_t containerClass); #endif //ETHERLIB_PACKET_REGISTRY_H \ No newline at end of file