Last active
July 21, 2016 11:31
-
-
Save wido/23276de5d71ee79253d8a55568883519 to your computer and use it in GitHub Desktop.
Java IPv6 EUI-64 calculator
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
/* | |
* Java code using Java-ipv6 from Google Code to convert | |
* a given IPv6 subnet and a MAC address into a IPv6 address | |
* calculated using SLAAC. | |
* | |
* Author: Wido den Hollander <wido@widodh.nl> | |
*/ | |
import com.googlecode.ipv6.IPv6Address; | |
import com.googlecode.ipv6.IPv6Network; | |
public class IPv6EUI64 { | |
public static IPv6Address EUI64Address(final IPv6Network cidr, final String macAddress) { | |
if (cidr.getNetmask().asPrefixLength() > 64) { | |
throw new IllegalArgumentException("IPv6 subnet " + cidr.toString() + " is not 64 bits or larger in size"); | |
} | |
String mac[] = macAddress.toLowerCase().split(":"); | |
return IPv6Address.fromString(cidr.getFirst().toString() + | |
Integer.toHexString(Integer.parseInt(mac[0], 16) ^ 2) + | |
mac[1] + ":" + mac[2] + "ff:fe" + mac[3] +":" + mac[4] + mac[5]); | |
} | |
public static void main(String[] argv) { | |
IPv6Network cidr = IPv6Network.fromString("2001:db8:100::/64"); | |
String mac = "06:7a:88:00:00:8b"; | |
IPv6Address eui64addr = EUI64Address(cidr, mac); | |
/* This will print 2001:db8:100:0:47a:88ff:fe00:8b */ | |
System.out.println(eui64addr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment