2017-04-01 23:00:36 +03:00
|
|
|
package amqp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-23 04:32:50 +03:00
|
|
|
"crypto/tls"
|
2021-09-08 20:27:48 +03:00
|
|
|
"encoding/binary"
|
2018-02-14 20:00:12 +03:00
|
|
|
"errors"
|
2021-09-02 23:42:44 +03:00
|
|
|
"fmt"
|
2017-05-01 08:02:53 +03:00
|
|
|
"io"
|
2017-04-30 02:38:15 +03:00
|
|
|
"math"
|
2017-04-01 23:00:36 +03:00
|
|
|
"net"
|
2021-09-20 20:55:31 +03:00
|
|
|
"net/url"
|
2017-04-27 06:35:29 +03:00
|
|
|
"sync"
|
2017-04-17 06:39:31 +03:00
|
|
|
"time"
|
2021-09-08 05:43:35 +03:00
|
|
|
|
2021-09-16 20:32:22 +03:00
|
|
|
"github.com/Azure/go-amqp/internal/bitmap"
|
2021-09-08 05:43:35 +03:00
|
|
|
"github.com/Azure/go-amqp/internal/buffer"
|
2021-09-09 20:24:27 +03:00
|
|
|
"github.com/Azure/go-amqp/internal/encoding"
|
2021-09-15 21:40:36 +03:00
|
|
|
"github.com/Azure/go-amqp/internal/frames"
|
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
|
|
|
DefaultIdleTimeout = 1 * time.Minute
|
2019-12-13 00:07:15 +03:00
|
|
|
DefaultMaxFrameSize = 65536
|
2018-01-19 06:36:02 +03:00
|
|
|
DefaultMaxSessions = 65536
|
2017-04-30 02:38:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
var (
|
2018-02-14 20:00:12 +03:00
|
|
|
ErrTimeout = errors.New("amqp: timeout waiting for response")
|
|
|
|
|
|
|
|
// ErrConnClosed is propagated to Session and Senders/Receivers
|
2019-01-30 06:02:08 +03:00
|
|
|
// when Client.Close() is called or the server closes the connection
|
|
|
|
// without specifying an error.
|
2018-02-14 20:00:12 +03:00
|
|
|
ErrConnClosed = errors.New("amqp: connection closed")
|
2017-04-01 23:00:36 +03:00
|
|
|
)
|
|
|
|
|
2018-04-14 22:04:11 +03:00
|
|
|
// ConnOption is a function for configuring an AMQP connection.
|
2017-05-04 05:30:30 +03:00
|
|
|
type ConnOption func(*conn) error
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-04 05:30:30 +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
|
2017-05-04 05:30:30 +03:00
|
|
|
// know which hostname the client is attempting to connect to.
|
|
|
|
func ConnServerHostname(hostname string) ConnOption {
|
|
|
|
return func(c *conn) error {
|
2017-04-16 21:37:51 +03:00
|
|
|
c.hostname = hostname
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 02:38:15 +03:00
|
|
|
// ConnTLS toggles TLS negotiation.
|
2017-05-04 05:30:30 +03:00
|
|
|
//
|
|
|
|
// Default: false.
|
2017-04-30 02:38:15 +03:00
|
|
|
func ConnTLS(enable bool) ConnOption {
|
2017-05-04 05:30:30 +03:00
|
|
|
return func(c *conn) error {
|
2017-04-23 19:42:48 +03:00
|
|
|
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 {
|
2017-05-04 05:30:30 +03:00
|
|
|
return func(c *conn) error {
|
2017-04-23 19:42:48 +03:00
|
|
|
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.
|
2017-05-04 05:30:30 +03:00
|
|
|
//
|
|
|
|
// Default: 1 minute.
|
2017-04-30 02:38:15 +03:00
|
|
|
func ConnIdleTimeout(d time.Duration) ConnOption {
|
2017-05-04 05:30:30 +03:00
|
|
|
return func(c *conn) error {
|
2017-04-30 02:38:15 +03:00
|
|
|
if d < 0 {
|
2021-09-02 23:42:44 +03:00
|
|
|
return errors.New("idle timeout cannot be negative")
|
2017-04-30 02:38:15 +03:00
|
|
|
}
|
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
|
2017-05-07 01:26:17 +03:00
|
|
|
// the connection will accept.
|
2017-04-30 02:38:15 +03:00
|
|
|
//
|
|
|
|
// Must be 512 or greater.
|
|
|
|
//
|
2017-05-04 05:30:30 +03:00
|
|
|
// Default: 512.
|
2017-04-30 02:38:15 +03:00
|
|
|
func ConnMaxFrameSize(n uint32) ConnOption {
|
2017-05-04 05:30:30 +03:00
|
|
|
return func(c *conn) error {
|
2017-04-30 02:38:15 +03:00
|
|
|
if n < 512 {
|
2021-09-02 23:42:44 +03:00
|
|
|
return errors.New("max frame size must be 512 or greater")
|
2017-04-30 02:38:15 +03:00
|
|
|
}
|
2017-04-27 06:35:29 +03:00
|
|
|
c.maxFrameSize = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:30:30 +03:00
|
|
|
// 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
|
|
|
|
2018-01-19 06:36:02 +03:00
|
|
|
// ConnMaxSessions sets the maximum number of channels.
|
|
|
|
//
|
|
|
|
// n must be in the range 1 to 65536.
|
|
|
|
//
|
|
|
|
// Default: 65536.
|
|
|
|
func ConnMaxSessions(n int) ConnOption {
|
|
|
|
return func(c *conn) error {
|
|
|
|
if n < 1 {
|
2021-09-02 23:42:44 +03:00
|
|
|
return errors.New("max sessions cannot be less than 1")
|
2018-01-19 06:36:02 +03:00
|
|
|
}
|
|
|
|
if n > 65536 {
|
2021-09-02 23:42:44 +03:00
|
|
|
return errors.New("max sessions cannot be greater than 65536")
|
2018-01-19 06:36:02 +03:00
|
|
|
}
|
|
|
|
c.channelMax = uint16(n - 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:14:36 +03:00
|
|
|
// ConnProperty sets an entry in the connection properties map sent to the server.
|
|
|
|
//
|
|
|
|
// This option can be used multiple times.
|
|
|
|
func ConnProperty(key, value string) ConnOption {
|
|
|
|
return func(c *conn) error {
|
|
|
|
if key == "" {
|
2021-09-02 23:42:44 +03:00
|
|
|
return errors.New("connection property key must not be empty")
|
2018-02-13 21:14:36 +03:00
|
|
|
}
|
|
|
|
if c.properties == nil {
|
2021-09-09 20:24:27 +03:00
|
|
|
c.properties = make(map[encoding.Symbol]interface{})
|
2018-02-13 21:14:36 +03:00
|
|
|
}
|
2021-09-09 20:24:27 +03:00
|
|
|
c.properties[encoding.Symbol(key)] = value
|
2018-02-13 21:14:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 05:50:14 +03:00
|
|
|
// ConnContainerID sets the container-id to use when opening the connection.
|
|
|
|
//
|
|
|
|
// A container ID will be randomly generated if this option is not used.
|
|
|
|
func ConnContainerID(id string) ConnOption {
|
|
|
|
return func(c *conn) error {
|
|
|
|
c.containerID = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:30:30 +03:00
|
|
|
// conn is an AMQP connection.
|
2021-09-20 20:55:31 +03:00
|
|
|
// only exported fields and methods are part of public surface area,
|
|
|
|
// all others are considered to be internal implementation details.
|
2017-05-04 05:30:30 +03:00
|
|
|
type conn struct {
|
|
|
|
net net.Conn // underlying connection
|
|
|
|
connectTimeout time.Duration // time to wait for reads/writes during conn establishment
|
2017-04-23 19:42:48 +03:00
|
|
|
|
|
|
|
// TLS
|
2017-04-30 02:38:15 +03:00
|
|
|
tlsNegotiation bool // negotiate TLS
|
|
|
|
tlsComplete bool // TLS negotiation complete
|
2017-05-04 05:30:30 +03:00
|
|
|
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
|
2021-09-09 20:24:27 +03:00
|
|
|
saslHandlers map[encoding.Symbol]stateFunc // map of supported handlers keyed by SASL mechanism, SASL not negotiated if nil
|
2021-09-20 20:55:31 +03:00
|
|
|
saslComplete bool // SASL negotiation complete; internal *except* for SASL auth methods
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-04-30 02:38:15 +03:00
|
|
|
// local settings
|
2021-09-09 20:24:27 +03:00
|
|
|
maxFrameSize uint32 // max frame size to accept
|
|
|
|
channelMax uint16 // maximum number of channels to allow
|
|
|
|
hostname string // hostname of remote server (set explicitly or parsed from URL)
|
|
|
|
idleTimeout time.Duration // maximum period between receiving frames
|
|
|
|
properties map[encoding.Symbol]interface{} // additional properties sent upon connection open
|
|
|
|
containerID string // set explicitly or randomly generated
|
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
|
2021-09-20 20:55:31 +03:00
|
|
|
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
|
2018-02-14 20:00:12 +03:00
|
|
|
errMu sync.Mutex // mux holds errMu from start until shutdown completes; operations are sequential before mux is started
|
2021-09-20 20:55:31 +03:00
|
|
|
err error // error to be returned to client; internal *except* for SASL auth methods
|
|
|
|
Done chan struct{} // indicates the connection is done
|
2017-04-01 23:00:36 +03:00
|
|
|
|
|
|
|
// mux
|
2021-09-20 20:55:31 +03:00
|
|
|
NewSession chan newSessionResp // 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
|
2018-02-14 20:00:12 +03:00
|
|
|
connErr chan error // connReader/Writer notifications of an error
|
|
|
|
closeMux chan struct{} // indicates that the mux should stop
|
|
|
|
closeMuxOnce sync.Once
|
2017-05-04 09:12:18 +03:00
|
|
|
|
|
|
|
// connReader
|
2021-09-15 21:40:36 +03:00
|
|
|
rxProto chan protoHeader // protoHeaders received by connReader
|
|
|
|
rxFrame chan frames.Frame // AMQP frames received by connReader
|
2018-02-12 02:26:24 +03:00
|
|
|
rxDone chan struct{}
|
|
|
|
connReaderRun chan func() // functions to be run by conn reader (set deadline on conn to run)
|
2017-05-04 09:12:18 +03:00
|
|
|
|
|
|
|
// connWriter
|
2021-09-15 21:40:36 +03:00
|
|
|
txFrame chan frames.Frame // AMQP frames to be sent by connWriter
|
|
|
|
txBuf buffer.Buffer // buffer for marshaling frames before transmitting
|
2017-11-13 07:51:38 +03:00
|
|
|
txDone chan struct{}
|
2017-05-04 09:12:18 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:36:02 +03:00
|
|
|
type newSessionResp struct {
|
|
|
|
session *Session
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-10-06 00:34:54 +03:00
|
|
|
// test hooks to fake dialing
|
|
|
|
var (
|
|
|
|
tcpDialer func(network, address string) (net.Conn, error)
|
|
|
|
tlsDialer func(dialer *net.Dialer, network, addr string, config *tls.Config) (*tls.Conn, error) = tls.DialWithDialer
|
|
|
|
)
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
func dialConn(addr string, opts ...ConnOption) (*conn, error) {
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
host, port := u.Hostname(), u.Port()
|
|
|
|
if port == "" {
|
|
|
|
port = "5672"
|
|
|
|
if u.Scheme == "amqps" {
|
|
|
|
port = "5671"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend SASL credentials when the user/pass segment is not empty
|
|
|
|
if u.User != nil {
|
|
|
|
pass, _ := u.User.Password()
|
|
|
|
opts = append([]ConnOption{
|
|
|
|
ConnSASLPlain(u.User.Username(), pass),
|
|
|
|
}, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// append default options so user specified can overwrite
|
|
|
|
opts = append([]ConnOption{
|
|
|
|
ConnServerHostname(host),
|
|
|
|
}, opts...)
|
|
|
|
|
|
|
|
c, err := newConn(nil, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := &net.Dialer{Timeout: c.connectTimeout}
|
2021-10-06 00:34:54 +03:00
|
|
|
if tcpDialer == nil {
|
|
|
|
tcpDialer = dialer.Dial
|
|
|
|
}
|
2021-09-20 20:55:31 +03:00
|
|
|
switch u.Scheme {
|
|
|
|
case "amqp", "":
|
2021-10-06 00:34:54 +03:00
|
|
|
c.net, err = tcpDialer("tcp", net.JoinHostPort(host, port))
|
2021-09-20 20:55:31 +03:00
|
|
|
case "amqps":
|
|
|
|
c.initTLSConfig()
|
|
|
|
c.tlsNegotiation = false
|
2021-10-06 00:34:54 +03:00
|
|
|
c.net, err = tlsDialer(dialer, "tcp", net.JoinHostPort(host, port), c.tlsConfig)
|
2021-09-20 20:55:31 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported scheme %q", u.Scheme)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:12:18 +03:00
|
|
|
func newConn(netConn net.Conn, opts ...ConnOption) (*conn, error) {
|
|
|
|
c := &conn{
|
|
|
|
net: netConn,
|
2017-05-07 02:57:27 +03:00
|
|
|
maxFrameSize: DefaultMaxFrameSize,
|
2021-09-20 20:55:31 +03:00
|
|
|
PeerMaxFrameSize: DefaultMaxFrameSize,
|
2018-01-19 06:36:02 +03:00
|
|
|
channelMax: DefaultMaxSessions - 1, // -1 because channel-max starts at zero
|
2017-05-07 02:57:27 +03:00
|
|
|
idleTimeout: DefaultIdleTimeout,
|
2018-08-01 05:50:14 +03:00
|
|
|
containerID: randString(40),
|
2021-09-20 20:55:31 +03:00
|
|
|
Done: make(chan struct{}),
|
2017-05-04 09:12:18 +03:00
|
|
|
connErr: make(chan error, 2), // buffered to ensure connReader/Writer won't leak
|
2018-02-14 20:00:12 +03:00
|
|
|
closeMux: make(chan struct{}),
|
2017-05-04 09:12:18 +03:00
|
|
|
rxProto: make(chan protoHeader),
|
2021-09-15 21:40:36 +03:00
|
|
|
rxFrame: make(chan frames.Frame),
|
2017-11-13 07:51:38 +03:00
|
|
|
rxDone: make(chan struct{}),
|
2018-02-12 02:26:24 +03:00
|
|
|
connReaderRun: make(chan func(), 1), // buffered to allow queueing function before interrupt
|
2021-09-20 20:55:31 +03:00
|
|
|
NewSession: make(chan newSessionResp),
|
|
|
|
DelSession: make(chan *Session),
|
2021-09-15 21:40:36 +03:00
|
|
|
txFrame: make(chan frames.Frame),
|
2017-11-13 07:51:38 +03:00
|
|
|
txDone: make(chan struct{}),
|
2017-05-04 09:12:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply options
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 19:13:49 +03:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) initTLSConfig() {
|
|
|
|
// create a new config if not already set
|
|
|
|
if c.tlsConfig == nil {
|
|
|
|
c.tlsConfig = new(tls.Config)
|
|
|
|
}
|
2017-05-04 09:12:18 +03:00
|
|
|
|
2018-01-28 19:13:49 +03:00
|
|
|
// TLS config must have ServerName or InsecureSkipVerify set
|
|
|
|
if c.tlsConfig.ServerName == "" && !c.tlsConfig.InsecureSkipVerify {
|
|
|
|
c.tlsConfig.ServerName = c.hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// Start establishes the connection and begins multiplexing network IO.
|
2021-10-06 00:34:54 +03:00
|
|
|
// It is an error to call Start() on a connection that's been closed.
|
2021-09-20 20:55:31 +03:00
|
|
|
func (c *conn) Start() error {
|
2017-05-04 09:56:55 +03:00
|
|
|
// start reader
|
2017-05-04 09:12:18 +03:00
|
|
|
go c.connReader()
|
|
|
|
|
|
|
|
// run connection establishment state machine
|
|
|
|
for state := c.negotiateProto; state != nil; {
|
|
|
|
state = state()
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if err occurred
|
|
|
|
if c.err != nil {
|
2017-11-13 07:51:38 +03:00
|
|
|
close(c.txDone) // close here since connWriter hasn't been started yet
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = c.Close()
|
2018-01-28 19:13:49 +03:00
|
|
|
return c.err
|
2017-05-04 09:12:18 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 09:56:55 +03:00
|
|
|
// start multiplexor and writer
|
2017-05-04 09:12:18 +03:00
|
|
|
go c.mux()
|
2017-05-04 09:56:55 +03:00
|
|
|
go c.connWriter()
|
2017-05-04 09:12:18 +03:00
|
|
|
|
2018-01-28 19:13:49 +03:00
|
|
|
return nil
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// Close closes the connection.
|
2017-11-13 07:51:38 +03:00
|
|
|
func (c *conn) Close() error {
|
2018-02-14 20:00:12 +03:00
|
|
|
c.closeMuxOnce.Do(func() { close(c.closeMux) })
|
2021-09-20 20:55:31 +03:00
|
|
|
err := c.Err()
|
2018-02-14 20:00:12 +03:00
|
|
|
if err == ErrConnClosed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2017-11-13 07:51:38 +03:00
|
|
|
}
|
|
|
|
|
2018-02-14 20:00:12 +03:00
|
|
|
// close should only be called by conn.mux.
|
2017-11-13 07:51:38 +03:00
|
|
|
func (c *conn) close() {
|
2021-09-20 20:55:31 +03:00
|
|
|
close(c.Done) // notify goroutines and blocked functions to exit
|
2017-11-13 07:51:38 +03:00
|
|
|
|
|
|
|
// wait for writing to stop, allows it to send the final close frame
|
|
|
|
<-c.txDone
|
|
|
|
|
2017-04-24 06:24:12 +03:00
|
|
|
err := c.net.Close()
|
2018-02-14 20:00:12 +03:00
|
|
|
switch {
|
|
|
|
// conn.err already set
|
|
|
|
case c.err != nil:
|
|
|
|
|
|
|
|
// conn.err not set and c.net.Close() returned a non-nil error
|
|
|
|
case err != nil:
|
2017-04-24 06:24:12 +03:00
|
|
|
c.err = err
|
2018-02-14 20:00:12 +03:00
|
|
|
|
|
|
|
// no errors
|
|
|
|
default:
|
|
|
|
c.err = ErrConnClosed
|
2017-04-24 06:24:12 +03:00
|
|
|
}
|
2017-11-13 07:51:38 +03:00
|
|
|
|
|
|
|
// check rxDone after closing net, otherwise may block
|
|
|
|
// for up to c.idleTimeout
|
|
|
|
<-c.rxDone
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// Err returns the connection's error state after it's been closed.
|
|
|
|
// Calling this on an open connection will block until the connection is closed.
|
|
|
|
func (c *conn) Err() error {
|
2017-11-13 07:51:38 +03:00
|
|
|
c.errMu.Lock()
|
|
|
|
defer c.errMu.Unlock()
|
|
|
|
return c.err
|
|
|
|
}
|
|
|
|
|
2018-04-15 06:18:34 +03:00
|
|
|
// mux is started in it's own goroutine after initial connection establishment.
|
|
|
|
// It handles muxing of sessions, keepalives, and connection errors.
|
2017-05-04 05:30:30 +03:00
|
|
|
func (c *conn) mux() {
|
2018-01-19 06:36:02 +03:00
|
|
|
var (
|
2018-04-15 06:18:34 +03:00
|
|
|
// allocated channels
|
2021-09-16 20:32:22 +03:00
|
|
|
channels = bitmap.New(uint32(c.channelMax))
|
2018-04-15 06:18:34 +03:00
|
|
|
|
2018-01-19 06:36:02 +03:00
|
|
|
// create the next session to allocate
|
2021-09-16 20:32:22 +03:00
|
|
|
nextChannel, _ = channels.Next()
|
2018-04-15 06:18:34 +03:00
|
|
|
nextSession = newSessionResp{session: newSession(c, uint16(nextChannel))}
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2018-01-19 06:36:02 +03:00
|
|
|
// map channels to sessions
|
|
|
|
sessionsByChannel = make(map[uint16]*Session)
|
|
|
|
sessionsByRemoteChannel = make(map[uint16]*Session)
|
|
|
|
)
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// hold the errMu lock until error or done
|
2017-04-27 06:35:29 +03:00
|
|
|
c.errMu.Lock()
|
|
|
|
defer c.errMu.Unlock()
|
2018-02-14 20:00:12 +03:00
|
|
|
defer c.close() // defer order is important. c.errMu unlock indicates that connection is finally complete
|
2017-04-27 06:35:29 +03:00
|
|
|
|
2017-04-01 23:00:36 +03:00
|
|
|
for {
|
2017-05-01 08:02:53 +03:00
|
|
|
// check if last loop returned an error
|
2017-04-01 23:00:36 +03:00
|
|
|
if c.err != nil {
|
2017-04-24 06:24:12 +03:00
|
|
|
return
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-05-01 08:02:53 +03:00
|
|
|
// error from connReader
|
2017-05-04 09:12:18 +03:00
|
|
|
case c.err = <-c.connErr:
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// new frame from connReader
|
2017-04-23 04:31:07 +03:00
|
|
|
case fr := <-c.rxFrame:
|
2018-02-08 08:26:49 +03:00
|
|
|
var (
|
|
|
|
session *Session
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
2021-09-15 21:40:36 +03:00
|
|
|
switch body := fr.Body.(type) {
|
2019-01-30 06:02:08 +03:00
|
|
|
// Server initiated close.
|
2021-09-15 21:40:36 +03:00
|
|
|
case *frames.PerformClose:
|
2019-01-30 06:02:08 +03:00
|
|
|
if body.Error != nil {
|
|
|
|
c.err = body.Error
|
|
|
|
} else {
|
|
|
|
c.err = ErrConnClosed
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
2018-02-08 08:26:49 +03:00
|
|
|
// RemoteChannel should be used when frame is Begin
|
2021-09-15 21:40:36 +03:00
|
|
|
case *frames.PerformBegin:
|
2019-12-04 07:00:02 +03:00
|
|
|
if body.RemoteChannel == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
session, ok = sessionsByChannel[*body.RemoteChannel]
|
2018-01-19 06:36:02 +03:00
|
|
|
if !ok {
|
2018-02-08 08:26:49 +03:00
|
|
|
break
|
2018-01-19 06:36:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:40:36 +03:00
|
|
|
session.remoteChannel = fr.Channel
|
|
|
|
sessionsByRemoteChannel[fr.Channel] = session
|
2018-02-08 08:26:49 +03:00
|
|
|
|
|
|
|
default:
|
2021-09-15 21:40:36 +03:00
|
|
|
session, ok = sessionsByRemoteChannel[fr.Channel]
|
2018-02-08 08:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2021-09-15 21:40:36 +03:00
|
|
|
c.err = fmt.Errorf("unexpected frame: %#v", fr.Body)
|
2018-02-08 08:26:49 +03:00
|
|
|
continue
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
2017-12-31 08:22:02 +03:00
|
|
|
|
|
|
|
select {
|
2018-01-19 06:36:02 +03:00
|
|
|
case session.rx <- fr:
|
2018-02-14 20:00:12 +03:00
|
|
|
case <-c.closeMux:
|
2017-12-31 08:22:02 +03:00
|
|
|
return
|
|
|
|
}
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +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,
|
2017-05-07 05:10:33 +03:00
|
|
|
// this avoids the lock/unlock for session lookup.
|
2021-09-20 20:55:31 +03:00
|
|
|
case c.NewSession <- nextSession:
|
2018-01-19 06:36:02 +03:00
|
|
|
if nextSession.err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-15 06:18:34 +03:00
|
|
|
// save session into map
|
2018-01-19 06:36:02 +03:00
|
|
|
ch := nextSession.session.channel
|
|
|
|
sessionsByChannel[ch] = nextSession.session
|
|
|
|
|
2018-04-15 06:18:34 +03:00
|
|
|
// get next available channel
|
2021-09-16 20:32:22 +03:00
|
|
|
next, ok := channels.Next()
|
2018-04-15 06:18:34 +03:00
|
|
|
if !ok {
|
2021-09-02 23:42:44 +03:00
|
|
|
nextSession = newSessionResp{err: fmt.Errorf("reached connection channel max (%d)", c.channelMax)}
|
2018-01-19 06:36:02 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// create the next session to send
|
2018-04-15 06:18:34 +03:00
|
|
|
nextSession = newSessionResp{session: newSession(c, uint16(next))}
|
2017-05-01 08:02:53 +03:00
|
|
|
|
|
|
|
// session deletion
|
2021-09-20 20:55:31 +03:00
|
|
|
case s := <-c.DelSession:
|
2018-01-19 06:36:02 +03:00
|
|
|
delete(sessionsByChannel, s.channel)
|
|
|
|
delete(sessionsByRemoteChannel, s.remoteChannel)
|
2021-09-16 20:32:22 +03:00
|
|
|
channels.Remove(uint32(s.channel))
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// connection is complete
|
2018-02-14 20:00:12 +03:00
|
|
|
case <-c.closeMux:
|
2017-04-27 06:35:29 +03:00
|
|
|
return
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 02:38:15 +03:00
|
|
|
// connReader reads from the net.Conn, decodes frames, and passes them
|
2017-05-04 05:30:30 +03:00
|
|
|
// up via the conn.rxFrame and conn.rxProto channels.
|
|
|
|
func (c *conn) connReader() {
|
2017-11-13 07:51:38 +03:00
|
|
|
defer close(c.rxDone)
|
|
|
|
|
2021-09-08 05:43:35 +03:00
|
|
|
buf := &buffer.Buffer{}
|
2017-04-24 06:24:12 +03:00
|
|
|
|
2017-04-30 02:38:15 +03:00
|
|
|
var (
|
2021-10-05 02:56:20 +03:00
|
|
|
negotiating = true // true during conn establishment, check for protoHeaders
|
|
|
|
currentHeader frames.Header // keep track of the current header, for frames split across multiple TCP packets
|
|
|
|
frameInProgress bool // true if in the middle of receiving data for currentHeader
|
2017-04-30 02:38:15 +03:00
|
|
|
)
|
2017-04-23 04:31:07 +03:00
|
|
|
|
2017-04-27 06:35:29 +03:00
|
|
|
for {
|
2019-11-27 03:18:31 +03:00
|
|
|
switch {
|
|
|
|
// Cheaply reuse free buffer space when fully read.
|
2021-09-08 05:43:35 +03:00
|
|
|
case buf.Len() == 0:
|
|
|
|
buf.Reset()
|
2019-11-27 03:18:31 +03:00
|
|
|
|
|
|
|
// Prevent excessive/unbounded growth by shifting data to beginning of buffer.
|
2021-09-08 05:43:35 +03:00
|
|
|
case int64(buf.Size()) > int64(c.maxFrameSize):
|
|
|
|
buf.Reclaim()
|
2018-02-03 22:54:49 +03:00
|
|
|
}
|
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// need to read more if buf doesn't contain the complete frame
|
2017-05-01 08:02:53 +03:00
|
|
|
// or there's not enough in buf to parse the header
|
2021-10-05 02:56:20 +03:00
|
|
|
if frameInProgress || buf.Len() < frames.HeaderSize {
|
2018-06-01 06:34:23 +03:00
|
|
|
if c.idleTimeout > 0 {
|
|
|
|
_ = c.net.SetReadDeadline(time.Now().Add(c.idleTimeout))
|
|
|
|
}
|
2021-09-08 05:43:35 +03:00
|
|
|
err := buf.ReadFromOnce(c.net)
|
2017-04-27 06:35:29 +03:00
|
|
|
if err != nil {
|
2021-09-20 17:22:47 +03:00
|
|
|
debug(1, "connReader error: %v", err)
|
2017-11-13 07:51:38 +03:00
|
|
|
select {
|
2018-02-12 02:26:24 +03:00
|
|
|
// check if error was due to close in progress
|
2021-09-20 20:55:31 +03:00
|
|
|
case <-c.Done:
|
2017-11-13 07:51:38 +03:00
|
|
|
return
|
2018-02-12 02:26:24 +03:00
|
|
|
|
|
|
|
// if there is a pending connReaderRun function, execute it
|
|
|
|
case f := <-c.connReaderRun:
|
|
|
|
f()
|
|
|
|
continue
|
|
|
|
|
|
|
|
// send error to mux and return
|
2017-11-13 07:51:38 +03:00
|
|
|
default:
|
2018-02-12 02:26:24 +03:00
|
|
|
c.connErr <- err
|
|
|
|
return
|
2017-11-13 07:51:38 +03:00
|
|
|
}
|
2017-04-24 06:24:12 +03:00
|
|
|
}
|
2017-04-27 06:35:29 +03:00
|
|
|
}
|
2017-04-23 04:31:07 +03:00
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// read more if buf doesn't contain enough to parse the header
|
2021-10-05 02:56:20 +03:00
|
|
|
if buf.Len() < frames.HeaderSize {
|
2017-04-27 06:35:29 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-04-23 04:31:07 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// during negotiation, check for proto frames
|
2021-09-08 05:43:35 +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)
|
2017-04-23 04:31:07 +03:00
|
|
|
if err != nil {
|
2017-05-04 09:12:18 +03:00
|
|
|
c.connErr <- err
|
2017-04-24 06:24:12 +03:00
|
|
|
return
|
2017-04-23 04:31:07 +03:00
|
|
|
}
|
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// send proto header
|
2017-04-24 06:24:12 +03:00
|
|
|
select {
|
2021-09-20 20:55:31 +03:00
|
|
|
case <-c.Done:
|
2017-04-24 06:24:12 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// parse the header if a frame isn't in progress
|
2017-04-27 06:35:29 +03:00
|
|
|
if !frameInProgress {
|
2017-05-01 08:02:53 +03:00
|
|
|
var err error
|
2021-10-05 02:56:20 +03:00
|
|
|
currentHeader, err = frames.ParseHeader(buf)
|
2017-04-27 06:35:29 +03:00
|
|
|
if err != nil {
|
2017-05-04 09:12:18 +03:00
|
|
|
c.connErr <- err
|
2017-04-27 06:35:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
frameInProgress = true
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// check size is reasonable
|
2017-04-30 02:38:15 +03:00
|
|
|
if currentHeader.Size > math.MaxInt32 { // make max size configurable
|
2021-09-02 23:42:44 +03:00
|
|
|
c.connErr <- errors.New("payload too large")
|
2017-04-30 02:38:15 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:56:20 +03:00
|
|
|
bodySize := int64(currentHeader.Size - frames.HeaderSize)
|
2017-04-30 02:38:15 +03:00
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// the full frame has been received
|
2021-09-08 05:43:35 +03:00
|
|
|
if int64(buf.Len()) < bodySize {
|
2017-04-27 06:35:29 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
frameInProgress = false
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// check if body is empty (keepalive)
|
2017-04-30 02:38:15 +03:00
|
|
|
if bodySize == 0 {
|
2017-05-01 08:02:53 +03:00
|
|
|
continue
|
2017-04-30 02:38:15 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// parse the frame
|
2021-09-08 05:43:35 +03:00
|
|
|
b, ok := buf.Next(bodySize)
|
2018-02-03 22:54:49 +03:00
|
|
|
if !ok {
|
|
|
|
c.connErr <- io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:56:20 +03:00
|
|
|
parsedBody, err := frames.ParseBody(buffer.New(b))
|
2017-04-27 06:35:29 +03:00
|
|
|
if err != nil {
|
2017-05-04 09:12:18 +03:00
|
|
|
c.connErr <- err
|
2017-04-27 06:35:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// send to mux
|
2017-04-27 06:35:29 +03:00
|
|
|
select {
|
2021-09-20 20:55:31 +03:00
|
|
|
case <-c.Done:
|
2017-04-27 06:35:29 +03:00
|
|
|
return
|
2021-09-15 21:40:36 +03:00
|
|
|
case c.rxFrame <- frames.Frame{Channel: currentHeader.Channel, Body: parsedBody}:
|
2017-04-27 06:35:29 +03:00
|
|
|
}
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:12:18 +03:00
|
|
|
func (c *conn) connWriter() {
|
2017-11-13 07:51:38 +03:00
|
|
|
defer close(c.txDone)
|
|
|
|
|
2019-06-20 17:50:15 +03:00
|
|
|
// disable write timeout
|
|
|
|
if c.connectTimeout != 0 {
|
|
|
|
c.connectTimeout = 0
|
|
|
|
_ = c.net.SetWriteDeadline(time.Time{})
|
|
|
|
}
|
|
|
|
|
2017-05-07 01:26:17 +03:00
|
|
|
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
|
|
|
|
)
|
2017-05-04 09:12:18 +03:00
|
|
|
|
2017-05-07 01:26:17 +03:00
|
|
|
if keepalivesEnabled {
|
|
|
|
ticker := time.NewTicker(keepaliveInterval)
|
2017-05-04 09:12:18 +03:00
|
|
|
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:
|
2017-05-04 09:56:55 +03:00
|
|
|
err = c.writeFrame(fr)
|
2021-09-15 21:40:36 +03:00
|
|
|
if err == nil && fr.Done != nil {
|
|
|
|
close(fr.Done)
|
2017-12-31 08:22:02 +03:00
|
|
|
}
|
2017-05-04 09:12:18 +03:00
|
|
|
|
|
|
|
// keepalive timer
|
2017-05-07 01:26:17 +03:00
|
|
|
case <-keepalive:
|
2017-05-04 09:12:18 +03:00
|
|
|
_, err = c.net.Write(keepaliveFrame)
|
2017-05-07 01:26:17 +03:00
|
|
|
// 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
|
2021-09-20 20:55:31 +03:00
|
|
|
case <-c.Done:
|
2017-11-13 07:51:38 +03:00
|
|
|
// send close
|
2021-09-15 21:40:36 +03:00
|
|
|
cls := &frames.PerformClose{}
|
2019-12-02 21:06:02 +03:00
|
|
|
debug(1, "TX: %s", cls)
|
2021-09-15 21:40:36 +03:00
|
|
|
_ = c.writeFrame(frames.Frame{
|
|
|
|
Type: frameTypeAMQP,
|
|
|
|
Body: cls,
|
2017-11-13 07:51:38 +03:00
|
|
|
})
|
2017-05-04 09:56:55 +03:00
|
|
|
return
|
2017-05-04 09:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// writeFrame writes a frame to the network.
|
|
|
|
// used externally by SASL only.
|
2021-09-15 21:40:36 +03:00
|
|
|
func (c *conn) writeFrame(fr frames.Frame) error {
|
2017-05-04 09:56:55 +03:00
|
|
|
if c.connectTimeout != 0 {
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
|
2017-05-04 09:56:55 +03:00
|
|
|
}
|
2017-05-07 02:57:27 +03:00
|
|
|
|
|
|
|
// writeFrame into txBuf
|
2021-09-08 05:43:35 +03:00
|
|
|
c.txBuf.Reset()
|
2017-05-04 09:56:55 +03:00
|
|
|
err := writeFrame(&c.txBuf, fr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-07 05:10:33 +03:00
|
|
|
// validate the frame isn't exceeding peer's max frame size
|
2021-09-08 05:43:35 +03:00
|
|
|
requiredFrameSize := c.txBuf.Len()
|
2021-09-20 20:55:31 +03:00
|
|
|
if uint64(requiredFrameSize) > uint64(c.PeerMaxFrameSize) {
|
|
|
|
return fmt.Errorf("%T frame size %d larger than peer's max frame size %d", fr, requiredFrameSize, c.PeerMaxFrameSize)
|
2017-05-04 09:56:55 +03:00
|
|
|
}
|
|
|
|
|
2017-05-07 02:57:27 +03:00
|
|
|
// write to network
|
2021-09-08 05:43:35 +03:00
|
|
|
_, err = c.net.Write(c.txBuf.Bytes())
|
2017-05-04 09:56:55 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-07 02:57:27 +03:00
|
|
|
// writeProtoHeader writes an AMQP protocol header to the
|
|
|
|
// network
|
2017-05-04 09:56:55 +03:00
|
|
|
func (c *conn) writeProtoHeader(pID protoID) error {
|
|
|
|
if c.connectTimeout != 0 {
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = c.net.SetWriteDeadline(time.Now().Add(c.connectTimeout))
|
2017-05-04 09:56:55 +03:00
|
|
|
}
|
|
|
|
_, err := c.net.Write([]byte{'A', 'M', 'Q', 'P', byte(pID), 1, 0, 0})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:12:18 +03:00
|
|
|
// keepaliveFrame is an AMQP frame with no body, used for keepalives
|
|
|
|
var keepaliveFrame = []byte{0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00}
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// SendFrame is used by sessions and links to send frames across the network.
|
|
|
|
func (c *conn) SendFrame(fr frames.Frame) error {
|
2017-05-04 09:12:18 +03:00
|
|
|
select {
|
|
|
|
case c.txFrame <- fr:
|
2018-05-21 06:32:31 +03:00
|
|
|
return nil
|
2021-09-20 20:55:31 +03:00
|
|
|
case <-c.Done:
|
|
|
|
return c.Err()
|
2017-05-04 09:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:30:30 +03:00
|
|
|
// 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
|
|
|
|
|
2021-09-20 20:55:31 +03:00
|
|
|
// negotiateProto determines which proto to negotiate next.
|
|
|
|
// used externally by SASL only.
|
2017-05-04 05:30:30 +03:00
|
|
|
func (c *conn) negotiateProto() stateFunc {
|
2017-05-01 08:02:53 +03:00
|
|
|
// in the order each must be negotiated
|
2017-04-01 23:00:36 +03:00
|
|
|
switch {
|
2017-04-23 19:42:48 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:12:18 +03:00
|
|
|
type protoID uint8
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// protocol IDs received in protoHeaders
|
2017-04-01 23:00:36 +03:00
|
|
|
const (
|
2017-05-04 09:12:18 +03:00
|
|
|
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.
|
2017-05-04 09:12:18 +03:00
|
|
|
func (c *conn) exchangeProtoHeader(pID protoID) stateFunc {
|
2017-05-01 08:02:53 +03:00
|
|
|
// write the proto header
|
2017-05-04 09:56:55 +03:00
|
|
|
c.err = c.writeProtoHeader(pID)
|
|
|
|
if c.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// read response header
|
2017-05-07 01:26:17 +03:00
|
|
|
p, err := c.readProtoHeader()
|
|
|
|
if err != nil {
|
|
|
|
c.err = err
|
2017-04-01 23:00:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:12:18 +03:00
|
|
|
if pID != p.ProtoID {
|
2021-09-02 23:42:44 +03:00
|
|
|
c.err = fmt.Errorf("unexpected protocol header %#00x, expected %#00x", p.ProtoID, pID)
|
2017-04-01 23:00:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// go to the proto specific state
|
2017-05-04 09:12:18 +03:00
|
|
|
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:
|
2021-09-02 23:42:44 +03:00
|
|
|
c.err = fmt.Errorf("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.
|
2017-05-07 01:26:17 +03:00
|
|
|
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:
|
2021-09-02 23:42:44 +03:00
|
|
|
return p, fmt.Errorf("unexpected frame %#v", fr)
|
2017-05-07 01:26:17 +03:00
|
|
|
case <-deadline:
|
|
|
|
return p, ErrTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:30:30 +03:00
|
|
|
// startTLS wraps the conn with TLS and returns to Client.negotiateProto
|
|
|
|
func (c *conn) startTLS() stateFunc {
|
2018-01-28 19:13:49 +03:00
|
|
|
c.initTLSConfig()
|
2017-04-30 05:33:03 +03:00
|
|
|
|
2018-02-12 02:26:24 +03:00
|
|
|
done := make(chan struct{})
|
2017-04-30 05:33:03 +03:00
|
|
|
|
2018-02-12 02:26:24 +03:00
|
|
|
// this function will be executed by connReader
|
|
|
|
c.connReaderRun <- func() {
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = c.net.SetReadDeadline(time.Time{}) // clear timeout
|
2018-02-12 02:26:24 +03:00
|
|
|
|
|
|
|
// wrap existing net.Conn and perform TLS handshake
|
|
|
|
tlsConn := tls.Client(c.net, c.tlsConfig)
|
|
|
|
if c.connectTimeout != 0 {
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = tlsConn.SetWriteDeadline(time.Now().Add(c.connectTimeout))
|
2018-02-12 02:26:24 +03:00
|
|
|
}
|
|
|
|
c.err = tlsConn.Handshake()
|
|
|
|
|
|
|
|
// swap net.Conn
|
|
|
|
c.net = tlsConn
|
|
|
|
c.tlsComplete = true
|
|
|
|
|
|
|
|
close(done)
|
2017-05-04 05:30:30 +03:00
|
|
|
}
|
2018-02-12 02:26:24 +03:00
|
|
|
|
|
|
|
// set deadline to interrupt connReader
|
2018-02-17 19:24:03 +03:00
|
|
|
_ = c.net.SetReadDeadline(time.Time{}.Add(1))
|
2018-02-12 02:26:24 +03:00
|
|
|
|
|
|
|
<-done
|
|
|
|
|
2017-04-30 05:33:03 +03:00
|
|
|
if c.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// 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
|
2017-05-04 05:30:30 +03:00
|
|
|
func (c *conn) openAMQP() stateFunc {
|
2017-05-01 08:02:53 +03:00
|
|
|
// send open frame
|
2021-09-15 21:40:36 +03:00
|
|
|
open := &frames.PerformOpen{
|
2019-12-02 21:06:02 +03:00
|
|
|
ContainerID: c.containerID,
|
|
|
|
Hostname: c.hostname,
|
|
|
|
MaxFrameSize: c.maxFrameSize,
|
|
|
|
ChannelMax: c.channelMax,
|
2021-09-20 17:22:47 +03:00
|
|
|
IdleTimeout: c.idleTimeout / 2, // per spec, advertise half our idle timeout
|
2019-12-02 21:06:02 +03:00
|
|
|
Properties: c.properties,
|
|
|
|
}
|
|
|
|
debug(1, "TX: %s", open)
|
2021-09-15 21:40:36 +03:00
|
|
|
c.err = c.writeFrame(frames.Frame{
|
|
|
|
Type: frameTypeAMQP,
|
|
|
|
Body: open,
|
|
|
|
Channel: 0,
|
2017-04-22 22:56:08 +03:00
|
|
|
})
|
2017-05-04 09:56:55 +03:00
|
|
|
if c.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +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
|
|
|
|
}
|
2021-09-15 21:40:36 +03:00
|
|
|
o, ok := fr.Body.(*frames.PerformOpen)
|
2017-04-24 06:24:12 +03:00
|
|
|
if !ok {
|
2021-09-15 21:40:36 +03:00
|
|
|
c.err = fmt.Errorf("unexpected frame type %T", fr.Body)
|
2017-04-27 06:35:29 +03:00
|
|
|
return nil
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
2019-12-02 21:06:02 +03:00
|
|
|
debug(1, "RX: %s", o)
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// update peer settings
|
2017-04-27 06:35:29 +03:00
|
|
|
if o.MaxFrameSize > 0 {
|
2021-09-20 20:55:31 +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 {
|
2017-05-07 01:26:17 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:02:53 +03:00
|
|
|
// 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
|
2017-05-04 05:30:30 +03:00
|
|
|
func (c *conn) negotiateSASL() stateFunc {
|
2017-05-03 04:49:31 +03:00
|
|
|
// 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
|
|
|
|
}
|
2021-09-15 21:40:36 +03:00
|
|
|
sm, ok := fr.Body.(*frames.SASLMechanisms)
|
2017-04-24 06:24:12 +03:00
|
|
|
if !ok {
|
2021-09-15 21:40:36 +03:00
|
|
|
c.err = fmt.Errorf("unexpected frame type %T", fr.Body)
|
2017-04-01 23:00:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-02 21:06:02 +03:00
|
|
|
debug(1, "RX: %s", sm)
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-03 04:49:31 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:49:31 +03:00
|
|
|
// no match
|
2021-09-02 23:42:44 +03:00
|
|
|
c.err = fmt.Errorf("no supported auth mechanism (%v)", sm.Mechanisms) // TODO: send "auth not supported" frame?
|
2017-04-01 23:00:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:30:30 +03:00
|
|
|
// 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.
|
2021-09-20 20:55:31 +03:00
|
|
|
// used externally by SASL only.
|
2017-05-04 05:30:30 +03:00
|
|
|
func (c *conn) saslOutcome() stateFunc {
|
2017-05-03 04:49:31 +03:00
|
|
|
// 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
|
|
|
|
}
|
2021-09-15 21:40:36 +03:00
|
|
|
so, ok := fr.Body.(*frames.SASLOutcome)
|
2017-04-24 06:24:12 +03:00
|
|
|
if !ok {
|
2021-09-15 21:40:36 +03:00
|
|
|
c.err = fmt.Errorf("unexpected frame type %T", fr.Body)
|
2017-04-27 06:35:29 +03:00
|
|
|
return nil
|
2017-04-01 23:00:36 +03:00
|
|
|
}
|
2019-12-02 21:06:02 +03:00
|
|
|
debug(1, "RX: %s", so)
|
2017-04-01 23:00:36 +03:00
|
|
|
|
2017-05-03 04:49:31 +03:00
|
|
|
// check if auth succeeded
|
2021-09-15 21:40:36 +03:00
|
|
|
if so.Code != encoding.CodeSASLOK {
|
2021-09-02 23:42:44 +03:00
|
|
|
c.err = fmt.Errorf("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
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:49:31 +03:00
|
|
|
// 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.
|
2021-09-20 20:55:31 +03:00
|
|
|
// used externally by SASL only.
|
2021-09-15 21:40:36 +03:00
|
|
|
func (c *conn) readFrame() (frames.Frame, error) {
|
2017-05-04 05:30:30 +03:00
|
|
|
var deadline <-chan time.Time
|
|
|
|
if c.connectTimeout != 0 {
|
|
|
|
deadline = time.After(c.connectTimeout)
|
|
|
|
}
|
2017-05-04 09:12:18 +03:00
|
|
|
|
2021-09-15 21:40:36 +03:00
|
|
|
var fr frames.Frame
|
2017-04-24 06:24:12 +03:00
|
|
|
select {
|
|
|
|
case fr = <-c.rxFrame:
|
|
|
|
return fr, nil
|
2017-05-04 09:12:18 +03:00
|
|
|
case err := <-c.connErr:
|
2017-04-24 06:24:12 +03:00
|
|
|
return fr, err
|
2017-04-24 07:03:50 +03:00
|
|
|
case p := <-c.rxProto:
|
2021-09-02 23:42:44 +03:00
|
|
|
return fr, fmt.Errorf("unexpected protocol header %#v", p)
|
2017-05-04 05:30:30 +03:00
|
|
|
case <-deadline:
|
2018-02-12 01:55:49 +03:00
|
|
|
return fr, ErrTimeout
|
2017-04-24 06:24:12 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-08 20:27:48 +03:00
|
|
|
|
|
|
|
type protoHeader struct {
|
|
|
|
ProtoID protoID
|
|
|
|
Major uint8
|
|
|
|
Minor uint8
|
|
|
|
Revision uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseProtoHeader reads the proto header from r and returns the results
|
|
|
|
//
|
|
|
|
// An error is returned if the protocol is not "AMQP" or if the version is not 1.0.0.
|
|
|
|
func parseProtoHeader(r *buffer.Buffer) (protoHeader, error) {
|
|
|
|
const protoHeaderSize = 8
|
|
|
|
buf, ok := r.Next(protoHeaderSize)
|
|
|
|
if !ok {
|
|
|
|
return protoHeader{}, errors.New("invalid protoHeader")
|
|
|
|
}
|
|
|
|
_ = buf[7]
|
|
|
|
|
|
|
|
if !bytes.Equal(buf[:4], []byte{'A', 'M', 'Q', 'P'}) {
|
|
|
|
return protoHeader{}, fmt.Errorf("unexpected protocol %q", buf[:4])
|
|
|
|
}
|
|
|
|
|
|
|
|
p := protoHeader{
|
|
|
|
ProtoID: protoID(buf[4]),
|
|
|
|
Major: buf[5],
|
|
|
|
Minor: buf[6],
|
|
|
|
Revision: buf[7],
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Major != 1 || p.Minor != 0 || p.Revision != 0 {
|
|
|
|
return p, fmt.Errorf("unexpected protocol version %d.%d.%d", p.Major, p.Minor, p.Revision)
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writesFrame encodes fr into buf.
|
2021-09-20 20:55:31 +03:00
|
|
|
// split out from conn.WriteFrame for testing purposes.
|
2021-09-15 21:40:36 +03:00
|
|
|
func writeFrame(buf *buffer.Buffer, fr frames.Frame) error {
|
2021-09-08 20:27:48 +03:00
|
|
|
// write header
|
|
|
|
buf.Append([]byte{
|
|
|
|
0, 0, 0, 0, // size, overwrite later
|
2021-09-15 21:40:36 +03:00
|
|
|
2, // doff, see frameHeader.DataOffset comment
|
|
|
|
fr.Type, // frame type
|
2021-09-08 20:27:48 +03:00
|
|
|
})
|
2021-09-15 21:40:36 +03:00
|
|
|
buf.AppendUint16(fr.Channel) // channel
|
2021-09-08 20:27:48 +03:00
|
|
|
|
|
|
|
// write AMQP frame body
|
2021-09-15 21:40:36 +03:00
|
|
|
err := encoding.Marshal(buf, fr.Body)
|
2021-09-08 20:27:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate size
|
|
|
|
if uint(buf.Len()) > math.MaxUint32 {
|
|
|
|
return errors.New("frame too large")
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve raw bytes
|
|
|
|
bufBytes := buf.Bytes()
|
|
|
|
|
|
|
|
// write correct size
|
|
|
|
binary.BigEndian.PutUint32(bufBytes, uint32(len(bufBytes)))
|
|
|
|
return nil
|
|
|
|
}
|