-
-
Notifications
You must be signed in to change notification settings - Fork 853
[client] Add reverse dns zone #3217
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
Conversation
are we adding the PTR zone as search domain? |
I've excluded it but seems it's not marked by OS tools anyway |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can split the logic to make it more readable
func addReverseZone(config *nbdns.Config, ipNet *net.IPNet) {
zoneName, err := reverseZoneName(ipNet)
if err != nil {
log.Warn(err)
return
}
if zoneExists(config, zoneName) {
log.Debugf("reverse DNS zone %s already exists", zoneName)
return
}
records := collectPTRRecords(config.CustomZones, ipNet)
config.CustomZones = append(config.CustomZones, nbdns.CustomZone{
Domain: zoneName,
Records: records,
})
log.Debugf("added reverse DNS zone: %s with %d records", zoneName, len(records))
}
// reverseZoneName computes the reverse DNS zone name
func reverseZoneName(ipNet *net.IPNet) (string, error) {
networkIP := ipNet.IP.Mask(ipNet.Mask)
maskOnes, _ := ipNet.Mask.Size()
// round up to nearest byte
octetsToUse := (maskOnes + 7) / 8
octets := strings.Split(ip4.String(), ".")
if octetsToUse > len(octets) {
return "", fmt.Errorf("invalid network mask size for reverse DNS: %d", maskOnes)
}
// reverse the first octetsToUse octets.
reversed := make([]string, octetsToUse)
for i := 0; i < octetsToUse; i++ {
reversed[octetsToUse-1-i] = octets[i]
}
zone := fmt.Sprintf("%s.in-addr.arpa", strings.Join(reversed, "."))
return dns.Fqdn(zone), nil
}
// zoneExists returns true if the given zone name already exists in the configuration.
func zoneExists(config *nbdns.Config, zoneName string) bool {
for _, zone := range config.CustomZones {
if zone.Domain == zoneName {
return true
}
}
return false
}
// collectPTRRecords iterates over all A records in the existing zones,
// creating PTR records where applicable.
func collectPTRRecords(zones []nbdns.CustomZone, ipNet *net.IPNet) []nbdns.SimpleRecord {
var records []nbdns.SimpleRecord
for _, zone := range zones {
for _, record := range zone.Records {
if record.Type != int(dns.TypeA) {
continue
}
if ptrRecord, ok := createPTRRecord(record, ipNet); ok {
records = append(records, ptrRecord)
}
}
}
return records
}
|
Describe your changes
Issue ticket number and link
Closes #3214
Checklist