]> Sergey Matveev's repositories - btrtrc.git/blob - bep40.go
Fix #244
[btrtrc.git] / bep40.go
1 package torrent
2
3 import (
4         "bytes"
5         "encoding/binary"
6         "errors"
7         "fmt"
8         "hash/crc32"
9
10         "net"
11 )
12
13 var table = crc32.MakeTable(crc32.Castagnoli)
14
15 type peerPriority = uint32
16
17 type ipPort struct {
18         IP   net.IP
19         Port uint16
20 }
21
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))
25 }
26
27 func ipv4Mask(a, b net.IP) net.IPMask {
28         if !sameSubnet(16, 32, a, b) {
29                 return net.IPv4Mask(0xff, 0xff, 0x55, 0x55)
30         }
31         if !sameSubnet(24, 32, a, b) {
32                 return net.IPv4Mask(0xff, 0xff, 0xff, 0x55)
33         }
34         return net.IPv4Mask(0xff, 0xff, 0xff, 0xff)
35 }
36
37 func mask(prefix, bytes int) net.IPMask {
38         ret := make(net.IPMask, bytes)
39         for i := range ret {
40                 ret[i] = 0x55
41         }
42         for i := 0; i < prefix; i++ {
43                 ret[i] = 0xff
44         }
45         return ret
46 }
47
48 func ipv6Mask(a, b net.IP) net.IPMask {
49         for i := 6; i <= 16; i++ {
50                 if !sameSubnet(i*8, 128, a, b) {
51                         return mask(i, 16)
52                 }
53         }
54         panic(fmt.Sprintf("%s %s", a, b))
55 }
56
57 func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
58         if a.IP.Equal(b.IP) {
59                 var ret [4]byte
60                 binary.BigEndian.PutUint16(ret[0:2], a.Port)
61                 binary.BigEndian.PutUint16(ret[2:4], b.Port)
62                 return ret[:], nil
63         }
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
67         }
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
71         }
72         return nil, errors.New("incomparable IPs")
73 }
74
75 func bep40Priority(a, b ipPort) (peerPriority, error) {
76         bs, err := bep40PriorityBytes(a, b)
77         if err != nil {
78                 return 0, nil
79         }
80         i := len(bs) / 2
81         _a, _b := bs[:i], bs[i:]
82         if bytes.Compare(_a, _b) > 0 {
83                 bs = append(_b, _a...)
84         }
85         return crc32.Checksum(bs, table), nil
86 }
87
88 func bep40PriorityIgnoreError(a, b ipPort) peerPriority {
89         prio, _ := bep40Priority(a, b)
90         return prio
91 }