Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite IPAddress without union #8829

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions cores/esp32/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,62 @@
#include <Arduino.h>
#include <IPAddress.h>
#include <Print.h>
#include <algorithm>

IPAddress::IPAddress()
{
_address.dword = 0;
}
// IPAddress::IPAddress()
// {
// _address.dword = 0;
// }

IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
{
_address.bytes[0] = first_octet;
_address.bytes[1] = second_octet;
_address.bytes[2] = third_octet;
_address.bytes[3] = fourth_octet;
}
: _address({first_octet,
second_octet,
third_octet,
fourth_octet})
{}

IPAddress::IPAddress(uint32_t address)
{
_address.dword = address;
uint32_t& addressRef = reinterpret_cast<uint32_t&>(_address.front());
addressRef = address;
}

IPAddress::IPAddress(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
}
IPAddress::IPAddress(const uint8_t *address) : _address({address[0], address[1], address[2], address[3]})
{}

IPAddress& IPAddress::operator=(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
std::copy(address, address + _address.size(), _address.begin());
return *this;
}

IPAddress& IPAddress::operator=(uint32_t address)
{
_address.dword = address;
uint32_t& addressRef = reinterpret_cast<uint32_t&>(_address.front());
addressRef = address;
return *this;
}

bool IPAddress::operator==(const uint8_t* addr) const
{
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
return std::equal(_address.begin(), _address.end(), addr);
}

size_t IPAddress::printTo(Print& p) const
{
size_t n = 0;
for(int i = 0; i < 3; i++) {
n += p.print(_address.bytes[i], DEC);
n += p.print(_address[i], DEC);
n += p.print('.');
}
n += p.print(_address.bytes[3], DEC);
n += p.print(_address[3], DEC);
return n;
}

String IPAddress::toString() const
{
char szRet[16];
sprintf(szRet,"%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
sprintf(szRet,"%u.%u.%u.%u", _address[0], _address[1], _address[2], _address[3]);
return String(szRet);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ bool IPAddress::fromString(const char *address)
// Too much dots (there must be 3 dots)
return false;
}
_address.bytes[dots++] = acc;
_address[dots++] = acc;
acc = 0;
}
else
Expand All @@ -117,7 +117,7 @@ bool IPAddress::fromString(const char *address)
// Too few dots (there must be 3 dots)
return false;
}
_address.bytes[3] = acc;
_address[3] = acc;
return true;
}

Expand Down
19 changes: 9 additions & 10 deletions cores/esp32/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@
#include <stdint.h>
#include <WString.h>
#include <Printable.h>
#include <array>

// A class to make it easier to handle and pass around IP addresses

class IPAddress: public Printable
{
private:
union {
uint8_t bytes[4]; // IPv4 address
uint32_t dword;
} _address;
alignas(alignof(uint32_t)) std::array<uint8_t,4> _address{}; // IPv4 address
Copy link
Contributor

@TD-er TD-er Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does std::array initialize with a zero value?
You did remove the default constructor, so it must be set to zero here.

Edit:
Yep, the {} does indeed initialize all elements.
See: https://en.cppreference.com/w/cpp/container/array

I was a bit worried it might only initialize the array and not just its elements as that was indeed an issue with C++11 requiring double braces.


// Access the raw byte array containing the address. Because this returns a pointer
// to the internal structure rather than a copy of the address this function should only
// be used when you know that the usage of the returned uint8_t* will be transient and not
// stored.
uint8_t* raw_address()
{
return _address.bytes;
return _address.data();
}

public:
// Constructors
IPAddress();
IPAddress() = default;
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
IPAddress(uint32_t address);
IPAddress(const uint8_t *address);
Expand All @@ -54,26 +52,27 @@ class IPAddress: public Printable
bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }


// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const
{
return _address.dword;
return reinterpret_cast<const uint32_t&>(_address.front());
}
bool operator==(const IPAddress& addr) const
{
return _address.dword == addr._address.dword;
return _address == addr._address;
}
bool operator==(const uint8_t* addr) const;

// Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const
{
return _address.bytes[index];
return _address[index];
}
uint8_t& operator[](int index)
{
return _address.bytes[index];
return _address[index];
}

// Overloaded copy operators to allow initialisation of IPAddress objects from other types
Expand Down