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

Commit

Permalink
WIP: update quic-go
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Mar 30, 2022
1 parent c0db730 commit 1fd7be8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
12 changes: 6 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type conn struct {
sess quic.Session
quicConn quic.Connection
pconn *reuseConn
transport *transport
scope network.ConnManagementScope
Expand All @@ -33,16 +33,16 @@ var _ tpt.CapableConn = &conn{}
// It must be called even if the peer closed the connection in order for
// garbage collection to properly work in this package.
func (c *conn) Close() error {
c.transport.removeConn(c.sess)
err := c.sess.CloseWithError(0, "")
c.transport.removeConn(c.quicConn)
err := c.quicConn.CloseWithError(0, "")
c.pconn.DecreaseCount()
c.scope.Done()
return err
}

// IsClosed returns whether a connection is fully closed.
func (c *conn) IsClosed() bool {
return c.sess.Context().Err() != nil
return c.quicConn.Context().Err() != nil
}

func (c *conn) allowWindowIncrease(size uint64) bool {
Expand All @@ -51,13 +51,13 @@ func (c *conn) allowWindowIncrease(size uint64) bool {

// OpenStream creates a new stream.
func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) {
qstr, err := c.sess.OpenStreamSync(ctx)
qstr, err := c.quicConn.OpenStreamSync(ctx)
return &stream{Stream: qstr}, err
}

// AcceptStream accepts a stream opened by the other side.
func (c *conn) AcceptStream() (network.MuxedStream, error) {
qstr, err := c.sess.AcceptStream(context.Background())
qstr, err := c.quicConn.AcceptStream(context.Background())
return &stream{Stream: qstr}, err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/libp2p/go-libp2p-testing v0.7.0
github.com/libp2p/go-libp2p-tls v0.3.0
github.com/libp2p/go-netroute v0.2.0
github.com/lucas-clemente/quic-go v0.25.0
github.com/lucas-clemente/quic-go v0.26.1-0.20220327120659-d008a253b8db
github.com/minio/sha256-simd v0.1.1
github.com/multiformats/go-multiaddr v0.4.1
github.com/multiformats/go-multiaddr-fmt v0.1.0
Expand Down
18 changes: 9 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,18 @@ github.com/libp2p/go-openssl v0.0.7 h1:eCAzdLejcNVBzP/iZM9vqHnQm+XyCEbSSIheIPRGN
github.com/libp2p/go-openssl v0.0.7/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lucas-clemente/quic-go v0.25.0 h1:K+X9Gvd7JXsOHtU0N2icZ2Nw3rx82uBej3mP4CLgibc=
github.com/lucas-clemente/quic-go v0.25.0/go.mod h1:YtzP8bxRVCBlO77yRanE264+fY/T2U9ZlW1AaHOsMOg=
github.com/lucas-clemente/quic-go v0.26.1-0.20220327120659-d008a253b8db h1:Gl5FR0FhG94mvEk0JUrRh+koE+BEcxP+cFmRkTNZ/1M=
github.com/lucas-clemente/quic-go v0.26.1-0.20220327120659-d008a253b8db/go.mod h1:AzgQoPda7N+3IqMMMkywBKggIFo2KT6pfnlrQ2QieeI=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc=
github.com/marten-seemann/qtls-go1-15 v0.1.4/go.mod h1:GyFwywLKkRt+6mfU99csTEY1joMZz5vmB1WNZH3P81I=
github.com/marten-seemann/qtls-go1-16 v0.1.4 h1:xbHbOGGhrenVtII6Co8akhLEdrawwB2iHl5yhJRpnco=
github.com/marten-seemann/qtls-go1-16 v0.1.4/go.mod h1:gNpI2Ol+lRS3WwSOtIUUtRwZEQMXjYK+dQSBFbethAk=
github.com/marten-seemann/qtls-go1-17 v0.1.0 h1:P9ggrs5xtwiqXv/FHNwntmuLMNq3KaSIG93AtAZ48xk=
github.com/marten-seemann/qtls-go1-17 v0.1.0/go.mod h1:fz4HIxByo+LlWcreM4CZOYNuz3taBQ8rN2X6FqvaWo8=
github.com/marten-seemann/qtls-go1-18 v0.1.0-beta.1 h1:EnzzN9fPUkUck/1CuY1FlzBaIYMoiBsdwTNmNGkwUUM=
github.com/marten-seemann/qtls-go1-18 v0.1.0-beta.1/go.mod h1:PUhIQk19LoFt2174H4+an8TYvWOGjb/hHwphBeaDHwI=
github.com/marten-seemann/qtls-go1-16 v0.1.5 h1:o9JrYPPco/Nukd/HpOHMHZoBDXQqoNtUCmny98/1uqQ=
github.com/marten-seemann/qtls-go1-16 v0.1.5/go.mod h1:gNpI2Ol+lRS3WwSOtIUUtRwZEQMXjYK+dQSBFbethAk=
github.com/marten-seemann/qtls-go1-17 v0.1.1 h1:DQjHPq+aOzUeh9/lixAGunn6rIOQyWChPSI4+hgW7jc=
github.com/marten-seemann/qtls-go1-17 v0.1.1/go.mod h1:C2ekUKcDdz9SDWxec1N/MvcXBpaX9l3Nx67XaR84L5s=
github.com/marten-seemann/qtls-go1-18 v0.1.1 h1:qp7p7XXUFL7fpBvSS1sWD+uSqPvzNQK43DH+/qEkj0Y=
github.com/marten-seemann/qtls-go1-18 v0.1.1/go.mod h1:mJttiymBAByA49mhlNZZGrH5u1uXYZJ+RW28Py7f4m4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
Expand Down Expand Up @@ -580,6 +579,7 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
24 changes: 12 additions & 12 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ func newListener(rconn *reuseConn, t *transport, localPeer peer.ID, key ic.PrivK
// Accept accepts new connections.
func (l *listener) Accept() (tpt.CapableConn, error) {
for {
sess, err := l.quicListener.Accept(context.Background())
qconn, err := l.quicListener.Accept(context.Background())
if err != nil {
return nil, err
}
c, err := l.setupConn(sess)
c, err := l.setupConn(qconn)
if err != nil {
sess.CloseWithError(0, err.Error())
qconn.CloseWithError(0, err.Error())
continue
}
if l.transport.gater != nil && !(l.transport.gater.InterceptAccept(c) && l.transport.gater.InterceptSecured(network.DirInbound, c.remotePeerID, c)) {
c.scope.Done()
sess.CloseWithError(errorCodeConnectionGating, "connection gated")
qconn.CloseWithError(errorCodeConnectionGating, "connection gated")
continue
}
l.transport.addConn(sess, c)
l.transport.addConn(qconn, c)

// return through active hole punching if any
key := holePunchKey{addr: sess.RemoteAddr().String(), peer: c.remotePeerID}
key := holePunchKey{addr: qconn.RemoteAddr().String(), peer: c.remotePeerID}
var wasHolePunch bool
l.transport.holePunchingMx.Lock()
holePunch, ok := l.transport.holePunching[key]
Expand All @@ -97,17 +97,17 @@ func (l *listener) Accept() (tpt.CapableConn, error) {
}
}

func (l *listener) setupConn(sess quic.Session) (*conn, error) {
func (l *listener) setupConn(qconn quic.Connection) (*conn, error) {
connScope, err := l.rcmgr.OpenConnection(network.DirInbound, false)
if err != nil {
log.Debugw("resource manager blocked incoming connection", "addr", sess.RemoteAddr(), "error", err)
log.Debugw("resource manager blocked incoming connection", "addr", qconn.RemoteAddr(), "error", err)
return nil, err
}
// The tls.Config used to establish this connection already verified the certificate chain.
// Since we don't have any way of knowing which tls.Config was used though,
// we have to re-determine the peer's identity here.
// Therefore, this is expected to never fail.
remotePubKey, err := p2ptls.PubKeyFromCertChain(sess.ConnectionState().TLS.PeerCertificates)
remotePubKey, err := p2ptls.PubKeyFromCertChain(qconn.ConnectionState().TLS.PeerCertificates)
if err != nil {
connScope.Done()
return nil, err
Expand All @@ -118,19 +118,19 @@ func (l *listener) setupConn(sess quic.Session) (*conn, error) {
return nil, err
}
if err := connScope.SetPeer(remotePeerID); err != nil {
log.Debugw("resource manager blocked incoming connection for peer", "peer", remotePeerID, "addr", sess.RemoteAddr(), "error", err)
log.Debugw("resource manager blocked incoming connection for peer", "peer", remotePeerID, "addr", qconn.RemoteAddr(), "error", err)
connScope.Done()
return nil, err
}
remoteMultiaddr, err := toQuicMultiaddr(sess.RemoteAddr())
remoteMultiaddr, err := toQuicMultiaddr(qconn.RemoteAddr())
if err != nil {
connScope.Done()
return nil, err
}

l.conn.IncreaseCount()
return &conn{
sess: sess,
quicConn: qconn,
pconn: l.conn,
transport: l.transport,
scope: connScope,
Expand Down
30 changes: 15 additions & 15 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type transport struct {
holePunching map[holePunchKey]*activeHolePunch

connMx sync.Mutex
conns map[quic.Session]*conn
conns map[quic.Connection]*conn
}

var _ tpt.Transport = &transport{}
Expand Down Expand Up @@ -173,7 +173,7 @@ func NewTransport(key ic.PrivKey, psk pnet.PSK, gater connmgr.ConnectionGater, r
connManager: connManager,
gater: gater,
rcmgr: rcmgr,
conns: make(map[quic.Session]*conn),
conns: make(map[quic.Connection]*conn),
holePunching: make(map[holePunchKey]*activeHolePunch),
}
config.AllowConnectionWindowIncrease = tr.allowWindowIncrease
Expand Down Expand Up @@ -215,7 +215,7 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp
if err != nil {
return nil, err
}
sess, err := quicDialContext(ctx, pconn, addr, host, tlsConf, t.clientConfig)
qconn, err := quicDialContext(ctx, pconn, addr, host, tlsConf, t.clientConfig)
if err != nil {
scope.Done()
pconn.DecreaseCount()
Expand All @@ -235,11 +235,11 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp

localMultiaddr, err := toQuicMultiaddr(pconn.LocalAddr())
if err != nil {
sess.CloseWithError(0, "")
qconn.CloseWithError(0, "")
return nil, err
}
c := &conn{
sess: sess,
quicConn: qconn,
pconn: pconn,
transport: t,
scope: scope,
Expand All @@ -251,22 +251,22 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp
remoteMultiaddr: remoteMultiaddr,
}
if t.gater != nil && !t.gater.InterceptSecured(network.DirOutbound, p, c) {
sess.CloseWithError(errorCodeConnectionGating, "connection gated")
qconn.CloseWithError(errorCodeConnectionGating, "connection gated")
return nil, fmt.Errorf("secured connection gated")
}
t.addConn(sess, c)
t.addConn(qconn, c)
return c, nil
}

func (t *transport) addConn(sess quic.Session, c *conn) {
func (t *transport) addConn(conn quic.Connection, c *conn) {
t.connMx.Lock()
t.conns[sess] = c
t.conns[conn] = c
t.connMx.Unlock()
}

func (t *transport) removeConn(sess quic.Session) {
func (t *transport) removeConn(conn quic.Connection) {
t.connMx.Lock()
delete(t.conns, sess)
delete(t.conns, conn)
t.connMx.Unlock()
}

Expand Down Expand Up @@ -376,13 +376,13 @@ func (t *transport) Listen(addr ma.Multiaddr) (tpt.Listener, error) {
return ln, nil
}

func (t *transport) allowWindowIncrease(sess quic.Session, size uint64) bool {
// If the QUIC session tries to increase the window before we've inserted it
func (t *transport) allowWindowIncrease(conn quic.Connection, size uint64) bool {
// If the QUIC connection tries to increase the window before we've inserted it
// into our connections map (which we do right after dialing / accepting it),
// we have no way to account for that memory. This should be very rare.
// Block this attempt. The session can request more memory later.
// Block this attempt. The connection can request more memory later.
t.connMx.Lock()
c, ok := t.conns[sess]
c, ok := t.conns[conn]
t.connMx.Unlock()
if !ok {
return false
Expand Down
2 changes: 1 addition & 1 deletion transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestConnectionPassedToQUIC(t *testing.T) {
defer func() { quicDialContext = origQuicDialContext }()

var conn net.PacketConn
quicDialContext = func(_ context.Context, c net.PacketConn, _ net.Addr, _ string, _ *tls.Config, _ *quic.Config) (quic.Session, error) {
quicDialContext = func(_ context.Context, c net.PacketConn, _ net.Addr, _ string, _ *tls.Config, _ *quic.Config) (quic.Connection, error) {
conn = c
return nil, errors.New("listen error")
}
Expand Down

0 comments on commit 1fd7be8

Please sign in to comment.