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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations