go-amqp/conn.go

743 строки
19 KiB
Go
Исходник Обычный вид История

2017-04-01 23:00:36 +03:00
package amqp
import (
"bytes"
2017-04-23 04:32:50 +03:00
"crypto/tls"
"io"
2017-04-30 02:38:15 +03:00
"math"
2017-04-01 23:00:36 +03:00
"net"
2017-04-27 06:35:29 +03:00
"sync"
"sync/atomic"
2017-04-17 06:39:31 +03:00
"time"
2017-04-01 23:00:36 +03:00
)
2017-05-07 04:24:06 +03:00
// Default connection options
2017-04-01 23:00:36 +03:00
const (
2017-05-07 02:57:27 +03:00
DefaultMaxFrameSize = 512
DefaultIdleTimeout = 1 * time.Minute
defaultChannelMax = 1
2017-04-30 02:38:15 +03:00
)
// Errors
var (
ErrTimeout = errorNew("timeout waiting for response")
2017-04-01 23:00:36 +03:00
)
2017-04-30 02:38:15 +03:00
// ConnOption is an function for configuring an AMQP connection.
type ConnOption func(*conn) error
2017-04-01 23:00:36 +03:00
// ConnServerHostname sets the hostname sent in the AMQP
2017-04-30 02:38:15 +03:00
// Open frame and TLS ServerName (if not otherwise set).
//
// This is useful when the AMQP connection will be established
// via a pre-established TLS connection as the server may not
// know which hostname the client is attempting to connect to.
func ConnServerHostname(hostname string) ConnOption {
return func(c *conn) error {
c.hostname = hostname
return nil
}
}
2017-04-30 02:38:15 +03:00
// ConnTLS toggles TLS negotiation.
//
// Default: false.
2017-04-30 02:38:15 +03:00
func ConnTLS(enable bool) ConnOption {
return func(c *conn) error {
c.tlsNegotiation = enable
return nil
}
}
2017-04-30 02:38:15 +03:00
// ConnTLSConfig sets the tls.Config to be used during
// TLS negotiation.
//
// This option is for advanced usage, in most scenarios
// providing a URL scheme of "amqps://" or ConnTLS(true)
// is sufficient.
func ConnTLSConfig(tc *tls.Config) ConnOption {
return func(c *conn) error {
c.tlsConfig = tc
c.tlsNegotiation = true
2017-04-23 04:32:50 +03:00
return nil
}
}
2017-04-30 02:38:15 +03:00
// ConnIdleTimeout specifies the maximum period between receiving
// frames from the peer.
//
// Resolution is milliseconds. A value of zero indicates no timeout.
// This setting is in addition to TCP keepalives.
//
// Default: 1 minute.
2017-04-30 02:38:15 +03:00
func ConnIdleTimeout(d time.Duration) ConnOption {
return func(c *conn) error {
2017-04-30 02:38:15 +03:00
if d < 0 {
return errorNew("idle timeout cannot be negative")
}
2017-04-27 06:35:29 +03:00
c.idleTimeout = d
return nil
}
}
2017-04-30 02:38:15 +03:00
// ConnMaxFrameSize sets the maximum frame size that
// the connection will accept.
2017-04-30 02:38:15 +03:00
//
// Must be 512 or greater.
//
// Default: 512.
2017-04-30 02:38:15 +03:00
func ConnMaxFrameSize(n uint32) ConnOption {
return func(c *conn) error {
2017-04-30 02:38:15 +03:00
if n < 512 {
return errorNew("max frame size must be 512 or greater")
}
2017-04-27 06:35:29 +03:00
c.maxFrameSize = n
return nil
}
}
// ConnConnectTimeout configures how long to wait for the
// server during connection establishment.
//
// Once the connection has been established, ConnIdleTimeout
// applies. If duration is zero, no timeout will be applied.
//
// Default: 0.
func ConnConnectTimeout(d time.Duration) ConnOption {
return func(c *conn) error { c.connectTimeout = d; return nil }
}
2017-04-01 23:00:36 +03:00
// conn is an AMQP connection.
type conn struct {
net net.Conn // underlying connection
connectTimeout time.Duration // time to wait for reads/writes during conn establishment
pauseRead int32 // atomically set to indicate connReader should pause reading from network
resumeRead chan struct{} // connReader reads from channel while paused, until channel is closed
// TLS
2017-04-30 02:38:15 +03:00
tlsNegotiation bool // negotiate TLS
tlsComplete bool // TLS negotiation complete
tlsConfig *tls.Config // TLS config, default used if nil (ServerName set to Client.hostname)
2017-04-01 23:00:36 +03:00
2017-04-30 02:38:15 +03:00
// SASL
saslHandlers map[symbol]stateFunc // map of supported handlers keyed by SASL mechanism, SASL not negotiated if nil
2017-04-30 02:38:15 +03:00
saslComplete bool // SASL negotiation complete
2017-04-01 23:00:36 +03:00
2017-04-30 02:38:15 +03:00
// local settings
maxFrameSize uint32 // max frame size we accept
channelMax uint16 // maximum number of channels we'll create
hostname string // hostname of remote server (set explicitly or parsed from URL)
idleTimeout time.Duration // maximum period between receiving frames
2017-04-27 06:35:29 +03:00
2017-04-30 02:38:15 +03:00
// peer settings
peerIdleTimeout time.Duration // maximum period between sending frames
peerMaxFrameSize uint32 // maximum frame size peer will accept
2017-04-01 23:00:36 +03:00
2017-04-30 02:38:15 +03:00
// conn state
errMu sync.Mutex // mux holds errMu from start until shutdown completes; operations are sequential before mux is started
err error // error to be returned to client
doneOnce sync.Once // only close done once
done chan struct{} // indicates the connection is done
2017-04-01 23:00:36 +03:00
// mux
newSession chan *Session // new Sessions are requested from mux by reading off this channel
delSession chan *Session // session completion is indicated to mux by sending the Session on this channel
connErr chan error // connReader/Writer notifications of an error
// connReader
rxProto chan protoHeader // protoHeaders received by connReader
rxFrame chan frame // AMQP frames received by connReader
// connWriter
txFrame chan frame // AMQP frames to be sent by connWriter
txBuf bytes.Buffer // buffer for marshaling frames before transmitting
}
func newConn(netConn net.Conn, opts ...ConnOption) (*conn, error) {
c := &conn{
net: netConn,
2017-05-07 02:57:27 +03:00
maxFrameSize: DefaultMaxFrameSize,
peerMaxFrameSize: DefaultMaxFrameSize,
channelMax: defaultChannelMax,
2017-05-07 02:57:27 +03:00
idleTimeout: DefaultIdleTimeout,
done: make(chan struct{}),
connErr: make(chan error, 2), // buffered to ensure connReader/Writer won't leak
rxProto: make(chan protoHeader),
rxFrame: make(chan frame),
newSession: make(chan *Session),
delSession: make(chan *Session),
txFrame: make(chan frame),
}
// apply options
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
// start reader
go c.connReader()
// run connection establishment state machine
for state := c.negotiateProto; state != nil; {
state = state()
}
// check if err occurred
if c.err != nil {
c.close()
return nil, c.err
}
// start multiplexor and writer
go c.mux()
go c.connWriter()
return c, nil
2017-04-01 23:00:36 +03:00
}
func (c *conn) close() error {
2017-04-01 23:00:36 +03:00
// TODO: shutdown AMQP
c.closeDone() // notify goroutines and blocked functions to exit
2017-04-27 06:35:29 +03:00
// Client.mux holds err lock until shutdown, we block until
// shutdown completes and we can return the error (if any)
2017-04-27 06:35:29 +03:00
c.errMu.Lock()
defer c.errMu.Unlock()
2017-04-24 06:24:12 +03:00
err := c.net.Close()
if c.err == nil {
c.err = err
}
return c.err
2017-04-01 23:00:36 +03:00
}
// closeDone closes Client.done if it has not already been closed
func (c *conn) closeDone() {
c.doneOnce.Do(func() { close(c.done) })
2017-04-01 23:00:36 +03:00
}
// mux is start in it's own goroutine after initial connection establishment.
// It handles muxing of sessions, keepalives, and connection errors.
func (c *conn) mux() {
// create the next session to allocate
nextSession := newSession(c, 0)
2017-04-01 23:00:36 +03:00
// map channel to sessions
2017-04-01 23:00:36 +03:00
sessions := make(map[uint16]*Session)
// we hold the errMu lock until error or done
2017-04-27 06:35:29 +03:00
c.errMu.Lock()
defer c.errMu.Unlock()
2017-04-01 23:00:36 +03:00
for {
// check if last loop returned an error
2017-04-01 23:00:36 +03:00
if c.err != nil {
2017-04-27 06:35:29 +03:00
c.closeDone()
2017-04-24 06:24:12 +03:00
return
2017-04-01 23:00:36 +03:00
}
select {
// error from connReader
case c.err = <-c.connErr:
2017-04-01 23:00:36 +03:00
// new frame from connReader
case fr := <-c.rxFrame:
// lookup session and send to Session.mux
ch, ok := sessions[fr.channel]
if !ok {
2017-04-30 02:38:15 +03:00
c.err = errorErrorf("unexpected frame: %#v", fr.body)
continue
2017-04-01 23:00:36 +03:00
}
ch.rx <- fr
2017-04-01 23:00:36 +03:00
// new session request
//
// Continually try to send the next session on the channel,
// then add it to the sessions map. This allows us to control ID
// allocation and prevents the need to have shared map. Since new
// sessions are far less frequent than frames being sent to sessions,
// we can avoid the lock/unlock for session lookup.
case c.newSession <- nextSession:
2017-04-01 23:00:36 +03:00
sessions[nextSession.channel] = nextSession
// create the next session to send
nextSession = newSession(c, nextSession.channel+1) // TODO: enforce max session/wrapping
// session deletion
2017-04-01 23:00:36 +03:00
case s := <-c.delSession:
delete(sessions, s.channel)
// connection is complete
2017-04-27 06:35:29 +03:00
case <-c.done:
return
2017-04-01 23:00:36 +03:00
}
}
}
2017-05-07 02:57:27 +03:00
// frameReader returns io.EOF on each read, this allows
// ReadFrom to work with a net.conn without blocking until
// the connection is closed
type frameReader struct {
r io.Reader // underlying reader
}
func (f *frameReader) Read(p []byte) (int, error) {
n, err := f.r.Read(p)
if err != nil {
return n, err
}
return n, io.EOF
}
2017-04-30 02:38:15 +03:00
// connReader reads from the net.Conn, decodes frames, and passes them
// up via the conn.rxFrame and conn.rxProto channels.
func (c *conn) connReader() {
buf := new(bytes.Buffer)
2017-04-24 06:24:12 +03:00
2017-04-30 02:38:15 +03:00
var (
negotiating = true // true during conn establishment, we should check for protoHeaders
currentHeader frameHeader // keep track of the current header, for frames split across multiple TCP packets
frameInProgress bool // true if we're in the middle of receiving data for currentHeader
2017-04-30 02:38:15 +03:00
)
// frameReader facilitates reading directly into buf
fr := &frameReader{r: c.net}
2017-04-27 06:35:29 +03:00
for {
// we need to read more if buf doesn't contain the complete frame
// or there's not enough in buf to parse the header
if frameInProgress || buf.Len() < frameHeaderSize {
2017-04-30 18:56:16 +03:00
c.net.SetReadDeadline(time.Now().Add(c.idleTimeout))
_, err := buf.ReadFrom(fr)
2017-04-27 06:35:29 +03:00
if err != nil {
2017-04-30 05:33:03 +03:00
if atomic.LoadInt32(&c.pauseRead) == 1 {
// need to stop reading during TLS negotiation,
// see conn.startTLS()
2017-04-30 05:33:03 +03:00
c.pauseRead = 0
for range c.resumeRead {
// reads indicate paused, resume on close
}
fr.r = c.net // conn wrapped with TLS
2017-04-30 05:33:03 +03:00
continue
}
c.connErr <- err
2017-04-27 06:35:29 +03:00
return
2017-04-24 06:24:12 +03:00
}
2017-04-27 06:35:29 +03:00
}
// read more if we didn't get enough to parse header
if buf.Len() < frameHeaderSize {
2017-04-27 06:35:29 +03:00
continue
}
// during negotiation, check for proto frames
2017-04-27 06:35:29 +03:00
if negotiating && bytes.Equal(buf.Bytes()[:4], []byte{'A', 'M', 'Q', 'P'}) {
2017-04-30 02:38:15 +03:00
p, err := parseProtoHeader(buf)
if err != nil {
c.connErr <- err
2017-04-24 06:24:12 +03:00
return
}
// we know negotiation is complete once an AMQP proto frame
// is received
2017-04-27 06:35:29 +03:00
if p.ProtoID == protoAMQP {
negotiating = false
2017-04-24 06:24:12 +03:00
}
// send proto header
2017-04-24 06:24:12 +03:00
select {
case <-c.done:
return
2017-04-27 06:35:29 +03:00
case c.rxProto <- p:
}
2017-04-30 02:38:15 +03:00
2017-04-27 06:35:29 +03:00
continue
}
// parse the header if we're not completeing an already
// parsed frame
2017-04-27 06:35:29 +03:00
if !frameInProgress {
var err error
2017-04-27 06:35:29 +03:00
currentHeader, err = parseFrameHeader(buf)
if err != nil {
c.connErr <- err
2017-04-27 06:35:29 +03:00
return
}
frameInProgress = true
}
// check size is reasonable
2017-04-30 02:38:15 +03:00
if currentHeader.Size > math.MaxInt32 { // make max size configurable
c.connErr <- errorNew("payload too large")
2017-04-30 02:38:15 +03:00
return
}
bodySize := int(currentHeader.Size - frameHeaderSize)
2017-04-30 02:38:15 +03:00
// check if we have the full frame
2017-04-30 02:38:15 +03:00
if buf.Len() < bodySize {
2017-04-27 06:35:29 +03:00
continue
}
frameInProgress = false
// check if body is empty (keepalive)
2017-04-30 02:38:15 +03:00
if bodySize == 0 {
continue
2017-04-30 02:38:15 +03:00
}
// parse the frame
2017-04-30 18:56:16 +03:00
payload := bytes.NewBuffer(buf.Next(bodySize))
parsedBody, err := parseFrameBody(payload)
2017-04-27 06:35:29 +03:00
if err != nil {
c.connErr <- err
2017-04-27 06:35:29 +03:00
return
}
// send to mux
2017-04-27 06:35:29 +03:00
select {
case <-c.done:
return
2017-04-30 18:56:16 +03:00
case c.rxFrame <- frame{channel: currentHeader.Channel, body: parsedBody}:
2017-04-27 06:35:29 +03:00
}
2017-04-01 23:00:36 +03:00
}
}
func (c *conn) connWriter() {
var (
// keepalives are sent at a rate of 1/2 idle timeout
keepaliveInterval = c.peerIdleTimeout / 2
// 0 disables keepalives
keepalivesEnabled = keepaliveInterval > 0
// set if enable, nil if not; nil channels block forever
keepalive <-chan time.Time
)
if keepalivesEnabled {
ticker := time.NewTicker(keepaliveInterval)
defer ticker.Stop()
keepalive = ticker.C
}
var err error
for {
if err != nil {
c.connErr <- err
return
}
select {
// frame write request
case fr := <-c.txFrame:
err = c.writeFrame(fr)
// keepalive timer
case <-keepalive:
_, err = c.net.Write(keepaliveFrame)
// It would be slightly more efficient in terms of network
// resources to reset the timer each time a frame is sent.
// However, keepalives are small (8 bytes) and the interval
// is usually on the order of minutes. It does not seem
// worth it to add extra operations in the write path to
// avoid. (To properly reset a timer it needs to be stopped,
// possibly drained, then reset.)
// connection complete
case <-c.done:
return
}
}
}
// writeFrame writes a frame to the network, may only be used
// by connWriter after initial negotiation.
func (c *conn) writeFrame(fr frame) error {
if c.connectTimeout != 0 {
c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
2017-05-07 02:57:27 +03:00
// writeFrame into txBuf
c.txBuf.Reset()
err := writeFrame(&c.txBuf, fr)
if err != nil {
return err
}
2017-05-07 02:57:27 +03:00
// validate we're not exceeding peer's max frame size
if uint64(c.txBuf.Len()) > uint64(c.peerMaxFrameSize) {
return errorErrorf("frame larger than peer ")
}
2017-05-07 02:57:27 +03:00
// write to network
_, err = c.net.Write(c.txBuf.Bytes())
return err
}
2017-05-07 02:57:27 +03:00
// writeProtoHeader writes an AMQP protocol header to the
// network
func (c *conn) writeProtoHeader(pID protoID) error {
if c.connectTimeout != 0 {
c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
_, err := c.net.Write([]byte{'A', 'M', 'Q', 'P', byte(pID), 1, 0, 0})
return err
}
// keepaliveFrame is an AMQP frame with no body, used for keepalives
var keepaliveFrame = []byte{0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00}
2017-05-07 02:57:27 +03:00
// wantWriteFrame is used by sessions and links to send frame to
// connWriter.
func (c *conn) wantWriteFrame(fr frame) {
select {
case c.txFrame <- fr:
case <-c.done:
}
}
// stateFunc is a state in a state machine.
//
// The state is advanced by returning the next state.
// The state machine concludes when nil is returned.
type stateFunc func() stateFunc
2017-04-30 02:38:15 +03:00
// negotiateProto determines which proto to negotiate next
func (c *conn) negotiateProto() stateFunc {
// in the order each must be negotiated
2017-04-01 23:00:36 +03:00
switch {
case c.tlsNegotiation && !c.tlsComplete:
2017-04-23 21:01:44 +03:00
return c.exchangeProtoHeader(protoTLS)
2017-04-01 23:00:36 +03:00
case c.saslHandlers != nil && !c.saslComplete:
2017-04-23 21:01:44 +03:00
return c.exchangeProtoHeader(protoSASL)
2017-04-01 23:00:36 +03:00
default:
2017-04-23 21:01:44 +03:00
return c.exchangeProtoHeader(protoAMQP)
2017-04-01 23:00:36 +03:00
}
}
type protoID uint8
// protocol IDs received in protoHeaders
2017-04-01 23:00:36 +03:00
const (
protoAMQP protoID = 0x0
protoTLS protoID = 0x2
protoSASL protoID = 0x3
2017-04-01 23:00:36 +03:00
)
2017-04-30 02:38:15 +03:00
// exchangeProtoHeader performs the round trip exchange of protocol
// headers, validation, and returns the protoID specific next state.
func (c *conn) exchangeProtoHeader(pID protoID) stateFunc {
// write the proto header
c.err = c.writeProtoHeader(pID)
if c.err != nil {
return nil
}
2017-04-01 23:00:36 +03:00
// read response header
p, err := c.readProtoHeader()
if err != nil {
c.err = err
2017-04-01 23:00:36 +03:00
return nil
}
if pID != p.ProtoID {
c.err = errorErrorf("unexpected protocol header %#00x, expected %#00x", p.ProtoID, pID)
2017-04-01 23:00:36 +03:00
return nil
}
// go to the proto specific state
switch pID {
2017-04-23 21:01:44 +03:00
case protoAMQP:
2017-04-30 02:38:15 +03:00
return c.openAMQP
2017-04-23 21:01:44 +03:00
case protoTLS:
2017-04-30 02:38:15 +03:00
return c.startTLS
2017-04-23 21:01:44 +03:00
case protoSASL:
2017-04-30 02:38:15 +03:00
return c.negotiateSASL
2017-04-01 23:00:36 +03:00
default:
2017-04-30 02:38:15 +03:00
c.err = errorErrorf("unknown protocol ID %#02x", p.ProtoID)
2017-04-01 23:00:36 +03:00
return nil
}
}
2017-05-07 02:57:27 +03:00
// readProtoHeader reads a protocol header packet from c.rxProto.
func (c *conn) readProtoHeader() (protoHeader, error) {
var deadline <-chan time.Time
if c.connectTimeout != 0 {
deadline = time.After(c.connectTimeout)
}
var p protoHeader
select {
case p = <-c.rxProto:
return p, nil
case err := <-c.connErr:
return p, err
case fr := <-c.rxFrame:
return p, errorErrorf("unexpected frame %#v", fr)
case <-deadline:
return p, ErrTimeout
}
}
// startTLS wraps the conn with TLS and returns to Client.negotiateProto
func (c *conn) startTLS() stateFunc {
// create a new config if not already set
if c.tlsConfig == nil {
2017-04-24 00:58:59 +03:00
c.tlsConfig = new(tls.Config)
}
// TLS config must have ServerName or InsecureSkipVerify set
if c.tlsConfig.ServerName == "" && !c.tlsConfig.InsecureSkipVerify {
c.tlsConfig.ServerName = c.hostname
}
2017-04-30 05:33:03 +03:00
// convoluted method to pause connReader, explorer simpler alternatives
c.resumeRead = make(chan struct{}) // 1. create channel
atomic.StoreInt32(&c.pauseRead, 1) // 2. indicate should pause
c.net.SetReadDeadline(time.Time{}.Add(1)) // 3. set deadline to interrupt connReader
c.resumeRead <- struct{}{} // 4. wait for connReader to read from chan, indicating paused
defer close(c.resumeRead) // 5. defer connReader resume by closing channel
c.net.SetReadDeadline(time.Time{}) // 6. clear deadline
// wrap existing net.Conn and perform TLS handshake
tlsConn := tls.Client(c.net, c.tlsConfig)
if c.connectTimeout != 0 {
tlsConn.SetWriteDeadline(time.Now().Add(c.connectTimeout))
}
c.err = tlsConn.Handshake()
2017-04-30 05:33:03 +03:00
if c.err != nil {
return nil
}
// swap net.Conn
c.net = tlsConn
c.tlsComplete = true
// go to next protocol
2017-04-23 04:32:50 +03:00
return c.negotiateProto
}
2017-04-30 02:38:15 +03:00
// openAMQP round trips the AMQP open performative
func (c *conn) openAMQP() stateFunc {
// send open frame
c.err = c.writeFrame(frame{
2017-04-30 02:38:15 +03:00
typ: frameTypeAMQP,
body: &performOpen{
ContainerID: randString(),
Hostname: c.hostname,
MaxFrameSize: c.maxFrameSize,
ChannelMax: c.channelMax,
2017-04-30 02:38:15 +03:00
IdleTimeout: c.idleTimeout,
},
channel: 0,
})
if c.err != nil {
return nil
}
2017-04-01 23:00:36 +03:00
// get the response
2017-04-24 06:24:12 +03:00
fr, err := c.readFrame()
2017-04-01 23:00:36 +03:00
if err != nil {
2017-04-24 06:24:12 +03:00
c.err = err
2017-04-01 23:00:36 +03:00
return nil
}
2017-04-30 02:38:15 +03:00
o, ok := fr.body.(*performOpen)
2017-04-24 06:24:12 +03:00
if !ok {
2017-04-30 02:38:15 +03:00
c.err = errorErrorf("unexpected frame type %T", fr.body)
2017-04-27 06:35:29 +03:00
return nil
2017-04-01 23:00:36 +03:00
}
// update peer settings
2017-04-27 06:35:29 +03:00
if o.MaxFrameSize > 0 {
2017-04-30 05:33:03 +03:00
c.peerMaxFrameSize = o.MaxFrameSize
2017-04-27 06:35:29 +03:00
}
2017-04-23 21:01:44 +03:00
if o.IdleTimeout > 0 {
// TODO: reject very small idle timeouts
2017-04-30 02:38:15 +03:00
c.peerIdleTimeout = o.IdleTimeout
2017-04-17 06:39:31 +03:00
}
2017-04-01 23:00:36 +03:00
if o.ChannelMax < c.channelMax {
c.channelMax = o.ChannelMax
}
// connection established, exit state machine
2017-04-01 23:00:36 +03:00
return nil
}
2017-04-30 02:38:15 +03:00
// negotiateSASL returns the SASL handler for the first matched
// mechanism specified by the server
func (c *conn) negotiateSASL() stateFunc {
// read mechanisms frame
2017-04-24 06:24:12 +03:00
fr, err := c.readFrame()
2017-04-01 23:00:36 +03:00
if err != nil {
c.err = err
return nil
}
2017-04-30 02:38:15 +03:00
sm, ok := fr.body.(*saslMechanisms)
2017-04-24 06:24:12 +03:00
if !ok {
2017-04-30 02:38:15 +03:00
c.err = errorErrorf("unexpected frame type %T", fr.body)
2017-04-01 23:00:36 +03:00
return nil
}
// return first match in c.saslHandlers based on order received
2017-04-01 23:00:36 +03:00
for _, mech := range sm.Mechanisms {
if state, ok := c.saslHandlers[mech]; ok {
return state
}
}
// no match
c.err = errorErrorf("no supported auth mechanism (%v)", sm.Mechanisms) // TODO: send "auth not supported" frame?
2017-04-01 23:00:36 +03:00
return nil
}
// saslOutcome processes the SASL outcome frame and return Client.negotiateProto
2017-04-30 02:38:15 +03:00
// on success.
//
// SASL handlers return this stateFunc when the mechanism specific negotiation
// has completed.
func (c *conn) saslOutcome() stateFunc {
// read outcome frame
2017-04-24 06:24:12 +03:00
fr, err := c.readFrame()
2017-04-01 23:00:36 +03:00
if err != nil {
c.err = err
return nil
}
2017-04-30 02:38:15 +03:00
so, ok := fr.body.(*saslOutcome)
2017-04-24 06:24:12 +03:00
if !ok {
2017-04-30 02:38:15 +03:00
c.err = errorErrorf("unexpected frame type %T", fr.body)
2017-04-27 06:35:29 +03:00
return nil
2017-04-01 23:00:36 +03:00
}
// check if auth succeeded
2017-04-23 21:01:44 +03:00
if so.Code != codeSASLOK {
c.err = errorErrorf("SASL PLAIN auth failed with code %#00x: %s", so.Code, so.AdditionalData) // implement Stringer for so.Code
2017-04-01 23:00:36 +03:00
return nil
}
// return to c.negotiateProto
2017-04-01 23:00:36 +03:00
c.saslComplete = true
return c.negotiateProto
}
2017-04-24 06:24:12 +03:00
2017-04-30 02:38:15 +03:00
// readFrame is used during connection establishment to read a single frame.
//
2017-05-07 02:57:27 +03:00
// After setup, conn.mux handles incoming frames.
func (c *conn) readFrame() (frame, error) {
var deadline <-chan time.Time
if c.connectTimeout != 0 {
deadline = time.After(c.connectTimeout)
}
2017-04-24 06:24:12 +03:00
var fr frame
select {
case fr = <-c.rxFrame:
return fr, nil
case err := <-c.connErr:
2017-04-24 06:24:12 +03:00
return fr, err
case p := <-c.rxProto:
2017-04-30 02:38:15 +03:00
return fr, errorErrorf("unexpected protocol header %#v", p)
case <-deadline:
return fr, ErrTimeout // TODO: move to connReader
2017-04-24 06:24:12 +03:00
}
}