Move IpPort to missinggo
This commit is contained in:
parent
148bb977bc
commit
1dc406c81c
4
Peer.go
4
Peer.go
|
@ -28,6 +28,6 @@ func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
||||||
me.PexPeerFlags = fs
|
me.PexPeerFlags = fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Peer) addr() ipPort {
|
func (me Peer) addr() IpPort {
|
||||||
return ipPort{me.IP, uint16(me.Port)}
|
return IpPort{me.IP, uint16(me.Port)}
|
||||||
}
|
}
|
||||||
|
|
6
bep40.go
6
bep40.go
|
@ -48,7 +48,7 @@ func ipv6Mask(a, b net.IP) net.IPMask {
|
||||||
panic(fmt.Sprintf("%s %s", a, b))
|
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) {
|
if a.IP.Equal(b.IP) {
|
||||||
var ret [4]byte
|
var ret [4]byte
|
||||||
binary.BigEndian.PutUint16(ret[0:2], a.Port)
|
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")
|
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)
|
bs, err := bep40PriorityBytes(a, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -79,7 +79,7 @@ func bep40Priority(a, b ipPort) (peerPriority, error) {
|
||||||
return crc32.Checksum(bs, table), nil
|
return crc32.Checksum(bs, table), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func bep40PriorityIgnoreError(a, b ipPort) peerPriority {
|
func bep40PriorityIgnoreError(a, b IpPort) peerPriority {
|
||||||
prio, _ := bep40Priority(a, b)
|
prio, _ := bep40Priority(a, b)
|
||||||
return prio
|
return prio
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,21 +9,21 @@ import (
|
||||||
|
|
||||||
func TestBep40Priority(t *testing.T) {
|
func TestBep40Priority(t *testing.T) {
|
||||||
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
|
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
|
||||||
ipPort{net.ParseIP("123.213.32.10"), 0},
|
IpPort{net.ParseIP("123.213.32.10"), 0},
|
||||||
ipPort{net.ParseIP("98.76.54.32"), 0},
|
IpPort{net.ParseIP("98.76.54.32"), 0},
|
||||||
))
|
))
|
||||||
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
|
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
|
||||||
ipPort{net.ParseIP("98.76.54.32"), 0},
|
IpPort{net.ParseIP("98.76.54.32"), 0},
|
||||||
ipPort{net.ParseIP("123.213.32.10"), 0},
|
IpPort{net.ParseIP("123.213.32.10"), 0},
|
||||||
))
|
))
|
||||||
assert.Equal(t, peerPriority(0x99568189), bep40PriorityIgnoreError(
|
assert.Equal(t, peerPriority(0x99568189), bep40PriorityIgnoreError(
|
||||||
ipPort{net.ParseIP("123.213.32.10"), 0},
|
IpPort{net.ParseIP("123.213.32.10"), 0},
|
||||||
ipPort{net.ParseIP("123.213.32.234"), 0},
|
IpPort{net.ParseIP("123.213.32.234"), 0},
|
||||||
))
|
))
|
||||||
assert.EqualValues(t, "\x00\x00\x00\x00", func() []byte {
|
assert.EqualValues(t, "\x00\x00\x00\x00", func() []byte {
|
||||||
b, _ := bep40PriorityBytes(
|
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
|
return b
|
||||||
}())
|
}())
|
||||||
|
|
18
client.go
18
client.go
|
@ -442,7 +442,7 @@ func (cl *Client) incomingConnection(nc net.Conn) {
|
||||||
if tc, ok := nc.(*net.TCPConn); ok {
|
if tc, ok := nc.(*net.TCPConn); ok {
|
||||||
tc.SetLinger(0)
|
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
|
c.Discovery = peerSourceIncoming
|
||||||
cl.runReceivedConn(c)
|
cl.runReceivedConn(c)
|
||||||
}
|
}
|
||||||
|
@ -585,7 +585,7 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
|
||||||
|
|
||||||
// Performs initiator handshakes and returns a connection. Returns nil
|
// Performs initiator handshakes and returns a connection. Returns nil
|
||||||
// *connection if no connection for valid reasons.
|
// *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 = cl.newConnection(nc, true, remoteAddr, network)
|
||||||
c.headerEncrypted = encryptHeader
|
c.headerEncrypted = encryptHeader
|
||||||
ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
|
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
|
// Returns nil connection and nil error if no connection could be established
|
||||||
// for valid reasons.
|
// 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())
|
dr := cl.dialFirst(ctx, addr.String())
|
||||||
nc := dr.Conn
|
nc := dr.Conn
|
||||||
if nc == nil {
|
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
|
// Returns nil connection and nil error if no connection could be established
|
||||||
// for valid reasons.
|
// 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)
|
torrent.Add("establish outgoing connection", 1)
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
|
ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
|
||||||
cl.rLock()
|
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
|
// Called to dial out and run a connection. The addr we're given is already
|
||||||
// considered half-open.
|
// 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())
|
cl.dialRateLimiter.Wait(context.Background())
|
||||||
c, err := cl.establishOutgoingConn(t, addr)
|
c, err := cl.establishOutgoingConn(t, addr)
|
||||||
cl.lock()
|
cl.lock()
|
||||||
|
@ -1182,7 +1182,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
|
||||||
cl.badPeerIPs[ip.String()] = struct{}{}
|
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{
|
c = &connection{
|
||||||
conn: nc,
|
conn: nc,
|
||||||
outgoing: outgoing,
|
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.
|
// Our IP as a peer should see it.
|
||||||
func (cl *Client) publicAddr(peer net.IP) ipPort {
|
func (cl *Client) publicAddr(peer net.IP) IpPort {
|
||||||
return ipPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
|
return IpPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *Client) ListenAddrs() (ret []net.Addr) {
|
func (cl *Client) ListenAddrs() (ret []net.Addr) {
|
||||||
|
@ -1277,7 +1277,7 @@ func (cl *Client) ListenAddrs() (ret []net.Addr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *Client) onBadAccept(addr ipPort) {
|
func (cl *Client) onBadAccept(addr IpPort) {
|
||||||
ip := maskIpForAcceptLimiting(addr.IP)
|
ip := maskIpForAcceptLimiting(addr.IP)
|
||||||
if cl.acceptLimiter == nil {
|
if cl.acceptLimiter == nil {
|
||||||
cl.acceptLimiter = make(map[ipStr]int)
|
cl.acceptLimiter = make(map[ipStr]int)
|
||||||
|
|
|
@ -45,7 +45,7 @@ type connection struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
outgoing bool
|
outgoing bool
|
||||||
network string
|
network string
|
||||||
remoteAddr ipPort
|
remoteAddr IpPort
|
||||||
// The Reader and Writer for this Conn, with hooks installed for stats,
|
// The Reader and Writer for this Conn, with hooks installed for stats,
|
||||||
// limiting, deadlines etc.
|
// limiting, deadlines etc.
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
@ -1551,6 +1551,6 @@ func (c *connection) remoteIp() net.IP {
|
||||||
return c.remoteAddr.IP
|
return c.remoteAddr.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) remoteIpPort() ipPort {
|
func (c *connection) remoteIpPort() IpPort {
|
||||||
return c.remoteAddr
|
return c.remoteAddr
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ func TestSendBitfieldThenHave(t *testing.T) {
|
||||||
config: &ClientConfig{DownloadRateLimiter: unlimited},
|
config: &ClientConfig{DownloadRateLimiter: unlimited},
|
||||||
}
|
}
|
||||||
cl.initLogger()
|
cl.initLogger()
|
||||||
c := cl.newConnection(nil, false, ipPort{}, "")
|
c := cl.newConnection(nil, false, IpPort{}, "")
|
||||||
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
|
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
|
||||||
c.t.setInfo(&metainfo.Info{
|
c.t.setInfo(&metainfo.Info{
|
||||||
Pieces: make([]byte, metainfo.HashSize*3),
|
Pieces: make([]byte, metainfo.HashSize*3),
|
||||||
|
@ -105,7 +105,7 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
|
||||||
t.setChunkSize(defaultChunkSize)
|
t.setChunkSize(defaultChunkSize)
|
||||||
t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
|
t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
|
||||||
r, w := net.Pipe()
|
r, w := net.Pipe()
|
||||||
cn := cl.newConnection(r, true, ipPort{}, "")
|
cn := cl.newConnection(r, true, IpPort{}, "")
|
||||||
cn.setTorrent(t)
|
cn.setTorrent(t)
|
||||||
mrlErr := make(chan error)
|
mrlErr := make(chan error)
|
||||||
msg := pp.Message{
|
msg := pp.Message{
|
||||||
|
|
21
ipport.go
21
ipport.go
|
@ -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))}
|
|
||||||
}
|
|
1
misc.go
1
misc.go
|
@ -155,4 +155,5 @@ var unlimited = rate.NewLimiter(rate.Inf, 0)
|
||||||
type (
|
type (
|
||||||
pieceIndex = int
|
pieceIndex = int
|
||||||
InfoHash = metainfo.Hash
|
InfoHash = metainfo.Hash
|
||||||
|
IpPort = missinggo.IpPort
|
||||||
)
|
)
|
||||||
|
|
|
@ -13,7 +13,7 @@ func TestPrioritizedPeers(t *testing.T) {
|
||||||
pp := prioritizedPeers{
|
pp := prioritizedPeers{
|
||||||
om: btree.New(3),
|
om: btree.New(3),
|
||||||
getPrio: func(p Peer) peerPriority {
|
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()
|
_, ok := pp.DeleteMin()
|
||||||
|
|
|
@ -1736,7 +1736,7 @@ func (t *Torrent) initiateConn(peer Peer) {
|
||||||
if t.cl.badPeerIPPort(peer.IP, peer.Port) {
|
if t.cl.badPeerIPPort(peer.IP, peer.Port) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
addr := ipPort{peer.IP, uint16(peer.Port)}
|
addr := IpPort{peer.IP, uint16(peer.Port)}
|
||||||
if t.addrActive(addr.String()) {
|
if t.addrActive(addr.String()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue