-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
misc.go
37 lines (30 loc) · 792 Bytes
/
misc.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
package dht
import (
"net"
"github.com/anacrolix/missinggo/v2/iter"
"github.com/anacrolix/dht/v2/int160"
"github.com/anacrolix/dht/v2/krpc"
"github.com/anacrolix/dht/v2/types"
)
func mustListen(addr string) net.PacketConn {
ret, err := net.ListenPacket("udp", addr)
if err != nil {
panic(err)
}
return ret
}
func addrResolver(addr string) func() ([]Addr, error) {
return func() ([]Addr, error) {
ua, err := net.ResolveUDPAddr("udp", addr)
return []Addr{NewAddr(ua)}, err
}
}
type addrMaybeId = types.AddrMaybeId
func randomIdInBucket(rootId int160.T, bucketIndex int) int160.T {
id := int160.FromByteArray(krpc.RandomNodeID())
for i := range iter.N(bucketIndex) {
id.SetBit(i, rootId.GetBit(i))
}
id.SetBit(bucketIndex, !rootId.GetBit(bucketIndex))
return id
}