aboutsummaryrefslogtreecommitdiff
path: root/middleware/reverse/network.go
blob: 83ce21309c0f3d2b11517cd06f1a134161fcbc7b (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package reverse

import (
	"bytes"
	"net"
	"regexp"
	"strings"
)

type network struct {
	IPnet        *net.IPNet
	Zone         string // forward lookup zone
	Template     string
	TTL          uint32
	RegexMatchIP *regexp.Regexp
}

// TODO: we might want to get rid of these regexes.
const hexDigit = "0123456789abcdef"
const templateNameIP = "{ip}"
const regexMatchV4 = "((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\-){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))"
const regexMatchV6 = "([0-9a-fA-F]{32})"

// hostnameToIP converts the hostname back to an ip, based on the template
// returns nil if there is no IP found.
func (network *network) hostnameToIP(rname string) net.IP {
	var matchedIP net.IP

	match := network.RegexMatchIP.FindStringSubmatch(rname)
	if len(match) != 2 {
		return nil
	}

	if network.IPnet.IP.To4() != nil {
		matchedIP = net.ParseIP(strings.Replace(match[1], "-", ".", 4))
	} else {
		// TODO: can probably just allocate a []byte and use that.
		var buf bytes.Buffer
		// convert back to an valid ipv6 string with colons
		for i := 0; i < 8*4; i += 4 {
			buf.WriteString(match[1][i : i+4])
			if i < 28 {
				buf.WriteString(":")
			}
		}
		matchedIP = net.ParseIP(buf.String())
	}

	// No valid ip or it does not belong to this network
	if matchedIP == nil || !network.IPnet.Contains(matchedIP) {
		return nil
	}

	return matchedIP
}

// ipToHostname converts an IP to an DNS compatible hostname and injects it into the template.domain.
func (network *network) ipToHostname(ip net.IP) (name string) {
	if ipv4 := ip.To4(); ipv4 != nil {
		// replace . to -
		name = uitoa(ipv4[0]) + "-" +
			uitoa(ipv4[1]) + "-" +
			uitoa(ipv4[2]) + "-" +
			uitoa(ipv4[3])
	} else {
		// assume v6
		// ensure zeros are present in string
		buf := make([]byte, 0, len(ip)*4)
		for i := 0; i < len(ip); i++ {
			v := ip[i]
			buf = append(buf, hexDigit[v>>4])
			buf = append(buf, hexDigit[v&0xF])
		}
		name = string(buf)
	}
	// inject the converted ip into the fqdn template
	return strings.Replace(network.Template, templateNameIP, name, 1)
}

// just the same from net.ip package, but with uint8
func uitoa(val uint8) string {
	if val == 0 {
		// avoid string allocation
		return "0"
	}
	var buf [20]byte // big enough for 64bit value base 10
	i := len(buf) - 1
	for val >= 10 {
		q := val / 10
		buf[i] = byte('0' + val - q*10)
		i--
		val = q
	}
	// val < 10
	buf[i] = byte('0' + val)
	return string(buf[i:])
}

type networks []network

func (n networks) Len() int      { return len(n) }
func (n networks) Swap(i, j int) { n[i], n[j] = n[j], n[i] }

// cidr closer to the ip wins (by netmask)
func (n networks) Less(i, j int) bool {
	isize, _ := n[i].IPnet.Mask.Size()
	jsize, _ := n[j].IPnet.Mask.Size()
	return isize > jsize
}