-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnsparse.go
More file actions
58 lines (48 loc) · 1.07 KB
/
dnsparse.go
File metadata and controls
58 lines (48 loc) · 1.07 KB
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
// Source taken from https://github.com/majek/goplayground/tree/master/resolve
package main
import (
"fmt"
"os"
"net"
)
func unpackDns(msg []byte, dnsType uint16) (domain string, id uint16, ips []net.IP) {
d := new(dnsMsg)
if !d.Unpack(msg) {
// fmt.Fprintf(os.Stderr, "dns error (unpacking)\n")
return
}
id = d.id
if len(d.question) < 1 {
// fmt.Fprintf(os.Stderr, "dns error (wrong question section)\n")
return
}
domain = d.question[0].Name
if len(domain) < 1 {
// fmt.Fprintf(os.Stderr, "dns error (wrong domain in question)\n")
return
}
_, addrs, err := answer(domain, "server", d, dnsType)
if err == nil {
switch (dnsType) {
case dnsTypeA:
ips = convertRR_A(addrs)
case dnsTypeAAAA:
ips = convertRR_AAAA(addrs)
}
}
return
}
func packDns(domain string, id uint16, dnsType uint16) []byte {
out := new(dnsMsg)
out.id = id
out.recursion_desired = true
out.question = []dnsQuestion{
{domain, dnsType, dnsClassINET},
}
msg, ok := out.Pack()
if !ok {
fmt.Fprintf(os.Stderr, "can't pack domain %s\n", domain)
os.Exit(1)
}
return msg
}