Wiesner András 05288d7a3c - ARP cache auto lookup feature added
- IGMPv2 capabilities added (report membership, leave group)
- ICMP capabilities added (ping)
- Tx Message Queue added
2023-01-14 14:24:56 +01:00

59 lines
2.0 KiB
C

#include <stdbool.h>
#include "udp_packet.h"
#include "../../utils.h"
#include "ipv4_packet.h"
#include "ethernet_frame.h"
#define ETH_UDP_HEADER_SIZE (8)
typedef struct {
uint32_t sourceIpAddr;
uint32_t destIpAddr;
uint8_t zero;
uint8_t protocol;
uint16_t udpLength;
} UdpPseudoHeader;
static uint16_t udp_checksum(const UdpPseudoHeader *pseudoHeader, const uint8_t * hdr, uint32_t size) {
uint32_t sum = chksum((const uint8_t *)pseudoHeader, sizeof(UdpPseudoHeader)) + chksum(hdr, size);
while (sum >> 16) {
sum = (sum & 0xFFFF) + (sum >> 16);
}
return sum;
}
int parse_udp(const uint8_t *hdr, uint32_t size, PcktHeaderElement *pcktHdrLe, struct EthInterface_ *intf) {
UdpProps *udpProps = HEADER_FETCH_PROPS(UdpProps, pcktHdrLe);
FETCH_WORD_H2N_ADVANCE(&udpProps->SourcePort, hdr);
FETCH_WORD_H2N_ADVANCE(&udpProps->DestinationPort, hdr);
FETCH_WORD_H2N_ADVANCE(&udpProps->Length, hdr);
FETCH_WORD_H2N_ADVANCE(&udpProps->Checksum, hdr);
// common fields...
udpProps->headerSize = ETH_UDP_HEADER_SIZE;
udpProps->validityOK = (size == udpProps->Length); // TODO UDP checksum validation!
udpProps->containedPacketClass = 0;
return udpProps->validityOK ? 0 : -1;
}
void insert_udp_header(uint8_t *hdr, const PcktHeaderElement *headers) {
uint8_t *hdrBegin = hdr;
UdpProps *udpProps = (UdpProps *) &headers->props;
FILL_WORD_H2N_ADVANCE(hdr, udpProps->SourcePort);
FILL_WORD_H2N_ADVANCE(hdr, udpProps->DestinationPort);
FILL_WORD_H2N_ADVANCE(hdr, udpProps->Length);
uint8_t *ChkSumPtr = hdr;
udpProps->Checksum = 0;
FILL_WORD_H2N_ADVANCE(hdr, udpProps->Checksum);
// calculate checksum
const IPv4Props *ipProps = (IPv4Props *) &headers->prev->props;
UdpPseudoHeader ph = {ipProps->SourceIPAddr, ipProps->DestIPAddr, 0, ETH_UDP_PACKET_CLASS, htons(udpProps->Length)};
udpProps->Checksum = udp_checksum(&ph, hdrBegin, udpProps->Length);
memcpy(ChkSumPtr, &udpProps->Checksum, 2);
}