/// <summary>
        /// Retrieve a list of the current PcapDevices
        /// </summary>
        /// <returns>
        /// A <see cref="List&lt;LibPcapLiveDevice&gt;"/>
        /// </returns>
        private static List <LibPcapLiveDevice> GetDevices()
        {
            var deviceList = new List <LibPcapLiveDevice>();

            var devicePtr   = IntPtr.Zero;
            var errorBuffer = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE);

            int result = LibPcapSafeNativeMethods.pcap_findalldevs(ref devicePtr, errorBuffer);

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

            IntPtr nextDevPtr = devicePtr;

            while (nextDevPtr != IntPtr.Zero)
            {
                // Marshal pointer into a struct
                PcapUnmanagedStructures.pcap_if pcap_if_unmanaged =
                    (PcapUnmanagedStructures.pcap_if)Marshal.PtrToStructure(nextDevPtr,
                                                                            typeof(PcapUnmanagedStructures.pcap_if));
                PcapInterface pcap_if = new PcapInterface(pcap_if_unmanaged);
                deviceList.Add(new LibPcapLiveDevice(pcap_if));
                nextDevPtr = pcap_if_unmanaged.Next;
            }
            LibPcapSafeNativeMethods.pcap_freealldevs(devicePtr);  // Free unmanaged memory allocation.

            return(deviceList);
        }
Exemplo n.º 2
0
        internal PcapInterface(PcapUnmanagedStructures.pcap_if pcapIf, NetworkInterface networkInterface)
        {
            Name        = pcapIf.Name;
            Description = pcapIf.Description;
            Flags       = pcapIf.Flags;
            Addresses   = new List <PcapAddress>();

            // attempt to populate the mac address,
            // friendly name etc of this device
            if (networkInterface != null)
            {
                var ipProperties        = networkInterface.GetIPProperties();
                int gatewayAddressCount = ipProperties.GatewayAddresses.Count;
                if (gatewayAddressCount != 0)
                {
                    List <IPAddress> gatewayAddresses = new List <IPAddress>();
                    foreach (GatewayIPAddressInformation gatewayInfo in ipProperties.GatewayAddresses)
                    {
                        gatewayAddresses.Add(gatewayInfo.Address);
                    }
                    GatewayAddresses = gatewayAddresses;
                }
                MacAddress   = networkInterface.GetPhysicalAddress();
                FriendlyName = networkInterface.Name;
            }

            // retrieve addresses
            IntPtr address = pcapIf.Addresses;

            while (address != IntPtr.Zero)
            {
                //A sockaddr struct
                PcapUnmanagedStructures.pcap_addr addr;

                //Marshal memory pointer into a struct
                addr = (PcapUnmanagedStructures.pcap_addr)Marshal.PtrToStructure(address,
                                                                                 typeof(PcapUnmanagedStructures.pcap_addr));

                PcapAddress newAddress = new PcapAddress(addr);
                Addresses.Add(newAddress);

                // is this a hardware address?
                // if so we should set our internal m_macAddress member variable
                if ((newAddress.Addr != null) &&
                    (newAddress.Addr.type == Sockaddr.AddressTypes.HARDWARE))
                {
                    if (m_macAddress == null)
                    {
                        m_macAddress = newAddress;
                    }
                    else if (!MacAddress.Equals(newAddress.Addr.hardwareAddress))
                    {
                        throw new InvalidOperationException("found multiple hardware addresses, existing addr "
                                                            + MacAddress.ToString() + ", new address " + newAddress.Addr.hardwareAddress.ToString());
                    }
                }

                address = addr.Next; // move to the next address
            }
        }
Exemplo n.º 3
0
        internal PcapInterface(PcapUnmanagedStructures.pcap_if pcapIf)
        {
            Name        = pcapIf.Name;
            Description = pcapIf.Description;
            Flags       = pcapIf.Flags;

            // retrieve addresses
            Addresses = new List <PcapAddress>();
            IntPtr address = pcapIf.Addresses;

            while (address != IntPtr.Zero)
            {
                //A sockaddr struct
                PcapUnmanagedStructures.pcap_addr addr;

                //Marshal memory pointer into a struct
                addr = (PcapUnmanagedStructures.pcap_addr)Marshal.PtrToStructure(address,
                                                                                 typeof(PcapUnmanagedStructures.pcap_addr));

                PcapAddress newAddress = new PcapAddress(addr);
                Addresses.Add(newAddress);

                // is this a hardware address?
                // if so we should set our internal m_macAddress member variable
                if ((newAddress.Addr != null) &&
                    (newAddress.Addr.type == Sockaddr.AddressTypes.HARDWARE))
                {
                    if (m_macAddress == null)
                    {
                        m_macAddress = newAddress;
                    }
                    else
                    {
                        throw new System.InvalidOperationException("found multiple hardware addresses, existing addr "
                                                                   + MacAddress.ToString() + ", new address " + newAddress.Addr.hardwareAddress.ToString());
                    }
                }

                address = addr.Next; // move to the next address
            }
        }