c# - Calculate IP Address range for exclusive access using REMOTE_ADDR -


this question has answer here:

i've read multiple questions (and answers) before posted this, can't seem somehow.. how calculate ip range using remote_addr? example i've given exlusive access range: 197.168.178.1 - 197.168.178.10 when access site 197.168.178.6 want access. how manage that?

what got far list of class iprange - these ip addresses specified in database , can altered range. id used identify range accessing site.

public class iprange {     public int id { get; set; }     public system.net.ipaddress rangestart { get; set; }     public system.net.ipaddress rangeend { get; set; } } 

and i've tried match incoming ip address (request.servervariables["remote_addr"]) range using next method;

    private static int matchaddress(system.net.ipaddress ip, list<iprange> range)     {         int value = -1;         foreach (iprange item in range)         {             if (item.rangestart.address >= ip.address && ip.address <= item.rangeend.address)             {                 value = item.id;                 break;             }         }         return value;     } 

the method address obsolete , returns different think does; according warning should use ipaddress.equals, method can compare 2 addresses see if it's same, want know if it's defined within range.

what method match (or should use component this)?

p.s. knowdledge of networking below par, know that, might dumb question, i'm lost atm.

you can use regular expressions in case. spattern string contains regular expression , ips contains test cases. here's example of console application:

using system; using system.text.regularexpressions;  namespace testsapplication {     class program     {         static void main(string[] args)         {             string spattern = "197.168.178.([1-9]|10)$";             string[] ips =                  {                     "197.168.178.1",                     "197.168.178.10",                     "197.168.178.5",                     "197.168.178.255",                     "255.255.255.0"                 };             foreach (string s in ips)             {                 console.write("{0,24}", s);                  if (regex.ismatch(s, spattern, regexoptions.ignorecase))                     console.writeline("  (match '{0}' found)", spattern);                 else                     console.writeline("  no match!");             }             console.readline(); //pause         }     } } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -