public static uint IPToINT(string strIP) { return(ScannerStatic.IPToINT(strIP, false)); }
public static void GetIpRange(string[] strIps) { List <Range> lst = new List <Range>(); Regex reg = new Regex(@"^(\d{1,3}\.){3}\d{1,3}"); foreach (var v in strIps) { string strIP = string.Empty; string[] strLine = v.Trim().Trim(',').Trim().Split(m_split); if (strLine.Length == 0) { continue; } Range rg = new Range(); try { if (!reg.IsMatch(v.Trim())) { foreach (var ip in Dns.GetHostEntry(v.Trim()).AddressList) { if (!ip.IsIPv6LinkLocal) { strIP = ip.ToString(); if (!ScannerConfiger.DomainDic.ContainsKey(strIP)) { rg.Start = rg.End = ScannerStatic.IPToINT(strIP, true); ScannerConfiger.DomainDic.Add(strIP, new HashSet <string>()); } ScannerConfiger.DomainDic[strIP].Add(v.Trim()); break; } } if (rg.Start == 0) { throw new Exception("Can not get IPV4"); } } else if (strLine.Length == 1) { rg.Start = rg.End = ScannerStatic.IPToINT(strLine[0].Trim(), true); } else if (strLine.Length == 2) { rg.Start = ScannerStatic.IPToINT(strLine[0].Trim(), true); if (v.Contains("-")) { rg.End = ScannerStatic.IPToINT(strLine[1].Trim(), true); } else { int nBit = (32 - int.Parse(strLine[1])); if (nBit < 0) { throw new Exception(); } if (nBit == 0) { rg.End = rg.Start; } else { uint nMask = 0xFFFFFFFF << nBit; rg.Start = (rg.Start & nMask) + 1; rg.End = (rg.Start | (~nMask) - 1); } } if (rg.End < rg.Start) { throw new ArgumentException("Invalid IP range"); } } else { throw new FormatException("Format error"); } lst.Add(rg); } catch (Exception ex) { throw new Exception("Error:" + v + "\r\n" + ex.Message, ex); } } ScannerConfiger.IPRange = lst; }