Skip to content

Commit

Permalink
Refactor code: use enum class in TcpLayer (#1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekMs authored May 15, 2024
1 parent 3a77e45 commit 2f13768
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 101 deletions.
37 changes: 30 additions & 7 deletions Common++/header/DeprecationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@
/// @file

#ifndef PCPP_DEPRECATED
#if defined(__GNUC__) || defined(__clang__)
#define PCPP_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define PCPP_DEPRECATED __declspec(deprecated)
#else
#pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
#define PCPP_DEPRECATED
#if defined(__GNUC__) || defined(__clang__)
#define PCPP_DEPRECATED(msg) __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
#define PCPP_DEPRECATED(msg) __declspec(deprecated(msg))
#else
#pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
#define PCPP_DEPRECATED(msg)
#endif
#endif

#if !defined(DISABLE_WARNING_PUSH) || !defined(DISABLE_WARNING_POP)
#if defined(_MSC_VER)
#define DISABLE_WARNING_PUSH __pragma(warning( push ))
#define DISABLE_WARNING_POP __pragma(warning( pop ))
#define DISABLE_WARNING(warningNumber) __pragma(warning( disable : warningNumber ))

#define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(4996)
#elif defined(__GNUC__) || defined(__clang__)
#define DO_PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)

#define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(-Wdeprecated-declarations)
#else
#pragma message("WARNING: Disabling of warnings is not implemented for this compiler")
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP

#define DISABLE_WARNING_DEPRECATED
#endif
#endif
2 changes: 1 addition & 1 deletion Examples/Tutorials/Tutorial-PacketCraftAndEdit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int main(int argc, char* argv[])
// add URG flag
tcpLayer->getTcpHeader()->urgFlag = 1;
// add MSS TCP option
tcpLayer->addTcpOptionAfter(pcpp::TcpOptionBuilder(pcpp::TCPOPT_MSS, (uint16_t)1460));
tcpLayer->insertTcpOptionAfter(pcpp::TcpOptionBuilder(pcpp::TcpOptionEnumType::Mss, (uint16_t)1460));

// let's get the HTTP layer
auto* httpRequestLayer = parsedPacket.getLayerOfType<pcpp::HttpRequestLayer>();
Expand Down
8 changes: 4 additions & 4 deletions Examples/Tutorials/Tutorial-PacketParsing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ std::string printTcpFlags(pcpp::TcpLayer* tcpLayer)
return result;
}

std::string printTcpOptionType(pcpp::TcpOptionType optionType)
std::string printTcpOptionType(pcpp::TcpOptionEnumType optionType)
{
switch (optionType)
{
case pcpp::PCPP_TCPOPT_NOP:
case pcpp::TcpOptionEnumType::Nop:
return "NOP";
case pcpp::PCPP_TCPOPT_TIMESTAMP:
case pcpp::TcpOptionEnumType::Timestamp:
return "Timestamp";
default:
return "Other";
Expand Down Expand Up @@ -160,7 +160,7 @@ int main(int argc, char* argv[])
std::cout << "TCP options: ";
for (pcpp::TcpOption tcpOption = tcpLayer->getFirstTcpOption(); tcpOption.isNotNull(); tcpOption = tcpLayer->getNextTcpOption(tcpOption))
{
std::cout << printTcpOptionType(tcpOption.getTcpOptionType()) << " ";
std::cout << printTcpOptionType(tcpOption.getTcpOptionEnumType()) << " ";
}
std::cout << std::endl;

Expand Down
6 changes: 4 additions & 2 deletions Packet++/header/HttpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ namespace pcpp
* default status code string
* @deprecated Use other constructors instead.
*/
PCPP_DEPRECATED explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode, const std::string& statusCodeString);
PCPP_DEPRECATED("Use other constructors instead")
explicit HttpResponseLayer(HttpVersion version, const HttpResponseStatusCode& statusCode, const std::string& statusCodeString);

/**
* A constructor that allocates a new HTTP response header with only the first line filled. Object will be created without further fields.
Expand Down Expand Up @@ -732,7 +733,8 @@ namespace pcpp
* @return True if setting the status code was completed successfully, false otherwise
* @deprecated Use the other overload instead.
*/
PCPP_DEPRECATED bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);
PCPP_DEPRECATED("Use the other overload instead")
bool setStatusCode(const HttpResponseStatusCode& newStatusCode, const std::string& statusCodeString);

/**
* Set the status code
Expand Down
Loading

0 comments on commit 2f13768

Please sign in to comment.