Setting:
- FritzBox 7530 v7.29
- forwarding ports 7, 9, 80 and 433 IPv4 and IPv6 to HomeServer
- CentOS Stream 8 HomeServer directly connected to FritzBox via Ethernet cable
- own public IPv6 address, shared IPv4 address with other ISP customers
- IPv6 DynDNS service correctly updating IPv6 prefix
- Java script to send magic packets
- forwarding works, apache and other services reachable when HomeServer is running
- need to be able to start HomeServer remotely via WakeOnLan
Question: Why does the following script only start the HomeServer sporadically over the DynDNS domain or IPv6 address? Only the local broadcast IPv4 works every time.
Script:
import java.net.*;
public class WakeOnLan {
public static final int PORT = 9;
public static void main(String[] args) {
// works every time
// local broadcast works every time
String ipStr = "192.168.178.255";
// works only sometimes
// local link adress works only sometimes
String ipStr = "fe80::IPv6 mac address of target";
// works only sometimes
// public IPv6 prefix with IPv6 mac address of target
String ipStr = "public IPv6 prefix:IPv6 mac address of target";
// mac adress of target
String macStr = "mac address of target";
try {
byte[] macBytes = getMacBytes(macStr);
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
InetAddress address = InetAddress.getByName(ipStr);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
System.out.println("Wake-on-LAN packet sent to: "+address);
}
catch (Exception e) {
System.out.println("Failed to send Wake-on-LAN packet:"+e);
System.exit(1);
}
}
private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
}
}