34 lines
1.2 KiB
C
34 lines
1.2 KiB
C
//
|
|
// Created by epagris on 2022.11.03..
|
|
//
|
|
|
|
#include <memory.h>
|
|
#include <stdbool.h>
|
|
#include "ethernet_frame.h"
|
|
#include "../../utils.h"
|
|
|
|
#define ETH_ETHERTYPE_LENGTH_THRESHOLD (1500)
|
|
#define ETH_ETHERNET_HEADER_SIZE (14)
|
|
|
|
int parse_ethernet(const uint8_t *hdr, uint32_t size, PcktProps * props) {
|
|
EthernetProps * frameProps = (EthernetProps *)props;
|
|
FETCH_ADVANCE(frameProps->destAddr, hdr, ETH_HW_ADDR_LEN); // copy destination address
|
|
FETCH_ADVANCE(frameProps->sourceAddr, hdr, ETH_HW_ADDR_LEN); // copy destination address
|
|
FETCH_WORD_H2N_ADVANCE(&frameProps->length_type, hdr); // copy length/type field
|
|
|
|
// fill-in packet property header fields
|
|
frameProps->validityOK = true;
|
|
frameProps->containedPacketClass =
|
|
frameProps->length_type <= ETH_ETHERTYPE_LENGTH_THRESHOLD ? 0 : frameProps->length_type;
|
|
frameProps->headerSize = ETH_ETHERNET_HEADER_SIZE;
|
|
|
|
return frameProps->containedPacketClass;
|
|
}
|
|
|
|
void insert_ethernet_header(uint8_t *hdr, const PcktHeaderElement *headers) {
|
|
EthernetProps * ethProps = (EthernetProps *) &headers->props;
|
|
FILL_ADVANCE(hdr, ethProps->destAddr, ETH_HW_ADDR_LEN);
|
|
FILL_ADVANCE(hdr, ethProps->sourceAddr, ETH_HW_ADDR_LEN);
|
|
FILL_WORD_H2N_ADVANCE(hdr, ethProps->length_type);
|
|
}
|