blob: 9c16a8cfcd24aa8a07ecd0bd81509f905145a993 [file] [log] [blame]
// Copyright 2016 The Upspin 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 serverutil
import (
"net"
"upspin.io/rpc/local"
)
// IsLoopback returns true if the name only resolves to loopback addresses.
func IsLoopback(addr string) bool {
host, _, err := net.SplitHostPort(addr)
if err != nil {
host = addr
}
if host == "localhost" || host == "127.0.0.1" || host == "::1" {
return true
}
// Check for local IPC.
if local.IsLocal(host) {
return true
}
// Check for loopback network.
ips, err := net.LookupIP(host)
if err != nil {
return false
}
for _, ip := range ips {
if !ip.IsLoopback() {
return false
}
}
return true
}