]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move IpPort to missinggo
authorMatt Joiner <anacrolix@gmail.com>
Thu, 15 Nov 2018 23:35:30 +0000 (10:35 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 15 Nov 2018 23:35:30 +0000 (10:35 +1100)
Peer.go
bep40.go
bep40_test.go
client.go
connection.go
connection_test.go
ipport.go [deleted file]
misc.go
prioritized_peers_test.go
torrent.go

diff --git a/Peer.go b/Peer.go
index e4a7a2fb4cfcd6695b79d9567cf921a16b1680ef..266ce98ecbc41e33d1415c910c7b4538ae8be08d 100644 (file)
--- a/Peer.go
+++ b/Peer.go
@@ -28,6 +28,6 @@ func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
        me.PexPeerFlags = fs
 }
 
-func (me Peer) addr() ipPort {
-       return ipPort{me.IP, uint16(me.Port)}
+func (me Peer) addr() IpPort {
+       return IpPort{me.IP, uint16(me.Port)}
 }
index 6c507d354280e5156f3cbc1996939c2167d3d356..9a643553d730e2a6b408ea37a7eb6ec4af558c4b 100644 (file)
--- a/bep40.go
+++ b/bep40.go
@@ -48,7 +48,7 @@ func ipv6Mask(a, b net.IP) net.IPMask {
        panic(fmt.Sprintf("%s %s", a, b))
 }
 
-func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
+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)
@@ -66,7 +66,7 @@ func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
        return nil, errors.New("incomparable IPs")
 }
 
