Skip to content

Commit

Permalink
Merge branch 'dev' into refactor/log-templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimi1010 authored Jan 27, 2025
2 parents e24044a + 2d0fc5a commit 8ac895d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 65 deletions.
46 changes: 26 additions & 20 deletions Common++/header/MacAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <iterator>
#include <ostream>
#include <cstdint>
#include <cstring>
#include <string>
#include <array>

/// @file

Expand All @@ -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<uint8_t, 6>& 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"
Expand All @@ -54,22 +60,16 @@ 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).
/// If the list is invalid, the constructor throws an exception.
/// @param[in] octets An initializer list containing the values of type uint8_t representing the MAC address
MacAddress(std::initializer_list<uint8_t> octets)
{
if (octets.size() != sizeof(m_Address))
if (octets.size() != m_Address.size())
{
throw std::invalid_argument("Invalid initializer list size, should be 6");
}
Expand All @@ -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
Expand All @@ -103,43 +103,49 @@ 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;
}

/// Returns the pointer to raw data
/// @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<uint8_t, 6> 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<uint8_t, 6> m_Address{};
};

inline std::ostream& operator<<(std::ostream& oss, const pcpp::MacAddress& macAddress)
Expand Down
11 changes: 5 additions & 6 deletions Packet++/header/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
66 changes: 33 additions & 33 deletions Packet++/header/LdapLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<int>(value) << std::dec;
if (separator.empty())
{
separator = ", ";
}
}
}

os << "}}";
return os;
}
os << "}}";
return os;
}
} // namespace pcpp
11 changes: 5 additions & 6 deletions Packet++/header/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,9 @@ namespace pcpp
return dynamic_cast<TLayer*>(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;
}
1 change: 1 addition & 0 deletions Packet++/header/StpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Layer.h"
#include "MacAddress.h"
#include <cstring>

/// @file

Expand Down
2 changes: 2 additions & 0 deletions Packet++/src/CotpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "S7CommLayer.h"
#include <PayloadLayer.h>

#include <cstring>

namespace pcpp
{

Expand Down
2 changes: 2 additions & 0 deletions Packet++/src/EthDot3Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "PayloadLayer.h"
#include "LLCLayer.h"

#include <cstring>

namespace pcpp
{

Expand Down
2 changes: 2 additions & 0 deletions Packet++/src/LLCLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "PayloadLayer.h"
#include "StpLayer.h"

#include <cstring>

namespace pcpp
{

Expand Down
1 change: 1 addition & 0 deletions Packet++/src/S7CommLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "S7CommLayer.h"
#include <iostream>
#include <cstring>
#include <sstream>

namespace pcpp
Expand Down
1 change: 1 addition & 0 deletions Packet++/src/TpktLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "EndianPortable.h"
#include "CotpLayer.h"
#include "PayloadLayer.h"
#include <cstring>
#include <sstream>

namespace pcpp
Expand Down
2 changes: 2 additions & 0 deletions Packet++/src/VxlanLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "EthLayer.h"
#include "EndianPortable.h"

#include <cstring>

namespace pcpp
{

Expand Down
1 change: 1 addition & 0 deletions Tests/PcppTestFramework/PcppTestFramework.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <iomanip>
#include <cstring>
#include <iostream>
#include "memplumber.h"
#include "PcppTestFrameworkCommon.h"
Expand Down

0 comments on commit 8ac895d

Please sign in to comment.