Skip to content

Instantly share code, notes, and snippets.

@mpsenn
Last active December 18, 2015 00:49
Show Gist options
  • Save mpsenn/5699459 to your computer and use it in GitHub Desktop.
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.
// 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