Search in sources :

Example 91 with Inet4Address

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);
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 92 with Inet4Address

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();
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 93 with Inet4Address

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;
}
Also used : DatagramDnsQueryEncoder(io.netty.handler.codec.dns.DatagramDnsQueryEncoder) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DnsResponse(io.netty.handler.codec.dns.DnsResponse) DnsException(io.vertx.core.dns.DnsException) ChannelOption(io.netty.channel.ChannelOption) ContextImpl(io.vertx.core.impl.ContextImpl) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) SrvRecord(io.vertx.core.dns.SrvRecord) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DatagramChannel(io.netty.channel.socket.DatagramChannel) DnsSection(io.netty.handler.codec.dns.DnsSection) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DnsResponseCode(io.vertx.core.dns.DnsResponseCode) AsyncResult(io.vertx.core.AsyncResult) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) VertxInternal(io.vertx.core.impl.VertxInternal) ChannelInitializer(io.netty.channel.ChannelInitializer) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) PartialPooledByteBufAllocator(io.vertx.core.net.impl.PartialPooledByteBufAllocator) ChannelPipeline(io.netty.channel.ChannelPipeline) Future(io.vertx.core.Future) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) RecordDecoder(io.vertx.core.dns.impl.decoder.RecordDecoder) ChannelFuture(io.netty.channel.ChannelFuture) DnsClient(io.vertx.core.dns.DnsClient) Objects(java.util.Objects) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DatagramDnsResponseDecoder(io.netty.handler.codec.dns.DatagramDnsResponseDecoder) MxRecord(io.vertx.core.dns.MxRecord) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress)

Example 94 with Inet4Address

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()));
                }
            }
        }
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) Inet4Address(java.net.Inet4Address) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 95 with Inet4Address

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;
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

Inet4Address (java.net.Inet4Address)184 InetAddress (java.net.InetAddress)85 Inet6Address (java.net.Inet6Address)45 NetworkInterface (java.net.NetworkInterface)39 UnknownHostException (java.net.UnknownHostException)28 LinkAddress (android.net.LinkAddress)24 SocketException (java.net.SocketException)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)19 InterfaceAddress (java.net.InterfaceAddress)17 ByteBuffer (java.nio.ByteBuffer)17 RouteInfo (android.net.RouteInfo)12 LinkProperties (android.net.LinkProperties)7 InetSocketAddress (java.net.InetSocketAddress)6 Test (org.junit.Test)6 WifiDisplay (android.hardware.display.WifiDisplay)5 WifiDisplaySessionInfo (android.hardware.display.WifiDisplaySessionInfo)5 DhcpResults (android.net.DhcpResults)5 IpConfiguration (android.net.IpConfiguration)5 IpAssignment (android.net.IpConfiguration.IpAssignment)5