Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  37] [ 2]  / answers: 1 / hits: 16405  / 7 Years ago, wed, september 13, 2017, 12:00:00

Please pardon for this trival question



Given a set of Ip the set is quite large and might increase
https://github.com/client9/ipcat/blob/master/datacenters.csv#L4



Small example set - first column start ip second - end ip range



enter



I will get the user ip from the request . I need to check if the ip falls in these set of ranges . How do i accomplish this.



I have looked into ip_range_check and range_check.



But they donot check for a ip given given range . How is thhis possible in node js with utmost performance. I dont want to go for a exhaustive search as performance is a hight priority.



Please help something new and quite challenging so far to me.


More From » node.js

 Answers
19

This is quite easy if we convert the ips to simple numbers:



function IPtoNum(ip){
return Number(
ip.split(.)
.map(d => (000+d).substr(-3) )
.join()
);
}


Then we can check a certain range as:



 if( IPtoNum(min) < IPtoNum(val) &&    IPtoNum(max) > IPtoNum(val) ) alert(in range);


That can also be applied to a table:



const ranges = [
[..41, 192.168.45],
[123.124.125, 126.124.123]
];

const ip = 125.12.125;
const inRange = ranges.some(
([min,max]) => IPtoNum(min) < IPtoNum(ip) && IPtoNum(max) > IPtoNum(ip)
);

[#56493] Monday, September 11, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcelofrankiea

Total Points: 200
Total Questions: 96
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;