// Public Methods /// <summary> /// Converts sntp message into a byte array. /// </summary> public byte[] ToArray() { using (ByteWriter byteWriter = new ByteWriter(Constants.SNTP_MIN_MESSAGE_SIZE, ByteOrder.Network)) { byteWriter.Write(_flags); byteWriter.Write(_stratum); byteWriter.Write(_poll); byteWriter.Write(_precision); byteWriter.Write(_rootdelay); byteWriter.Write(_rootDispersion); byteWriter.Write(_referenceIdentifier); byteWriter.Write(_referenceTimestamp); byteWriter.Write(_originateTimestamp); byteWriter.Write(_receiveTimestamp); byteWriter.Write(_transmitTimestamp); byteWriter.Write(_keyIdentifier); // optional byteWriter.Write(_messageDigest); //optional return byteWriter.GetBytes(); } }
/// <summary> /// Converts dhcp message into a byte array. /// </summary> public byte[] ToArray() { ByteWriter byteWriter = new ByteWriter(Constants.DHCP_MIN_MESSAGE_SIZE, ByteOrder.Network); byteWriter.Write(_op); byteWriter.Write(_htype); byteWriter.Write(_hlen); byteWriter.Write(_hops); byteWriter.Write(_xid); byteWriter.Write(_secs); // (ReverseByteOrder(BitConverter.GetBytes(this._secs))) ?? byteWriter.Write(_flags); byteWriter.Write(_ciaddr); byteWriter.Write(_yiaddr); byteWriter.Write(_siaddr); byteWriter.Write(_giaddr); byteWriter.Write(_chaddr); byteWriter.Write(_sname); byteWriter.Write(_file); byte[] data = new byte[(_options.Count > 0 ? 4 + _options.Count * 2 + _optionDataSize + 1 : 0)]; int offset = 0; if (_options.Count > 0) { BitConverter.GetBytes(Constants.DHCP_WIN_OPTIONS_MAGIC_NUMBER).CopyTo(data, offset); offset += 4; foreach (Byte optionId in _optionOrdering) { DhcpOption option = (DhcpOption)optionId; if (this._options.Contains(option)) { data[offset++] = optionId; byte[] optionValue = (byte[])_options[option]; if (this._options[option] != null && optionValue.Length > 0) { data[offset++] = (Byte)optionValue.Length; Array.Copy((byte[])_options[option], 0, data, offset, optionValue.Length); offset += optionValue.Length; } } } foreach (DhcpOption option in _options.Keys) { if (Array.IndexOf(_optionOrdering, (Byte)option) == -1) { data[offset++] = (Byte)option; byte[] optionValue = (byte[])_options[option]; if (this._options[option] != null && optionValue.Length > 0) { data[offset++] = (Byte)optionValue.Length; Array.Copy((byte[])_options[option], 0, data, offset, optionValue.Length); offset += optionValue.Length; } } } data[offset] = (Byte)DhcpOption.End; } byteWriter.Write(data); return byteWriter.GetBytes(); }
internal void WriteBytes(ByteWriter bw) { bw.Write(_transactionID); bw.Write(_flags); }