Skip to content
This repository was archived by the owner on Sep 9, 2022. It is now read-only.

Commit 2ec9f71

Browse files
committed
tag hop relays for active stop connections
1 parent 1804298 commit 2ec9f71

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

conn.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
type Conn struct {
1515
stream inet.Stream
1616
remote pstore.PeerInfo
17+
relay *Relay
1718
}
1819

1920
type NetAddr struct {
@@ -30,6 +31,7 @@ func (n *NetAddr) String() string {
3031
}
3132

3233
func (c *Conn) Close() error {
34+
c.untagHop()
3335
return c.stream.Reset()
3436
}
3537

@@ -60,6 +62,14 @@ func (c *Conn) RemoteAddr() net.Addr {
6062
}
6163
}
6264

65+
func (c *Conn) tagHop() {
66+
c.relay.host.ConnManager().UpsertTag(c.stream.Conn().RemotePeer(), "relay-hop-stream", incrementTag)
67+
}
68+
69+
func (c *Conn) untagHop() {
70+
c.relay.host.ConnManager().UpsertTag(c.stream.Conn().RemotePeer(), "relay-hop-stream", decrementTag)
71+
}
72+
6373
// TODO: is it okay to cast c.Conn().RemotePeer() into a multiaddr? might be "user input"
6474
func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
6575
proto := ma.ProtocolWithCode(ma.P_P2P).Name

dial.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (t
1616
if err != nil {
1717
return nil, err
1818
}
19+
c.tagHop()
1920
return d.upgrader.UpgradeOutbound(ctx, d, c, p)
2021
}
2122

listen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (l *RelayListener) Accept() (manet.Conn, error) {
3535
// TODO: Pretty print.
3636
log.Infof("accepted relay connection: %q", c)
3737

38+
c.tagHop()
3839
return c, nil
3940
case <-l.ctx.Done():
4041
return nil, l.ctx.Err()

relay.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ..
118118

119119
func (r *Relay) addLiveHop(from, to peer.ID) {
120120
atomic.AddInt32(&r.liveHopCount, 1)
121-
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", func(v int) int { return v + 1 })
122-
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", func(v int) int { return v + 1 })
121+
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", incrementTag)
122+
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", incrementTag)
123123
}
124124

125125
func (r *Relay) rmLiveHop(from, to peer.ID) {
126126
atomic.AddInt32(&r.liveHopCount, -1)
127-
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", func(v int) int { return v - 1 })
128-
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", func(v int) int { return v - 1 })
127+
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", decrementTag)
128+
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", decrementTag)
129129

130130
}
131131

@@ -180,7 +180,7 @@ func (r *Relay) DialPeer(ctx context.Context, relay pstore.PeerInfo, dest pstore
180180
return nil, RelayError{msg.GetCode()}
181181
}
182182

183-
return &Conn{stream: s, remote: dest}, nil
183+
return &Conn{stream: s, remote: dest, relay: r}, nil
184184
}
185185

186186
func (r *Relay) Matches(addr ma.Multiaddr) bool {
@@ -438,7 +438,7 @@ func (r *Relay) handleStopStream(s inet.Stream, msg *pb.CircuitRelay) {
438438
}
439439

440440
select {
441-
case r.incoming <- &Conn{stream: s, remote: src}:
441+
case r.incoming <- &Conn{stream: s, remote: src, relay: r}:
442442
case <-time.After(RelayAcceptTimeout):
443443
r.handleError(s, pb.CircuitRelay_STOP_RELAY_REFUSED)
444444
}

util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ func peerInfoToPeer(pi pstore.PeerInfo) *pb.CircuitRelay_Peer {
4949
return p
5050
}
5151

52+
func incrementTag(v int) int {
53+
return v + 1
54+
}
55+
56+
func decrementTag(v int) int {
57+
if v > 0 {
58+
return v - 1
59+
} else {
60+
return v
61+
}
62+
}
63+
5264
type delimitedReader struct {
5365
r io.Reader
5466
buf []byte

0 commit comments

Comments
 (0)