public ReloadMessage(ReloadConfig rc, NodeId OriginatorNodeID, Destination destination, UInt64 trans_id, RELOAD_MessageBody reload_content) { m_ReloadConfig = rc; forwarding_header.version = ReloadGlobals.RELOAD_VERSION; forwarding_header.ttl = ReloadGlobals.TTL; forwarding_header.overlay = m_ReloadConfig.OverlayHash; forwarding_header.transaction_id = trans_id; reload_message_body = reload_content; forwarding_header.via_list = new List<Destination>(); forwarding_header.via_list.Add(new Destination(OriginatorNodeID)); forwarding_header.destination_list = new List<Destination>(); forwarding_header.destination_list.Add(destination); SignerIdentity myId = m_ReloadConfig.AccessController.MyIdentity; security_block = new SecurityBlock(rc, myId); /* Sign the message, create stream of body */ security_block.SignMessage(m_ReloadConfig.OverlayHash, trans_id.ToString(), reload_message_body); }
} // End ToBytes() public ReloadMessage FromBytes(byte[] bytes, ref long offset, ReadFlags flags) { if (bytes == null) { m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "FromBytes: bytes = null!!"); return null; } if (offset != 0) offset = 0; var ms = new MemoryStream(bytes, (int)offset, (int)(bytes.Count() - offset)); //prepare variable for return (ref value) offset = 0; using (var reader = new BinaryReader(ms)) { try { UInt32 RELO_Tag; //first check if this is a RELOAD message by checking th RELO tag RELO_Tag = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32()); if (RELO_Tag != RELOTAG) { m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, String.Format("==> message has no RELO tag")); return null; } forwarding_header.overlay = (UInt32)IPAddress.NetworkToHostOrder( reader.ReadInt32()); if (forwarding_header.overlay != m_ReloadConfig.OverlayHash) throw new System.Exception("Message from wrong overlay! (probably invalid hash)"); /* configuration_sequence */ forwarding_header.configuration_sequence = (UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); /* version */ forwarding_header.version = reader.ReadByte(); /* ttl */ forwarding_header.ttl = reader.ReadByte(); /* fragment */ forwarding_header.fragment = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32()); /* length */ forwarding_header.length = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32()); // obsolete -> auto fragmentation if message too large //if (forwarding_header.length >= ReloadGlobals.MAX_PACKET_BUFFER_SIZE) { // m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, // String.Format("==> forwarding header exceeds maximum value of {0}", // ReloadGlobals.MAX_PACKET_BUFFER_SIZE)); // return null; //} /* transaction_id */ forwarding_header.transaction_id = (UInt64)IPAddress.NetworkToHostOrder(reader.ReadInt64()); /* max_response_length */ forwarding_header.max_response_length = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32()); /* via list length */ forwarding_header.via_list_length = (UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); /* destination list length */ forwarding_header.destination_list_length = (UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); /* options length */ forwarding_header.options_length = (UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); if (forwarding_header.via_list_length != 0) forwarding_header.via_list = ReadDestList(reader, forwarding_header.via_list_length); if (forwarding_header.destination_list_length != 0) forwarding_header.destination_list = ReadDestList(reader, forwarding_header.destination_list_length); if (forwarding_header.options_length != 0) forwarding_header.fw_options = ReadOptionList(reader, forwarding_header.options_length); long reload_msg_begin = ms.Position; if (0 != (forwarding_header.fragment & 0x80000000)) { //is this a fragment? uint fragment_offset = (forwarding_header.fragment & 0x3FFFFFFF); bool last_fragment = ((forwarding_header.fragment & 0x40000000) != 0); if (fragment_offset == 0 && last_fragment == true) { //single fragment message (means not fragmented) => process as usual } else { m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_FRAGMENTATION, String.Format("Fragmented Message: offset " + fragment_offset + "READ bytes " + (forwarding_header.length - reload_msg_begin)) + " left " + (reader.BaseStream.Length - ms.Position)); reload_message_body = new MessageFragment(RELOAD_MessageCode.Invalid, fragment_offset, last_fragment).FromReader(this, reader, forwarding_header.length - reload_msg_begin); //this should be the fragmentsize instead of ms.Length better use forwardingheader.length TODO: FIX it offset = ms.Position; return this; } } // now turn to message body RELOAD_MessageCode MsgCode = (RELOAD_MessageCode)(UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); // now read message length long reload_msg_size = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32()); /* set pointer before msg code that the message routines itself * can read it again */ ms.Seek(reload_msg_begin, SeekOrigin.Begin); switch (MsgCode) { case RELOAD_MessageCode.Probe_Request: case RELOAD_MessageCode.Probe_Answer: break; case RELOAD_MessageCode.Attach_Request: case RELOAD_MessageCode.Attach_Answer: reload_message_body = new AttachReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Store_Request: reload_message_body = new StoreReq( m_ReloadConfig.ThisMachine.UsageManager).FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Store_Answer: reload_message_body = new StoreAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Fetch_Request: reload_message_body = new FetchReq(m_ReloadConfig.ThisMachine.UsageManager).FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Fetch_Answer: reload_message_body = new FetchAns(m_ReloadConfig.ThisMachine.UsageManager).FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Remove_Request: case RELOAD_MessageCode.Remove_Answer: //reload_message_body = new RemoveReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Find_Request: case RELOAD_MessageCode.Find_Answer: break; case RELOAD_MessageCode.Join_Request: case RELOAD_MessageCode.Join_Answer: reload_message_body = new JoinReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Leave_Request: case RELOAD_MessageCode.Leave_Answer: reload_message_body = new LeaveReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Update_Request: case RELOAD_MessageCode.Update_Answer: reload_message_body = new UpdateReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Route_Query_Request: case RELOAD_MessageCode.Route_Query_Answer: break; case RELOAD_MessageCode.Ping_Request: reload_message_body = new PingReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Ping_Answer: reload_message_body = new PingReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Stat_Request: case RELOAD_MessageCode.Stat_Answer: break; case RELOAD_MessageCode.App_Attach_Request: case RELOAD_MessageCode.App_Attach_Answer: reload_message_body = new AppAttachReqAns().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Error: reload_message_body = new ErrorResponse().FromReader(this, reader, reload_msg_size); break; case RELOAD_MessageCode.Unused: case RELOAD_MessageCode.Unused2: default: throw new System.Exception(String.Format("Invalid RELOAD message type {0}", MsgCode)); } if (reload_message_body != null) reload_message_body.RELOAD_MsgCode = MsgCode; // now read message extension length UInt32 message_extension_size = (UInt32)IPAddress.NetworkToHostOrder( reader.ReadInt32()); if (message_extension_size != 0) { //skip message extension length reader.ReadBytes((int)message_extension_size); } if (ReloadGlobals.TLS) { /* Obtain security block */ security_block = new SecurityBlock(m_ReloadConfig).FromReader(reader, reload_msg_size); } else { // do the FAKE int iNodeIDLen = reader.ReadByte(); if (iNodeIDLen != 0) { NodeId nodeid = new NodeId(reader.ReadBytes(iNodeIDLen)); if (flags != ReadFlags.no_certcheck) { if (ReloadGlobals.TLS && nodeid != security_block.OriginatorNodeID) throw new System.Exception("Wrong message Originator!"); security_block.OriginatorNodeID = nodeid; } } else throw new System.Exception("Originator of message cannot be read!"); int iLHNodeIDLen = reader.ReadByte(); if (iLHNodeIDLen != 0) LastHopNodeId = new NodeId(reader.ReadBytes(iLHNodeIDLen)); } offset = ms.Position; return this; } // End mok security catch (Exception ex) { m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "ReloadMesssage.FromBytes(): " + ex.Message); return null; } } }