Exemplo n.º 1
0
 private void doReadPacket()
 {
     while (true)
     {
         if (needToStop)
         {
             return;
         }
         if (Ndisapi.ReadPacket(driverPtr, ref request))
         {
             buffer = (INTERMEDIATE_BUFFER)Marshal.PtrToStructure(bufferPtr, typeof(INTERMEDIATE_BUFFER));
             lock (packets)
             {
                 packets.Enqueue(buffer.m_IBuffer.Clone() as byte[]);
             }
             //Тут нужен мульти лок
             //captureDevice_OnPacketArrival(buffer);
         }
         else
         {
             System.Threading.Thread.Sleep(16);
         }
     }
 }
Exemplo n.º 2
0
        public bool startASync(int deviceNumber)
        {
            if (!ready)
            {
                return(false);
            }
            try
            {
                ADAPTER_MODE mode = new ADAPTER_MODE
                {
                    dwFlags        = Ndisapi.MSTCP_FLAG_SENT_LISTEN | Ndisapi.MSTCP_FLAG_RECV_LISTEN,
                    hAdapterHandle = adapters.m_nAdapterHandle[deviceNumber]
                };
                Ndisapi.SetAdapterMode(driverPtr, ref mode);
                if (host != null)
                {
                    IP_ADDRESS_V4 serverIp = new IP_ADDRESS_V4()
                    {
                        m_AddressType = Ndisapi.IP_SUBNET_V4_TYPE,
                        m_IpSubnet    = new IP_SUBNET_V4
                        {
                            m_Ip     = BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0),
                            m_IpMask = 0xFFFFFFFF
                        }
                    };
                    STATIC_FILTER_TABLE filtersTable = new STATIC_FILTER_TABLE();
                    filtersTable.m_StaticFilters = new STATIC_FILTER[256];
                    filtersTable.m_TableSize     = 3;

                    filtersTable.m_StaticFilters[0].m_Adapter      = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[0].m_ValidFields  = Ndisapi.NETWORK_LAYER_VALID;
                    filtersTable.m_StaticFilters[0].m_FilterAction = Ndisapi.FILTER_PACKET_REDIRECT;

                    filtersTable.m_StaticFilters[0].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_SEND;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_DEST_ADDRESS;

                    filtersTable.m_StaticFilters[0].m_NetworkFilter.m_IPv4.m_DestAddress = serverIp;

                    filtersTable.m_StaticFilters[1].m_Adapter      = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[1].m_ValidFields  = Ndisapi.NETWORK_LAYER_VALID;
                    filtersTable.m_StaticFilters[1].m_FilterAction = Ndisapi.FILTER_PACKET_REDIRECT;

                    filtersTable.m_StaticFilters[1].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_dwUnionSelector    = Ndisapi.IPV4;
                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_ValidFields = Ndisapi.IP_V4_FILTER_SRC_ADDRESS;

                    filtersTable.m_StaticFilters[1].m_NetworkFilter.m_IPv4.m_SrcAddress = serverIp;

                    filtersTable.m_StaticFilters[2].m_Adapter          = 0; // applied to all adapters
                    filtersTable.m_StaticFilters[2].m_ValidFields      = 0;
                    filtersTable.m_StaticFilters[2].m_FilterAction     = Ndisapi.FILTER_PACKET_PASS;
                    filtersTable.m_StaticFilters[2].m_dwDirectionFlags = Ndisapi.PACKET_FLAG_ON_RECEIVE | Ndisapi.PACKET_FLAG_ON_SEND;
                    Ndisapi.SetPacketFilterTable(driverPtr, ref filtersTable);
                }

                buffer    = new INTERMEDIATE_BUFFER();
                bufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));
                Win32Api.ZeroMemory(bufferPtr, Marshal.SizeOf(buffer));
                request = new ETH_REQUEST
                {
                    hAdapterHandle = adapters.m_nAdapterHandle[deviceNumber],
                    EthPacket      = { Buffer = bufferPtr }
                };
                threadReadPacket.Start();
                threadEvents.Start();
                ready = false;
                return(true);
            }
            catch { return(false); }
        }