use of java.net.NetworkInterface in project Openfire by igniterealtime.
the class LocalIPResolver method getLocalIP.
public static String getLocalIP() {
if (overrideIp != null && overrideIp.length() >= 7) {
return overrideIp;
}
Enumeration ifaces;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
Enumeration iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
}
}
}
ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
Enumeration iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
}
}
}
return InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "127.0.0.1";
}
use of java.net.NetworkInterface in project screenbird by adamhub.
the class MediaUtil method getMacAddress.
/**
* Returns the MAC address for the current computer.
* @return
*/
public static String getMacAddress() {
StringBuilder macAddress = new StringBuilder();
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
}
} catch (SocketException e) {
log(e);
} catch (UnknownHostException e) {
log(e);
}
return macAddress.toString();
}
use of java.net.NetworkInterface in project ats-framework by Axway.
the class HostUtils method getIpAddressesList.
/**
* Return a list with addresses.
* if called with true, it will try to return the first IPv4 address or if there isn't such - the last IPv6 address
* if called with false, it will return a list with all IP addresses
*
* @return
*/
private static List<InetAddress> getIpAddressesList(boolean exitOnFirstIPv4) {
//list containing all ip addresses
List<InetAddress> ipList = new ArrayList<InetAddress>();
try {
// cycle all net interfaces
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) netInterfaces.nextElement();
if (!netInterface.isLoopback()) {
// for each net interface cycle all IP addresses
Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses();
InetAddress ipAddress = null;
while (ipAddresses.hasMoreElements()) {
ipAddress = (InetAddress) ipAddresses.nextElement();
if (ipAddress instanceof java.net.Inet4Address) {
Inet4Address ipv4 = (Inet4Address) ipAddress;
if (!ipv4.isLoopbackAddress() && !LOCAL_HOST_NAME.equals(ipv4.getCanonicalHostName()) && !LOCAL_HOST_IPv4.equals(ipv4.getCanonicalHostName())) {
// we found an appropriate IPv4 address
ipList.add(ipv4);
if (exitOnFirstIPv4) {
break;
}
}
} else //if( ip instanceof java.net.Inet6Address )
{
Inet6Address ipv6 = (Inet6Address) ipAddress;
// FIXME: currently we do not filter out the temporary IPv6 addresses
if (!ipv6.isLinkLocalAddress() && !LOCAL_HOST_IPv6.equals(ipv6.getCanonicalHostName())) {
// We found an appropriate IPv6 address. Add it to the list, but keep searching for an appropriate IPv4 address.
ipList.add(ipv6);
}
}
}
}
}
} catch (SocketException se) {
log.error("Error obtaining the local host address", se);
}
/*
* We will return either list with the IP addresses or empty list
*/
if (exitOnFirstIPv4 && !ipList.isEmpty()) {
int listSize = ipList.size();
return ipList.subList(listSize - 1, listSize);
}
return ipList;
}
use of java.net.NetworkInterface in project ats-framework by Axway.
the class HostUtils method isLocalHost.
/**
* Tells if host is the local host. Note it will return true if:
* <ul>
* <li>host is null,</li>
* <li>empty string,</li>
* <li>IP of the local host including 127.0.0.1,</li>
* <li>the name of the local host including "localhost".</li>
* </ul>
* Otherwise it will return false.
*
* @param host
* the host to check
* @return if host is the local host
*/
public static boolean isLocalHost(String host) {
if (StringUtils.isNullOrEmpty(host)) {
// we assume a local host
return true;
} else if (localHosts.contains(host)) {
// we already know this is a local host
return true;
} else if (nonlocalHosts.contains(host)) {
// we already know this is not a local host
return false;
} else {
// unknown host, check if it is local or not
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
// we hope this will never happen
log.error("Error obtaining info about this system's network interfaces. We will assume '" + host + "' is a local host", e);
return true;
}
while (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
String hostAddress = inetAddress.getHostAddress();
if (inetAddress instanceof Inet6Address) {
if (hostAddress != null && stripIPv6InterfaceId(compressIPv6Address(host)).equalsIgnoreCase(stripIPv6InterfaceId(compressIPv6Address(hostAddress)))) {
localHosts.add(host);
return true;
}
} else if (host.equalsIgnoreCase(hostAddress)) {
localHosts.add(host);
return true;
}
String hostName = inetAddress.getHostName();
if (host.equalsIgnoreCase(hostName)) {
localHosts.add(host);
return true;
}
}
}
nonlocalHosts.add(host);
return false;
}
}
use of java.net.NetworkInterface in project kcanotify by antest1.
the class KcaVpnService method getBuilder.
private Builder getBuilder(List<Rule> listAllowed, List<Rule> listRule) {
SharedPreferences prefs = getSharedPreferences("pref", Context.MODE_PRIVATE);
boolean subnet = prefs.getBoolean("subnet", false);
boolean tethering = prefs.getBoolean("tethering", false);
boolean lan = prefs.getBoolean("lan", false);
boolean ip6 = prefs.getBoolean("ip6", true);
boolean filter = prefs.getBoolean("filter", false);
boolean system = prefs.getBoolean("manage_system", false);
// Build VPN service
Builder builder = new Builder();
builder.setSession(getString(R.string.app_name));
// VPN address
String vpn4 = prefs.getString("vpn4", "10.1.10.1");
Log.i(TAG, "vpn4=" + vpn4);
builder.addAddress(vpn4, 32);
if (ip6) {
String vpn6 = prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1");
Log.i(TAG, "vpn6=" + vpn6);
builder.addAddress(vpn6, 128);
}
// DNS address
if (filter)
for (InetAddress dns : getDns(KcaVpnService.this)) {
if (ip6 || dns instanceof Inet4Address) {
Log.i(TAG, "dns=" + dns);
builder.addDnsServer(dns);
}
}
// Subnet routing
if (subnet) {
// Exclude IP ranges
List<IPUtil.CIDR> listExclude = new ArrayList<>();
// localhost
listExclude.add(new IPUtil.CIDR("127.0.0.0", 8));
if (tethering) {
// USB tethering 192.168.42.x
// Wi-Fi tethering 192.168.43.x
listExclude.add(new IPUtil.CIDR("192.168.42.0", 23));
// Wi-Fi direct 192.168.49.x
listExclude.add(new IPUtil.CIDR("192.168.49.0", 24));
}
if (lan) {
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni != null && ni.isUp() && !ni.isLoopback() && ni.getName() != null && !ni.getName().startsWith("tun"))
for (InterfaceAddress ia : ni.getInterfaceAddresses()) if (ia.getAddress() instanceof Inet4Address) {
IPUtil.CIDR local = new IPUtil.CIDR(ia.getAddress(), ia.getNetworkPrefixLength());
Log.i(TAG, "Excluding " + ni.getName() + " " + local);
listExclude.add(local);
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
Configuration config = getResources().getConfiguration();
if (config.mcc == 310 && config.mnc == 260) {
// T-Mobile Wi-Fi calling
listExclude.add(new IPUtil.CIDR("66.94.2.0", 24));
listExclude.add(new IPUtil.CIDR("66.94.6.0", 23));
listExclude.add(new IPUtil.CIDR("66.94.8.0", 22));
listExclude.add(new IPUtil.CIDR("208.54.0.0", 16));
}
// broadcast
listExclude.add(new IPUtil.CIDR("224.0.0.0", 3));
Collections.sort(listExclude);
try {
InetAddress start = InetAddress.getByName("0.0.0.0");
for (IPUtil.CIDR exclude : listExclude) {
Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..." + exclude.getEnd().getHostAddress());
for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) try {
builder.addRoute(include.address, include.prefix);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
start = IPUtil.plus1(exclude.getEnd());
}
for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255")) try {
builder.addRoute(include.address, include.prefix);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} catch (UnknownHostException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} else {
String addresses = prefs.getString("bypass_address", "");
if (!addresses.equals("")) {
List<IPUtil.CIDR> listExclude = new ArrayList<>();
for (String cidr : addresses.split(",")) {
String[] cidr_split = cidr.trim().split("/");
listExclude.add(new IPUtil.CIDR(cidr_split[0], Integer.parseInt(cidr_split[1])));
}
Collections.sort(listExclude);
try {
InetAddress start = InetAddress.getByName("0.0.0.0");
for (IPUtil.CIDR exclude : listExclude) {
for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) try {
builder.addRoute(include.address, include.prefix);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
start = IPUtil.plus1(exclude.getEnd());
}
for (IPUtil.CIDR include : IPUtil.toCIDR(start.getHostAddress(), "255.255.255.255")) try {
builder.addRoute(include.address, include.prefix);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} catch (UnknownHostException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} else
builder.addRoute("0.0.0.0", 0);
}
Log.i(TAG, "IPv6=" + ip6);
if (ip6)
builder.addRoute("0:0:0:0:0:0:0:0", 0);
// MTU
int mtu = jni_get_mtu();
Log.i(TAG, "MTU=" + mtu);
builder.setMtu(mtu);
return builder;
}
Aggregations