Files
KLALB/src/org/kne/cloud/network/ipv6/Inet6AddressGroup.java
T
2025-11-15 11:08:16 +08:00

143 lines
4.0 KiB
Java

package org.kne.cloud.network.ipv6;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
public class Inet6AddressGroup implements Comparable<Inet6AddressGroup>{
private static final byte[][] maskTransf=new byte[129][16];
static {
for (int i = 0; i < 129; i++) {
for(int j=0;j<i;j++) {
maskTransf[i][j/8]=(byte) (((0b10000000)>>>j%8)|maskTransf[i][j/8]);
}
}
/* for (int i = 0; i < maskTransf.length; i++) {
try {
System.out.println(InetAddress.getByAddress(maskTransf[i]));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}*/
}
public Inet6AddressGroup(Inet6Address address) {
this(address, 128);
}
@Override
public String toString() {
return address.getHostAddress()+"/"+prefixLength;
}
@Override
public int hashCode() {
return Objects.hash(address, prefixLength);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Inet6AddressGroup other = (Inet6AddressGroup) obj;
return Objects.equals(address, other.address) && prefixLength == other.prefixLength;
}
public Inet6AddressGroup(Inet6Address address, int prefixLength) {
super();
this.address = address;
this.prefixLength = prefixLength;
}
public Inet6AddressGroup(DataInputStream in) throws IOException {
readFromStream(in);
}
private Inet6Address address;
private int prefixLength=128;
public Inet6Address getAddress() {
return address;
}
public int getPrefixLength() {
return prefixLength;
}
private byte[] cachedRawAddress;
public byte[] getRawAddress() {
if(cachedRawAddress==null) {
return (cachedRawAddress=address.getAddress());
}else {
return cachedRawAddress;
}
}
private byte[] cachedAndm;
private byte[] getAndm(byte[]mask) {
if(cachedAndm==null) {
return (cachedAndm=and0(mask ,getRawAddress()));
}else {
return cachedAndm;
}
}
public boolean checkMatch(Inet6Address ia) {
byte[]mask=maskTransf[prefixLength];
byte[]andm=getAndm(mask);
return andeq0(mask,ia.getAddress(),andm);
}
public boolean checkMatch(byte[] ia) {
byte[]mask=maskTransf[prefixLength];
byte[]andm=getAndm(mask);
return andeq0(mask,ia,andm);
}
private static boolean andeq0(byte[] bs, byte[] address2, byte[] andm) {
for (int i = 0; i < bs.length; i++) {
if(andm[i]!=(byte) (bs[i]&address2[i])) {
return false;
}
}
return true;
}
private static byte[] and0(byte[] bs, byte[] address2) {
byte[]rez=new byte[bs.length];
for (int i = 0; i < rez.length; i++) {
rez[i]=(byte) (bs[i]&address2[i]);
}
return rez;
}
@Override
public int compareTo(Inet6AddressGroup o) {
return -Integer.compare(prefixLength, o.prefixLength);
}
public void writeToStream(DataOutputStream out) throws IOException {
out.write(address.getAddress());
out.write(prefixLength);
}
public void readFromStream(DataInputStream in) throws IOException {
byte[]b=new byte[16];
in.readFully(b);
address=(Inet6Address) Inet6Address.getByAddress(b);
prefixLength=in.read();
}
public Inet6AddressGroup createPrefixOnlyAddressGroup(int newPrefixLength) {
byte[]mask=maskTransf[newPrefixLength];
byte[]andm=getAndm(mask);
try {
return new Inet6AddressGroup((Inet6Address) Inet6Address.getByAddress(andm), newPrefixLength);
} catch (UnknownHostException e) {
return null;
}
}
public Inet6AddressGroup createPrefixOnlyAddressGroup() {
return createPrefixOnlyAddressGroup(prefixLength);
}
public static void main(String[] args) throws UnknownHostException {
Inet6AddressGroup i6ag=new Inet6AddressGroup((Inet6Address) Inet6Address.getByName("1234:1234::1234"),12);
System.out.println(i6ag);
System.out.println(i6ag.createPrefixOnlyAddressGroup());
}
}