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

package net

import (
	
	
	
	
	
	
)

const cacheMaxAge = 5 * time.Second

func ( string) string {
	,  := netip.ParseAddr()
	if  != nil {
		return ""
	}
	return .String()
}

type byName struct {
	addrs         []string
	canonicalName string
}

// hosts contains known host entries.
var hosts struct {
	sync.Mutex

	// Key for the list of literal IP addresses must be a host
	// name. It would be part of DNS labels, a FQDN or an absolute
	// FQDN.
	// For now the key is converted to lower case for convenience.
	byName map[string]byName

	// Key for the list of host names must be a literal IP address
	// including IPv6 address with zone identifier.
	// We don't support old-classful IP address notation.
	byAddr map[string][]string

	expire time.Time
	path   string
	mtime  time.Time
	size   int64
}

func () {
	 := time.Now()
	 := testHookHostsPath

	if .Before(hosts.expire) && hosts.path ==  && len(hosts.byName) > 0 {
		return
	}
	, ,  := stat()
	if  == nil && hosts.path ==  && hosts.mtime.Equal() && hosts.size ==  {
		hosts.expire = .Add(cacheMaxAge)
		return
	}

	 := make(map[string]byName)
	 := make(map[string][]string)

	,  := open()
	if  != nil {
		if !errors.Is(, fs.ErrNotExist) && !errors.Is(, fs.ErrPermission) {
			return
		}
	}

	if  != nil {
		defer .close()
		for ,  := .readLine(); ; ,  = .readLine() {
			if  := bytealg.IndexByteString(, '#');  >= 0 {
				// Discard comments.
				 = [0:]
			}
			 := getFields()
			if len() < 2 {
				continue
			}
			 := parseLiteralIP([0])
			if  == "" {
				continue
			}

			var  string
			for  := 1;  < len(); ++ {
				 := absDomainName([])
				 := []byte([])
				lowerASCIIBytes()
				 := absDomainName(string())

				if  == 1 {
					 = 
				}

				[] = append([], )

				if ,  := [];  {
					[] = byName{
						addrs:         append(.addrs, ),
						canonicalName: .canonicalName,
					}
					continue
				}

				[] = byName{
					addrs:         []string{},
					canonicalName: ,
				}
			}
		}
	}
	// Update the data cache.
	hosts.expire = .Add(cacheMaxAge)
	hosts.path = 
	hosts.byName = 
	hosts.byAddr = 
	hosts.mtime = 
	hosts.size = 
}

// lookupStaticHost looks up the addresses and the canonical name for the given host from /etc/hosts.
func ( string) ([]string, string) {
	hosts.Lock()
	defer hosts.Unlock()
	readHosts()
	if len(hosts.byName) != 0 {
		if hasUpperCase() {
			 := []byte()
			lowerASCIIBytes()
			 = string()
		}
		if ,  := hosts.byName[absDomainName()];  {
			 := make([]string, len(.addrs))
			copy(, .addrs)
			return , .canonicalName
		}
	}
	return nil, ""
}

// lookupStaticAddr looks up the hosts for the given address from /etc/hosts.
func ( string) []string {
	hosts.Lock()
	defer hosts.Unlock()
	readHosts()
	 = parseLiteralIP()
	if  == "" {
		return nil
	}
	if len(hosts.byAddr) != 0 {
		if ,  := hosts.byAddr[];  {
			 := make([]string, len())
			copy(, )
			return 
		}
	}
	return nil
}