diff --git a/AGHTechDoc.md b/AGHTechDoc.md index 8eba5ed6..e50982ba 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -22,7 +22,8 @@ Contents: * Update client * Delete client * API: Find clients by IP -* Enable DHCP server +* DHCP server + * DHCP server in DNS * "Show DHCP status" command * "Check DHCP" command * "Enable DHCP" command @@ -375,9 +376,9 @@ Error response: UI shows error message "Auto-update has failed" -## Enable DHCP server +## DHCP server -Algorithm: +Enable DHCP server algorithm: * UI shows DHCP configuration screen with "Enabled DHCP" button disabled, and "Check DHCP" button enabled * User clicks on "Check DHCP"; UI sends request to server @@ -389,6 +390,21 @@ Algorithm: * UI shows the status +### DHCP server in DNS + +DHCP leases are used in several ways by DNS module. + +* For "A" DNS reqeust we reply with an IP address leased by our DHCP server. + + < A bills-notebook.lan. + > A bills-notebook.lan. = 192.168.1.100 + +* For "PTR" DNS request we reply with a hostname from an active DHCP lease. + + < PTR 100.1.168.192.in-addr.arpa. + > PTR 100.1.168.192.in-addr.arpa. = bills-notebook. + + ### "Show DHCP status" command Request: diff --git a/dnsforward/handle_dns.go b/dnsforward/handle_dns.go index 6860dcf3..1495e736 100644 --- a/dnsforward/handle_dns.go +++ b/dnsforward/handle_dns.go @@ -94,6 +94,20 @@ func processInitial(ctx *dnsContext) int { return resultDone } +// Return TRUE if host names doesn't contain disallowed characters +func isHostnameOK(hostname string) bool { + for _, c := range hostname { + if !((c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '.' || c == '-') { + log.Debug("DNS: skipping invalid hostname %s from DHCP", hostname) + return false + } + } + return true +} + func (s *Server) onDHCPLeaseChanged(flags int) { switch flags { case dhcpd.LeaseChangedAdded, @@ -110,15 +124,17 @@ func (s *Server) onDHCPLeaseChanged(flags int) { ll := s.dhcpServer.Leases(dhcpd.LeasesAll) for _, l := range ll { - if len(l.Hostname) == 0 { + if len(l.Hostname) == 0 || !isHostnameOK(l.Hostname) { continue } - m[l.IP.String()] = l.Hostname + lowhost := strings.ToLower(l.Hostname) + + m[l.IP.String()] = lowhost ip := make(net.IP, 4) copy(ip, l.IP.To4()) - hostToIP[l.Hostname] = ip + hostToIP[lowhost] = ip } log.Debug("DNS: added %d A/PTR entries from DHCP", len(m))