public TcpHeader(IPHeader ipHeader) { IpHeader = ipHeader; byte[] buffer = IpHeader.Data; using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); SequenceNumber = (uint)IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _uiAcknowledgementNumber = (uint)IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); _usDataOffsetAndFlags = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); WindowSize = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = (short)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); _usUrgentPointer = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); HeaderLength = (byte)(_usDataOffsetAndFlags >> 12); HeaderLength *= 4; MessageLength = buffer.Length - HeaderLength; } } // Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength); byte[] data = new byte[MessageLength]; Array.Copy(buffer, HeaderLength, data, 0, MessageLength); Data = data; }
void rec(byte[] data, int length) { IPHeader ipHeader = new IPHeader(data, length); IPacket packet = ipHeader.ParseData(); if (OnPacket != null) OnPacket(ipHeader.ProtocolType, packet); if (OnTcpStream != null && ipHeader.ProtocolType == ProtocolType.Tcp) { TcpHeader tcp = (TcpHeader)packet; if ((_Filter == null || _Filter.AllowTcpPacket(tcp))) { TcpStream stream = TcpStream.GetStream(_TcpStreams, tcp); if (stream != null) { if (stream.IsClossed) _TcpStreams.Remove(stream); OnTcpStream(stream); } } } Receive(); }
public UdpHeader(IPHeader ipHeader) { IpHeader = ipHeader; byte[] buffer = IpHeader.Data; using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (BinaryReader binaryReader = new BinaryReader(memoryStream)) { SourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); DestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Length = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); Checksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16()); } } byte[] data = new byte[buffer.Length - 8]; Array.Copy(buffer, 8, data, 0, data.Length); Data = data; //Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + 8, buffer.Count - 8); }