internal Header(ByteReader br) { _transactionID = br.ReadUInt16(); _flags = br.ReadUInt16(); _numQuestions = br.ReadUInt16(); _numAnswers = br.ReadUInt16(); _numAuthorities = br.ReadUInt16(); _numAdditionals = br.ReadUInt16(); }
/// <summary> /// Initializes a new instance of the <see cref="DhcpMessage"/> class. /// <param name="data">Array containing the data to decode.</param> /// </summary> public DhcpMessage(byte[] data) { ByteReader byteReader = new ByteReader(data, ByteOrder.Network); _timestamp = DateTime.Now; _op = byteReader.ReadByte(); _htype = byteReader.ReadByte(); _hlen = byteReader.ReadByte(); _hops = byteReader.ReadByte(); _xid = byteReader.ReadUInt32(); _secs = byteReader.ReadUInt16(); _flags = byteReader.ReadBytes(2); _ciaddr = byteReader.ReadBytes(4); _yiaddr = byteReader.ReadBytes(4); _siaddr = byteReader.ReadBytes(4); _giaddr = byteReader.ReadBytes(4); _chaddr = byteReader.ReadBytes(16); _sname = byteReader.ReadBytes(64); _file = byteReader.ReadBytes(128); byte[] rawOptions = byteReader.ReadBytes(byteReader.AvailableBytes); int offset = 0; // if magic number is valid then process options if (offset + 4 < rawOptions.Length && (BitConverter.ToUInt32(rawOptions, offset) == Constants.DHCP_OPTIONS_MAGIC_NUMBER || BitConverter.ToUInt32(rawOptions, offset) == Constants.DHCP_WIN_OPTIONS_MAGIC_NUMBER)) { offset += 4; Boolean end = false; while (!end && offset < rawOptions.Length) { DhcpOption option = (DhcpOption)rawOptions[offset]; offset++; int optionLen; switch (option) { case DhcpOption.Pad: continue; case DhcpOption.End: end = true; continue; default: optionLen = (int)rawOptions[offset]; offset++; break; } byte[] optionData = new byte[optionLen]; Array.Copy(rawOptions, offset, optionData, 0, optionLen); offset += optionLen; this._options.Add(option, optionData); this._optionDataSize += optionLen; } } }
/// <summary> /// Initializes a new instance of the <see cref="SntpMessage"/> class. /// /// <param name="data">Array containing the data to decode.</param> /// </summary> public SntpMessage(byte[] data) { if (data.Length < Constants.SNTP_MIN_MESSAGE_SIZE || data.Length > Constants.SNTP_MAX_MESSAGE_SIZE) { throw new ArgumentOutOfRangeException("Data", "Byte array must have a length between " + Constants.SNTP_MIN_MESSAGE_SIZE + " and " + Constants.SNTP_MAX_MESSAGE_SIZE); } using (ByteReader byteReader = new ByteReader(data, ByteOrder.Network)) { _timestamp = DateTime.Now; _flags = byteReader.ReadByte(); _stratum = byteReader.ReadByte(); _poll = byteReader.ReadByte(); _precision = byteReader.ReadByte(); _rootdelay = byteReader.ReadBytes(4); _rootDispersion = byteReader.ReadBytes(4); _referenceIdentifier = byteReader.ReadBytes(4); _referenceTimestamp = byteReader.ReadBytes(8); _originateTimestamp = byteReader.ReadBytes(8); _receiveTimestamp = byteReader.ReadBytes(8); _transmitTimestamp = byteReader.ReadBytes(8); if (byteReader.AvailableBytes > 0) { _keyIdentifier = byteReader.ReadBytes(4); // optional } if (byteReader.AvailableBytes > 0) { _messageDigest = byteReader.ReadBytes(16); //optional } } this.DestinationDateTime = DateTime.Now.ToUniversalTime(); }