-func bep40Priority(a, b ipPort) (peerPriority, error) {
+func bep40Priority(a, b IpPort) (peerPriority, error) {
        bs, err := bep40PriorityBytes(a, b)
        if err != nil {
                return 0, err
@@ -79,7 +79,7 @@ func bep40Priority(a, b ipPort) (peerPriority, error) {
        return crc32.Checksum(bs, table), nil
 }
 
-func bep40PriorityIgnoreError(a, b ipPort) peerPriority {
+func bep40PriorityIgnoreError(a, b IpPort) peerPriority {
        prio, _ := bep40Priority(a, b)
        return prio
 }
index 78102149c1789323e9a899a88833e7f92b55d26c..446eaa8c0ff5e1a766de5fce01ca5b7b37ae3ed0 100644 (file)
@@ -9,21 +9,21 @@ import (
 
 func TestBep40Priority(t *testing.T) {
        assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
-               ipPort{net.ParseIP("123.213.32.10"), 0},
-               ipPort{net.ParseIP("98.76.54.32"), 0},
+               IpPort{net.ParseIP("123.213.32.10"), 0},
+               IpPort{net.ParseIP("98.76.54.32"), 0},
        ))
        assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
-               ipPort{net.ParseIP("98.76.54.32"), 0},
-               ipPort{net.ParseIP("123.213.32.10"), 0},
+               IpPort{net.ParseIP("98.76.54.32"), 0},
+               IpPort{net.ParseIP("123.213.32.10"), 0},
        ))
        assert.Equal(t, peerPriority(0x99568189), bep40PriorityIgnoreError(
-               ipPort{net.ParseIP("123.213.32.10"), 0},
-               ipPort{net.ParseIP("123.213.32.234"), 0},
+               IpPort{net.ParseIP("123.213.32.10"), 0},
+               IpPort{net.ParseIP("123.213.32.234"), 0},
        ))
        assert.EqualValues(t, "\x00\x00\x00\x00", func() []byte {
                b, _ := bep40PriorityBytes(
-                       ipPort{net.ParseIP("123.213.32.234"), 0},
-                       ipPort{net.ParseIP("123.213.32.234"), 0},
+                       IpPort{net.ParseIP("123.213.32.234"), 0},
+                       IpPort{net.ParseIP("123.213.32.234"), 0},
                )
                return b
        }())
index dd3e4cca0cc0d32735140cbbfd97133ab7e5fe9b..81f620caf283a886badaee7fad449599169c31fd 100644 (file)
--- a/client.go
+++ b/client.go
@@ -442,7 +442,7 @@ func (cl *Client) incomingConnection(nc net.Conn) {
        if tc, ok := nc.(*net.TCPConn); ok {
                tc.SetLinger(0)
        }
-       c := cl.newConnection(nc, false, ipPortFromNetAddr(nc.RemoteAddr()), nc.RemoteAddr().Network())
+       c := cl.newConnection(nc, false, missinggo.IpPortFromNetAddr(nc.RemoteAddr()), nc.RemoteAddr().Network())
        c.Discovery = peerSourceIncoming
        cl.runReceivedConn(c)
 }
@@ -585,7 +585,7 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
 
 // Performs initiator handshakes and returns a connection. Returns nil
 // *connection if no connection for valid reasons.
-func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr ipPort, network string) (c *connection, err error) {
+func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr IpPort, network string) (c *connection, err error) {
        c = cl.newConnection(nc, true, remoteAddr, network)
        c.headerEncrypted = encryptHeader
        ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
@@ -607,7 +607,7 @@ func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torr
 
 // Returns nil connection and nil error if no connection could be established
 // for valid reasons.
-func (cl *Client) establishOutgoingConnEx(t *Torrent, addr ipPort, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) {
+func (cl *Client) establishOutgoingConnEx(t *Torrent, addr IpPort, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) {
        dr := cl.dialFirst(ctx, addr.String())
        nc := dr.Conn
        if nc == nil {
@@ -623,7 +623,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr ipPort, ctx context.C
 
 // Returns nil connection and nil error if no connection could be established
 // for valid reasons.
-func (cl *Client) establishOutgoingConn(t *Torrent, addr ipPort) (c *connection, err error) {
+func (cl *Client) establishOutgoingConn(t *Torrent, addr IpPort) (c *connection, err error) {
        torrent.Add("establish outgoing connection", 1)
        ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
                cl.rLock()
@@ -658,7 +658,7 @@ func (cl *Client) establishOutgoingConn(t *Torrent, addr ipPort) (c *connection,
 
 // Called to dial out and run a connection. The addr we're given is already
 // considered half-open.
-func (cl *Client) outgoingConnection(t *Torrent, addr ipPort, ps peerSource) {
+func (cl *Client) outgoingConnection(t *Torrent, addr IpPort, ps peerSource) {
        cl.dialRateLimiter.Wait(context.Background())
        c, err := cl.establishOutgoingConn(t, addr)
        cl.lock()
@@ -1182,7 +1182,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
        cl.badPeerIPs[ip.String()] = struct{}{}
 }
 
-func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr ipPort, network string) (c *connection) {
+func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr IpPort, network string) (c *connection) {
        c = &connection{
                conn:            nc,
                outgoing:        outgoing,
@@ -1263,8 +1263,8 @@ func (cl *Client) findListenerIp(f func(net.IP) bool) net.IP {
 }
 
 // Our IP as a peer should see it.
-func (cl *Client) publicAddr(peer net.IP) ipPort {
-       return ipPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
+func (cl *Client) publicAddr(peer net.IP) IpPort {
+       return IpPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
 }
 
 func (cl *Client) ListenAddrs() (ret []net.Addr) {
@@ -1277,7 +1277,7 @@ func (cl *Client) ListenAddrs() (ret []net.Addr) {
        return
 }
 
-func (cl *Client) onBadAccept(addr ipPort) {
+func (cl *Client) onBadAccept(addr IpPort) {
        ip := maskIpForAcceptLimiting(addr.IP)
        if cl.acceptLimiter == nil {
                cl.acceptLimiter = make(map[ipStr]int)
index 2b35ef4dc1ae3d4888504687fafbfa279db6a739..69b80244ba3eba0912b1aa12749b9cb62d7e9184 100644 (file)
@@ -45,7 +45,7 @@ type connection struct {
        conn       net.Conn
        outgoing   bool
        network    string
-       remoteAddr ipPort
+       remoteAddr IpPort
        // The Reader and Writer for this Conn, with hooks installed for stats,
        // limiting, deadlines etc.
        w io.Writer
@@ -1551,6 +1551,6 @@ func (c *connection) remoteIp() net.IP {
        return c.remoteAddr.IP
 }
 
-func (c *connection) remoteIpPort() ipPort {
+func (c *connection) remoteIpPort() IpPort {
        return c.remoteAddr
 }
index 4239745924cd5eaaa061f5dc0af36b36e05624bc..a50eeff641ee55471300e2cb6e3a3fd0fe2f18e6 100644 (file)
@@ -23,7 +23,7 @@ func TestSendBitfieldThenHave(t *testing.T) {
                config: &ClientConfig{DownloadRateLimiter: unlimited},
        }
        cl.initLogger()
-       c := cl.newConnection(nil, false, ipPort{}, "")
+       c := cl.newConnection(nil, false, IpPort{}, "")
        c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
        c.t.setInfo(&metainfo.Info{
                Pieces: make([]byte, metainfo.HashSize*3),
@@ -105,7 +105,7 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
        t.setChunkSize(defaultChunkSize)
        t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
        r, w := net.Pipe()
-       cn := cl.newConnection(r, true, ipPort{}, "")
+       cn := cl.newConnection(r, true, IpPort{}, "")
        cn.setTorrent(t)
        mrlErr := make(chan error)
        msg := pp.Message{
diff --git a/ipport.go b/ipport.go
deleted file mode 100644 (file)
index 38036cb..0000000
--- a/ipport.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package torrent
-
-import (
-       "net"
-       "strconv"
-
-       "github.com/anacrolix/missinggo"
-)
-
-type ipPort struct {
-       IP   net.IP
-       Port uint16
-}
-
-func (me ipPort) String() string {
-       return net.JoinHostPort(me.IP.String(), strconv.FormatUint(uint64(me.Port), 10))
-}
-
-func ipPortFromNetAddr(na net.Addr) ipPort {
-       return ipPort{missinggo.AddrIP(na), uint16(missinggo.AddrPort(na))}
-}
diff --git a/misc.go b/misc.go
index 9d0943833ee5cc30d13ca913f51f54f2bd0eebce..7e73e723393252d20b474661fc2e2d295d593b24 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -155,4 +155,5 @@ var unlimited = rate.NewLimiter(rate.Inf, 0)
 type (
        pieceIndex = int
        InfoHash   = metainfo.Hash
+       IpPort     = missinggo.IpPort
 )
index df4c4b2d1b2177cec2a57bd74b8a6636970a8ca8..e7c5265104c564472006ef5b907ea6146b4c11aa 100644 (file)
@@ -13,7 +13,7 @@ func TestPrioritizedPeers(t *testing.T) {
        pp := prioritizedPeers{
                om: btree.New(3),
                getPrio: func(p Peer) peerPriority {
-                       return bep40PriorityIgnoreError(p.addr(), ipPort{IP: net.ParseIP("0.0.0.0")})
+                       return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
                },
        }
        _, ok := pp.DeleteMin()
index da5e3fa7a95c75386182734f5c71a25da51df2c5..154b83c675f84ac288ef5b42815b41cb648e7839 100644 (file)
@@ -1736,7 +1736,7 @@ func (t *Torrent) initiateConn(peer Peer) {
        if t.cl.badPeerIPPort(peer.IP, peer.Port) {
                return
        }
-       addr := ipPort{peer.IP, uint16(peer.Port)}
+       addr := IpPort{peer.IP, uint16(peer.Port)}
        if t.addrActive(addr.String()) {
                return
        }