Skip to content

Commit

Permalink
Define type and accessor for Role
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthomson committed Mar 5, 2018
1 parent e535d6b commit 0fcd2c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
72 changes: 30 additions & 42 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import (
type Role uint8

const (
RoleClient = Role(iota)
RoleServer = Role(iota)
RoleClient = Role(1)
RoleServer = Role(2)
)

// The state of a QUIC connection.
type State uint8

const (
StateInit = State(iota)
StateWaitClientInitial = State(iota)
StateWaitServerFirstFlight = State(iota)
StateWaitClientSecondFlight = State(iota)
StateEstablished = State(iota)
StateClosing = State(iota)
StateClosed = State(iota)
StateError = State(iota)
StateInit = State(1)
StateWaitClientInitial = State(2)
StateWaitServerFirstFlight = State(3)
StateWaitClientSecondFlight = State(4)
StateEstablished = State(5)
StateClosing = State(6)
StateClosed = State(7)
StateError = State(8)
)

const (
Expand Down Expand Up @@ -100,7 +100,7 @@ calls to notify it of various events.
*/
type Connection struct {
handler ConnectionHandler
Role Role
role Role
state State
version VersionNumber
clientConnId ConnectionId
Expand Down Expand Up @@ -216,38 +216,26 @@ func (c *Connection) start() error {
}

func (c *Connection) label() string {
if c.Role == RoleClient {
return "client"
}
return "server"
return string(c.role)
}

func (c *Connection) Role() Role {
return c.role
}

func (c *Connection) setState(state State) {
if c.state == state {
return
}

c.log(logTypeConnection, "%s: Connection state %v -> %v", c.label(), c.state, state)
c.log(logTypeConnection, "%s: Connection state %s -> %v", c.label(), StateName(c.state), StateName(state))
if c.handler != nil {
c.handler.StateChanged(state)
}
c.state = state
}

// String converts a Role into something usable for debugging.
func (role Role) String() string {
switch role {
case RoleClient:
return "client"
case RoleServer:
return "server"
default:
panic("unknown minq.Role")
}
}

// String converts a State into something usable for debugging.
func (state State) String() string {
func StateName(state State) string {
// TODO(ekr@rtfm.com): is there a way to get the name from the
// const value.
switch state {
Expand All @@ -268,12 +256,12 @@ func (state State) String() string {
case StateError:
return "StateError"
default:
panic("unknown minq.State")
return "Unknown state"
}
}

func (c *Connection) myStream(id uint64) bool {
return id == 0 || (((id & 1) == 1) == (c.Role == RoleServer))
return id == 0 || (((id & 1) == 1) == (c.role == RoleServer))
}

func (c *Connection) sameTypeStream(id1 uint64, id2 uint64) bool {
Expand Down Expand Up @@ -420,7 +408,7 @@ func (c *Connection) determineAead(pt uint8) cipher.AEAD {
aead = c.writeProtected.aead
}

if c.Role == RoleClient {
if c.role == RoleClient {
switch {
case pt == packetTypeInitial:
aead = c.writeClear.aead
Expand Down Expand Up @@ -512,7 +500,7 @@ func (c *Connection) sendPacket(pt uint8, tosend []frame, containsOnlyAcks bool)
}

connId := c.serverConnId
if c.Role == RoleClient {
if c.role == RoleClient {
if pt == packetTypeInitial {
connId = c.clientConnId
}
Expand Down Expand Up @@ -541,7 +529,7 @@ func (c *Connection) sendFramesInPacket(pt uint8, tosend []frame) error {
connId = c.serverConnId

longHeader := true
if c.Role == RoleClient {
if c.role == RoleClient {
switch {
case pt == packetTypeInitial:
aead = c.writeClear.aead
Expand Down Expand Up @@ -938,7 +926,7 @@ func (c *Connection) input(p []byte) error {
assert(int(hdrlen) <= len(p))

if isLongHeader(&hdr) && hdr.Version != c.version {
if c.Role == RoleServer {
if c.role == RoleServer {
c.log(logTypeConnection, "%s: Received unsupported version %v, expected %v", c.label(), hdr.Version, c.version)
err = c.sendVersionNegotiation(hdr.ConnectionID, hdr.PacketNumber, hdr.Version)
if err != nil {
Expand Down Expand Up @@ -1196,7 +1184,7 @@ func (c *Connection) processCleartext(hdr *packetHeader, payload []byte, naf *bo
}

// This is fresh data so sanity check.
if c.Role == RoleClient {
if c.role == RoleClient {
if c.state != StateWaitServerFirstFlight {
// TODO(ekr@rtfm.com): Not clear what to do here. It's
// clearly a protocol error, but also allows on-path
Expand Down Expand Up @@ -1246,7 +1234,7 @@ func (c *Connection) processCleartext(hdr *packetHeader, payload []byte, naf *bo
if err != nil {
return err
}
if c.Role == RoleClient {
if c.role == RoleClient {
// We did this on the server already.
c.setTransportParameters()
}
Expand Down Expand Up @@ -1685,7 +1673,7 @@ func (c *Connection) CheckTimer() (int, error) {
// Right now just re-send everything we might need to send.

// Special case the client's first message.
if c.Role == RoleClient && (c.state == StateInit ||
if c.role == RoleClient && (c.state == StateInit ||
c.state == StateWaitServerFirstFlight) {
err := c.sendClientInitial()
return 1, err
Expand All @@ -1710,7 +1698,7 @@ func (c *Connection) setupAeadMasking() (err error) {
}

var sendLabel, recvLabel string
if c.Role == RoleClient {
if c.role == RoleClient {
sendLabel = clientCtSecretLabel
recvLabel = serverCtSecretLabel
} else {
Expand All @@ -1733,7 +1721,7 @@ func (c *Connection) setupAeadMasking() (err error) {
// Called when the handshake is complete.
func (c *Connection) handshakeComplete() (err error) {
var sendLabel, recvLabel string
if c.Role == RoleClient {
if c.role == RoleClient {
sendLabel = clientPpSecretLabel
recvLabel = serverPpSecretLabel
} else {
Expand Down Expand Up @@ -1763,7 +1751,7 @@ func (c *Connection) packetNonce(pn uint64) []byte {
func (c *Connection) CreateStream() *Stream {
// First see if there is a stream that we haven't
// created with this suffix.
suff := c.streamSuffix(c.Role, false)
suff := c.streamSuffix(c.role, false)
var i uint64
for i = 0; i <= c.maxStream; i++ {
if (i & 0x3) != suff {
Expand Down
2 changes: 1 addition & 1 deletion tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"crypto/x509"
"encoding/hex"
"fmt"
"log"

"github.com/bifurcation/mint"
"log"
)

type TlsConfig struct {
Expand Down

0 comments on commit 0fcd2c9

Please sign in to comment.