Exemplo n.º 1
0
        public unsafe PacketCommunicatorReceiveResult ReceiveSomePackets(out int countGot, int maxPackets, HandlePacket callback)
        {
            this.AssertMode(PacketCommunicatorMode.Capture);
            PcapDataLink dataLink = new PcapDataLink(\u003CModule\u003E.pcap_datalink(this._pcapDescriptor));

            PacketCommunicator.PacketHandler   packetHandler   = new PacketCommunicator.PacketHandler(callback, dataLink);
            PacketCommunicator.HandlerDelegate handlerDelegate = new PacketCommunicator.HandlerDelegate(packetHandler.Handle);
            // ISSUE: cast to a function pointer type
            __FnPtr <void (byte *, pcap_pkthdr *, byte *)> local = (__FnPtr <void (byte *, pcap_pkthdr *, byte *)>)(IntPtr) Marshal.GetFunctionPointerForDelegate((Delegate)handlerDelegate).ToPointer();

            countGot = \u003CModule\u003E.pcap_dispatch(this._pcapDescriptor, maxPackets, local, (byte *)0);
            GC.KeepAlive((object)handlerDelegate);
            switch (countGot)
            {
            case -2:
                countGot = 0;
                return(PacketCommunicatorReceiveResult.BreakLoop);

            case -1:
                throw PcapError.BuildInvalidOperation("Failed reading from device", this._pcapDescriptor);

            case 0:
                if (packetHandler.PacketCounter != 0)
                {
                    countGot = packetHandler.PacketCounter;
                    return(PacketCommunicatorReceiveResult.Eof);
                }
                break;
            }
            return(PacketCommunicatorReceiveResult.Ok);
        }
Exemplo n.º 2
0
 public unsafe void SetKernelMinimumBytesToCopy(int size)
 {
     if (\u003CModule\u003E.pcap_setmintocopy(this._pcapDescriptor, size) != 0)
     {
         throw PcapError.BuildInvalidOperation("Error setting kernel minimum bytes to copy to " + size.ToString((IFormatProvider)CultureInfo.InvariantCulture), this._pcapDescriptor);
     }
 }
Exemplo n.º 3
0
 public unsafe void SetKernelBufferSize(int size)
 {
     if (\u003CModule\u003E.pcap_setbuff(this._pcapDescriptor, size) != 0)
     {
         throw PcapError.BuildInvalidOperation("Error setting kernel buffer size to " + size.ToString((IFormatProvider)CultureInfo.InvariantCulture), this._pcapDescriptor);
     }
 }
 internal unsafe void SetFilter(pcap *pcapDescriptor)
 {
     if (\u003CModule\u003E.pcap_setfilter(pcapDescriptor, this._bpf) != 0)
     {
         throw PcapError.BuildInvalidOperation("Failed setting bpf filter", pcapDescriptor);
     }
 }
 internal unsafe void Transmit(pcap *pcapDescriptor, [MarshalAs(UnmanagedType.U1)] bool isSync)
 {
     if (\u003CModule\u003E.pcap_sendqueue_transmit(pcapDescriptor, this._pcapSendQueue, isSync ? 1 : 0) < (uint)*(int *)((IntPtr)this._pcapSendQueue + 4L))
     {
         throw PcapError.BuildInvalidOperation("Failed transmiting packets from queue", pcapDescriptor);
     }
 }
Exemplo n.º 6
0
        public unsafe PacketCommunicatorReceiveResult ReceiveStatistics(int count, HandleStatistics callback)
        {
            this.AssertMode(PacketCommunicatorMode.Statistics);
            PacketCommunicator.HandlerDelegate handlerDelegate = new PacketCommunicator.HandlerDelegate(new PacketCommunicator.StatisticsHandler(callback).Handle);
            // ISSUE: cast to a function pointer type
            __FnPtr <void (byte *, pcap_pkthdr *, byte *)> local = (__FnPtr <void (byte *, pcap_pkthdr *, byte *)>)(IntPtr) Marshal.GetFunctionPointerForDelegate((Delegate)handlerDelegate).ToPointer();
            int num = \u003CModule\u003E.pcap_loop(this._pcapDescriptor, count, local, (byte *)0);

            GC.KeepAlive((object)handlerDelegate);
            if (num == -1)
            {
                throw PcapError.BuildInvalidOperation("Failed reading from device", this._pcapDescriptor);
            }
            return(num != -2 ? PacketCommunicatorReceiveResult.Ok : PacketCommunicatorReceiveResult.BreakLoop);
        }
Exemplo n.º 7
0
        public unsafe void SendPacket(Packet packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }
            if (packet.Length == 0)
            {
                return;

                fixed(byte *numPtr = &packet.Buffer[0])
                {
                    if (\u003CModule\u003E.pcap_sendpacket(this._pcapDescriptor, numPtr, packet.Length) != 0)
                    {
                        throw PcapError.BuildInvalidOperation("Failed writing to device", this._pcapDescriptor);
                    }
                }
        }
