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
|
||||
}
|
||||
|
||||
func (me Peer) addr() ipPort {
|
||||
return ipPort{me.IP, uint16(me.Port)}
|
||||
func (me Peer) addr() IpPort {
|
||||
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))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}())
|
||||
|
|
18
client.go
18
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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
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 (
|
||||
pieceIndex = int
|
||||
InfoHash = metainfo.Hash
|
||||
IpPort = missinggo.IpPort
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue