Skip to content

Instantly share code, notes, and snippets.

@sploders101
Created December 29, 2018 22:47
Show Gist options
  • Save sploders101/926099f58228b2aae7a389a3fdebedcb to your computer and use it in GitHub Desktop.
Save sploders101/926099f58228b2aae7a389a3fdebedcb to your computer and use it in GitHub Desktop.
Simple functions for performing netmask checks on an IP.
function IPBInt(ip) {
const iparr = ip.split(".");
let bip = 0n;
for(let i=0;i<iparr.length;i++) {
bip = bip << 8n;
bip += BigInt(iparr[i]);
}
return bip;
}
function maskMatch(ip,dest,mask) {
ip = IPBInt(ip);
dest = IPBInt(dest);
mask = IPBInt(mask);
return ((~(ip^dest))&mask)===mask;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment