dns: remove goto INVALID and a naked return

This commit is one small step toward modernizing and making the DNSServer readable.
pull/9004/head
Daniel Nephin 4 years ago
parent 6513faf14c
commit 9b1944dae0

@ -592,8 +592,7 @@ func (d *DNSServer) parseDatacenter(labels []string, datacenter *string) bool {
// doDispatch is used to parse a request and invoke the correct handler. // doDispatch is used to parse a request and invoke the correct handler.
// parameter maxRecursionLevel will handle whether recursive call can be performed // parameter maxRecursionLevel will handle whether recursive call can be performed
func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *dns.Msg, maxRecursionLevel int) (ecsGlobal bool) { func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *dns.Msg, maxRecursionLevel int) bool {
ecsGlobal = true
// By default the query is in the default datacenter // By default the query is in the default datacenter
datacenter := d.agent.config.Datacenter datacenter := d.agent.config.Datacenter
@ -633,19 +632,26 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
} }
} }
invalid := func() bool {
d.logger.Warn("QName invalid", "qname", qName)
d.addSOA(cfg, resp)
resp.SetRcode(req, dns.RcodeNameError)
return true
}
if queryKind == "" { if queryKind == "" {
goto INVALID return invalid()
} }
switch queryKind { switch queryKind {
case "service": case "service":
n := len(queryParts) n := len(queryParts)
if n < 1 { if n < 1 {
goto INVALID return invalid()
} }
if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) { if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) {
goto INVALID return invalid()
} }
lookup := serviceLookup{ lookup := serviceLookup{
@ -689,11 +695,11 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
} }
case "connect": case "connect":
if len(queryParts) < 1 { if len(queryParts) < 1 {
goto INVALID return invalid()
} }
if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) { if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) {
goto INVALID return invalid()
} }
lookup := serviceLookup{ lookup := serviceLookup{
@ -709,11 +715,11 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
d.serviceLookup(cfg, lookup, req, resp) d.serviceLookup(cfg, lookup, req, resp)
case "ingress": case "ingress":
if len(queryParts) < 1 { if len(queryParts) < 1 {
goto INVALID return invalid()
} }
if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) { if !d.parseDatacenterAndEnterpriseMeta(querySuffixes, cfg, &datacenter, &entMeta) {
goto INVALID return invalid()
} }
lookup := serviceLookup{ lookup := serviceLookup{
@ -729,11 +735,11 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
d.serviceLookup(cfg, lookup, req, resp) d.serviceLookup(cfg, lookup, req, resp)
case "node": case "node":
if len(queryParts) < 1 { if len(queryParts) < 1 {
goto INVALID return invalid()
} }
if !d.parseDatacenter(querySuffixes, &datacenter) { if !d.parseDatacenter(querySuffixes, &datacenter) {
goto INVALID return invalid()
} }
// Allow a "." in the node name, just join all the parts // Allow a "." in the node name, just join all the parts
@ -742,22 +748,22 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
case "query": case "query":
// ensure we have a query name // ensure we have a query name
if len(queryParts) < 1 { if len(queryParts) < 1 {
goto INVALID return invalid()
} }
if !d.parseDatacenter(querySuffixes, &datacenter) { if !d.parseDatacenter(querySuffixes, &datacenter) {
goto INVALID return invalid()
} }
// Allow a "." in the query name, just join all the parts. // Allow a "." in the query name, just join all the parts.
query := strings.Join(queryParts, ".") query := strings.Join(queryParts, ".")
ecsGlobal = false
d.preparedQueryLookup(cfg, network, datacenter, query, remoteAddr, req, resp, maxRecursionLevel) d.preparedQueryLookup(cfg, network, datacenter, query, remoteAddr, req, resp, maxRecursionLevel)
return false
case "addr": case "addr":
// <address>.addr.<suffixes>.<domain> - addr must be the second label, datacenter is optional // <address>.addr.<suffixes>.<domain> - addr must be the second label, datacenter is optional
if len(queryParts) != 1 { if len(queryParts) != 1 {
goto INVALID return invalid()
} }
switch len(queryParts[0]) / 2 { switch len(queryParts[0]) / 2 {
@ -765,7 +771,7 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
case 4: case 4:
ip, err := hex.DecodeString(queryParts[0]) ip, err := hex.DecodeString(queryParts[0])
if err != nil { if err != nil {
goto INVALID return invalid()
} }
resp.Answer = append(resp.Answer, &dns.A{ resp.Answer = append(resp.Answer, &dns.A{
@ -781,7 +787,7 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
case 16: case 16:
ip, err := hex.DecodeString(queryParts[0]) ip, err := hex.DecodeString(queryParts[0])
if err != nil { if err != nil {
goto INVALID return invalid()
} }
resp.Answer = append(resp.Answer, &dns.AAAA{ resp.Answer = append(resp.Answer, &dns.AAAA{
@ -795,14 +801,7 @@ func (d *DNSServer) doDispatch(network string, remoteAddr net.Addr, req, resp *d
}) })
} }
} }
// early return without error return true
return
INVALID:
d.logger.Warn("QName invalid", "qname", qName)
d.addSOA(cfg, resp)
resp.SetRcode(req, dns.RcodeNameError)
return
} }
func (d *DNSServer) trimDomain(query string) string { func (d *DNSServer) trimDomain(query string) string {

Loading…
Cancel
Save