From 0f3c8ca6014a5ca3d83cfb16758e2e7e2013ccc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiesner=20Andr=C3=A1s?= Date: Thu, 3 Nov 2022 16:58:17 +0100 Subject: [PATCH] Ethernet II, IPv4, UDP packet parsers added (not complete) --- Doxyfile | 2 +- global_state.c | 59 ++++++++++++++++++++++ global_state.h | 6 ++- packet_registry.c | 11 +++- packet_registry.h | 26 +++++++--- prefab/packet_parsers/ethernet_frame.c | 26 ++++++++++ prefab/packet_parsers/ethernet_frame.h | 33 ++++++++++++ prefab/packet_parsers/ipv4_packet.c | 69 ++++++++++++++++++++++++++ prefab/packet_parsers/ipv4_packet.h | 43 ++++++++++++++++ prefab/packet_parsers/packet_parsers.c | 1 + prefab/packet_parsers/packet_parsers.h | 8 +++ prefab/packet_parsers/udp_packet.c | 20 ++++++++ prefab/packet_parsers/udp_packet.h | 31 ++++++++++++ prefab/prefab.h | 6 +++ utils.c | 56 +++++++++++++++++++++ utils.h | 38 ++++++++++++++ 16 files changed, 424 insertions(+), 11 deletions(-) create mode 100644 prefab/packet_parsers/ethernet_frame.c create mode 100644 prefab/packet_parsers/ethernet_frame.h create mode 100644 prefab/packet_parsers/ipv4_packet.c create mode 100644 prefab/packet_parsers/ipv4_packet.h create mode 100644 prefab/packet_parsers/packet_parsers.c create mode 100644 prefab/packet_parsers/packet_parsers.h create mode 100644 prefab/packet_parsers/udp_packet.c create mode 100644 prefab/packet_parsers/udp_packet.h create mode 100644 prefab/prefab.h diff --git a/Doxyfile b/Doxyfile index a545e37..dd02eeb 100644 --- a/Doxyfile +++ b/Doxyfile @@ -2453,7 +2453,7 @@ UML_LIMIT_NUM_FIELDS = 10 DOT_UML_DETAILS = NO # The DOT_WRAP_THRESHOLD tag can be used to set the maximum number of characters -# to display on a single line. If the actual line length exceeds this threshold +# to display on a single line. If the actual line length_type exceeds this threshold # significantly it will wrapped across multiple lines. Some heuristics are apply # to avoid ugly line breaks. # Minimum value: 0, maximum value: 1000, default value: 17. diff --git a/global_state.c b/global_state.c index ba8de69..89b763e 100644 --- a/global_state.c +++ b/global_state.c @@ -1,8 +1,67 @@ +#include #include "global_state.h" #include "dynmem.h" +#include "prefab/prefab.h" + EthState gEthState; +static void register_packet_parsers() { + PcktClassDesc cdesc; + // Ethernet frame parser + cdesc.class = 0; + cdesc.containerClass = 0; + cdesc.procFun = parse_ethernet; + cdesc.propertySize = sizeof(EthernetProps); + packreg_add(E.pcktReg, &cdesc); + + // IPv4 packet parser + cdesc.class = ETH_IPv4_PACKET_CLASS; + cdesc.containerClass = 0; + cdesc.procFun = parse_ipv4; + cdesc.propertySize = sizeof(IPv4Props); + packreg_add(E.pcktReg, &cdesc); + + // UDP packet parser + cdesc.class = ETH_UDP_PACKET_CLASS; + cdesc.containerClass = ETH_IPv4_PACKET_CLASS; + cdesc.procFun = parse_udp; + cdesc.propertySize = sizeof(UDPProps); + packreg_add(E.pcktReg, &cdesc); +} + void ethlib_init() { dynmem_init(); // initialize dynamic memory subsystem + E.pcktReg = packreg_new(); // create new packet registry + register_packet_parsers(); // register packet parsers +} + +void process_raw_packet(const uint8_t * data, uint32_t size) { + uint16_t ownClass = 0, containerClass = 0; // Ethernet... + uint16_t offset = 0; + PcktProps * props = NULL; + do { + // get packet descriptor + const PcktClassDesc * cdesc = packreg_get_by_class(E.pcktReg, ownClass, containerClass); + if (cdesc == NULL) { + break; + } + + // allocate property object + props = (PcktProps *) dynmem_alloc(cdesc->propertySize); + + // call parsing function + cdesc->procFun(data + offset, size - offset, props); + uint16_t containedClass = props->containedPacketClass; + if (containedClass != 0) { + containerClass = ownClass; + offset += props->headerSize; + dynmem_free(props); + } + ownClass = containedClass; + } while (ownClass != 0); + + // TODO process innermost packet + + return; } \ No newline at end of file diff --git a/global_state.h b/global_state.h index 2b9e1c8..f9475bf 100644 --- a/global_state.h +++ b/global_state.h @@ -8,8 +8,8 @@ * Global EtherLib state. */ typedef struct { - MP * mp; ///< Memory pool for synamic allocations. - PcktRegistry pcktReg; ///< Packet registry. + MP * mp; ///< Memory pool for dynamic allocations + PcktRegistry * pcktReg; ///< Packet registry } EthState; extern EthState gEthState; @@ -20,4 +20,6 @@ extern EthState gEthState; */ void ethlib_init(); +void process_raw_packet(const uint8_t * data, uint32_t size); + #endif //ETHERLIB_GLOBAL_STATE_H diff --git a/packet_registry.c b/packet_registry.c index 23877fb..4267d93 100644 --- a/packet_registry.c +++ b/packet_registry.c @@ -34,6 +34,13 @@ void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc) { packReg->entCnt++; } -void packreg_get_by_class(uint16_t class) { - +const PcktClassDesc * packreg_get_by_class(const PcktRegistry * packReg, uint16_t ownClass, uint16_t containerClass) { + uint32_t i; + for (i = 0; i < packReg->entCnt; i++) { + const PcktClassDesc * cdesc = packReg->ents + i; + if ((cdesc->class == ownClass) && ((cdesc->containerClass == containerClass) || (containerClass == 0))) { + return cdesc; + } + } + return NULL; } \ No newline at end of file diff --git a/packet_registry.h b/packet_registry.h index a328617..05e0cf2 100644 --- a/packet_registry.h +++ b/packet_registry.h @@ -17,24 +17,29 @@ typedef uint8_t bool8_t; * to be aligned to 32-bit boundary and so must be guaranteed by extending * as well. (The size of the packet is divisible by 4.) */ + +#define PcktPropsHeader \ + uint16_t headerSize; /*< Header size in bytes */ \ + uint16_t containedPacketClass; /*< ID of contained packet. Zero if no packet contained. */ \ + bool8_t validityOK; /*< Indicates that checksum is OK. */ \ + + typedef struct { - uint16_t headerSize; ///< Header size in bytes - uint16_t containedPacketClass; ///< ID of contained packet. Zero if no packet contained. - bool8_t checksumOK; ///< Indicates that checksum is OK. + PcktPropsHeader } PcktProps; /** * @typedef int (*PcktProcFn)(const uint8_t *pHdr, uint32_t size, uint8_t *pPcktProps); * @brief Pckt processing function template. - * @param pHdr: pointer to the packet header + * @param hdr: pointer to the packet header * @param size: remaining packet size counted from pHdr - * @param pPcktProps: pointer to existing structure to store + * @param props: pointer to existing structure to store * packet properties to (e.g. source address, port etc.) * @return packet class of contained packet, or -1 on failure or * 0 if no more standard packet contained (e.g. UDP packet contains * user data) */ -typedef int (*PcktProcFn)(const uint8_t *pHdr, uint32_t size, uint8_t *pProps); +typedef int (*PcktProcFn)(const uint8_t *hdr, uint32_t size, PcktProps *props); /** * @struct PcktClassDesc @@ -45,6 +50,7 @@ typedef struct { uint16_t class; ///< Type identification of the packet 'class' (unique!) uint16_t containerClass; ///< Type of container packet packet (e.g.: IPv4 in case of UDP) PcktProcFn procFun; ///< Pckt processing function + uint16_t propertySize; ///< Size of property structure } PcktClassDesc; /** @@ -70,5 +76,13 @@ PcktRegistry * packreg_new(); */ void packreg_add(PcktRegistry * packReg, const PcktClassDesc * classDesc); +/** + * Get packet descriptor by own class and container class. + * @param packReg pointer to Packet Registry object + * @param ownClass packet's own class + * @param containerClass container packet's class (if 0, then search by container class is not considered) + * @return pointer to descriptor on success or NULL on failure + */ +const PcktClassDesc * packreg_get_by_class(const PcktRegistry * packReg, uint16_t ownClass, uint16_t containerClass); #endif //ETHERLIB_PACKET_REGISTRY_H \ No newline at end of file diff --git a/prefab/packet_parsers/ethernet_frame.c b/prefab/packet_parsers/ethernet_frame.c new file mode 100644 index 0000000..ef5f7e1 --- /dev/null +++ b/prefab/packet_parsers/ethernet_frame.c @@ -0,0 +1,26 @@ +// +// Created by epagris on 2022.11.03.. +// + +#include +#include +#include "ethernet_frame.h" +#include "etherlib/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; + COPY_ADVANCE(frameProps->destAddr, hdr, ETH_HW_ADDR_LEN); // copy destination address + COPY_ADVANCE(frameProps->sourceAddr, hdr, ETH_HW_ADDR_LEN); // copy destination address + COPY_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; +} \ No newline at end of file diff --git a/prefab/packet_parsers/ethernet_frame.h b/prefab/packet_parsers/ethernet_frame.h new file mode 100644 index 0000000..2e264b7 --- /dev/null +++ b/prefab/packet_parsers/ethernet_frame.h @@ -0,0 +1,33 @@ +// +// Created by epagris on 2022.11.03.. +// + +#ifndef ETHERLIB_ETHERNET_FRAME_H +#define ETHERLIB_ETHERNET_FRAME_H + +#include +#include "etherlib/packet_registry.h" + +#define ETH_HW_ADDR_LEN (6) + +/** + * @struct EthernetProps + * Ethernet frame properties + */ +typedef struct { + PcktPropsHeader + uint8_t destAddr[ETH_HW_ADDR_LEN]; ///< destination address + uint8_t sourceAddr[ETH_HW_ADDR_LEN]; ///< source address + uint16_t length_type; /// < frame length_type +} EthernetProps; + +/** + * Parse raw Ethernet frames. + * @param hdr pointer to the Ethernet packet header + * @param size total frame size + * @param props 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, PcktProps * props); + +#endif //ETHERLIB_ETHERNET_FRAME_H diff --git a/prefab/packet_parsers/ipv4_packet.c b/prefab/packet_parsers/ipv4_packet.c new file mode 100644 index 0000000..5e6c8b6 --- /dev/null +++ b/prefab/packet_parsers/ipv4_packet.c @@ -0,0 +1,69 @@ +// +// Created by epagris on 2022.11.03.. +// + +#include +#include "ipv4_packet.h" +#include "etherlib/utils.h" + +static uint16_t ip_checksum(const IPv4Props * ipProps) { + // sum fields + uint32_t sum = 0; + const uint16_t * pField = (const uint16_t *) &ipProps; + for (uint8_t i = 0; i < ipProps->IHL * 2; i++) { + if (i != 5) { // 6. 16-bit long field is the checksum itself, do not count int + sum += pField[i]; + } + } + + // 16-31 bit carry + uint16_t carry = sum >> 16; + + // add carry + sum += carry; + + // if a new carry bit arisen, then + // add to the sum + sum = (sum & 0xFFFF) + (sum >> 16); + + // invert result + uint16_t sum16 = sum ^ 0xFFFF; + return sum16; +} + +#define ETH_IP_HEADER_LENGTH (20) + +static bool check_ipv4_validity(const IPv4Props * ipProps) { + bool valid = + ipProps->Version != 4 && + ipProps->IHL == ETH_IP_HEADER_LENGTH && + ipProps->HeaderChecksum == ip_checksum(ipProps); + return valid; +} + +int parse_ipv4(const uint8_t *hdr, uint32_t size, PcktProps * props) { + IPv4Props * ipProps = (IPv4Props *)props; + uint8_t version_length; + COPY_ADVANCE(&version_length, hdr, 1); + ipProps->Version = (version_length) >> 4; + ipProps->IHL = (version_length) & 0x0F; + COPY_ADVANCE(&ipProps->DSF, hdr, 1); + COPY_WORD_H2N_ADVANCE(&ipProps->TotalLength, hdr); + COPY_WORD_H2N_ADVANCE(&ipProps->Identification, hdr); + uint16_t flags_fragOffset; + COPY_WORD_H2N_ADVANCE(&flags_fragOffset, hdr); + ipProps->Flags = (flags_fragOffset >> 13) & 0x07; + ipProps->FragmentOffset = (flags_fragOffset & ~(0x07 << 13)); + COPY_ADVANCE(&ipProps->TTL, hdr, 1); + COPY_ADVANCE(&ipProps->Protocol, hdr, 1); + COPY_WORD_H2N_ADVANCE(&ipProps->HeaderChecksum, hdr); + COPY_DWORD_H2N_ADVANCE(&ipProps->SourceIPAddr, hdr); + COPY_DWORD_H2N_ADVANCE(&ipProps->DestIPAddr, hdr); + + // fill-in common packet header fields + ipProps->validityOK = check_ipv4_validity(ipProps); + ipProps->containedPacketClass = ipProps->Protocol; + ipProps->headerSize = ETH_IP_HEADER_LENGTH; + + return ipProps->validityOK ? ipProps->Protocol : -1; +} \ No newline at end of file diff --git a/prefab/packet_parsers/ipv4_packet.h b/prefab/packet_parsers/ipv4_packet.h new file mode 100644 index 0000000..a84ca71 --- /dev/null +++ b/prefab/packet_parsers/ipv4_packet.h @@ -0,0 +1,43 @@ +#ifndef ETHERLIB_IPV4_PACKET_H +#define ETHERLIB_IPV4_PACKET_H + +#include +#include "etherlib/packet_registry.h" + +#define ETH_IPv4_PACKET_CLASS (0x0800) + +// IPv4 fejlécet leíró struktúra +typedef struct { + PcktPropsHeader + + // 1. 32-bit + uint8_t IHL; ///< header size + uint8_t Version; ///< version (always 4) + uint8_t DSF; ///< service type + uint16_t TotalLength; ///< total packet length + + // 2. 32-bit + uint16_t Identification; ///< identification + uint8_t Flags; ///< packet flags + uint16_t FragmentOffset; ///< packet fragmentation offset + + // 3. 32-bit + uint8_t TTL; // Time-To-Live + uint8_t Protocol; ///< Protocol + uint16_t HeaderChecksum; ///< header IP-checksum + + // 4. 32-bit + uint32_t SourceIPAddr; ///< source IP-address + uint32_t DestIPAddr; ///< destination IP-address +} IPv4Props; + +/** + * Parse raw IPv4 packets. + * @param hdr pointer to the IPv4 packet header + * @param size total packet size + * @param props pointer to property storage + * @return Protocol on success or -1 on failure + */ +int parse_ipv4(const uint8_t *hdr, uint32_t size, PcktProps * props); + +#endif //ETHERLIB_IPV4_PACKET_H diff --git a/prefab/packet_parsers/packet_parsers.c b/prefab/packet_parsers/packet_parsers.c new file mode 100644 index 0000000..7291e4d --- /dev/null +++ b/prefab/packet_parsers/packet_parsers.c @@ -0,0 +1 @@ +#include "packet_parsers.h" diff --git a/prefab/packet_parsers/packet_parsers.h b/prefab/packet_parsers/packet_parsers.h new file mode 100644 index 0000000..942d541 --- /dev/null +++ b/prefab/packet_parsers/packet_parsers.h @@ -0,0 +1,8 @@ +#ifndef ETHERLIB_PACKET_PARSERS_H +#define ETHERLIB_PACKET_PARSERS_H + +#include "ethernet_frame.h" +#include "ipv4_packet.h" +#include "udp_packet.h" + +#endif //ETHERLIB_PACKET_PARSERS_H diff --git a/prefab/packet_parsers/udp_packet.c b/prefab/packet_parsers/udp_packet.c new file mode 100644 index 0000000..9cf7d77 --- /dev/null +++ b/prefab/packet_parsers/udp_packet.c @@ -0,0 +1,20 @@ +#include +#include "udp_packet.h" +#include "etherlib/utils.h" + +#define ETH_UDP_HEADER_SIZE (8) + +int parse_udp(const uint8_t *hdr, uint32_t size, PcktProps * props) { + UDPProps * udpProps = (UDPProps *)props; + COPY_WORD_H2N_ADVANCE(&udpProps->SourcePort, hdr); + COPY_WORD_H2N_ADVANCE(&udpProps->DestinationPort, hdr); + COPY_WORD_H2N_ADVANCE(&udpProps->Length, hdr); + COPY_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; +} diff --git a/prefab/packet_parsers/udp_packet.h b/prefab/packet_parsers/udp_packet.h new file mode 100644 index 0000000..bcab46f --- /dev/null +++ b/prefab/packet_parsers/udp_packet.h @@ -0,0 +1,31 @@ +#ifndef ETHERLIB_UDP_PACKET_H +#define ETHERLIB_UDP_PACKET_H + +#include +#include "etherlib/packet_registry.h" + +#define ETH_UDP_PACKET_CLASS (17) + +/** + * @struct UDPProps + * UDP packet properties. + */ +typedef struct { + PcktPropsHeader + + uint16_t SourcePort; ///> source port + uint16_t DestinationPort; ///> destination port + uint16_t Length; ///> packet size + uint16_t Checksum; ///> UDP checksum +} UDPProps; + +/** + * Parse raw UDP packets. + * @param hdr pointer to the UDP packet header + * @param size total packet size + * @param props pointer to property storage + * @return 0 on success or -1 on failure + */ +int parse_udp(const uint8_t *hdr, uint32_t size, PcktProps * props); + +#endif //ETHERLIB_UDP_PACKET_H diff --git a/prefab/prefab.h b/prefab/prefab.h new file mode 100644 index 0000000..66a476c --- /dev/null +++ b/prefab/prefab.h @@ -0,0 +1,6 @@ +#ifndef ETHERLIB_PREFAB_H +#define ETHERLIB_PREFAB_H + +#include "packet_parsers/packet_parsers.h" + +#endif //ETHERLIB_PREFAB_H diff --git a/utils.c b/utils.c index c5de145..402b911 100644 --- a/utils.c +++ b/utils.c @@ -3,3 +3,59 @@ // #include "utils.h" + +static uint32_t crc32table[] = { /* CRC polynomial */ + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +uint32_t crc32(const uint8_t * data, uint32_t size) { + uint32_t checksum = ~0; + uint32_t i; + for (i = 0; i < size; i++) { + checksum = (checksum >> 8) ^ crc32table[checksum ^ data[i]]; + } + checksum = ~checksum; + return checksum; +} \ No newline at end of file diff --git a/utils.h b/utils.h index 7050170..1c6ed93 100644 --- a/utils.h +++ b/utils.h @@ -6,6 +6,30 @@ #define ETHERLIB_UTILS_H #include +#include +#include + +#ifndef htonl +#define htonl(a) \ + ((((a) >> 24) & 0x000000ff) | \ + (((a) >> 8) & 0x0000ff00) | \ + (((a) << 8) & 0x00ff0000) | \ + (((a) << 24) & 0xff000000)) +#endif + +#ifndef ntohl +#define ntohl(a) htonl((a)) +#endif + +#ifndef htons +#define htons(a) \ + ((((a) >> 8) & 0x00ff) | \ + (((a) << 8) & 0xff00)) +#endif + +#ifndef ntohs +#define ntohs(a) htons((a)) +#endif #define ERROR(...) printf(__VA_ARGS__) #define INFO(...) printf(__VA_ARGS__) @@ -15,4 +39,18 @@ #define ALIGN(p,t) (((size_t)(p) + (sizeof(t) - 1)) & ~(sizeof(t) - 1)) +#define COPY_ADVANCE(dst,src,n) memcpy((dst), (src), n), src += n +#define COPY_WORD_H2N_ADVANCE(dst,w) { uint16_t u; memcpy(&u, w, 2); u = htons(u); memcpy((dst), &u, 2); w += 2; } +#define COPY_DWORD_H2N_ADVANCE(dst,dw) { uint32_t du; memcpy(&du, dw, 4); du = htonl(du); memcpy((dst), &du, 4); dw += 4; } + +// ------------------ + +/** + * Compute CRC-32 checksum. + * @param data Data from which the checksum is being calculated. + * @param size Size of data byte array. + * @return checksum + */ +uint32_t crc32(const uint8_t * data, uint32_t size); + #endif //ETHERLIB_UTILS_H