EtherLib/eth_interface.h
Epagris 9532b6b274 - EthIntf: packet interception in both directions; DHCP automanage; config loading and storing; info printing
- utils: atoip(), lefttrim()
- ARP: bunch of bugfixes
- TCP, FTP: WIP
- DHCP: mutex; query the FSM state
2024-10-06 23:29:15 +02:00

234 lines
7.5 KiB
C

#ifndef ETHERLIB_ETH_INTERFACE_H
#define ETHERLIB_ETH_INTERFACE_H
#include <stdbool.h>
#include "arp_cache.h"
#include "connection_block.h"
#include "gen_queue.h"
#include "msg_queue.h"
#include "packet_sieve.h"
#include "prefab/conn_blocks/ipv4/ip_assembler.h"
#include "prefab/packet_parsers/dhcp.h"
#include "prefab/packet_parsers/ipv4_types.h"
#include "prefab/packet_parsers/packet_parsers.h"
#include <etherlib_options.h>
/**
* Ethernet interface low level definition.
*/
typedef struct EthIODef_ {
int (*llTxTrigger)(struct EthIODef_ *io, MsgQueue *mq); ///< Function pointer to low-level transmit function trigger
int (*llTxDone)(struct EthIODef_ *io, const RawPckt *rawPckt); ///< Transmission done (interrupt) callback
int (*llLinkChg)(struct EthIODef_ *io, int linkState, uint16_t speed, bool duplex); ///< Link change interrupt
int (*llTrigLinkChg)(struct EthIODef_ *io); ///< Trigger a link change interrupt callback
int (*llRxStore)(struct EthIODef_ *io, const RawPckt *rawPckt); ///< Receive done callback
int (*llError)(struct EthIODef_ *io, int error); ///< Low-level error interrupt
int (*llRxRead)(struct EthIODef_ *io); ///< Read received packets
int (*llRxNotify)(struct EthIODef_ *io); ///< Notify of received packets
void *tag; ///< Some arbitrary tagging
} EthIODef;
/**
* Ethernet interface capabilities
*/
typedef enum {
ETHINF_CAP_NONE = 0,
ETHINF_CAP_TX_CRC_OFFLOAD = 1,
ETHINF_CAP_TX_IPCHKSUM_OFFLOAD = 2,
ETHINF_CAP_TX_TCPUDPCHKSUM_OFFLOAD = 4,
ETHINF_CAP_RX_IPCHKSUM_OFFLOAD = 8,
ETHINF_CAP_RX_TCPUDPCHKSUM_OFFLOAD = 16,
ETHINF_CAP_ALL_RX_TX_CHECKSUM_OFFLOADS = 0b11111,
} EthIntfCap;
struct EthInterface_;
typedef void (*EthIntfEvtCb)(struct EthInterface_ *intf);
/**
* Frame interception direction.
*/
typedef enum {
ETH_INTERCEPT_TX = 1,
ETH_INTERCEPT_RX = 2
} EthIntfInterceptDir;
/**
* Function prototype for intercepting packets on an interface.
* @param intf pointer to corresponding Ethernet interface
* @param rawPckt pointer to the IMMUTABLE raw packet
*/
typedef void (*EthIntfInterceptCb)(struct EthInterface_ *intf, const RawPckt *rawPckt);
typedef enum {
ETH_EVT_LINK_CHANGE = 0,
ETH_EVT_IP_CHANGE = 1,
} EthIntfEvIndex;
/**
* Ethernet interface event callback table
*/
typedef struct {
EthIntfEvtCb linkChange; ///< Link change event
EthIntfEvtCb ipChange; ///< IP-address changed
} EthIntfEventCbTable;
/**
* Ethernet interface representation.
*/
typedef struct EthInterface_ {
PcktSieve sieve; ///< Packet sieve
EthIODef *ioDef; ///< Low-level IO definitions
EthernetAddress mac; ///< Ethernet address
uint32_t capabilities; ///< Ethernet interface capabilities
DhcpState *dhcp; ///< DHCP control block (allocated only, if DHCP operation is initiated)
bool manageDhcp; ///< Let the interface automatically manage the DHCP client (start/stop)
ip4_addr ip; ///< IP address
ip4_addr router; ///< Router IP address
ip4_addr netmask; ///< Subnet mask
ip4_addr dns; ///< Domain Name Server
ArpCache *arpc; ///< ARP cache
ConnBlock arpCb; ///< ARP connection block
MsgQueue *txQ; ///< Transmit queue
MsgQueue *rxQ; ///< Receive queue
ETHLIB_OS_QUEUE_TYPE eventQ; ///< Event queue
// ETHLIB_OS_SEM_TYPE eventSem; ///< Event queue semaphore
IPv4Assembler *ipra; ///< IPv4 reassembler
EthIntfEventCbTable evtCbTable; ///< Event callback table
bool linkState; ///< Interface link state
bool up; ///< Is the interface up?
EthIntfInterceptCb interceptTxCb; ///< Intercept TX callback
EthIntfInterceptCb interceptRxCb; ///< Intercept RX callback
} EthInterface;
/*
* Ethernet interface configuration.
*/
typedef struct {
ip4_addr ip, router, netmask, dns;
bool manageDhcp;
} EthInterfaceNetworkConfig;
/**
* Allocate a new Ethernet interface
* @param io Low-level IO definitions
* @return new interface instance
*/
EthInterface *ethintf_new(EthIODef *io);
/**
* Receive uncooked packet.
* @param rawPckt
*/
void ethinf_receive(EthInterface *intf, const RawPckt *rawPckt);
/**
* Notify interface of some event occured (e.g. a received, buffered packet, which needs to get
* transferred from the low level Ethernet-driver, a link change occured etc.)
* @param intf Pointer to Ethernet-interface
* @param event_code Event code
* @param event_data Event data
*/
void ethinf_push_notification(EthInterface *intf, uint16_t event_code, uint16_t event_data);
/**
* Pull formerly pushed notification from the event queue.
* @param intf Pointer to Ethernet-interface
* @param event_code pointer to event code
* @param event_data pointer to event data
*/
void ethinf_pull_notification(EthInterface *intf, uint16_t *event_code, uint16_t *event_data);
/**
* Transmit packet.
*/
void ethinf_transmit(EthInterface *intf, const RawPckt *rawPckt);
/**
* Set interface capabilities.
* @param intf pointer to Ethernet interface
* @param cap OR-ed bitfield of capabilities
*/
void ethinf_set_capabilities(EthInterface *intf, uint32_t cap);
/**
* Set Ethernet event callback.
* @param intf pointer to Ethernet interface
* @param idx index of Ethernet event
* @param cb callback function pointer
*/
void ethinf_set_event_callback(EthInterface *intf, EthIntfEvIndex idx, EthIntfEvtCb cb);
/**
* Trigger Ethernet event.
* @param intf pointer to Ethernet interface
* @param idx index of Ethernet interface event
*/
void ethinf_trigger_event(EthInterface *intf, EthIntfEvIndex idx);
/**
* Set link state.
* @param intf pointer to Ethernet interface
* @param ls link state
*/
void ethinf_set_link_state(EthInterface *intf, bool ls);
/**
* Get link state.
* @param intf pointer to Ethernet interface
* @return link state
*/
bool ethinf_get_link_state(EthInterface *intf);
/**
* Enable interface.
* @param intf pointer to Ethernet interface
*/
void ethinf_up(EthInterface *intf);
/**
* Disable interface.
* @param intf pointer to Ethernet interface
*/
void ethinf_down(EthInterface *intf);
/**
* Set callback for packet interception.
* @param intf pointer to Ethernet interface
* @param cb intercept callback pointer
*/
void ethinf_set_intercept_callback(EthInterface *intf, EthIntfInterceptCb cb, EthIntfInterceptDir dir);
/**
* Turn ON/OFF automatic DHCP client management.
* @param intf pointer to Ethernet interface
* @param automg automanagement enabled/disabled
*/
void ethinf_set_automatic_dhcp_management(EthInterface *intf, bool automg);
/**
* Set interface configuration.
* @param intf pointer to Ethernet interface
* @param config pointer to a filled config object
*/
void ethinf_set_config(EthInterface * intf, const EthInterfaceNetworkConfig * config);
/**
* Get interface configuration.
* @param intf pointer to Ethernet interface
* @param config pointer to an empty config object
*/
void ethinf_get_config(EthInterface *intf, EthInterfaceNetworkConfig *config);
/**
* Printf information on Ethernet interface.
* @param intf pointer to Ethernet interface
*/
void ethinf_print_info(const EthInterface * intf);
#endif // ETHERLIB_ETH_INTERFACE_H