/
label.go
189 lines (168 loc) · 3.67 KB
/
label.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
// Copyright 2011-2013 GoDCCP Authors. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.
package dccp
import (
"bytes"
"errors"
"hash/crc64"
"math/rand"
"strings"
)
// Label{} is an immutable object, representing a general-purpose address
// similar to IPv6 in that it consists of 16 opaque bytes.
type Label struct {
data [LabelLen]byte
h uint64
}
const LabelLen = 16
var (
LabelZero = Label{}
labelCRC64Table = crc64.MakeTable(crc64.ISO)
)
const labelFootprint = LabelLen
func (label *Label) Bytes() []byte {
if label != nil {
return label.data[:]
}
return LabelZero.data[:]
}
func (label *Label) hash() {
label.h = crc64.Checksum(label.data[:], labelCRC64Table)
}
func isZero(bb []byte) bool {
for _, b := range bb {
if b != 0 {
return false
}
}
return true
}
// ChooseLabel() creates a new label by choosing its bytes randomly
func ChooseLabel() *Label {
label := &Label{}
for i := 0; i < LabelLen/2; i++ {
q := rand.Int()
label.data[2*i] = byte(q & 0xff)
q >>= 8
label.data[2*i+1] = byte(q & 0xff)
}
label.hash()
return label
}
// Hash() returns the hash code of this label
func (label *Label) Hash() uint64 {
return label.h
}
// Equal() performs a deep check for equality with q@
func (label *Label) Equal(q *Label) bool {
for i := 0; i < LabelLen; i++ {
if label.data[i] != q.data[i] {
return false
}
}
return true
}
// ReadLabel() reads and creates a new label from a wire format representation in p@
func ReadLabel(p []byte) (label *Label, n int, err error) {
if len(p) < LabelLen {
return nil, 0, errors.New("label too short")
}
label = &Label{}
copy(label.data[:], p[:LabelLen])
label.hash()
if isZero(label.data[:]) {
return nil, LabelLen, nil
}
return label, LabelLen, nil
}
// Write() writes the wire format representation of the label into p@
func (label *Label) Write(p []byte) (n int, err error) {
if label == nil {
label = &LabelZero
}
if len(p) < LabelLen {
return 0, errors.New("label can't fit")
}
copy(p, label.data[:])
return LabelLen, nil
}
// String() returns a string representation of the label
func (label *Label) String() string {
if label == nil {
label = &LabelZero
}
var w bytes.Buffer
for i, b := range label.data {
if i%2 == 0 && i > 0 {
w.WriteByte('`')
}
w.WriteString(btox(b))
}
return string(w.Bytes())
}
func (label *Label) Address() string {
return label.String()
}
// ParseLabel() parses and creates a new label from the string representation in s@
func ParseLabel(s string) (label *Label, n int, err error) {
l := LabelLen*2 + LabelLen/2 - 1
if len(s) < l {
return nil, 0, errors.New("bad string label len")
}
s = strings.ToLower(s)
label = &Label{}
k := 0
for i := 0; i < LabelLen; {
if i%2 == 0 && i > 0 {
if s[i] != '`' {
return nil, 0, errors.New("missing label dot")
}
i++
continue
}
b, err := xtob(s[i : i+2])
if err != nil {
return nil, 0, err
}
i += 2
label.data[k] = byte(b)
k++
}
label.hash()
if isZero(label.data[:]) {
return nil, l, nil
}
return label, l, nil
}
const xalpha = "0123456789abcdef"
func btox(b byte) string {
r := make([]byte, 2)
r[1] = xalpha[b&0xf]
b >>= 4
r[0] = xalpha[b&0xf]
return string(r)
}
func xtohb(a byte) (b byte, err error) {
if a >= '0' && a <= '9' {
return a - '0', nil
}
if a >= 'a' && a <= 'f' {
return 9 + a - 'a', nil
}
return 0, errors.New("xtohb invalid char")
}
func xtob(s string) (b byte, err error) {
if len(s) != 2 {
return 0, errors.New("xtob len error")
}
b0, err := xtohb(s[1])
if err != nil {
return 0, err
}
b1, err := xtohb(s[0])
if err != nil {
return 0, err
}
return (b1 << 4) | b0, nil
}