Exemplo n.º 8
0
        public unsafe PacketCommunicatorReceiveResult ReceivePackets(int count, HandlePacket callback)
        {
            this.AssertMode(PacketCommunicatorMode.Capture);
            PcapDataLink dataLink = new PcapDataLink(\u003CModule\u003E.pcap_datalink(this._pcapDescriptor));

            PacketCommunicator.PacketHandler   packetHandler   = new PacketCommunicator.PacketHandler(callback, dataLink);
            PacketCommunicator.HandlerDelegate handlerDelegate = new PacketCommunicator.HandlerDelegate(packetHandler.Handle);
            // ISSUE: cast to a function pointer type
            __FnPtr <void (byte *, pcap_pkthdr *, byte *)> local = (__FnPtr <void (byte *, pcap_pkthdr *, byte *)>)(IntPtr) Marshal.GetFunctionPointerForDelegate((Delegate)handlerDelegate).ToPointer();
            int num = \u003CModule\u003E.pcap_loop(this._pcapDescriptor, count, local, (byte *)0);

            GC.KeepAlive((object)handlerDelegate);
            if (num == -2)
            {
                return(PacketCommunicatorReceiveResult.BreakLoop);
            }
            if (num == -1)
            {
                throw PcapError.BuildInvalidOperation("Failed reading from device", this._pcapDescriptor);
            }
            return(num == 0 && packetHandler.PacketCounter != count ? PacketCommunicatorReceiveResult.Eof : PacketCommunicatorReceiveResult.Ok);
        }
        private unsafe void Initialize(pcap *pcapDescriptor, string filterString, IpV4SocketAddress netmask)
        {
            // ISSUE: untyped stack allocation
            long num1 = (long)__untypedstackalloc(\u003CModule\u003E.__CxxQueryExceptionSize());
            basic_string\u003Cchar\u002Cstd\u003A\u003Achar_traits\u003Cchar\u003E\u002Cstd\u003A\u003Aallocator\u003Cchar\u003E\u0020\u003E stdAllocatorChar;

            MarshalingServices.ManagedToUnmanagedString(&stdAllocatorChar, filterString);
            // ISSUE: fault handler
            try
            {
                uint num2 = 0U;
                if (netmask != null)
                {
                    num2 = netmask.Address.ToValue();
                }
                bpf_program *bpfProgramPtr1 = (bpf_program *)\u003CModule\u003E.@new(16UL);
                bpf_program *bpfProgramPtr2;
                if ((IntPtr)bpfProgramPtr1 != IntPtr.Zero)
                {
                    // ISSUE: initblk instruction
                    __memset((IntPtr)bpfProgramPtr1, 0, 16L);
                    bpfProgramPtr2 = bpfProgramPtr1;
                }
                else
                {
                    bpfProgramPtr2 = (bpf_program *)0;
                }
                this._bpf = bpfProgramPtr2;
                try
                {
                    if (\u003CModule\u003E.pcap_compile(pcapDescriptor, this._bpf, \u003CModule\u003E.std\u002Ebasic_string\u003Cchar\u002Cstd\u003A\u003Achar_traits\u003Cchar\u003E\u002Cstd\u003A\u003Aallocator\u003Cchar\u003E\u0020\u003E\u002Ec_str(&stdAllocatorChar), 1, num2) < 0)
                    {
                        throw new ArgumentException("An error has occured when compiling the filter <" + filterString + ">: " + PcapError.GetErrorMessage(pcapDescriptor));
                    }
                }
                catch (Exception ex1) when(
                {
                    // ISSUE: unable to correctly present filter
                    uint num3 = (uint)Marshal.GetExceptionCode();
                    if (\u003CModule\u003E.__CxxExceptionFilter((void *)Marshal.GetExceptionPointers(), (void *)0, 0, (void *)0) != 0)
                    {
                        SuccessfulFiltering;
                    }
                    else
                    {
                        throw;
                    }
                }
                    )
                {
                    uint num4 = 0U;

                    \u003CModule\u003E.__CxxRegisterExceptionObject((void *)Marshal.GetExceptionPointers(), (void *)num1);
                    try
                    {
                        try
                        {
                            \u003CModule\u003E.delete((void *)this._bpf);
                            \u003CModule\u003E._CxxThrowException((void *)0, (_s__ThrowInfo *)0);
                        }
                        catch (Exception ex2) when(
                        {
                            // ISSUE: unable to correctly present filter
                            num4 = (uint)\u003CModule\u003E.__CxxDetectRethrow((void *)Marshal.GetExceptionPointers());
                            if ((int)num4 != 0)
                            {
                                SuccessfulFiltering;
                            }
                            else
                            {
                                throw;
                            }
                        }
                            )
                        {
                        }
                        if ((int)num4 != 0)
                        {
                            throw;
                        }
                    }