Esempio n. 1
0
 /// <summary>
 /// Find valid nic object from the nics array, accroding to the mac address
 /// </summary>
 /// <param name="mac">mac string to identify the nic</param>
 /// <param name="nics">all the nic objects inside the VM</param>
 /// <returns></returns>
 virtual protected NetworkInterface findValidNic(string mac, NetworkInterface[] nics)
 {
     if (null == mac || null == nics)
     {
         Debug.Print("invalid parameter for findValidNic");
         return(null);
     }
     NetworkInterface[] validNics = (from nic in nics where NicUtil.macsMatch(mac, nic) select nic).ToArray <NetworkInterface>();
     if (null == validNics || 0 == validNics.Length)
     {
         Debug.Print("does not find valid nic for mac: " + mac);
         return(null);
     }
     else
     {
         Debug.Print("found nic for mac: {0}", mac);
         return(validNics[0]);
     }
 }
Esempio n. 2
0
        private void UnsetStaticIpv6Setting()
        {
            string macaddr = mac.value;

            resetError();

            foreach (ManagementObject nic in WmiBase.Singleton.Win32_NetworkAdapterConfiguration)
            {
                if (!(bool)nic["ipEnabled"])
                {
                    continue;
                }

                if (!NicUtil.macsMatch(macaddr, nic["macAddress"].ToString()))
                {
                    continue;
                }

                IpSettingItem settings = new IpSettingItem(nic["macAddress"].ToString(), "IPV6", "", "", "", "");
                if (IpSettings.getIPseting(nic["macAddress"].ToString(), "IPV6", ref settings) == false)
                {
                    return;
                }

                try{
                    string argument = "interface ipv6 reset";
                    netshInvoke(argument);
                    IpSettings.removeIPseting(nic["macAddress"].ToString(), "IPV6");
                }
                catch (Exception e)
                {
                    errorCode.value = "1";
                    errorMsg.value  = e.ToString();

                    wmisession.Log("Exception " + e.ToString());
                    return;
                }
            }
        }
Esempio n. 3
0
        private void SetStaticIpv6Setting()
        {
            string macaddr = mac.value;

            resetError();

            if ((address6.Exists() && address6.value.Length != 0) || (gateway6.Exists() && gateway6.value.Length != 0))
            {
                bool FoundDevice = false;
                foreach (ManagementObject nic in WmiBase.Singleton.Win32_NetworkAdapterConfiguration)
                {
                    if (!(bool)nic["ipEnabled"])
                    {
                        continue;
                    }

                    if (!NicUtil.macsMatch(macaddr, nic["macAddress"].ToString()))
                    {
                        continue;
                    }

                    FoundDevice = true;

                    IpSettings.addIpSeting(nic["macAddress"].ToString(), nic["DHCPEnabled"].ToString(), "IPV6", "", "", "");

                    try{
                        if (address6.Exists() && address6.value.Length != 0)
                        {
                            string argument = "interface ipv6 set address {0} {1}";
                            argument = string.Format(argument, nic["interfaceIndex"], address6.value);

                            if (netshInvoke(argument) != 0)
                            {
                                return;
                            }
                        }

                        if (gateway6.Exists() && gateway6.value.Length != 0)
                        {
                            string argument = "interface ipv6 add route ::/0 {0} {1}";
                            argument = string.Format(argument, nic["interfaceIndex"], gateway6.value);

                            if (netshInvoke(argument) != 0)
                            {
                                resetError();
                                argument = "interface ipv6 set route ::/0 {0} {1}";
                                argument = string.Format(argument, nic["interfaceIndex"], gateway6.value);

                                if (netshInvoke(argument) != 0)
                                {
                                    return;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        errorCode.value = "1";
                        errorMsg.value  = e.ToString();

                        wmisession.Log("Exception " + e.ToString());
                        return;
                    }
                }
                if (!FoundDevice)
                {
                    errorCode.value = "101";
                    errorMsg.value  = "Device not ready to use or ipEnabled not been set";
                    wmisession.Log("Device not ready to use or ipEnabled not been set");
                    return;
                }
            }
        }
Esempio n. 4
0
        private void SetStaticIpv4Setting()
        {
            string macaddr = mac.value;

            resetError();

            if ((address.Exists() && address.value.Length != 0) || (gateway.Exists() && gateway.value.Length != 0))
            {
                bool FoundDevice = false;
                foreach (ManagementObject nic in WmiBase.Singleton.Win32_NetworkAdapterConfiguration)
                {
                    if (!(bool)nic["ipEnabled"])
                    {
                        continue;
                    }

                    if (!NicUtil.macsMatch(macaddr, nic["macAddress"].ToString()))
                    {
                        continue;
                    }

                    FoundDevice = true;
                    IpSettings.addIpSeting(nic["macAddress"].ToString(), nic["DHCPEnabled"].ToString(), "IPV4", "", "", "");

                    try{
                        if (address.Exists() && address.value.Length != 0)
                        {
                            string ipv4, netmask;
                            convertIpv4Mask(address.value, out ipv4, out netmask);

                            ManagementBaseObject objNewIP = nic.GetMethodParameters("EnableStatic");
                            objNewIP["IPAddress"]  = new string[] { ipv4 };
                            objNewIP["SubnetMask"] = new string[] { netmask };

                            if (setIpv4Network(nic, "EnableStatic", objNewIP, "ipv4 address setting") != 0)
                            {
                                return;
                            }
                        }

                        if (gateway.Exists() && gateway.value.Length != 0)
                        {
                            ManagementBaseObject objNewGate = nic.GetMethodParameters("SetGateways");
                            objNewGate["DefaultIPGateway"] = new string[] { gateway.value };

                            if (setIpv4Network(nic, "SetGateways", objNewGate, "ipv4 gateway setting") != 0)
                            {
                                return;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        errorCode.value = "1";
                        errorMsg.value  = e.ToString();

                        wmisession.Log("Exception " + e.ToString());
                        return;
                    }
                }

                if (!FoundDevice)
                {
                    errorCode.value = "101";
                    errorMsg.value  = "Device not ready to use or ipEnabled not been set";
                    wmisession.Log("Device not ready to use or ipEnabled not been set");
                    return;
                }
            }
        }