use of java.net.InterfaceAddress in project qpid-broker-j by apache.
the class AutoGeneratedSelfSignedKeyStoreImpl method generatePrivateKeyAndCertificate.
private void generatePrivateKeyAndCertificate() {
try {
Set<InetAddress> addresses = new HashSet<>();
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
for (InterfaceAddress inetAddress : networkInterface.getInterfaceAddresses()) {
addresses.add(inetAddress.getAddress());
}
}
Set<String> dnsNames = new HashSet<>();
for (InetAddress address : addresses) {
String hostName = address.getHostName();
if (hostName != null) {
dnsNames.add(hostName);
}
String canonicalHostName = address.getCanonicalHostName();
if (canonicalHostName != null) {
dnsNames.add(canonicalHostName);
}
}
long startTime = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(startTime);
calendar.add(Calendar.MONTH, _durationInMonths);
long duration = (calendar.getTimeInMillis() - startTime) / 1000;
final SSLUtil.KeyCertPair keyCertPair = SSLUtil.generateSelfSignedCertificate(_keyAlgorithm, _signatureAlgorithm, _keyLength, startTime, duration, "CN=Qpid", dnsNames, addresses);
_privateKey = keyCertPair.getPrivateKey();
_certificate = keyCertPair.getCertificate();
_generated = true;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | IOException e) {
throw new IllegalConfigurationException("Unable to construct keystore", e);
}
}
use of java.net.InterfaceAddress in project cordova-plugin-networkinterface by salbahra.
the class networkinterface method getIPv4Subnet.
public static String getIPv4Subnet(InetAddress inetAddress) {
try {
NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);
List<InterfaceAddress> intAddrs = ni.getInterfaceAddresses();
for (InterfaceAddress ia : intAddrs) {
if (!ia.getAddress().isLoopbackAddress() && ia.getAddress() instanceof Inet4Address) {
return getIPv4SubnetFromNetPrefixLength(ia.getNetworkPrefixLength()).getHostAddress().toString();
}
}
} catch (Exception e) {
}
return "";
}
use of java.net.InterfaceAddress in project android_frameworks_base by crdroidandroid.
the class LinkAddressTest method testConstructors.
public void testConstructors() throws SocketException {
LinkAddress address;
// Valid addresses work as expected.
address = new LinkAddress(V4_ADDRESS, 25);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(25, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
address = new LinkAddress(V6_ADDRESS, 127);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(127, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
// Nonsensical flags/scopes or combinations thereof are acceptable.
address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(64, address.getPrefixLength());
assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
assertEquals(RT_SCOPE_LINK, address.getScope());
address = new LinkAddress(V4 + "/23", 123, 456);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(23, address.getPrefixLength());
assertEquals(123, address.getFlags());
assertEquals(456, address.getScope());
// InterfaceAddress doesn't have a constructor. Fetch some from an interface.
List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
// We expect to find 127.0.0.1/8 and ::1/128, in any order.
LinkAddress ipv4Loopback, ipv6Loopback;
assertEquals(2, addrs.size());
if (addrs.get(0).getAddress() instanceof Inet4Address) {
ipv4Loopback = new LinkAddress(addrs.get(0));
ipv6Loopback = new LinkAddress(addrs.get(1));
} else {
ipv4Loopback = new LinkAddress(addrs.get(1));
ipv6Loopback = new LinkAddress(addrs.get(0));
}
assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
assertEquals(8, ipv4Loopback.getPrefixLength());
assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
assertEquals(128, ipv6Loopback.getPrefixLength());
// Null addresses are rejected.
try {
address = new LinkAddress(null, 24);
fail("Null InetAddress should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("Null string should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((InterfaceAddress) null);
fail("Null string should cause NullPointerException");
} catch (NullPointerException expected) {
}
// Invalid prefix lengths are rejected.
try {
address = new LinkAddress(V4_ADDRESS, -1);
fail("Negative IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, -1);
fail("Negative IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4_ADDRESS, 33);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
// Multicast addresses are rejected.
try {
address = new LinkAddress("224.0.0.2/32");
fail("IPv4 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress("ff02::1/128");
fail("IPv6 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
use of java.net.InterfaceAddress in project android_frameworks_base by AOSPA.
the class LinkAddressTest method testConstructors.
public void testConstructors() throws SocketException {
LinkAddress address;
// Valid addresses work as expected.
address = new LinkAddress(V4_ADDRESS, 25);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(25, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
address = new LinkAddress(V6_ADDRESS, 127);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(127, address.getPrefixLength());
assertEquals(0, address.getFlags());
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
// Nonsensical flags/scopes or combinations thereof are acceptable.
address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
assertEquals(V6_ADDRESS, address.getAddress());
assertEquals(64, address.getPrefixLength());
assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
assertEquals(RT_SCOPE_LINK, address.getScope());
address = new LinkAddress(V4 + "/23", 123, 456);
assertEquals(V4_ADDRESS, address.getAddress());
assertEquals(23, address.getPrefixLength());
assertEquals(123, address.getFlags());
assertEquals(456, address.getScope());
// InterfaceAddress doesn't have a constructor. Fetch some from an interface.
List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
// We expect to find 127.0.0.1/8 and ::1/128, in any order.
LinkAddress ipv4Loopback, ipv6Loopback;
assertEquals(2, addrs.size());
if (addrs.get(0).getAddress() instanceof Inet4Address) {
ipv4Loopback = new LinkAddress(addrs.get(0));
ipv6Loopback = new LinkAddress(addrs.get(1));
} else {
ipv4Loopback = new LinkAddress(addrs.get(1));
ipv6Loopback = new LinkAddress(addrs.get(0));
}
assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
assertEquals(8, ipv4Loopback.getPrefixLength());
assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
assertEquals(128, ipv6Loopback.getPrefixLength());
// Null addresses are rejected.
try {
address = new LinkAddress(null, 24);
fail("Null InetAddress should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("Null string should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress((InterfaceAddress) null);
fail("Null string should cause NullPointerException");
} catch (NullPointerException expected) {
}
// Invalid prefix lengths are rejected.
try {
address = new LinkAddress(V4_ADDRESS, -1);
fail("Negative IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, -1);
fail("Negative IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4_ADDRESS, 33);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
// Multicast addresses are rejected.
try {
address = new LinkAddress("224.0.0.2/32");
fail("IPv4 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
address = new LinkAddress("ff02::1/128");
fail("IPv6 multicast address should cause IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
}
use of java.net.InterfaceAddress in project NetGuard by M66B.
the class ServiceSinkhole method getDns.
public static List<InetAddress> getDns(Context context) {
List<InetAddress> listDns = new ArrayList<>();
List<String> sysDns = Util.getDefaultDNS(context);
// Get custom DNS servers
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean ip6 = prefs.getBoolean("ip6", true);
String vpnDns1 = prefs.getString("dns", null);
String vpnDns2 = prefs.getString("dns2", null);
Log.i(TAG, "DNS system=" + TextUtils.join(",", sysDns) + " VPN1=" + vpnDns1 + " VPN2=" + vpnDns2);
if (vpnDns1 != null)
try {
InetAddress dns = InetAddress.getByName(vpnDns1);
if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress()) && (ip6 || dns instanceof Inet4Address))
listDns.add(dns);
} catch (Throwable ignored) {
}
if (vpnDns2 != null)
try {
InetAddress dns = InetAddress.getByName(vpnDns2);
if (!(dns.isLoopbackAddress() || dns.isAnyLocalAddress()) && (ip6 || dns instanceof Inet4Address))
listDns.add(dns);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
// Use system DNS servers only when no two custom DNS servers specified
if (listDns.size() <= 1)
for (String def_dns : sysDns) try {
InetAddress ddns = InetAddress.getByName(def_dns);
if (!listDns.contains(ddns) && !(ddns.isLoopbackAddress() || ddns.isAnyLocalAddress()) && (ip6 || ddns instanceof Inet4Address))
listDns.add(ddns);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
// Remove local DNS servers when not routing LAN
boolean lan = prefs.getBoolean("lan", false);
boolean use_hosts = prefs.getBoolean("filter", false) && prefs.getBoolean("use_hosts", false);
if (lan && use_hosts) {
List<InetAddress> listLocal = new ArrayList<>();
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
if (nis != null)
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni != null && ni.isUp() && !ni.isLoopback()) {
List<InterfaceAddress> ias = ni.getInterfaceAddresses();
if (ias != null)
for (InterfaceAddress ia : ias) {
InetAddress hostAddress = ia.getAddress();
BigInteger host = new BigInteger(1, hostAddress.getAddress());
int prefix = ia.getNetworkPrefixLength();
BigInteger mask = BigInteger.valueOf(-1).shiftLeft(hostAddress.getAddress().length * 8 - prefix);
for (InetAddress dns : listDns) if (hostAddress.getAddress().length == dns.getAddress().length) {
BigInteger ip = new BigInteger(1, dns.getAddress());
if (host.and(mask).equals(ip.and(mask))) {
Log.i(TAG, "Local DNS server host=" + hostAddress + "/" + prefix + " dns=" + dns);
listLocal.add(dns);
}
}
}
}
}
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
List<InetAddress> listDns4 = new ArrayList<>();
List<InetAddress> listDns6 = new ArrayList<>();
try {
listDns4.add(InetAddress.getByName("8.8.8.8"));
listDns4.add(InetAddress.getByName("8.8.4.4"));
if (ip6) {
listDns6.add(InetAddress.getByName("2001:4860:4860::8888"));
listDns6.add(InetAddress.getByName("2001:4860:4860::8844"));
}
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
for (InetAddress dns : listLocal) {
listDns.remove(dns);
if (dns instanceof Inet4Address) {
if (listDns4.size() > 0) {
listDns.add(listDns4.get(0));
listDns4.remove(0);
}
} else {
if (listDns6.size() > 0) {
listDns.add(listDns6.get(0));
listDns6.remove(0);
}
}
}
}
return listDns;
}
Aggregations