public int GetIpsOfNetrange(NetRange netrange) { var p1 = int.Parse(netrange.to.Split('.')[0]) - (int.Parse(netrange.from.Split('.')[0])); var p2 = int.Parse(netrange.to.Split('.')[1]) - (int.Parse(netrange.from.Split('.')[1])); var p3 = int.Parse(netrange.to.Split('.')[2]) - (int.Parse(netrange.from.Split('.')[2])); var p4 = int.Parse(netrange.to.Split('.')[3]) - (int.Parse(netrange.from.Split('.')[3])); if (p1 == 0) { p1 = 1; } if (p2 == 0) { p2 = 1; } if (p3 == 0) { p3 = 1; } if (p4 == 0) { p4 = 1; } return(p1 * p2 * p3 * p4); }
/// <summary> /// Validate if Ip is in NetRange. /// </summary> /// <param name="ip"></param> /// <returns></returns> public bool IsIpInNetrange(string ip) { for (int i = 0; i < LstNetRange.Count; i++) { NetRange netrange = LstNetRange[i]; if (netrange == null) { return(true); } if (netrange.IsIpInNetrange(ip)) { return(true); } } return(false); }
private static NetRange API(string ip) { try { // send request to json endpoint HttpWebRequest ripeRequest = HttpWebRequest.CreateHttp("https://stat.ripe.net/data/whois/data.json?resource=" + ip); ripeRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0"; ripeRequest.Headers.Add("Upgrade-Insecure-Requests: 1"); ripeRequest.Method = "GET"; ripeRequest.Headers.Add(HttpRequestHeader.Cookie, "serverid=www-plone-3; path=/"); // parse response using (HttpWebResponse resp = (HttpWebResponse)ripeRequest.GetResponse()) { string response; using (var sr = new StreamReader(resp.GetResponseStream())) { response = sr.ReadToEnd(); } // get netrange (if any) JObject json = JObject.Parse(response); string netrange = json["data"]["records"][0][1]["value"].ToString(); if (!netrange.Contains("/")) { netrange = json["data"]["records"][0][0]["value"].ToString(); } var ips = NetRange.GetNetRangeIPs(netrange); return(new NetRange(ips[0], ips[1], "netrangeObtainedFromRipe")); } } catch (Exception) { // if there wasn't any netrange, return whole 0.0.0.0 to 255.255.255.255 return(null);//new NetRange("0.0.0.0", "255.255.255.255", "ripe_noNetRangeFound"); } }
public NetRange GetNetrange(string ip) { return(NetRange.GetNetrange(ip)); }