diff --git a/Common++/header/MacAddress.h b/Common++/header/MacAddress.h index e34c9d99a9..1cf5852e6b 100644 --- a/Common++/header/MacAddress.h +++ b/Common++/header/MacAddress.h @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include /// @file @@ -28,11 +28,17 @@ namespace pcpp /// The byte array length should be 6 (as MAC address is 6-byte long), and the remaining bytes are ignored. /// If the byte array is invalid, the constructor throws an exception. /// @param[in] addr A pointer to the byte array containing 6 bytes representing the MAC address - explicit MacAddress(const uint8_t* addr) + explicit MacAddress(const uint8_t addr[6]) { - memcpy(m_Address, addr, sizeof(m_Address)); + std::copy(addr, addr + 6, m_Address.begin()); } + /// A constructor that creates an instance of the class out of a std::array. + /// The array length should be 6 (as MAC address is 6-byte long). + /// @param [in] addr A std::array containing 6 bytes representing the MAC address + explicit MacAddress(const std::array& addr) : m_Address(addr) + {} + /// A constructor that creates an instance of the class out of a std::string. /// If the string doesn't represent a valid MAC address, the constructor throws an exception. /// @param[in] addr the string representing the MAC address in format "00:00:00:00:00:00" @@ -54,14 +60,8 @@ namespace pcpp /// @param[in] sixthOctet Represent the sixth octet in the address MacAddress(uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet) - { - m_Address[0] = firstOctet; - m_Address[1] = secondOctet; - m_Address[2] = thirdOctet; - m_Address[3] = fourthOctet; - m_Address[4] = fifthOctet; - m_Address[5] = sixthOctet; - } + : m_Address{ firstOctet, secondOctet, thirdOctet, fourthOctet, fifthOctet, sixthOctet } + {} /// A constructor that creates an instance out of the initializer list. /// The byte list length should be 6 (as MAC address is 6-byte long). @@ -69,7 +69,7 @@ namespace pcpp /// @param[in] octets An initializer list containing the values of type uint8_t representing the MAC address MacAddress(std::initializer_list octets) { - if (octets.size() != sizeof(m_Address)) + if (octets.size() != m_Address.size()) { throw std::invalid_argument("Invalid initializer list size, should be 6"); } @@ -81,7 +81,7 @@ namespace pcpp /// @return True if addresses are equal, false otherwise bool operator==(const MacAddress& other) const { - return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0; + return m_Address == other.m_Address; } /// Overload of the not-equal operator @@ -103,7 +103,7 @@ namespace pcpp throw std::invalid_argument("Invalid initializer list size, should be 6"); } - std::copy(octets.begin(), octets.end(), std::begin(m_Address)); + std::copy(octets.begin(), octets.end(), m_Address.begin()); return *this; } @@ -111,35 +111,41 @@ namespace pcpp /// @return The pointer to raw data const uint8_t* getRawData() const { - return m_Address; + return m_Address.data(); } /// Returns a std::string representation of the address /// @return A string representation of the address std::string toString() const; + /// @return A 6-byte integer representing the MAC address + std::array toByteArray() const + { + return m_Address; + } + /// Allocates a byte array of length 6 and copies address value into it. Array deallocation is user /// responsibility /// @param[in] arr A pointer to where array will be allocated void copyTo(uint8_t** arr) const { - *arr = new uint8_t[sizeof(m_Address)]; - memcpy(*arr, m_Address, sizeof(m_Address)); + *arr = new uint8_t[m_Address.size()]; + std::copy(m_Address.begin(), m_Address.end(), *arr); } /// Gets a pointer to an already allocated byte array and copies the address value to it. /// This method assumes array allocated size is at least 6 (the size of a MAC address) /// @param[in] arr A pointer to the array which address will be copied to - void copyTo(uint8_t* arr) const + void copyTo(uint8_t arr[6]) const { - memcpy(arr, m_Address, sizeof(m_Address)); + std::copy(m_Address.begin(), m_Address.end(), arr); } /// A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00" static MacAddress Zero; private: - uint8_t m_Address[6] = { 0 }; + std::array m_Address{}; }; inline std::ostream& operator<<(std::ostream& oss, const pcpp::MacAddress& macAddress) diff --git a/Packet++/header/Layer.h b/Packet++/header/Layer.h index 828cb5c84f..f2e1755e14 100644 --- a/Packet++/header/Layer.h +++ b/Packet++/header/Layer.h @@ -232,10 +232,9 @@ namespace pcpp virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten); }; + inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer) + { + os << layer.toString(); + return os; + } } // namespace pcpp - -inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer) -{ - os << layer.toString(); - return os; -} diff --git a/Packet++/header/LdapLayer.h b/Packet++/header/LdapLayer.h index 09e82c9ba1..9ab9b888dd 100644 --- a/Packet++/header/LdapLayer.h +++ b/Packet++/header/LdapLayer.h @@ -1320,47 +1320,47 @@ namespace pcpp : LdapResponseLayer(std::move(asn1Record), data, dataLen, prevLayer, packet) {} }; -} // namespace pcpp - -inline std::ostream& operator<<(std::ostream& os, const pcpp::LdapControl& control) -{ - os << "{" << control.controlType << ", " << control.controlValue << "}"; - return os; -} -inline std::ostream& operator<<(std::ostream& os, const pcpp::LdapAttribute& attr) -{ - os << "{" << attr.type << ", {"; + inline std::ostream& operator<<(std::ostream& os, const pcpp::LdapControl& control) + { + os << "{" << control.controlType << ", " << control.controlValue << "}"; + return os; + } - std::string separator; - for (const auto& value : attr.values) + inline std::ostream& operator<<(std::ostream& os, const pcpp::LdapAttribute& attr) { - os << separator << value; - if (separator.empty()) + os << "{" << attr.type << ", {"; + + std::string separator; + for (const auto& value : attr.values) { - separator = ", "; + os << separator << value; + if (separator.empty()) + { + separator = ", "; + } } - } - os << "}}"; - return os; -} - -inline std::ostream& operator<<(std::ostream& os, - const pcpp::LdapBindRequestLayer::SaslAuthentication& saslAuthentication) -{ - os << "{" << saslAuthentication.mechanism << ", {"; + os << "}}"; + return os; + } - std::string separator; - for (const auto& value : saslAuthentication.credentials) + inline std::ostream& operator<<(std::ostream& os, + const pcpp::LdapBindRequestLayer::SaslAuthentication& saslAuthentication) { - os << separator << "0x" << std::hex << static_cast(value) << std::dec; - if (separator.empty()) + os << "{" << saslAuthentication.mechanism << ", {"; + + std::string separator; + for (const auto& value : saslAuthentication.credentials) { - separator = ", "; + os << separator << "0x" << std::hex << static_cast(value) << std::dec; + if (separator.empty()) + { + separator = ", "; + } } - } - os << "}}"; - return os; -} + os << "}}"; + return os; + } +} // namespace pcpp diff --git a/Packet++/header/Packet.h b/Packet++/header/Packet.h index 4e7760f678..65324f6f26 100644 --- a/Packet++/header/Packet.h +++ b/Packet++/header/Packet.h @@ -445,10 +445,9 @@ namespace pcpp return dynamic_cast(curLayer); } + inline std::ostream& operator<<(std::ostream& os, const pcpp::Packet& packet) + { + os << packet.toString(); + return os; + } } // namespace pcpp - -inline std::ostream& operator<<(std::ostream& os, const pcpp::Packet& packet) -{ - os << packet.toString(); - return os; -} diff --git a/Packet++/header/StpLayer.h b/Packet++/header/StpLayer.h index f1b3cf75fd..d153bbb995 100644 --- a/Packet++/header/StpLayer.h +++ b/Packet++/header/StpLayer.h @@ -2,6 +2,7 @@ #include "Layer.h" #include "MacAddress.h" +#include /// @file diff --git a/Packet++/src/CotpLayer.cpp b/Packet++/src/CotpLayer.cpp index 2a218f547a..1183df5f13 100644 --- a/Packet++/src/CotpLayer.cpp +++ b/Packet++/src/CotpLayer.cpp @@ -2,6 +2,8 @@ #include "S7CommLayer.h" #include +#include + namespace pcpp { diff --git a/Packet++/src/EthDot3Layer.cpp b/Packet++/src/EthDot3Layer.cpp index 7a97897337..7091cd2eba 100644 --- a/Packet++/src/EthDot3Layer.cpp +++ b/Packet++/src/EthDot3Layer.cpp @@ -3,6 +3,8 @@ #include "PayloadLayer.h" #include "LLCLayer.h" +#include + namespace pcpp { diff --git a/Packet++/src/LLCLayer.cpp b/Packet++/src/LLCLayer.cpp index fe5c3c2fe5..5cfe5d84da 100644 --- a/Packet++/src/LLCLayer.cpp +++ b/Packet++/src/LLCLayer.cpp @@ -4,6 +4,8 @@ #include "PayloadLayer.h" #include "StpLayer.h" +#include + namespace pcpp { diff --git a/Packet++/src/S7CommLayer.cpp b/Packet++/src/S7CommLayer.cpp index 33358d23bb..0affaa8145 100644 --- a/Packet++/src/S7CommLayer.cpp +++ b/Packet++/src/S7CommLayer.cpp @@ -2,6 +2,7 @@ #include "S7CommLayer.h" #include +#include #include namespace pcpp diff --git a/Packet++/src/TpktLayer.cpp b/Packet++/src/TpktLayer.cpp index 2d003cd5e7..c3fe3a9ff7 100644 --- a/Packet++/src/TpktLayer.cpp +++ b/Packet++/src/TpktLayer.cpp @@ -2,6 +2,7 @@ #include "EndianPortable.h" #include "CotpLayer.h" #include "PayloadLayer.h" +#include #include namespace pcpp diff --git a/Packet++/src/VxlanLayer.cpp b/Packet++/src/VxlanLayer.cpp index ec1acc1657..6001b4197b 100644 --- a/Packet++/src/VxlanLayer.cpp +++ b/Packet++/src/VxlanLayer.cpp @@ -2,6 +2,8 @@ #include "EthLayer.h" #include "EndianPortable.h" +#include + namespace pcpp { diff --git a/Tests/PcppTestFramework/PcppTestFramework.h b/Tests/PcppTestFramework/PcppTestFramework.h index 9a1e60cad3..143e24332e 100644 --- a/Tests/PcppTestFramework/PcppTestFramework.h +++ b/Tests/PcppTestFramework/PcppTestFramework.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "memplumber.h" #include "PcppTestFrameworkCommon.h"