summaryrefslogtreecommitdiff
path: root/src/pkg/net/dnsconfig.go
blob: 243a4b431af4973e8bd9a14bb19dcfbe3c21df30 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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.

// Read system DNS config from /etc/resolv.conf

package net

import "os"

type _DNS_Config struct {
	servers		[]string;	// servers to use
	search		[]string;	// suffixes to append to local name
	ndots		int;		// number of dots in name to trigger absolute lookup
	timeout		int;		// seconds before giving up on packet
	attempts	int;		// lost packets before giving up on server
	rotate		bool;		// round robin among servers
}

var _DNS_configError os.Error

// See resolv.conf(5) on a Linux machine.
// TODO(rsc): Supposed to call uname() and chop the beginning
// of the host name to get the default search domain.
// We assume it's in resolv.conf anyway.
func _DNS_ReadConfig() (*_DNS_Config, os.Error) {
	file, err := open("/etc/resolv.conf");
	if err != nil {
		return nil, err
	}
	conf := new(_DNS_Config);
	conf.servers = make([]string, 3)[0:0];	// small, but the standard limit
	conf.search = make([]string, 0);
	conf.ndots = 1;
	conf.timeout = 1;
	conf.attempts = 1;
	conf.rotate = false;
	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
		f := getFields(line);
		if len(f) < 1 {
			continue
		}
		switch f[0] {
		case "nameserver":	// add one name server
			a := conf.servers;
			n := len(a);
			if len(f) > 1 && n < cap(a) {
				// One more check: make sure server name is
				// just an IP address.  Otherwise we need DNS
				// to look it up.
				name := f[1];
				if len(ParseIP(name)) != 0 {
					a = a[0 : n+1];
					a[n] = name;
					conf.servers = a;
				}
			}

		case "domain":	// set search path to just this domain
			if len(f) > 1 {
				conf.search = make([]string, 1);
				conf.search[0] = f[1];
			} else {
				conf.search = make([]string, 0)
			}

		case "search":	// set search path to given servers
			conf.search = make([]string, len(f)-1);
			for i := 0; i < len(conf.search); i++ {
				conf.search[i] = f[i+1]
			}

		case "options":	// magic options
			for i := 1; i < len(f); i++ {
				s := f[i];
				switch {
				case len(s) >= 6 && s[0:6] == "ndots:":
					n, _, _ := dtoi(s, 6);
					if n < 1 {
						n = 1
					}
					conf.ndots = n;
				case len(s) >= 8 && s[0:8] == "timeout:":
					n, _, _ := dtoi(s, 8);
					if n < 1 {
						n = 1
					}
					conf.timeout = n;
				case len(s) >= 8 && s[0:9] == "attempts:":
					n, _, _ := dtoi(s, 9);
					if n < 1 {
						n = 1
					}
					conf.attempts = n;
				case s == "rotate":
					conf.rotate = true
				}
			}
		}
	}
	file.close();

	return conf, nil;
}