1package rootservers
2
3import (
4 "bytes"
5 "net"
6 "os"
7)
8
9// should probally just be a part of magna
10func DecodeRootHints(path string) ([]string, error) {
11 rootServers := make([]string, 0)
12
13 data, err := os.ReadFile(path)
14 if err != nil {
15 return rootServers, err
16 }
17
18 for _, line := range bytes.Split(data, []byte{'\n'}) {
19 if len(line) == 0 {
20 continue
21 }
22
23 // skip comments
24 if line[0] == ';' {
25 continue
26 }
27
28 // xxx: not a great way to do this should probally just be a zone
29 // custom parser
30 fields := bytes.Fields(line)
31 if len(fields) != 4 {
32 continue
33 }
34
35 // only supports ipv4 for now, need to do testing with ipv6 and support
36 // https://datatracker.ietf.org/doc/html/rfc3596 in magna
37 if bytes.Equal(fields[2], []byte{'A'}) {
38 if address := net.ParseIP(string(fields[3])); address != nil {
39 rootServers = append(rootServers, address.String())
40 }
41 }
42 }
43
44 return rootServers, nil
45}