2011-03-08 18:27:36 +11:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2013-05-13 00:07:06 +10:00
|
|
|
// Extensions of the original work are copyright (c) 2011 Miek Gieben
|
2011-03-08 18:27:36 +11:00
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2011-12-10 07:45:57 +11:00
|
|
|
"os"
|
2011-03-08 18:27:36 +11:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2011-07-05 05:32:39 +10:00
|
|
|
// Wraps the contents of the /etc/resolv.conf.
|
2011-03-29 19:38:54 +11:00
|
|
|
type ClientConfig struct {
|
2011-06-02 20:31:12 +10:00
|
|
|
Servers []string // servers to use
|
|
|
|
Search []string // suffixes to append to local name
|
|
|
|
Port string // what port to use
|
|
|
|
Ndots int // number of dots in name to trigger absolute lookup
|
|
|
|
Timeout int // seconds before giving up on packet
|
2013-01-29 05:10:13 +11:00
|
|
|
Attempts int // lost packets before giving up on server, not used in the package dns
|
2011-03-29 19:38:54 +11:00
|
|
|
}
|
|
|
|
|
2011-12-10 02:13:59 +11:00
|
|
|
// ClientConfigFromFile parses a resolv.conf(5) like file and returns
|
|
|
|
// a *ClientConfig.
|
2014-01-19 18:45:16 +11:00
|
|
|
func ClientConfigFromFile(resolvconf string) (*ClientConfig, error) {
|
|
|
|
file, err := os.Open(resolvconf)
|
2011-03-08 18:27:36 +11:00
|
|
|
if err != nil {
|
2011-03-29 02:19:26 +11:00
|
|
|
return nil, err
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|
2013-05-11 09:21:13 +10:00
|
|
|
defer file.Close()
|
2011-06-02 20:31:12 +10:00
|
|
|
c := new(ClientConfig)
|
2011-03-08 18:27:36 +11:00
|
|
|
b := bufio.NewReader(file)
|
2012-10-12 19:53:38 +11:00
|
|
|
c.Servers = make([]string, 0)
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Search = make([]string, 0)
|
2011-07-24 07:43:43 +10:00
|
|
|
c.Port = "53"
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Ndots = 1
|
|
|
|
c.Timeout = 5
|
|
|
|
c.Attempts = 2
|
2011-03-08 18:27:36 +11:00
|
|
|
for line, ok := b.ReadString('\n'); ok == nil; line, ok = b.ReadString('\n') {
|
|
|
|
f := strings.Fields(line)
|
|
|
|
if len(f) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch f[0] {
|
|
|
|
case "nameserver": // add one name server
|
2012-10-12 19:53:38 +11:00
|
|
|
if len(f) > 1 {
|
2011-03-08 18:27:36 +11:00
|
|
|
// One more check: make sure server name is
|
|
|
|
// just an IP address. Otherwise we need DNS
|
|
|
|
// to look it up.
|
|
|
|
name := f[1]
|
2012-12-27 03:58:18 +11:00
|
|
|
c.Servers = append(c.Servers, name)
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
case "domain": // set search path to just this domain
|
|
|
|
if len(f) > 1 {
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Search = make([]string, 1)
|
|
|
|
c.Search[0] = f[1]
|
2011-03-08 18:27:36 +11:00
|
|
|
} else {
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Search = make([]string, 0)
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
case "search": // set search path to given servers
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Search = make([]string, len(f)-1)
|
|
|
|
for i := 0; i < len(c.Search); i++ {
|
|
|
|
c.Search[i] = f[i+1]
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
case "options": // magic options
|
|
|
|
for i := 1; i < len(f); i++ {
|
|
|
|
s := f[i]
|
|
|
|
switch {
|
|
|
|
case len(s) >= 6 && s[:6] == "ndots:":
|
|
|
|
n, _ := strconv.Atoi(s[6:])
|
|
|
|
if n < 1 {
|
|
|
|
n = 1
|
|
|
|
}
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Ndots = n
|
2011-03-08 18:27:36 +11:00
|
|
|
case len(s) >= 8 && s[:8] == "timeout:":
|
|
|
|
n, _ := strconv.Atoi(s[8:])
|
|
|
|
if n < 1 {
|
|
|
|
n = 1
|
|
|
|
}
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Timeout = n
|
2011-03-08 18:27:36 +11:00
|
|
|
case len(s) >= 8 && s[:9] == "attempts:":
|
|
|
|
n, _ := strconv.Atoi(s[9:])
|
|
|
|
if n < 1 {
|
|
|
|
n = 1
|
|
|
|
}
|
2011-03-29 19:38:54 +11:00
|
|
|
c.Attempts = n
|
2011-03-08 18:27:36 +11:00
|
|
|
case s == "rotate":
|
2011-03-24 19:24:24 +11:00
|
|
|
/* not imp */
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 19:38:54 +11:00
|
|
|
return c, nil
|
2011-03-08 18:27:36 +11:00
|
|
|
}
|