Skip to content

Commit

Permalink
Clang-format for Packet++ (#1510)
Browse files Browse the repository at this point in the history
* update http comment

* IP Reassembly comment

* fix comment

* fix more comment

* fix comment for layer

* pre-commit

* init format

* manually fix Bgp

* manually fix http

* manually fix Layer

* fix LLC

* manually fix ntp

* dns cpp

* Packet cpp

* ssl cpp

* revert ssl layer wrong format

* manully fix NTP, PPPoE, SingleCommandText

* manually fix SSH

* SSL manual fix

* TCP manual fix

* fix TCPReassembly

* Fix VRRP

* fix dns cpp

* fix ftp cpp

* manually fix http cpp

* fix empty line

* ICMP cpp

* manually fix IGMP

* manually fix LDAP

* NDP cpp

* Nflog cpp

* Ntp cpp

* PPPoE cpp

* SDP cpp

* SomeIpSd cpp

* SSL handshake cpp

* tcp cpp

* TestBasedProtocol cpp

* change to std::unique_ptr

* ndp cpp

* tcp reassembly cpp

* gre

* update ip reassembly

* update http

* Fix all

* remove the comment
  • Loading branch information
tigercosmos authored Aug 7, 2024
1 parent b640b4f commit 316ea6e
Show file tree
Hide file tree
Showing 130 changed files with 27,890 additions and 25,631 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: clang-format
args: ["--style=file"] # Use the .clang-format file for configuration
files: ^(Common\+\+|Tests|Examples)/.*\.(cpp|h)$
files: ^(Common\+\+|Tests|Examples|Packet\+\+)/.*\.(cpp|h)$
- id: cppcheck
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force"]
- repo: https://github.com/codespell-project/codespell
Expand Down
70 changes: 51 additions & 19 deletions Packet++/header/ArpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ namespace pcpp
/** Protocol type (PTYPE). The permitted PTYPE values share a numbering space with those for EtherType */
uint16_t protocolType;
/** Hardware address length (HLEN). For IPv4, this has the value 0x0800 */
uint8_t hardwareSize;
/** Protocol length (PLEN). Length (in octets) of addresses used in the upper layer protocol. (The upper layer protocol specified in PTYPE.) IPv4 address size is 4 */
uint8_t protocolSize;
/** Specifies the operation that the sender is performing: 1 (::ARP_REQUEST) for request, 2 (::ARP_REPLY) for reply */
uint8_t hardwareSize;
/** Protocol length (PLEN). Length (in octets) of addresses used in the upper layer protocol. (The upper layer
* protocol specified in PTYPE.) IPv4 address size is 4 */
uint8_t protocolSize;
/** Specifies the operation that the sender is performing: 1 (::ARP_REQUEST) for request, 2 (::ARP_REPLY) for
* reply */
uint16_t opcode;
/** Sender hardware address (SHA) */
uint8_t senderMacAddr[6];
Expand All @@ -46,8 +48,8 @@ namespace pcpp
*/
enum ArpOpcode
{
ARP_REQUEST = 0x0001, ///< ARP request
ARP_REPLY = 0x0002 ///< ARP reply (response)
ARP_REQUEST = 0x0001, ///< ARP request
ARP_REPLY = 0x0002 ///< ARP reply (response)
};

/**
Expand All @@ -64,7 +66,12 @@ namespace pcpp
* @param[in] prevLayer A pointer to the previous layer
* @param[in] packet A pointer to the Packet instance where layer will be stored in
*/
ArpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = ARP; m_DataLen = sizeof(arphdr); }
ArpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
: Layer(data, dataLen, prevLayer, packet)
{
m_Protocol = ARP;
m_DataLen = sizeof(arphdr);
}

/**
* A constructor that allocates a new ARP header
Expand All @@ -74,51 +81,73 @@ namespace pcpp
* @param[in] senderIpAddr The sender IP address (will be put in arphdr#senderIpAddr)
* @param[in] targetIpAddr The target IP address (will be put in arphdr#targetIpAddr)
*/
ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr, const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr);
ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr,
const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr);

~ArpLayer() {}
~ArpLayer()
{}

