// 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.

//go:build !windows

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

package net

import (
	
	
	
	
)

// See resolv.conf(5) on a Linux machine.
func dnsReadConfig( string) *dnsConfig {
	 := &dnsConfig{
		ndots:    1,
		timeout:  5 * time.Second,
		attempts: 2,
	}
	,  := open()
	if  != nil {
		.servers = defaultNS
		.search = dnsDefaultSearch()
		.err = 
		return 
	}
	defer .close()
	if ,  := .file.Stat();  == nil {
		.mtime = .ModTime()
	} else {
		.servers = defaultNS
		.search = dnsDefaultSearch()
		.err = 
		return 
	}
	for ,  := .readLine(); ; ,  = .readLine() {
		if len() > 0 && ([0] == ';' || [0] == '#') {
			// comment.
			continue
		}
		 := getFields()
		if len() < 1 {
			continue
		}
		switch [0] {
		case "nameserver": // add one name server
			if len() > 1 && len(.servers) < 3 { // small, but the standard limit
				// One more check: make sure server name is
				// just an IP address. Otherwise we need DNS
				// to look it up.
				if ,  := netip.ParseAddr([1]);  == nil {
					.servers = append(.servers, JoinHostPort([1], "53"))
				}
			}

		case "domain": // set search path to just this domain
			if len() > 1 {
				.search = []string{ensureRooted([1])}
			}

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

		case "options": // magic options
			for ,  := range [1:] {
				switch {
				case stringslite.HasPrefix(, "ndots:"):
					, ,  := dtoi([6:])
					if  < 0 {
						 = 0
					} else if  > 15 {
						 = 15
					}
					.ndots = 
				case stringslite.HasPrefix(, "timeout:"):
					, ,  := dtoi([8:])
					if  < 1 {
						 = 1
					}
					.timeout = time.Duration() * time.Second
				case stringslite.HasPrefix(, "attempts:"):
					, ,  := dtoi([9:])
					if  < 1 {
						 = 1
					}
					.attempts = 
				case  == "rotate":
					.rotate = true
				case  == "single-request" ||  == "single-request-reopen":
					// Linux option:
					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
					// "By default, glibc performs IPv4 and IPv6 lookups in parallel [...]
					//  This option disables the behavior and makes glibc
					//  perform the IPv6 and IPv4 requests sequentially."
					.singleRequest = true
				case  == "use-vc" ||  == "usevc" ||  == "tcp":
					// Linux (use-vc), FreeBSD (usevc) and OpenBSD (tcp) option:
					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
					// "Sets RES_USEVC in _res.options.
					//  This option forces the use of TCP for DNS resolutions."
					// https://www.freebsd.org/cgi/man.cgi?query=resolv.conf&sektion=5&manpath=freebsd-release-ports
					// https://man.openbsd.org/resolv.conf.5
					.useTCP = true
				case  == "trust-ad":
					.trustAD = true
				case  == "edns0":
					// We use EDNS by default.
					// Ignore this option.
				case  == "no-reload":
					.noReload = true
				default:
					.unknownOpt = true
				}
			}

		case "lookup":
			// OpenBSD option:
			// https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
			// "the legal space-separated values are: bind, file, yp"
			.lookup = [1:]

		default:
			.unknownOpt = true
		}
	}
	if len(.servers) == 0 {
		.servers = defaultNS
	}
	if len(.search) == 0 {
		.search = dnsDefaultSearch()
	}
	return 
}

func dnsDefaultSearch() []string {
	,  := getHostname()
	if  != nil {
		// best effort
		return nil
	}
	if  := bytealg.IndexByteString(, '.');  >= 0 &&  < len()-1 {
		return []string{ensureRooted([+1:])}
	}
	return nil
}

func ensureRooted( string) string {
	if len() > 0 && [len()-1] == '.' {
		return 
	}
	return  + "."
}