-
Notifications
You must be signed in to change notification settings - Fork 715
Added sized overloads for buffer construction of IPv6Address
and MacAddress
.
#1847
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
Changes from 5 commits
1f48aaa
5b4a78a
7d48ee0
b2f2949
6a7748d
170415e
0aaf158
e43e0bc
856a191
8316826
2e69735
9036156
5537578
665d195
708b7f8
a6cf5a4
e1c4703
019b376
4143274
870d33e
b0eea08
9399e5f
fb3f8f3
3075097
3803685
f04b721
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
#include <array> | ||
#include <memory> | ||
|
||
#include "DeprecationUtils.h" | ||
|
||
/// @file | ||
|
||
/// @namespace pcpp | ||
|
@@ -39,8 +41,21 @@ namespace pcpp | |
|
||
/// A constructor that creates an instance of the class out of 4-byte array. | ||
/// @param[in] bytes The address as 4-byte array in network byte order | ||
IPv4Address(const uint8_t bytes[4]) | ||
/// @remarks This constructor assumes that the provided array is exactly 4 bytes long. | ||
/// Prefer using the constructor with size parameter if the array length is not guaranteed to be 4 bytes. | ||
IPv4Address(const uint8_t bytes[4]) : IPv4Address(bytes, 4) | ||
{} | ||
|
||
/// A constructor that creates an instance of the class out of a 4-byte array. | ||
/// @param[in] bytes The address as 4-byte array in network byte order | ||
/// @param[in] size The size of the array in bytes | ||
/// @throws std::out_of_range If the provided size is smaller than 4 bytes. | ||
IPv4Address(const uint8_t bytes[4], size_t size) | ||
seladb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (size < 4) | ||
{ | ||
throw std::out_of_range("Buffer size is smaller than IPv4 address size"); | ||
} | ||
memcpy(m_Bytes.data(), bytes, 4 * sizeof(uint8_t)); | ||
} | ||
|
||
|
@@ -160,9 +175,22 @@ namespace pcpp | |
|
||
/// A constructor that creates an instance of the class out of 16-byte array. | ||
/// @param[in] bytes The address as 16-byte array in network byte order | ||
IPv6Address(const uint8_t bytes[16]) | ||
/// remarks This constructor assumes that the provided array is exactly 16 bytes long. | ||
/// Prefer using the constructor with size parameter if the array length is not guaranteed to be 16 bytes. | ||
IPv6Address(const uint8_t bytes[16]) : IPv6Address(bytes, 16) | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{} | ||
|
||
/// @brief A constructor that creates an instance of the class out of a 16-byte array. | ||
/// @param bytes The address as 16-byte array in network byte order | ||
/// @param size The size of the array in bytes | ||
/// @throws std::out_of_range If the provided size is smaller than 16 bytes. | ||
IPv6Address(const uint8_t bytes[16], size_t size) | ||
seladb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
memcpy(m_Bytes.data(), bytes, 16 * sizeof(uint8_t)); | ||
if (size < 16) | ||
{ | ||
throw std::out_of_range("Buffer size is smaller than IPv6 address size"); | ||
} | ||
std::memcpy(m_Bytes.data(), bytes, 16 * sizeof(uint8_t)); | ||
} | ||
|
||
/// A constructor that creates an instance of the class out of a 16-byte standard array. | ||
|
@@ -224,16 +252,39 @@ namespace pcpp | |
/// Allocates a byte array and copies address value into it. Array deallocation is user responsibility | ||
/// @param[in] arr A pointer to where array will be allocated | ||
/// @param[out] length Returns the length in bytes of the array that was allocated | ||
/// @deprecated Use copyToNewBuffer instead as it returns a unique pointer to the allocated array. | ||
PCPP_DEPRECATED("Use copyToNewBuffer instead.") | ||
void copyTo(uint8_t** arr, size_t& length) const; | ||
|
||
/// 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 16 (the size of an IPv6 address) | ||
/// @param[in] arr A pointer to the array which address will be copied to | ||
/// @remarks This method assumes that the provided array is at least 16 bytes long. | ||
/// Prefer using the copyTo(uint8_t* buffer, size_t size) method if the array length is not guaranteed to be 16 | ||
/// bytes. | ||
void copyTo(uint8_t* arr) const | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
memcpy(arr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t)); | ||
return copyTo(arr, 16); | ||
} | ||
|
||
/// @brief Copies the address value to a user-provided buffer. | ||
/// @param buffer[in] A pointer to the buffer where the address will be copied | ||
/// @param size[in] The size of the buffer in bytes | ||
/// @throws std::out_of_range If the provided size is smaller than 16 bytes. | ||
void copyTo(uint8_t* buffer, size_t size) const | ||
{ | ||
if (size < m_Bytes.size()) | ||
{ | ||
throw std::out_of_range("Buffer size is smaller than IPv6 address size"); | ||
} | ||
memcpy(buffer, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t)); | ||
} | ||
|
||
/// @brief Allocates a byte array and copies address value into it. | ||
/// @param[out] size Returns the size in bytes of the allocated array. Usually 16. | ||
/// @return A unique pointer to the allocated byte array containing the address value | ||
std::unique_ptr<uint8_t[]> copyToNewBuffer(size_t& size) const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debating if its better to return PS: Kinda starting to lean to pair as it will keep the buffer and size logically grouped? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can consider to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is This one mostly covers existing functionality that was in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am okay with having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is true. I can't find a lot of usages where a new buffer is required. Most of the time it is copied to an already existing buffer, if the copyTo api is used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ended up deprecating the allocating Example usage: MacAddress mac;
size_t addrLen = mac.copyTo(nullptr, 0); // Query the function for a required buffer.
uint8_t* addr = new uint8_t[addrLen];
if(mac.copyTo(addr, addrLen) != addrLen) { /* Error check */ }
// Use addr... |
||
|
||
/// Checks whether the address matches a network. | ||
/// @param network An IPv6Network network | ||
/// @return True if the address matches the network or false otherwise | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
#include <cstdint> | ||
#include <string> | ||
#include <array> | ||
#include <memory> | ||
|
||
#include "DeprecationUtils.h" | ||
|
||
/// @file | ||
|
||
|
@@ -28,8 +31,21 @@ 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[6]) | ||
/// @remarks This constructor assumes that the provided array is exactly 6 bytes long. | ||
/// Prefer using the constructor with size parameter if the array length is not guaranteed to be 6 bytes. | ||
explicit MacAddress(const uint8_t addr[6]) : MacAddress(addr, 6) | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{} | ||
|
||
/// @brief A constructor that creates an instance of the class out of a byte array. | ||
/// @param[in] addr The address as a byte array in network byte order | ||
/// @param[in] size The size of the array in bytes | ||
/// @throws std::out_of_range If the provided size is smaller than 6 bytes. | ||
explicit MacAddress(const uint8_t addr[6], size_t size) | ||
{ | ||
if (size < 6) | ||
{ | ||
throw std::out_of_range("Buffer size is smaller than MAC address size"); | ||
} | ||
std::copy(addr, addr + 6, m_Address.begin()); | ||
} | ||
|
||
|
@@ -127,18 +143,47 @@ namespace pcpp | |
/// 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 | ||
/// @deprecated Use copyToNewBuffer instead as it returns a unique pointer to the allocated array. | ||
PCPP_DEPRECATED("Use copyToNewBuffer instead as it returns a unique pointer to the allocated array.") | ||
void copyTo(uint8_t** arr) const | ||
{ | ||
*arr = new uint8_t[m_Address.size()]; | ||
std::copy(m_Address.begin(), m_Address.end(), *arr); | ||
size_t size; | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*arr = copyToNewBuffer(size).release(); | ||
} | ||
|
||
/// 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 | ||
/// @remarks This method assumes that the provided array is at least 6 bytes long. | ||
/// Prefer using the copyTo(uint8_t* buffer, size_t size) method if the array length is not guaranteed to be 6 | ||
/// bytes. | ||
void copyTo(uint8_t arr[6]) const | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
std::copy(m_Address.begin(), m_Address.end(), arr); | ||
return copyTo(arr, 6); | ||
} | ||
|
||
/// @brief Copies the address value to a user-provided buffer. | ||
/// @param[in] buffer A pointer to the buffer where the address will be copied | ||
/// @param[in] size The size of the buffer in bytes | ||
/// @throws std::out_of_range If the provided size is smaller than 6 bytes. | ||
void copyTo(uint8_t* buffer, size_t size) const | ||
{ | ||
if (size < m_Address.size()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto: do we need to support copying to an array smaller than 6 bytes? |
||
{ | ||
throw std::out_of_range("Buffer size is smaller than MAC address size"); | ||
} | ||
std::copy(m_Address.begin(), m_Address.end(), buffer); | ||
} | ||
|
||
/// @brief Allocates a byte array and copies address value into it. | ||
/// @param size[out] The size of the allocated array in bytes. Usually 6. | ||
/// @return A unique pointer to the allocated byte array containing the MAC address. | ||
std::unique_ptr<uint8_t[]> copyToNewBuffer(size_t& size) const | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
size = m_Address.size(); | ||
auto arr = std::make_unique<uint8_t[]>(size); | ||
std::copy(m_Address.begin(), m_Address.end(), arr.get()); | ||
return arr; | ||
} | ||
|
||
/// A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00" | ||
|
Uh oh!
There was an error while loading. Please reload this page.