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 +- diff --git a/Peer.go b/Peer.go index e4a7a2fb4cfcd6695b79d9567cf921a16b1680ef..266ce98ecbc41e33d1415c910c7b4538ae8be08d 100644 --- a/Peer.go +++ b/Peer.go @@ -28,6 +28,6 @@ } 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 6c507d354280e5156f3cbc1996939c2167d3d356..9a643553d730e2a6b408ea37a7eb6ec4af558c4b 100644 --- a/bep40.go +++ b/bep40.go @@ -48,7 +48,7 @@ } 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 @@ } 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 @@ } 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 78102149c1789323e9a899a88833e7f92b55d26c..446eaa8c0ff5e1a766de5fce01ca5b7b37ae3ed0 100644 --- a/bep40_test.go +++ b/bep40_test.go @@ -9,21 +9,21 @@ ) 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 dd3e4cca0cc0d32735140cbbfd97133ab7e5fe9b..81f620caf283a886badaee7fad449599169c31fd 100644 --- a/client.go +++ b/client.go @@ -442,7 +442,7 @@ defer nc.Close() 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 @@ } // 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 @@ } // 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 @@ } // 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 @@ } // 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 @@ } 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 @@ }).Addr()) } // 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 @@ }) 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 2b35ef4dc1ae3d4888504687fafbfa279db6a739..69b80244ba3eba0912b1aa12749b9cb62d7e9184 100644 --- a/connection.go +++ b/connection.go @@ -45,7 +45,7 @@ // The actual Conn, used for closing, and setting socket options. 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 4239745924cd5eaaa061f5dc0af36b36e05624bc..a50eeff641ee55471300e2cb6e3a3fd0fe2f18e6 100644 --- a/connection_test.go +++ b/connection_test.go @@ -23,7 +23,7 @@ cl := Client{ 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 @@ })) 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 38036cb40c3aaf86848cbd64b90142f9ded744d0..0000000000000000000000000000000000000000 --- 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 --- a/misc.go +++ b/misc.go @@ -155,4 +155,5 @@ type ( pieceIndex = int InfoHash = metainfo.Hash + IpPort = missinggo.IpPort ) diff --git a/prioritized_peers_test.go b/prioritized_peers_test.go index df4c4b2d1b2177cec2a57bd74b8a6636970a8ca8..e7c5265104c564472006ef5b907ea6146b4c11aa 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 da5e3fa7a95c75386182734f5c71a25da51df2c5..154b83c675f84ac288ef5b42815b41cb648e7839 100644 --- a/torrent.go +++ b/torrent.go @@ -1736,7 +1736,7 @@ } 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 }