Skip to content

Commit

Permalink
fixes for Common++
Browse files Browse the repository at this point in the history
  • Loading branch information
egecetin committed Nov 9, 2024
1 parent eaf6133 commit c4cac45
Show file tree
Hide file tree
Showing 21 changed files with 300 additions and 188 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ BreakBeforeBraces: Custom
BraceWrapping:
SplitEmptyFunction: false
AfterCaseLabel: true
QualifierAlignment: Left
...
34 changes: 34 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Checks: 'cert-*,
clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
misc-*,
modernize-*,
performance-*,
portability-*,
readability-*,
-cert-env33-c,
-cert-err58-cpp,
-clang-analyzer-optin.cplusplus.VirtualCall,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-const-cast,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-special-member-functions,
-modernize-avoid-c-arrays,
-modernize-use-trailing-return-type,
-misc-header-include-cycle,
-misc-include-cleaner,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-misc-use-anonymous-namespace,
-readability-function-cognitive-complexity,
-readability-magic-numbers'
2 changes: 1 addition & 1 deletion Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <stdint.h>
#include <cstdint>
#include <type_traits>

/// @file
Expand Down
96 changes: 46 additions & 50 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <string.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <algorithm>
#include <ostream>
Expand Down Expand Up @@ -186,7 +186,7 @@ namespace pcpp

uint32_t IPv4Address::toInt() const
{
uint32_t addr;
uint32_t addr = 0;
memcpy(&addr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t));
return addr;
}
Expand Down Expand Up @@ -518,7 +518,9 @@ namespace pcpp
bool IPAddress::operator==(const IPAddress& rhs) const
{
if (isIPv4())
{
return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
}

return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
}
Expand Down Expand Up @@ -561,7 +563,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32u)
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
{}

/**
Expand Down Expand Up @@ -616,7 +618,7 @@ namespace pcpp
*/
IPv4Address getNetworkPrefix() const
{
return IPv4Address(m_NetworkPrefix);
return m_NetworkPrefix;
}

/**
Expand Down Expand Up @@ -657,10 +659,10 @@ namespace pcpp
std::string toString() const;

private:
uint32_t m_NetworkPrefix;
uint32_t m_Mask;
uint32_t m_NetworkPrefix{};
uint32_t m_Mask{};

bool isValidNetmask(const IPv4Address& netmaskAddress);
static bool isValidNetmask(const IPv4Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv4Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv4Address& address, const IPv4Address& netmaskAddress);
};
Expand All @@ -678,7 +680,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128u)
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
{}

/**
Expand Down Expand Up @@ -733,7 +735,7 @@ namespace pcpp
*/
IPv6Address getNetworkPrefix() const
{
return IPv6Address(m_NetworkPrefix);
return { m_NetworkPrefix };
}

/**
Expand Down Expand Up @@ -774,10 +776,10 @@ namespace pcpp
std::string toString() const;

private:
uint8_t m_NetworkPrefix[16];
uint8_t m_Mask[16];
uint8_t m_NetworkPrefix[16]{};
uint8_t m_Mask[16]{};

bool isValidNetmask(const IPv6Address& netmaskAddress);
static bool isValidNetmask(const IPv6Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv6Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv6Address& address, const IPv6Address& netmaskAddress);
};
Expand All @@ -795,7 +797,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32u : 128u)
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U)
{}

/**
Expand Down Expand Up @@ -892,10 +894,8 @@ namespace pcpp
{
return this->operator=(*other.m_IPv4Network);
}
else
{
return this->operator=(*other.m_IPv6Network);
}

return this->operator=(*other.m_IPv6Network);
}

/**
Expand Down Expand Up @@ -1032,15 +1032,13 @@ namespace pcpp

return m_IPv4Network->includes(address.getIPv4());
}
else
{
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
}

/**
Expand All @@ -1058,15 +1056,13 @@ namespace pcpp

return m_IPv4Network->includes(*network.m_IPv4Network);
}
else
{
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
}

/**
Expand All @@ -1084,38 +1080,38 @@ namespace pcpp
};
} // namespace pcpp

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Address& ipv4Address)
{
os << ipv4Address.toString();
return os;
oss << ipv4Address.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Address& ipv6Address)
{
os << ipv6Address.toString();
return os;
oss << ipv6Address.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
oss << ipAddress.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Network& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Network& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPNetwork& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}
22 changes: 11 additions & 11 deletions Common++/header/IpUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <stdint.h>
#include <cstdint>
#ifdef __linux__
# include <netinet/in.h>
# include <arpa/inet.h>
Expand Down Expand Up @@ -59,43 +59,43 @@ namespace pcpp
{
/**
* Extract IPv4 address from sockaddr
* @param[in] sa - input sockaddr
* @param[in] saddr - input sockaddr
* @return Address in in_addr format
* @throws std::invalid_argument Sockaddr family is not AF_INET or sockaddr is nullptr.
*/
in_addr* sockaddr2in_addr(sockaddr* sa);
in_addr* sockaddr2in_addr(sockaddr* saddr);

