forked from presotto/go-mdns-sd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnsaux.go
80 lines (69 loc) · 2.17 KB
/
dnsaux.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mdns
// Helper routines for creating/manipulating dns messages.
import (
"net"
dns "github.com/vanadium/go-mdns-sd/go_dns"
)
// Create a new (unpacked) dns message and initialize the fields.
func newDnsMsg(id uint16, response, authoritative bool) *dns.Msg {
msg := new(dns.Msg)
msg.ID = id
msg.Response = response
msg.Authoritative = authoritative
msg.Question = make([]dns.Question, 0)
msg.Answer = make([]dns.RR, 0, 0)
msg.NS = make([]dns.RR, 0, 0)
msg.Extra = make([]dns.RR, 0, 0)
return msg
}
// Returns an A or AAAA RR, whichever is appropriate for the passed in address.
func NewAddressRR(name string, class uint16, ttl uint32, ip net.IP) dns.RR {
var rr dns.RR
if v4 := ip.To4(); v4 != nil {
rra := new(dns.RR_A)
rra.A = (uint32(v4[0]) << 24) | (uint32(v4[1]) << 16) | (uint32(v4[2]) << 8) | uint32(v4[3])
rra.Header().Rrtype = dns.TypeA
rr = rra
} else {
rraaaa := new(dns.RR_AAAA)
copy(rraaaa.AAAA[:], ip)
rraaaa.Header().Rrtype = dns.TypeAAAA
rr = rraaaa
}
rr.Header().Name = name
rr.Header().Class = class
rr.Header().Ttl = ttl
return rr
}
// Returns a SRV RR.
func NewSrvRR(name string, class uint16, ttl uint32, target string, port, priority, weight uint16) dns.RR {
return &dns.RR_SRV{dns.RR_Header{name, dns.TypeSRV, class, ttl, 0}, priority, weight, port, target}
}
// Returns a TXT RR. This is a limited TXT RR that can contain only one string
func NewTxtRR(name string, class uint16, ttl uint32, txt []string) dns.RR {
if txt == nil {
txt = []string{""}
}
return &dns.RR_TXT{dns.RR_Header{name, dns.TypeTXT, class, ttl, 0}, txt}
}
// Returns a PTR RR.
func NewPtrRR(name string, class uint16, ttl uint32, ptr string) dns.RR {
return &dns.RR_PTR{dns.RR_Header{name, dns.TypePTR, class, ttl, 0}, ptr}
}
// Convert an A RR into a net.IP
func AtoIP(rr *dns.RR_A) net.IP {
ip := make([]byte, 4)
ip[0] = byte(rr.A >> 24)
ip[1] = byte(rr.A >> 16)
ip[2] = byte(rr.A >> 8)
ip[3] = byte(rr.A)
return ip
}
// Convert an AAAA RR into a net.IP
func AAAAtoIP(rr *dns.RR_AAAA) net.IP {
ip := make([]byte, 16)
copy(ip, rr.AAAA[:])
return ip
}