public static List <NetworkInterfaceInfo> GetLocalNetworkInterfaces(
            SubnetClass subnetClasses = SubnetClass.C,
            bool flushCache           = false)
        {
            if (flushCache)
            {
                localNetworkInterfaces = null;
            }

            if (localNetworkInterfaces != null)
            {
                return(localNetworkInterfaces);
            }

            List <NetworkInterfaceInfo> workingInterfaces = new List <NetworkInterfaceInfo>();

            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
                    (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Tunnel) ||
                    (networkInterface.OperationalStatus != OperationalStatus.Up))
                {
                    continue;
                }

                IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                foreach (UnicastIPAddressInformation ipInformation in ipProperties.UnicastAddresses)
                {
                    IPAddress ipv4Mask = null;

                    if (OSVersionPlatform.GetGenericPlatform() == PlatformID.Unix)
                    {
                        //Mono does not implement IPv4Mask
                        //see https://bugzilla.xamarin.com/show_bug.cgi?id=2033
                        string mask = IPInfoTools.GetIPv4Mask(networkInterface.Name);
                        if (mask == null || IPAddress.TryParse(mask, out ipv4Mask) == false)
                        {
                            ipv4Mask = IPAddress.Parse("255.255.255.0"); // default to this
                        }
                    }
                    else
                    {
                        ipv4Mask = ipInformation.IPv4Mask;
                    }

                    SubnetClass interfaceClass = ipv4Mask.GetSubnetClass();

                    //optionally scan class A & B subnets
                    if (!((subnetClasses & interfaceClass) == interfaceClass))
                    {
                        continue;
                    }

                    IPAddress ipAddress = ipInformation.Address;

                    if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        NetworkInterfaceInfo interfaceInfo = new NetworkInterfaceInfo();

                        interfaceInfo.Host      = ipAddress;
                        interfaceInfo.Netmask   = ipv4Mask;
                        interfaceInfo.Broadcast = GetBroadcastAddress(interfaceInfo.Host, interfaceInfo.Netmask);

                        //deprecations are known - we only support IPv4 scanning with this code
                        //see above check for AddressFamily.InterNetwork
                        long host       = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Host.Address);
                        long netmask    = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Netmask.Address);
                        long broadcast  = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Broadcast.Address);
                        long network    = host & netmask;
                        long rangeStart = network + 1;
                        long rangeEnd   = broadcast - 1;

                        //e.g. if host == broadcast, netmask == 255.255.255.255
                        if (rangeEnd < rangeStart)
                        {
                            long swap = rangeEnd;
                            rangeEnd   = rangeStart;
                            rangeStart = swap;
                        }

                        interfaceInfo.RangeStart = new IPAddress((long)(uint)IPAddress.HostToNetworkOrder((int)rangeStart));
                        interfaceInfo.RangeEnd   = new IPAddress((long)(uint)IPAddress.HostToNetworkOrder((int)rangeEnd));

                        workingInterfaces.Add(interfaceInfo);
                    }
                }
            }

            //cache the results
            localNetworkInterfaces = workingInterfaces;

            return(localNetworkInterfaces);
        }
Beispiel #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (lbMessage.Text == string.Empty && tbSubnetName.Text == string.Empty)
            {
                lbMessage.Text          = "Mời bạn nhập SubnetName";
                gbSubnetName.BackColor  = Color.Coral;
                Handle_Empty_subnetName = false;
                return;
            }
            Handle_allowCalculate = false;
            string strNeededHost = "";

            if (tbNeededHost.Text == string.Empty)
            {
                strNeededHost = cbHostsPerSubnet.Text;
            }
            SubnetClass subnetItem = new SubnetClass(intSubnetOder.ToString(), tbIPAddress.Text, tbMaskNumber.Text, tbSubnetName.Text, tbNeededHost.Text, cbHostsPerSubnet.Text, tbSubnetID.Text, cbMaskBits.Text, cbSubnetMasks.Text, tbHostRange.Text, tbBroadCast.Text);

            liSubnet.Add(subnetItem);
            //liSubnet.Add(subnetItem);
            //DataGridViewRow dtRow = new DataGridViewRow();
            //dtRow.Cells[0].Value = tbIPAddress.Text;
            //dtRow.Cells[1].Value = intSubnetOder.ToString();
            //dtRow.Cells[2].Value = strNeededHost;
            //dtRow.Cells[0].Value = cbHostsPerSubnet.Text;
            //dtRow.Cells[0].Value = cbMaskBits.Text;
            //dtRow.Cells[0].Value = tbSubnetName.Text;
            //dtRow.Cells[0].Value = tbSubnetID.Text;
            //dtRow.Cells[0].Value = cbSubnetMasks.Text;
            //dtRow.Cells[0].Value = tbHostRange.Text;
            //dtRow.Cells[0].Value = tbBroadCast.Text;

            //table.Rows.Add(intSubnetOder.ToString(), tbIPAddress.Text, tbSubnetName.Text, tbNeededHost.Text, cbHostsPerSubnet.Text, tbSubnetID.Text, cbMaskBits.Text, cbSubnetMasks.Text, tbHostRange.Text, tbBroadCast.Text);



            tbIPAddress.Text      = IpAdressNext;
            tbMaskNumber.Text     = cbMaskBits.Text;
            Handle_allowCalculate = true;



            Package pg_send, pg_receive;

            pg_receive = new Package();

            pg_send = new Package(Package.PackageType.CTS_SubnetOne, tbIPAddress.Text, int.Parse(tbMaskNumber.Text), maxhost - int.Parse(cbHostsPerSubnet.Text) - 2);

            pg_send.SerializeSurrealists(client, client.GetStream());
            pg_receive = IpHelpers.getPackageDeserialized(client.GetStream());

            BindingSubnetToTable();
            if (pg_receive.IsEmptySubnet)
            {
                btnSubnet.Text    = "Hết";
                btnSubnet.Enabled = false;
                return;
            }

            reset_Value(pg_receive);


            //table.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);

            table.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            table.AutoSizeRowsMode    = DataGridViewAutoSizeRowsMode.None;


            intSubnetOder++;
            tbIPAddress.Enabled  = false;
            tbMaskNumber.Enabled = false;

            btnSubnet.Text = "Subnet " + (intSubnetOder + 1).ToString();
        }
