Search in sources :

Example 1 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class CurveServerMechanism method encode.

@Override
public Msg encode(Msg msg) {
    assert (state == State.CONNECTED);
    byte flags = 0;
    if (msg.hasMore()) {
        flags |= 0x01;
    }
    if (msg.isCommand()) {
        flags |= 0x02;
    }
    ByteBuffer messageNonce = ByteBuffer.allocate(Curve.Size.NONCE.bytes());
    messageNonce.put("CurveZMQMESSAGES".getBytes(ZMQ.CHARSET));
    Wire.putUInt64(messageNonce, cnNonce);
    int mlen = Curve.Size.ZERO.bytes() + 1 + msg.size();
    ByteBuffer messagePlaintext = ByteBuffer.allocate(mlen);
    messagePlaintext.put(Curve.Size.ZERO.bytes(), flags);
    messagePlaintext.position(Curve.Size.ZERO.bytes() + 1);
    msg.transfer(messagePlaintext, 0, msg.size());
    ByteBuffer messageBox = ByteBuffer.allocate(mlen);
    int rc = cryptoBox.afternm(messageBox, messagePlaintext, mlen, messageNonce, cnPrecom);
    assert (rc == 0);
    Msg encoded = new Msg(16 + mlen - Curve.Size.BOXZERO.bytes());
    encoded.putShortString("MESSAGE");
    encoded.put(messageNonce, 16, 8);
    encoded.put(messageBox, Curve.Size.BOXZERO.bytes(), mlen - Curve.Size.BOXZERO.bytes());
    cnNonce++;
    return encoded;
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class CurveServerMechanism method decode.

@Override
public Msg decode(Msg msg) {
    assert (state == State.CONNECTED);
    if (!compare(msg, "MESSAGE", true)) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
        errno.set(ZError.EPROTO);
        return null;
    }
    if (msg.size() < 33) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_MESSAGE);
        errno.set(ZError.EPROTO);
        return null;
    }
    ByteBuffer messageNonce = ByteBuffer.allocate(Curve.Size.NONCE.bytes());
    messageNonce.put("CurveZMQMESSAGEC".getBytes(ZMQ.CHARSET));
    msg.transfer(messageNonce, 8, 8);
    long nonce = msg.getLong(8);
    if (nonce <= cnPeerNonce) {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_SEQUENCE);
        errno.set(ZError.EPROTO);
        return null;
    }
    cnPeerNonce = nonce;
    int clen = Curve.Size.BOXZERO.bytes() + msg.size() - 16;
    ByteBuffer messagePlaintext = ByteBuffer.allocate(clen);
    ByteBuffer messageBox = ByteBuffer.allocate(clen);
    messageBox.position(Curve.Size.BOXZERO.bytes());
    msg.transfer(messageBox, 16, msg.size() - 16);
    int rc = cryptoBox.openAfternm(messagePlaintext, messageBox, clen, messageNonce, cnPrecom);
    if (rc == 0) {
        Msg decoded = new Msg(clen - 1 - Curve.Size.ZERO.bytes());
        byte flags = messagePlaintext.get(Curve.Size.ZERO.bytes());
        if ((flags & 0x01) != 0) {
            decoded.setFlags(Msg.MORE);
        }
        if ((flags & 0x02) != 0) {
            decoded.setFlags(Msg.COMMAND);
        }
        messagePlaintext.position(Curve.Size.ZERO.bytes() + 1);
        decoded.put(messagePlaintext);
        return decoded;
    } else {
        session.getSocket().eventHandshakeFailedProtocol(session.getEndpoint(), ZMQ.ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
        errno.set(ZError.EPROTO);
        return null;
    }
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class PlainServerMechanism method sendZapRequest.

private void sendZapRequest(byte[] username, byte[] password) {
    sendZapRequest(Mechanisms.PLAIN, true);
    // Username frame
    Msg msg = new Msg(username.length);
    msg.setFlags(Msg.MORE);
    msg.put(username);
    boolean rc = session.writeZapMsg(msg);
    assert (rc);
    // Password frame
    msg = new Msg(password.length);
    msg.put(password);
    rc = session.writeZapMsg(msg);
    assert (rc);
}
Also used : Msg(zmq.Msg)

Example 4 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class Mechanism method peerIdentity.

public final Msg peerIdentity() {
    byte[] data = new byte[0];
    int size = 0;
    if (identity != null) {
        data = identity.data();
        size = identity.size();
    }
    Msg msg = new Msg(size);
    msg.put(data, 0, size);
    msg.setFlags(Msg.IDENTITY);
    return msg;
}
Also used : Msg(zmq.Msg)

Example 5 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class CurveClientMechanism method encode.

@Override
public Msg encode(Msg msg) {
    assert (state == State.CONNECTED);
    byte flags = 0;
    if (msg.hasMore()) {
        flags |= 0x01;
    }
    if (msg.isCommand()) {
        flags |= 0x02;
    }
    ByteBuffer messageNonce = ByteBuffer.allocate(Curve.Size.NONCE.bytes());
    messageNonce.put("CurveZMQMESSAGEC".getBytes(ZMQ.CHARSET));
    Wire.putUInt64(messageNonce, cnNonce);
    int mlen = Curve.Size.ZERO.bytes() + 1 + msg.size();
    ByteBuffer messagePlaintext = ByteBuffer.allocate(mlen);
    messagePlaintext.put(Curve.Size.ZERO.bytes(), flags);
    messagePlaintext.position(Curve.Size.ZERO.bytes() + 1);
    msg.transfer(messagePlaintext, 0, msg.size());
    ByteBuffer messageBox = ByteBuffer.allocate(mlen);
    int rc = cryptoBox.afternm(messageBox, messagePlaintext, mlen, messageNonce, cnPrecom);
    assert (rc == 0);
    Msg encoded = new Msg(16 + mlen - Curve.Size.BOXZERO.bytes());
    encoded.putShortString("MESSAGE");
    encoded.put(messageNonce, 16, 8);
    encoded.put(messageBox, Curve.Size.BOXZERO.bytes(), mlen - Curve.Size.BOXZERO.bytes());
    cnNonce++;
    return encoded;
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1