1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // IsMulticastCapable reports whether ifi is an IP multicast-capable
10 // network interface. Network must be "ip", "ip4" or "ip6".
11 func IsMulticastCapable(network string, ifi *net.Interface) (net.IP, bool) {
13 case "ip", "ip4", "ip6":
17 if ifi == nil || ifi.Flags&net.FlagUp == 0 || ifi.Flags&net.FlagMulticast == 0 {
20 return hasRoutableIP(network, ifi)
23 // RoutedInterface returns a network interface that can route IP
24 // traffic and satisfies flags. It returns nil when an appropriate
25 // network interface is not found. Network must be "ip", "ip4" or
27 func RoutedInterface(network string, flags net.Flags) *net.Interface {
29 case "ip", "ip4", "ip6":
33 ift, err := net.Interfaces()
37 for _, ifi := range ift {
38 if ifi.Flags&flags != flags {
41 if _, ok := hasRoutableIP(network, &ifi); !ok {
49 func hasRoutableIP(network string, ifi *net.Interface) (net.IP, bool) {
50 ifat, err := ifi.Addrs()
54 for _, ifa := range ifat {
55 switch ifa := ifa.(type) {
57 if ip := routableIP(network, ifa.IP); ip != nil {
61 if ip := routableIP(network, ifa.IP); ip != nil {
69 func routableIP(network string, ip net.IP) net.IP {
70 if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
75 if ip := ip.To4(); ip != nil {
79 if ip.IsLoopback() { // addressing scope of the loopback address depends on each implementation
82 if ip := ip.To16(); ip != nil && ip.To4() == nil {
86 if ip := ip.To4(); ip != nil {
89 if ip := ip.To16(); ip != nil {