Beispiel #3
0
        public static List<NetworkInterfaceInfo> GetLocalNetworkInterfaces(
            SubnetClass subnetClasses = SubnetClass.C, 
            bool flushCache = false)
        {
            if (flushCache)
                localNetworkInterfaces = null;

            if (localNetworkInterfaces != null)
                return localNetworkInterfaces;

            List<NetworkInterfaceInfo> workingInterfaces = new List<NetworkInterfaceInfo>();

            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                if ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
                    (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Tunnel) ||
                    (networkInterface.OperationalStatus != OperationalStatus.Up))
                    continue;

                IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                foreach (UnicastIPAddressInformation ipInformation in ipProperties.UnicastAddresses)
                {
                    IPAddress ipv4Mask = null;

                    if (OSVersionPlatform.GetGenericPlatform() == PlatformID.Unix)
                    {
                        //Mono does not implement IPv4Mask
                        //see https://bugzilla.xamarin.com/show_bug.cgi?id=2033
                        string mask = IPInfoTools.GetIPv4Mask(networkInterface.Name);
                        if (mask == null || IPAddress.TryParse(mask, out ipv4Mask) == false)
                            ipv4Mask = IPAddress.Parse("255.255.255.0"); // default to this
                    }
                    else
                        ipv4Mask = ipInformation.IPv4Mask;

                    SubnetClass interfaceClass = ipv4Mask.GetSubnetClass();
                                        
                    //optionally scan class A & B subnets
                    if (!((subnetClasses & interfaceClass) == interfaceClass))
                        continue;

                    IPAddress ipAddress = ipInformation.Address;

                    if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        NetworkInterfaceInfo interfaceInfo = new NetworkInterfaceInfo();

                        interfaceInfo.Host = ipAddress;
                        interfaceInfo.Netmask = ipv4Mask;
                        interfaceInfo.Broadcast = GetBroadcastAddress(interfaceInfo.Host, interfaceInfo.Netmask);

                        //deprecations are known - we only support IPv4 scanning with this code
                        //see above check for AddressFamily.InterNetwork
                        long host = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Host.Address);
                        long netmask = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Netmask.Address);
                        long broadcast = (long)(uint)IPAddress.NetworkToHostOrder((int)interfaceInfo.Broadcast.Address);
                        long network = host & netmask;
                        long rangeStart = network + 1;
                        long rangeEnd = broadcast - 1;

                        //e.g. if host == broadcast, netmask == 255.255.255.255
                        if (rangeEnd < rangeStart)
                        {
                            long swap = rangeEnd;
                            rangeEnd = rangeStart;
                            rangeStart = swap;
                        }

                        interfaceInfo.RangeStart = new IPAddress((long)(uint)IPAddress.HostToNetworkOrder((int)rangeStart));
                        interfaceInfo.RangeEnd = new IPAddress((long)(uint)IPAddress.HostToNetworkOrder((int)rangeEnd));

                        workingInterfaces.Add(interfaceInfo);
                    }
                }
            }

            //cache the results
            localNetworkInterfaces = workingInterfaces;

            return localNetworkInterfaces;
        }
Beispiel #4
0
 public UnRedo(Kind kindOfUnRedo, SubnetClass Subnet)
 {
     kind   = kindOfUnRedo;
     subnet = Subnet;
 }