String s = "10.1.1.99"; Inet4Address a = (Inet4Address) InetAddress.getByName(s); byte[] b = a.getAddress(); int i = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | ((b[3] & 0xFF) << 0);
/** * Resolves IP address from a hostname. */ public static String resolveIpAddress(String hostname) { try { InetAddress netAddress; if (hostname == null || hostname.equalsIgnoreCase(LOCAL_HOST)) { netAddress = InetAddress.getLocalHost(); } else { netAddress = Inet4Address.getByName(hostname); } return netAddress.getHostAddress(); } catch (UnknownHostException ignore) { return null; } }
@Test public void inetAddress() { assertThat(IPUtil.fromInt(-1062731775).getHostAddress()).isEqualTo("192.168.0.1"); assertThat(IPUtil.fromInt(-1062731774).getHostAddress()).isEqualTo("192.168.0.2"); assertThat(IPUtil.fromIpString("192.168.0.1").getHostAddress()).isEqualTo("192.168.0.1"); assertThat(IPUtil.fromIpString("192.168.0.2").getHostAddress()).isEqualTo("192.168.0.2"); assertThat(IPUtil.fromIpv4String("192.168.0.1").getHostAddress()).isEqualTo("192.168.0.1"); assertThat(IPUtil.fromIpv4String("192.168.0.2").getHostAddress()).isEqualTo("192.168.0.2"); assertThat(IPUtil.toInt(IPUtil.fromIpString("192.168.0.1"))).isEqualTo(-1062731775); } }
protected SocketAddress localAddress(DatagramChannel handle) throws Exception { InetSocketAddress inetSocketAddress = (InetSocketAddress) handle.socket().getLocalSocketAddress(); InetAddress inetAddress = inetSocketAddress.getAddress(); if ((inetAddress instanceof Inet6Address) && (((Inet6Address) inetAddress).isIPv4CompatibleAddress())) { // Ugly hack to workaround a problem on linux : the ANY address is always converted to IPV6 // even if the original address was an IPV4 address. We do store the two IPV4 and IPV6 // ANY address in the map. byte[] ipV6Address = ((Inet6Address) inetAddress).getAddress(); byte[] ipV4Address = new byte[4]; System.arraycopy(ipV6Address, 12, ipV4Address, 0, 4); InetAddress inet4Adress = Inet4Address.getByAddress(ipV4Address); return new InetSocketAddress(inet4Adress, inetSocketAddress.getPort()); } else { return inetSocketAddress; } }
AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides); server.resumeAccepts(); channels.add(server); AcceptingChannel<? extends StreamConnection> server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), acceptListener, socketOptionsWithOverrides); server.resumeAccepts(); channels.add(server); if (listener.useProxyProtocol) { ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(new ProxyProtocolOpenListener(openListener, xnioSsl, buffers, socketOptionsWithOverrides)); sslServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides); } else { ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener); sslServer = xnioSsl.createSslConnectionServer(worker, new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port), (ChannelListener) acceptListener, socketOptionsWithOverrides);
@SneakyThrows(UnknownHostException.class) private static String getHostAddress() { return Inet4Address.getLocalHost().getHostAddress(); } }
@Override public void run() { try { Log.d("DXDX", Build.MANUFACTURER + ": conn listener: " + mPort); mServer = new ServerSocket(mPort); mServer.setReuseAddress(true); if (mServer != null && !mServer.isBound()) { mServer.bind(new InetSocketAddress(mPort)); } Log.d("DDDD", "Inet4Address: " + Inet4Address.getLocalHost().getHostAddress()); Socket socket = null; while (acceptRequests) { // this is a blocking operation socket = mServer.accept(); handleData(socket.getInetAddress().getHostAddress(), socket.getInputStream()); } Log.e("DXDX", Build.MANUFACTURER + ": Connection listener terminated. " + "acceptRequests: " + acceptRequests); socket.close(); socket = null; } catch (IOException e) { Log.e("DXDX", Build.MANUFACTURER + ": Connection listener EXCEPTION. " + e.toString()); e.printStackTrace(); } }
@Bean public Candidate candidate(LeaderProperties leaderProperties) throws UnknownHostException { String id = Inet4Address.getLocalHost().getHostName(); String role = leaderProperties.getRole(); return new DefaultCandidate(id, role); }
List <NSDServiceInfo> Data = new ArrayList<>(); InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress()); byte [] addressBytes = hostInet.getAddress(); Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet)); Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes); Log.d ( "IP", "IPv4" + dest4.getHostAddress ()); Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());
private static String getUid(Configuration config) { final String localHostErrMsg = "Cannot determine local host"; final String uid; if (config.has(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_HOSTNAME) && config.get(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_HOSTNAME)) { try { uid = Inet4Address.getLocalHost().getHostName(); } catch (UnknownHostException e) { throw new JanusGraphConfigurationException(localHostErrMsg, e); } } else { final byte[] addrBytes; try { addrBytes = Inet4Address.getLocalHost().getAddress(); } catch (UnknownHostException e) { throw new JanusGraphConfigurationException(localHostErrMsg, e); } uid = new String(Hex.encodeHex(addrBytes)); } return uid; }
@Test // Everything works fine, everyone gets through. public void testNewDNSServer() throws Exception { String format = "192.168.0.%d"; byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, format, ipv6Template, i4addr); assertTrue(thread.upstreamDnsServers.contains(i4addr)); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); thread.newDNSServer(builder, format, ipv6Template, i6addr); assertTrue(thread.upstreamDnsServers.contains(i6addr)); assertEquals(3, ipv6Template[ipv6Template.length - 1]); assertTrue(serversAdded.contains(InetAddress.getByAddress(ipv6Template))); }
public void writeBson(final BsonWriter bsonWriter) { try { bsonWriter.writeString(Inet4Address.getByAddress(this.ip_v4.value).getHostAddress()); } catch (UnknownHostException e) { Throwables.propagate(e); } } }
@Override public void accept(final InodeIdentifiedSocketDescriptor descriptor, final ByteBuffer buffer) { final Inet4Address address = (Inet4Address) descriptor.getSocketAddress().getAddress(); buffer.putInt(address.hashCode()); buffer.putInt(descriptor.getSocketAddress().getPort()); buffer.putLong(descriptor.getInode()); } }
@Test // IPv6 is disabled: We only get IPv4 servers through public void testNewDNSServer_ipv6disabled() throws Exception { byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; InetAddress i6addr = Inet6Address.getByName("::1"); thread.newDNSServer(builder, "192.168.0.%d", null, i6addr); assertTrue(serversAdded.isEmpty()); assertTrue(thread.upstreamDnsServers.isEmpty()); InetAddress i4addr = Inet4Address.getByName("127.0.0.1"); thread.newDNSServer(builder, "192.168.0.%d", null, i4addr); assertTrue(serversAdded.contains(InetAddress.getByName("192.168.0.2"))); assertTrue(thread.upstreamDnsServers.contains(i4addr)); }
byte[] bytes = ip.getAddress(); boolean leadingBytesOfZero = true; for (int i = 0; i < 15; ++i) { if (hasEmbeddedIPv4ClientAddress(ip6)) { addressAsLong = getEmbeddedIPv4ClientAddress(ip6).hashCode(); } else { addressAsLong = ByteBuffer.wrap(ip6.getAddress(), 0, 8).getLong(); return getInet4Address(Ints.toByteArray(coercedHash));
.srcPort(UdpPort.DOMAIN) .dstPort(UdpPort.DOMAIN) .srcAddr(InetAddress.getByAddress(new byte[]{8, 8, 4, 4})) .dstAddr(InetAddress.getByAddress(new byte[]{8, 8, 8, 8})) .correctChecksumAtBuild(true) .correctLengthAtBuild(true) .tos(IpV4Rfc791Tos.newInstance((byte) 0)) .protocol(IpNumber.UDP) .srcAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 4, 4})) .dstAddr((Inet4Address) Inet4Address.getByAddress(new byte[]{8, 8, 8, 8})) .correctChecksumAtBuild(true) .correctLengthAtBuild(true) assertEquals(Inet4Address.getByAddress(new byte[]{8, 8, 8, 8}), mockEventLoop.lastOutgoing.getAddress()); dnsPacketProxy.upstreamDnsServers.add(Inet4Address.getByAddress(new byte[]{1, 1, 1, 2})); dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData()); dnsPacketProxy.upstreamDnsServers.add(Inet4Address.getByAddress(new byte[]{1, 1, 1, i})); dnsPacketProxy.handleDnsRequest(ipOutPacket.getRawData()); assertEquals(Inet4Address.getByAddress(new byte[]{1, 1, 1, 8}), mockEventLoop.lastOutgoing.getAddress()); assertNull(mockEventLoop.lastResponse);
protected boolean doesSupportZoneDNS() { try { return Arrays.equals(Inet4Address.getByName("testzone1.localhost").getAddress(), new byte[] { 127, 0, 0, 1 }) && Arrays.equals(Inet4Address.getByName("testzone2.localhost").getAddress(), new byte[] { 127, 0, 0, 1 }) && Arrays.equals(Inet4Address.getByName("testzone3.localhost").getAddress(), new byte[] { 127, 0, 0, 1 }); } catch (UnknownHostException e) { return false; } }
byte[] bytes = ip.getAddress(); boolean leadingBytesOfZero = true; for (int i = 0; i < 15; ++i) { long addressAsLong = 0; if (hasEmbeddedIPv4ClientAddress(ip6)) { addressAsLong = getEmbeddedIPv4ClientAddress(ip6).hashCode(); } else { addressAsLong = ByteBuffer.wrap(ip6.getAddress(), 0, 8).getLong();
private static String computeUniqueInstanceId(Configuration config) { final String suffix; if (config.has(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_SUFFIX)) { suffix = LongEncoding.encode(config.get( GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID_SUFFIX)); } else { suffix = ManagementFactory.getRuntimeMXBean().getName() + LongEncoding.encode(INSTANCE_COUNTER.incrementAndGet()); } byte[] addrBytes; try { addrBytes = Inet4Address.getLocalHost().getAddress(); } catch (UnknownHostException e) { throw new TitanConfigurationException("Cannot determine local host", e); } String uid = new String(Hex.encodeHex(addrBytes)) + suffix; for (char c : ConfigElement.ILLEGAL_CHARS) { uid = StringUtils.replaceChars(uid,c,'-'); } return uid; }
private static Inet6Address toCompactAddress(Inet4Address a4) { byte[] b4 = a4.getAddress(); byte[] b6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b4[0], b4[1], b4[2], b4[3] }; try { return (Inet6Address) InetAddress.getByAddress(b6); } catch (UnknownHostException e) { throw new Error(e); } }