BAN: Add types for IPv4 and MAC addresses

This commit is contained in:
Bananymous 2024-02-02 13:48:07 +02:00
parent 4b332b5d42
commit 7b287a1d5b
2 changed files with 90 additions and 0 deletions

54
BAN/include/BAN/IPv4.h Normal file
View File

@ -0,0 +1,54 @@
#pragma once
#include <BAN/Formatter.h>
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<typename F>
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);
}
}
}

36
BAN/include/BAN/MAC.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include <BAN/Formatter.h>
namespace BAN
{
struct MACAddress
{
uint8_t address[6];
};
}
namespace BAN::Formatter
{
template<typename F>
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);
}
}
}