-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
nodeinfo.go
178 lines (158 loc) · 4.37 KB
/
nodeinfo.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
package dht
import (
"bytes"
"encoding"
"encoding/binary"
"fmt"
"github.com/anacrolix/missinggo/v2/slices"
"github.com/anacrolix/torrent/bencode"
"github.com/bitmagnet-io/bitmagnet/internal/protocol"
"math"
"math/rand"
"net"
"reflect"
)
type NodeInfo struct {
ID protocol.ID
Addr NodeAddr
}
func (ni NodeInfo) String() string {
return fmt.Sprintf("{%x at %s}", ni.ID, ni.Addr)
}
func RandomNodeInfo(ipLen int) (ni NodeInfo) {
ni.ID = protocol.RandomNodeID()
ni.Addr.Port = rand.Intn(math.MaxUint16 + 1)
ni.Addr.IP = make(net.IP, ipLen)
for i := 0; i < ipLen; i++ {
ni.Addr.IP[i] = byte(rand.Intn(256))
}
return
}
var _ interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
} = (*NodeInfo)(nil)
func (ni NodeInfo) MarshalBinary() ([]byte, error) {
var w bytes.Buffer
w.Write(ni.ID[:])
w.Write(ni.Addr.IP)
if err := binary.Write(&w, binary.BigEndian, uint16(ni.Addr.Port)); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (ni *NodeInfo) UnmarshalBinary(b []byte) error {
copy(ni.ID[:], b)
return ni.Addr.UnmarshalBinary(b[20:])
}
type (
CompactIPv4NodeInfo []NodeInfo
)
func (CompactIPv4NodeInfo) ElemSize() int {
return 26
}
// func (me *CompactIPv4NodeInfo) Scrub() {
// slices.FilterInPlace(me, func(ni *NodeInfo) bool {
// ni.Addr.IP = ni.Addr.IP.To4()
// return ni.Addr.IP != nil
// })
// }
func (ni CompactIPv4NodeInfo) MarshalBinary() ([]byte, error) {
return marshalBinarySlice(slices.Map(func(ni NodeInfo) NodeInfo {
ni.Addr.IP = ni.Addr.IP.To4()
return ni
}, ni).(CompactIPv4NodeInfo))
}
func (ni CompactIPv4NodeInfo) MarshalBencode() ([]byte, error) {
return bencodeBytesResult(ni.MarshalBinary())
}
func (ni *CompactIPv4NodeInfo) UnmarshalBinary(b []byte) error {
return unmarshalBinarySlice(ni, b)
}
func (ni *CompactIPv4NodeInfo) UnmarshalBencode(b []byte) error {
return unmarshalBencodedBinary(ni, b)
}
func unmarshalBencodedBinary(u encoding.BinaryUnmarshaler, b []byte) (err error) {
var ub string
err = bencode.Unmarshal(b, &ub)
if err != nil {
return
}
return u.UnmarshalBinary([]byte(ub))
}
type elemSizer interface {
ElemSize() int
}
func unmarshalBinarySlice(slice elemSizer, b []byte) (err error) {
sliceValue := reflect.ValueOf(slice).Elem()
elemType := sliceValue.Type().Elem()
bytesPerElem := slice.ElemSize()
elem := reflect.New(elemType)
for len(b) != 0 {
if len(b) < bytesPerElem {
err = fmt.Errorf("%d trailing bytes < %d required for element", len(b), bytesPerElem)
break
}
if bu, ok := elem.Interface().(encoding.BinaryUnmarshaler); ok {
err = bu.UnmarshalBinary(b[:bytesPerElem])
} else if elem.Elem().Len() == bytesPerElem {
reflect.Copy(elem.Elem(), reflect.ValueOf(b[:bytesPerElem]))
} else {
err = fmt.Errorf("can't unmarshal %v bytes into %v", bytesPerElem, elem.Type())
}
if err != nil {
return
}
sliceValue.Set(reflect.Append(sliceValue, elem.Elem()))
b = b[bytesPerElem:]
}
return
}
func marshalBinarySlice(slice elemSizer) (ret []byte, err error) {
var elems []encoding.BinaryMarshaler
makeInto(&elems, slice)
for _, e := range elems {
var b []byte
b, err = e.MarshalBinary()
if err != nil {
return
}
if len(b) != slice.ElemSize() {
panic(fmt.Sprintf("marshalled %d bytes, but expected %d", len(b), slice.ElemSize()))
}
ret = append(ret, b...)
}
return
}
func bencodeBytesResult(b []byte, err error) ([]byte, error) {
if err != nil {
return b, err
}
return bencode.Marshal(b)
}
// makes and sets a slice at *ptrTo, and type asserts all the elements from "from" to it.
func makeInto(ptrTo interface{}, from interface{}) {
fromSliceValue := reflect.ValueOf(from)
fromLen := fromSliceValue.Len()
if fromLen == 0 {
return
}
// Deref the pointer to slice.
slicePtrValue := reflect.ValueOf(ptrTo)
if slicePtrValue.Kind() != reflect.Ptr {
panic("destination is not a pointer")
}
destSliceValue := slicePtrValue.Elem()
// The type of the elements of the destination slice.
destSliceElemType := destSliceValue.Type().Elem()
destSliceValue.Set(reflect.MakeSlice(destSliceValue.Type(), fromLen, fromLen))
for i := range make([]struct{}, fromSliceValue.Len()) {
// The value inside the interface in the slice element.
itemValue := fromSliceValue.Index(i)
if itemValue.Kind() == reflect.Interface {
itemValue = itemValue.Elem()
}
convertedItem := itemValue.Convert(destSliceElemType)
destSliceValue.Index(i).Set(convertedItem)
}
}