// lookupIP looks up an IP address corresponding to the given name.
// It also returns a slice of other hostnames it found for the same IP.
lookupIP looks up an IP address corresponding to the given name.
It also returns a slice of other hostnames it found for the same IP.
// lookupIP looks up an IP address corresponding to the given name . // It also returns a slice of other hostnames it found for the same IP .
func lookupIP(hostFile string, addr string) (net.IP, []string, error) {
	var err error
	if hostFile != `` {
		var f *os.File
		if f, err = os.Open(hostFile); err != nil {
			return nil, nil, err
		}
		defer f.Close()
		scan := bufio.NewScanner(f)
		for scan.Scan() {
			fields := strings.Fields(scan.Text())
			if len(fields) < 2 || strings.HasPrefix(fields[0], "#") {
				continue
			}
			var hostnames []string
			for _, fld := range fields[1:] {
				if strings.ToLower(fld) == strings.ToLower(addr) {
					return net.ParseIP(fields[0]), hostnames, nil
				}
				hostnames = append(hostnames, fld)
			}
		}
	}
	ips, err := net.LookupIP(addr)
	if err != nil {
		return nil, nil, err
	}
	if len(ips) == 0 {
		return nil, nil, errors.New("No IP found")
	}
	ip := ips[0]
	names, err := net.LookupAddr(ip.String())
	return ip, names, err
}
func lookupIP ( hostFile string , addr string ) ( net . IP , [ ] string , error ) { var err error if hostFile != `` { var f * os . File if f , err = os . Open ( hostFile ) ; err != nil { return nil , nil , err } defer f . Close ( ) scan := bufio . NewScanner ( f ) for scan . Scan ( ) { fields := strings . Fields ( scan . Text ( ) ) if len ( fields ) < 2 || strings . HasPrefix ( fields [ 0 ] , " " ) { continue } var hostnames [ ] string for _ , fld := range fields [ 1 : ] { if strings . ToLower ( fld ) == strings . ToLower ( addr ) { return net . ParseIP ( fields [ 0 ] ) , hostnames , nil } hostnames = append ( hostnames , fld ) } } } ips , err := net . LookupIP ( addr ) if err != nil { return nil , nil , err } if len ( ips ) == 0 { return nil , nil , errors . New ( " " ) } ip := ips [ 0 ] names , err := net . LookupAddr ( ip . String ( ) ) return ip , names , err }
lookupIP looks up an IP address corresponding to the given name.
lookupIP looks up an IP address corresponding to the given name .
// First try the override // Read the file and walk each line looking for a match. // We do this so you can update the file without restarting the server // We're going to be real simple-minded. We take each line to consist of // one IP, followed by one or more whitespace-separated hostnames, with // the mac address at the end: // <ip> <hostname>... <mac> // Now just do a regular lookup since we didn't find it in the override