public void TestFromMacStringToByte() { const string fromStr = "32:70:d5:a0:5a:21"; byte[] resultBytes = NicUtil.getByteArrayfromMacString(fromStr); Assert.IsTrue(resultBytes.SequenceEqual(mac1)); }
/// <summary> /// Override method to customize how to report the NIC data /// </summary> /// <param name="device">The device needs to be reported</param> /// <param name="nic">nic object inside VM</param> override protected void writeDevice(string device, NetworkInterface nic) { if (null == device || null == nic) { return; // We trust xenstore, it is a SR-IOV device } string deviceId = getDeviceIndexFromIndexedDevicePath(device); string nameKey = String.Format("{0}/{1}/name", attrPath, deviceId); string macKey = String.Format("{0}/{1}/mac", attrPath, deviceId); // Update the xenstore info try { // Update name AXenStoreItem xenName = wmisession.GetXenStoreItem(nameKey); xenName.value = nic.Name; // Update mac AXenStoreItem xenMac = wmisession.GetXenStoreItem(macKey); byte[] byteMac = nic.GetPhysicalAddress().GetAddressBytes(); xenMac.value = NicUtil.getMacStringFromByteArray(byteMac); // Update ipv4 info updateVFIpInfo(deviceId, System.Net.Sockets.AddressFamily.InterNetwork, nic); // Update the ipv6 info updateVFIpInfo(deviceId, System.Net.Sockets.AddressFamily.InterNetworkV6, nic); } catch (Exception e) { Debug.Print("updateVFXenstoreAttrInfo error: {0}", e); } }
public void TestGetMacStringFromByteArray() { string expectedResult = UTFunctions.getMacString(mac1); string result = NicUtil.getMacStringFromByteArray(mac1); Assert.AreEqual(expectedResult, result); }
public void TestMatchByteArray() { byte[] arr2 = new byte[] { 0x32, 0x70, 0xd5, 0xa0, 0x5a, 0x21 }; bool result = NicUtil.matchByteArray(mac1, arr2); Assert.IsTrue(result); arr2 = new byte[] { 0x32, 0x70, 0xd5, 0xa0, 0x5a, 0x21, 0xff }; result = NicUtil.matchByteArray(mac1, arr2); Assert.IsTrue(result); arr2 = new byte[] { 0x32, 0x70, 0xd5, 0xa0, 0x5a, 0x22 }; result = NicUtil.matchByteArray(mac1, arr2); Assert.IsFalse(result); }
/// <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]); } }
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; } } }
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; } } }
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; } } }