Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12236


Ignore:
Timestamp:
03/23/15 10:41:30 (10 years ago)
Author:
ascheibe
Message:

#2356 select NICs more careful for generating slave UIDs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Clients.Hive.Slave/3.3/Manager/ConfigManager.cs

    r12012 r12236  
    2525using System.Linq;
    2626using System.Management;
     27using System.Net.NetworkInformation;
    2728using HeuristicLab.Clients.Hive.SlaveCore.Properties;
    2829
     
    3435  public class ConfigManager {
    3536    private static ConfigManager instance = null;
     37    private const string vmwareNameString = "VMware";
     38    private const string virtualboxNameString = "VirtualBox";
     39    private const int macLength = 12;
     40
    3641    public static ConfigManager Instance {
    3742      get { return instance; }
     
    179184    /// </summary>   
    180185    private static Guid GetUniqueMachineIdFromMac() {
    181       ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
    182       ManagementObjectCollection mgtCol = mgtClass.GetInstances();
    183 
    184       foreach (ManagementObject mgtObj in mgtCol) {
    185         foreach (var prop in mgtObj.Properties) {
    186           if (prop.Value != null && prop.Name == "MACAddress") {
    187             try {
    188               //simply take the first nic
    189               string mac = prop.Value.ToString();
    190               byte[] b = new byte[8];
    191               string[] macParts = mac.Split(':');
    192               if (macParts.Length == 6) {
    193                 for (int i = 0; i < macParts.Length; i++) {
    194                   b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
    195                 }
    196 
    197                 // also get machine name and save it to the first 4 bytes               
    198                 Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
    199                 return guid;
    200               } else
    201                 throw new Exception("Error getting mac addresse");
    202             }
    203             catch {
    204               throw new Exception("Error getting mac addresse");
    205             }
    206           }
    207         }
    208       }
    209       throw new Exception("Error getting mac addresse");
     186      //try to get a real network interface, not a virtual one
     187      NetworkInterface validNic = NetworkInterface.GetAllNetworkInterfaces()
     188                      .FirstOrDefault(x =>
     189                                  !x.Name.Contains(vmwareNameString) &&
     190                                  !x.Name.Contains(virtualboxNameString) &&
     191                                  (x.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
     192                                   x.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet));
     193
     194      if (validNic == default(NetworkInterface)) {
     195        validNic = NetworkInterface.GetAllNetworkInterfaces().First();
     196      }
     197
     198      string addr = validNic.GetPhysicalAddress().ToString();
     199      if (addr.Length != macLength) {
     200        throw new Exception("Error generating slave UID: MAC address has to have " + macLength + " digits. Actual MAC address is: " + addr);
     201      }
     202
     203      byte[] b = new byte[8];
     204      int j = 2;
     205      for (int i = 0; i < macLength; i += 2) {
     206        b[j++] = (byte)((ParseNybble(addr[i]) << 4) | ParseNybble(addr[i + 1]));
     207      }
     208      // also get machine name and save it to the first 4 bytes               
     209      Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
     210      return guid;
    210211    }
    211212
Note: See TracChangeset for help on using the changeset viewer.