下面列出了java.net.Inet6Address#getByAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void decode(byte[] bytes, int offset) {
length = (int)(bytes[offset]|bytes[offset+1]);
int headerSize = 4;
int currentIndex = offset + headerSize;
byte[] readAddress = new byte[16];
System.arraycopy(bytes,currentIndex,readAddress,0,16);
try{
srcAddress = (Inet6Address) Inet6Address.getByAddress(readAddress);
currentIndex = currentIndex + 16;
}catch(UnknownHostException e){
// FIXME: Poner logs con respecto a excepcion
}
flowLabel = (int)(bytes[currentIndex+1]|bytes[currentIndex+2]|bytes[currentIndex+3]);
}
@Override
public void decode(byte[] bytes, int offset) {
length = (int)(bytes[offset]|bytes[offset+1]);
int headerSize = 4;
int unprocessedBytes = length - headerSize;
int currentIndex = offset+headerSize;
while(unprocessedBytes > 0){
byte[] readAddress = new byte[16];
System.arraycopy(bytes,currentIndex,readAddress,0,16);
try{
Inet6Address newAddress = (Inet6Address) Inet6Address.getByAddress(readAddress);
addSourceIpAddress(newAddress);
currentIndex = currentIndex + 16;
unprocessedBytes = unprocessedBytes - 16;
}catch(UnknownHostException e){
// FIXME: Poner logs con respecto a excepcion
}
}
}
/**
* Return an InetAddress to represent the value of the address in the
* sin4_addr or sin6_addr fields. For IPv6 addresses, the Inet6Address is
* created with the sin6_scope_id in the sockaddr_in6 structure.
*/
private InetAddress address(int family) {
int len;
int offset;
int scope_id;
if (family == AF_INET) {
len = 4;
offset = OFFSET_SIN4_ADDR;
scope_id = 0;
} else {
len = 16;
offset = OFFSET_SIN6_ADDR;
scope_id = UNSAFE.getInt(address + OFFSET_SIN6_SCOPE_ID);
}
byte[] bytes = new byte[len];
UNSAFE.copyMemory(null, address + offset, bytes, ARRAY_BASE_OFFSET, len);
try {
if (scope_id == 0) {
return InetAddress.getByAddress(bytes);
} else {
return Inet6Address.getByAddress(null, bytes, scope_id);
}
} catch (UnknownHostException e) {
throw new InternalError(e);
}
}
@Nonnull
public InetAddress getIp() {
ByteBuffer buf = ByteBuffer.wrap(getData());
final byte[] dst = new byte[16];
buf.get(dst, 0, 16);
try {
return Inet6Address.getByAddress(dst);
} catch (UnknownHostException e) {
// This can happen only if the IP byte array is of illegal length
throw new IllegalStateException("Illegal IP address", e);
}
}
@Test
public void constructFromInet6AddressWithScopeId() throws UnknownHostException
{
byte[] bytes = fromString("2001:db8:85a3::8a2e:370:7334").toByteArray();
final InetAddress inetAddress = Inet6Address.getByAddress("host", bytes, 12);
assertEquals("2001:db8:85a3::8a2e:370:7334", fromInetAddress(inetAddress).toString());
}
@Nonnull
public InetAddress getIp() {
ByteBuffer buf = ByteBuffer.wrap(getData());
final byte[] dst = new byte[16];
buf.get(dst, 0, 16);
try {
return Inet6Address.getByAddress(dst);
} catch (UnknownHostException e) {
// This can happen only if the IP byte array is of illegal length
throw new IllegalStateException("Illegal IP address", e);
}
}
/**
* Decodes the body of the SubObject
*/
public void decode(){
byte[] ipadd=new byte[16];
System.arraycopy(this.subobject_bytes,2, ipadd, 0, 16);
try {
ipv6address=(Inet6Address)Inet6Address.getByAddress(ipadd);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prefix=(int)this.subobject_bytes[18];
}
@Nonnull
public InetAddress getIp() {
ByteBuffer buf = ByteBuffer.wrap(getData());
final byte[] dst = new byte[16];
buf.get(dst, 0, 16);
try {
return Inet6Address.getByAddress(dst);
} catch (UnknownHostException e) {
// This can happen only if the IP byte array is of illegal length
throw new IllegalStateException("Illegal IP address", e);
}
}
@Test
public void mapsIpv4AddressFromIpv6Address() throws Exception {
// Can't think of a reason why this would ever come up, but testing it just in case.
// ::ffff:127.0.0.1
Inet6Address address = Inet6Address.getByAddress(
"localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, 127, 0, 0, 1}, -1);
assertEquals(0, address.getScopeId());
String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));
assertEquals("127.0.0.1", addressString);
}
public static Field getIPV6AddressAsString(byte[] bytes) throws OnRecordErrorException {
try {
InetAddress addr = Inet6Address.getByAddress(bytes);
return Field.create(addr.getHostAddress());
} catch (UnknownHostException e) {
throw new OnRecordErrorException(Errors.NETFLOW_13, e.getClass().getSimpleName(), e.getMessage(), e);
}
}
/**
* Get instances of InetAddress by calling InetAddress.getByAddress(String, byte[]),
* and passing in a 16-byte address. Then test their properties.
* @param response HttpServletResponse used to return failure message
* @throws IOException If method being tested throws this.
* @throws AssertionFailedException If an assertion fails
*/
private static void testGetByAddress6(HttpServletResponse response)
throws IOException, AssertionFailedException {
//Passing in 17-bytes should throw an exception
byte[] badAddr = new byte[17];
UnknownHostException caught = null;
try {
InetAddress.getByAddress(badAddr);
} catch (UnknownHostException e) {
caught = e;
}
assertNotNull("caught", caught, response);
//Next we try an Ipv4-apped address
byte[] mappedAddr = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 127, 0, 0, 1};
//square brackets should get stripped in name
Inet4Address inet4Addr = (Inet4Address) InetAddress.getByAddress("[localhost]", mappedAddr);
testLocalHost(inet4Addr, response);
//Now we test a real IPv6 address
byte[] addr =
new byte[] {0x10, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0x20, 0xC, 0x41, 0x7A};
String addressString = "1080:0:0:0:8:800:200C:417A";
String hostName = "my.host";
Inet6Address inet6Addr = (Inet6Address) InetAddress.getByAddress(hostName, addr);
testNameAndAddress6(hostName, addressString, addr, 0, inet6Addr, response);
//Now we test a real IPv6 address with a scope ID
int scopeID = 17;
addressString = "1080:0:0:0:8:800:200C:417A%17";
inet6Addr = Inet6Address.getByAddress(hostName, addr, scopeID);
testNameAndAddress6(hostName, addressString, addr, scopeID, inet6Addr, response);
}
static void testSerializedLo0Inet6Address() throws IOException {
System.err.println("\n testSerializedLo0Inet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedLo0Inet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedLo0Inet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
// displayExpectedInet6Address(expectedInet6Address);
byte[] serializedAddress = SerialData_ifname_lo0;
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockLo0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockLo0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockLo0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockLo0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
static void testSerializedE1000gInet6Address() throws IOException {
System.err.println("\n testSerializedE1000gInet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface
.getByName(NETWORK_IF_E1000G0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedE1000gInet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedE1000gInet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
byte[] serializedAddress = SerialData_ifname_e1000g0;
// displayExpectedInet6Address(expectedInet6Address);
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockE1000g0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockE1000g0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockE1000g0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
static void testSerializedLo0Inet6Address() throws IOException {
System.err.println("\n testSerializedLo0Inet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedLo0Inet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedLo0Inet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
// displayExpectedInet6Address(expectedInet6Address);
byte[] serializedAddress = SerialData_ifname_lo0;
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockLo0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockLo0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockLo0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockLo0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
static void testSerializedLo0Inet6Address() throws IOException {
System.err.println("\n testSerializedLo0Inet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedLo0Inet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedLo0Inet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
// displayExpectedInet6Address(expectedInet6Address);
byte[] serializedAddress = SerialData_ifname_lo0;
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockLo0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockLo0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockLo0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockLo0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
static void testSerializedLo0Inet6Address() throws IOException {
System.err.println("\n testSerializedLo0Inet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedLo0Inet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedLo0Inet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME,
LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
// displayExpectedInet6Address(expectedInet6Address);
byte[] serializedAddress = SerialData_ifname_lo0;
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockLo0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockLo0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockLo0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockLo0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockLo0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
@Override
public void decode(byte[] bytes, int offset) {
length = (int)(bytes[offset]|bytes[offset+1]);
int headerSize = 4;
int unprocessedBytes = length - headerSize;
int currentIndex = offset+headerSize;
if(unprocessedBytes > 0){
byte[] readAddress = new byte[16];
System.arraycopy(bytes,currentIndex,readAddress,0,16);
try{
errorNodeAddress = (Inet6Address) Inet6Address.getByAddress(readAddress);
currentIndex = currentIndex + 16;
unprocessedBytes = unprocessedBytes - 16;
}catch(UnknownHostException e){
// FIXME: Poner logs con respecto a excepcion
}
if(unprocessedBytes > 0){
flags = (int)(bytes[currentIndex]);
currentIndex = currentIndex + 1;
unprocessedBytes = unprocessedBytes - 1;
if(unprocessedBytes > 0){
errorCode = (int)(bytes[currentIndex]);
currentIndex = currentIndex + 1;
unprocessedBytes = unprocessedBytes - 1;
if(unprocessedBytes > 0){
errorValue = (int)(bytes[currentIndex]|bytes[currentIndex+1]);
currentIndex = currentIndex + 2;
unprocessedBytes = unprocessedBytes - 2;
}else{
// Malformed Object
}
}else{
// Malformed Object
}
}else{
// Malformed Object
}
}// Malformed Object
}
static void testSerializedE1000gInet6Address() throws IOException {
System.err.println("\n testSerializedE1000gInet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface
.getByName(NETWORK_IF_E1000G0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedE1000gInet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedE1000gInet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
byte[] serializedAddress = SerialData_ifname_e1000g0;
// displayExpectedInet6Address(expectedInet6Address);
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockE1000g0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockE1000g0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockE1000g0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
static void testSerializedE1000gInet6Address() throws IOException {
System.err.println("\n testSerializedE1000gInet6Address: enter \n");
boolean testWithNetIf = true;
boolean useMockInet6Address = false;
NetworkInterface testNetIf = NetworkInterface
.getByName(NETWORK_IF_E1000G0);
Inet6Address expectedInet6Address = null;
if (testNetIf != null) {
System.err
.println("\n testSerializedE1000gInet6Address: using netif \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf);
} catch (UnknownHostException ukhEx) {
ukhEx.printStackTrace();
testWithNetIf = true;
useMockInet6Address = true;
}
} else {
System.err
.println("\n testSerializedE1000gInet6Address: using index \n");
try {
expectedInet6Address = Inet6Address.getByAddress(
E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO);
} catch (UnknownHostException ukhEx1) {
ukhEx1.printStackTrace();
useMockInet6Address = true;
}
testWithNetIf = false;
}
byte[] serializedAddress = SerialData_ifname_e1000g0;
// displayExpectedInet6Address(expectedInet6Address);
try (ByteArrayInputStream bis = new ByteArrayInputStream(
serializedAddress);
ObjectInputStream oin = new ObjectInputStream(bis)) {
Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject();
System.err.println("Deserialized Inet6Address "
+ deserializedIPV6Addr);
if (!useMockInet6Address) {
assertHostNameEqual(expectedInet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
expectedInet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getBareHostAddress(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(expectedInet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
assertScopeIdEqual(expectedInet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
if (testWithNetIf) {
assertNetworkInterfaceEqual(
expectedInet6Address.getScopedInterface(),
deserializedIPV6Addr.getScopedInterface());
} else {
assertNetworkInterfaceEqual(null,
deserializedIPV6Addr.getScopedInterface());
}
} else { // use MockLo0Inet6Address
assertHostNameEqual(MockE1000g0Inet6Address.getHostName(),
deserializedIPV6Addr.getHostName());
if (testWithNetIf) {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddress(),
deserializedIPV6Addr.getHostAddress());
} else {
assertHostAddressEqual(
MockE1000g0Inet6Address.getHostAddressWithIndex(),
deserializedIPV6Addr.getHostAddress());
}
assertAddressEqual(MockE1000g0Inet6Address.getAddress(),
deserializedIPV6Addr.getAddress());
if (testWithNetIf) {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(),
deserializedIPV6Addr.getScopeId());
} else {
assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(),
deserializedIPV6Addr.getScopeId());
}
assertNetworkInterfaceNameEqual(
MockE1000g0Inet6Address.getScopeIfName(),
deserializedIPV6Addr.getScopedInterface());
}
} catch (Exception e) {
System.err.println("Exception caught during deserialization");
failed = true;
e.printStackTrace();
}
}
/**
* Returns the destination IPv6 address.
*
* @return The configured destination IPv6 address.
*
* @throws TimeoutException if there is a timeout reading the IPv6
* destination address.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #setIPv6DestinationAddress(Inet6Address)
* @see java.net.Inet6Address
*
* @since 1.2.1
*/
public Inet6Address getIPv6DestinationAddress() throws TimeoutException, XBeeException {
try {
return (Inet6Address) Inet6Address.getByAddress(getParameter("DL"));
} catch (UnknownHostException e) {
throw new XBeeException(e);
}
}