summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/iputils.go
blob: c3ac3aaa873f7ec04881e78880731459f1316654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
}