本文整理匯總了Golang中net.IP.IsLoopback方法的典型用法代碼示例。如果您正苦於以下問題:Golang IP.IsLoopback方法的具體用法?Golang IP.IsLoopback怎麽用?Golang IP.IsLoopback使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.IP
的用法示例。
在下文中一共展示了IP.IsLoopback方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: IsGlobalIP
// IsGlobalIP determs passed ip address is global or not.
// if ip address is global , it returns address type(ip4 or ip6).
func IsGlobalIP(trial net.IP) string {
type localIPrange struct {
from net.IP
to net.IP
}
locals := []localIPrange{
localIPrange{net.ParseIP("10.0.0.0"), net.ParseIP("10.255.255.255")},
localIPrange{net.ParseIP("172.16.0.0"), net.ParseIP("172.31.255.255")},
localIPrange{net.ParseIP("192.168.0.0"), net.ParseIP("192.168.255.255")}}
if trial == nil || trial.IsLoopback() {
return ""
}
//for udp6
if trial.To4() == nil {
if trial.IsGlobalUnicast() {
return "ip6"
}
return ""
}
//for udp4
for _, r := range locals {
if bytes.Compare(trial, r.from) >= 0 && bytes.Compare(trial, r.to) <= 0 {
return ""
}
}
return "ip4"
}
示例2: getIPStringAddresses
// Get IP addresses
//
// http://play.golang.org/p/BDt3qEQ_2H
func getIPStringAddresses() []string {
addrStrings := []string{}
if ifaces, err := net.Interfaces(); err == nil {
for _, iface := range ifaces {
// skip
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
if addrs, err := iface.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue
}
addrStrings = append(addrStrings, ip.String())
}
}
}
}
return addrStrings
}
示例3: LocalIpv4Addrs
// LocalIpv4Addrs scan all ip addresses with loopback excluded.
func LocalIpv4Addrs() ([]string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
ips := make([]string, 0)
for _, addr := range addrs {
var ip net.IP
switch x := addr.(type) {
case *net.IPNet:
ip = x.IP
case *net.IPAddr:
ip = x.IP
default:
err = fmt.Errorf("unknown interface address type for: %+v", x)
return nil, err
}
if ip.IsLoopback() || ip.To4() == nil {
// loopback excluded, ipv6 excluded
continue
}
ips = append(ips, ip.String())
}
return ips, nil
}
示例4: getv4loopback
func getv4loopback() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback != 0 {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
ip = ip.To4()
if ip == nil {
continue
}
if ip.IsLoopback() {
return ip.String(), nil
}
}
}
}
return "", errors.New("you do not have loopback?")
}
示例5: getIP
func getIP() net.IP {
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return nil
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
return ip
}
}
return net.IPv6loopback
}
示例6: getFirstLocalIPAddr
// getFirstLocalIPAddr returns the first available IP address of the local machine
// This is a fix for Beaglebone Black where net.LookupIP(hostname) return no IP address.
func getFirstLocalIPAddr() (net.IP, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() || ip.IsUnspecified() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip, nil
}
return nil, errors.New("Could not determine ip address")
}
示例7: findLoopbackDevice
// findLoopbackDevice iterates through all the interfaces on a machine and
// returns the ip addr, mask of the loopback device
func (a *Agent) findLoopbackDevice() (string, string, string, error) {
var ifcs []net.Interface
var err error
ifcs, err = net.Interfaces()
if err != nil {
return "", "", "", err
}
for _, ifc := range ifcs {
addrs, err := ifc.Addrs()
if err != nil {
return "", "", "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.IsLoopback() {
if ip.To4() == nil {
continue
}
return ifc.Name, ip.String(), addr.String(), nil
}
}
}
return "", "", "", fmt.Errorf("no loopback devices with IPV4 addr found")
}
示例8: validateRemoteAddr
func validateRemoteAddr(ip net.IP) bool {
if ip == nil {
return false
}
if ip.IsInterfaceLocalMulticast() {
return false
}
if ip.IsLinkLocalMulticast() {
return false
}
if ip.IsLinkLocalUnicast() {
return false
}
if ip.IsLoopback() {
return false
}
if ip.IsMulticast() {
return false
}
if ip.IsUnspecified() {
return false
}
if isBroadcasty(ip) {
return false
}
return true
}
示例9: getIfaceIP
func getIfaceIP(iface string) string {
i, err := net.InterfaceByName(iface)
if err != nil {
time.Sleep(time.Second * 5)
panic(err)
}
addrs, err := i.Addrs()
if err != nil {
time.Sleep(time.Second * 5)
panic(err)
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String()
}
panic("No IP for " + iface)
}
示例10: addrsOfOneInterface
// 一個接口上的所有ip4地址
func addrsOfOneInterface(iface net.Interface) (addrs []*net.IPAddr) {
ifaddrs, err := iface.Addrs()
if (err != nil) || (len(ifaddrs) == 0) {
return
}
for _, ifaddr := range ifaddrs {
var ip net.IP
switch v := ifaddr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
}
if ip.IsLoopback() {
return
}
ip = ip.To4()
if ip != nil {
addr, _ := net.ResolveIPAddr("ip", ip.String())
addrs = append(addrs, addr)
}
}
return
}
示例11: GetLocalIP
// GetLocalIP return the first external-IP4 configured for the first
// interface connected to this node.
func GetLocalIP() (net.IP, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range interfaces {
if (iface.Flags & net.FlagUp) == 0 {
continue // interface down
}
if (iface.Flags & net.FlagLoopback) != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip != nil && !ip.IsLoopback() {
if ip = ip.To4(); ip != nil {
return ip, nil
}
}
}
}
return nil, errors.New("cannot find local IP address")
}
示例12: macAddress
func macAddress() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
// interface down or loopback interface
continue
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() || ip.To4() == nil {
continue
}
return iface.HardwareAddr.String(), nil
}
}
return "", errors.New("not connected to the network")
}
示例13: printInterfaces
// print interfaces to know where the proxy is listening
func printInterfaces() {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println("Can't get interfaces. You have to have at least one network connection.")
log.Fatal("No interface found")
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPAddr:
case *net.IPNet:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
fmt.Println("http://" + ip.String() + Config.Service.Listen)
}
}
示例14: GetIPAddress
// GetIPAddress : Used to get IP of the running machine
func GetIPAddress() string {
ifaces, _ := net.Interfaces()
// handle err
for _, i := range ifaces {
addrs, _ := i.Addrs()
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String()
// process IP address
}
}
return ""
}
示例15: GetFirstInterface
func GetFirstInterface() (name string, ip string) {
ifaces, _ := net.Interfaces()
for _, iface := range ifaces {
addrs, _ := iface.Addrs()
ipV4 := false
ipAddrs := []string{}
for _, addr := range addrs {
var ip net.IP
if ipnet, ok := addr.(*net.IPNet); ok {
ip = ipnet.IP
} else if ipaddr, ok := addr.(*net.IPAddr); ok {
ip = ipaddr.IP
}
if ip != nil && ip.To4() != nil && !ip.IsLoopback() {
ipstr := addr.String()
idx := strings.Index(ipstr, "/")
if idx >= 0 {
ipstr = ipstr[:idx]
}
ipAddrs = append(ipAddrs, ipstr)
ipV4 = true
}
}
if !ipV4 {
continue
}
return iface.Name, ipAddrs[0]
}
return "", "0.0.0.0"
}