-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4_rev.go
95 lines (80 loc) · 2.11 KB
/
ipv4_rev.go
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
package dn42
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/miekg/dns"
)
const IPv4_PTR_SUFFIX = ".in-addr.arpa."
func (dn42 DN42) reverseIPv4(ip string) (string, int) {
splitted := strings.Split(ip, ".")
reverse(splitted)
ipMaskMax := 8 * len(splitted)
for len(splitted) < 4 {
splitted = append(splitted, "0")
}
return strings.Join(splitted, "."), ipMaskMax
}
func (dn42 DN42) parseIPv4Ptr(qname string) (*net.IP, int, error) {
if !strings.HasSuffix(qname, IPv4_PTR_SUFFIX) {
return nil, 0, errors.New("not IPv4 ptr query")
}
qname = strings.TrimSuffix(qname, IPv4_PTR_SUFFIX)
ipString, ipMask := dn42.reverseIPv4(qname)
splitted := strings.SplitN(ipString, "/", 2)
if len(splitted) == 2 {
ipString = splitted[0]
queryMask, err := strconv.Atoi(splitted[1])
if err != nil {
return nil, 0, err
}
if ipMask > queryMask {
ipMask = queryMask
}
}
ip := net.ParseIP(ipString)
if ip == nil {
return nil, 0, errors.New("not IPv4 ptr query")
}
if ip.To4() == nil {
return nil, 0, errors.New("not IPv4 ptr query")
}
return &ip, ipMask, nil
}
func (dn42 DN42) findIPv4RecordFile(ip net.IP, ipMask int) (string, int, error) {
ipString := ip.String()
for i := ipMask; i >= 0; i-- {
_, cidr, err := net.ParseCIDR(fmt.Sprintf("%s/%d", ipString, i))
if err != nil {
return "", 0, err
}
cidrFilename := strings.ReplaceAll(cidr.String(), "/", "_")
if dn42.fileExists("inetnum", cidrFilename) {
return cidrFilename, i, nil
}
}
return "", 0, errors.New("file for range not found")
}
func (dn42 DN42) generateIPv4CNAMERecord(qname string, ip net.IP, ipMask int) ([]dns.RR, string, error) {
ipCidrString := fmt.Sprintf("%s/%d", ip, ipMask)
splitted := strings.Split(ipCidrString, ".")
reverse(splitted)
ipCidrString = strings.Join(splitted, ".")
target := ipCidrString + IPv4_PTR_SUFFIX
if target != qname {
cnameRecord := &dns.CNAME{
Hdr: dns.RR_Header{
Name: qname,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: dn42.Ttl,
},
Target: target,
}
return []dns.RR{cnameRecord}, target, nil
} else {
return []dns.RR{}, target, nil
}
}