Created
February 20, 2020 17:36
-
-
Save KaKi87/7c3907a1ec03ebc8ecb0294ab7176bce to your computer and use it in GitHub Desktop.
Is IP in CIDR ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isIpInCidr = (ip, cidr) => { | |
const [range, bits = 32] = cidr.split('/'); | |
const mask = ~(2 ** (32 - bits) - 1); | |
const ip4ToInt = ip => ip.split('.').reduce((int, oct) => (int << 8) + parseInt(oct, 10), 0) >>> 0; | |
return (ip4ToInt(ip) & mask) === (ip4ToInt(range) & mask); | |
}; | |
/* | |
Source : https://tech.mybuilder.com/determining-if-an-ipv4-address-is-within-a-cidr-range-in-javascript/ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment