forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_address.go
77 lines (60 loc) · 1.71 KB
/
interface_address.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ip
import (
"fmt"
"net"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type InterfaceAddress interface {
GetInterfaceName() string
// GetIP gets the exposed internet protocol address of the above interface
GetIP() (string, error)
}
type simpleInterfaceAddress struct {
interfaceName string
ip string
}
func NewSimpleInterfaceAddress(interfaceName string, ip string) InterfaceAddress {
return simpleInterfaceAddress{interfaceName: interfaceName, ip: ip}
}
func (s simpleInterfaceAddress) GetInterfaceName() string { return s.interfaceName }
func (s simpleInterfaceAddress) GetIP() (string, error) {
ip2 := net.ParseIP(s.ip)
if ip2 == nil {
return "", fmt.Errorf("Cannot parse IP '%s'", s.ip)
}
return fmtIP(ip2), nil
}
type resolvingInterfaceAddress struct {
interfaceName string
ipResolver Resolver
ip string
}
func NewResolvingInterfaceAddress(
interfaceName string,
ipResolver Resolver,
) InterfaceAddress {
return &resolvingInterfaceAddress{
interfaceName: interfaceName,
ipResolver: ipResolver,
}
}
func (s resolvingInterfaceAddress) GetInterfaceName() string { return s.interfaceName }
func (s *resolvingInterfaceAddress) GetIP() (string, error) {
if s.ip != "" {
return s.ip, nil
}
ip, err := s.ipResolver.GetPrimaryIPv4(s.interfaceName)
if err != nil {
return "", bosherr.WrapError(err, "Getting primary IPv4")
}
s.ip = fmtIP(ip.IP)
return s.ip, nil
}
func fmtIP(ip net.IP) string {
if p4 := ip.To4(); len(p4) == net.IPv4len {
return ip.String()
}
return fmt.Sprintf("%x:%x:%x:%x:%x:%x:%x:%x",
[]byte(ip[0:2]), []byte(ip[2:4]), []byte(ip[4:6]), []byte(ip[6:8]),
[]byte(ip[8:10]), []byte(ip[10:12]), []byte(ip[12:14]), []byte(ip[14:16]))
}