當前位置: 首頁>>代碼示例>>Golang>>正文


Golang IP.DefaultMask方法代碼示例

本文整理匯總了Golang中net.IP.DefaultMask方法的典型用法代碼示例。如果您正苦於以下問題:Golang IP.DefaultMask方法的具體用法?Golang IP.DefaultMask怎麽用?Golang IP.DefaultMask使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.IP的用法示例。


在下文中一共展示了IP.DefaultMask方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getAddresses

func getAddresses(iface net.Interface) (addresses map[string]interface{}, err error) {
	addresses = make(map[string]interface{})

	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}
	for _, addr := range addrs {
		var ip net.IP
		addressInfo := make(map[string]string)
		switch v := addr.(type) {
		case *net.IPNet:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		}

		netmask := ip.DefaultMask()
		if netmask != nil {
			addressInfo["family"] = "inet"
		} else {
			addressInfo["family"] = "inet6"
		}
		addresses[ip.String()] = addressInfo
	}

	mac := iface.HardwareAddr
	if mac != nil {
		addressInfo := make(map[string]string)
		addressInfo["family"] = "lladdr"
		addresses[mac.String()] = addressInfo
	}

	return
}
開發者ID:kentaro,項目名稱:verity,代碼行數:35,代碼來源:network.go

示例2: Mask

func Mask(addr net.IP) {
	mask := addr.DefaultMask()
	network := addr.Mask(mask)
	ones, bits := mask.Size()
	fmt.Println("Address is ", addr.String(),
		" Default mask length is ", bits,
		"Leading ones count is ", ones,
		"Mask is (hex) ", mask.String(),
		" Network is ", network.String())
}
開發者ID:taysom,項目名稱:va,代碼行數:10,代碼來源:net.go

示例3: SpanningCIDR

// SpanningCIDR computes network covers given IP addresses
func SpanningCIDR(first, last net.IP) *net.IPNet {
	_, bits := last.DefaultMask().Size()

	var network net.IPNet
	for ones := bits; !network.Contains(first); ones-- {
		network.Mask = net.CIDRMask(ones, bits)
		network.IP = last.Mask(network.Mask)
	}
	return &network
}
開發者ID:hanjianwei,項目名稱:cidr-lite,代碼行數:11,代碼來源:cidr.go

示例4: trackIp

func (f *summaryFormatter) trackIp(ip net.IP) {
	for _, net := range f.ipAddrs {
		if net.Contains(ip) {
			return
		}
	}

	ipNet := net.IPNet{ip, ip.DefaultMask()}
	f.ipAddrs = append(f.ipAddrs, ipNet)
	f.netStrings = append(f.netStrings, ipNet.String())
}
開發者ID:AlexisBruemmer,項目名稱:juju,代碼行數:11,代碼來源:output_summary.go

示例5: main

func main() {
	mn := new(MyNet)
	ifaces, err := net.Interfaces()
	if err != nil {
		log.Fatal(err)
	}
	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 {
			log.Fatal(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
				continue
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			ip = ip.To4()
			if ip == nil {
				continue // not an ipv4 address
			}
			fmt.Println("IP: ", ip)
			fmt.Println("Subnet: ", ip.Mask(ip.DefaultMask()))
			mn.IP = addr.(*net.IPNet)
			mn.Subnet = ip.Mask(ip.DefaultMask())
		}
	}
	fmt.Println(mn)
	mn.GetNetwork()
}
開發者ID:gxb5443,項目名稱:go-discovery,代碼行數:42,代碼來源:main.go

示例6: classfulSubnet

func classfulSubnet(sn net.IP) net.IPNet {
	return net.IPNet{
		IP:   sn,
		Mask: sn.DefaultMask(),
	}
}
開發者ID:matomesc,項目名稱:rkt,代碼行數:6,代碼來源:options.go

示例7: IsIPv4

func IsIPv4(ip net.IP) bool {
	return ip.DefaultMask() != nil
}
開發者ID:ScriptRock,項目名稱:peerdiscovery,代碼行數:3,代碼來源:etcd.go


注:本文中的net.IP.DefaultMask方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。