EtherLib/prefab/conn_blocks/udp_connblock.c

57 lines
2.0 KiB
C

//
// Created by epagris on 2022.11.07..
//
#include "udp_connblock.h"
#include <stddef.h>
#include "../packet_parsers/packet_parsers.h"
#include "../../utils.h"
#include "../../dynmem.h"
static bool filtUdp(const PcktSieveFilterCondition * filtCond, const PcktProps * contProps, const PcktProps * ownProps) {
IPv4Props * ipProps = (IPv4Props *) contProps;
UdpProps * udpProps = (UdpProps *) ownProps;
return ipProps->Protocol == ETH_UDP_PACKET_CLASS && filtCond->uw[0] == udpProps->DestinationPort;
}
ConnBlock udp_new_connblock(EthInterface * intf, ip4_addr ipAddr, uint16_t port, SieveCallBackFn cbFn) {
ConnBlock udpConnB;
ConnBlock ipConnB = ipv4_new_connblock(intf, ipAddr, NULL); // create new IPv4 connection block
PcktSieveFilterCondition filtCond;
packfiltcond_zero(&filtCond);
filtCond.uw[0] = port;
PcktSieveLayerTag tag;
tag.u = 0;
udpConnB.sieveLayer = packsieve_new_layer(ipConnB.sieveLayer, &filtCond, filtUdp, cbFn, tag, ETH_UDP_PACKET_CLASS);
ASSERT_NULL(udpConnB.sieveLayer);
return udpConnB;
}
#define ALLOC_HEADER_ELEMENT(T) (PcktHeaderElement *) dynmem_alloc(ETH_PCKT_HEADER_ELEMENT_HEAD_SIZE + sizeof(T))
#define HEADER_FETCH_PROPS(T,h) (T *)(&h->props)
int udp_send(const struct ConnBlock_ * connBlock, const uint8_t *data, uint32_t size) {
// allocate headers
PcktHeaderElement * udpHeader = ALLOC_HEADER_ELEMENT(UdpProps);
PcktHeaderElement * ipHeader = ALLOC_HEADER_ELEMENT(IPv4Props);
PcktHeaderElement * ethHeader = ALLOC_HEADER_ELEMENT(EthernetProps);
udpHeader->next = NULL;
udpHeader->prev = ipHeader;
ipHeader->next = udpHeader;
ipHeader->prev = ethHeader;
ethHeader->next = ipHeader;
ethHeader->prev = NULL;
UdpProps * udpProps = HEADER_FETCH_PROPS(UdpProps, udpHeader);
IPv4Props * ipProps = HEADER_FETCH_PROPS(IPv4Props, udpHeader);
EthernetProps * ethProps = HEADER_FETCH_PROPS(EthernetProps, udpHeader);
udpProps->Length = size;
return 0;
}