Example #1
0
        /// <summary>
        /// Clones the specified <see cref="source" />.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns><see cref="Native.NdisApi.ETH_M_REQUEST" />.</returns>
        public Native.NdisApi.ETH_M_REQUEST CloneEthMRequest(ref Native.NdisApi.ETH_M_REQUEST source)
        {
            var destination = CreateEthMRequest();

            CloneEthMRequest(ref source, ref destination);
            return(destination);
        }
Example #2
0
 /// <summary>
 /// Disposes the specified object.
 /// </summary>
 /// <param name="obj">The object.</param>
 public void DisposeObject(Native.NdisApi.ETH_M_REQUEST obj)
 {
     foreach (var packet in obj._ethPacket)
     {
         _pinnedManagedArrayAllocator.FreeArray(packet._buffer);
     }
 }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NdisApi" /> class.
        /// </summary>
        /// <param name="handle">The filter driver handle.</param>
        protected NdisApi(SafeFilterDriverHandle handle)
        {
            Handle = handle;

            _ndisApiHelper       = new NdisApiHelper();
            _ethPacketsToMstcp   = _ndisApiHelper.CreateEthMRequest();
            _ethPacketsToAdapter = _ndisApiHelper.CreateEthMRequest();
        }
Example #4
0
        /// <summary>
        /// Zeroes the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        public void ZeroEthMRequest(ref Native.NdisApi.ETH_M_REQUEST request)
        {
            request.hAdapterHandle = IntPtr.Zero;

            for (int i = 0; i < request.dwPacketsNumber; i++)
            {
                Kernel32.ZeroMemory(request._ethPacket[i]._buffer, Native.NdisApi.IntermediateBufferSize);
            }
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NdisApi" /> class.
        /// </summary>
        /// <param name="handle">The filter driver handle.</param>
        /// <param name="driverNameBytes">The driver name bytes.</param>
        protected NdisApi(SafeFilterDriverHandle handle, byte[] driverNameBytes)
        {
            _driverNameBytes = driverNameBytes;
            Handle           = handle;

            _ndisApiHelper       = new NdisApiHelper();
            _ethPacketsToMstcp   = _ndisApiHelper.CreateEthMRequest();
            _ethPacketsToAdapter = _ndisApiHelper.CreateEthMRequest();
        }
Example #6
0
        /// <summary>
        /// Clones the specified request.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destination">The destination.</param>
        public void CloneEthMRequest(ref Native.NdisApi.ETH_M_REQUEST source, ref Native.NdisApi.ETH_M_REQUEST destination)
        {
            destination.hAdapterHandle   = source.hAdapterHandle;
            destination.dwPacketsSuccess = source.dwPacketsSuccess;
            destination.dwPacketsNumber  = source.dwPacketsNumber;

            for (int i = 0; i < source.dwPacketsSuccess; i++)
            {
                Kernel32.CopyMemory(destination._ethPacket[i]._buffer, source._ethPacket[i]._buffer, (uint)Native.NdisApi.IntermediateBufferSize);
            }
        }
Example #7
0
        /// <summary>
        /// Sends the packets to MSTCP or adapter.
        /// </summary>
        /// <param name="ethMRequest">The ether multiple request.</param>
        /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
        public bool SendPackets(ref Native.NdisApi.ETH_M_REQUEST ethMRequest)
        {
            lock (_lock)
            {
                if (ethMRequest.dwPacketsSuccess == 0)
                {
                    return(true);
                }


                _ethPacketsToAdapter.dwPacketsSuccess = 0;
                _ethPacketsToMstcp.dwPacketsSuccess   = 0;
                _ethPacketsToAdapter.dwPacketsNumber  = 0;
                _ethPacketsToMstcp.dwPacketsNumber    = 0;
                _ethPacketsToAdapter.hAdapterHandle   = ethMRequest.hAdapterHandle;
                _ethPacketsToMstcp.hAdapterHandle     = ethMRequest.hAdapterHandle;

                unsafe
                {
                    for (int i = 0; i < ethMRequest.dwPacketsSuccess; i++)
                    {
                        var packet = ethMRequest._ethPacket[i];
                        var buffer = (Native.NdisApi.INTERMEDIATE_BUFFER_U *)packet._buffer;

                        if (buffer->m_dwDeviceFlags == Native.NdisApi.PACKET_FLAG.PACKET_FLAG_ON_SEND)
                        {
                            _ethPacketsToAdapter._ethPacket[_ethPacketsToAdapter.dwPacketsNumber++]._buffer = packet._buffer;
                        }
                        else
                        {
                            _ethPacketsToMstcp._ethPacket[_ethPacketsToMstcp.dwPacketsNumber++]._buffer = packet._buffer;
                        }
                    }
                }

                var success = true;

                if (_ethPacketsToMstcp.dwPacketsNumber > 0)
                {
                    success = SendPacketsToMstcp(ref _ethPacketsToMstcp);
                }

                if (_ethPacketsToAdapter.dwPacketsNumber > 0)
                {
                    success = SendPacketsToAdapter(ref _ethPacketsToAdapter) && success;
                }

                return(success);
            }
        }
Example #8
0
        /// <summary>
        /// Creates a new <see cref="Native.NdisApi.ETH_M_REQUEST" />.
        /// </summary>
        /// <remarks>The adapter handle still needs to be set.</remarks>
        /// <returns>NativeMethods.ETH_M_REQUEST_256.</returns>
        public Native.NdisApi.ETH_M_REQUEST CreateEthMRequest()
        {
            var ethPackets = new Native.NdisApi.ETH_M_REQUEST
            {
                _ethPacket      = new Native.NdisApi.NDISRD_ETH_Packet[Native.NdisApi.EthMRequestEthPacketSize],
                dwPacketsNumber = Native.NdisApi.EthMRequestEthPacketSize
            };

            for (var i = 0; i < Native.NdisApi.EthMRequestEthPacketSize; i++)
            {
                ethPackets._ethPacket[i]._buffer = _pinnedManagedArrayAllocator.AllocateArray(Native.NdisApi.IntermediateBufferSize);
            }

            return(ethPackets);
        }
Example #9
0
 /// <summary>
 /// Sends the packets to MSTCP.
 /// </summary>
 /// <param name="ethMRequest">The ether multiple request.</param>
 /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
 public bool SendPacketsToMstcp(ref Native.NdisApi.ETH_M_REQUEST ethMRequest)
 {
     return(Native.NdisApi.SendPacketsToMstcp(Handle, ref ethMRequest));
 }
Example #10
0
 /// <summary>
 /// Reads the packets to the specified <see cref="ethMRequest" />.
 /// </summary>
 /// <param name="ethMRequest">The ether multiple request.</param>
 /// <remarks>The adapter handle needs to be set.</remarks>
 /// <returns><c>true</c> if successful, <c>false</c> otherwise.</returns>
 public bool ReadPackets(ref Native.NdisApi.ETH_M_REQUEST ethMRequest)
 {
     return(Native.NdisApi.ReadPackets(Handle, ref ethMRequest));
 }