EtherLib/prefab/packet_parsers/ethernet_frame.h

51 lines
1.4 KiB
C

//
// Created by epagris on 2022.11.03..
//
#ifndef ETHERLIB_ETHERNET_FRAME_H
#define ETHERLIB_ETHERNET_FRAME_H
#include <stdint.h>
#include "../../packet_registry.h"
#include "../../packet_sieve.h"
#define ETH_HW_ADDR_LEN (6)
#define ETH_ETHERNET_HEADER_SIZE (14)
#define HEADER_FETCH_PROPS(T,h) (T *)(&((h)->props))
#define ALLOC_HEADER_ELEMENT(T) (PcktHeaderElement *) dynmem_alloc(ETH_PCKT_HEADER_ELEMENT_HEAD_SIZE + sizeof(T))
typedef uint8_t EthernetAddress[ETH_HW_ADDR_LEN];
/**
* @struct EthernetProps
* Ethernet frame properties
*/
typedef struct {
PcktPropsHeader;
EthernetAddress destAddr[ETH_HW_ADDR_LEN]; ///< destination address
EthernetAddress sourceAddr[ETH_HW_ADDR_LEN]; ///< source address
uint16_t length_type; ///< frame length_type
} EthernetProps;
struct EthInterface_;
/**
* Parse raw Ethernet frames.
* @param hdr pointer to the Ethernet packet header
* @param size total frame size
* @param pcktHdrLe pointer to property storage
* @return EtherType or 0 (if size is less than 1500) on success or -1 on error
*/
int parse_ethernet(const uint8_t *hdr, uint32_t size, PcktHeaderElement *pcktHdrLe, struct EthInterface_ *intf);
/**
* Insert Ethernet header to specified address.
* @param hdr space where the header is to be inserted
* @param headers linked list of header, top is always relevant
*/
void insert_ethernet_header(uint8_t * hdr, const PcktHeaderElement * headers);
#endif //ETHERLIB_ETHERNET_FRAME_H