| 1 |
// Package ifcace deals with interfaces. |
| 2 |
package iface |
| 3 |
|
| 4 |
import ( |
| 5 |
"net" |
| 6 |
) |
| 7 |
|
| 8 |
// List returns a list of routable IP addresses from an interface. |
| 9 |
func List(name string) []string { |
| 10 |
ifis, err := net.Interfaces() |
| 11 |
if err != nil { |
| 12 |
return nil |
| 13 |
} |
| 14 |
var addrs []net.Addr |
| 15 |
for _, ifi := range ifis { |
| 16 |
if ifi.Name == name { |
| 17 |
addrs, _ = ifi.Addrs() |
| 18 |
} |
| 19 |
} |
| 20 |
if len(addrs) == 0 { |
| 21 |
return nil |
| 22 |
} |
| 23 |
|
| 24 |
ips := []string{} |
| 25 |
for _, addr := range addrs { |
| 26 |
ipnet, ok := addr.(*net.IPNet) |
| 27 |
if !ok { |
| 28 |
continue |
| 29 |
} |
| 30 |
if ipnet.IP.IsLoopback() { |
| 31 |
continue |
| 32 |
} |
| 33 |
if ipnet.IP.IsPrivate() { |
| 34 |
continue |
| 35 |
} |
| 36 |
if ipnet.IP.IsLinkLocalUnicast() || ipnet.IP.IsUnspecified() { |
| 37 |
continue |
| 38 |
} |
| 39 |
ips = append(ips, ipnet.IP.String()) |
| 40 |
} |
| 41 |
return ips |
| 42 |
} |