EtherLib/packet_sieve.h
2024-04-24 22:23:08 +02:00

182 lines
5.5 KiB
C

#ifndef ETHERLIB_PACKET_SIEVE_H
#define ETHERLIB_PACKET_SIEVE_H
#include <stdbool.h>
#include "packet_registry.h"
#include "packet.h"
/**
* Packet header information.
*/
typedef struct PcktHeaderElement_ {
struct PcktHeaderElement_ *next, *prev; ///< Next and previous header in the linked list
PcktProps props; ///< Properties (allocated to appropriate size)
} PcktHeaderElement;
/**
* Free chain of packet header. List is auto-rewound.
* @param hdr pointer to header chain element
*/
void pckthdr_chain_free(PcktHeaderElement *hdr);
#define ETH_PCKT_HEADER_ELEMENT_HEAD_SIZE (2 * sizeof(PcktHeaderElement *))
/**
* @union PcktSieveFilterCondition
* Union for storing filter conditions.
*/
typedef union {
uint16_t uw[8];
uint32_t u[4];
uint64_t lu[2];
int16_t sw[8]; // signed word
int32_t i[4];
int64_t li[2];
uint8_t d[16];
} PcktSieveFilterCondition;
/**
* Function for comparing filter conditions.
* @param c1 condition 1
* @param c2 condition 2
* @return true if cond1 matches cond2, otherwise false
*/
bool packfiltcond_cmp(const PcktSieveFilterCondition *c1, const PcktSieveFilterCondition *c2);
/**
* Clear packet filter condition structure.
* @param cond pointer to existing filter condition structure
*/
void packfiltcond_zero(PcktSieveFilterCondition *cond);
struct EthInterface_;
/**
* Sieve filter function type.
*/
typedef bool (*SieveFilterFn)(const PcktSieveFilterCondition *filtCond, const PcktProps *contProps, const PcktProps *ownProps, struct EthInterface_ *intf);
struct PcktSieveLayer_;
typedef union {
void *p;
uint32_t u;
} PcktSieveLayerTag;
// actions triggered by callback function returns
#define SIEVE_LAYER_REMOVE_THIS (-100)
/**
* Callback function type for packet sieve match.
*/
typedef int (*SieveCallBackFn)(const Pckt *pckt, PcktSieveLayerTag tag);
/**
* Callback function for printing connblock report.
*/
struct ConnBlock_;
typedef void (*ConnBlockReportFn)(const struct ConnBlock_ *connBlock);
/**
* Timestamp callback function.
*/
typedef void(*TxTimeStampCBFn)(uint32_t ts_s, uint32_t ts_ns, uint32_t tag);
/**
* Management event types.
*/
typedef enum {
PSLEVT_NONE, ///< No event.
PSLEVT_REMOVE ///< The sievele layer is about to get deleted.
} PcktSieveLayerMgmtEvent;
// Management callback return values
#define PSMGMT_RET_PROCEED (0) // proceed with processing
#define PSMGMT_RET_ABORT (1) // abort processing
/**
* Packet sieve layer management function.
*/
typedef int(*MgmtCBFn)(const struct PcktSieveLayer_ * layer, PcktSieveLayerMgmtEvent event);
#define PCKT_SIEVE_INFOTAG_LEN (24)
/**
* Packet sieve layer structure.
*/
typedef struct PcktSieveLayer_ {
uint16_t packetClass; ///< Packet class (e.g. IP)
PcktSieveFilterCondition filtCond; ///< Filter condition, arbitrary type (e.g. destination IP-address)
bool matchAny; ///< Match any packet, don't test against filter condition
SieveFilterFn filtFn; ///< Filter function pointer
SieveCallBackFn cbFn; ///< Associated callback function
PcktSieveLayerTag tag; ///< Layer tag (arbitrary information)
TxTimeStampCBFn txTsCb; ///< Transmit timestamp callback pointer
struct PcktSieveLayer_ *parent; ///< Pointer to parent node in the sieve tree
struct PcktSieveLayer_ *next, *prev; ///< Next and previous sieve layer on the same level
struct PcktSieveLayer_ *nodes; ///< Subnodes in the sieve tree
ConnBlockReportFn connBReportFn; ///< Connection block report function pointer
MgmtCBFn mgmtCb; ///< Management callback function
} PcktSieveLayer;
/**
* Packet sieve class.
*/
typedef struct PcktSieve_ {
PcktSieveLayer layer0; ///< Top of sieve tree
struct EthInterface_ *intf; ///< Ethernet interface
} PcktSieve;
/**
* Create new packet sieve.
* @return pointer to packet sieve object or NULL on failure
*/
PcktSieve *packsieve_new();
/**
* Process packet with packet sieve.
* @param bottomLayer
* @param data
* @param size
*/
void packsieve_input(PcktSieve *sieve, const RawPckt *rawPckt);
/**
* Create a new sieve filter layer.
* @param parent parent layer
* @param filtCond filter condition passed to the filter callback function
* @param matchAny don't test against filter criteria, accept any incoming packet (e.g. pass IP packets with any destination address)
* @param filtFn filter callback function, returning boolean
* @param cbFn callback function invoked, if packet has passed filter criteria
* @param tag arbitrary tag, passed to callbacn function, when invoked
* @param pcktClass packet class associated with current sieve layer
* @return pointer to new layer object or NULL on failure
*/
PcktSieveLayer *packsieve_new_layer(PcktSieveLayer *parent, const PcktSieveFilterCondition *filtCond, bool matchAny, SieveFilterFn filtFn, SieveCallBackFn cbFn, PcktSieveLayerTag tag, uint16_t pcktClass);
/**
* Remove sieve layer from packet sieve.
* @param layer Layer to remove and deallocate
*/
bool packsieve_remove_layer(const PcktSieveLayer *layer);
/**
* Recursively report layer structure on terminal.
* @param sieve pointer to packet sieve structure
* @param layer pointer to existing layer
* @param indent sub-tree indentation
*/
void packsieve_report(const PcktSieve *sieve, const PcktSieveLayer *layer, uint32_t indent);
#define packsieve_report_full(sieve) packsieve_report((sieve), &(sieve)->layer0, 0)
/**
* Report info on given layer and its parents.
* @param sieve pointer to packet sieve structure
* @param layer pointer to an existing layer
*/
void packsieve_layer_info(const PcktSieve *sieve, const PcktSieveLayer *layer);
#endif //ETHERLIB_PACKET_SIEVE_H