use of java.net.InterfaceAddress in project android_frameworks_base by AOSPA.
the class NetworkManagementService method modifyNat.
private void modifyNat(String action, String internalInterface, String externalInterface) throws SocketException {
final Command cmd = new Command("nat", action, internalInterface, externalInterface);
final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(internalInterface);
if (internalNetworkInterface == null) {
cmd.appendArg("0");
} else {
// Don't touch link-local routes, as link-local addresses aren't routable,
// kernel creates link-local routes on all interfaces automatically
List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(internalNetworkInterface.getInterfaceAddresses());
cmd.appendArg(interfaceAddresses.size());
for (InterfaceAddress ia : interfaceAddresses) {
InetAddress addr = NetworkUtils.getNetworkPart(ia.getAddress(), ia.getNetworkPrefixLength());
cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
}
}
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
use of java.net.InterfaceAddress in project cxf by apache.
the class UDPDestination method findNetworkInterface.
private NetworkInterface findNetworkInterface() throws SocketException {
String name = (String) this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE);
NetworkInterface ret = null;
if (!StringUtils.isEmpty(name)) {
ret = NetworkInterface.getByName(name);
}
if (ret == null) {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> possibles = new ArrayList<>();
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
if (ni.supportsMulticast() && ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof java.net.Inet4Address && !ia.getAddress().isLoopbackAddress() && !ni.getDisplayName().startsWith("vnic")) {
possibles.add(ni);
}
}
}
}
ret = possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
}
return ret;
}
use of java.net.InterfaceAddress in project Payara by payara.
the class DomainDiscoveryService method addLocalNodes.
private void addLocalNodes(List<DiscoveryNode> nodes, int port) throws SocketException, NumberFormatException {
Enumeration e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e.nextElement();
if (!ni.isLoopback()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof Inet4Address && !ia.getAddress().isLoopbackAddress()) {
logger.log(Level.FINE, "Adding network interface {0}", ia.getAddress());
nodes.add(new SimpleDiscoveryNode(new Address(ia.getAddress(), port)));
}
}
}
}
}
use of java.net.InterfaceAddress in project scheduling by ow2-proactive.
the class BroadcastDiscoveryClient method broadcastToAllInterfaces.
private void broadcastToAllInterfaces(DatagramSocket c) {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (isLoopBack(networkInterface)) {
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null) {
continue;
}
try {
DatagramPacket sendPacket = new DatagramPacket(new byte[0], 0, broadcast, port);
c.send(sendPacket);
} catch (Exception e) {
logger.warn("Could not broadcast to interface " + networkInterface.getName(), e);
}
}
}
} catch (SocketException e) {
logger.warn("Could not broadcast to all interfaces", e);
}
}
use of java.net.InterfaceAddress in project fdroidclient by f-droid.
the class WifiStateChangeService method setIpInfoFromNetworkInterface.
private void setIpInfoFromNetworkInterface() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
if (networkInterfaces == null) {
return;
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface netIf = networkInterfaces.nextElement();
for (Enumeration<InetAddress> inetAddresses = netIf.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.isLoopbackAddress() || inetAddress instanceof Inet6Address) {
continue;
}
if (netIf.getDisplayName().contains("wlan0") || netIf.getDisplayName().contains("eth0") || netIf.getDisplayName().contains("ap0")) {
FDroidApp.ipAddressString = inetAddress.getHostAddress();
for (InterfaceAddress address : netIf.getInterfaceAddresses()) {
short networkPrefixLength = address.getNetworkPrefixLength();
if (networkPrefixLength > 32) {
// java.lang.IllegalArgumentException: Value [64] not in range [0,32]
continue;
}
if (inetAddress.equals(address.getAddress()) && !TextUtils.isEmpty(FDroidApp.ipAddressString)) {
String cidr = String.format(Locale.ENGLISH, "%s/%d", FDroidApp.ipAddressString, networkPrefixLength);
FDroidApp.subnetInfo = new SubnetUtils(cidr).getInfo();
break;
}
}
}
}
}
} catch (SocketException e) {
Log.e(TAG, "Could not get ip address", e);
}
}
Aggregations