Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASI VirtualSocket interface and Java networking classes implementation #998

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2025 Maksim Tiushev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.net;

public final class TInet4Address extends TInetAddress {
static final int INADDRSZ = 4;

TInet4Address(byte[] addr, String hostname) {
super(addr, hostname, IPv4);
validateIPv4Address(addr);
}

private static void validateIPv4Address(byte[] addr) {
if (addr.length != INADDRSZ) {
throw new IllegalArgumentException("IPv4 address must be exactly 4 bytes long.");
}
}

@Override
public boolean isAnyLocalAddress() {
return address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0;
}

@Override
public boolean isLoopbackAddress() {
return address[0] == 127;
}

@Override
public boolean isLinkLocalAddress() {
return (address[0] & 0xFF) == 169 && (address[1] & 0xFF) == 254;
}

@Override
public boolean isMCGlobal() {
return (address[0] & 0xFF) >= 224
&& (address[0] & 0xFF) <= 238
&& !(address[0] == 224 && address[1] == 0 && address[2] == 0);
}

@Override
public boolean isMCLinkLocal() {
return (address[0] & 0xFF) == 224 && (address[1] & 0xFF) == 0;
}

@Override
public boolean isMCOrgLocal() {
return (address[0] & 0xFF) == 239 && (address[1] & 0xFF) >= 192;
}

@Override
public boolean isMCSiteLocal() {
return (address[0] & 0xFF) == 239 && (address[1] & 0xFF) == 255;
}

@Override
public boolean isMulticastAddress() {
return (address[0] & 0xFF) >= 224 && (address[0] & 0xFF) <= 239;
}

@Override
public boolean isSiteLocalAddress() {
int firstOctet = address[0] & 0xFF;
int secondOctet = address[1] & 0xFF;

return (firstOctet == 10)
|| (firstOctet == 172 && secondOctet >= 16 && secondOctet <= 31)
|| (firstOctet == 192 && secondOctet == 168);
}
}
154 changes: 154 additions & 0 deletions classlib/src/main/java/org/teavm/classlib/java/net/TInet6Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2025 Maksim Tiushev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.net;

public final class TInet6Address extends TInetAddress {
static final int INADDRSZ = 16;

TInet6Address(byte[] addr, String hostname) {
super(addr, hostname, IPv6);
validateIPv6Address(addr);
}

private static void validateIPv6Address(byte[] addr) {
if (addr.length != INADDRSZ) {
throw new IllegalArgumentException("IPv6 address must be exactly 16 bytes long.");
}
}

@Override
public String getHostAddress() {
StringBuilder sb = new StringBuilder();
int[] segments = new int[8];

for (int i = 0; i < 8; i++) {
segments[i] = ((address[i * 2] & 0xFF) << 8) | (address[i * 2 + 1] & 0xFF);
}

int startZero = -1;
int maxZeroLength = 0;
int zeroLength = 0;
int currentStart = -1;
for (int i = 0; i < segments.length; i++) {
if (segments[i] == 0) {
if (currentStart == -1) {
currentStart = i;
}
zeroLength++;
} else {
if (zeroLength > maxZeroLength) {
maxZeroLength = zeroLength;
startZero = currentStart;
}
zeroLength = 0;
currentStart = -1;
}
}
if (zeroLength > maxZeroLength) {
maxZeroLength = zeroLength;
startZero = currentStart;
}

for (int i = 0; i < segments.length; i++) {
if (startZero == i && maxZeroLength > 1) {
if (i == 0) {
sb.append("::");
} else {
sb.append(':');
}
i += maxZeroLength - 1;
continue;
}
sb.append(Integer.toHexString(segments[i]));
if (i < segments.length - 1) {
sb.append(':');
}
}
return sb.toString();
}

@Override
public boolean isAnyLocalAddress() {
for (byte b : address) {
if (b != 0) {
return false;
}
}
return true;
}

public boolean isIPv4CompatibleAddress() {
for (int i = 0; i < 12; i++) {
if (address[i] != 0) {
return false;
}
}
return true;
}

@Override
public boolean isLinkLocalAddress() {
return (address[0] & 0xFF) == 0xFE && (address[1] & 0xC0) == 0x80;
}

@Override
public boolean isLoopbackAddress() {
if (address[15] != 1) {
return false;
}
for (int i = 0; i < 15; i++) {
if (address[i] != 0) {
return false;
}
}
return true;
}

@Override
public boolean isMCGlobal() {
return (address[0] & 0xFF) == 0xFF && (address[1] & 0x0F) == 0x0E;
}

@Override
public boolean isMCLinkLocal() {
return (address[0] & 0xFF) == 0xFF && (address[1] & 0x0F) == 0x02;
}

@Override
public boolean isMCNodeLocal() {
return (address[0] & 0xFF) == 0xFF && (address[1] & 0x0F) == 0x01;
}

@Override
public boolean isMCOrgLocal() {
return (address[0] & 0xFF) == 0xFF && (address[1] & 0x0F) == 0x08;
}

@Override
public boolean isMCSiteLocal() {
return (address[0] & 0xFF) == 0xFF && (address[1] & 0x0F) == 0x05;
}

@Override
public boolean isMulticastAddress() {
return (address[0] & 0xFF) == 0xFF;
}

@Override
public boolean isSiteLocalAddress() {
return (address[0] & 0xFF) == 0xFE && (address[1] & 0xC0) == 0xC0;
}
}
Loading