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

Tag the hop relay when creating stop streams #77

Merged
merged 3 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type Conn struct {
stream inet.Stream
raulk marked this conversation as resolved.
Show resolved Hide resolved
remote pstore.PeerInfo
relay *Relay
vyzo marked this conversation as resolved.
Show resolved Hide resolved
}

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

func (c *Conn) Close() error {
c.untagHop()
return c.stream.Reset()
}

Expand Down Expand Up @@ -60,6 +62,14 @@ func (c *Conn) RemoteAddr() net.Addr {
}
}

func (c *Conn) tagHop() {
c.relay.host.ConnManager().UpsertTag(c.stream.Conn().RemotePeer(), "relay-hop-stream", incrementTag)
vyzo marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *Conn) untagHop() {
c.relay.host.ConnManager().UpsertTag(c.stream.Conn().RemotePeer(), "relay-hop-stream", decrementTag)
}

// TODO: is it okay to cast c.Conn().RemotePeer() into a multiaddr? might be "user input"
func (c *Conn) RemoteMultiaddr() ma.Multiaddr {
proto := ma.ProtocolWithCode(ma.P_P2P).Name
Expand Down
1 change: 1 addition & 0 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (d *RelayTransport) Dial(ctx context.Context, a ma.Multiaddr, p peer.ID) (t
if err != nil {
return nil, err
}
c.tagHop()
return d.upgrader.UpgradeOutbound(ctx, d, c, p)
}

Expand Down
1 change: 1 addition & 0 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (l *RelayListener) Accept() (manet.Conn, error) {
// TODO: Pretty print.
log.Infof("accepted relay connection: %q", c)

c.tagHop()
return c, nil
case <-l.ctx.Done():
return nil, l.ctx.Err()
Expand Down
12 changes: 6 additions & 6 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ func NewRelay(ctx context.Context, h host.Host, upgrader *tptu.Upgrader, opts ..

func (r *Relay) addLiveHop(from, to peer.ID) {
atomic.AddInt32(&r.liveHopCount, 1)
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", func(v int) int { return v + 1 })
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", func(v int) int { return v + 1 })
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", incrementTag)
vyzo marked this conversation as resolved.
Show resolved Hide resolved
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", incrementTag)
}

func (r *Relay) rmLiveHop(from, to peer.ID) {
atomic.AddInt32(&r.liveHopCount, -1)
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", func(v int) int { return v - 1 })
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", func(v int) int { return v - 1 })
r.host.ConnManager().UpsertTag(from, "relay-hop-stream", decrementTag)
vyzo marked this conversation as resolved.
Show resolved Hide resolved
r.host.ConnManager().UpsertTag(to, "relay-hop-stream", decrementTag)

}

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

return &Conn{stream: s, remote: dest}, nil
return &Conn{stream: s, remote: dest, relay: r}, nil
}

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

select {
case r.incoming <- &Conn{stream: s, remote: src}:
case r.incoming <- &Conn{stream: s, remote: src, relay: r}:
case <-time.After(RelayAcceptTimeout):
r.handleError(s, pb.CircuitRelay_STOP_RELAY_REFUSED)
}
Expand Down
12 changes: 12 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func peerInfoToPeer(pi pstore.PeerInfo) *pb.CircuitRelay_Peer {
return p
}

func incrementTag(v int) int {
return v + 1
}

func decrementTag(v int) int {
if v > 0 {
return v - 1
} else {
return v
}
}

type delimitedReader struct {
r io.Reader
buf []byte
Expand Down