Extend ZTP/1.0 protocol

The new protocol adds support for protocol version and  exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.

The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.

When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:

    greeting    = tag1, adaptation, tag2, version, length, socket_type
    tag1        = BYTE / 0xff
    adaptation  = 8 BYTES
    tag2        = BYTE / 0x7f
    version     = BYTE / 1
    length      = BYTE / 1
    socket_type = BYTE

The protocol does not define the value of adaptation field.

When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.

This patch supports both ZTP/1.0 and new protocol.
This commit is contained in:
Martin Hurton
2012-09-01 13:59:22 +02:00
parent 6347f8b0c9
commit 1bca4f6f03
2 changed files with 185 additions and 2 deletions

View File

@@ -67,6 +67,12 @@ namespace zmq
// Function to handle network disconnections.
void error ();
// Receives the greeting message from the peer.
int receive_greeting ();
// Detects the protocol used by the peer.
bool handshake ();
// Writes data to the socket. Returns the number of bytes actually
// written (even zero is to be considered to be a success). In case
// of error or orderly shutdown by the other peer -1 is returned.
@@ -81,6 +87,16 @@ namespace zmq
// Underlying socket.
fd_t s;
// Maximum size of a greeting message:
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
// up to 255 remaining bytes.
const static size_t maximum_greeting_size = 10 + 1 + 1 + 255;
// Size of v1 greeting message:
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
// socket_type (1)
const static size_t v1_greeting_size = 10 + 1 + 1 + 1;
handle_t handle;
unsigned char *inpos;
@@ -92,6 +108,26 @@ namespace zmq
size_t outsize;
encoder_t encoder;
// When true, we are still trying to determine whether
// the peer is using versioned protocol, and if so, which
// version. When false, normal message flow has started.
bool handshaking;
// The receive buffer holding the greeting message
// that we are receiving from the peer.
unsigned char greeting [maximum_greeting_size];
// The number of bytes of the greeting message that
// we have already received.
unsigned int greeting_bytes_read;
// The size of the greeting message.
unsigned int greeting_size;
// The send buffer holding the greeting message
// that we are sending to the peer.
unsigned char greeting_output_buffer [v1_greeting_size];
// The session this engine is attached to.
zmq::session_base_t *session;