2018-11-20 17:51:25 -05:00
package quic
import (
"context"
"crypto/tls"
"fmt"
"net"
"sync"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
2018-11-23 11:04:53 -05:00
"github.com/lucas-clemente/quic-go/internal/qerr"
2018-11-20 17:51:25 -05:00
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/internal/wire"
)
type client struct {
mutex sync . Mutex
conn connection
// If the client is created with DialAddr, we create a packet conn.
// If it is started with Dial, we take a packet conn as a parameter.
createdPacketConn bool
packetHandlers packetHandlerManager
2018-11-23 11:04:53 -05:00
token [ ] byte
2018-11-20 17:51:25 -05:00
2018-11-27 09:29:03 -05:00
versionNegotiated utils . AtomicBool // has the server accepted our version
2018-11-20 17:51:25 -05:00
receivedVersionNegotiationPacket bool
negotiatedVersions [ ] protocol . VersionNumber // the list of versions from the version negotiation packet
2018-11-23 11:04:53 -05:00
tlsConf * tls . Config
config * Config
2018-11-20 17:51:25 -05:00
2018-11-23 11:04:53 -05:00
srcConnID protocol . ConnectionID
destConnID protocol . ConnectionID
origDestConnID protocol . ConnectionID // the destination conn ID used on the first Initial (before a Retry)
2018-11-20 17:51:25 -05:00
2019-01-02 07:01:06 -05:00
initialPacketNumber protocol . PacketNumber
2018-11-20 17:51:25 -05:00
initialVersion protocol . VersionNumber
version protocol . VersionNumber
handshakeChan chan struct { }
session quicSession
logger utils . Logger
}
var _ packetHandler = & client { }
var (
// make it possible to mock connection ID generation in the tests
generateConnectionID = protocol . GenerateConnectionID
generateConnectionIDForInitial = protocol . GenerateConnectionIDForInitial
)
// DialAddr establishes a new QUIC connection to a server.
2018-11-27 09:29:03 -05:00
// It uses a new UDP connection and closes this connection when the QUIC session is closed.
2018-11-20 17:51:25 -05:00
// The hostname for SNI is taken from the given address.
func DialAddr (
addr string ,
tlsConf * tls . Config ,
config * Config ,
) ( Session , error ) {
return DialAddrContext ( context . Background ( ) , addr , tlsConf , config )
}
// DialAddrContext establishes a new QUIC connection to a server using the provided context.
2018-11-27 09:29:03 -05:00
// See DialAddr for details.
2018-11-20 17:51:25 -05:00
func DialAddrContext (
ctx context . Context ,
addr string ,
tlsConf * tls . Config ,
config * Config ,
) ( Session , error ) {
udpAddr , err := net . ResolveUDPAddr ( "udp" , addr )
if err != nil {
return nil , err
}
udpConn , err := net . ListenUDP ( "udp" , & net . UDPAddr { IP : net . IPv4zero , Port : 0 } )
if err != nil {
return nil , err
}
return dialContext ( ctx , udpConn , udpAddr , addr , tlsConf , config , true )
}
// Dial establishes a new QUIC connection to a server using a net.PacketConn.
2018-11-27 09:29:03 -05:00
// The same PacketConn can be used for multiple calls to Dial and Listen,
// QUIC connection IDs are used for demultiplexing the different connections.
2018-11-20 17:51:25 -05:00
// The host parameter is used for SNI.
func Dial (
pconn net . PacketConn ,
remoteAddr net . Addr ,
host string ,
tlsConf * tls . Config ,
config * Config ,
) ( Session , error ) {
return DialContext ( context . Background ( ) , pconn , remoteAddr , host , tlsConf , config )
}
// DialContext establishes a new QUIC connection to a server using a net.PacketConn using the provided context.
2018-11-27 09:29:03 -05:00
// See Dial for details.
2018-11-20 17:51:25 -05:00
func DialContext (
ctx context . Context ,
pconn net . PacketConn ,
remoteAddr net . Addr ,
host string ,
tlsConf * tls . Config ,
config * Config ,
) ( Session , error ) {
return dialContext ( ctx , pconn , remoteAddr , host , tlsConf , config , false )
}
func dialContext (
ctx context . Context ,
pconn net . PacketConn ,
remoteAddr net . Addr ,
host string ,
tlsConf * tls . Config ,
config * Config ,
createdPacketConn bool ,
) ( Session , error ) {
config = populateClientConfig ( config , createdPacketConn )
packetHandlers , err := getMultiplexer ( ) . AddConn ( pconn , config . ConnectionIDLength )
if err != nil {
return nil , err
}
2018-11-23 11:04:53 -05:00
c , err := newClient ( pconn , remoteAddr , config , tlsConf , host , createdPacketConn )
2018-11-20 17:51:25 -05:00
if err != nil {
return nil , err
}
c . packetHandlers = packetHandlers
if err := c . dial ( ctx ) ; err != nil {
return nil , err
}
return c . session , nil
}
func newClient (
pconn net . PacketConn ,
remoteAddr net . Addr ,
config * Config ,
tlsConf * tls . Config ,
host string ,
createdPacketConn bool ,
) ( * client , error ) {
2018-11-23 11:04:53 -05:00
if tlsConf == nil {
tlsConf = & tls . Config { }
2018-11-20 17:51:25 -05:00
}
2018-11-23 11:04:53 -05:00
if tlsConf . ServerName == "" {
2018-11-20 17:51:25 -05:00
var err error
2018-11-23 11:04:53 -05:00
tlsConf . ServerName , _ , err = net . SplitHostPort ( host )
2018-11-20 17:51:25 -05:00
if err != nil {
return nil , err
}
}
// check that all versions are actually supported
if config != nil {
for _ , v := range config . Versions {
if ! protocol . IsValidVersion ( v ) {
return nil , fmt . Errorf ( "%s is not a valid QUIC version" , v )
}
}
}
2018-11-27 09:29:03 -05:00
srcConnID , err := generateConnectionID ( config . ConnectionIDLength )
if err != nil {
return nil , err
}
destConnID , err := generateConnectionIDForInitial ( )
if err != nil {
return nil , err
}
2018-11-20 17:51:25 -05:00
c := & client {
2018-11-27 09:29:03 -05:00
srcConnID : srcConnID ,
destConnID : destConnID ,
2018-11-20 17:51:25 -05:00
conn : & conn { pconn : pconn , currentAddr : remoteAddr } ,
createdPacketConn : createdPacketConn ,
tlsConf : tlsConf ,
config : config ,
version : config . Versions [ 0 ] ,
handshakeChan : make ( chan struct { } ) ,
logger : utils . DefaultLogger . WithPrefix ( "client" ) ,
}
2018-11-27 09:29:03 -05:00
return c , nil
2018-11-20 17:51:25 -05:00
}
// populateClientConfig populates fields in the quic.Config with their default values, if none are set
// it may be called with nil
func populateClientConfig ( config * Config , createdPacketConn bool ) * Config {
if config == nil {
config = & Config { }
}
versions := config . Versions
if len ( versions ) == 0 {
versions = protocol . SupportedVersions
}
handshakeTimeout := protocol . DefaultHandshakeTimeout
if config . HandshakeTimeout != 0 {
handshakeTimeout = config . HandshakeTimeout
}
idleTimeout := protocol . DefaultIdleTimeout
if config . IdleTimeout != 0 {
idleTimeout = config . IdleTimeout
}
maxReceiveStreamFlowControlWindow := config . MaxReceiveStreamFlowControlWindow
if maxReceiveStreamFlowControlWindow == 0 {
2018-11-23 11:04:53 -05:00
maxReceiveStreamFlowControlWindow = protocol . DefaultMaxReceiveStreamFlowControlWindow
2018-11-20 17:51:25 -05:00
}
maxReceiveConnectionFlowControlWindow := config . MaxReceiveConnectionFlowControlWindow
if maxReceiveConnectionFlowControlWindow == 0 {
2018-11-23 11:04:53 -05:00
maxReceiveConnectionFlowControlWindow = protocol . DefaultMaxReceiveConnectionFlowControlWindow
2018-11-20 17:51:25 -05:00
}
maxIncomingStreams := config . MaxIncomingStreams
if maxIncomingStreams == 0 {
maxIncomingStreams = protocol . DefaultMaxIncomingStreams
} else if maxIncomingStreams < 0 {
maxIncomingStreams = 0
}
maxIncomingUniStreams := config . MaxIncomingUniStreams
if maxIncomingUniStreams == 0 {
maxIncomingUniStreams = protocol . DefaultMaxIncomingUniStreams
} else if maxIncomingUniStreams < 0 {
maxIncomingUniStreams = 0
}
connIDLen := config . ConnectionIDLength
if connIDLen == 0 && ! createdPacketConn {
connIDLen = protocol . DefaultConnectionIDLength
}
return & Config {
Versions : versions ,
HandshakeTimeout : handshakeTimeout ,
IdleTimeout : idleTimeout ,
ConnectionIDLength : connIDLen ,
MaxReceiveStreamFlowControlWindow : maxReceiveStreamFlowControlWindow ,
MaxReceiveConnectionFlowControlWindow : maxReceiveConnectionFlowControlWindow ,
MaxIncomingStreams : maxIncomingStreams ,
MaxIncomingUniStreams : maxIncomingUniStreams ,
KeepAlive : config . KeepAlive ,
}
}
func ( c * client ) dial ( ctx context . Context ) error {
2018-11-23 11:04:53 -05:00
c . logger . Infof ( "Starting new connection to %s (%s -> %s), source connection ID %s, destination connection ID %s, version %s" , c . tlsConf . ServerName , c . conn . LocalAddr ( ) , c . conn . RemoteAddr ( ) , c . srcConnID , c . destConnID , c . version )
2018-11-20 17:51:25 -05:00
2018-11-23 11:04:53 -05:00
if err := c . createNewTLSSession ( c . version ) ; err != nil {
2018-11-20 17:51:25 -05:00
return err
}
err := c . establishSecureConnection ( ctx )
2019-01-02 07:01:06 -05:00
if err == errCloseForRecreating {
2018-11-20 17:51:25 -05:00
return c . dial ( ctx )
}
return err
}
// establishSecureConnection runs the session, and tries to establish a secure connection
// It returns:
2019-01-02 07:01:06 -05:00
// - errCloseSessionRecreating when the server sends a version negotiation packet, or a stateless retry is performed
2018-11-20 17:51:25 -05:00
// - any other error that might occur
2018-11-23 11:04:53 -05:00
// - when the connection is forward-secure
2018-11-20 17:51:25 -05:00
func ( c * client ) establishSecureConnection ( ctx context . Context ) error {
errorChan := make ( chan error , 1 )
go func ( ) {
err := c . session . run ( ) // returns as soon as the session is closed
2019-01-02 07:01:06 -05:00
if err != errCloseForRecreating && c . createdPacketConn {
2018-11-20 17:51:25 -05:00
c . conn . Close ( )
}
errorChan <- err
} ( )
select {
case <- ctx . Done ( ) :
// The session will send a PeerGoingAway error to the server.
c . session . Close ( )
return ctx . Err ( )
case err := <- errorChan :
return err
case <- c . handshakeChan :
// handshake successfully completed
return nil
}
}
func ( c * client ) handlePacket ( p * receivedPacket ) {
2018-11-27 09:29:03 -05:00
if p . hdr . IsVersionNegotiation ( ) {
go c . handleVersionNegotiationPacket ( p . hdr )
return
2018-11-20 17:51:25 -05:00
}
2018-11-27 09:29:03 -05:00
if p . hdr . Type == protocol . PacketTypeRetry {
go c . handleRetryPacket ( p . hdr )
return
2018-11-20 17:51:25 -05:00
}
// this is the first packet we are receiving
// since it is not a Version Negotiation Packet, this means the server supports the suggested version
2018-11-27 09:29:03 -05:00
if ! c . versionNegotiated . Get ( ) {
c . versionNegotiated . Set ( true )
2018-11-20 17:51:25 -05:00
}
c . session . handlePacket ( p )
}
2018-11-27 09:29:03 -05:00
func ( c * client ) handleVersionNegotiationPacket ( hdr * wire . Header ) {
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
2018-11-20 17:51:25 -05:00
// ignore delayed / duplicated version negotiation packets
2018-11-27 09:29:03 -05:00
if c . receivedVersionNegotiationPacket || c . versionNegotiated . Get ( ) {
c . logger . Debugf ( "Received a delayed Version Negotiation packet." )
return
2018-11-20 17:51:25 -05:00
}
for _ , v := range hdr . SupportedVersions {
if v == c . version {
2018-11-27 09:29:03 -05:00
// The Version Negotiation packet contains the version that we offered.
// This might be a packet sent by an attacker (or by a terribly broken server implementation).
return
2018-11-20 17:51:25 -05:00
}
}
2018-11-27 09:29:03 -05:00
c . logger . Infof ( "Received a Version Negotiation packet. Supported Versions: %s" , hdr . SupportedVersions )
2018-11-20 17:51:25 -05:00
newVersion , ok := protocol . ChooseSupportedVersion ( c . config . Versions , hdr . SupportedVersions )
if ! ok {
2018-11-27 09:29:03 -05:00
c . session . destroy ( qerr . InvalidVersion )
c . logger . Debugf ( "No compatible version found." )
return
2018-11-20 17:51:25 -05:00
}
c . receivedVersionNegotiationPacket = true
c . negotiatedVersions = hdr . SupportedVersions
// switch to negotiated version
c . initialVersion = c . version
c . version = newVersion
c . logger . Infof ( "Switching to QUIC version %s. New connection ID: %s" , newVersion , c . destConnID )
2019-01-02 07:01:06 -05:00
c . initialPacketNumber = c . session . closeForRecreating ( )
2018-11-20 17:51:25 -05:00
}
func ( c * client ) handleRetryPacket ( hdr * wire . Header ) {
2018-11-27 09:29:03 -05:00
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
2018-11-20 17:51:25 -05:00
c . logger . Debugf ( "<- Received Retry" )
2018-11-27 09:29:03 -05:00
( & wire . ExtendedHeader { Header : * hdr } ) . Log ( c . logger )
2018-11-23 11:04:53 -05:00
if ! hdr . OrigDestConnectionID . Equal ( c . destConnID ) {
c . logger . Debugf ( "Ignoring spoofed Retry. Original Destination Connection ID: %s, expected: %s" , hdr . OrigDestConnectionID , c . destConnID )
2018-11-20 17:51:25 -05:00
return
}
2018-11-23 11:04:53 -05:00
if hdr . SrcConnectionID . Equal ( c . destConnID ) {
c . logger . Debugf ( "Ignoring Retry, since the server didn't change the Source Connection ID." )
2018-11-20 17:51:25 -05:00
return
}
2018-11-23 11:04:53 -05:00
// If a token is already set, this means that we already received a Retry from the server.
// Ignore this Retry packet.
if len ( c . token ) > 0 {
c . logger . Debugf ( "Ignoring Retry, since a Retry was already received." )
2018-11-20 17:51:25 -05:00
return
}
2018-11-23 11:04:53 -05:00
c . origDestConnID = c . destConnID
2018-11-20 17:51:25 -05:00
c . destConnID = hdr . SrcConnectionID
c . token = hdr . Token
2019-01-02 07:01:06 -05:00
c . initialPacketNumber = c . session . closeForRecreating ( )
2018-11-20 17:51:25 -05:00
}
2018-11-23 11:04:53 -05:00
func ( c * client ) createNewTLSSession ( version protocol . VersionNumber ) error {
params := & handshake . TransportParameters {
InitialMaxStreamDataBidiRemote : protocol . InitialMaxStreamData ,
InitialMaxStreamDataBidiLocal : protocol . InitialMaxStreamData ,
InitialMaxStreamDataUni : protocol . InitialMaxStreamData ,
InitialMaxData : protocol . InitialMaxData ,
IdleTimeout : c . config . IdleTimeout ,
MaxBidiStreams : uint64 ( c . config . MaxIncomingStreams ) ,
MaxUniStreams : uint64 ( c . config . MaxIncomingUniStreams ) ,
DisableMigration : true ,
2018-11-20 17:51:25 -05:00
}
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
runner := & runner {
onHandshakeCompleteImpl : func ( _ Session ) { close ( c . handshakeChan ) } ,
2018-11-23 11:04:53 -05:00
retireConnectionIDImpl : c . packetHandlers . Retire ,
removeConnectionIDImpl : c . packetHandlers . Remove ,
2018-11-20 17:51:25 -05:00
}
2018-11-23 11:04:53 -05:00
sess , err := newClientSession (
2018-11-20 17:51:25 -05:00
c . conn ,
runner ,
c . token ,
2018-11-23 11:04:53 -05:00
c . origDestConnID ,
2018-11-20 17:51:25 -05:00
c . destConnID ,
c . srcConnID ,
c . config ,
2018-11-23 11:04:53 -05:00
c . tlsConf ,
2019-01-02 07:01:06 -05:00
c . initialPacketNumber ,
2018-11-23 11:04:53 -05:00
params ,
c . initialVersion ,
2018-11-20 17:51:25 -05:00
c . logger ,
c . version ,
)
if err != nil {
return err
}
c . session = sess
c . packetHandlers . Add ( c . srcConnID , c )
return nil
}
func ( c * client ) Close ( ) error {
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
if c . session == nil {
return nil
}
return c . session . Close ( )
}
func ( c * client ) destroy ( e error ) {
c . mutex . Lock ( )
defer c . mutex . Unlock ( )
if c . session == nil {
return
}
c . session . destroy ( e )
}
func ( c * client ) GetVersion ( ) protocol . VersionNumber {
c . mutex . Lock ( )
v := c . version
c . mutex . Unlock ( )
return v
}
func ( c * client ) GetPerspective ( ) protocol . Perspective {
return protocol . PerspectiveClient
}