]> Sergey Matveev's repositories - btrtrc.git/blobdiff - bep40.go
Drop support for go 1.20
[btrtrc.git] / bep40.go
index b92ef9d943bad4ac4e6b1fb6ab61e149c11bae04..9a643553d730e2a6b408ea37a7eb6ec4af558c4b 100644 (file)
--- a/bep40.go
+++ b/bep40.go
@@ -3,9 +3,9 @@ package torrent
 import (
        "bytes"
        "encoding/binary"
+       "errors"
        "fmt"
        "hash/crc32"
-
        "net"
 )
 
@@ -13,11 +13,6 @@ var table = crc32.MakeTable(crc32.Castagnoli)
 
 type peerPriority = uint32
 
-type ipPort struct {
-       IP   net.IP
-       Port uint16
-}
-
 func sameSubnet(ones, bits int, a, b net.IP) bool {
        mask := net.CIDRMask(ones, bits)
        return a.Mask(mask).Equal(b.Mask(mask))
@@ -53,30 +48,38 @@ func ipv6Mask(a, b net.IP) net.IPMask {
        panic(fmt.Sprintf("%s %s", a, b))
 }
 
-func bep40PriorityBytes(a, b ipPort) []byte {
+func bep40PriorityBytes(a, b IpPort) ([]byte, error) {
        if a.IP.Equal(b.IP) {
                var ret [4]byte
                binary.BigEndian.PutUint16(ret[0:2], a.Port)
                binary.BigEndian.PutUint16(ret[2:4], b.Port)
-               return ret[:]
+               return ret[:], nil
        }
        if a4, b4 := a.IP.To4(), b.IP.To4(); a4 != nil && b4 != nil {
                m := ipv4Mask(a.IP, b.IP)
-               return append(a4.Mask(m), b4.Mask(m)...)
+               return append(a4.Mask(m), b4.Mask(m)...), nil
        }
        if a6, b6 := a.IP.To16(), b.IP.To16(); a6 != nil && b6 != nil {
                m := ipv6Mask(a.IP, b.IP)
-               return append(a6.Mask(m), b6.Mask(m)...)
+               return append(a6.Mask(m), b6.Mask(m)...), nil
        }
-       panic(fmt.Sprintf("%s %s", a.IP, b.IP))
+       return nil, errors.New("incomparable IPs")
 }
 
-func bep40Priority(a, b ipPort) peerPriority {
-       bs := bep40PriorityBytes(a, b)
+func bep40Priority(a, b IpPort) (peerPriority, error) {
+       bs, err := bep40PriorityBytes(a, b)
+       if err != nil {
+               return 0, err
+       }
        i := len(bs) / 2
        _a, _b := bs[:i], bs[i:]
        if bytes.Compare(_a, _b) > 0 {
                bs = append(_b, _a...)
        }
-       return crc32.Checksum(bs, table)
+       return crc32.Checksum(bs, table), nil
+}
+
+func bep40PriorityIgnoreError(a, b IpPort) peerPriority {
+       prio, _ := bep40Priority(a, b)
+       return prio
 }