Debian Codegraph @

types/HostAddress.go

raw symbols SCIP
1 package types
2
3 // Reference: https://www.ietf.org/rfc/rfc4120.txt
4 // Section: 5.2.5
5
6 import (
7 "bytes"
8 "fmt"
9 "net"
10
11 "github.com/jcmturner/gofork/encoding/asn1"
12 "gopkg.in/jcmturner/gokrb5.v5/iana/addrtype"
13 )
14
15 // HostAddresses implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.2.5
16 type HostAddresses []HostAddress
17
18 // HostAddress implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.2.5
19 type HostAddress struct {
20 AddrType int32 `asn1:"explicit,tag:0"`
21 Address []byte `asn1:"explicit,tag:1"`
22 }
23
24 // GetHostAddress returns a HostAddress struct from a string in the format <hostname>:<port>
25 func GetHostAddress(s string) (HostAddress, error) {
26 var h HostAddress
27 cAddr, _, err := net.SplitHostPort(s)
28 if err != nil {
29 return h, fmt.Errorf("invalid format of client address: %v", err)
30 }
31 ip := net.ParseIP(cAddr)
32 hb, err := ip.MarshalText()
33 if err != nil {
34 return h, fmt.Errorf("could not marshal client's address into bytes: %v", err)
35 }
36 var ht int32
37 if ip.To4() != nil {
38 ht = addrtype.IPv4
39 } else if ip.To16() != nil {
40 ht = addrtype.IPv6
41 } else {
42 return h, fmt.Errorf("could not determine client's address types: %v", err)
43 }
44 h = HostAddress{
45 AddrType: ht,
46 Address: hb,
47 }
48 return h, nil
49 }
50
51 // GetAddress returns a string representation of the HostAddress.
52 func (h *HostAddress) GetAddress() (string, error) {
53 var b []byte
54 _, err := asn1.Unmarshal(h.Address, &b)
55 return string(b), err
56 }
57
58 // LocalHostAddresses returns a HostAddresses struct for the local machines interface IP addresses.
59 func LocalHostAddresses() (ha HostAddresses, err error) {
60 ifs, err := net.Interfaces()
61 if err != nil {
62 return
63 }
64 for _, iface := range ifs {
65 if iface.Flags&net.FlagLoopback != 0 || iface.Flags&net.FlagUp == 0 {
66 // Interface is either loopback of not up
67 continue
68 }
69 addrs, err := iface.Addrs()
70 if err != nil {
71 continue
72 }
73 for _, addr := range addrs {
74 var ip net.IP
75 switch v := addr.(type) {
76 case *net.IPNet:
77 ip = v.IP
78 case *net.IPAddr:
79 ip = v.IP
80 }
81 var a HostAddress
82 if ip.To16() == nil {
83 //neither IPv4 or IPv6
84 continue
85 }
86 if ip.To4() != nil {
87 //Is IPv4
88 a.AddrType = addrtype.IPv4
89 a.Address = ip.To4()
90 } else {
91 a.AddrType = addrtype.IPv6
92 a.Address = ip.To16()
93 }
94 ha = append(ha, a)
95 }
96 }
97 return ha, nil
98 }
99
100 // HostAddressesFromNetIPs returns a HostAddresses type from a slice of net.IP
101 func HostAddressesFromNetIPs(ips []net.IP) (ha HostAddresses) {
102 for _, ip := range ips {
103 ha = append(ha, HostAddressFromNetIP(ip))
104 }
105 return ha
106 }
107
108 // HostAddressFromNetIP returns a HostAddress type from a net.IP
109 func HostAddressFromNetIP(ip net.IP) HostAddress {
110 if ip.To4() != nil {
111 //Is IPv4
112 return HostAddress{
113 AddrType: addrtype.IPv4,
114 Address: ip.To4(),
115 }
116 }
117 return HostAddress{
118 AddrType: addrtype.IPv6,
119 Address: ip.To16(),
120 }
121 }
122
123 // HostAddressesEqual tests if two HostAddress slices are equal.
124 func HostAddressesEqual(h, a []HostAddress) bool {
125 if len(h) != len(a) {
126 return false
127 }
128 for _, e := range a {
129 var found bool
130 for _, i := range h {
131 if e.Equal(i) {
132 found = true
133 break
134 }
135 }
136 if !found {
137 return false
138 }
139 }
140 return true
141 }
142
143 // HostAddressesContains tests if a HostAddress is contained in a HostAddress slice.
144 func HostAddressesContains(h []HostAddress, a HostAddress) bool {
145 for _, e := range h {
146 if e.Equal(a) {
147 return true
148 }
149 }
150 return false
151 }
152
153 // Equal tests if the HostAddress is equal to another HostAddress provided.
154 func (h *HostAddress) Equal(a HostAddress) bool {
155 if h.AddrType != a.AddrType {
156 return false
157 }
158 return bytes.Equal(h.Address, a.Address)
159 }
160
161 // Contains tests if a HostAddress is contained within the HostAddresses struct.
162 func (h *HostAddresses) Contains(a HostAddress) bool {
163 for _, e := range *h {
164 if e.Equal(a) {
165 return true
166 }
167 }
168 return false
169 }
170
171 // Equal tests if a HostAddress slice is equal to the HostAddresses struct.
172 func (h *HostAddresses) Equal(a []HostAddress) bool {
173 if len(*h) != len(a) {
174 return false
175 }
176 for _, e := range a {
177 if !h.Contains(e) {
178 return false
179 }
180 }
181 return true
182 }