EtherLib/packet_registry.h

74 lines
2.2 KiB
C

#ifndef ETHERLIB_PACKET_REGISTRY_H
#define ETHERLIB_PACKET_REGISTRY_H
#include <stdint.h>
/**
* Boolean on 8-bits, not full machine-word size.
*/
typedef uint8_t bool8_t;
/**
* \typedef PcktProps
* \brief Packet properties
*
* Every packet property structure must extend this base structure,
* so that, must begin with fields defined here. PcktProps is guaranteed
* to be aligned to 32-bit boundary and so must be guaranteed by extending
* as well. (The size of the packet is divisible by 4.)
*/
typedef struct {
uint16_t headerSize; ///< Header size in bytes
uint16_t containedPacketClass; ///< ID of contained packet. Zero if no packet contained.
bool8_t checksumOK; ///< Indicates that checksum is OK.
} PcktProps;
/**
* @typedef int (*PcktProcFn)(const uint8_t *pHdr, uint32_t size, uint8_t *pPcktProps);
* @brief Pckt processing function template.
* @param pHdr: pointer to the packet header
* @param size: remaining packet size counted from pHdr
* @param pPcktProps: pointer to existing structure to store
* packet properties to (e.g. source address, port etc.)
* @return packet class of contained packet, or -1 on failure or
* 0 if no more standard packet contained (e.g. UDP packet contains
* user data)
*/
typedef int (*PcktProcFn)(const uint8_t *pHdr, uint32_t size, uint8_t *pProps);
/**
* @struct PcktClassDesc
* Pckt class descriptor. Pckt parsers can be registered
* using PcktClassDesc assignments.
*/
typedef struct {
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
} PcktClassDesc;
/**
* @struct PcktRegistry
* Packet registry sturcture.
*/
typedef struct {
PcktClassDesc * ents; ///< Packet registry entries
uint16_t entCnt; ///< Number of registry entries
uint16_t reservedCnt; ///< Buffer reservation in elements.
} PcktRegistry;
// ---------------------
/**
* Create new packet registry
*/
PcktRegistry * packreg_new();
/**
* Add new packet class. (e.g. UDP, IP...)
* @param classDesc Packet class descriptor
*/
void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc);
#endif //ETHERLIB_PACKET_REGISTRY_H