private static List <WinPcapDevice> Devices(string rpcapString,
                                                    RemoteAuthentication remoteAuthentication)
        {
            var retval = new List <WinPcapDevice>();

            var devicePtr   = IntPtr.Zero;
            var errorBuffer = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE); //will hold errors

            // convert the remote authentication structure to unmanaged memory if
            // one was specified
            IntPtr rmAuthPointer;

            if (remoteAuthentication == null)
            {
                rmAuthPointer = IntPtr.Zero;
            }
            else
            {
                rmAuthPointer = remoteAuthentication.GetUnmanaged();
            }

            int result = SafeNativeMethods.pcap_findalldevs_ex(rpcapString,
                                                               rmAuthPointer,
                                                               ref devicePtr,
                                                               errorBuffer);

            // free the memory if any was allocated
            if (rmAuthPointer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(rmAuthPointer);
            }

            if (result < 0)
            {
                throw new PcapException(errorBuffer.ToString());
            }

            IntPtr nextDevPtr = devicePtr;

            while (nextDevPtr != IntPtr.Zero)
            {
                // Marshal pointer into a struct
                var pcap_if_unmanaged =
                    (LibPcap.PcapUnmanagedStructures.pcap_if)Marshal.PtrToStructure(nextDevPtr,
                                                                                    typeof(LibPcap.PcapUnmanagedStructures.pcap_if));
                LibPcap.PcapInterface pcap_if = new LibPcap.PcapInterface(pcap_if_unmanaged);

                retval.Add(new WinPcapDevice(pcap_if));
                nextDevPtr = pcap_if_unmanaged.Next;
            }

            LibPcap.LibPcapSafeNativeMethods.pcap_freealldevs(devicePtr);  // Free unmanaged memory allocation.

            return(retval);
        }
Example #2
0
        private static List <NPcapDevice> BuildDeviceList(IntPtr devicePtr)
        {
            var    retval     = new List <NPcapDevice>();
            IntPtr nextDevPtr = devicePtr;

            while (nextDevPtr != IntPtr.Zero)
            {
                // Marshal pointer into a struct
                var pcap_if_unmanaged =
                    (LibPcap.PcapUnmanagedStructures.pcap_if)Marshal.PtrToStructure(nextDevPtr,
                                                                                    typeof(LibPcap.PcapUnmanagedStructures.pcap_if));
                LibPcap.PcapInterface pcap_if = new LibPcap.PcapInterface(pcap_if_unmanaged);

                retval.Add(new NPcapDevice(pcap_if));
                nextDevPtr = pcap_if_unmanaged.Next;
            }

            return(retval);
        }