use of zmq.Msg in project jeromq by zeromq.
the class CurveClientMechanism 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("CurveZMQMESSAGES".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_CRYPTOGRAPHIC);
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;
}
}
use of zmq.Msg in project jeromq by zeromq.
the class Router method identifyPeer.
private boolean identifyPeer(Pipe pipe, boolean isLocallyInitiated) {
Blob identity;
if (connectRid != null && !connectRid.isEmpty() && isLocallyInitiated) {
identity = Blob.createBlob(connectRid.getBytes(ZMQ.CHARSET));
connectRid = null;
Outpipe outpipe = outpipes.get(identity);
// Not allowed to duplicate an existing rid
assert (outpipe == null);
} else {
if (options.rawSocket) {
// Always assign identity for raw-socket
ByteBuffer buffer = ByteBuffer.allocate(5);
buffer.put((byte) 0);
Wire.putUInt32(buffer, nextRid++);
identity = Blob.createBlob(buffer.array());
} else {
// Pick up handshake cases and also case where next identity is set
Msg msg = pipe.read();
if (msg == null) {
return false;
}
if (msg.size() == 0) {
// Fall back on the auto-generation
ByteBuffer buf = ByteBuffer.allocate(5);
buf.put((byte) 0);
Wire.putUInt32(buf, nextRid++);
identity = Blob.createBlob(buf.array());
} else {
identity = Blob.createBlob(msg);
if (outpipes.containsKey(identity)) {
if (!handover) {
// Ignore peers with duplicate ID
return false;
}
// We will allow the new connection to take over this
// identity. Temporarily assign a new identity to the
// existing pipe so we can terminate it asynchronously.
ByteBuffer buf = ByteBuffer.allocate(5);
buf.put((byte) 0);
Wire.putUInt32(buf, nextRid++);
Blob newIdentity = Blob.createBlob(buf.array());
// Remove the existing identity entry to allow the new
// connection to take the identity.
Outpipe existingOutpipe = outpipes.remove(identity);
existingOutpipe.pipe.setIdentity(newIdentity);
outpipes.put(newIdentity, existingOutpipe);
existingOutpipe.pipe.terminate(true);
}
}
}
}
pipe.setIdentity(identity);
// Add the record into output pipes lookup table
Outpipe outpipe = new Outpipe(pipe, true);
outpipes.put(identity, outpipe);
return true;
}
use of zmq.Msg in project jeromq by zeromq.
the class Server method xrecv.
@Override
protected Msg xrecv() {
Msg msg;
ValueReference<Pipe> pipe = new ValueReference<>();
msg = fq.recvPipe(errno, pipe);
// Drop any messages with more flag
while (msg != null && msg.hasMore()) {
// drop all frames of the current multi-frame message
msg = fq.recvPipe(errno, null);
while (msg != null && msg.hasMore()) {
msg = fq.recvPipe(errno, null);
}
// get the new message
if (msg != null) {
msg = fq.recvPipe(errno, pipe);
}
}
if (msg == null) {
return msg;
}
assert (pipe.get() != null);
int routingId = pipe.get().getRoutingId();
msg.setRoutingId(routingId);
return msg;
}
use of zmq.Msg in project jeromq by zeromq.
the class Sub method xsetsockopt.
@Override
public boolean xsetsockopt(int option, Object optval) {
if (option != ZMQ.ZMQ_SUBSCRIBE && option != ZMQ.ZMQ_UNSUBSCRIBE) {
errno.set(ZError.EINVAL);
return false;
}
if (optval == null) {
errno.set(ZError.EINVAL);
return false;
}
byte[] val = Options.parseBytes(option, optval);
// Create the subscription message.
Msg msg = new Msg(val.length + 1);
if (option == ZMQ.ZMQ_SUBSCRIBE) {
msg.put((byte) 1);
} else {
// option = ZMQ.ZMQ_UNSUBSCRIBE
msg.put((byte) 0);
}
msg.put(val);
// Pass it further on in the stack.
boolean rc = super.xsend(msg);
if (!rc) {
errno.set(ZError.EINVAL);
throw new IllegalStateException("Failed to send subscribe/unsubscribe message");
}
return true;
}
use of zmq.Msg in project jeromq by zeromq.
the class Dish method xrecv.
@Override
protected Msg xrecv() {
// return it straight ahead.
if (pendingMsg != null) {
Msg msg = pendingMsg;
pendingMsg = null;
return msg;
}
return xxrecv();
}
Aggregations