Debian Codegraph @

clientconfig.go

raw symbols SCIP
1 package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/
2
3 import (
4 "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/"
5 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
6 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
7 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
8 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
9 )
10
11 // ClientConfig wraps the contents of the /etc/resolv.conf file.
12 type ClientConfigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig# struct {
13 Serversscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Servers. []string // servers to use
14 Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. []string // suffixes to append to local name
15 Portscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Port. string // what port to use
16 Ndotsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Ndots. int // number of dots in name to trigger absolute lookup
17 Timeoutscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Timeout. int // seconds before giving up on packet
18 Attemptsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Attempts. int // lost packets before giving up on server, not used in the package dns
19 }
20
21 // ClientConfigFromFile parses a resolv.conf(5) like file and returns
22 // a *ClientConfig.
23 func ClientConfigFromFilescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfigFromFile().(resolvconf string) (*ClientConfigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#, error) {
24 file, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(resolvconf)
25 if err != nil {
26 return nil, err
27 }
28 defer file.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().()
29 return ClientConfigFromReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfigFromReader().(file)
30 }
31
32 // ClientConfigFromReader works like ClientConfigFromFile but takes an io.Reader as argument
33 func ClientConfigFromReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfigFromReader().(resolvconf ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#) (*ClientConfigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#, error) {
34 c := new(ClientConfigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#)
35 scanner := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(resolvconf)
36 c.Serversscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Servers. = make([]string, 0)
37 c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. = make([]string, 0)
38 c.Portscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Port. = "53"
39 c.Ndotsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Ndots. = 1
40 c.Timeoutscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Timeout. = 5
41 c.Attemptsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Attempts. = 2
42
43 for scanner.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() {
44 if err := scanner.Errscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Err().(); err != nil {
45 return nil, err
46 }
47 line := scanner.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().()
48 f := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 strings/Fields().(line)
49 if len(f) < 1 {
50 continue
51 }
52 switch f[0] {
53 case "nameserver": // add one name server
54 if len(f) > 1 {
55 // One more check: make sure server name is
56 // just an IP address. Otherwise we need DNS
57 // to look it up.
58 name := f[1]
59 c.Serversscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Servers. = append(c.Serversscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Servers., name)
60 }
61
62 case "domain": // set search path to just this domain
63 if len(f) > 1 {
64 c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. = make([]string, 1)
65 c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search.[0] = f[1]
66 } else {
67 c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. = make([]string, 0)
68 }
69
70 case "search": // set search path to given servers
71 c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. = cloneSlicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/cloneSlice().(f[1:])
72
73 case "options": // magic options
74 for _, s := range f[1:] {
75 switch {
76 case len(s) >= 6 && s[:6] == "ndots:":
77 n, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(s[6:])
78 if n < 0 {
79 n = 0
80 } else if n > 15 {
81 n = 15
82 }
83 c.Ndotsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Ndots. = n
84 case len(s) >= 8 && s[:8] == "timeout:":
85 n, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(s[8:])
86 if n < 1 {
87 n = 1
88 }
89 c.Timeoutscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Timeout. = n
90 case len(s) >= 9 && s[:9] == "attempts:":
91 n, _ := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(s[9:])
92 if n < 1 {
93 n = 1
94 }
95 c.Attemptsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Attempts. = n
96 case s == "rotate":
97 /* not imp */
98 }
99 }
100 }
101 }
102 return c, nil
103 }
104
105 // NameList returns all of the names that should be queried based on the
106 // config. It is based off of go's net/dns name building, but it does not
107 // check the length of the resulting names.
108 func (c *ClientConfigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#) NameListscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#NameList().(name string) []string {
109 // if this domain is already fully qualified, no append needed.
110 if IsFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsFqdn().(name) {
111 return []string{name}
112 }
113
114 // Check to see if the name has more labels than Ndots. Do this before making
115 // the domain fully qualified.
116 hasNdots := CountLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/CountLabel().(name) > c.Ndotsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Ndots.
117 // Make the domain fully qualified.
118 name = Fqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Fqdn().(name)
119
120 // Make a list of names based off search.
121 names := []string{}
122
123 // If name has enough dots, try that first.
124 if hasNdots {
125 names = append(names, name)
126 }
127 for _, s := range c.Searchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClientConfig#Search. {
128 names = append(names, Fqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Fqdn().(name+s))
129 }
130 // If we didn't have enough dots, try after suffixes.
131 if !hasNdots {
132 names = append(names, name)
133 }
134 return names
135 }