/**
* Get a pointer to the ARP header. Notice this points directly to the data, so every change will change the actual packet data
* Get a pointer to the ARP header. Notice this points directly to the data, so every change will change the
* actual packet data
* @return A pointer to the @ref arphdr
*/
inline arphdr* getArpHeader() const { return (arphdr*)m_Data; }
inline arphdr* getArpHeader() const
{
return (arphdr*)m_Data;
}

/**
* Get the sender hardware address (SHA) in the form of MacAddress
* @return A MacAddress containing the sender hardware address (SHA)
*/
inline MacAddress getSenderMacAddress() const { return MacAddress(getArpHeader()->senderMacAddr); }
inline MacAddress getSenderMacAddress() const
{
return MacAddress(getArpHeader()->senderMacAddr);
}

/**
* Get the target hardware address (THA) in the form of MacAddress
* @return A MacAddress containing the target hardware address (THA)
*/
inline MacAddress getTargetMacAddress() const { return MacAddress(getArpHeader()->targetMacAddr); }
inline MacAddress getTargetMacAddress() const
{
return MacAddress(getArpHeader()->targetMacAddr);
}

/**
* Get the sender protocol address (SPA) in the form of IPv4Address
* @return An IPv4Address containing the sender protocol address (SPA)
*/
inline IPv4Address getSenderIpAddr() const { return getArpHeader()->senderIpAddr; }
inline IPv4Address getSenderIpAddr() const
{
return getArpHeader()->senderIpAddr;
}

/**
* Get the target protocol address (TPA) in the form of IPv4Address
* @return An IPv4Address containing the target protocol address (TPA)
*/
inline IPv4Address getTargetIpAddr() const { return getArpHeader()->targetIpAddr; }
inline IPv4Address getTargetIpAddr() const
{
return getArpHeader()->targetIpAddr;
}

// implement abstract methods

/**
* Does nothing for this layer (ArpLayer is always last)
*/
void parseNextLayer() {}
void parseNextLayer()
{}

/**
* @return The size of @ref arphdr
*/
size_t getHeaderLen() const { return sizeof(arphdr); }
size_t getHeaderLen() const
{
return sizeof(arphdr);
}

/**
* Calculate the following fields:
Expand All @@ -142,7 +171,10 @@ namespace pcpp

std::string toString() const;

OsiModelLayer getOsiModelLayer() const { return OsiModelNetworkLayer; }
OsiModelLayer getOsiModelLayer() const
{
return OsiModelNetworkLayer;
}
};

} // namespace pcpp
} // namespace pcpp
90 changes: 66 additions & 24 deletions Packet++/header/Asn1Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,48 @@ namespace pcpp
/**
* @return The ASN.1 tag class
*/
Asn1TagClass getTagClass() const { return m_TagClass; }
Asn1TagClass getTagClass() const
{
return m_TagClass;
}

/**
* @return True if it's a constructed record, or false if it's a primitive record
*/
bool isConstructed() const { return m_IsConstructed; }
bool isConstructed() const
{
return m_IsConstructed;
}

/**
* @return The ASN.1 Universal tag type if the record is of class Universal, otherwise Asn1UniversalTagType#NotApplicable
* @return The ASN.1 Universal tag type if the record is of class Universal, otherwise
* Asn1UniversalTagType#NotApplicable
*/
Asn1UniversalTagType getUniversalTagType() const;

/**
* @return The ASN.1 tag type value
*/
uint8_t getTagType() const { return m_TagType; }
uint8_t getTagType() const
{
return m_TagType;
}

/**
* @return The length of the record value
*/
size_t getValueLength() const { return m_ValueLength; }
size_t getValueLength() const
{
return m_ValueLength;
}

/**
* @return The total length of the record
*/
size_t getTotalLength() const { return m_TotalLength; }
size_t getTotalLength() const
{
return m_TotalLength;
}

