internal Packet(byte[] pData, int pStart, int pLength) { mBuffer = new byte[pLength]; WriteBytes(pData, pStart, pLength); ushort opcode; ReadUShort(out opcode); mOpcode = (EOpcode)opcode; }
public Arithmetic(ECondition condition, EOpcode opcode, bool setConditionFlags, string parameterString) { Condition = condition; Opcode = opcode; SetConditionFlags = setConditionFlags; Decoded = false; Parse(parameterString); }
public Packet(byte[] pData, int pStart, int pLength, bool pReadOpcode = true) { mBuffer = new byte[pLength]; WriteBytes(pData, pStart, pLength); if (pReadOpcode) { ushort opcode; ReadUShort (out opcode); mOpcode = (EOpcode)opcode; } }
public Arithmetic(ECondition condition, EOpcode opcode, bool setConditionFlags, ERegister rn, ERegister rd, byte rotate, byte immediate) { Operand2 = EOperand2.RotateImmediate; Condition = condition; Opcode = opcode; SetConditionFlags = setConditionFlags; Rn = rn; Rd = rd; Rotate = rotate; Immediate = immediate; Decoded = true; }
public Arithmetic(ECondition condition, EOpcode opcode, bool setConditionFlags, ERegister rn, ERegister rd, ERegister rs, EShiftInstruction shiftInst, ERegister rm) { Operand2 = EOperand2.RsShiftRm; Condition = condition; Opcode = opcode; SetConditionFlags = setConditionFlags; Rn = rn; Rd = rd; Rs = rs; ShiftInst = shiftInst; Rm = rm; Decoded = true; }
public Arithmetic(ECondition condition, EOpcode opcode, bool setConditionFlags, ERegister rn, ERegister rd, byte shiftCount, EShiftInstruction shiftInst, ERegister rm) { Operand2 = EOperand2.ImmediateShiftRm; Condition = condition; Opcode = opcode; SetConditionFlags = setConditionFlags; Rn = rn; Rd = rd; ShiftCount = shiftCount; ShiftInst = shiftInst; Rm = rm; Decoded = true; }
public PacketHandlerAttribute(EOpcode pOpcode) { Opcode = pOpcode; }
public Packet(EOpcode pOpcode) { mBuffer = new byte[DEFAULT_SIZE]; mOpcode = pOpcode; WriteUShort((ushort)pOpcode); }
/// <summary> /// 将要发送的数据封装为WebSocket通信数据帧。 /// 默认mask为0 /// </summary> /// <param name="data">发送的数据</param> /// <param name="isMask">是否做掩码处理(默认为false)</param> /// <param name="isFin">是否是结束帧(默认为true)</param> /// <param name="opcode">操作码(默认为TEXT)</param> byte[] CreateDataFrame(byte[] data, bool isMask = false, bool isFin = true, EOpcode opcode = EOpcode.TEXT) { int bufferSize = 10; if (null != data) { bufferSize += data.Length; } ByteArray ba = new ByteArray(bufferSize, false); int b1 = 0; if (isFin) { b1 = b1 | 128; } b1 = b1 | (int)opcode; ba.Write((byte)b1); int b2 = 0; byte[] maskKeys = null; if (isMask) { b2 = b2 | 128; maskKeys = new byte[4]; Random rand = new Random(); for (int i = 0; i < maskKeys.Length; i++) { maskKeys[i] = (byte)rand.Next(); } for (int i = 0; i < data.Length; i++) { var maskKey = maskKeys[i % 4]; data[i] = (byte)(data[i] ^ maskKey); } } if (data != null) { if (data.Length > 65535) { ba.Write((byte)(b2 | 127)); ba.Write((long)data.Length); } else if (data.Length > 125) { ba.Write((byte)(b2 | 126)); ba.Write((ushort)data.Length); } else { ba.Write((byte)(b2 | data.Length)); } if (isMask) { for (int i = 0; i < maskKeys.Length; i++) { ba.Write(maskKeys[i]); } } ba.Write(data); } else { ba.Write((byte)0); } return(ba.GetAvailableBytes()); }
private DHCPMessage(Stream stream) : this() { _Opcode = (EOpcode)stream.ReadByte(); _HardwareType = (EHardwareType)stream.ReadByte(); // Đọc phần Hlen (HardwareLength) để xác định độ dài mảng lưu ClientHardwareAddress _ClientHardwareAddress = new byte[stream.ReadByte()]; _Hops = (byte)stream.ReadByte(); _XID = Utilities.ReadWriteStream.Read4Bytes(stream); _Secs = Utilities.ReadWriteStream.Read2Bytes(stream); // Ref: [RFC2131] page 11 // Flag 16 bit, chỉ có bit 0 là đang được dùng (to specify server MUST send broadcast to client or not) _Flags_Broadcast = ((Utilities.ReadWriteStream.Read2Bytes(stream) & 0x8000) == 0x8000); _ClientIPAddress = Utilities.ReadWriteStream.ReadIPAddress(stream); _YourIPAddress = Utilities.ReadWriteStream.ReadIPAddress(stream); _NextServerIPAddress = Utilities.ReadWriteStream.ReadIPAddress(stream); _RelayAgentIPAddress = Utilities.ReadWriteStream.ReadIPAddress(stream); // ClientHardwareAddress được pad cho đủ 16 bytes, sau khi đọc ClientHardwareAddress cần đọc tiếp phần còn lạiđể loại bỏ bytes padding stream.Read(_ClientHardwareAddress, 0, _ClientHardwareAddress.Length); for (int i = _ClientHardwareAddress.Length; i < 16; i++) { stream.ReadByte(); } // Ref: [RFC2132] Section 9.3, page 26 // ServerHostname (sname) và BootFileName (file) có thể được overload để lưu DHCPOption. // Chuyển thành mảng byte buffer để xử lí sau byte[] serverHostnameBuffer = new byte[64]; byte[] bootFileNameBuffer = new byte[128]; stream.Read(serverHostnameBuffer, 0, serverHostnameBuffer.Length); stream.Read(bootFileNameBuffer, 0, bootFileNameBuffer.Length); // Ref: [RFC1048], [https://community.cisco.com/t5/switching/why-dhcp-option-has-quot-magic-cookie-quot/td-p/1764244] // Ban đầu // Vì DHCP message có format gần như giống hoàn toàn với BOOTP message. // Nên magic cookie dùng để phân biệt DHCP message với BOOTP. // Nếu như tồn tại magic cookie, thì mọi thứ phía sau đó được xử lí như DHCP Options. // Bây giờ // magic cookie được gán cố định theo RFC1048 và các RFC sau chấp nhận nó, nên đây xem như trường hiển nhiên // nếu như không có giá trị như được đề cập ở dưới, thì đã có lỗi trong quá trình IO hoặc truyền // Magic cookies được cố định là 4 bytes [0x63 0x82 0x53 0x63] hay [99 130 83 99] if (stream.ReadByte() != 0x63) { throw new IOException(); } if (stream.ReadByte() != 0x82) { throw new IOException(); } if (stream.ReadByte() != 0x53) { throw new IOException(); } if (stream.ReadByte() != 0x63) { throw new IOException(); } // Phần còn lại là DHCPOption. Phần này cần xử lí byte[] optionsBuffer = new byte[stream.Length - stream.Position]; stream.Read(optionsBuffer, 0, optionsBuffer.Length); // Xác định thông tin trong DHCPOptionOverload byte overload = ScanOverload(new MemoryStream(optionsBuffer)); switch (overload) { default: _ServerHostname = Utilities.ReadWriteStream.ReadStringToNull(new MemoryStream(serverHostnameBuffer)); _BootFileName = Utilities.ReadWriteStream.ReadStringToNull(new MemoryStream(bootFileNameBuffer)); _Options = ReadOptions(optionsBuffer, new byte[0], new byte[0]); break; case 1: _ServerHostname = Utilities.ReadWriteStream.ReadStringToNull(new MemoryStream(serverHostnameBuffer)); _Options = ReadOptions(optionsBuffer, bootFileNameBuffer, new byte[0]); break; case 2: _BootFileName = Utilities.ReadWriteStream.ReadStringToNull(new MemoryStream(bootFileNameBuffer)); _Options = ReadOptions(optionsBuffer, serverHostnameBuffer, new byte[0]); break; case 3: _Options = ReadOptions(optionsBuffer, bootFileNameBuffer, serverHostnameBuffer); break; } }