/// <summary>
        ///  Get chassis network properties
        /// </summary>
        /// <returns>Response packet containing network properties</returns>
        public ChassisNetworkPropertiesResponse GetChassisNetworkProperties()
        {
            string[] ipAddresses, subnets, gateways = null;
            string dnsHostName, dhcpServer, dnsDomain, macAddress = null;
            int physicalIndex;

            bool dhcpEnabled = true;

            ChassisNetworkPropertiesResponse response = new ChassisNetworkPropertiesResponse();
            response.chassisNetworkPropertyCollection = new List<ChassisNetworkProperty>();

            Tracer.WriteInfo("Received GetChassisNetworkProperties()");
            Tracer.WriteUserLog("Invoked GetChassisNetworkProperties()");

            // Set default completion code to unknown.
            response.completionCode = Contracts.CompletionCode.Unknown;
            response.statusDescription = String.Empty;

            try
            {

                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();

                foreach (ManagementObject mo in moc)
                {
                    if (!string.IsNullOrEmpty((string)mo["MACAddress"]))
                    {
                        int logicalIndex = -1;
                        uint index = (uint)mo["Index"];
                        if (index >= 0)
                        {
                            logicalIndex = (int)index;
                        }
                        else
                        {
                            // Failed to verify Index
                            Console.WriteLine(@"Could not obtain network controller interface Index.");
                            response.completionCode = Contracts.CompletionCode.Failure;
                            return response;
                        }

                        physicalIndex = Contracts.SharedFunc.NetworkCtrlPhysicalIndex(logicalIndex);

                        // Configure specified network interface
                        if (physicalIndex > -1)
                        {
                            // If interface has IP enabled
                            if ((bool)mo["ipEnabled"])
                            {

                                ipAddresses = (string[])mo["IPAddress"];
                                subnets = (string[])mo["IPSubnet"];
                                gateways = (string[])mo["DefaultIPGateway"];
                                dnsHostName = (string)mo["DNSHostName"];
                                dhcpServer = (string)mo["DHCPServer"];
                                dnsDomain = (string)mo["DNSDomain"];
                                macAddress = (string)mo["MACAddress"];
                                dhcpEnabled = (bool)mo["DHCPEnabled"];

                                // response object
                                ChassisNetworkProperty cr = new ChassisNetworkProperty();

                                if (ipAddresses != null)
                                {
                                    for (int i = 0; i < ipAddresses.Count(); i++)
                                    {
                                        if (ChassisManagerUtil.CheckIpFormat(ipAddresses[i]))
                                        {
                                            if (string.IsNullOrEmpty(cr.ipAddress))
                                                cr.ipAddress = ipAddresses[i].ToString();
                                            else
                                                cr.ipAddress += ", " + ipAddresses[i].ToString();
                                        }
                                    }
                                }

                                if (subnets != null)
                                {
                                    for (int i = 0; i < subnets.Count(); i++)
                                    {
                                        if (string.IsNullOrEmpty(cr.subnetMask))
                                            cr.subnetMask = subnets[i].ToString();
                                        else
                                            cr.subnetMask += ", " + subnets[i].ToString();
                                    }
                                }

                                if (gateways != null)
                                {
                                    for (int i = 0; i < gateways.Count(); i++)
                                    {
                                        if (string.IsNullOrEmpty(cr.gatewayAddress))
                                            cr.gatewayAddress = gateways[i].ToString();
                                        else
                                            cr.gatewayAddress += ", " + gateways[i].ToString();
                                    }
                                }

                                cr.dhcpServer = dhcpServer;
                                cr.dnsDomain = dnsDomain;
                                cr.dnsHostName = dnsHostName;
                                cr.macAddress = macAddress;
                                cr.dhcpEnabled = dhcpEnabled;
                                cr.completionCode = Contracts.CompletionCode.Success;
                                cr.PhysicalIndex = physicalIndex;
                                response.chassisNetworkPropertyCollection.Add(cr);
                            }
                            else // all other interfaces (with ip not enables)
                            {
                                macAddress = (string)mo["MACAddress"];
                                // Populating interfaces only with valid mac addresses - ignoring loopback and other virtual interfaces
                                if (macAddress != null)
                                {
                                    ChassisNetworkProperty cr = new ChassisNetworkProperty();
                                    cr.macAddress = macAddress;
                                    cr.completionCode = Contracts.CompletionCode.Success;
                                    cr.PhysicalIndex = physicalIndex;
                                    response.chassisNetworkPropertyCollection.Add(cr);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.WriteError("GetChassisNetworkProperties failed with exception:" + ex.Message);
                response.completionCode = Contracts.CompletionCode.Failure;
                response.statusDescription = String.Format("GetChassisNetworkProperties failed with exception:" + ex.Message);
            }

            // sort by physical address
            response.chassisNetworkPropertyCollection.Sort();

            response.completionCode = Contracts.CompletionCode.Success;
            return response;
        }
Exemplo n.º 2
0
        /// <summary>
        ///  Get chassis network properties
        /// </summary>
        /// <returns>Response packet containing network properties</returns>
        public ChassisNetworkPropertiesResponse GetChassisNetworkProperties()
        {
            string[] ipAddresses, subnets, gateways = null;
            string dnsHostName, dhcpServer, dnsDomain, macAddress = null;

            bool dhcpEnabled = true;

            ChassisNetworkPropertiesResponse response = new ChassisNetworkPropertiesResponse();
            response.chassisNetworkPropertyCollection = new List<ChassisNetworkProperty>();

            Tracer.WriteInfo("Received GetChassisNetworkProperties()");
            Tracer.WriteUserLog("Invoked GetChassisNetworkProperties()");

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            // Set default completion code to unknown.
            response.completionCode = Contracts.CompletionCode.Unknown;
            response.statusDescription = String.Empty;

            try
            {
                foreach (ManagementObject mo in moc)
                {
                    // If interface has IP enabled
                    if ((bool)mo["ipEnabled"])
                    {
                        ipAddresses = (string[])mo["IPAddress"];
                        subnets = (string[])mo["IPSubnet"];
                        gateways = (string[])mo["DefaultIPGateway"];
                        dnsHostName = (string)mo["DNSHostName"];
                        dhcpServer = (string)mo["DHCPServer"];
                        dnsDomain = (string)mo["DNSDomain"];
                        macAddress = (string)mo["MACAddress"];
                        dhcpEnabled = (bool)mo["DHCPEnabled"];
                        int loopCount = 0;
                        foreach (string ip in ipAddresses)
                        {
                            if (ChassisManagerUtil.CheckIpFormat(ip))
                            {
                                ChassisNetworkProperty cr = new ChassisNetworkProperty();
                                cr.ipAddress = ipAddresses.ToArray()[loopCount];
                                if (subnets != null)
                                {
                                    cr.subnetMask = subnets.ToArray()[loopCount];
                                }
                                if (gateways != null)
                                {
                                    cr.gatewayAddress = gateways.ToArray()[loopCount];
                                }
                                cr.dhcpServer = dhcpServer;
                                cr.dnsDomain = dnsDomain;
                                cr.dnsHostName = dnsHostName;
                                cr.macAddress = macAddress;
                                cr.dhcpEnabled = dhcpEnabled;
                                cr.completionCode = Contracts.CompletionCode.Success;
                                response.chassisNetworkPropertyCollection.Add(cr);
                            }
                            loopCount++;
                        }
                    }
                    else // all other interfaces (with ip not enables)
                    {
                        macAddress = (string)mo["MACAddress"];
                        // Populating interfaces only with valid mac addresses - ignoring loopback and other virtual interfaces
                        if (macAddress != null)
                        {
                            ChassisNetworkProperty cr = new ChassisNetworkProperty();
                            cr.macAddress = macAddress;
                            cr.completionCode = Contracts.CompletionCode.Success;
                            response.chassisNetworkPropertyCollection.Add(cr);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.WriteError("GetChassisNetworkProperties failed with exception :" + ex.Message);
                response.completionCode = Contracts.CompletionCode.Failure;
                response.statusDescription = String.Format("GetChassisNetworkProperties failed with exception :" + ex.Message);
            }

            response.completionCode = Contracts.CompletionCode.Success;
            return response;
        }