func (m *Memberlist) readRemoteState(conn net.Conn) (bool, []pushNodeState, []byte, error) {
	conn.SetDeadline(time.Now().Add(m.config.TCPTimeout))
	var bufConn io.Reader = bufio.NewReader(conn)
	buf := [1]byte{0}
	if _, err := bufConn.Read(buf[:]); err != nil {
		return false, nil, nil, err
	}
	msgType := messageType(buf[0])
	if msgType == encryptMsg {
		if !m.config.EncryptionEnabled() {
			return false, nil, nil,
				fmt.Errorf("Remote state is encrypted and encryption is not configured")
		}
		plain, err := m.decryptRemoteState(bufConn)
		if err != nil {
			return false, nil, nil, err
		}
		msgType = messageType(plain[0])
		bufConn = bytes.NewReader(plain[1:])
	} else if m.config.EncryptionEnabled() {
		return false, nil, nil,
			fmt.Errorf("Encryption is configured but remote state is not encrypted")
	}
	hd := codec.MsgpackHandle{}
	dec := codec.NewDecoder(bufConn, &hd)
	if msgType == compressMsg {
		var c compress
		if err := dec.Decode(&c); err != nil {
			return false, nil, nil, err
		}
		decomp, err := decompressBuffer(&c)
		if err != nil {
			return false, nil, nil, err
		}
		msgType = messageType(decomp[0])
		bufConn = bytes.NewReader(decomp[1:])
		dec = codec.NewDecoder(bufConn, &hd)
	}
	if msgType != pushPullMsg {
		err := fmt.Errorf("received invalid msgType (%d)", msgType)
		return false, nil, nil, err
	}
	var header pushPullHeader
	if err := dec.Decode(&header); err != nil {
		return false, nil, nil, err
	}
	remoteNodes := make([]pushNodeState, header.Nodes)
	for i := 0; i < header.Nodes; i++ {
		if err := dec.Decode(&remoteNodes[i]); err != nil {
			return false, remoteNodes, nil, err
		}
	}
	var userBuf []byte
	if header.UserStateLen > 0 {
		userBuf = make([]byte, header.UserStateLen)
		bytes, err := io.ReadAtLeast(bufConn, userBuf, header.UserStateLen)
		if err == nil && bytes != header.UserStateLen {
			err = fmt.Errorf(
				"Failed to read full user state (%d / %d)",
				bytes, header.UserStateLen)
		}
		if err != nil {
			return false, remoteNodes, nil, err
		}
	}
	for idx := range remoteNodes {
		if m.ProtocolVersion() < 2 || remoteNodes[idx].Port == 0 {
			remoteNodes[idx].Port = uint16(m.config.BindPort)
		}
	}
	return header.Join, remoteNodes, userBuf, nil
}
recvRemoteState is used to read the remote state from a connection
package memberlist
import (
	"bufio"
	"bytes"
	"encoding/binary"
	"fmt"
	"io"
	"net"
	"time"
	"github.com/socketplane/socketplane/Godeps/_workspace/src/github.com/armon/go-metrics"
	"github.com/socketplane/socketplane/Godeps/_workspace/src/github.com/hashicorp/go-msgpack/codec"
)
const (
	ProtocolVersionMin uint8 = 1
	ProtocolVersionMax       = 2
)
type messageType uint8
const (
	pingMsg messageType = iota
	indirectPingMsg
	ackRespMsg
	suspectMsg
	aliveMsg
	deadMsg
	pushPullMsg
	compoundMsg
	userMsg  
	compressMsg
	encryptMsg
)
type compressionType uint8
const (
	lzwAlgo compressionType = iota
)
const (
	MetaMaxSize            = 512  
	compoundHeaderOverhead = 2    
	compoundOverhead       = 2    
	udpBufSize             = 65536
	udpRecvBuf             = 2 * 1024 * 1024
	udpSendBuf             = 1400
	userMsgOverhead        = 1
	blockingWarning        = 10 * time.Millisecond  
	maxPushStateBytes      = 10 * 1024 * 1024
)
type ping struct {
	SeqNo uint32
	Node string
}
type indirectPingReq struct {
	SeqNo  uint32
	Target []byte
	Port   uint16
	Node   string
}
type ackResp struct {
	SeqNo uint32
}
type suspect struct {
	Incarnation uint32
	Node        string
	From        string  
}
type alive struct {
	Incarnation uint32
	Node        string
	Addr        []byte
	Port        uint16
	Meta        []byte
	Vsn []uint8
}
type dead struct {
	Incarnation uint32
	Node        string
	From        string  
}
type pushPullHeader struct {
	Nodes        int
	UserStateLen int   
	Join         bool  
}
type pushNodeState struct {
	Name        string
	Addr        []byte
	Port        uint16
	Meta        []byte
	Incarnation uint32
	State       nodeStateType
	Vsn         []uint8  
}
type compress struct {
	Algo compressionType
	Buf  []byte
}
type msgHandoff struct {
	msgType messageType
	buf     []byte
	from    net.Addr
}
func (m *Memberlist) encryptionVersion() encryptionVersion {
	switch m.ProtocolVersion() {
	case 1:
		return 0
	default:
		return 1
	}
}
func setUDPRecvBuf(c *net.UDPConn) {
	size := udpRecvBuf
	for {
		if err := c.SetReadBuffer(size); err == nil {
			break
		}
		size = size / 2
	}
}
func (m *Memberlist) tcpListen() {
	for {
		conn, err := m.tcpListener.AcceptTCP()
		if err != nil {
			if m.shutdown {
				break
			}
			m.logger.Printf("[ERR] memberlist: Error accepting TCP connection: %s", err)
			continue
		}
		go m.handleConn(conn)
	}
}
func (m *Memberlist) handleConn(conn *net.TCPConn) {
	m.logger.Printf("[DEBUG] memberlist: Responding to push/pull sync with: %s", conn.RemoteAddr())
	defer conn.Close()
	metrics.IncrCounter([]string{"memberlist", "tcp", "accept"}, 1)
	join, remoteNodes, userState, err := m.readRemoteState(conn)
	if err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to receive remote state: %s", err)
		return
	}
	if err := m.sendLocalState(conn, join); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to push local state: %s", err)
	}
	if err := m.verifyProtocol(remoteNodes); err != nil {
		m.logger.Printf("[ERR] memberlist: Push/pull verification failed: %s", err)
		return
	}
	if join && m.config.Merge != nil {
		nodes := make([]*Node, len(remoteNodes))
		for idx, n := range remoteNodes {
			nodes[idx] = &Node{
				Name: n.Name,
				Addr: n.Addr,
				Port: n.Port,
				Meta: n.Meta,
				PMin: n.Vsn[0],
				PMax: n.Vsn[1],
				PCur: n.Vsn[2],
				DMin: n.Vsn[3],
				DMax: n.Vsn[4],
				DCur: n.Vsn[5],
			}
		}
		if m.config.Merge.NotifyMerge(nodes) {
			m.logger.Printf("[WARN] memberlist: Cluster merge canceled")
			return
		}
	}
	m.mergeState(remoteNodes)
	if m.config.Delegate != nil {
		m.config.Delegate.MergeRemoteState(userState, join)
	}
}
func (m *Memberlist) udpListen() {
	var n int
	var addr net.Addr
	var err error
	var lastPacket time.Time
	for {
		if !lastPacket.IsZero() && time.Now().Sub(lastPacket) > blockingWarning {
			diff := time.Now().Sub(lastPacket)
			m.logger.Printf(
				"[DEBUG] memberlist: Potential blocking operation. Last command took %v",
				diff)
		}
		buf := make([]byte, udpBufSize)
		n, addr, err = m.udpListener.ReadFrom(buf)
		if err != nil {
			if m.shutdown {
				break
			}
			m.logger.Printf("[ERR] memberlist: Error reading UDP packet: %s", err)
			continue
		}
		if n < 1 {
			m.logger.Printf("[ERR] memberlist: UDP packet too short (%d bytes). From: %s",
				len(buf), addr)
			continue
		}
		lastPacket = time.Now()
		metrics.IncrCounter([]string{"memberlist", "udp", "received"}, float32(n))
		m.ingestPacket(buf[:n], addr)
	}
}
func (m *Memberlist) ingestPacket(buf []byte, from net.Addr) {
	if m.config.EncryptionEnabled() {
		plain, err := decryptPayload(m.config.Keyring.GetKeys(), buf, nil)
		if err != nil {
			m.logger.Printf("[ERR] memberlist: Decrypt packet failed: %v", err)
			return
		}
		buf = plain
	}
	m.handleCommand(buf, from)
}
func (m *Memberlist) handleCommand(buf []byte, from net.Addr) {
	msgType := messageType(buf[0])
	buf = buf[1:]
	switch msgType {
	case compoundMsg:
		m.handleCompound(buf, from)
	case compressMsg:
		m.handleCompressed(buf, from)
	case pingMsg:
		m.handlePing(buf, from)
	case indirectPingMsg:
		m.handleIndirectPing(buf, from)
	case ackRespMsg:
		m.handleAck(buf, from)
	case suspectMsg:
		fallthrough
	case aliveMsg:
		fallthrough
	case deadMsg:
		fallthrough
	case userMsg:
		select {
		case m.handoff <- msgHandoff{msgType, buf, from}:
		default:
			m.logger.Printf("[WARN] memberlist: UDP handler queue full, dropping message (%d)", msgType)
		}
	default:
		m.logger.Printf("[ERR] memberlist: UDP msg type (%d) not supported. From: %s", msgType, from)
	}
}
func (m *Memberlist) udpHandler() {
	for {
		select {
		case msg := <-m.handoff:
			msgType := msg.msgType
			buf := msg.buf
			from := msg.from
			switch msgType {
			case suspectMsg:
				m.handleSuspect(buf, from)
			case aliveMsg:
				m.handleAlive(buf, from)
			case deadMsg:
				m.handleDead(buf, from)
			case userMsg:
				m.handleUser(buf, from)
			default:
				m.logger.Printf("[ERR] memberlist: UDP msg type (%d) not supported. From: %s (handler)", msgType, from)
			}
		case <-m.shutdownCh:
			return
		}
	}
}
func (m *Memberlist) handleCompound(buf []byte, from net.Addr) {
	trunc, parts, err := decodeCompoundMessage(buf)
	if err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode compound request: %s", err)
		return
	}
	if trunc > 0 {
		m.logger.Printf("[WARN] memberlist: Compound request had %d truncated messages", trunc)
	}
	for _, part := range parts {
		m.handleCommand(part, from)
	}
}
func (m *Memberlist) handlePing(buf []byte, from net.Addr) {
	var p ping
	if err := decode(buf, &p); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode ping request: %s", err)
		return
	}
	if p.Node != "" && p.Node != m.config.Name {
		m.logger.Printf("[WARN] memberlist: Got ping for unexpected node '%s'", p.Node)
		return
	}
	ack := ackResp{p.SeqNo}
	if err := m.encodeAndSendMsg(from, ackRespMsg, &ack); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to send ack: %s", err)
	}
}
func (m *Memberlist) handleIndirectPing(buf []byte, from net.Addr) {
	var ind indirectPingReq
	if err := decode(buf, &ind); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode indirect ping request: %s", err)
		return
	}
	if m.ProtocolVersion() < 2 || ind.Port == 0 {
		ind.Port = uint16(m.config.BindPort)
	}
	localSeqNo := m.nextSeqNo()
	ping := ping{SeqNo: localSeqNo, Node: ind.Node}
	destAddr := &net.UDPAddr{IP: ind.Target, Port: int(ind.Port)}
	respHandler := func() {
		ack := ackResp{ind.SeqNo}
		if err := m.encodeAndSendMsg(from, ackRespMsg, &ack); err != nil {
			m.logger.Printf("[ERR] memberlist: Failed to forward ack: %s", err)
		}
	}
	m.setAckHandler(localSeqNo, respHandler, m.config.ProbeTimeout)
	if err := m.encodeAndSendMsg(destAddr, pingMsg, &ping); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to send ping: %s", err)
	}
}
func (m *Memberlist) handleAck(buf []byte, from net.Addr) {
	var ack ackResp
	if err := decode(buf, &ack); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode ack response: %s", err)
		return
	}
	m.invokeAckHandler(ack.SeqNo)
}
func (m *Memberlist) handleSuspect(buf []byte, from net.Addr) {
	var sus suspect
	if err := decode(buf, &sus); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode suspect message: %s", err)
		return
	}
	m.suspectNode(&sus)
}
func (m *Memberlist) handleAlive(buf []byte, from net.Addr) {
	var live alive
	if err := decode(buf, &live); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode alive message: %s", err)
		return
	}
	if m.ProtocolVersion() < 2 || live.Port == 0 {
		live.Port = uint16(m.config.BindPort)
	}
	m.aliveNode(&live, nil, false)
}
func (m *Memberlist) handleDead(buf []byte, from net.Addr) {
	var d dead
	if err := decode(buf, &d); err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decode dead message: %s", err)
		return
	}
	m.deadNode(&d)
}
func (m *Memberlist) handleUser(buf []byte, from net.Addr) {
	d := m.config.Delegate
	if d != nil {
		d.NotifyMsg(buf)
	}
}
func (m *Memberlist) handleCompressed(buf []byte, from net.Addr) {
	payload, err := decompressPayload(buf)
	if err != nil {
		m.logger.Printf("[ERR] memberlist: Failed to decompress payload: %v", err)
		return
	}
	m.handleCommand(payload, from)
}
func (m *Memberlist) encodeAndSendMsg(to net.Addr, msgType messageType, msg interface{}) error {
	out, err := encode(msgType, msg)
	if err != nil {
		return err
	}
	if err := m.sendMsg(to, out.Bytes()); err != nil {
		return err
	}
	return nil
}
func (m *Memberlist) sendMsg(to net.Addr, msg []byte) error {
	bytesAvail := udpSendBuf - len(msg) - compoundHeaderOverhead
	if m.config.EncryptionEnabled() {
		bytesAvail -= encryptOverhead(m.encryptionVersion())
	}
	extra := m.getBroadcasts(compoundOverhead, bytesAvail)
	if len(extra) == 0 {
		return m.rawSendMsg(to, msg)
	}
	msgs := make([][]byte, 0, 1+len(extra))
	msgs = append(msgs, msg)
	msgs = append(msgs, extra...)
	compound := makeCompoundMessage(msgs)
	return m.rawSendMsg(to, compound.Bytes())
}
func (m *Memberlist) rawSendMsg(to net.Addr, msg []byte) error {
	if m.config.EnableCompression {
		buf, err := compressPayload(msg)
		if err != nil {
			m.logger.Printf("[WARN] memberlist: Failed to compress payload: %v", err)
		} else {
			if buf.Len() < len(msg) {
				msg = buf.Bytes()
			}
		}
	}
	if m.config.EncryptionEnabled() {
		var buf bytes.Buffer
		primaryKey := m.config.Keyring.GetPrimaryKey()
		err := encryptPayload(m.encryptionVersion(), primaryKey, msg, nil, &buf)
		if err != nil {
			m.logger.Printf("[ERR] memberlist: Encryption of message failed: %v", err)
			return err
		}
		msg = buf.Bytes()
	}
	metrics.IncrCounter([]string{"memberlist", "udp", "sent"}, float32(len(msg)))
	_, err := m.udpListener.WriteTo(msg, to)
	return err
}
func (m *Memberlist) sendAndReceiveState(addr []byte, port uint16, join bool) ([]pushNodeState, []byte, error) {
	dialer := net.Dialer{Timeout: m.config.TCPTimeout}
	dest := net.TCPAddr{IP: addr, Port: int(port)}
	conn, err := dialer.Dial("tcp", dest.String())
	if err != nil {
		return nil, nil, err
	}
	defer conn.Close()
	m.logger.Printf("[DEBUG] memberlist: Initiating push/pull sync with: %s", conn.RemoteAddr())
	metrics.IncrCounter([]string{"memberlist", "tcp", "connect"}, 1)
	if err := m.sendLocalState(conn, join); err != nil {
		return nil, nil, err
	}
	_, remote, userState, err := m.readRemoteState(conn)
	if err != nil {
		err := fmt.Errorf("Reading remote state failed: %v", err)
		return nil, nil, err
	}
	return remote, userState, nil
}
func (m *Memberlist) sendLocalState(conn net.Conn, join bool) error {
	conn.SetDeadline(time.Now().Add(m.config.TCPTimeout))
	m.nodeLock.RLock()
	localNodes := make([]pushNodeState, len(m.nodes))
	for idx, n := range m.nodes {
		localNodes[idx].Name = n.Name
		localNodes[idx].Addr = n.Addr
		localNodes[idx].Port = n.Port
		localNodes[idx].Incarnation = n.Incarnation
		localNodes[idx].State = n.State
		localNodes[idx].Meta = n.Meta
		localNodes[idx].Vsn = []uint8{
			n.PMin, n.PMax, n.PCur,
			n.DMin, n.DMax, n.DCur,
		}
	}
	m.nodeLock.RUnlock()
	var userData []byte
	if m.config.Delegate != nil {
		userData = m.config.Delegate.LocalState(join)
	}
	bufConn := bytes.NewBuffer(nil)
	header := pushPullHeader{Nodes: len(localNodes), UserStateLen: len(userData), Join: join}
	hd := codec.MsgpackHandle{}
	enc := codec.NewEncoder(bufConn, &hd)
	if _, err := bufConn.Write([]byte{byte(pushPullMsg)}); err != nil {
		return err
	}
	if err := enc.Encode(&header); err != nil {
		return err
	}
	for i := 0; i < header.Nodes; i++ {
		if err := enc.Encode(&localNodes[i]); err != nil {
			return err
		}
	}
	if userData != nil {
		if _, err := bufConn.Write(userData); err != nil {
			return err
		}
	}
	sendBuf := bufConn.Bytes()
	if m.config.EnableCompression {
		compBuf, err := compressPayload(bufConn.Bytes())
		if err != nil {
			m.logger.Printf("[ERROR] memberlist: Failed to compress local state: %v", err)
		} else {
			sendBuf = compBuf.Bytes()
		}
	}
	if m.config.EncryptionEnabled() {
		crypt, err := m.encryptLocalState(sendBuf)
		if err != nil {
			m.logger.Printf("[ERROR] memberlist: Failed to encrypt local state: %v", err)
			return err
		}
		sendBuf = crypt
	}
	metrics.IncrCounter([]string{"memberlist", "tcp", "sent"}, float32(len(sendBuf)))
	if _, err := conn.Write(sendBuf); err != nil {
		return err
	}
	return nil
}
func (m *Memberlist) encryptLocalState(sendBuf []byte) ([]byte, error) {
	var buf bytes.Buffer
	buf.WriteByte(byte(encryptMsg))
	sizeBuf := make([]byte, 4)
	encVsn := m.encryptionVersion()
	encLen := encryptedLength(encVsn, len(sendBuf))
	binary.BigEndian.PutUint32(sizeBuf, uint32(encLen))
	buf.Write(sizeBuf)
	key := m.config.Keyring.GetPrimaryKey()
	err := encryptPayload(encVsn, key, sendBuf, buf.Bytes()[:5], &buf)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
func (m *Memberlist) decryptRemoteState(bufConn io.Reader) ([]byte, error) {
	cipherText := bytes.NewBuffer(nil)
	cipherText.WriteByte(byte(encryptMsg))
	_, err := io.CopyN(cipherText, bufConn, 4)
	if err != nil {
		return nil, err
	}
	moreBytes := binary.BigEndian.Uint32(cipherText.Bytes()[1:5])
	if moreBytes > maxPushStateBytes {
		return nil, fmt.Errorf("Remote node state is larger than limit (%d)", moreBytes)
	}
	_, err = io.CopyN(cipherText, bufConn, int64(moreBytes))
	if err != nil {
		return nil, err
	}
	dataBytes := cipherText.Bytes()[:5]
	cipherBytes := cipherText.Bytes()[5:]
	keys := m.config.Keyring.GetKeys()
	return decryptPayload(keys, cipherBytes, dataBytes)
}