PcapPlusPlus  19.12
pcpp::DnsLayer Class Reference

#include <DnsLayer.h>

Inheritance diagram for pcpp::DnsLayer:
pcpp::Layer pcpp::IDataContainer

Public Member Functions

 DnsLayer (uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
 
 DnsLayer ()
 
 DnsLayer (const DnsLayer &other)
 
DnsLayeroperator= (const DnsLayer &other)
 
dnshdrgetDnsHeader () const
 
DnsQuerygetQuery (const std::string &name, bool exactMatch) const
 
DnsQuerygetFirstQuery () const
 
DnsQuerygetNextQuery (DnsQuery *query) const
 
size_t getQueryCount () const
 
DnsQueryaddQuery (const std::string &name, DnsType dnsType, DnsClass dnsClass)
 
DnsQueryaddQuery (DnsQuery *const copyQuery)
 
bool removeQuery (const std::string &queryNameToRemove, bool exactMatch)
 
bool removeQuery (DnsQuery *queryToRemove)
 
DnsResourcegetAnswer (const std::string &name, bool exactMatch) const
 
DnsResourcegetFirstAnswer () const
 
DnsResourcegetNextAnswer (DnsResource *answer) const
 
size_t getAnswerCount () const
 
DnsResourceaddAnswer (const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
 
DnsResourceaddAnswer (DnsResource *const copyAnswer)
 
bool removeAnswer (const std::string &answerNameToRemove, bool exactMatch)
 
bool removeAnswer (DnsResource *answerToRemove)
 
DnsResourcegetAuthority (const std::string &name, bool exactMatch) const
 
DnsResourcegetFirstAuthority () const
 
DnsResourcegetNextAuthority (DnsResource *authority) const
 
size_t getAuthorityCount () const
 
DnsResourceaddAuthority (const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
 
DnsResourceaddAuthority (DnsResource *const copyAuthority)
 
bool removeAuthority (const std::string &authorityNameToRemove, bool exactMatch)
 
bool removeAuthority (DnsResource *authorityToRemove)
 
DnsResourcegetAdditionalRecord (const std::string &name, bool exactMatch) const
 
DnsResourcegetFirstAdditionalRecord () const
 
DnsResourcegetNextAdditionalRecord (DnsResource *additionalRecord) const
 
size_t getAdditionalRecordCount () const
 
DnsResourceaddAdditionalRecord (const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
 
DnsResourceaddAdditionalRecord (const std::string &name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData *data)
 
DnsResourceaddAdditionalRecord (DnsResource *const copyAdditionalRecord)
 
bool removeAdditionalRecord (const std::string &additionalRecordNameToRemove, bool exactMatch)
 
bool removeAdditionalRecord (DnsResource *additionalRecordToRemove)
 
void parseNextLayer ()
 
size_t getHeaderLen () const
 
void computeCalculateFields ()
 
std::string toString () const
 
OsiModelLayer getOsiModelLayer () const
 
- Public Member Functions inherited from pcpp::Layer
virtual ~Layer ()
 
LayergetNextLayer () const
 
LayergetPrevLayer () const
 
ProtocolType getProtocol () const
 
uint8_t * getData () const
 
size_t getDataLen () const
 
uint8_t * getLayerPayload () const
 
size_t getLayerPayloadSize () const
 
bool isAllocatedToPacket () const
 
void copyData (uint8_t *toArr) const
 
uint8_t * getDataPtr (size_t offset=0) const
 

Static Public Member Functions

static const std::map< uint16_t, bool > * getDNSPortMap ()
 

Detailed Description

Represents the DNS protocol layer.
CURRENTLY ONLY DNS PARSING IS AVAILABLE. CREATING AND EDITING DNS ATTRIBUTES WILL BE ADDED LATER

Constructor & Destructor Documentation

◆ DnsLayer() [1/3]

pcpp::DnsLayer::DnsLayer ( uint8_t *  data,
size_t  dataLen,
Layer prevLayer,
Packet packet 
)

A constructor that creates the layer from an existing packet raw data

Parameters
[in]dataA pointer to the raw data
[in]dataLenSize of the data in bytes
[in]prevLayerA pointer to the previous layer
[in]packetA pointer to the Packet instance where layer will be stored in

◆ DnsLayer() [2/3]

pcpp::DnsLayer::DnsLayer ( )

A constructor that creates an empty DNS layer: all members of dnshdr are set to 0 and layer will contain no records

◆ DnsLayer() [3/3]

pcpp::DnsLayer::DnsLayer ( const DnsLayer other)

A copy constructor for this layer

Member Function Documentation

◆ addAdditionalRecord() [1/3]

DnsResource* pcpp::DnsLayer::addAdditionalRecord ( const std::string &  name,
DnsType  dnsType,
DnsClass  dnsClass,
uint32_t  ttl,
IDnsResourceData data 
)

Add a new DNS additional record to the layer

Parameters
[in]nameThe value that shall be set in the name field of the additional record
[in]dnsTypeThe value that shall be set in the DNS type field of the additional record
[in]dnsClassThe value that shall be set in the DNS class field of the additional record
[in]ttlThe value that shall be set in the 'time-to-leave' field of the additional record
[in]dataThe additional record data to be set. The type of the data should match the type of the DNS record (for example: DNS record of type A should have data of type IPv4DnsResourceData. Please see DnsResource::setData() for more info on this
Returns
A pointer to the newly created DNS additional record or NULL if additional record could not be created (an appropriate error log message will be printed in this case)

◆ addAdditionalRecord() [2/3]

DnsResource* pcpp::DnsLayer::addAdditionalRecord ( const std::string &  name,
DnsType  dnsType,
uint16_t  customData1,
uint32_t  customData2,
IDnsResourceData data 
)

Add a new DNS additional record to the layer that doesn't have DNS class and TTL. Instead these bytes may contains some arbitrary data. In the future I may add support for these kinds of additional data records. For now, these bytes are set as raw

Parameters
[in]nameThe value that shall be set in the name field of the additional record
[in]dnsTypeThe value that shall be set in the DNS type field of the additional record
[in]customData1Two bytes of the arbitrary data that will be set in the offset usually used for the DNS class
[in]customData2Four bytes of the arbitrary data that will be set in the offset usually used for the TTL
[in]dataThe additional record data to be set. The type of the data should match the type of the DNS record. (for example: DNS record of type A should have data of type IPv4DnsResourceData. Please see DnsResource::setData() for more info on this
Returns
A pointer to the newly created DNS additional record or NULL if additional record could not be created (an appropriate error log message will be printed in this case)

◆ addAdditionalRecord() [3/3]

DnsResource* pcpp::DnsLayer::addAdditionalRecord ( DnsResource *const  copyAdditionalRecord)

Add a new DNS additional record similar to an already existing DNS additional record. All additional record fields will be copied from the existing additional record param[in] copyAdditionalRecord The record to create the new record from. copyAdditionalRecord won't be changed in any way

Returns
A pointer to the newly created DNS additional record or NULL if query could not be created (an appropriate error log message will be printed in this case)

◆ addAnswer() [1/2]

DnsResource* pcpp::DnsLayer::addAnswer ( const std::string &  name,
DnsType  dnsType,
DnsClass  dnsClass,
uint32_t  ttl,
IDnsResourceData data 
)

Add a new DNS answer to the layer

Parameters
[in]nameThe value that shall be set in the name field of the answer
[in]dnsTypeThe value that shall be set in the DNS type field of the answer
[in]dnsClassThe value that shall be set in the DNS class field of the answer
[in]ttlThe value that shall be set in the 'time-to-leave' field of the answer
[in]dataThe answer data to be set. The type of the data should match the type of the DNS record (for example: DNS record of type A should have data of type IPv4DnsResourceData. Please see DnsResource::setData() for more info on this
Returns
A pointer to the newly created DNS answer or NULL if answer could not be created (an appropriate error log message will be printed in this case)

◆ addAnswer() [2/2]

DnsResource* pcpp::DnsLayer::addAnswer ( DnsResource *const  copyAnswer)

Add a new DNS answer similar to an already existing DNS answer. All answer fields will be copied from the existing answer param[in] copyAnswer The record to create the new record from. copyAnswer won't be changed in any way

Returns
A pointer to the newly created DNS answer or NULL if query could not be created (an appropriate error log message will be printed in this case)

◆ addAuthority() [1/2]

DnsResource* pcpp::DnsLayer::addAuthority ( const std::string &  name,
DnsType  dnsType,
DnsClass  dnsClass,
uint32_t  ttl,
IDnsResourceData data 
)

Add a new DNS authority to the layer

Parameters
[in]nameThe value that shall be set in the name field of the authority
[in]dnsTypeThe value that shall be set in the DNS type field of the authority
[in]dnsClassThe value that shall be set in the DNS class field of the authority
[in]ttlThe value that shall be set in the 'time-to-leave' field of the authority
[in]dataThe authority data to be set. The type of the data should match the type of the DNS record (for example: DNS record of type A should have data of type IPv4DnsResourceData. Please see DnsResource::setData() for more info on this
Returns
A pointer to the newly created DNS authority or NULL if authority could not be created (an appropriate error log message will be printed in this case)

◆ addAuthority() [2/2]

DnsResource* pcpp::DnsLayer::addAuthority ( DnsResource *const  copyAuthority)

Add a new DNS authority similar to an already existing DNS authority. All authority fields will be copied from the existing authority param[in] copyAuthority The record to create the new record from. copyAuthority won't be changed in any way

Returns
A pointer to the newly created DNS authority or NULL if query could not be created (an appropriate error log message will be printed in this case)

◆ addQuery() [1/2]

DnsQuery* pcpp::DnsLayer::addQuery ( const std::string &  name,
DnsType  dnsType,
DnsClass  dnsClass 
)

Add a new DNS query to the layer

Parameters
[in]nameThe value that shall be set in the name field of the query
[in]dnsTypeThe value that shall be set in the DNS type field of the query
[in]dnsClassThe value that shall be set in the DNS class field of the query
Returns
A pointer to the newly created DNS query or NULL if query could not be created (an appropriate error log message will be printed in this case)

◆ addQuery() [2/2]

DnsQuery* pcpp::DnsLayer::addQuery ( DnsQuery *const  copyQuery)

Add a new DNS query similar to an already existing DNS query. All query fields will be copied from the existing query param[in] copyQuery The record to create the new record from. copyQuery won't be changed in any way

Returns
A pointer to the newly created DNS query or NULL if query could not be created (an appropriate error log message will be printed in this case)

◆ computeCalculateFields()

void pcpp::DnsLayer::computeCalculateFields ( )
inlinevirtual

Does nothing for this layer

Implements pcpp::Layer.

◆ getAdditionalRecord()

DnsResource* pcpp::DnsLayer::getAdditionalRecord ( const std::string &  name,
bool  exactMatch 
) const

Searches for a DNS additional record by its name field. Notice this method returns only an additional record which its name equals to the requested name. If several additional records match the requested name, the first one will be returned. If no additional records match the requested name, NULL will be returned

Parameters
[in]nameThe name of the additional record to search
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
The first matching DNS additional record or NULL if no additional records were found

◆ getAdditionalRecordCount()

size_t pcpp::DnsLayer::getAdditionalRecordCount ( ) const
Returns
The number of DNS additional records in the packet

◆ getAnswer()

DnsResource* pcpp::DnsLayer::getAnswer ( const std::string &  name,
bool  exactMatch 
) const

Searches for a DNS answer by its name field. Notice this method returns only an answer which its name equals to the requested name. If several answers match the requested name, the first one will be returned. If no answers match the requested name, NULL will be returned

Parameters
[in]nameThe name of the answer to search
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
The first matching DNS answer or NULL if no answers were found

◆ getAnswerCount()

size_t pcpp::DnsLayer::getAnswerCount ( ) const
Returns
The number of DNS answers in the packet

◆ getAuthority()

DnsResource* pcpp::DnsLayer::getAuthority ( const std::string &  name,
bool  exactMatch 
) const

Searches for a DNS authority by its name field. Notice this method returns only an authority which its name equals to the requested name. If several authorities match the requested name, the first one will be returned. If no authorities match the requested name, NULL will be returned

Parameters
[in]nameThe name of the authority to search
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
The first matching DNS authority or NULL if no authorities were found

◆ getAuthorityCount()

size_t pcpp::DnsLayer::getAuthorityCount ( ) const
Returns
The number of DNS authorities in the packet

◆ getDnsHeader()

dnshdr* pcpp::DnsLayer::getDnsHeader ( ) const
inline

Get a pointer to the DNS header (as opposed to the DNS data which is the queries, answers, etc. Data can be retrieved through the other methods of this layer. Notice the return value points directly to the data, so every change will change the actual packet data

Returns
A pointer to the dnshdr

◆ getDNSPortMap()

static const std::map<uint16_t, bool>* pcpp::DnsLayer::getDNSPortMap ( )
static
Returns
A pointer to a map containing all UDP ports recognize as DNS

◆ getFirstAdditionalRecord()

DnsResource* pcpp::DnsLayer::getFirstAdditionalRecord ( ) const
Returns
The first DNS additional record in the packet or NULL if packet doesn't contain any additional records

◆ getFirstAnswer()

DnsResource* pcpp::DnsLayer::getFirstAnswer ( ) const
Returns
The first DNS answer in the packet or NULL if packet doesn't contain any answers

◆ getFirstAuthority()

DnsResource* pcpp::DnsLayer::getFirstAuthority ( ) const
Returns
The first DNS authority in the packet or NULL if packet doesn't contain any authorities

◆ getFirstQuery()

DnsQuery* pcpp::DnsLayer::getFirstQuery ( ) const
Returns
The first DNS query in the packet or NULL if packet doesn't contain any queries

◆ getHeaderLen()

size_t pcpp::DnsLayer::getHeaderLen ( ) const
inlinevirtual

Return the size of the DNS data in the packet including he DNS header and size of all queries, answers, authorities and additional records

Implements pcpp::Layer.

◆ getNextAdditionalRecord()

DnsResource* pcpp::DnsLayer::getNextAdditionalRecord ( DnsResource additionalRecord) const

Get the DNS additional record following a certain additional record

Parameters
[in]additionalRecordA pointer to a DNS additional record that exist in the packet
Returns
The DNS additional record following 'additionalRecord'. If 'additionalRecord' is NULL or 'additionalRecord' is the last additional record in the packet NULL will be returned

◆ getNextAnswer()

DnsResource* pcpp::DnsLayer::getNextAnswer ( DnsResource answer) const

Get the DNS answer following a certain answer

Parameters
[in]answerA pointer to a DNS answer that exist in the packet
Returns
The DNS answer following 'answer'. If 'answer' is NULL or 'answer' is the last answer in the packet NULL will be returned

◆ getNextAuthority()

DnsResource* pcpp::DnsLayer::getNextAuthority ( DnsResource authority) const

Get the DNS authority following a certain authority

Parameters
[in]authorityA pointer to a DNS authority that exist in the packet
Returns
The DNS authority following 'authority'. If 'authority' is NULL or 'authority' is the last authority in the packet NULL will be returned

◆ getNextQuery()

DnsQuery* pcpp::DnsLayer::getNextQuery ( DnsQuery query) const

Get the DNS query following a certain query

Parameters
[in]queryA pointer to a DNS query that exist in the packet
Returns
The DNS query following 'query'. If 'query' is NULL or 'query' is the last query in the packet NULL will be returned

◆ getOsiModelLayer()

OsiModelLayer pcpp::DnsLayer::getOsiModelLayer ( ) const
inlinevirtual
Returns
The OSI Model layer this protocol belongs to

Implements pcpp::Layer.

◆ getQuery()

DnsQuery* pcpp::DnsLayer::getQuery ( const std::string &  name,
bool  exactMatch 
) const

Searches for a DNS query by its name field. Notice this method returns only a query which its name equals to the requested name. If several queries match the requested name, the first one will be returned. If no queries match the requested name, NULL will be returned

Parameters
[in]nameThe name of the query to search
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
The first matching DNS query or NULL if no queries were found

◆ getQueryCount()

size_t pcpp::DnsLayer::getQueryCount ( ) const
Returns
The number of DNS queries in the packet

◆ operator=()

DnsLayer& pcpp::DnsLayer::operator= ( const DnsLayer other)

An assignment operator for this layer

◆ parseNextLayer()

void pcpp::DnsLayer::parseNextLayer ( )
inlinevirtual

Does nothing for this layer (DnsLayer is always last)

Implements pcpp::Layer.

◆ removeAdditionalRecord() [1/2]

bool pcpp::DnsLayer::removeAdditionalRecord ( const std::string &  additionalRecordNameToRemove,
bool  exactMatch 
)

Remove an existing additional record by name. If several additional records matches the name, the first match will be removed

Parameters
[in]additionalRecordNameToRemoveThe name of the additional record to remove
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
True if additional record was found and successfully removed or false if additional record was not found or couldn't be removed

◆ removeAdditionalRecord() [2/2]

bool pcpp::DnsLayer::removeAdditionalRecord ( DnsResource additionalRecordToRemove)

Remove an existing additional record

Parameters
[in]additionalRecordToRemoveA pointer to the additional record to remove
Returns
True if additional record was found and successfully removed or false if additional record was not found or couldn't be removed

◆ removeAnswer() [1/2]

bool pcpp::DnsLayer::removeAnswer ( const std::string &  answerNameToRemove,
bool  exactMatch 
)

Remove an existing answer by name. If several answers matches the name, the first match will be removed

Parameters
[in]answerNameToRemoveThe name of the answer to remove
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
True if answer was found and successfully removed or false if answer was not found or couldn't be removed

◆ removeAnswer() [2/2]

bool pcpp::DnsLayer::removeAnswer ( DnsResource answerToRemove)

Remove an existing answer

Parameters
[in]answerToRemoveA pointer to the answer to remove
Returns
True if answer was found and successfully removed or false if answer was not found or couldn't be removed

◆ removeAuthority() [1/2]

bool pcpp::DnsLayer::removeAuthority ( const std::string &  authorityNameToRemove,
bool  exactMatch 
)

Remove an existing authority by name. If several authorities matches the name, the first match will be removed

Parameters
[in]authorityNameToRemoveThe name of the authority to remove
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
True if authority was found and successfully removed or false if authority was not found or couldn't be removed

◆ removeAuthority() [2/2]

bool pcpp::DnsLayer::removeAuthority ( DnsResource authorityToRemove)

Remove an existing authority

Parameters
[in]authorityToRemoveA pointer to the authority to remove
Returns
True if authority was found and successfully removed or false if authority was not found or couldn't be removed

◆ removeQuery() [1/2]

bool pcpp::DnsLayer::removeQuery ( const std::string &  queryNameToRemove,
bool  exactMatch 
)

Remove an existing query by name. If several queries matches the name, the first match will be removed

Parameters
[in]queryNameToRemoveThe name of the query to remove
[in]exactMatchIndicate whether to match the whole name or just a part of it
Returns
True if query was found and successfully removed or false if query was not found or couldn't be removed

◆ removeQuery() [2/2]

bool pcpp::DnsLayer::removeQuery ( DnsQuery queryToRemove)

Remove an existing query

Parameters
[in]queryToRemoveA pointer to the query to remove
Returns
True if query was found and successfully removed or false if query was not found or couldn't be removed

◆ toString()

std::string pcpp::DnsLayer::toString ( ) const
virtual
Returns
A string representation of the layer most important data (should look like the layer description in Wireshark)

Implements pcpp::Layer.