Showing results for 
Search instead for 
Did you mean: 

HowTo: Modify DNS requests using libDNS.rts

This article uses the libDNS.rts trafficscript library as described in libDNS.rts: Interrogating and managing DNS traffic in Traffic Manager.


In this example, we inspect DNS requests.  If the client is seeking to resolve*, we rewrite the request to resolve instead before passing the DNS request on to the real DNS server.


We also need to patch up the DNS response to restore the correct value for the 'question'.


Request rule


import libDNS.rts as dns;

$data = request.get();

# Create a data structure holding the contents of the DNS request
$packet = dns.convertRawDataToObject( $data, "udp" );

# Get the Question section from the request
$question = dns.getQuestion( $packet );
$host = $question["host"];

if( string.wildmatch( $host, "*" ) && $host != "" ) {
   # We'll rewrite the question
# We also need to store the original value
# so that we can restore it in the response "question", $question ); $packet = dns.setQuestion( $packet, "", $question["type"], $question["class"] ); request.set( dns.convertObjectToRawData( $packet, "udp" )); }


Response rule

import libDNS.rts as dns;

$question = "question" );
if( !$question ) break; # We don't need to patch up the response

$data = response.get();
$packet = dns.convertRawDataToObject( $data, "udp" );

$packet = dns.setQuestion($packet, $question["host"], $question["type"], $question["class"] );

# Set the name of each Answer RR to what was asked for in the question
$i = 0;
while( $i < $packet["answercount"] ) {
   $packet["answer"][$i++]["name"] = $question["host"];

# Convert the data structure back into raw data and set it as the response
response.set( dns.convertObjectToRawData( $packet, "udp" ));
Version history
Revision #:
2 of 2
Last update:
‎12-21-2020 07:41:AM
Updated by:
Labels (1)