Skip to content

Commit

Permalink
Refactor DHCP port checking code in DhcpLayer (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadongjun authored Aug 23, 2024
1 parent 91cde5e commit fdd4caf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Packet++/header/DhcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,14 @@ namespace pcpp
*/
bool removeAllOptions();

/**
* A static method that checks whether a pair of ports are considered DHCP ports
* @param[in] portSrc The source port number to check
* @param[in] portDst The destination port number to check
* @return True if these are DHCP port numbers, false otherwise
*/
static inline bool isDhcpPorts(uint16_t portSrc, uint16_t portDst);

// implement abstract methods

/**
Expand Down Expand Up @@ -868,4 +876,13 @@ namespace pcpp

DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
};

// implementation of inline methods

bool DhcpLayer::isDhcpPorts(uint16_t portSrc, uint16_t portDst)
{
return ((portSrc == 68 && portDst == 67) || (portSrc == 67 && portDst == 68) ||
(portSrc == 67 && portDst == 67));
}

} // namespace pcpp
2 changes: 1 addition & 1 deletion Packet++/src/UdpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace pcpp
uint8_t* udpData = m_Data + sizeof(udphdr);
size_t udpDataLen = m_DataLen - sizeof(udphdr);

if ((portSrc == 68 && portDst == 67) || (portSrc == 67 && portDst == 68) || (portSrc == 67 && portDst == 67))
if (DhcpLayer::isDhcpPorts(portSrc, portDst))
m_NextLayer = new DhcpLayer(udpData, udpDataLen, this, m_Packet);
else if (VxlanLayer::isVxlanPort(portDst))
m_NextLayer = new VxlanLayer(udpData, udpDataLen, this, m_Packet);
Expand Down

0 comments on commit fdd4caf

Please sign in to comment.