Ejemplo n.º 1
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
            }
        }
Ejemplo n.º 2
0
        internal PcapInterface(pcap_if_wrapper pcapIf, NetworkInterface networkInterface, ICredentials credentials)
        {
            Name             = pcapIf.GetNameString();
            Description      = pcapIf.GetDescriptionString();
            Flags            = pcapIf.native.Flags;
            Addresses        = new List <PcapAddress>();
            GatewayAddresses = new List <IPAddress>();
            Credentials      = credentials;

            // 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)
                {
                    foreach (GatewayIPAddressInformation gatewayInfo in ipProperties.GatewayAddresses)
                    {
                        GatewayAddresses.Add(gatewayInfo.Address);
                    }
                }
                FriendlyName = networkInterface.Name;
            }

            // retrieve addresses
            var address = pcapIf.native.Addresses;

            while (address != IntPtr.Zero)
            {
                // Marshal memory pointer into a sockaddr struct
                var addr = Marshal.PtrToStructure <pcap_addr>(address);

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

                // is this a hardware address?
                // if so we should set our MacAddress
                if (newAddress.Addr?.type == Sockaddr.AddressTypes.HARDWARE)
                {
                    if (MacAddress == null)
                    {
                        MacAddress = newAddress.Addr.hardwareAddress;
                    }
                    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
            }
        }
Ejemplo 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
            }
        }
Ejemplo n.º 4
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
            }
        }
Ejemplo n.º 5
0
        internal PcapInterface(pcap_if pcapIf, NetworkInterface networkInterface, RemoteAuthentication credentials)
        {
            Name             = pcapIf.Name;
            Description      = pcapIf.Description;
            Flags            = pcapIf.Flags;
            Addresses        = new List <PcapAddress>();
            GatewayAddresses = new List <IPAddress>();
            Credentials      = credentials;

            // retrieve addresses
            var address = pcapIf.Addresses;

            while (address != IntPtr.Zero)
            {
                // Marshal memory pointer into a sockaddr struct
                var addr = Marshal.PtrToStructure <pcap_addr>(address);

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

                // is this a hardware address?
                // if so we should set our MacAddress
                if (newAddress.Addr?.type == Sockaddr.AddressTypes.HARDWARE)
                {
                    if (MacAddress == null)
                    {
                        MacAddress = newAddress.Addr.hardwareAddress;
                    }
                    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
            }

            // 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)
                {
                    foreach (GatewayIPAddressInformation gatewayInfo in ipProperties.GatewayAddresses)
                    {
                        GatewayAddresses.Add(gatewayInfo.Address);
                    }
                }
                FriendlyName = networkInterface.Name;

                PhysicalAddress mac = networkInterface.GetPhysicalAddress();
                if (MacAddress == null && mac != null)
                {
                    PcapAddress pcapAddress = new PcapAddress();
                    pcapAddress.Addr = new Sockaddr(mac);
                    Addresses.Add(pcapAddress);
                    if (pcapAddress.Addr.hardwareAddress.GetAddressBytes().Length != 0)
                    {
                        MacAddress = pcapAddress.Addr.hardwareAddress;
                    }
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                FriendlyName = WindowsNativeMethods.GetInterfaceAlias(Name);
            }
        }