forked from scionproto/scion
/
host.go
346 lines (289 loc) · 6.71 KB
/
host.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2016 ETH Zurich
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package addr
import (
"encoding/binary"
"fmt"
"net"
"strings"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/serrors"
)
type HostAddrType uint8
const (
HostTypeNone HostAddrType = iota
HostTypeIPv4
HostTypeIPv6
HostTypeSVC
)
func (t HostAddrType) String() string {
switch t {
case HostTypeNone:
return "None"
case HostTypeIPv4:
return "IPv4"
case HostTypeIPv6:
return "IPv6"
case HostTypeSVC:
return "SVC"
}
return fmt.Sprintf("UNKNOWN (%d)", t)
}
const (
HostLenNone = 0
HostLenIPv4 = net.IPv4len
HostLenIPv6 = net.IPv6len
HostLenSVC = 2
)
var (
// ErrBadHostAddrType indicates an invalid host address type.
ErrBadHostAddrType = serrors.New("unsupported host address type")
// ErrMalformedHostAddrType indicates a malformed host address type.
ErrMalformedHostAddrType = serrors.New("malformed host address type")
)
const (
SvcBS HostSVC = 0x0000
SvcPS HostSVC = 0x0001
SvcCS HostSVC = 0x0002
SvcSB HostSVC = 0x0003
SvcSIG HostSVC = 0x0004
SvcHPS HostSVC = 0x0005
SvcNone HostSVC = 0xffff
SVCMcast HostSVC = 0x8000
)
type HostAddr interface {
Size() int
Type() HostAddrType
Pack() common.RawBytes
IP() net.IP
Copy() HostAddr
Equal(HostAddr) bool
fmt.Stringer
}
var _ HostAddr = (HostNone)(nil)
type HostNone net.IP
func (h HostNone) Size() int {
return HostLenNone
}
func (h HostNone) Type() HostAddrType {
return HostTypeNone
}
func (h HostNone) Pack() common.RawBytes {
return common.RawBytes{}
}
func (h HostNone) IP() net.IP {
return nil
}
func (h HostNone) Copy() HostAddr {
return HostNone{}
}
func (h HostNone) Equal(o HostAddr) bool {
_, ok := o.(HostNone)
return ok
}
func (h HostNone) String() string {
return "<None>"
}
var _ HostAddr = (HostIPv4)(nil)
type HostIPv4 net.IP
func (h HostIPv4) Size() int {
return HostLenIPv4
}
func (h HostIPv4) Type() HostAddrType {
return HostTypeIPv4
}
func (h HostIPv4) Pack() common.RawBytes {
return common.RawBytes(h.IP())
}
func (h HostIPv4) IP() net.IP {
// XXX(kormat): ensure the reply is the 4-byte representation.
return net.IP(h).To4()
}
func (h HostIPv4) Copy() HostAddr {
return HostIPv4(append(net.IP(nil), h...))
}
func (h HostIPv4) Equal(o HostAddr) bool {
ha, ok := o.(HostIPv4)
return ok && net.IP(h).Equal(net.IP(ha))
}
func (h HostIPv4) String() string {
return h.IP().String()
}
var _ HostAddr = (HostIPv6)(nil)
type HostIPv6 net.IP
func (h HostIPv6) Size() int {
return HostLenIPv6
}
func (h HostIPv6) Type() HostAddrType {
return HostTypeIPv6
}
func (h HostIPv6) Pack() common.RawBytes {
return common.RawBytes(h)[:HostLenIPv6]
}
func (h HostIPv6) IP() net.IP {
return net.IP(h)
}
func (h HostIPv6) Copy() HostAddr {
return HostIPv6(append(net.IP(nil), h...))
}
func (h HostIPv6) Equal(o HostAddr) bool {
ha, ok := o.(HostIPv6)
return ok && net.IP(h).Equal(net.IP(ha))
}
func (h HostIPv6) String() string {
return h.IP().String()
}
var _ HostAddr = (*HostSVC)(nil)
type HostSVC uint16
// HostSVCFromString returns the SVC address corresponding to str. For anycast
// SVC addresses, use BS_A, PS_A, CS_A, and SB_A; shorthand versions without
// the _A suffix (e.g., PS) also return anycast SVC addresses. For multicast,
// use BS_M, PS_M, CS_M, and SB_M.
func HostSVCFromString(str string) HostSVC {
var m HostSVC
switch {
case strings.HasSuffix(str, "_A"):
str = strings.TrimSuffix(str, "_A")
case strings.HasSuffix(str, "_M"):
str = strings.TrimSuffix(str, "_M")
m = SVCMcast
}
switch str {
case "BS":
return SvcBS | m
case "PS":
return SvcPS | m
case "CS":
return SvcCS | m
case "SB":
return SvcSB | m
case "SIG":
return SvcSIG | m
default:
return SvcNone
}
}
func (h HostSVC) Size() int {
return HostLenSVC
}
func (h HostSVC) Type() HostAddrType {
return HostTypeSVC
}
func (h HostSVC) Pack() common.RawBytes {
out := make(common.RawBytes, HostLenSVC)
binary.BigEndian.PutUint16(out, uint16(h))
return out
}
func (h HostSVC) IP() net.IP {
return nil
}
func (h HostSVC) IsMulticast() bool {
return (h & SVCMcast) != 0
}
func (h HostSVC) Base() HostSVC {
return h & ^HostSVC(SVCMcast)
}
func (h HostSVC) Multicast() HostSVC {
return h | HostSVC(SVCMcast)
}
func (h HostSVC) Copy() HostAddr {
return h
}
func (h HostSVC) Equal(o HostAddr) bool {
ha, ok := o.(HostSVC)
return ok && h == ha
}
func (h HostSVC) String() string {
name := h.BaseString()
cast := 'A'
if h.IsMulticast() {
cast = 'M'
}
return fmt.Sprintf("%v %c (0x%04x)", name, cast, uint16(h))
}
// BaseString returns the upper case name of the service. For hosts or unrecognized services, it
// returns UNKNOWN.
func (h HostSVC) BaseString() string {
switch h.Base() {
case SvcBS:
return "BS"
case SvcPS:
return "PS"
case SvcCS:
return "CS"
case SvcSB:
return "SB"
case SvcSIG:
return "SIG"
default:
return "UNKNOWN"
}
}
func HostFromRaw(b common.RawBytes, htype HostAddrType) (HostAddr, error) {
switch htype {
case HostTypeNone:
return HostNone{}, nil
case HostTypeIPv4:
if len(b) < HostLenIPv4 {
return nil, serrors.WithCtx(ErrMalformedHostAddrType, "type", htype)
}
return HostIPv4(b[:HostLenIPv4]), nil
case HostTypeIPv6:
if len(b) < HostLenIPv6 {
return nil, serrors.WithCtx(ErrMalformedHostAddrType, "type", htype)
}
return HostIPv6(b[:HostLenIPv6]), nil
case HostTypeSVC:
return HostSVC(binary.BigEndian.Uint16(b)), nil
default:
return nil, serrors.WithCtx(ErrBadHostAddrType, "type", htype)
}
}
func HostFromIP(ip net.IP) HostAddr {
if ip4 := ip.To4(); ip4 != nil {
return HostIPv4(ip4)
}
return HostIPv6(ip)
}
func HostFromIPStr(s string) HostAddr {
ip := net.ParseIP(s)
if ip == nil {
return nil
}
return HostFromIP(ip)
}
func HostLen(htype HostAddrType) (uint8, error) {
var length uint8
switch htype {
case HostTypeNone:
length = HostLenNone
case HostTypeIPv4:
length = HostLenIPv4
case HostTypeIPv6:
length = HostLenIPv6
case HostTypeSVC:
length = HostLenSVC
default:
return 0, serrors.WithCtx(ErrBadHostAddrType, "type", htype)
}
return length, nil
}
func HostTypeCheck(t HostAddrType) bool {
switch t {
case HostTypeIPv6, HostTypeIPv4, HostTypeSVC:
return true
}
return false
}