Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IPv6 DNS records creation #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions dhcp-to-dns.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,38 @@ $ScriptLock $0 false 10;
}
:local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0);

:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) (!type or type=A) ] do={
:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) (!type or type=A or type=AAAA) ] do={
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
:local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=$MacAddress active-address=($DnsRecordVal->"address") status=bound ] ] > 0) do={
$LogPrintExit2 debug $0 ("Lease for " . $MacAddress . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting DNS entry.") false;
:local MacAddrIface [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
:local MacAddress [ :pick $MacAddrIface 0 [ :find $MacAddrIface "%" ] ];
:local Interface [ :pick $MacAddrIface ([ :find $MacAddrIface "%" ]+1) [ :len $MacAddrIface ] ];
:local DHCPServerName [ /ip/dhcp-server/get [ find where interface=$Interface ] name ];

:if ($DnsRecordVal->"type" != "AAAA") do={
:if ([ :len [ /ip/dhcp-server/lease/find where active-mac-address=$MacAddress active-address=($DnsRecordVal->"address") server=$DHCPServerName status=bound ] ] > 0) do={
$LogPrintExit2 debug $0 ("Lease for " . $MacAddrIface . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting DNS entry.") false;
} else={
:local Found false;
$LogPrintExit2 info $0 ("Lease expired for " . $MacAddrIface . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
/ip/dns/static/remove $DnsRecord;
/ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
}
} else={
:local Found false;
$LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
/ip/dns/static/remove $DnsRecord;
/ip/dns/static/remove [ find where type=CNAME comment=($DnsRecordVal->"comment") ];
:if (([ :len [ /ip/dhcp-server/lease/find where active-mac-address=$MacAddress server=$DHCPServerName status=bound ] ] > 0) and ([ :len [ /ipv6/neighbor/find where mac-address=$MacAddress address=($DnsRecordVal->"address") interface=$Interface status!=failed ] ] > 0)) do={
$LogPrintExit2 debug $0 ("Lease and IPv6 neighbor for " . $MacAddrIface . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting AAAA DNS entry.") false;
} else={
:local Found false;
$LogPrintExit2 info $0 ("Lease expired or IPv6 neighbor failed for " . $MacAddrIface . " (" . $DnsRecordVal->"name" . "), deleting AAAA DNS entry.") false;
/ip/dns/static/remove $DnsRecord;
}
}
}

:foreach Lease in=[ /ip/dhcp-server/lease/find where status=bound ] do={
:local LeaseVal;
:do {
:set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
:local DupMacLeases [ /ip/dhcp-server/lease/find where active-mac-address=($LeaseVal->"active-mac-address") status=bound ];
:local DupMacLeases [ /ip/dhcp-server/lease/find where active-mac-address=($LeaseVal->"active-mac-address") server=($LeaseVal->"server") status=bound ];
:if ([ :len $DupMacLeases ] > 1) do={
$LogPrintExit2 debug $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"active-mac-address") . ", using last one.") false;
:set LeaseVal [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) ];
Expand All @@ -61,7 +75,8 @@ $ScriptLock $0 false 10;
}

:if ([ :len ($LeaseVal->"active-address") ] > 0) do={
:local Comment ($CommentPrefix . $LeaseVal->"active-mac-address");
:local Interface ([ /ip/dhcp-server/get [ find where name=($LeaseVal->"server") ] ]->"interface");
:local Comment ($CommentPrefix . $LeaseVal->"active-mac-address" . "%" . $Interface);
:local MacDash [ $CharacterReplace ($LeaseVal->"active-mac-address") ":" "-" ];
:local HostName [ $CharacterReplace [ $EitherOr ([ $ParseKeyValueStore ($LeaseVal->"comment") ]->"hostname") ($LeaseVal->"host-name") ] " " "" ];
:local Network [ /ip/dhcp-server/network/find where ($LeaseVal->"active-address") in address ];
Expand Down Expand Up @@ -101,6 +116,25 @@ $ScriptLock $0 false 10;
/ip/dns/static/add name=($HostName . "." . $NetDomain) type=CNAME cname=($MacDash . "." . $NetDomain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
}
}

:local V6Neighbors [ /ipv6/neighbor/find where mac-address=($LeaseVal->"active-mac-address") interface=$Interface (((address & ffff::) ^ fe80::) != ::) status=reachable ];
:if ([ :len $V6Neighbors ] > 0) do={
:local V6Neighbor ($V6Neighbors->0);
:local V6NeighborVal [ /ipv6/neighbor/get $V6Neighbor ];
:local DnsRecord [ /ip/dns/static/find where comment=$Comment type=AAAA ];
:if ([ :len $DnsRecord ] > 0) do={
:local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
:if ($DnsRecordVal->"address" = $V6NeighborVal->"address" && $DnsRecordVal->"name" = ($MacDash . "." . $NetDomain)) do={
$LogPrintExit2 debug $0 ("V6 DNS entry for " . $LeaseVal->"active-mac-address" . " does not need updating.") false;
} else={
$LogPrintExit2 info $0 ("Replacing V6 DNS entry for " . $LeaseVal->"active-mac-address" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $V6NeighborVal->"address" . ").") false;
/ip/dns/static/set address=($V6NeighborVal->"address") name=($MacDash . "." . $NetDomain) $DnsRecord;
}
} else={
$LogPrintExit2 info $0 ("Adding new V6 DNS entry for " . $LeaseVal->"active-mac-address" . " (" . ($MacDash . "." . $NetDomain) . " -> " . $V6NeighborVal->"address" . ").") false;
/ip/dns/static/add name=($MacDash . "." . $NetDomain) type=AAAA address=($V6NeighborVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
}
}
} else={
$LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
}
Expand Down