private void encryptAndSend(Message msg) throws Exception { EncryptHeader hdr = new EncryptHeader(EncryptHeader.ENCRYPT, getSymVersion()); if (this.encrypt_entire_message) hdr.type |= EncryptHeader.ENCRYPT_ENTIRE_MSG; if (encrypt_entire_message) { if (msg.getSrc() == null) msg.setSrc(local_addr); Buffer serialized_msg = Util.streamableToBuffer(msg); byte[] encrypted_msg = code( serialized_msg.getBuf(), serialized_msg.getOffset(), serialized_msg.getLength(), false); // exclude existing headers, they will be seen again when we decrypt and unmarshal the msg at // the receiver Message tmp = msg.copy(false, false).setBuffer(encrypted_msg).putHeader(this.id, hdr); down_prot.down(new Event(Event.MSG, tmp)); return; } // copy neeeded because same message (object) may be retransmitted -> no double encryption Message msgEncrypted = msg.copy(false) .putHeader(this.id, hdr) .setBuffer(code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), false)); down_prot.down(new Event(Event.MSG, msgEncrypted)); }
/** Send a message to the address specified in dest */ void sendUdpMessage(Message msg) throws Exception { IpAddress dest; Message copy; Event evt; dest = (IpAddress) msg.getDest(); // guaranteed not to be null setSourceAddress(msg); if (Trace.debug) { Trace.debug( "UDP.sendUdpMessage()", "sending message to " + msg.getDest() + " (src=" + msg.getSrc() + "), headers are " + msg.getHeaders()); // Don't send if destination is local address. Instead, switch dst and src and put in // up_queue. // If multicast message, loopback a copy directly to us (but still multicast). Once we receive // this, // we will discard our own multicast message } if (loopback && (dest.equals(local_addr) || dest.isMulticastAddress())) { copy = msg.copy(); copy.removeHeader(name); copy.setSrc(local_addr); copy.setDest(dest); evt = new Event(Event.MSG, copy); /* Because Protocol.up() is never called by this bottommost layer, we call up() directly in the observer. This allows e.g. PerfObserver to get the time of reception of a message */ if (observer != null) { observer.up(evt, up_queue.size()); } if (Trace.debug) { Trace.info("UDP.sendUdpMessage()", "looped back local message " + copy); } passUp(evt); if (!dest.isMulticastAddress()) { return; } } if (use_outgoing_packet_handler) { outgoing_queue.add(msg); return; } send(msg); }
private Message _decrypt(final Cipher cipher, Message msg, boolean decrypt_entire_msg) throws Exception { byte[] decrypted_msg; if (cipher == null) decrypted_msg = code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), true); else decrypted_msg = cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength()); if (!decrypt_entire_msg) { msg.setBuffer(decrypted_msg); return msg; } Message ret = Util.streamableFromBuffer(Message.class, decrypted_msg, 0, decrypted_msg.length); if (ret.getDest() == null) ret.setDest(msg.getDest()); if (ret.getSrc() == null) ret.setSrc(msg.getSrc()); return ret; }
public Message makeReply() { Message retval = new Message(src_addr); if (dest_addr != null) retval.setSrc(dest_addr); return retval; }
/** * If the sender is null, set our own address. We cannot just go ahead and set the address anyway, * as we might be sending a message on behalf of someone else ! E.g. in case of retransmission, * when the original sender has crashed, or in a FLUSH protocol when we have to return all * unstable messages with the FLUSH_OK response. */ void setSourceAddress(Message msg) { if (msg.getSrc() == null) { msg.setSrc(local_addr); } }