935 lines
34 KiB
C++
935 lines
34 KiB
C++
/**
|
|
* @file source/ActivMiddleware/Misc/PcapParser.h
|
|
*
|
|
* @brief Header file for the pcap parser class and functions.
|
|
*/
|
|
|
|
#if (!defined ACTIV_PCAP_PARSER_H)
|
|
#define ACTIV_PCAP_PARSER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/ActivBase.h"
|
|
#include "ActivMiddleware/ActivBase/File.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/FieldTypes/DateTime.h"
|
|
|
|
#include "ActivMiddleware/ActivFramework/UiIo.h"
|
|
|
|
#include "ActivMiddleware/Misc/Fmt.h"
|
|
|
|
namespace Activ
|
|
{
|
|
|
|
namespace Pcap
|
|
{
|
|
|
|
//! Parser class.
|
|
class Parser
|
|
{
|
|
public:
|
|
//! List of supported protocols
|
|
enum LinkLayer
|
|
{
|
|
LINK_LAYER_UNDEFINED, // Any other unsupported protocol
|
|
LINK_LAYER_ETHERNET, // IEEE 802.3 Ethernet protocol
|
|
LINK_LAYER_ETHERNET_VLAN, // IEEE 802.1Q Ethernet VLAN protocol
|
|
LINK_LAYER_ETHERNET_QINQ // IEEE 802.1ad Ethernet QinQ protocol
|
|
};
|
|
|
|
enum InternetLayer
|
|
{
|
|
INTERNET_LAYER_UNDEFINED, // Any other unsupported protocol
|
|
INTERNET_LAYER_IP_V4, // IPv4 protocol
|
|
INTERNET_LAYER_IP_V6 // IPv6 protocol (options are currently not supported)
|
|
};
|
|
|
|
enum TransportLayer
|
|
{
|
|
TRANSPORT_LAYER_UNDEFINED, // Any other unsupported protocol
|
|
TRANSPORT_LAYER_UDP, // UDP protocol
|
|
TRANSPORT_LAYER_TCP // TCP protocol
|
|
};
|
|
|
|
//! Pcap file header class.
|
|
class PcapFileHeader
|
|
{
|
|
public:
|
|
//! Clean up old values.
|
|
void Reset();
|
|
|
|
uint32_t m_endianNumber; ///< determined if the file is written in big or little endian
|
|
uint16_t m_versionMajor; ///< major version number of the file format
|
|
uint16_t m_versionMinor; ///< minor version number of the file format
|
|
int32_t m_timeZoneOffset; ///< GMT to local time zone correction in seconds, always 0
|
|
uint32_t m_accuracy; ///< accuracy of timestamps, always 0
|
|
uint32_t m_snapshotLenght; ///< the maximum amount of byte captured for each datagram
|
|
uint32_t m_linkLayerType; ///< link layer header type (only 1, i.e. ethernet, is supported)
|
|
bool m_isSameEndian; ///< Whether the file is written in the same endian as this machine
|
|
bool m_isNanoseconds; ///< Whether timestamps are in nanoseconds or microseconds
|
|
};
|
|
|
|
//! Pcap datagram header class.
|
|
class PcapDatagramHeader
|
|
{
|
|
public:
|
|
//! Clean up old values.
|
|
void Reset();
|
|
|
|
DateTime m_timestamp; ///< timestamp of when the packet was captured
|
|
uint32_t m_savedLength; ///< the number of bytes saves for this datagram
|
|
uint32_t m_originalLength; ///< the original legth of the datagram
|
|
};
|
|
|
|
//! Ethernet header class.
|
|
class EthernetHeader
|
|
{
|
|
public:
|
|
//! Clean up old values.
|
|
void Reset();
|
|
|
|
String m_sourceMacAddress; ///< the MAC address of the source
|
|
String m_destinationMacAddress; ///< the MAC address of the recipient
|
|
LinkLayer m_ethernetType; ///< ethernet protocol type (see LinkLayer enum for values)
|
|
};
|
|
|
|
//! IP header class.
|
|
class IpHeader
|
|
{
|
|
public:
|
|
//! Clean up old values.
|
|
void Reset();
|
|
|
|
String m_sourceIpAddress; ///< the IP address of the source
|
|
String m_destinationIpAddress; ///< the IP address of the recipient
|
|
uint16_t m_packetLength; ///< the packet length. In IPv4 this includes the header, in IPv6 it doesn't
|
|
InternetLayer m_ipType; ///< IP protocol type (see InternetLayer enum for values)
|
|
};
|
|
|
|
//! Transport layer header class.
|
|
class TransportHeader
|
|
{
|
|
public:
|
|
//! Clean up old values.
|
|
void Reset();
|
|
|
|
uint16_t m_sourcePort; ///< the port of the source
|
|
uint16_t m_destinationPort; ///< the port of the recipient
|
|
uint16_t m_packetLength; ///< the packet length. UDP has an explicit length that includes the header, for TCP this field is a data offset
|
|
TransportLayer m_transportType; ///< either UDP or TCP
|
|
};
|
|
|
|
//! Headers class, contains a collection of all the other headers.
|
|
class Headers
|
|
{
|
|
public:
|
|
PcapDatagramHeader m_pcapDatagramHeader; ///< The pcap header.
|
|
EthernetHeader m_ethernetHeader; ///< Link layer header.
|
|
IpHeader m_ipHeader; ///< Internet layer header.
|
|
TransportHeader m_transportHeader; ///< Transport layer header.
|
|
};
|
|
|
|
//! Constructor.
|
|
Parser();
|
|
|
|
//! Destructor.
|
|
~Parser();
|
|
|
|
/**
|
|
* @brief Open a pcap file.
|
|
*
|
|
* @param fileName the pcap file to open
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval ...
|
|
*/
|
|
StatusCode OpenPcapFile(const String &fileName);
|
|
|
|
/**
|
|
* @brief Open a pcap file.
|
|
*
|
|
* @param fileName the pcap file to open
|
|
* @param pcapFileHeader returns the pcap file header
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval ...
|
|
*/
|
|
StatusCode OpenPcapFile(const String &fileName, PcapFileHeader *&pcapFileHeader);
|
|
|
|
//! Close a pcap file.
|
|
StatusCode ClosePcapFile();
|
|
|
|
//! Return true if file is open.
|
|
bool IsPcapFileOpen();
|
|
|
|
/**
|
|
* @brief Parse the next datagram in the file and returns the payload. The pPayload pointer returned may be invalidated by successive calls to this method.
|
|
*
|
|
* @param pPayload the payload after pcap and all networks layers have been removed
|
|
* @param payloadLength the length of the payload as recorded in the pcap file
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_NOT_INITIALIZED
|
|
* @retval STATUS_CODE_INVALID_LENGTH
|
|
* @retval STATUS_CODE_IGNORED
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParseNextDatagram(const byte_t *&pPayload, size_t &payloadLength);
|
|
|
|
/**
|
|
* @brief Parse the next datagram in the file and returns the payload. The pPayload and headers pointers returned may be invalidated by successive calls to this method.
|
|
*
|
|
* @param pPayload the payload after pcap and all networks layers have been removed
|
|
* @param payloadLength the length of the payload as recorded in the pcap file
|
|
* @param headers returns the headers that have been parsed
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_NOT_INITIALIZED
|
|
* @retval STATUS_CODE_INVALID_LENGTH
|
|
* @retval STATUS_CODE_IGNORED
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParseNextDatagram(const byte_t *&pPayload, size_t &payloadLength, Headers *&headers);
|
|
|
|
/**
|
|
* @brief Move to the beginning of the file, but after the pcap file header.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
StatusCode MoveToFirstDatagram();
|
|
|
|
private:
|
|
/**
|
|
* @brief Parse the pcap header of the next datagram.
|
|
*
|
|
* @param pDatagram the datagram after the pcap header has beeen removed
|
|
* @param datagramLength the length of the datagram as recorded in the pcap file
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_NOT_INITIALIZED
|
|
* @retval STATUS_CODE_INVALID_LENGTH
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParsePcapHeader(const byte_t *&pDatagram, size_t &datagramLength);
|
|
|
|
/**
|
|
* @brief Parse link layer header.
|
|
*
|
|
* @param pDatagram the datagram to parse
|
|
* @param datagramLength the length of the datagram as recorded in the pcap file
|
|
* @param pPayload the payload after the ethernet header has been removed
|
|
* @param payloadLength the length of the payload as recorded in the pcap file
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_IGNORED
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParseLinkHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload, size_t &payloadLength);
|
|
|
|
/**
|
|
* @brief Parse internet layer header.
|
|
*
|
|
* @param pDatagram the datagram to parse
|
|
* @param datagramLength the length of the datagram as recorded in the pcap file
|
|
* @param pPayload the payload after the IP header has been removed
|
|
* @param payloadLength the length of the payload as recorded in the pcap file
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_IGNORED
|
|
* @retval STATUS_CODE_INVALID_LENGTH
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParseInternetHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload, size_t &payloadLength);
|
|
|
|
/**
|
|
* @brief Parse transport layer header.
|
|
*
|
|
* @param pDatagram the datagram to parse
|
|
* @param datagramLength the length of the datagram as recorded in the pcap file
|
|
* @param pPayload the payload after the TCP or UDP header has been removed
|
|
* @param payloadLength the length of the payload as recorded in the pcap file
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval STATUS_CODE_IGNORED
|
|
* @retval STATUS_CODE_INVALID_LENGTH
|
|
* @retval ...
|
|
*/
|
|
StatusCode ParseTransportHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload, size_t &payloadLength);
|
|
|
|
/**
|
|
* @brief Refill the buffer from the pcap file.
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_END_OF_DATA
|
|
* @retval ...
|
|
*/
|
|
StatusCode ReadPcapFile();
|
|
|
|
/**
|
|
* @brief Parse the fields in the pcap file header.
|
|
*
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
template<MessageHandler::Endian ENDIAN>
|
|
StatusCode ParsePcapFileHeader();
|
|
|
|
/**
|
|
* @brief Parse the fields in the pcap datagram header.
|
|
* @retval STAUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
template<MessageHandler::Endian ENDIAN>
|
|
StatusCode ParsePcapDatagramHeader();
|
|
|
|
PcapFileHeader m_pcapFileHeader; ///< Pcap file header.
|
|
Headers m_headers; ///< Datagram headers.
|
|
|
|
// See https://wiki.wireshark.org/Development/LibpcapFileFormat for the meaning of these values.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint32_t SAME_ENDIAN_MICRO = 0xa1b2c3d4; ///< Timestamps in microseconds, same endian as file writer.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint32_t SWAP_ENDIAN_MICRO = 0xd4c3b2a1; ///< Timestamps in microseconds, different endian than the file writer.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint32_t SAME_ENDIAN_NANO = 0xa1b23c4d; ///< Timestamps in nanoseconds, same endian as the file writer.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint32_t SWAP_ENDIAN_NANO = 0x4d3cb2a1; ///< Timestaps in nanoseconds, different endian than the file writer.
|
|
|
|
// See http://www.tcpdump.org/linktypes.html for the complete list of types.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint32_t LINK_TYPE_ETHERNET = 1; ///< The supported link layer protocol.
|
|
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t PCAP_FILE_HEADER_SIZE = 24; ///< Size of the pcap file header.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t PCAP_DATAGRAM_HEADER_SIZE = 16; ///< Size of the pcap datagram header.
|
|
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t ETHERNET_HEADER_LEN = 14; ///< Ethernet header size without vlan.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t IPV4_HEADER_LEN = 20; ///< IPv4 header length without options.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t IPV6_HEADER_LEN = 40; ///< IPv6 header length without options.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t UDP_HEADER_LEN = 8; ///< UDP header length.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t TCP_HEADER_LEN = 20; ///< TCP header length without options.
|
|
|
|
// EtherType field, see https://en.wikipedia.org/wiki/EtherType for all available values.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint16_t VLAN_TAG = 0x8100; ///< Identifies VLAN ethernet headers, protocol IEEE 802.1Q.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint16_t QINQ_TAG = 0x88A8; ///< Identifies VLAN ethernet headers incapsulated into VLAN headers, protocol IEEE 802.1ad.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint16_t IPV4_TAG = 0x0800; ///< The next header is IPv4.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST uint16_t IPV6_TAG = 0x86DD; ///< The next header is IPv6.
|
|
|
|
// IP protocol byte, see https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers for all available values.
|
|
// See https://en.wikipedia.org/wiki/IPv4 for the position of the protocol byte in the IPv4 header.
|
|
// See https://en.wikipedia.org/wiki/IPv6_packet for the position of the protocol byte in the IPv6 header.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST byte_t TCP_TAG = 0x06; ///< Identifies the TCP protocol.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST byte_t UDP_TAG = 0x11; ///< Identifies the UDP protocol.
|
|
static ACTIV_BOOST_CONSTEXPR_OR_CONST size_t BUFFER_SIZE = 1024 * 1024; ///< 1 MB, buffer size.
|
|
|
|
byte_t * m_pBuffer; ///< The buffer used to store datagrams.
|
|
size_t m_bufferCurrentPosition; ///< Position in the buffer at the start of the current pcap datagram header.
|
|
size_t m_bufferEndPosition; ///< End of the buffer, can be less than BUFFER_SIZE.
|
|
File m_pcapFile; ///< File in pcap format.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void Parser::PcapFileHeader::Reset()
|
|
{
|
|
m_endianNumber = 0;
|
|
m_versionMajor = 0;
|
|
m_versionMinor = 0;
|
|
m_timeZoneOffset = 0;
|
|
m_accuracy = 0;
|
|
m_snapshotLenght = 0;
|
|
m_linkLayerType = 0;
|
|
m_isSameEndian = true;
|
|
m_isNanoseconds = false;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void Parser::PcapDatagramHeader::Reset()
|
|
{
|
|
m_timestamp.Reset();
|
|
m_savedLength = 0;
|
|
m_originalLength = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void Parser::EthernetHeader::Reset()
|
|
{
|
|
m_sourceMacAddress = "";
|
|
m_destinationMacAddress = "";
|
|
m_ethernetType = LINK_LAYER_UNDEFINED;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void Parser::IpHeader::Reset()
|
|
{
|
|
m_sourceIpAddress = "";
|
|
m_destinationIpAddress = "";
|
|
m_packetLength = 0;
|
|
m_ipType = INTERNET_LAYER_UNDEFINED;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void Parser::TransportHeader::Reset()
|
|
{
|
|
m_sourcePort = 0;
|
|
m_destinationPort = 0;
|
|
m_packetLength = 0;
|
|
m_transportType = TRANSPORT_LAYER_UNDEFINED;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline Parser::Parser() :
|
|
m_pBuffer(new byte_t[BUFFER_SIZE]),
|
|
m_bufferCurrentPosition(0),
|
|
m_bufferEndPosition(0)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline Parser::~Parser()
|
|
{
|
|
delete[] m_pBuffer;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::OpenPcapFile(const String &fileName)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, m_pcapFile.Open(fileName, File::AccessFlags::Read(), File::ShareFlags::Read()));
|
|
|
|
// Initialize.
|
|
m_bufferCurrentPosition = 0;
|
|
m_bufferEndPosition = 0;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ReadPcapFile());
|
|
|
|
if (PCAP_FILE_HEADER_SIZE > m_bufferEndPosition)
|
|
return STATUS_CODE_END_OF_DATA;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_LOCAL, uint32_t>(
|
|
m_pcapFileHeader.m_endianNumber, m_pBuffer, sizeof(uint32_t))));
|
|
|
|
switch (m_pcapFileHeader.m_endianNumber)
|
|
{
|
|
case SAME_ENDIAN_MICRO:
|
|
m_pcapFileHeader.m_isSameEndian = true;
|
|
m_pcapFileHeader.m_isNanoseconds = false;
|
|
break;
|
|
|
|
case SWAP_ENDIAN_MICRO:
|
|
m_pcapFileHeader.m_isSameEndian = false;
|
|
m_pcapFileHeader.m_isNanoseconds = false;
|
|
break;
|
|
|
|
case SAME_ENDIAN_NANO:
|
|
m_pcapFileHeader.m_isSameEndian = true;
|
|
m_pcapFileHeader.m_isNanoseconds = true;
|
|
break;
|
|
|
|
case SWAP_ENDIAN_NANO:
|
|
m_pcapFileHeader.m_isSameEndian = false;
|
|
m_pcapFileHeader.m_isNanoseconds = true;
|
|
break;
|
|
}
|
|
|
|
// Parse the rest of the header.
|
|
if (m_pcapFileHeader.m_isSameEndian)
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParsePcapFileHeader<MessageHandler::ENDIAN_LOCAL>());
|
|
else
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParsePcapFileHeader<MessageHandler::ENDIAN_ALIEN>());
|
|
|
|
m_bufferCurrentPosition = PCAP_FILE_HEADER_SIZE;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::OpenPcapFile(const String &fileName, PcapFileHeader *&pcapFileHeader)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, OpenPcapFile(fileName));
|
|
pcapFileHeader = &m_pcapFileHeader;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ClosePcapFile()
|
|
{
|
|
return m_pcapFile.Close();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool Parser::IsPcapFileOpen()
|
|
{
|
|
return m_pcapFile.IsOpen();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParseNextDatagram(const byte_t *&pPayload, size_t &payloadLength)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParsePcapHeader(pPayload, payloadLength));
|
|
|
|
const byte_t *pDatagram = pPayload;
|
|
size_t datagramLength = payloadLength;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParseLinkHeader(pDatagram, datagramLength, pPayload, payloadLength));
|
|
|
|
pDatagram = pPayload;
|
|
datagramLength = payloadLength;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParseInternetHeader(pDatagram, datagramLength, pPayload, payloadLength));
|
|
|
|
pDatagram = pPayload;
|
|
datagramLength = payloadLength;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParseTransportHeader(pDatagram, datagramLength, pPayload, payloadLength));
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParseNextDatagram(const byte_t *&pPayload, size_t &payloadLength, Headers *&headers)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParseNextDatagram(pPayload, payloadLength));
|
|
headers = &m_headers;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParsePcapHeader(const byte_t *&pDatagram, size_t &datagramLength)
|
|
{
|
|
if (!m_pcapFile.IsOpen())
|
|
return STATUS_CODE_NOT_INITIALIZED;
|
|
|
|
// Reset previous values.
|
|
m_headers.m_pcapDatagramHeader.Reset();
|
|
|
|
// Parse Pcap datagram header.
|
|
if ((m_bufferCurrentPosition + PCAP_DATAGRAM_HEADER_SIZE) > m_bufferEndPosition)
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ReadPcapFile());
|
|
|
|
if (m_pcapFileHeader.m_isSameEndian)
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParsePcapDatagramHeader<MessageHandler::ENDIAN_LOCAL>());
|
|
else
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ParsePcapDatagramHeader<MessageHandler::ENDIAN_ALIEN>());
|
|
|
|
// The datagram must have a valid length.
|
|
if (m_headers.m_pcapDatagramHeader.m_savedLength > m_pcapFileHeader.m_snapshotLenght)
|
|
return STATUS_CODE_INVALID_LENGTH;
|
|
|
|
if (m_headers.m_pcapDatagramHeader.m_savedLength > m_headers.m_pcapDatagramHeader.m_originalLength)
|
|
return STATUS_CODE_INVALID_LENGTH;
|
|
|
|
m_bufferCurrentPosition += PCAP_DATAGRAM_HEADER_SIZE;
|
|
|
|
// Read the datagram.
|
|
if ((m_bufferCurrentPosition + m_headers.m_pcapDatagramHeader.m_savedLength) > m_bufferEndPosition)
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, ReadPcapFile());
|
|
|
|
size_t datagramStart = m_bufferCurrentPosition;
|
|
m_bufferCurrentPosition += m_headers.m_pcapDatagramHeader.m_savedLength;
|
|
|
|
datagramLength = m_headers.m_pcapDatagramHeader.m_savedLength;
|
|
pDatagram = &m_pBuffer[datagramStart];
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParseLinkHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload,
|
|
size_t &payloadLength)
|
|
{
|
|
if (LINK_TYPE_ETHERNET != m_pcapFileHeader.m_linkLayerType)
|
|
return STATUS_CODE_IGNORED;
|
|
|
|
uint16_t headerLength = 0;
|
|
|
|
// Reset old values.
|
|
m_headers.m_ethernetHeader.Reset();
|
|
|
|
// Parse ethernet header.
|
|
if (ETHERNET_HEADER_LEN > datagramLength)
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
uint16_t ethernetTag = 0;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(ethernetTag,
|
|
&pDatagram[ETHERNET_HEADER_LEN - 2], sizeof(uint16_t))));
|
|
|
|
headerLength += ETHERNET_HEADER_LEN;
|
|
m_headers.m_ethernetHeader.m_ethernetType = LINK_LAYER_ETHERNET;
|
|
|
|
// IEEE 802.1Q ethernet header there are 4 additional bytes to parse.
|
|
if (VLAN_TAG == ethernetTag)
|
|
{
|
|
if (datagramLength < static_cast<size_t>(headerLength + 4))
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(ethernetTag,
|
|
&pDatagram[ETHERNET_HEADER_LEN + 2], sizeof(uint16_t))));
|
|
|
|
headerLength += 4;
|
|
m_headers.m_ethernetHeader.m_ethernetType = LINK_LAYER_ETHERNET_VLAN;
|
|
}
|
|
|
|
// IEEE 802.1ad ethernet header there are 8 additional bytes to parse.
|
|
if (QINQ_TAG == ethernetTag)
|
|
{
|
|
if (datagramLength < static_cast<size_t>(headerLength + 8))
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(ethernetTag,
|
|
&pDatagram[ETHERNET_HEADER_LEN + 6], sizeof(uint16_t))));
|
|
|
|
headerLength += 8;
|
|
m_headers.m_ethernetHeader.m_ethernetType = LINK_LAYER_ETHERNET_QINQ;
|
|
}
|
|
|
|
// Parse the MAC addresses.
|
|
m_headers.m_ethernetHeader.m_destinationMacAddress = Fmt::Format("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
|
pDatagram[0], pDatagram[1], pDatagram[2], pDatagram[3], pDatagram[4], pDatagram[5]);
|
|
m_headers.m_ethernetHeader.m_sourceMacAddress = Fmt::Format("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
|
pDatagram[6], pDatagram[7], pDatagram[8], pDatagram[9], pDatagram[10], pDatagram[11]);
|
|
|
|
// Reset the Ip header and update the protocol type.
|
|
m_headers.m_ipHeader.Reset();
|
|
|
|
if (IPV4_TAG == ethernetTag)
|
|
m_headers.m_ipHeader.m_ipType = INTERNET_LAYER_IP_V4;
|
|
|
|
if (IPV6_TAG == ethernetTag)
|
|
m_headers.m_ipHeader.m_ipType = INTERNET_LAYER_IP_V6;
|
|
|
|
payloadLength = datagramLength - headerLength;
|
|
pPayload = ((payloadLength > 0) ? &pDatagram[headerLength] : 0);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParseInternetHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload,
|
|
size_t &payloadLength)
|
|
{
|
|
if (INTERNET_LAYER_UNDEFINED == m_headers.m_ipHeader.m_ipType)
|
|
return STATUS_CODE_IGNORED;
|
|
|
|
uint16_t headerLength = 0;
|
|
|
|
// Resets transport header.
|
|
m_headers.m_transportHeader.Reset();
|
|
|
|
if (INTERNET_LAYER_IP_V4 == m_headers.m_ipHeader.m_ipType)
|
|
{
|
|
if (IPV4_HEADER_LEN > datagramLength)
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
if (TCP_TAG == pDatagram[9])
|
|
m_headers.m_transportHeader.m_transportType = TRANSPORT_LAYER_TCP;
|
|
|
|
if (UDP_TAG == pDatagram[9])
|
|
m_headers.m_transportHeader.m_transportType = TRANSPORT_LAYER_UDP;
|
|
|
|
// Check if the ip header contains options and skip them.
|
|
byte_t ihl = 15 & pDatagram[0];
|
|
|
|
if (5 > ihl)
|
|
return STATUS_CODE_INVALID_LENGTH;
|
|
|
|
if (datagramLength < (ihl * sizeof(uint32_t)))
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
headerLength += ihl * sizeof(uint32_t);
|
|
|
|
// Parse IP addresses.
|
|
m_headers.m_ipHeader.m_sourceIpAddress =
|
|
Fmt::Format("{}.{}.{}.{}", pDatagram[12], pDatagram[13], pDatagram[14], pDatagram[15]);
|
|
m_headers.m_ipHeader.m_destinationIpAddress =
|
|
Fmt::Format("{}.{}.{}.{}", pDatagram[16], pDatagram[17], pDatagram[18], pDatagram[19]);
|
|
|
|
// Parse packet length.
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(
|
|
m_headers.m_ipHeader.m_packetLength, &pDatagram[2], sizeof(uint16_t))));
|
|
}
|
|
|
|
// NOTE: does not support optional fields in IPv6, the packets with options will be ignored.
|
|
if (INTERNET_LAYER_IP_V6 == m_headers.m_ipHeader.m_ipType)
|
|
{
|
|
if (IPV6_HEADER_LEN > datagramLength)
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
if (TCP_TAG == pDatagram[6])
|
|
m_headers.m_transportHeader.m_transportType = TRANSPORT_LAYER_TCP;
|
|
|
|
if (UDP_TAG == pDatagram[6])
|
|
m_headers.m_transportHeader.m_transportType = TRANSPORT_LAYER_UDP;
|
|
|
|
headerLength += IPV6_HEADER_LEN;
|
|
|
|
// Parse IP addresses, 16 bytes format.
|
|
m_headers.m_ipHeader.m_sourceIpAddress =
|
|
(Fmt::Format("{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}",
|
|
pDatagram[8], pDatagram[9], pDatagram[10], pDatagram[11],
|
|
pDatagram[12], pDatagram[13], pDatagram[14], pDatagram[15],
|
|
pDatagram[16], pDatagram[17], pDatagram[18], pDatagram[19],
|
|
pDatagram[20], pDatagram[21], pDatagram[22], pDatagram[23]));
|
|
|
|
m_headers.m_ipHeader.m_destinationIpAddress =
|
|
(Fmt::Format("{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}",
|
|
pDatagram[24], pDatagram[25], pDatagram[26], pDatagram[27],
|
|
pDatagram[28], pDatagram[29], pDatagram[30], pDatagram[31],
|
|
pDatagram[32], pDatagram[33], pDatagram[34], pDatagram[35],
|
|
pDatagram[36], pDatagram[37], pDatagram[38], pDatagram[39]));
|
|
|
|
// Parse the packet length.
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(
|
|
m_headers.m_ipHeader.m_packetLength, &pDatagram[4], sizeof(uint16_t))));
|
|
}
|
|
|
|
// Use the length from the IP header, rather than datagramLength to calculate the real payload length.
|
|
// This will remove any padding bytes from the end of the payload.
|
|
payloadLength = m_headers.m_ipHeader.m_packetLength - headerLength;
|
|
pPayload = ((payloadLength > 0) ? &pDatagram[headerLength] : 0);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ParseTransportHeader(const byte_t *pDatagram, size_t datagramLength, const byte_t *&pPayload,
|
|
size_t &payloadLength)
|
|
{
|
|
if (TRANSPORT_LAYER_UNDEFINED == m_headers.m_transportHeader.m_transportType)
|
|
return STATUS_CODE_IGNORED;
|
|
|
|
uint16_t headerLength = 0;
|
|
|
|
// Parse source and destination port, they are in the same locations in both TCP and UDP.
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(
|
|
m_headers.m_transportHeader.m_sourcePort, pDatagram, sizeof(uint16_t))));
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(
|
|
m_headers.m_transportHeader.m_destinationPort, &pDatagram[2], sizeof(uint16_t))));
|
|
|
|
if (TRANSPORT_LAYER_UDP == m_headers.m_transportHeader.m_transportType)
|
|
{
|
|
if (datagramLength < UDP_HEADER_LEN)
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<MessageHandler::ENDIAN_BIG, uint16_t>(
|
|
m_headers.m_transportHeader.m_packetLength, &pDatagram[4], sizeof(uint16_t))));
|
|
|
|
headerLength += UDP_HEADER_LEN;
|
|
}
|
|
|
|
if (TRANSPORT_LAYER_TCP == m_headers.m_transportHeader.m_transportType)
|
|
{
|
|
if (datagramLength < TCP_HEADER_LEN)
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
|
|
// Check if the TCP header contains options and skip them.
|
|
byte_t dataOffset = pDatagram[12] >> 4;
|
|
|
|
if (5 > dataOffset)
|
|
return STATUS_CODE_INVALID_LENGTH;
|
|
|
|
if (datagramLength < dataOffset * sizeof(uint32_t))
|
|
{
|
|
pPayload = 0;
|
|
payloadLength = 0;
|
|
return STATUS_CODE_END_OF_DATA;
|
|
}
|
|
headerLength += (dataOffset * sizeof(uint32_t));
|
|
m_headers.m_transportHeader.m_packetLength = dataOffset;
|
|
}
|
|
|
|
payloadLength = datagramLength - headerLength;
|
|
pPayload = ((payloadLength > 0) ? &pDatagram[headerLength] : 0);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::ReadPcapFile()
|
|
{
|
|
// If there are bytes left at the end of the buffer, copy them to the start of the buffer.
|
|
if ((0 < m_bufferCurrentPosition) && (m_bufferCurrentPosition < m_bufferEndPosition))
|
|
std::memcpy(m_pBuffer, &m_pBuffer[m_bufferCurrentPosition], m_bufferEndPosition - m_bufferCurrentPosition);
|
|
|
|
size_t bytesRead = 0;
|
|
size_t bytesToMove = m_bufferEndPosition - m_bufferCurrentPosition;
|
|
|
|
StatusCode statusCode = m_pcapFile.Read(&m_pBuffer[bytesToMove], BUFFER_SIZE - bytesToMove, bytesRead);
|
|
|
|
// Reached the end of the file.
|
|
if ((0 == bytesRead) && (STATUS_CODE_SUCCESS == statusCode))
|
|
statusCode = STATUS_CODE_END_OF_DATA;
|
|
|
|
if ((STATUS_CODE_SUCCESS != statusCode) && (STATUS_CODE_END_OF_DATA != statusCode))
|
|
{
|
|
m_bufferCurrentPosition = 0;
|
|
m_bufferEndPosition = 0;
|
|
}
|
|
|
|
m_bufferEndPosition = bytesRead + bytesToMove;
|
|
m_bufferCurrentPosition = 0;
|
|
|
|
return statusCode;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode Parser::MoveToFirstDatagram()
|
|
{
|
|
m_bufferCurrentPosition = 0;
|
|
m_bufferEndPosition = 0;
|
|
return m_pcapFile.SetPosition(PCAP_FILE_HEADER_SIZE);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageValidator::Endian ENDIAN>
|
|
inline StatusCode Parser::ParsePcapFileHeader()
|
|
{
|
|
size_t bufferIndex = sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint16_t>(m_pcapFileHeader.m_versionMajor,
|
|
&m_pBuffer[bufferIndex], sizeof(uint16_t))));
|
|
bufferIndex += sizeof(uint16_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint16_t>(m_pcapFileHeader.m_versionMinor,
|
|
&m_pBuffer[bufferIndex], sizeof(uint16_t))));
|
|
bufferIndex += sizeof(uint16_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopySignedBinaryIntegralToSignedBinaryIntegral<ENDIAN, int32_t>(m_pcapFileHeader.m_timeZoneOffset,
|
|
&m_pBuffer[bufferIndex], sizeof(int32_t))));
|
|
bufferIndex += sizeof(int32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(m_pcapFileHeader.m_accuracy,
|
|
&m_pBuffer[bufferIndex], sizeof(uint32_t))));
|
|
bufferIndex += sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(m_pcapFileHeader.m_snapshotLenght,
|
|
&m_pBuffer[bufferIndex], sizeof(uint32_t))));
|
|
bufferIndex += sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(m_pcapFileHeader.m_linkLayerType,
|
|
&m_pBuffer[bufferIndex], sizeof(uint32_t))));
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageValidator::Endian ENDIAN>
|
|
inline StatusCode Parser::ParsePcapDatagramHeader()
|
|
{
|
|
size_t bufferIndex = m_bufferCurrentPosition;
|
|
uint32_t time = 0;
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(time, &m_pBuffer[bufferIndex],
|
|
sizeof(uint32_t))));
|
|
|
|
m_headers.m_pcapDatagramHeader.m_timestamp.SetEpoch(time);
|
|
bufferIndex += sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(time, &m_pBuffer[bufferIndex],
|
|
sizeof(uint32_t))));
|
|
|
|
if (m_pcapFileHeader.m_isNanoseconds)
|
|
m_headers.m_pcapDatagramHeader.m_timestamp.AddMilliseconds(time / 1000000);
|
|
else
|
|
m_headers.m_pcapDatagramHeader.m_timestamp.AddMilliseconds(time / 1000);
|
|
|
|
bufferIndex += sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(
|
|
m_headers.m_pcapDatagramHeader.m_savedLength, &m_pBuffer[bufferIndex], sizeof(uint32_t))));
|
|
|
|
bufferIndex += sizeof(uint32_t);
|
|
|
|
ACTIV_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
|
|
(MessageValidator::CopyUnsignedBinaryIntegralToUnsignedBinaryIntegral<ENDIAN, uint32_t>(
|
|
m_headers.m_pcapDatagramHeader.m_originalLength, &m_pBuffer[bufferIndex], sizeof(uint32_t))));
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
} // namespace Pcap
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_PCAP_PARSER_H)
|