- utils: atoip(), lefttrim() - ARP: bunch of bugfixes - TCP, FTP: WIP - DHCP: mutex; query the FSM state
112 lines
4.0 KiB
C
112 lines
4.0 KiB
C
//
|
|
// Created by epagris on 2022.10.31..
|
|
//
|
|
|
|
#ifndef ETHERLIB_UTILS_H
|
|
#define ETHERLIB_UTILS_H
|
|
|
|
#include <etherlib_options.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <memory.h>
|
|
|
|
#include "prefab/packet_parsers/ipv4_types.h"
|
|
|
|
#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(...) MSG(__VA_ARGS__)
|
|
#define WARNING(...) MSG(__VA_ARGS__)
|
|
#define INFO(...) MSG(__VA_ARGS__)
|
|
|
|
#define IPv4(a,b,c,d) ((a) | (b << 8) | (c << 16) | (d << 24))
|
|
#define EXPLODE_IPv4(ip) (ip & 0xFF), ((ip >> 8) & 0xFF), ((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)
|
|
#define PRINT_IPv4(ip) MSG("%u.%u.%u.%u", (ip & 0xFF), ((ip >> 8) & 0xFF), ((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF))
|
|
#define PRINT_HWADDR(hwaddr) MSG("%02x:%02x:%02x:%02x:%02x:%02x", (hwaddr)[0], (hwaddr)[1], (hwaddr)[2], (hwaddr)[3], (hwaddr)[4], (hwaddr)[5])
|
|
|
|
#define ASSERT_BAD_ALIGN(p) if ((size_t)(p) & 0b11) ERROR("Bad memory alignment in function '%s' in file '%s' on line %d!\n", __func__, __FILE__, __LINE__)
|
|
#define ASSERT_NULL(p) if ((p) == NULL) ERROR("NULL in function '%s' in file '%s' on line %d!\n", __func__, __FILE__, __LINE__)
|
|
|
|
#define ALIGN(p,t) (((size_t)(p) + (sizeof(t) - 1)) & ~(sizeof(t) - 1))
|
|
#define FLOOR_TO_4(x) ((x) & ~0b11)
|
|
#define CEIL_TO_4(x) ((((x) >> 2) + (((x) & 0b11) ? 1 : 0)) << 2)
|
|
|
|
#define FETCH_ADVANCE(dst,src,n) memcpy((dst), (src), n), (src) += n
|
|
#define FILL_ADVANCE(dst,src,n) memcpy((dst), (src), n), (dst) += n
|
|
#define FETCH_BYTE_ADVANCE(dst,src) FETCH_ADVANCE(dst,src,1)
|
|
#define FILL_BYTE_ADVANCE(dst,src) FILL_ADVANCE(dst,src,1)
|
|
#define FETCH_WORD_H2N_ADVANCE(dst,w) { uint16_t u; memcpy(&u, w, 2); u = htons(u); memcpy((dst), &u, 2); (w) += 2; }
|
|
#define FETCH_WORD_ADVANCE(dst,w) { memcpy(dst, w, 2); (w) += 2; }
|
|
#define FETCH_WORD_H2N(dst,w) { uint16_t u; memcpy(&u, w, 2); u = htons(u); memcpy((dst), &u, 2); }
|
|
#define FETCH_WORD(dst,w) memcpy(dst, w, 2)
|
|
#define FILL_WORD_ADVANCE(dst,w) { memcpy((dst), &(w), 2); (dst) += 2; }
|
|
#define FILL_WORD_H2N_ADVANCE(dst,w) { uint16_t u = htons(w); memcpy((dst), &u, 2); (dst) += 2; }
|
|
#define FILL_WORD_H2N(dst,w) { uint16_t u = htons(w); memcpy((dst), &u, 2); }
|
|
#define FETCH_DWORD_H2N_ADVANCE(dst,dw) { uint32_t du; memcpy(&du, dw, 4); du = htonl(du); memcpy((dst), &du, 4); (dw) += 4; }
|
|
#define FETCH_DWORD_H2N(dst,dw) { uint32_t du; memcpy(&du, dw, 4); du = htonl(du); memcpy((dst), &du, 4); }
|
|
#define FETCH_DWORD(dst,dw) { uint32_t du; memcpy(&du, dw, 4); memcpy((dst), &du, 4); }
|
|
#define FETCH_DWORD_ADVANCE(dst,dw) { uint32_t du; memcpy(&du, dw, 4); memcpy((dst), &du, 4); (dw) += 4; }
|
|
#define FILL_DWORD_H2N_ADVANCE(dst,dw) { uint32_t du = htonl(dw); memcpy(dst, &du, 4); (dst) += 4; }
|
|
#define FILL_DWORD_ADVANCE(dst,dw) { memcpy(dst, &(dw), 4); (dst) += 4; }
|
|
|
|
#ifndef MIN
|
|
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
#endif
|
|
#ifndef MAX
|
|
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
#endif
|
|
|
|
// ------------------
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* Compute IP/UDP/ICMP/TCP etc. 16-bit checksum.
|
|
* @param data pointer to data block to process
|
|
* @param size size of data block
|
|
* @return checksum
|
|
*/
|
|
uint16_t chksum(const uint8_t *data, uint32_t size, int swap);
|
|
|
|
/**
|
|
* Rip off whitespaces from the beginning of a string.
|
|
* @param str string to be trimmed
|
|
* @param pointer to the trimmed string
|
|
*/
|
|
const char *trimleft(const char *str);
|
|
|
|
/**
|
|
* Parse string representation of an IPv4 address and convert to binary form.
|
|
* @param str string representation of an IPv4 address
|
|
* @return IPv4 address
|
|
*/
|
|
ip4_addr atoip(const char * str);
|
|
|
|
#endif //ETHERLIB_UTILS_H
|