/**
* @return A string representation of the record
Expand All @@ -180,8 +196,7 @@ namespace pcpp
* @tparam Asn1RecordType The type to cast to
* @return A pointer to the type after casting
*/
template <class Asn1RecordType>
Asn1RecordType* castAs()
template <class Asn1RecordType> Asn1RecordType* castAs()
{
auto result = dynamic_cast<Asn1RecordType*>(this);
if (result == nullptr)
Expand Down Expand Up @@ -240,7 +255,8 @@ namespace pcpp
* @param value A byte array of the tag value
* @param valueLen The length of the value byte array
*/
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value, size_t valueLen);
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t* value,
size_t valueLen);

/**
* A constructor to create a generic record
Expand All @@ -256,7 +272,11 @@ namespace pcpp
/**
* @return A pointer to the tag value
*/
const uint8_t* getValue() { decodeValueIfNeeded(); return m_Value; }
const uint8_t* getValue()
{
decodeValueIfNeeded();
return m_Value;
}

protected:
Asn1GenericRecord() = default;
Expand Down Expand Up @@ -285,21 +305,27 @@ namespace pcpp
* @param tagType The record tag type value
* @param subRecords A list of sub-records to assign as the record value
*/
explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector<Asn1Record*>& subRecords);
explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
const std::vector<Asn1Record*>& subRecords);

/**
* A constructor to create a constructed record
* @param tagClass The record tag class
* @param tagType The record tag type value
* @param subRecords A PointerVector of sub-records to assign as the record value
*/
explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector<Asn1Record>& subRecords);
explicit Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType,
const PointerVector<Asn1Record>& subRecords);

/**
* @return A reference to the list of sub-records. It's important to note that any modifications made to
* this list will directly affect the internal structure
*/
PointerVector<Asn1Record>& getSubRecords() { decodeValueIfNeeded(); return m_SubRecords; };
PointerVector<Asn1Record>& getSubRecords()
{
decodeValueIfNeeded();
return m_SubRecords;
};

protected:
Asn1ConstructedRecord() = default;
Expand All @@ -309,8 +335,7 @@ namespace pcpp

std::vector<std::string> toStringList() override;

template<typename Iterator>
void init(Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
template <typename Iterator> void init(Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
{
m_TagType = tagType;
m_TagClass = tagClass;
Expand All @@ -328,6 +353,7 @@ namespace pcpp
m_ValueLength = recordValueLength;
m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
}

private:
PointerVector<Asn1Record> m_SubRecords;
};
Expand Down Expand Up @@ -414,7 +440,11 @@ namespace pcpp
/**
* @return The integer value of this record
*/
uint32_t getValue() { decodeValueIfNeeded(); return m_Value; }
uint32_t getValue()
{
decodeValueIfNeeded();
return m_Value;
}

protected:
Asn1IntegerRecord() = default;
Expand Down Expand Up @@ -463,16 +493,20 @@ namespace pcpp
explicit Asn1OctetStringRecord(const std::string& value);

/**
* A constructor to create a record of type Octet String from a non-printable value
* @param value A byte array to set as the record value
* A constructor to create a record of type Octet String from a non-printable value
* @param value A byte array to set as the record value
* @param valueLength The length of the byte array
*/
*/
explicit Asn1OctetStringRecord(const uint8_t* value, size_t valueLength);

/**
* @return The string value of this record
*/
std::string getValue() { decodeValueIfNeeded(); return m_Value; };
std::string getValue()
{
decodeValueIfNeeded();
return m_Value;
};

protected:
void decodeValue(uint8_t* data, bool lazy) override;
Expand Down Expand Up @@ -505,7 +539,11 @@ namespace pcpp
/**
* @return The boolean value of this record
*/
bool getValue() { decodeValueIfNeeded(); return m_Value; };
bool getValue()
{
decodeValueIfNeeded();
return m_Value;
};

protected:
void decodeValue(uint8_t* data, bool lazy) override;
Expand Down Expand Up @@ -534,7 +572,11 @@ namespace pcpp
Asn1NullRecord();

protected:
void decodeValue(uint8_t* data, bool lazy) override {}
std::vector<uint8_t> encodeValue() const override { return {}; }
void decodeValue(uint8_t* data, bool lazy) override
{}
std::vector<uint8_t> encodeValue() const override
{
return {};
}
};
}
} // namespace pcpp
Loading

0 comments on commit 316ea6e

Please sign in to comment.