Last active
December 14, 2015 00:48
-
-
Save sean-kang/5001159 to your computer and use it in GitHub Desktop.
Prototype for network address verification. This function should work with existing verification code using regular expression. For example, this function will try to calculate numbers even if an octet is bigger than 255.
This file contains hidden or 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
| function verify_address(cidr_addr) { | |
| var parts = cidr_addr.split("/"); | |
| var ip = parts[0]; | |
| var prefix = parts[1]; | |
| var mask = 0xffffffff; | |
| var ip_parts = ip.split('.'); | |
| var ip_hex = (((((((+ip_parts[0])*256)+(+ip_parts[1]))*256)+(+ip_parts[2]))*256)+(+ip_parts[3])) >>> 0; | |
| var prefix_hex = (~(mask >>> prefix)) >>> 0; | |
| var net_addr = (ip_hex & prefix_hex) >>> 0; | |
| print("[debug] ip:" + ip + "\n") | |
| print("[debug] ip_binary:" + ip_hex.toString(2) + "\n") | |
| print("[debug] prefix:" + prefix + "\n") | |
| print("[debug] prefix_binary:" + prefix_hex.toString(2) + "\n") | |
| print("[debug] net_addr_binary:" + net_addr.toString(2) + "\n") | |
| if (ip_hex == net_addr) { | |
| return true | |
| } | |
| else { | |
| return false | |
| } | |
| } | |
| function main() { | |
| test_addr = "192.168.3.128/25"; | |
| print(verify_address(test_addr)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment