示例#1
0
文件: net.go 项目: neviim/scope
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
}
示例#2
0
func InterfaceIPs() ([]*net.IPAddr, error) {
	var (
		addrs []*net.IPAddr
	)

	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}
	for _, iface := range ifaces {
		iaddrs, err := iface.Addrs()
		if err != nil {
			return nil, err
		}

		for _, iaddr := range iaddrs {
			var (
				ip   net.IP
				zone string
			)

			switch x := iaddr.(type) {
			case *net.IPAddr:
				ip = x.IP
				zone = x.Zone
			case *net.IPNet:
				ip = x.IP
				zone = ""
			}

			if ip.IsMulticast() ||
				ip.IsUnspecified() ||
				ip.IsInterfaceLocalMulticast() ||
				ip.IsLinkLocalMulticast() ||
				ip.IsLinkLocalUnicast() {
				continue
			}

			if ipv4 := ip.To4(); ipv4 != nil {
				ip = ipv4
			}

			addrs = append(addrs, &net.IPAddr{
				IP:   ip,
				Zone: zone,
			})
		}
	}

	return addrs, nil
}