aboutsummaryrefslogtreecommitdiff
path: root/core/dnsserver/address.go
blob: 872e44cb7cdda1c71906788f0b9673830efdb83d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dnsserver

import (
	"fmt"
	"net"
	"strings"
)

type zoneAddr struct {
	Zone      string
	Port      string
	Transport string // dns, tls or grpc
	Address   string // used for bound zoneAddr - validation of overlapping
}

// String returns the string representation of z.
func (z zoneAddr) String() string {
	s := z.Transport + "://" + z.Zone + ":" + z.Port
	if z.Address != "" {
		s += " on " + z.Address
	}
	return s
}

// SplitProtocolHostPort splits a full formed address like "dns://[::1]:53" into parts.
func SplitProtocolHostPort(address string) (protocol string, ip string, port string, err error) {
	parts := strings.Split(address, "://")
	switch len(parts) {
	case 1:
		ip, port, err := net.SplitHostPort(parts[0])
		return "", ip, port, err
	case 2:
		ip, port, err := net.SplitHostPort(parts[1])
		return parts[0], ip, port, err
	default:
		return "", "", "", fmt.Errorf("provided value is not in an address format : %s", address)
	}
}

type zoneOverlap struct {
	registeredAddr map[zoneAddr]zoneAddr // each zoneAddr is registered once by its key
	unboundOverlap map[zoneAddr]zoneAddr // the "no bind" equiv ZoneAddr is registered by its original key
}

func newOverlapZone() *zoneOverlap {
	return &zoneOverlap{registeredAddr: make(map[zoneAddr]zoneAddr), unboundOverlap: make(map[zoneAddr]zoneAddr)}
}

// registerAndCheck adds a new zoneAddr for validation, it returns information about existing or overlapping with already registered
// we consider that an unbound address is overlapping all bound addresses for same zone, same port
func (zo *zoneOverlap) registerAndCheck(z zoneAddr) (existingZone *zoneAddr, overlappingZone *zoneAddr) {
	existingZone, overlappingZone = zo.check(z)
	if existingZone != nil || overlappingZone != nil {
		return existingZone, overlappingZone
	}
	// there is no overlap, keep the current zoneAddr for future checks
	zo.registeredAddr[z] = z
	zo.unboundOverlap[z.unbound()] = z
	return nil, nil
}

// check validates a zoneAddr for overlap without registering it
func (zo *zoneOverlap) check(z zoneAddr) (existingZone *zoneAddr, overlappingZone *zoneAddr) {
	if exist, ok := zo.registeredAddr[z]; ok {
		// exact same zone already registered
		return &exist, nil
	}
	uz := z.unbound()
	if already, ok := zo.unboundOverlap[uz]; ok {
		if z.Address == "" {
			// current is not bound to an address, but there is already another zone with a bind address registered
			return nil, &already
		}
		if _, ok := zo.registeredAddr[uz]; ok {
			// current zone is bound to an address, but there is already an overlapping zone+port with no bind address
			return nil, &uz
		}
	}
	// there is no overlap
	return nil, nil
}

// unbound returns an unbound version of the zoneAddr
func (z zoneAddr) unbound() zoneAddr {
	return zoneAddr{Zone: z.Zone, Address: "", Port: z.Port, Transport: z.Transport}
}