forked from seladb/PcapPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create COTP * create COTP tests * Add COTP to README.md * small changes * add override * add comment to cotphdr * add comment to cotp * fix type * create COTP * create COTP tests * small changes * add override * add comment to cotphdr * add comment to cotp * fix type * Changed COTP value * Renaming to camel case (in parameters, constructor, ...) * Remove unnecessary elements, modify to check the values (type, length), modify the values in tests * add explicit to constructor and override to getOsiModellLayer * Update README.md with COTP * create COTP * create COTP tests * Add COTP to README.md * small changes * add override * add comment to cotphdr * add comment to cotp * fix type * create COTP * create COTP tests * small changes * add override * add comment to cotphdr * add comment to cotp * fix type * Changed COTP value * Renaming to camel case (in parameters, constructor, ...) * Remove unnecessary elements, modify to check the values (type, length), modify the values in tests * add explicit to constructor and override to getOsiModellLayer * Update README.md with COTP * create COTP * create COTP tests * create COTP * Changed COTP value * Remove unnecessary elements, modify to check the values (type, length), modify the values in tests * add explicit to constructor and override to getOsiModellLayer * add back new lines at the end of the files * added new lines, renaming and delete unnecessary things. * remove static keyword * remove static keyword * static keyword * delete unused parameters from TpktLayer.cpp * delete unused variables * modify test * rename cotp * rename tpkt_cotp files * modify comment * modify test --------- Co-authored-by: Vörös Vivien <[email protected]>
- Loading branch information
Showing
14 changed files
with
262 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#ifndef PCAPPLUSPLUS_COTPLAYER_H | ||
#define PCAPPLUSPLUS_COTPLAYER_H | ||
|
||
#include "EthLayer.h" | ||
#include "Layer.h" | ||
|
||
namespace pcpp | ||
{ | ||
|
||
/** | ||
* @struct cotphdr | ||
* Represents a COTP protocol header | ||
*/ | ||
#pragma pack(push, 1) | ||
typedef struct | ||
{ | ||
/** length */ | ||
uint8_t length; | ||
/** PDU type identifier */ | ||
uint8_t pduType ; | ||
/** TPDU number sequence*/ | ||
uint8_t tpduNumber; | ||
} cotphdr; | ||
#pragma pack(pop) | ||
|
||
/** | ||
* @class CotpLayer | ||
* Represents a COTP (Connection Oriented Transport Protocol) | ||
*/ | ||
class CotpLayer : public Layer | ||
{ | ||
public: | ||
/** | ||
* A constructor that creates the layer from an existing packet raw data | ||
* @param[in] data A pointer to the raw data (will be casted to @ref cotphdr) | ||
* @param[in] dataLen Size of the data in bytes | ||
* @param[in] prevLayer A pointer to the previous layer | ||
* @param[in] packet A pointer to the Packet instance where layer will be stored in | ||
*/ | ||
CotpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet) | ||
: Layer(data, dataLen, prevLayer, packet) | ||
{ | ||
m_Protocol = COTP; | ||
} | ||
|
||
/** | ||
* A constructor that allocates a new COTP header | ||
* @param[in] tpduNumber Protocol TPDU number | ||
*/ | ||
explicit CotpLayer(uint8_t tpduNumber); | ||
|
||
virtual ~CotpLayer() {} | ||
|
||
/** | ||
* @return COTP length | ||
*/ | ||
uint8_t getLength() const; | ||
|
||
/** | ||
* @return COTP PDU type | ||
*/ | ||
uint8_t getPduType() const; | ||
|
||
/** | ||
* @return COTP TPDU number | ||
*/ | ||
uint8_t getTpduNumber() const; | ||
|
||
/** | ||
* @return Size of @ref cotphdr | ||
*/ | ||
size_t getHeaderLen() const override { return sizeof(cotphdr); } | ||
|
||
/** | ||
* Set the value of the length | ||
* @param[in] length The value of the length | ||
*/ | ||
void setLength(uint8_t length) const; | ||
|
||
/** | ||
* Set the value of the version | ||
* @param[in] pduType The number of the PDU type | ||
*/ | ||
void setPduType(uint8_t pduType) const; | ||
|
||
/** | ||
* Set the value of the version | ||
* @param[in] tpduNumber The value of the TPDU number | ||
*/ | ||
void setTpduNumber(uint8_t tpduNumber) const; | ||
|
||
/** | ||
* Does nothing for this layer | ||
*/ | ||
void computeCalculateFields() override {} | ||
|
||
/** | ||
* Currently parses the rest of the packet as a generic payload (PayloadLayer) | ||
*/ | ||
void parseNextLayer() override; | ||
|
||
/** | ||
* A static method that takes a byte array and detects whether it is a COTP | ||
* @param[in] data A byte array | ||
* @param[in] dataSize The byte array size (in bytes) | ||
* @return True if the data looks like a valid COTP layer | ||
*/ | ||
static bool isDataValid(const uint8_t *data, size_t dataSize); | ||
|
||
std::string toString() const override; | ||
|
||
OsiModelLayer getOsiModelLayer() const override { return OsiModelTransportLayer; } | ||
|
||
private: | ||
cotphdr *getCotpHeader() const { return (cotphdr *)m_Data; } | ||
}; | ||
|
||
} // namespace pcpp | ||
|
||
#endif // PCAPPLUSPLUS_COTPLAYER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "../header/CotpLayer.h" | ||
#include "EndianPortable.h" | ||
#include <PayloadLayer.h> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <sstream> | ||
|
||
namespace pcpp | ||
{ | ||
|
||
pcpp::CotpLayer::CotpLayer(uint8_t tpduNumber) | ||
{ | ||
const size_t headerLen = sizeof(cotphdr); | ||
m_DataLen = headerLen; | ||
m_Data = new uint8_t[headerLen]; | ||
memset(m_Data, 0, headerLen); | ||
cotphdr *cotpHdr = (cotphdr *)m_Data; | ||
cotpHdr->length = 0x02; | ||
cotpHdr->pduType = 0x0f; | ||
cotpHdr->tpduNumber = tpduNumber; | ||
m_Protocol = COTP; | ||
} | ||
|
||
std::string CotpLayer::toString() const | ||
{ | ||
return "Cotp Layer"; | ||
} | ||
|
||
uint8_t CotpLayer::getLength() const { return getCotpHeader()->length; } | ||
|
||
uint8_t CotpLayer::getPduType() const { return getCotpHeader()->pduType; } | ||
|
||
uint8_t CotpLayer::getTpduNumber() const { return getCotpHeader()->tpduNumber; } | ||
|
||
void CotpLayer::setLength(uint8_t length) const { getCotpHeader()->length = length; } | ||
|
||
void CotpLayer::setPduType(uint8_t pduType) const { getCotpHeader()->pduType = pduType; } | ||
|
||
void CotpLayer::setTpduNumber(uint8_t tpduNumber) const { getCotpHeader()->tpduNumber = tpduNumber; } | ||
|
||
bool CotpLayer::isDataValid(const uint8_t *data, size_t dataSize) | ||
{ | ||
if (!data || dataSize < sizeof(cotphdr)) | ||
return false; | ||
|
||
return data[1] == 0xf0 && data[0] == 2; | ||
} | ||
|
||
void CotpLayer::parseNextLayer() | ||
{ | ||
size_t headerLen = getHeaderLen(); | ||
if (m_DataLen <= headerLen) | ||
return; | ||
|
||
uint8_t *payload = m_Data + headerLen; | ||
size_t payloadLen = m_DataLen - headerLen; | ||
|
||
m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet); | ||
} | ||
} // namespace pcpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "../TestDefinition.h" | ||
#include "../Utils/TestUtils.h" | ||
#include "CotpLayer.h" | ||
#include "Packet.h" | ||
#include "SystemUtils.h" | ||
|
||
using namespace std; | ||
|
||
PTF_TEST_CASE(CotpLayerTest) | ||
{ | ||
timeval time; | ||
gettimeofday(&time, nullptr); | ||
|
||
READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/tpkt_cotp.dat"); | ||
|
||
pcpp::Packet cotpPacket(&rawPacket1); | ||
PTF_ASSERT_TRUE(cotpPacket.isPacketOfType(pcpp::COTP)); | ||
auto cotpLayer = cotpPacket.getLayerOfType<pcpp::CotpLayer>(); | ||
PTF_ASSERT_NOT_NULL(cotpLayer); | ||
PTF_ASSERT_EQUAL(cotpLayer->getNextLayer()->getProtocol(), pcpp::GenericPayload, enum); | ||
PTF_ASSERT_EQUAL(cotpLayer->getHeaderLen(), 3); | ||
PTF_ASSERT_EQUAL(cotpLayer->getLength(), 0x02); | ||
PTF_ASSERT_EQUAL(cotpLayer->getPduType(), 0xf0); | ||
PTF_ASSERT_EQUAL(cotpLayer->getTpduNumber(), 0x80); | ||
PTF_ASSERT_EQUAL(cotpLayer->toString(), "Cotp Layer"); | ||
|
||
pcpp::CotpLayer newCotpPacket(120); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getHeaderLen(), 3); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getLength(), 0x02); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getPduType(), 0x0f); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getTpduNumber(), 0x78); | ||
|
||
newCotpPacket.setLength((uint8_t)4); | ||
newCotpPacket.setPduType((uint8_t)210); | ||
newCotpPacket.setTpduNumber((uint8_t)125); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getLength(), 0x04); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getPduType(), 0xd2); | ||
PTF_ASSERT_EQUAL(newCotpPacket.getTpduNumber(), 0x7d); | ||
|
||
} // CotpLayerTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters