blob: a52dedb90e52c0e3e0d2345a571a41ba468d1e67 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
* @author smorgrav
*/
package com.yahoo.vespa.hosted.node.admin.docker;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
class NetworkPrefixTranslator {
/**
* For NPTed networks we want to find the private address from a public.
*
* @param address The original address to translate
* @param prefix The prefix address
* @param subnetSize in bits - e.g a /64 subnet equals 64 bits
* @return The translated address
*/
static Inet6Address translate(InetAddress address, InetAddress prefix, int subnetSize) {
byte[] originalAddress = address.getAddress();
byte[] prefixAddress = prefix.getAddress();
byte[] translatedAddress = new byte[16];
for (int i = 0; i < 16; i++) {
translatedAddress[i] = i < subnetSize / 8 ? prefixAddress[i] : originalAddress[i];
}
try {
return (Inet6Address) InetAddress.getByAddress(address.getHostName(), translatedAddress);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
|