Search in sources :

Example 11 with Inet4Address

use of java.net.Inet4Address in project hudson-2.x by hudson.

the class Computer method getHostName.

/**
     * This method tries to compute the name of the host that's reachable by all the other nodes.
     *
     * <p>
     * Since it's possible that the slave is not reachable from the master (it may be behind a firewall,
     * connecting to master via JNLP), this method may return null.
     *
     * It's surprisingly tricky for a machine to know a name that other systems can get to,
     * especially between things like DNS search suffix, the hosts file, and YP.
     *
     * <p>
     * So the technique here is to compute possible interfaces and names on the slave,
     * then try to ping them from the master, and pick the one that worked.
     *
     * <p>
     * The computation may take some time, so it employs caching to make the successive lookups faster.
     *
     * @since 1.300
     * @return
     *      null if the host name cannot be computed (for example because this computer is offline,
     *      because the slave is behind the firewall, etc.) 
     */
public String getHostName() throws IOException, InterruptedException {
    if (hostNameCached)
        // in the worst case we end up having multiple threads computing the host name simultaneously, but that's not harmful, just wasteful.
        return cachedHostName;
    VirtualChannel channel = getChannel();
    // can't compute right now
    if (channel == null)
        return null;
    for (String address : channel.call(new ListPossibleNames())) {
        try {
            InetAddress ia = InetAddress.getByName(address);
            if (!(ia instanceof Inet4Address)) {
                LOGGER.fine(address + " is not an IPv4 address");
                continue;
            }
            if (!ComputerPinger.checkIsReachable(ia, 3)) {
                LOGGER.fine(address + " didn't respond to ping");
                continue;
            }
            cachedHostName = ia.getCanonicalHostName();
            hostNameCached = true;
            return cachedHostName;
        } catch (IOException e) {
            // if a given name fails to parse on this host, we get this error
            LOGGER.log(Level.FINE, "Failed to parse " + address, e);
        }
    }
    // allow the administrator to manually specify the host name as a fallback. HUDSON-5373
    cachedHostName = channel.call(new GetFallbackName());
    hostNameCached = true;
    return null;
}
Also used : Inet4Address(java.net.Inet4Address) VirtualChannel(hudson.remoting.VirtualChannel) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 12 with Inet4Address

use of java.net.Inet4Address in project netty by netty.

the class DefaultHostsFileEntriesResolverTest method shouldntFindWhenAddressTypeDoesntMatch.

@Test
public void shouldntFindWhenAddressTypeDoesntMatch() {
    Map<String, Inet4Address> inet4Entries = new HashMap<String, Inet4Address>();
    Map<String, Inet6Address> inet6Entries = new HashMap<String, Inet6Address>();
    inet4Entries.put("localhost", NetUtil.LOCALHOST4);
    DefaultHostsFileEntriesResolver resolver = new DefaultHostsFileEntriesResolver(new HostsFileEntries(inet4Entries, inet6Entries));
    InetAddress address = resolver.address("localhost", ResolvedAddressTypes.IPV6_ONLY);
    Assert.assertNull("Should pick an IPv6 address", address);
}
Also used : Inet4Address(java.net.Inet4Address) HashMap(java.util.HashMap) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 13 with Inet4Address

use of java.net.Inet4Address in project netty by netty.

the class DefaultHostsFileEntriesResolverTest method shouldPickIpv4WhenBothAreDefinedButIpv4IsPreferred.

@Test
public void shouldPickIpv4WhenBothAreDefinedButIpv4IsPreferred() {
    Map<String, Inet4Address> inet4Entries = new HashMap<String, Inet4Address>();
    Map<String, Inet6Address> inet6Entries = new HashMap<String, Inet6Address>();
    inet4Entries.put("localhost", NetUtil.LOCALHOST4);
    inet6Entries.put("localhost", NetUtil.LOCALHOST6);
    DefaultHostsFileEntriesResolver resolver = new DefaultHostsFileEntriesResolver(new HostsFileEntries(inet4Entries, inet6Entries));
    InetAddress address = resolver.address("localhost", ResolvedAddressTypes.IPV4_PREFERRED);
    Assert.assertTrue("Should pick an IPv4 address", address instanceof Inet4Address);
}
Also used : Inet4Address(java.net.Inet4Address) HashMap(java.util.HashMap) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 14 with Inet4Address

use of java.net.Inet4Address in project platformlayer by platformlayer.

the class SocatPeerToPeerCopy method findChannel.

private InetAddressPair findChannel(OpsTarget src, OpsTarget target) throws OpsException {
    InetAddress srcAddress = ((SshOpsTarget) src).getHost();
    InetAddress targetAddress = ((SshOpsTarget) target).getHost();
    if (srcAddress instanceof Inet4Address) {
        if (targetAddress instanceof Inet6Address) {
            Inet6Address srcIpv6 = findIpv6(src);
            if (srcIpv6 != null) {
                srcAddress = srcIpv6;
            } else {
                throw new UnsupportedOperationException();
            }
        }
    } else {
        if (targetAddress instanceof Inet4Address) {
            Inet6Address targetIpv6 = findIpv6(target);
            if (targetIpv6 != null) {
                targetAddress = targetIpv6;
            } else {
                throw new UnsupportedOperationException();
            }
        }
    }
    return new InetAddressPair(srcAddress, targetAddress);
}
Also used : Inet4Address(java.net.Inet4Address) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 15 with Inet4Address

use of java.net.Inet4Address in project platform_frameworks_base by android.

the class WifiDisplayController method getInterfaceAddress.

private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
    NetworkInterface iface;
    try {
        iface = NetworkInterface.getByName(info.getInterface());
    } catch (SocketException ex) {
        Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface(), ex);
        return null;
    }
    Enumeration<InetAddress> addrs = iface.getInetAddresses();
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr instanceof Inet4Address) {
            return (Inet4Address) addr;
        }
    }
    Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface() + " because it had no IPv4 addresses.");
    return null;
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

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