Net_DNS: Examples: Dynamic DNS Update

Performing a dynamic DNS update can be accomplished by using special values for certain RRset fields. The following example is a TSIG signed DNS update packet. For security reasons, this example will not get executed.

The following example will delete a resource and add a resource record within the same DNS packet. The following resource record is deleted:

www.example.com A 192.168.0.1
    
and this record is added:
www.example.com 3600 IN A 192.168.0.1
    
Note that the CLASS and TTL values are left out of the deleted RR.
require_once("Net/DNS.php");
$r = new Net_DNS_Resolver();
$r->nameservers = array("pula.ypass.net");
$packet = new Net_DNS_Packet();
$packet->header = new Net_DNS_Header();
$packet->header->id = $r->nextid();
$packet->header->qr = 0;
$packet->header->opcode = "UPDATE";
$packet->question[0] = new Net_DNS_Question("example.com", "SOA", "IN");
$packet->answer = array(); // Answer is the prerequisite section

// Delete a resource record by setting the ttl to 0 and the class
// to "NONE".  Add the RR object to the authority/update section
$DELrr = new Net_DNS_RR("www.example.com 3600 IN A 192.168.0.1");
$DELrr->ttl = 0;
$DELrr->class = "NONE";
$packet->authority[0] = $DELrr; // Authority is the update section

// Add a resource record by adding the RR to the update section.
$ADDrr = new Net_DNS_RR("www.example.com 3600 IN A 192.168.0.2");
$packet->authority[1] = $ADDrr; // Authority is the update section

// Add a TSIG RR into the additional section.  This will sign the
// packet using the key specified in $privkey.  This should be the
// key as configured in the named.conf zone configuration.  The
// packet is signed when a call is made to $packet->data()
$tsig = new Net_DNS_RR("pula.ypass.net-example.com TSIG " . $privkey);
$packet->additional = array($tsig);

$packet->header->qdcount = count($packet->question);
$packet->header->ancount = count($packet->answer);
$packet->header->nscount = count($packet->authority);
$packet->header->arcount = count($packet->additional);

$ans = $r->send_tcp($packet, $packet->data());
if ($ans->header->rcode != "NOERROR") {
  echo "Error: " . $ans->header->rcode . "\n";
} else {
  echo "Resource Record Deleted: " . $DELrr->string());
  echo "Resource Record Added: " . $ADDrr->string());
}