Create a gist now

Instantly share code, notes, and snippets.

@wido /IPv6EUI64.java
Last active Jul 21, 2016

What would you like to do?
Java IPv6 EUI-64 calculator
/*
* 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