Skip to content

Commit

Permalink
InetEndpoint: Add support for IP4P address format.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Oct 9, 2023
1 parent 04e503c commit e21fb5b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tunnel/src/main/java/com/wireguard/config/InetEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.wireguard.util.NonNullForAll;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -103,7 +105,16 @@ public Optional<InetEndpoint> getResolved() {
break;
}
}
resolved = new InetEndpoint(address.getHostAddress(), true, port);
if (address instanceof Inet6Address) {
byte[] v6 = address.getAddress();
if ((v6[0] == 0x20) && (v6[1] == 0x01) && (v6[2] == 0x00) && (v6[3] == 0x00)) {
InetAddress v4 = InetAddress.getByAddress(Arrays.copyOfRange(v6, 12, 16));
int p = ((v6[10] & 0xFF) << 8) | (v6[11] & 0xFF);
resolved = new InetEndpoint(v4.getHostAddress(), true, p);
}
}
if (resolved == null)
resolved = new InetEndpoint(address.getHostAddress(), true, port);
lastResolution = Instant.now();
} catch (final UnknownHostException e) {
resolved = null;
Expand Down

0 comments on commit e21fb5b

Please sign in to comment.