public override void WriteToBitcoinBuffer(byte[] buffer, int offset) { ((uint)Network).WriteBytes(buffer, offset); Encoding.ASCII.GetBytes(Command, 0, Command.Length < MAX_COMMAND_LENGTH ? Command.Length : MAX_COMMAND_LENGTH, buffer, Command_Offset(ref offset)); PayloadLength.WriteBytes(buffer, PayloadLength_Offset(ref offset)); if (Payload.IncludeChecksum) { Checksum.WriteBytes(buffer, Checksum_Offset(ref offset)); } Payload.WriteToBitcoinBuffer(buffer, Payload_Offset(ref offset)); }
public Packet(byte[] byteArray) { if (byteArray != null) { if (_log.IsInfoEnabled) { _log.Info("~~~~~~~~~~~~~~~~Starting Packet Creation.~~~~~~~~~~~~~~~~~~~~~~~~"); } if (_log.IsInfoEnabled) { _log.InfoFormat("{0}--{2} bytes in: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(byteArray), byteArray.Length.ToString()); } ID = BitConverter.ToUInt32(byteArray, 0); //Len = 4 if (ID != Connector.StandardID) { if (ThrowWhenInvalid) { throw new InvalidPacketException(); } else { return; } } if (_log.IsInfoEnabled) { _log.InfoFormat("ID={0}", ID.ToString()); } Length = BitConverter.ToInt32(byteArray, 4);//Len = 4 if (_log.IsInfoEnabled) { _log.InfoFormat("Length={0}", Length.ToString()); } Origin = (OriginType)BitConverter.ToInt32(byteArray, 8);//Len = 4 if (_log.IsInfoEnabled) { _log.InfoFormat("Origin={0}", Origin.ToString()); } Unknown = BitConverter.ToInt32(byteArray, 12);//Len = 4 if (_log.IsInfoEnabled) { _log.InfoFormat("Unknown={0}", Unknown.ToString()); } PayloadLength = BitConverter.ToInt32(byteArray, 16);//Len = 4 if (_log.IsInfoEnabled) { _log.InfoFormat("RemainingPacketLength={0}", PayloadLength.ToString()); } PacketType = (PacketTypes)BitConverter.ToUInt32(byteArray, 20);//Len = 4 if (_log.IsInfoEnabled) { _log.InfoFormat("PacketType={0}", PacketType.ToString()); } List <byte> newArray = new List <byte>(); int ln = byteArray.Length; if (ln > Length) { ln = Length; } for (int i = HeaderLength; i < ln; i++) { newArray.Add(byteArray[i]); } Payload = newArray.ToArray(); _package = GetPackage(Payload); int packetLength = 0; if (_package != null) { byte[] packetBytes = _package.GetBytes(); packetLength = packetBytes.Length; } if (packetLength + HeaderLength != Length && ThrowWhenInvalid) { throw new InvalidPacketException(); } if (_log.IsInfoEnabled) { _log.InfoFormat("{0}--{2} Result bytes: {1}", MethodBase.GetCurrentMethod().ToString(), Utility.BytesToDebugString(this.GetBytes()), this.GetBytes().Length); } if (_log.IsInfoEnabled) { _log.Info("~~~~~~~~~~~~~~~~Packet Creation Ended.~~~~~~~~~~~~~~~~~~~~~~~~"); } } }
private int Checksum_Offset(ref int offset) { return(offset += (int)PayloadLength.ByteSize()); }
public Message(byte[] buffer, int offset) : base(buffer, offset) { Contract.Requires <ArgumentNullException>(buffer != null, "buffer"); Contract.Requires <ArgumentException>(buffer.Length >= Message.MinimumByteSize, "buffer"); Contract.Requires <ArgumentOutOfRangeException>(offset >= 0, "offset"); Contract.Requires <ArgumentOutOfRangeException>(offset <= buffer.Length - Message.MinimumByteSize, "offset"); Network = (MessageNetwork)buffer.ReadUInt32(offset); Command = Encoding.ASCII.GetString(buffer, Command_Offset(ref offset), MAX_COMMAND_LENGTH).TrimEnd('\0'); PayloadLength = buffer.ReadUInt32(PayloadLength_Offset(ref offset)); ByteSize = ((uint)Network).ByteSize() + MAX_COMMAND_LENGTH * BufferOperations.UINT8_SIZE + PayloadLength.ByteSize(); if (MessagePayloadFactory.PayloadRequiresChecksum(Command)) { Checksum = buffer.ReadUInt32(Checksum_Offset(ref offset)); ByteSize += Checksum.ByteSize(); } Payload = MessagePayloadFactory.ConstructMessagePayload(Command, buffer, Payload_Offset(ref offset), PayloadLength); ByteSize += Payload.ByteSize; }
public override string ToString() { return(OpCode.ToString() + ": " + PayloadLength.ToString() + " bytes (" + Flags.ToString() + ")"); }
/// <summary cref="Packet.ToString(StringOutputType)" /> public override string ToString(StringOutputType outputFormat) { var buffer = new StringBuilder(); string color = ""; string colorEscape = ""; if (outputFormat == StringOutputType.Colored || outputFormat == StringOutputType.VerboseColored) { color = Color; colorEscape = AnsiEscapeSequences.Reset; } if (outputFormat == StringOutputType.Normal || outputFormat == StringOutputType.Colored) { // build the output string buffer.AppendFormat("{0}[IPv6Packet: SourceAddress={2}, DestinationAddress={3}, NextHeader={4}]{1}", color, colorEscape, SourceAddress, DestinationAddress, NextHeader); } if (outputFormat == StringOutputType.Verbose || outputFormat == StringOutputType.VerboseColored) { // collect the properties and their value Dictionary <string, string> properties = new Dictionary <string, string>(); string ipVersion = Convert.ToString((int)Version, 2).PadLeft(4, '0'); properties.Add("version", ipVersion + " .... .... .... .... .... .... .... = " + (int)Version); string trafficClass = Convert.ToString(TrafficClass, 2).PadLeft(8, '0').Insert(4, " "); properties.Add("traffic class", ".... " + trafficClass + " .... .... .... .... .... = 0x" + TrafficClass.ToString("x").PadLeft(8, '0')); string flowLabel = Convert.ToString(FlowLabel, 2).PadLeft(20, '0').Insert(16, " ").Insert(12, " ").Insert(8, " ").Insert(4, " "); properties.Add("flow label", ".... .... .... " + flowLabel + " = 0x" + FlowLabel.ToString("x").PadLeft(8, '0')); properties.Add("payload length", PayloadLength.ToString()); properties.Add("next header", NextHeader.ToString() + " (0x" + NextHeader.ToString("x") + ")"); properties.Add("hop limit", HopLimit.ToString()); properties.Add("source", SourceAddress.ToString()); properties.Add("destination", DestinationAddress.ToString()); // calculate the padding needed to right-justify the property names int padLength = Utils.RandomUtils.LongestStringLength(new List <string>(properties.Keys)); // build the output string buffer.AppendLine("IP: ******* IP - \"Internet Protocol (Version 6)\" - offset=? length=" + TotalPacketLength); buffer.AppendLine("IP:"); foreach (var property in properties) { if (property.Key.Trim() != "") { buffer.AppendLine("IP: " + property.Key.PadLeft(padLength) + " = " + property.Value); } else { buffer.AppendLine("IP: " + property.Key.PadLeft(padLength) + " " + property.Value); } } buffer.AppendLine("IP"); } // append the base class output buffer.Append(base.ToString(outputFormat)); return(buffer.ToString()); }