From 7b287a1d5b22eab4b262df1bfb825604873ba68c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 2 Feb 2024 13:48:07 +0200 Subject: [PATCH] BAN: Add types for IPv4 and MAC addresses --- BAN/include/BAN/IPv4.h | 54 ++++++++++++++++++++++++++++++++++++++++++ BAN/include/BAN/MAC.h | 36 ++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 BAN/include/BAN/IPv4.h create mode 100644 BAN/include/BAN/MAC.h diff --git a/BAN/include/BAN/IPv4.h b/BAN/include/BAN/IPv4.h new file mode 100644 index 0000000000..5ead835b64 --- /dev/null +++ b/BAN/include/BAN/IPv4.h @@ -0,0 +1,54 @@ +#pragma once + +#include + +namespace BAN +{ + + struct IPv4Address + { + IPv4Address(uint32_t u32_address) + { + address[0] = u32_address >> 24; + address[1] = u32_address >> 16; + address[2] = u32_address >> 8; + address[3] = u32_address >> 0; + } + + constexpr uint32_t as_u32() + { + return + ((uint32_t)address[0] << 24) | + ((uint32_t)address[1] << 16) | + ((uint32_t)address[2] << 8) | + ((uint32_t)address[3] << 0); + } + + uint8_t address[4]; + }; + static_assert(sizeof(IPv4Address) == 4); + +} + +namespace BAN::Formatter +{ + + template + void print_argument(F putc, const IPv4Address& ipv4, const ValueFormat&) + { + ValueFormat format { + .base = 10, + .percision = 0, + .fill = 0, + .upper = false, + }; + + print_argument(putc, ipv4.address[0], format); + for (size_t i = 1; i < 4; i++) + { + putc('.'); + print_argument(putc, ipv4.address[i], format); + } + } + +} diff --git a/BAN/include/BAN/MAC.h b/BAN/include/BAN/MAC.h new file mode 100644 index 0000000000..1fdcc9e60b --- /dev/null +++ b/BAN/include/BAN/MAC.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +namespace BAN +{ + + struct MACAddress + { + uint8_t address[6]; + }; + +} + +namespace BAN::Formatter +{ + + template + void print_argument(F putc, const MACAddress& mac, const ValueFormat&) + { + ValueFormat format { + .base = 16, + .percision = 0, + .fill = 2, + .upper = true, + }; + + print_argument(putc, mac.address[0], format); + for (size_t i = 1; i < 6; i++) + { + putc(':'); + print_argument(putc, mac.address[i], format); + } + } + +}