status-go/vendor/github.com/lucas-clemente/quic-go/conn_id_generator.go

144 lines
4.6 KiB
Go
Raw Normal View History

2022-04-01 16:16:46 +00:00
package quic
import (
"fmt"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/internal/wire"
)
type connIDGenerator struct {
2022-11-04 13:57:20 +00:00
generator ConnectionIDGenerator
2022-04-01 16:16:46 +00:00
highestSeq uint64
activeSrcConnIDs map[uint64]protocol.ConnectionID
initialClientDestConnID protocol.ConnectionID
addConnectionID func(protocol.ConnectionID)
getStatelessResetToken func(protocol.ConnectionID) protocol.StatelessResetToken
removeConnectionID func(protocol.ConnectionID)
retireConnectionID func(protocol.ConnectionID)
2022-11-04 13:57:20 +00:00
replaceWithClosed func([]protocol.ConnectionID, protocol.Perspective, []byte)
2022-04-01 16:16:46 +00:00
queueControlFrame func(wire.Frame)
version protocol.VersionNumber
}
func newConnIDGenerator(
initialConnectionID protocol.ConnectionID,
initialClientDestConnID protocol.ConnectionID, // nil for the client
addConnectionID func(protocol.ConnectionID),
getStatelessResetToken func(protocol.ConnectionID) protocol.StatelessResetToken,
removeConnectionID func(protocol.ConnectionID),
retireConnectionID func(protocol.ConnectionID),
2022-11-04 13:57:20 +00:00
replaceWithClosed func([]protocol.ConnectionID, protocol.Perspective, []byte),
2022-04-01 16:16:46 +00:00
queueControlFrame func(wire.Frame),
2022-11-04 13:57:20 +00:00
generator ConnectionIDGenerator,
2022-04-01 16:16:46 +00:00
version protocol.VersionNumber,
) *connIDGenerator {
m := &connIDGenerator{
2022-11-04 13:57:20 +00:00
generator: generator,
2022-04-01 16:16:46 +00:00
activeSrcConnIDs: make(map[uint64]protocol.ConnectionID),
addConnectionID: addConnectionID,
getStatelessResetToken: getStatelessResetToken,
removeConnectionID: removeConnectionID,
retireConnectionID: retireConnectionID,
replaceWithClosed: replaceWithClosed,
queueControlFrame: queueControlFrame,
version: version,
}
m.activeSrcConnIDs[0] = initialConnectionID
m.initialClientDestConnID = initialClientDestConnID
return m
}
func (m *connIDGenerator) SetMaxActiveConnIDs(limit uint64) error {
2022-11-04 13:57:20 +00:00
if m.generator.ConnectionIDLen() == 0 {
2022-04-01 16:16:46 +00:00
return nil
}
// The active_connection_id_limit transport parameter is the number of
// connection IDs the peer will store. This limit includes the connection ID
// used during the handshake, and the one sent in the preferred_address
// transport parameter.
// We currently don't send the preferred_address transport parameter,
// so we can issue (limit - 1) connection IDs.
2022-11-04 13:57:20 +00:00
for i := uint64(len(m.activeSrcConnIDs)); i < utils.Min(limit, protocol.MaxIssuedConnectionIDs); i++ {
2022-04-01 16:16:46 +00:00
if err := m.issueNewConnID(); err != nil {
return err
}
}
return nil
}
func (m *connIDGenerator) Retire(seq uint64, sentWithDestConnID protocol.ConnectionID) error {
if seq > m.highestSeq {
return &qerr.TransportError{
ErrorCode: qerr.ProtocolViolation,
ErrorMessage: fmt.Sprintf("retired connection ID %d (highest issued: %d)", seq, m.highestSeq),
}
}
connID, ok := m.activeSrcConnIDs[seq]
// We might already have deleted this connection ID, if this is a duplicate frame.
if !ok {
return nil
}
if connID.Equal(sentWithDestConnID) {
return &qerr.TransportError{
ErrorCode: qerr.ProtocolViolation,
ErrorMessage: fmt.Sprintf("retired connection ID %d (%s), which was used as the Destination Connection ID on this packet", seq, connID),
}
}
m.retireConnectionID(connID)
delete(m.activeSrcConnIDs, seq)
// Don't issue a replacement for the initial connection ID.
if seq == 0 {
return nil
}
return m.issueNewConnID()
}
func (m *connIDGenerator) issueNewConnID() error {
2022-11-04 13:57:20 +00:00
connID, err := m.generator.GenerateConnectionID()
2022-04-01 16:16:46 +00:00
if err != nil {
return err
}
m.activeSrcConnIDs[m.highestSeq+1] = connID
m.addConnectionID(connID)
m.queueControlFrame(&wire.NewConnectionIDFrame{
SequenceNumber: m.highestSeq + 1,
ConnectionID: connID,
StatelessResetToken: m.getStatelessResetToken(connID),
})
m.highestSeq++
return nil
}
func (m *connIDGenerator) SetHandshakeComplete() {
if m.initialClientDestConnID != nil {
m.retireConnectionID(m.initialClientDestConnID)
m.initialClientDestConnID = nil
}
}
func (m *connIDGenerator) RemoveAll() {
if m.initialClientDestConnID != nil {
m.removeConnectionID(m.initialClientDestConnID)
}
for _, connID := range m.activeSrcConnIDs {
m.removeConnectionID(connID)
}
}
2022-11-04 13:57:20 +00:00
func (m *connIDGenerator) ReplaceWithClosed(pers protocol.Perspective, connClose []byte) {
connIDs := make([]protocol.ConnectionID, 0, len(m.activeSrcConnIDs)+1)
2022-04-01 16:16:46 +00:00
if m.initialClientDestConnID != nil {
2022-11-04 13:57:20 +00:00
connIDs = append(connIDs, m.initialClientDestConnID)
2022-04-01 16:16:46 +00:00
}
for _, connID := range m.activeSrcConnIDs {
2022-11-04 13:57:20 +00:00
connIDs = append(connIDs, connID)
2022-04-01 16:16:46 +00:00
}
2022-11-04 13:57:20 +00:00
m.replaceWithClosed(connIDs, pers, connClose)
2022-04-01 16:16:46 +00:00
}