diff options
author | Andrea Barberio <insomniac@slackware.it> | 2017-11-29 21:25:48 +0000 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2017-12-05 23:32:27 +0000 |
commit | 1403bbe04ce275148b601c32e9551b2281110347 (patch) | |
tree | 6eacd224c2ea6b6e3865cb774970726809363d46 /dhcpv6/iputils.go |
Initial commit
Diffstat (limited to 'dhcpv6/iputils.go')
-rw-r--r-- | dhcpv6/iputils.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/dhcpv6/iputils.go b/dhcpv6/iputils.go new file mode 100644 index 0000000..c3ac3aa --- /dev/null +++ b/dhcpv6/iputils.go @@ -0,0 +1,30 @@ +package dhcpv6 + +import ( + "fmt" + "net" +) + +func GetLinkLocalAddr(ifname string) (*net.IP, error) { + ifaces, err := net.Interfaces() + if err != nil { + return nil, err + } + for _, iface := range ifaces { + if iface.Name != ifname { + continue + } + ifaddrs, err := iface.Addrs() + if err != nil { + return nil, err + } + for _, ifaddr := range ifaddrs { + if ifaddr, ok := ifaddr.(*net.IPNet); ok { + if ifaddr.IP.To4() == nil && ifaddr.IP.IsLinkLocalUnicast() { + return &ifaddr.IP, nil + } + } + } + } + return nil, fmt.Errorf("No link-local address found for interface %v", ifname) +} |