From 0856254a80a9eaa99735f4cbcc5c183e55a5c64c Mon Sep 17 00:00:00 2001 From: David McCurley <44048235+mrengineer7777@users.noreply.github.com> Date: Fri, 29 Apr 2022 17:36:03 -0500 Subject: [PATCH 01/12] Added new classes MacAddress and MacAddress8 In the same style as class IPAddress. Based on Apache License. --- CMakeLists.txt | 2 + cores/esp32/MacAddress.cpp | 95 +++++++++++++++++++++++++++++++++++++ cores/esp32/MacAddress.h | 53 +++++++++++++++++++++ cores/esp32/MacAddress8.cpp | 95 +++++++++++++++++++++++++++++++++++++ cores/esp32/MacAddress8.h | 50 +++++++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 cores/esp32/MacAddress.cpp create mode 100644 cores/esp32/MacAddress.h create mode 100644 cores/esp32/MacAddress8.cpp create mode 100644 cores/esp32/MacAddress8.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 851d1488eca..6935a1ad652 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,8 @@ set(CORE_SRCS cores/esp32/libb64/cdecode.c cores/esp32/libb64/cencode.c cores/esp32/main.cpp + cores/esp32/MacAddress.cpp + cores/esp32/MacAddress8.cpp cores/esp32/MD5Builder.cpp cores/esp32/Print.cpp cores/esp32/stdlib_noniso.c diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp new file mode 100644 index 00000000000..a49e8a31f95 --- /dev/null +++ b/cores/esp32/MacAddress.cpp @@ -0,0 +1,95 @@ +#include +#include + +//Default constructor, blank mac address. +MacAddress::MacAddress() { + _mac.val = 0; +} + +MacAddress::MacAddress(uint64_t mac) { + _mac.val = mac; +} + +MacAddress::MacAddress(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); +} + +//Parse user entered string into MAC address +bool MacAddress::fromCStr(const char *buf) { + char cs[18]; + char *token; + char *next; //Unused but required + int i; + + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. + + for(i=0; i + +// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses. +class MacAddress { +private: + union { + struct { + uint8_t align[2]; + uint8_t bytes[6]; + }; + uint64_t val; + } _mac; + +public: + MacAddress(); + MacAddress(uint64_t mac); + MacAddress(const uint8_t *macbytearray); + virtual ~MacAddress() {} + bool fromCStr(const char *buf); + bool fromString(const String &macstr); + void toBytes(uint8_t *buf); + int toCStr(char *buf); + String toString() const; + uint64_t Value(); + + operator uint64_t() const; + MacAddress& operator=(const uint8_t *mac); + MacAddress& operator=(uint64_t macval); + bool operator==(const uint8_t *mac) const; + bool operator==(const MacAddress& mac2) const; +}; + +#endif diff --git a/cores/esp32/MacAddress8.cpp b/cores/esp32/MacAddress8.cpp new file mode 100644 index 00000000000..7d953d8dad7 --- /dev/null +++ b/cores/esp32/MacAddress8.cpp @@ -0,0 +1,95 @@ +#include +#include + +//Default constructor, blank mac address. +MacAddress8::MacAddress8() { + _mac.val = 0; +} + +MacAddress8::MacAddress8(uint64_t mac) { + _mac.val = mac; +} + +MacAddress8::MacAddress8(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); +} + +//Parse user entered string into MAC address +bool MacAddress8::fromCStr(const char *buf) { + char cs[24]; + char *token; + char *next; //Unused but required + int i; + + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. + + for(i=0; i + +// A class to make it easier to handle and pass around 8-byte EUI-64(used for IEEE 802.15.4) addresses. See . +class MacAddress8 { +private: + union { + uint8_t bytes[8]; + uint64_t val; + } _mac; + +public: + MacAddress8(); + MacAddress8(uint64_t mac); + MacAddress8(const uint8_t *macbytearray); + virtual ~MacAddress8() {} + bool fromCStr(const char *buf); + bool fromString(const String &macstr); + void toBytes(uint8_t *buf); + int toCStr(char *buf); + String toString() const; + uint64_t Value(); + + operator uint64_t() const; + MacAddress8& operator=(const uint8_t *mac); + MacAddress8& operator=(uint64_t macval); + bool operator==(const uint8_t *mac) const; + bool operator==(const MacAddress8& mac2) const; +}; + +#endif From 0e84fad0fab30b5e0c65fd90bd48e9402b5392ff Mon Sep 17 00:00:00 2001 From: David McCurley Date: Tue, 3 May 2022 13:24:06 -0500 Subject: [PATCH 02/12] Update MacAddress8.h --- cores/esp32/MacAddress8.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/MacAddress8.h b/cores/esp32/MacAddress8.h index c0cee04ffe4..c8284a3abd5 100644 --- a/cores/esp32/MacAddress8.h +++ b/cores/esp32/MacAddress8.h @@ -1,5 +1,5 @@ //----------------------------------------------------------------------------- -// MacAddress8.h - class to make it easier to handle BSSID and MAC addresses. +// MacAddress8.h - class to make it easier to handle 8-byte EUI-64 addresses. // // Copyright 2022 David McCurley // Licensed under the Apache License, Version 2.0 (the "License"). From 8b6fdba431d1ecd78ac902059f3a852ec76279e6 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:14:59 -0300 Subject: [PATCH 03/12] Added Printable, constructor and extra operators Added a few changes to make it closer to IPAddress Class implementation. --- cores/esp32/MacAddress.h | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/cores/esp32/MacAddress.h b/cores/esp32/MacAddress.h index 00af8287bd9..8ebeb7f30dc 100644 --- a/cores/esp32/MacAddress.h +++ b/cores/esp32/MacAddress.h @@ -21,7 +21,7 @@ #include // A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses. -class MacAddress { +class MacAddress : public Printable { private: union { struct { @@ -35,6 +35,7 @@ class MacAddress { MacAddress(); MacAddress(uint64_t mac); MacAddress(const uint8_t *macbytearray); + MAcAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6); virtual ~MacAddress() {} bool fromCStr(const char *buf); bool fromString(const String &macstr); @@ -43,11 +44,43 @@ class MacAddress { String toString() const; uint64_t Value(); - operator uint64_t() const; + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _mac.bytes[index]; + } + uint8_t& operator[](int index) + { + return _mac.bytes[index]; + } + MacAddress& operator=(const uint8_t *mac); MacAddress& operator=(uint64_t macval); bool operator==(const uint8_t *mac) const; bool operator==(const MacAddress& mac2) const; + + operator uint64_t() const + { + return _mac.val; + } + operator const uint8_t*() const + { + return _mac.bytes; + } + operator const uint64_t*() const + { + return &_mac.val; + } + + virtual size_t printTo(Print& p) const; + + // future use in Arduino Networking + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; }; #endif From 2ed58053090f9c06f604d9110eaf426e72b697fa Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:27:05 -0300 Subject: [PATCH 04/12] Added construtor and Printable Makes it closer to IPAddress Class implementation --- cores/esp32/MacAddress.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp index a49e8a31f95..368969d8aca 100644 --- a/cores/esp32/MacAddress.cpp +++ b/cores/esp32/MacAddress.cpp @@ -14,6 +14,15 @@ MacAddress::MacAddress(const uint8_t *macbytearray) { memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); } +MacAddress::MacAddress((uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) { + _mac.bytes[0] = b1; + _mac.bytes[1] = b2; + _mac.bytes[2] = b3; + _mac.bytes[3] = b4; + _mac.bytes[4] = b5; + _mac.bytes[5] = b6; +} + //Parse user entered string into MAC address bool MacAddress::fromCStr(const char *buf) { char cs[18]; @@ -63,12 +72,6 @@ uint64_t MacAddress::Value() { return _mac.val; } -//Implicit conversion object to number [same as .Value()] -MacAddress::operator uint64_t() const -{ - return _mac.val; -} - //Overloaded copy operators to allow initialisation of MacAddress objects from other types MacAddress& MacAddress::operator=(const uint8_t *mac) { @@ -93,3 +96,16 @@ bool MacAddress::operator==(const MacAddress& mac2) const { return _mac.val == mac2._mac.val; } + +size_t MacAddress::printTo(Print& p) const +{ + size_t n = 0; + for(int i = 0; i < 6; i++) { + if(i){ + n += p.print(':'); + } + n += p.printf("%02x", _mac.bytes[i]); + } + return n; +} + From 37d7e199315ad20c81ef9f9e919127d6e013b688 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:34:35 -0300 Subject: [PATCH 05/12] Fixes include Printable --- cores/esp32/MacAddress.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/esp32/MacAddress.h b/cores/esp32/MacAddress.h index 8ebeb7f30dc..8b24505666d 100644 --- a/cores/esp32/MacAddress.h +++ b/cores/esp32/MacAddress.h @@ -18,7 +18,9 @@ #ifndef MacAddress_h #define MacAddress_h +#include #include +#include // A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses. class MacAddress : public Printable { From 45da7ea4ee3f91a645fff042295d37abc4abdf9c Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:40:39 -0300 Subject: [PATCH 06/12] Update MacAddress.cpp --- cores/esp32/MacAddress.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp index 368969d8aca..492462332a5 100644 --- a/cores/esp32/MacAddress.cpp +++ b/cores/esp32/MacAddress.cpp @@ -14,7 +14,7 @@ MacAddress::MacAddress(const uint8_t *macbytearray) { memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); } -MacAddress::MacAddress((uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) { +MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) { _mac.bytes[0] = b1; _mac.bytes[1] = b2; _mac.bytes[2] = b3; From d9cb093ebc8e1f6ec0b0e3b4c5040b6eb9b6cda4 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:41:20 -0300 Subject: [PATCH 07/12] Update MacAddress.h --- cores/esp32/MacAddress.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/MacAddress.h b/cores/esp32/MacAddress.h index 8b24505666d..c4a86d36653 100644 --- a/cores/esp32/MacAddress.h +++ b/cores/esp32/MacAddress.h @@ -37,7 +37,7 @@ class MacAddress : public Printable { MacAddress(); MacAddress(uint64_t mac); MacAddress(const uint8_t *macbytearray); - MAcAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6); + MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6); virtual ~MacAddress() {} bool fromCStr(const char *buf); bool fromString(const String &macstr); From 9e03911b84c8915d41f93146ff0d400c50c923db Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 09:46:23 -0300 Subject: [PATCH 08/12] Update MacAddress.cpp --- cores/esp32/MacAddress.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp index 492462332a5..98fe27e108f 100644 --- a/cores/esp32/MacAddress.cpp +++ b/cores/esp32/MacAddress.cpp @@ -1,5 +1,6 @@ #include #include +#include //Default constructor, blank mac address. MacAddress::MacAddress() { From 450c4dd19f038868d3412745dd7ad8ee19ad843e Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 10:24:05 -0300 Subject: [PATCH 09/12] Added Printable --- cores/esp32/MacAddress8.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cores/esp32/MacAddress8.cpp b/cores/esp32/MacAddress8.cpp index 7d953d8dad7..30510118dcf 100644 --- a/cores/esp32/MacAddress8.cpp +++ b/cores/esp32/MacAddress8.cpp @@ -1,5 +1,6 @@ #include #include +#include //Default constructor, blank mac address. MacAddress8::MacAddress8() { @@ -93,3 +94,15 @@ bool MacAddress8::operator==(const MacAddress8& mac2) const { return _mac.val == mac2._mac.val; } + +size_t MacAddress8::printTo(Print& p) const +{ + size_t n = 0; + for(int i = 0; i < 8; i++) { + if(i){ + n += p.print(':'); + } + n += p.printf("%02x", _mac.bytes[i]); + } + return n; +} From dca7e225c2475b2ae405d34725976f946fc1da89 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 5 May 2022 10:28:22 -0300 Subject: [PATCH 10/12] Added Printble and some more operators --- cores/esp32/MacAddress8.h | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cores/esp32/MacAddress8.h b/cores/esp32/MacAddress8.h index c8284a3abd5..c7adb5288b6 100644 --- a/cores/esp32/MacAddress8.h +++ b/cores/esp32/MacAddress8.h @@ -18,10 +18,12 @@ #ifndef MacAddress8_h #define MacAddress8_h +#include #include +#include // A class to make it easier to handle and pass around 8-byte EUI-64(used for IEEE 802.15.4) addresses. See . -class MacAddress8 { +class MacAddress8 : public Printable { private: union { uint8_t bytes[8]; @@ -45,6 +47,35 @@ class MacAddress8 { MacAddress8& operator=(uint64_t macval); bool operator==(const uint8_t *mac) const; bool operator==(const MacAddress8& mac2) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _mac.bytes[index]; + } + uint8_t& operator[](int index) + { + return _mac.bytes[index]; + } + + operator const uint8_t*() const + { + return _mac.bytes; + } + operator const uint64_t*() const + { + return &_mac.val; + } + + virtual size_t printTo(Print& p) const; + + // future use in Arduino Networking + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; }; #endif From a6330b2ad4386b0d93c58d88a22b80f0ba44a530 Mon Sep 17 00:00:00 2001 From: David McCurley Date: Fri, 6 May 2022 13:10:03 -0500 Subject: [PATCH 11/12] Cleanup and bounds checking Moved implementation details .h->.cpp. Added bounds checking on index operators. Added constructor to MacAddress8 to match MacAddress. --- cores/esp32/MacAddress.cpp | 60 +++++++++++++++++++++++++------- cores/esp32/MacAddress.h | 35 ++++++------------- cores/esp32/MacAddress8.cpp | 69 +++++++++++++++++++++++++++++-------- cores/esp32/MacAddress8.h | 38 +++++++------------- 4 files changed, 124 insertions(+), 78 deletions(-) diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp index 98fe27e108f..0e9716ee390 100644 --- a/cores/esp32/MacAddress.cpp +++ b/cores/esp32/MacAddress.cpp @@ -73,31 +73,55 @@ uint64_t MacAddress::Value() { return _mac.val; } -//Overloaded copy operators to allow initialisation of MacAddress objects from other types -MacAddress& MacAddress::operator=(const uint8_t *mac) -{ - memcpy(_mac.bytes, mac, sizeof(_mac.bytes)); +//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0]; +uint8_t MacAddress::operator[](int index) const { + index = EnforceIndexBounds(index); + return _mac.bytes[index]; +} + +//Allow setting individual octets of the address. e.g. ma[2] = 255; +uint8_t& MacAddress::operator[](int index) { + index = EnforceIndexBounds(index); + return _mac.bytes[index]; +} + +//Overloaded copy operator: init MacAddress object from byte array +MacAddress& MacAddress::operator=(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); return *this; } -MacAddress& MacAddress::operator=(uint64_t macval) -{ +//Overloaded copy operator: init MacAddress object from uint64_t +MacAddress& MacAddress::operator=(uint64_t macval) { _mac.val = macval; return *this; } //Compare class to byte array -bool MacAddress::operator==(const uint8_t *mac) const -{ - return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes)); +bool MacAddress::operator==(const uint8_t *macbytearray) const { + return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes)); } //Allow comparing value of two classes -bool MacAddress::operator==(const MacAddress& mac2) const -{ +bool MacAddress::operator==(const MacAddress& mac2) const { return _mac.val == mac2._mac.val; } - + +//Type converter object to uint64_t [same as .Value()] +MacAddress::operator uint64_t() const { + return _mac.val; +} + +//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma; +MacAddress::operator const uint8_t*() const { + return _mac.bytes; +} + +//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma; +MacAddress::operator const uint64_t*() const { + return &_mac.val; +} + size_t MacAddress::printTo(Print& p) const { size_t n = 0; @@ -109,4 +133,14 @@ size_t MacAddress::printTo(Print& p) const } return n; } - + +//Bounds checking +int MacAddress::EnforceIndexBounds(int i) const { + if(i < 0) { + return 0; + } + if(i >= sizeof(_mac.bytes)) { + return sizeof(_mac.bytes)-1; + } + return i; +} diff --git a/cores/esp32/MacAddress.h b/cores/esp32/MacAddress.h index c4a86d36653..de335a957a1 100644 --- a/cores/esp32/MacAddress.h +++ b/cores/esp32/MacAddress.h @@ -46,33 +46,15 @@ class MacAddress : public Printable { String toString() const; uint64_t Value(); - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const - { - return _mac.bytes[index]; - } - uint8_t& operator[](int index) - { - return _mac.bytes[index]; - } - - MacAddress& operator=(const uint8_t *mac); + uint8_t operator[](int index) const; + uint8_t& operator[](int index); + MacAddress& operator=(const uint8_t *macbytearray); MacAddress& operator=(uint64_t macval); - bool operator==(const uint8_t *mac) const; + bool operator==(const uint8_t *macbytearray) const; bool operator==(const MacAddress& mac2) const; - - operator uint64_t() const - { - return _mac.val; - } - operator const uint8_t*() const - { - return _mac.bytes; - } - operator const uint64_t*() const - { - return &_mac.val; - } + operator uint64_t() const; + operator const uint8_t*() const; + operator const uint64_t*() const; virtual size_t printTo(Print& p) const; @@ -83,6 +65,9 @@ class MacAddress : public Printable { friend class Server; friend class DhcpClass; friend class DNSClient; + +private: + int EnforceIndexBounds(int i) const; }; #endif diff --git a/cores/esp32/MacAddress8.cpp b/cores/esp32/MacAddress8.cpp index 30510118dcf..f42b32c4b0c 100644 --- a/cores/esp32/MacAddress8.cpp +++ b/cores/esp32/MacAddress8.cpp @@ -15,6 +15,17 @@ MacAddress8::MacAddress8(const uint8_t *macbytearray) { memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); } +MacAddress8::MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { + _mac.bytes[0] = b1; + _mac.bytes[1] = b2; + _mac.bytes[2] = b3; + _mac.bytes[3] = b4; + _mac.bytes[4] = b5; + _mac.bytes[5] = b6; + _mac.bytes[6] = b7; + _mac.bytes[7] = b8; +} + //Parse user entered string into MAC address bool MacAddress8::fromCStr(const char *buf) { char cs[24]; @@ -64,37 +75,54 @@ uint64_t MacAddress8::Value() { return _mac.val; } -//Implicit conversion object to number [same as .Value()] -MacAddress8::operator uint64_t() const -{ - return _mac.val; +uint8_t MacAddress8::operator[](int index) const { + index = EnforceIndexBounds(index); + return _mac.bytes[index]; } -//Overloaded copy operators to allow initialisation of MacAddress objects from other types -MacAddress8& MacAddress8::operator=(const uint8_t *mac) -{ - memcpy(_mac.bytes, mac, sizeof(_mac.bytes)); +//Allow setting individual octets of the address. e.g. ma[2] = 255; +uint8_t& MacAddress8::operator[](int index) { + index = EnforceIndexBounds(index); + return _mac.bytes[index]; +} + +//Overloaded copy operator: init MacAddress object from byte array +MacAddress8& MacAddress8::operator=(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); return *this; } -MacAddress8& MacAddress8::operator=(uint64_t macval) -{ +//Overloaded copy operator: init MacAddress object from uint64_t +MacAddress8& MacAddress8::operator=(uint64_t macval) { _mac.val = macval; return *this; } //Compare class to byte array -bool MacAddress8::operator==(const uint8_t *mac) const -{ - return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes)); +bool MacAddress8::operator==(const uint8_t *macbytearray) const { + return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes)); } //Allow comparing value of two classes -bool MacAddress8::operator==(const MacAddress8& mac2) const -{ +bool MacAddress8::operator==(const MacAddress8& mac2) const { return _mac.val == mac2._mac.val; } +//Type converter object to uint64_t [same as .Value()] +MacAddress8::operator uint64_t() const { + return _mac.val; +} + +//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma; +MacAddress8::operator const uint8_t*() const { + return _mac.bytes; +} + +//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma; +MacAddress8::operator const uint64_t*() const { + return &_mac.val; +} + size_t MacAddress8::printTo(Print& p) const { size_t n = 0; @@ -106,3 +134,14 @@ size_t MacAddress8::printTo(Print& p) const } return n; } + +//Bounds checking +int MacAddress8::EnforceIndexBounds(int i) const { + if(i < 0) { + return 0; + } + if(i >= sizeof(_mac.bytes)) { + return sizeof(_mac.bytes)-1; + } + return i; +} diff --git a/cores/esp32/MacAddress8.h b/cores/esp32/MacAddress8.h index c7adb5288b6..afb8d4a808b 100644 --- a/cores/esp32/MacAddress8.h +++ b/cores/esp32/MacAddress8.h @@ -34,39 +34,24 @@ class MacAddress8 : public Printable { MacAddress8(); MacAddress8(uint64_t mac); MacAddress8(const uint8_t *macbytearray); + MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8); virtual ~MacAddress8() {} bool fromCStr(const char *buf); bool fromString(const String &macstr); void toBytes(uint8_t *buf); - int toCStr(char *buf); + int toCStr(char *buf); String toString() const; uint64_t Value(); - operator uint64_t() const; - MacAddress8& operator=(const uint8_t *mac); + uint8_t operator[](int index) const; + uint8_t& operator[](int index); + MacAddress8& operator=(const uint8_t *macbytearray); MacAddress8& operator=(uint64_t macval); - bool operator==(const uint8_t *mac) const; + bool operator==(const uint8_t *macbytearray) const; bool operator==(const MacAddress8& mac2) const; - - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const - { - return _mac.bytes[index]; - } - uint8_t& operator[](int index) - { - return _mac.bytes[index]; - } - - operator const uint8_t*() const - { - return _mac.bytes; - } - operator const uint64_t*() const - { - return &_mac.val; - } - + operator uint64_t() const; + operator const uint8_t*() const; + operator const uint64_t*() const; virtual size_t printTo(Print& p) const; // future use in Arduino Networking @@ -75,7 +60,10 @@ class MacAddress8 : public Printable { friend class Client; friend class Server; friend class DhcpClass; - friend class DNSClient; + friend class DNSClient; + +private: + int EnforceIndexBounds(int i) const; }; #endif From d05cd87691a69da29baaaebe47c445a8a45b4f6c Mon Sep 17 00:00:00 2001 From: David McCurley Date: Fri, 6 May 2022 17:46:54 -0500 Subject: [PATCH 12/12] Fixed printTo Chars must be uppercase to match toString() and pass test --- cores/esp32/MacAddress.cpp | 2 +- cores/esp32/MacAddress8.cpp | 2 +- cores/esp32/MacAddress8.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp index 0e9716ee390..0498b1f93e6 100644 --- a/cores/esp32/MacAddress.cpp +++ b/cores/esp32/MacAddress.cpp @@ -129,7 +129,7 @@ size_t MacAddress::printTo(Print& p) const if(i){ n += p.print(':'); } - n += p.printf("%02x", _mac.bytes[i]); + n += p.printf("%02X", _mac.bytes[i]); } return n; } diff --git a/cores/esp32/MacAddress8.cpp b/cores/esp32/MacAddress8.cpp index f42b32c4b0c..7406bee8a42 100644 --- a/cores/esp32/MacAddress8.cpp +++ b/cores/esp32/MacAddress8.cpp @@ -130,7 +130,7 @@ size_t MacAddress8::printTo(Print& p) const if(i){ n += p.print(':'); } - n += p.printf("%02x", _mac.bytes[i]); + n += p.printf("%02X", _mac.bytes[i]); } return n; } diff --git a/cores/esp32/MacAddress8.h b/cores/esp32/MacAddress8.h index afb8d4a808b..360ce8c9fe7 100644 --- a/cores/esp32/MacAddress8.h +++ b/cores/esp32/MacAddress8.h @@ -52,6 +52,7 @@ class MacAddress8 : public Printable { operator uint64_t() const; operator const uint8_t*() const; operator const uint64_t*() const; + virtual size_t printTo(Print& p) const; // future use in Arduino Networking