Skip to content

Commit

Permalink
apply fixes for Common++
Browse files Browse the repository at this point in the history
  • Loading branch information
egecetin committed Jan 1, 2025
1 parent ab75534 commit fbafcd8
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 215 deletions.
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'
41 changes: 23 additions & 18 deletions Common++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ add_library(
src/OUILookup.cpp
src/PcapPlusPlusVersion.cpp
src/SystemUtils.cpp
src/TablePrinter.cpp)
src/TablePrinter.cpp
)

set(public_headers
header/DeprecationUtils.h
header/GeneralUtils.h
header/IpAddress.h
header/IpAddressUtils.h
header/IpUtils.h
header/Logger.h
header/LRUList.h
header/MacAddress.h
header/OUILookup.h
header/PcapPlusPlusVersion.h
header/PointerVector.h
header/SystemUtils.h
header/TablePrinter.h
header/TimespecTimeval.h)
set(
public_headers
header/DeprecationUtils.h
header/GeneralUtils.h
header/IpAddress.h
header/IpAddressUtils.h
header/IpUtils.h
header/Logger.h
header/LRUList.h
header/MacAddress.h
header/OUILookup.h
header/PcapPlusPlusVersion.h
header/PointerVector.h
header/SystemUtils.h
header/TablePrinter.h
header/TimespecTimeval.h
)

# Set the public header that will be installed
set_property(TARGET Common++ PROPERTY PUBLIC_HEADER ${public_headers})
Expand All @@ -35,7 +38,8 @@ target_include_directories(
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/header> $<INSTALL_INTERFACE:include/pcapplusplus>
# Don't link with EndianPortable and json as it won't be exported
PRIVATE $<TARGET_PROPERTY:EndianPortable,INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE $<TARGET_PROPERTY:json,INTERFACE_INCLUDE_DIRECTORIES>)
PRIVATE $<TARGET_PROPERTY:json,INTERFACE_INCLUDE_DIRECTORIES>
)

if(WIN32)
target_link_libraries(Common++ PRIVATE ws2_32 iphlpapi)
Expand All @@ -48,7 +52,8 @@ if(PCAPPP_INSTALL)
ARCHIVE DESTINATION ${PCAPPP_INSTALL_LIBDIR}
LIBRARY DESTINATION ${PCAPPP_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${PCAPPP_INSTALL_INCLUDEDIR}
RUNTIME DESTINATION ${PCAPPP_INSTALL_BINDIR})
RUNTIME DESTINATION ${PCAPPP_INSTALL_BINDIR}
)
endif()

set_property(TARGET Common++ PROPERTY OUTPUT_NAME "Common++")
Expand Down
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 @@ -145,7 +145,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 @@ -395,7 +395,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 @@ -433,7 +435,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32u)
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -476,7 +478,7 @@ namespace pcpp
/// @return The network prefix, for example: the network prefix of 10.10.10.10/16 is 10.10.0.0
IPv4Address getNetworkPrefix() const
{
return IPv4Address(m_NetworkPrefix);
return m_NetworkPrefix;
}

/// @return The lowest non-reserved IPv4 address in this network, for example: the lowest address
Expand Down Expand Up @@ -505,10 +507,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 @@ -521,7 +523,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128u)
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -564,7 +566,7 @@ namespace pcpp
/// @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::
IPv6Address getNetworkPrefix() const
{
return IPv6Address(m_NetworkPrefix);
return { m_NetworkPrefix };
}

/// @return The lowest non-reserved IPv6 address in this network, for example: the lowest address in 3546::/16
Expand Down Expand Up @@ -593,10 +595,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 @@ -609,7 +611,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an IP address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @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)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -696,10 +698,8 @@ namespace pcpp
{
return this->operator=(*other.m_IPv4Network);
}
else
{
return this->operator=(*other.m_IPv6Network);
}

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

/// Overload of an assignment operator.
Expand Down Expand Up @@ -814,15 +814,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());
}

/// @param network An IP network
Expand All @@ -838,15 +836,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);
}

/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
Expand All @@ -861,40 +857,40 @@ namespace pcpp
std::unique_ptr<IPv6Network> m_IPv6Network;
};

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;
}

} // namespace pcpp
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 @@ -53,34 +53,34 @@ namespace pcpp
namespace internal
{
/// 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
/// @param[in] inAddr Address in in_addr format
Expand Down
Loading

0 comments on commit fbafcd8

Please sign in to comment.