예제 #1
0
        private void OnReceive(IAsyncResult ar)
        {
            var received = _socket.EndReceive(ar);
            var buffer = new ArraySegment<byte>(ar.AsyncState as byte[], 0, 32*1024);
            var ipHeader = new IPHeader(buffer);
            var packet = ParseData(ipHeader);

            ProcessPacket(ipHeader.ProtocolType, packet);

            var header = new byte[32 * 1024];
            _socket.BeginReceive(header, 0, 32 * 1024, SocketFlags.None, OnReceive, header);
        }
예제 #2
0
        private void OnReceive(IAsyncResult ar)
        {
            var received = _socket.EndReceive(ar);
            var buffer   = new ArraySegment <byte>(ar.AsyncState as byte[], 0, 32 * 1024);
            var ipHeader = new IPHeader(buffer);
            var packet   = ParseData(ipHeader);

            ProcessPacket(ipHeader.ProtocolType, packet);

            var header = new byte[32 * 1024];

            _socket.BeginReceive(header, 0, 32 * 1024, SocketFlags.None, OnReceive, header);
        }
예제 #3
0
        private object ParseData(IPHeader ipHeader)
        {
            switch (ipHeader.ProtocolType)
            {
                case ProtocolType.Tcp:
                    return new TcpHeader(ipHeader);

                case ProtocolType.Udp:
                    return new UdpHeader(ipHeader);

                case ProtocolType.Unknown:
                    return null;
            }
            throw new NotSupportedException();
        }
예제 #4
0
        private object ParseData(IPHeader ipHeader)
        {
            switch (ipHeader.ProtocolType)
            {
            case ProtocolType.Tcp:
                return(new TcpHeader(ipHeader));

            case ProtocolType.Udp:
                return(new UdpHeader(ipHeader));

            case ProtocolType.Unknown:
                return(null);
            }
            throw new NotSupportedException();
        }
예제 #5
0
 public UdpHeader(IPHeader ipHeader)
 {
     IpHeader = ipHeader;
     var buffer = IpHeader.Data;
     using(var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
     {
         using (var 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());
         }
     }
     Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + 8, buffer.Count - 8);
 }
예제 #6
0
        public UdpHeader(IPHeader ipHeader)
        {
            IpHeader = ipHeader;
            var buffer = IpHeader.Data;

            using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
            {
                using (var 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());
                }
            }
            Data = new ArraySegment <byte>(buffer.Array, buffer.Offset + 8, buffer.Count - 8);
        }
예제 #7
0
 public TcpHeader(IPHeader ipHeader)
 {
     IpHeader = ipHeader;
     var buffer = IpHeader.Data;
     using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
     {
         using (var 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.Count - HeaderLength;
         }
     }
     Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength);
 }
예제 #8
0
        public TcpHeader(IPHeader ipHeader)
        {
            IpHeader = ipHeader;
            var buffer = IpHeader.Data;

            using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
            {
                using (var 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.Count - HeaderLength;
                }
            }
            Data = new ArraySegment <byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength);
        }