Last active
December 18, 2015 00:49
-
-
Save mpsenn/5699459 to your computer and use it in GitHub Desktop.
Determine if a string is an IPv4 or IPv6 multicast or unicast ip address.
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
// Tests the ipAddress string and returns whether it's unicast or multicast. | |
function getNetworkMode(ipAddress) { | |
if(ipAddress.contains(':')) { | |
// IPv6 | |
if(/^ff\w{2}:/i.test(ipAddress)) | |
return "Multicast"; | |
return "Unicast"; | |
} | |
// an IPv4 address or a hostname | |
var octetMatch = ipAddress.match(/^(\d+)\./); | |
if(octetMatch == null) // ipAddress is a hostname | |
return "Unicast"; | |
// IPv4 address | |
var firstOctet = octetMatch[1]; | |
if(224 <= firstOctet && firstOctet <= 239) | |
return "Multicast"; | |
return "Unicast"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment