public Dictionary <string, string> SubNCsToDCs(string nc, string currentDC) { Dictionary <string, string> ret = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase) { }; Dictionary <string, DomainControllerHelper> temp = new Dictionary <string, DomainControllerHelper>(ForestBase.DCList); foreach (DomainControllerHelper dc in temp.Values) { if (!dc.UDPPingSuccess) { DomainControllerHelper tempdc = DomainControllerHelper.UDPPing(dc.Name); StoreDC(tempdc, tempdc.DefaultNamingContext); } } foreach (KeyValuePair <string, List <string> > ncinfo in ForestBase.NCsToDCs.Where(n => n.Key.ToLowerInvariant().EndsWith(nc.ToLowerInvariant()))) { if (ncinfo.Value.Contains(currentDC)) { ret.AddSafe(ncinfo.Key, currentDC); } else { ret.AddSafe(ncinfo.Key, ncinfo.Value[0]); } } return(ret); }
public static List <DomainControllerHelper> DnsGetDCs(string domainDNS, string siteName, string forestName, NetApi32.DS_OPEN_FLAGS flags, NetApi32.DS_OPTION_FLAGS siteOptions, bool doUdpPing = false) { List <DomainControllerHelper> ret = new List <DomainControllerHelper> { }; DomainControllerHelper errdc = new DomainControllerHelper() { Success = false }; int iret = -1; IntPtr iphandle = IntPtr.Zero; try { if (domainDNS == null) { domainDNS = Environment.ExpandEnvironmentVariables("%USERDNSDOMAIN%"); } iret = NetApi32.DsGetDcOpen(domainDNS, siteOptions, siteName, IntPtr.Zero, forestName, flags, out iphandle); } catch (Win32Exception Win32Ex) { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; } catch (Exception ex) { errdc.ErrorString = ex.Message; } if (iret == 0) { string dnsName = null; IntPtr socketcnt = IntPtr.Zero; IntPtr sockets = IntPtr.Zero; while ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED)) { errdc = new DomainControllerHelper() { Success = false }; try { iret = NetApi32.DsGetDcNext(iphandle, ref socketcnt, out sockets, out dnsName); } catch (Win32Exception Win32Ex) { errdc.ErrorString = Win32Ex.ErrorCode + " (Win32: " + Win32Ex.Message + ")"; } catch (Exception ex) { errdc.ErrorString = ex.Message; } if ((iret == (int)NetApi32.ERROR_LIST.ERROR_SUCCESS) || (iret == (int)NetApi32.ERROR_LIST.ERROR_FILEMARK_DETECTED)) { if (doUdpPing) { DateTime start = DateTime.Now; GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing against {0} ({1})", dnsName, domainDNS)); ret.Add(DomainControllerHelper.UDPPing(dnsName)); double dur = DateTime.Now.Subtract(start).TotalMilliseconds; GlobalEventHandler.RaiseMessageOccured(String.Format("UDPPing {0} in {1} ms", dnsName, dur.ToString())); } else { ret.Add(new DomainControllerHelper(domainDNS, dnsName)); } } else { if (ret.Count == 0) { HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString()); ret.Add(errdc); } } } } else { if (ret.Count == 0) { HandleError(iret, ref errdc, domainDNS, siteName, flags.ToString()); ret.Add(errdc); } } if (iphandle != IntPtr.Zero) { NetApi32.DsGetDcClose(iphandle); } return(ret); }