Skip to content

Commit 1dc406c

Browse files
committedNov 15, 2018
Move IpPort to missinggo
1 parent 148bb97 commit 1dc406c

10 files changed

+29
-49
lines changed
 

‎Peer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
2828
me.PexPeerFlags = fs
2929
}
3030

31-
func (me Peer) addr() ipPort {
32-
return ipPort{me.IP, uint16(me.Port)}
31+
func (me Peer) addr() IpPort {
32+
return IpPort{me.IP, uint16(me.Port)}
3333
}

‎bep40.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func ipv6Mask(a, b net.IP) net.IPMask {
4848
panic(fmt.Sprintf("%s %s", a, b))
4949
}
5050

51-
func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
51+
func bep40PriorityBytes(a, b IpPort) ([]byte, error) {
5252
if a.IP.Equal(b.IP) {
5353
var ret [4]byte
5454
binary.BigEndian.PutUint16(ret[0:2], a.Port)
@@ -66,7 +66,7 @@ func bep40PriorityBytes(a, b ipPort) ([]byte, error) {
6666
return nil, errors.New("incomparable IPs")
6767
}
6868

69-
func bep40Priority(a, b ipPort) (peerPriority, error) {
69+
func bep40Priority(a, b IpPort) (peerPriority, error) {
7070
bs, err := bep40PriorityBytes(a, b)
7171
if err != nil {
7272
return 0, err
@@ -79,7 +79,7 @@ func bep40Priority(a, b ipPort) (peerPriority, error) {
7979
return crc32.Checksum(bs, table), nil
8080
}
8181

82-
func bep40PriorityIgnoreError(a, b ipPort) peerPriority {
82+
func bep40PriorityIgnoreError(a, b IpPort) peerPriority {
8383
prio, _ := bep40Priority(a, b)
8484
return prio
8585
}

‎bep40_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import (
99

1010
func TestBep40Priority(t *testing.T) {
1111
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
12-
ipPort{net.ParseIP("123.213.32.10"), 0},
13-
ipPort{net.ParseIP("98.76.54.32"), 0},
12+
IpPort{net.ParseIP("123.213.32.10"), 0},
13+
IpPort{net.ParseIP("98.76.54.32"), 0},
1414
))
1515
assert.EqualValues(t, 0xec2d7224, bep40PriorityIgnoreError(
16-
ipPort{net.ParseIP("98.76.54.32"), 0},
17-
ipPort{net.ParseIP("123.213.32.10"), 0},
16+
IpPort{net.ParseIP("98.76.54.32"), 0},
17+
IpPort{net.ParseIP("123.213.32.10"), 0},
1818
))
1919
assert.Equal(t, peerPriority(0x99568189), bep40PriorityIgnoreError(
20-
ipPort{net.ParseIP("123.213.32.10"), 0},
21-
ipPort{net.ParseIP("123.213.32.234"), 0},
20+
IpPort{net.ParseIP("123.213.32.10"), 0},
21+
IpPort{net.ParseIP("123.213.32.234"), 0},
2222
))
2323
assert.EqualValues(t, "\x00\x00\x00\x00", func() []byte {
2424
b, _ := bep40PriorityBytes(
25-
ipPort{net.ParseIP("123.213.32.234"), 0},
26-
ipPort{net.ParseIP("123.213.32.234"), 0},
25+
IpPort{net.ParseIP("123.213.32.234"), 0},
26+
IpPort{net.ParseIP("123.213.32.234"), 0},
2727
)
2828
return b
2929
}())

‎client.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (cl *Client) incomingConnection(nc net.Conn) {
442442
if tc, ok := nc.(*net.TCPConn); ok {
443443
tc.SetLinger(0)
444444
}
445-
c := cl.newConnection(nc, false, ipPortFromNetAddr(nc.RemoteAddr()), nc.RemoteAddr().Network())
445+
c := cl.newConnection(nc, false, missinggo.IpPortFromNetAddr(nc.RemoteAddr()), nc.RemoteAddr().Network())
446446
c.Discovery = peerSourceIncoming
447447
cl.runReceivedConn(c)
448448
}
@@ -585,7 +585,7 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
585585

586586
// Performs initiator handshakes and returns a connection. Returns nil
587587
// *connection if no connection for valid reasons.
588-
func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr ipPort, network string) (c *connection, err error) {
588+
func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr IpPort, network string) (c *connection, err error) {
589589
c = cl.newConnection(nc, true, remoteAddr, network)
590590
c.headerEncrypted = encryptHeader
591591
ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
@@ -607,7 +607,7 @@ func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torr
607607

608608
// Returns nil connection and nil error if no connection could be established
609609
// for valid reasons.
610-
func (cl *Client) establishOutgoingConnEx(t *Torrent, addr ipPort, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) {
610+
func (cl *Client) establishOutgoingConnEx(t *Torrent, addr IpPort, ctx context.Context, obfuscatedHeader bool) (c *connection, err error) {
611611
dr := cl.dialFirst(ctx, addr.String())
612612
nc := dr.Conn
613613
if nc == nil {
@@ -623,7 +623,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr ipPort, ctx context.C
623623

624624
// Returns nil connection and nil error if no connection could be established
625625
// for valid reasons.
626-
func (cl *Client) establishOutgoingConn(t *Torrent, addr ipPort) (c *connection, err error) {
626+
func (cl *Client) establishOutgoingConn(t *Torrent, addr IpPort) (c *connection, err error) {
627627
torrent.Add("establish outgoing connection", 1)
628628
ctx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
629629
cl.rLock()
@@ -658,7 +658,7 @@ func (cl *Client) establishOutgoingConn(t *Torrent, addr ipPort) (c *connection,
658658

659659
// Called to dial out and run a connection. The addr we're given is already
660660
// considered half-open.
661-
func (cl *Client) outgoingConnection(t *Torrent, addr ipPort, ps peerSource) {
661+
func (cl *Client) outgoingConnection(t *Torrent, addr IpPort, ps peerSource) {
662662
cl.dialRateLimiter.Wait(context.Background())
663663
c, err := cl.establishOutgoingConn(t, addr)
664664
cl.lock()
@@ -1182,7 +1182,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
11821182
cl.badPeerIPs[ip.String()] = struct{}{}
11831183
}
11841184

1185-
func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr ipPort, network string) (c *connection) {
1185+
func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr IpPort, network string) (c *connection) {
11861186
c = &connection{
11871187
conn: nc,
11881188
outgoing: outgoing,
@@ -1263,8 +1263,8 @@ func (cl *Client) findListenerIp(f func(net.IP) bool) net.IP {
12631263
}
12641264

12651265
// Our IP as a peer should see it.
1266-
func (cl *Client) publicAddr(peer net.IP) ipPort {
1267-
return ipPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
1266+
func (cl *Client) publicAddr(peer net.IP) IpPort {
1267+
return IpPort{cl.publicIp(peer), uint16(cl.incomingPeerPort())}
12681268
}
12691269

12701270
func (cl *Client) ListenAddrs() (ret []net.Addr) {
@@ -1277,7 +1277,7 @@ func (cl *Client) ListenAddrs() (ret []net.Addr) {
12771277
return
12781278
}
12791279

1280-
func (cl *Client) onBadAccept(addr ipPort) {
1280+
func (cl *Client) onBadAccept(addr IpPort) {
12811281
ip := maskIpForAcceptLimiting(addr.IP)
12821282
if cl.acceptLimiter == nil {
12831283
cl.acceptLimiter = make(map[ipStr]int)

‎connection.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type connection struct {
4545
conn net.Conn
4646
outgoing bool
4747
network string
48-
remoteAddr ipPort
48+
remoteAddr IpPort
4949
// The Reader and Writer for this Conn, with hooks installed for stats,
5050
// limiting, deadlines etc.
5151
w io.Writer
@@ -1551,6 +1551,6 @@ func (c *connection) remoteIp() net.IP {
15511551
return c.remoteAddr.IP
15521552
}
15531553

1554-
func (c *connection) remoteIpPort() ipPort {
1554+
func (c *connection) remoteIpPort() IpPort {
15551555
return c.remoteAddr
15561556
}

‎connection_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestSendBitfieldThenHave(t *testing.T) {
2323
config: &ClientConfig{DownloadRateLimiter: unlimited},
2424
}
2525
cl.initLogger()
26-
c := cl.newConnection(nil, false, ipPort{}, "")
26+
c := cl.newConnection(nil, false, IpPort{}, "")
2727
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
2828
c.t.setInfo(&metainfo.Info{
2929
Pieces: make([]byte, metainfo.HashSize*3),
@@ -105,7 +105,7 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
105105
t.setChunkSize(defaultChunkSize)
106106
t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
107107
r, w := net.Pipe()
108-
cn := cl.newConnection(r, true, ipPort{}, "")
108+
cn := cl.newConnection(r, true, IpPort{}, "")
109109
cn.setTorrent(t)
110110
mrlErr := make(chan error)
111111
msg := pp.Message{

‎ipport.go

-21
This file was deleted.

‎misc.go

+1
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,5 @@ var unlimited = rate.NewLimiter(rate.Inf, 0)
155155
type (
156156
pieceIndex = int
157157
InfoHash = metainfo.Hash
158+
IpPort = missinggo.IpPort
158159
)

‎prioritized_peers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestPrioritizedPeers(t *testing.T) {
1313
pp := prioritizedPeers{
1414
om: btree.New(3),
1515
getPrio: func(p Peer) peerPriority {
16-
return bep40PriorityIgnoreError(p.addr(), ipPort{IP: net.ParseIP("0.0.0.0")})
16+
return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
1717
},
1818
}
1919
_, ok := pp.DeleteMin()

‎torrent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ func (t *Torrent) initiateConn(peer Peer) {
17361736
if t.cl.badPeerIPPort(peer.IP, peer.Port) {
17371737
return
17381738
}
1739-
addr := ipPort{peer.IP, uint16(peer.Port)}
1739+
addr := IpPort{peer.IP, uint16(peer.Port)}
17401740
if t.addrActive(addr.String()) {
17411741
return
17421742
}

0 commit comments

Comments
 (0)
Please sign in to comment.