From 1dc406c81c155820aa689d8976227d287ff5da8f Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 16 Nov 2018 10:35:30 +1100 Subject: [PATCH] Move IpPort to missinggo --- Peer.go | 4 ++-- bep40.go | 6 +++--- bep40_test.go | 16 ++++++++-------- client.go | 18 +++++++++--------- connection.go | 4 ++-- connection_test.go | 4 ++-- ipport.go | 21 --------------------- misc.go | 1 + prioritized_peers_test.go | 2 +- torrent.go | 2 +- 10 files changed, 29 insertions(+), 49 deletions(-) delete mode 100644 ipport.go diff --git a/Peer.go b/Peer.go index e4a7a2fb..266ce98e 100644 --- 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)} } diff --git a/bep40.go b/bep40.go index 6c507d35..9a643553 100644 --- 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 } diff --git a/bep40_test.go b/bep40_test.go index 78102149..446eaa8c 100644 --- a/bep40_test.go +++ b/bep40_test.go @@ -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 }()) diff --git a/client.go b/client.go index dd3e4cca..81f620ca 100644 --- 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) diff --git a/connection.go b/connection.go index 2b35ef4d..69b80244 100644 --- a/connection.go +++ b/connection.go @@ -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 } diff --git a/connection_test.go b/connection_test.go index 42397459..a50eeff6 100644 --- a/connection_test.go +++ b/connection_test.go @@ -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 index 38036cb4..00000000 --- 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 9d094383..7e73e723 100644 --- 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 ) diff --git a/prioritized_peers_test.go b/prioritized_peers_test.go index df4c4b2d..e7c52651 100644 --- a/prioritized_peers_test.go +++ b/prioritized_peers_test.go @@ -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() diff --git a/torrent.go b/torrent.go index da5e3fa7..154b83c6 100644 --- a/torrent.go +++ b/torrent.go @@ -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 }