Skip to content

Commit d6baf8e

Browse files
committed
send error codes on transport conn and stream failures
1 parent e5acd28 commit d6baf8e

File tree

7 files changed

+23
-12
lines changed

7 files changed

+23
-12
lines changed

p2p/net/swarm/swarm.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,12 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
385385
// If we do this in the Upgrader, we will not be able to do this.
386386
if s.gater != nil {
387387
if allow, _ := s.gater.InterceptUpgraded(c); !allow {
388-
// TODO Send disconnect with reason here
389-
err := tc.Close()
388+
var err error
389+
if tcc, ok := tc.(network.CloseWithErrorer); ok {
390+
err = tcc.CloseWithError(network.ConnGated)
391+
} else {
392+
err = tc.Close()
393+
}
390394
if err != nil {
391395
log.Warnf("failed to close connection with peer %s and addr %s; err: %s", p, addr, err)
392396
}

p2p/net/swarm/swarm_conn.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ func (c *Conn) start() {
138138
}
139139
scope, err := c.swarm.ResourceManager().OpenStream(c.RemotePeer(), network.DirInbound)
140140
if err != nil {
141-
ts.Reset()
141+
if tse, ok := ts.(network.ResetWithErrorer); ok {
142+
tse.ResetWithError(network.StreamResourceLimitExceeded)
143+
} else {
144+
ts.Reset()
145+
}
142146
continue
143147
}
144148
c.swarm.refs.Add(1)

p2p/net/upgrader/listener.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ func (l *listener) handleIncoming() {
162162
// if we stop accepting connections for some reason,
163163
// we'll eventually close all the open ones
164164
// instead of hanging onto them.
165-
conn.Close()
165+
if cc, ok := conn.(network.CloseWithErrorer); ok {
166+
cc.CloseWithError(network.ConnRateLimited)
167+
} else {
168+
conn.Close()
169+
}
166170
}
167171
}()
168172
}

p2p/transport/quic/listener.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
tpt "github.com/libp2p/go-libp2p/core/transport"
1212
p2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
1313
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
14-
1514
ma "github.com/multiformats/go-multiaddr"
1615
"github.com/quic-go/quic-go"
1716
)
@@ -54,12 +53,12 @@ func (l *listener) Accept() (tpt.CapableConn, error) {
5453
c, err := l.wrapConn(qconn)
5554
if err != nil {
5655
log.Debugf("failed to setup connection: %s", err)
57-
qconn.CloseWithError(1, "")
56+
qconn.CloseWithError(quic.ApplicationErrorCode(network.ConnResourceLimitExceeded), "")
5857
continue
5958
}
6059
l.transport.addConn(qconn, c)
6160
if l.transport.gater != nil && !(l.transport.gater.InterceptAccept(c) && l.transport.gater.InterceptSecured(network.DirInbound, c.remotePeerID, c)) {
62-
c.closeWithError(errorCodeConnectionGating, "connection gated")
61+
c.closeWithError(quic.ApplicationErrorCode(network.ConnGated), "connection gated")
6362
continue
6463
}
6564

p2p/transport/quic/transport.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ var ErrHolePunching = errors.New("hole punching attempted; no active dial")
3434

3535
var HolePunchTimeout = 5 * time.Second
3636

37-
const errorCodeConnectionGating = 0x47415445 // GATE in ASCII
38-
3937
// The Transport implements the tpt.Transport interface for QUIC connections.
4038
type transport struct {
4139
privKey ic.PrivKey
@@ -169,7 +167,7 @@ func (t *transport) dialWithScope(ctx context.Context, raddr ma.Multiaddr, p pee
169167
remoteMultiaddr: raddr,
170168
}
171169
if t.gater != nil && !t.gater.InterceptSecured(network.DirOutbound, p, c) {
172-
pconn.CloseWithError(errorCodeConnectionGating, "connection gated")
170+
pconn.CloseWithError(quic.ApplicationErrorCode(network.ConnGated), "connection gated")
173171
return nil, fmt.Errorf("secured connection gated")
174172
}
175173
t.addConn(pconn, c)

p2p/transport/quic/virtuallistener.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package libp2pquic
33
import (
44
"sync"
55

6+
"github.com/libp2p/go-libp2p/core/network"
67
tpt "github.com/libp2p/go-libp2p/core/transport"
78
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
89

@@ -142,8 +143,8 @@ func (r *acceptLoopRunner) innerAccept(l *listener, expectedVersion quic.Version
142143
select {
143144
case ch <- acceptVal{conn: conn}:
144145
default:
146+
conn.(network.CloseWithErrorer).CloseWithError(network.ConnRateLimited)
145147
// accept queue filled up, drop the connection
146-
conn.Close()
147148
log.Warn("Accept queue filled. Dropping connection.")
148149
}
149150

p2p/transport/quicreuse/listener.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"sync"
1212

13+
"github.com/libp2p/go-libp2p/core/network"
1314
"github.com/libp2p/go-libp2p/core/transport"
1415
ma "github.com/multiformats/go-multiaddr"
1516
"github.com/quic-go/quic-go"
@@ -212,7 +213,7 @@ func (l *listener) Close() error {
212213
close(l.queue)
213214
// drain the queue
214215
for conn := range l.queue {
215-
conn.CloseWithError(1, "closing")
216+
conn.CloseWithError(quic.ApplicationErrorCode(network.ConnShutdown), "closing")
216217
}
217218
})
218219
return nil

0 commit comments

Comments
 (0)