/**
* Attempt to extract IPv4 address from sockaddr
* @param[in] sa - input sockaddr
* @param[in] saddr - input sockaddr
* @return Pointer to address in in_addr format or nullptr if extraction fails.
*/
in_addr* try_sockaddr2in_addr(sockaddr* sa);
in_addr* try_sockaddr2in_addr(sockaddr* saddr);

/**
* Extract IPv6 address from sockaddr
* @param[in] sa - input sockaddr
* @param[in] saddr - input sockaddr
* @return Address in in6_addr format
* @throws std::invalid_argument Sockaddr family is not AF_INET6 or sockaddr is nullptr.
*/
in6_addr* sockaddr2in6_addr(sockaddr* sa);
in6_addr* sockaddr2in6_addr(sockaddr* saddr);

/**
* Attempt to extract IPv6 address from sockaddr
* @param[in] sa - input sockaddr
* @param[in] saddr - input sockaddr
* @return Pointer to address in in6_addr format or nullptr if extraction fails.
*/
in6_addr* try_sockaddr2in6_addr(sockaddr* sa);
in6_addr* try_sockaddr2in6_addr(sockaddr* saddr);

/**
* Converts a sockaddr format address to its string representation
* @param[in] sa Address in sockaddr format
* @param[in] saddr Address in sockaddr format
* @param[out] resultString String representation of the address
* @param[in] resultBufLen Length of the result buffer.
* @throws std::invalid_argument Sockaddr family is not AF_INET or AF_INET6, sockaddr is nullptr or the result
* str buffer is insufficient.
*/
void sockaddr2string(sockaddr const* sa, char* resultString, size_t resultBufLen);
void sockaddr2string(const sockaddr* saddr, char* resultString, size_t resultBufLen);

/**
* Convert a in_addr format address to 32bit representation
Expand Down
17 changes: 10 additions & 7 deletions Common++/header/LRUList.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstddef>
#include <list>
#include <unordered_map>

Expand All @@ -26,17 +27,15 @@ namespace pcpp
template <typename T> class LRUList
{
public:
typedef typename std::list<T>::iterator ListIterator;
typedef typename std::unordered_map<T, ListIterator>::iterator MapIterator;
using ListIterator = typename std::list<T>::iterator;
using MapIterator = typename std::unordered_map<T, ListIterator>::iterator;

/**
* A c'tor for this class
* @param[in] maxSize The max size this list can go
*/
explicit LRUList(size_t maxSize)
{
m_MaxSize = maxSize;
}
explicit LRUList(std::size_t maxSize) : m_MaxSize(maxSize)
{}

/**
* Puts an element in the list. This element will be inserted (or advanced if it already exists) to the head of
Expand All @@ -58,7 +57,7 @@ namespace pcpp
// iterator to the element that prevented the insertion
std::pair<MapIterator, bool> pair =
m_CacheItemsMap.insert(std::make_pair(element, m_CacheItemsList.begin()));
if (pair.second == false) // already exists
if (!static_cast<bool>(pair.second)) // already exists
{
m_CacheItemsList.erase(pair.first->second);
pair.first->second = m_CacheItemsList.begin();
Expand All @@ -70,11 +69,13 @@ namespace pcpp
--lruIter;

if (deletedValue != nullptr)
{
#if __cplusplus > 199711L || _MSC_VER >= 1800
*deletedValue = std::move(*lruIter);
#else
*deletedValue = *lruIter;
#endif
}
m_CacheItemsMap.erase(*lruIter);
m_CacheItemsList.erase(lruIter);
return 1;
Expand Down Expand Up @@ -109,7 +110,9 @@ namespace pcpp
{
MapIterator iter = m_CacheItemsMap.find(element);
if (iter == m_CacheItemsMap.end())
{
return;
}

m_CacheItemsList.erase(iter->second);
m_CacheItemsMap.erase(iter);
Expand Down
Loading

0 comments on commit c4cac45

Please sign in to comment.