use of java.net.Inet4Address in project cw-omnibus by commonsguy.
the class WebServerService method raiseReadyEvent.
private void raiseReadyEvent() {
ServerStartedEvent event = new ServerStartedEvent();
try {
for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces.hasMoreElements(); ) {
NetworkInterface ni = enInterfaces.nextElement();
for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements(); ) {
InetAddress addr = enAddresses.nextElement();
if (addr instanceof Inet4Address) {
event.addUrl("http://" + addr.getHostAddress() + ":4999");
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
EventBus.getDefault().removeAllStickyEvents();
EventBus.getDefault().postSticky(event);
}
use of java.net.Inet4Address in project che by eclipse.
the class DefaultNetworkFinder method getMatchingInetAddress.
/**
* Search if a given network interface is matching the given subnet
* If there is a match, returns the InetAddress
*
* @param subnet
* the first digits of an ip address. Like 123.123.123
* @return optional ipv4 internet address if there was a matching one
*/
@Override
public Optional<InetAddress> getMatchingInetAddress(String subnet) {
Enumeration<NetworkInterface> interfacesEnum = null;
try {
interfacesEnum = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOG.error("Unable to list the network interfaces", e);
return Optional.empty();
}
while (interfacesEnum.hasMoreElements()) {
NetworkInterface networkInterface = interfacesEnum.nextElement();
Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
while (inetAddressEnumeration.hasMoreElements()) {
InetAddress inetAddress = inetAddressEnumeration.nextElement();
if (inetAddress instanceof Inet4Address) {
if (inetAddress.getHostAddress().startsWith(subnet)) {
return Optional.of(inetAddress);
}
}
}
}
return Optional.empty();
}
use of java.net.Inet4Address in project vert.x by eclipse.
the class DnsClientImpl method reverseLookup.
@Override
public DnsClient reverseLookup(String address, Handler<AsyncResult<String>> handler) {
// An other option would be to change address to be of type InetAddress.
try {
InetAddress inetAddress = InetAddress.getByName(address);
byte[] addr = inetAddress.getAddress();
StringBuilder reverseName = new StringBuilder(64);
if (inetAddress instanceof Inet4Address) {
// reverse ipv4 address
reverseName.append(addr[3] & 0xff).append(".").append(addr[2] & 0xff).append(".").append(addr[1] & 0xff).append(".").append(addr[0] & 0xff);
} else {
// It is an ipv 6 address time to reverse it
for (int i = 0; i < 16; i++) {
reverseName.append(HEX_TABLE[(addr[15 - i] & 0xf)]);
reverseName.append(".");
reverseName.append(HEX_TABLE[(addr[15 - i] >> 4) & 0xf]);
if (i != 15) {
reverseName.append(".");
}
}
}
reverseName.append(".in-addr.arpa");
return resolvePTR(reverseName.toString(), handler);
} catch (UnknownHostException e) {
// Should never happen as we work with ip addresses as input
// anyway just in case notify the handler
actualCtx.runOnContext((v) -> handler.handle(Future.failedFuture(e)));
}
return this;
}
use of java.net.Inet4Address in project elasticsearch by elastic.
the class Netty4TransportPublishAddressIT method testDifferentPorts.
public void testDifferentPorts() throws Exception {
if (!NetworkUtils.SUPPORTS_V6) {
return;
}
logger.info("--> starting a node on ipv4 only");
Settings ipv4Settings = Settings.builder().put("network.host", "127.0.0.1").build();
// should bind 127.0.0.1:XYZ
String ipv4OnlyNode = internalCluster().startNode(ipv4Settings);
logger.info("--> starting a node on ipv4 and ipv6");
Settings bothSettings = Settings.builder().put("network.host", "_local_").build();
// should bind [::1]:XYZ and 127.0.0.1:XYZ+1
internalCluster().startNode(bothSettings);
logger.info("--> waiting for the cluster to declare itself stable");
// fails if port of publish address does not match corresponding bound address
ensureStableCluster(2);
logger.info("--> checking if boundAddress matching publishAddress has same port");
NodesInfoResponse nodesInfoResponse = client().admin().cluster().prepareNodesInfo().get();
for (NodeInfo nodeInfo : nodesInfoResponse.getNodes()) {
BoundTransportAddress boundTransportAddress = nodeInfo.getTransport().getAddress();
if (nodeInfo.getNode().getName().equals(ipv4OnlyNode)) {
assertThat(boundTransportAddress.boundAddresses().length, equalTo(1));
assertThat(boundTransportAddress.boundAddresses()[0].getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
} else {
assertThat(boundTransportAddress.boundAddresses().length, greaterThan(1));
for (TransportAddress boundAddress : boundTransportAddress.boundAddresses()) {
assertThat(boundAddress, instanceOf(TransportAddress.class));
TransportAddress inetBoundAddress = (TransportAddress) boundAddress;
if (inetBoundAddress.address().getAddress() instanceof Inet4Address) {
// IPv4 address is preferred publish address for _local_
assertThat(inetBoundAddress.getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
}
}
}
}
}
use of java.net.Inet4Address in project Fling by entertailion.
the class FlingFrame method getPreferredInetAddress.
private InterfaceAddress getPreferredInetAddress(String prefix) {
InterfaceAddress selectedInterfaceAddress = null;
try {
Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces();
while (list.hasMoreElements()) {
NetworkInterface iface = list.nextElement();
if (iface == null)
continue;
Log.d(LOG_TAG, "interface=" + iface.getName());
Iterator<InterfaceAddress> it = iface.getInterfaceAddresses().iterator();
while (it.hasNext()) {
InterfaceAddress interfaceAddress = it.next();
if (interfaceAddress == null)
continue;
InetAddress address = interfaceAddress.getAddress();
Log.d(LOG_TAG, "address=" + address);
if (address instanceof Inet4Address) {
// same subnet as the selected ChromeCast device
if (address.getHostAddress().toString().startsWith(prefix)) {
return interfaceAddress;
}
}
}
}
} catch (Exception ex) {
}
return selectedInterfaceAddress;
}
Aggregations