+ Start a Discussion
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Validates the range between two ip addrresses?

Hi All,

 

I have to input two ip addresses from apex page. i.e., ipFrom and ipTo

for example:

ipFrom - 10.35.34.90

ipTo - 10.35.34.80

 

Above is incorrect range.

 

Is there any method to validate the ip address range as above ?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Assuming all IPs are formatted as 4-octet numbers, you can do this:

 

integer iptoint(string ip) {
string[] octet = ip.split('\\.');
return integer.valueof(octet[0])*16777216+integer.valueof(octet[1])*65536+integer.valueof(octet[2])*256+integer.valueof(octet[3]);
}

boolean isiniprange(string fromip, string toip, string currentip) {
integer lowip, highip, myip;
lowip = iptoint(fromip);
highip = iptoint(toip);
myip = iptoint(currentip);
return lowip <= myip && highip >= myip;

 

All Answers

sfdcfoxsfdcfox

Assuming all IPs are formatted as 4-octet numbers, you can do this:

 

integer iptoint(string ip) {
string[] octet = ip.split('\\.');
return integer.valueof(octet[0])*16777216+integer.valueof(octet[1])*65536+integer.valueof(octet[2])*256+integer.valueof(octet[3]);
}

boolean isiniprange(string fromip, string toip, string currentip) {
integer lowip, highip, myip;
lowip = iptoint(fromip);
highip = iptoint(toip);
myip = iptoint(currentip);
return lowip <= myip && highip >= myip;

 

This was selected as the best answer
Naresh Krishna.ax1176Naresh Krishna.ax1176

It worked. :) Thanks a lot