...

Source file src/net/dnsconfig_unix.go

Documentation: net

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !js && !windows
     6  
     7  // Read system DNS config from /etc/resolv.conf
     8  
     9  package net
    10  
    11  import (
    12  	"internal/bytealg"
    13  	"time"
    14  )
    15  
    16  // See resolv.conf(5) on a Linux machine.
    17  func dnsReadConfig(filename string) *dnsConfig {
    18  	conf := &dnsConfig{
    19  		ndots:    1,
    20  		timeout:  5 * time.Second,
    21  		attempts: 2,
    22  	}
    23  	file, err := open(filename)
    24  	if err != nil {
    25  		conf.servers = defaultNS
    26  		conf.search = dnsDefaultSearch()
    27  		conf.err = err
    28  		return conf
    29  	}
    30  	defer file.close()
    31  	if fi, err := file.file.Stat(); err == nil {
    32  		conf.mtime = fi.ModTime()
    33  	} else {
    34  		conf.servers = defaultNS
    35  		conf.search = dnsDefaultSearch()
    36  		conf.err = err
    37  		return conf
    38  	}
    39  	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
    40  		if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
    41  			// comment.
    42  			continue
    43  		}
    44  		f := getFields(line)
    45  		if len(f) < 1 {
    46  			continue
    47  		}
    48  		switch f[0] {
    49  		case "nameserver": // add one name server
    50  			if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
    51  				// One more check: make sure server name is
    52  				// just an IP address. Otherwise we need DNS
    53  				// to look it up.
    54  				if parseIPv4(f[1]) != nil {
    55  					conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
    56  				} else if ip, _ := parseIPv6Zone(f[1]); ip != nil {
    57  					conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
    58  				}
    59  			}
    60  
    61  		case "domain": // set search path to just this domain
    62  			if len(f) > 1 {
    63  				conf.search = []string{ensureRooted(f[1])}
    64  			}
    65  
    66  		case "search": // set search path to given servers
    67  			conf.search = make([]string, len(f)-1)
    68  			for i := 0; i < len(conf.search); i++ {
    69  				conf.search[i] = ensureRooted(f[i+1])
    70  			}
    71  
    72  		case "options": // magic options
    73  			for _, s := range f[1:] {
    74  				switch {
    75  				case hasPrefix(s, "ndots:"):
    76  					n, _, _ := dtoi(s[6:])
    77  					if n < 0 {
    78  						n = 0
    79  					} else if n > 15 {
    80  						n = 15
    81  					}
    82  					conf.ndots = n
    83  				case hasPrefix(s, "timeout:"):
    84  					n, _, _ := dtoi(s[8:])
    85  					if n < 1 {
    86  						n = 1
    87  					}
    88  					conf.timeout = time.Duration(n) * time.Second
    89  				case hasPrefix(s, "attempts:"):
    90  					n, _, _ := dtoi(s[9:])
    91  					if n < 1 {
    92  						n = 1
    93  					}
    94  					conf.attempts = n
    95  				case s == "rotate":
    96  					conf.rotate = true
    97  				case s == "single-request" || s == "single-request-reopen":
    98  					// Linux option:
    99  					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
   100  					// "By default, glibc performs IPv4 and IPv6 lookups in parallel [...]
   101  					//  This option disables the behavior and makes glibc
   102  					//  perform the IPv6 and IPv4 requests sequentially."
   103  					conf.singleRequest = true
   104  				case s == "use-vc" || s == "usevc" || s == "tcp":
   105  					// Linux (use-vc), FreeBSD (usevc) and OpenBSD (tcp) option:
   106  					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
   107  					// "Sets RES_USEVC in _res.options.
   108  					//  This option forces the use of TCP for DNS resolutions."
   109  					// https://www.freebsd.org/cgi/man.cgi?query=resolv.conf&sektion=5&manpath=freebsd-release-ports
   110  					// https://man.openbsd.org/resolv.conf.5
   111  					conf.useTCP = true
   112  				default:
   113  					conf.unknownOpt = true
   114  				}
   115  			}
   116  
   117  		case "lookup":
   118  			// OpenBSD option:
   119  			// https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
   120  			// "the legal space-separated values are: bind, file, yp"
   121  			conf.lookup = f[1:]
   122  
   123  		default:
   124  			conf.unknownOpt = true
   125  		}
   126  	}
   127  	if len(conf.servers) == 0 {
   128  		conf.servers = defaultNS
   129  	}
   130  	if len(conf.search) == 0 {
   131  		conf.search = dnsDefaultSearch()
   132  	}
   133  	return conf
   134  }
   135  
   136  func dnsDefaultSearch() []string {
   137  	hn, err := getHostname()
   138  	if err != nil {
   139  		// best effort
   140  		return nil
   141  	}
   142  	if i := bytealg.IndexByteString(hn, '.'); i >= 0 && i < len(hn)-1 {
   143  		return []string{ensureRooted(hn[i+1:])}
   144  	}
   145  	return nil
   146  }
   147  
   148  func hasPrefix(s, prefix string) bool {
   149  	return len(s) >= len(prefix) && s[:len(prefix)] == prefix
   150  }
   151  
   152  func ensureRooted(s string) string {
   153  	if len(s) > 0 && s[len(s)-1] == '.' {
   154  		return s
   155  	}
   156  	return s + "."
   157  }
   158  

View as plain text