2018-04-18 05:28:31 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
|
|
|
|
import java.net.Inet4Address;
|
|
|
|
import java.net.Inet6Address;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
|
|
|
public class IPCidr implements Parcelable {
|
|
|
|
InetAddress address;
|
|
|
|
int cidr;
|
|
|
|
|
|
|
|
|
|
|
|
public static final Parcelable.Creator<IPCidr> CREATOR = new Parcelable.Creator<IPCidr>() {
|
|
|
|
@Override
|
|
|
|
public IPCidr createFromParcel(final Parcel in) {
|
|
|
|
return new IPCidr(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPCidr[] newArray(final int size) {
|
|
|
|
return new IPCidr[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
public IPCidr(String in) {
|
2018-04-18 05:28:31 +02:00
|
|
|
parse(in);
|
|
|
|
}
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
private void parse(String in) {
|
2018-04-18 05:28:31 +02:00
|
|
|
cidr = -1;
|
|
|
|
int slash = in.lastIndexOf('/');
|
|
|
|
if (slash != -1 && slash < in.length() - 1) {
|
|
|
|
try {
|
|
|
|
cidr = Integer.parseInt(in.substring(slash + 1), 10);
|
|
|
|
in = in.substring(0, slash);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 18:29:14 +02:00
|
|
|
address = Attribute.parseIPString(in);
|
2018-04-18 05:28:31 +02:00
|
|
|
if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0))
|
|
|
|
cidr = 128;
|
|
|
|
else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0))
|
|
|
|
cidr = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
public InetAddress getAddress() {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCidr() {
|
|
|
|
return cidr;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return String.format("%s/%d", address.getHostAddress(), cidr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(final Parcel dest, final int flags) {
|
|
|
|
dest.writeString(this.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IPCidr(final Parcel in) {
|
|
|
|
try {
|
|
|
|
parse(in.readString());
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|