13 var table = crc32.MakeTable(crc32.Castagnoli)
15 type peerPriority = uint32
22 func sameSubnet(ones, bits int, a, b net.IP) bool {
23 mask := net.CIDRMask(ones, bits)
24 return a.Mask(mask).Equal(b.Mask(mask))
27 func ipv4Mask(a, b net.IP) net.IPMask {
28 if !sameSubnet(16, 32, a, b) {
29 return net.IPv4Mask(0xff, 0xff, 0x55, 0x55)
31 if !sameSubnet(24, 32, a, b) {
32 return net.IPv4Mask(0xff, 0xff, 0xff, 0x55)
34 return net.IPv4Mask(0xff, 0xff, 0xff, 0xff)
37 func mask(prefix, bytes int) net.IPMask {
38 ret := make(net.IPMask, bytes)
42 for i := 0; i < prefix; i++ {
48 func ipv6Mask(a, b net.IP) net.IPMask {
49 for i := 6; i <= 16; i++ {
50 if !sameSubnet(i*8, 128, a, b) {
54 panic(fmt.Sprintf("%s %s", a, b))
57 func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
60 binary.BigEndian.PutUint16(ret[0:2], a.Port)
61 binary.BigEndian.PutUint16(ret[2:4], b.Port)
64 if a4, b4 := a.IP.To4(), b.IP.To4(); a4 != nil && b4 != nil {
65 m := ipv4Mask(a.IP, b.IP)
66 return append(a4.Mask(m), b4.Mask(m)...), nil
68 if a6, b6 := a.IP.To16(), b.IP.To16(); a6 != nil && b6 != nil {
69 m := ipv6Mask(a.IP, b.IP)
70 return append(a6.Mask(m), b6.Mask(m)...), nil
72 return nil, errors.New("incomparable IPs")
75 func bep40Priority(a, b ipPort) (peerPriority, error) {
76 bs, err := bep40PriorityBytes(a, b)
81 _a, _b := bs[:i], bs[i:]
82 if bytes.Compare(_a, _b) > 0 {
83 bs = append(_b, _a...)
85 return crc32.Checksum(bs, table), nil
88 func bep40PriorityIgnoreError(a, b ipPort) peerPriority {
89 prio, _ := bep40Priority(a, b)