List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
From source file:org.wso2.carbon.appmgt.keymgt.internal.AppMKeyMgtServiceComponent.java
/** * Get INetAddress by host name or IP Address * * @param host name or host IP String/*w w w . ja v a2 s . c o m*/ * @return InetAddress * @throws java.net.UnknownHostException */ private InetAddress getHostAddress(String host) throws UnknownHostException { String[] splittedString = host.split("\\."); boolean value = checkIfIP(splittedString); if (!value) { return InetAddress.getByName(host); } byte[] byteAddress = new byte[4]; for (int i = 0; i < splittedString.length; i++) { if (Integer.parseInt(splittedString[i]) > 127) { byteAddress[i] = new Integer(Integer.parseInt(splittedString[i]) - 256).byteValue(); } else { byteAddress[i] = Byte.parseByte(splittedString[i]); } } return InetAddress.getByAddress(byteAddress); }
From source file:org.midonet.cluster.ClusterBgpManagerTest.java
@Test public void testAddRoute() throws Throwable { final BGP myBgp = new BGP().setPortId(portId).setPeerAddr(IPv4Addr.fromString("192.168.1.1")).setLocalAS(1) .setPeerAS(2);/* w ww . j av a2 s .c o m*/ UUID bgpId = bgpMgr.create(myBgp); myBgp.setId(bgpId); final AdRoute route = new AdRoute().setPrefixLength((byte) 24) .setNwPrefix(InetAddress.getByAddress(IPv4Addr.stringToBytes("192.168.1.2"))).setBgpId(bgpId); UUID routeId = clusterBgpManager.adRouteMgr.create(Converter.toAdRouteConfig(route)); route.setId(routeId); clusterBgpManager.registerNewBuilder(portId, new FailingBGPListBuilder() { @Override public void addBGP(BGP bgp) { assertEquals(myBgp, bgp); } @Override public void addAdvertisedRoute(AdRoute adRoute) { assertEquals(route, adRoute); } }.expectCalls(2)); }
From source file:org.cesecore.certificates.certificate.certextensions.standard.NameConstraint.java
/** * Formats an encoded name constraint from parseNameConstraintEntry into human-readable form. *//*from w ww. ja v a 2 s . c o m*/ private static String formatNameConstraintEntry(String encoded) { if (encoded == null) { return ""; } int type = getNameConstraintType(encoded); Object data = getNameConstraintData(encoded); switch (type) { case GeneralName.dNSName: case GeneralName.directoryName: return (String) data; // not changed during encoding case GeneralName.iPAddress: byte[] bytes = (byte[]) data; byte[] ip = new byte[bytes.length / 2]; byte[] netmaskBytes = new byte[bytes.length / 2]; System.arraycopy(bytes, 0, ip, 0, ip.length); System.arraycopy(bytes, ip.length, netmaskBytes, 0, netmaskBytes.length); int netmask = 0; for (int i = 0; i < 8 * netmaskBytes.length; i++) { final boolean one = (netmaskBytes[i / 8] >> (7 - i % 8) & 1) == 1; if (one && netmask == i) { netmask++; // leading ones } else if (one) { // trailings ones = error! throw new IllegalArgumentException("Unsupported netmask with mixed ones/zeros"); } } try { return InetAddress.getByAddress(ip).getHostAddress() + "/" + netmask; } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } case GeneralName.rfc822Name: // Prepend @ is it's only the domain part to distinguish from DNS names String str = (String) data; return (str.contains("@") ? str : "@" + str); default: throw new UnsupportedOperationException("Unsupported name constraint type " + type); } }
From source file:edu.umass.cs.nio.MessageNIOTransport.java
/** * @param bytes//w w w . j av a 2 s .com * @return Sender InetSocketAddress from bytes * @throws UnknownHostException */ public static InetSocketAddress getSenderAddress(byte[] bytes) throws UnknownHostException { ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, NIOHeader.BYTES); byte[] addressBytes = new byte[4]; bbuf.get(addressBytes); InetAddress address = InetAddress.getByAddress(addressBytes); int port = (int) bbuf.getShort(); if (port < 0) port += 2 * (Short.MAX_VALUE + 1); return new InetSocketAddress(address, port); }
From source file:org.midonet.cluster.ClusterBgpManagerTest.java
@Test public void testRemoveRoute() throws Throwable { final BGP myBgp = new BGP().setPortId(portId).setPeerAddr(IPv4Addr.fromString("192.168.1.1")).setLocalAS(1) .setPeerAS(2);/*from w w w. ja va2s.c o m*/ UUID bgpId = bgpMgr.create(myBgp); myBgp.setId(bgpId); final AdRoute route = new AdRoute().setPrefixLength((byte) 24) .setNwPrefix(InetAddress.getByAddress(IPv4Addr.stringToBytes("192.168.1.2"))).setBgpId(bgpId); UUID routeId = clusterBgpManager.adRouteMgr.create(Converter.toAdRouteConfig(route)); route.setId(routeId); clusterBgpManager.registerNewBuilder(portId, new FailingBGPListBuilder() { @Override public void addBGP(BGP bgp) { assertEquals(myBgp, bgp); } @Override public void addAdvertisedRoute(AdRoute adRoute) { assertEquals(route, adRoute); } @Override public void removeAdvertisedRoute(AdRoute adRoute) { assertEquals(route, adRoute); } }.expectCalls(3)); clusterBgpManager.adRouteMgr.delete(routeId); }
From source file:Messenger.TorLib.java
/** * This method opens a TOR socket, and does an anonymous DNS resolve through it. * Since Tor caches things, this is a very fast lookup if we've already connected there * The resolve does a gethostbyname() on the exit node. * @param targetHostname String containing the hostname to look up. * @return String representation of the IP address: "x.x.x.x" *///from w w w. jav a2s. co m static String TorResolve(String targetHostname) { int targetPort = 0; // we dont need a port to resolve try { Socket s = TorSocketPre(targetHostname, targetPort, TOR_RESOLVE); DataInputStream is = new DataInputStream(s.getInputStream()); byte version = is.readByte(); byte status = is.readByte(); if (status != (byte) 90) { //failed for some reason, return useful exception throw (new IOException(ParseSOCKSStatus(status))); } int port = is.readShort(); byte[] ipAddrBytes = new byte[4]; is.read(ipAddrBytes); InetAddress ia = InetAddress.getByAddress(ipAddrBytes); //System.out.println("Resolved into:"+ia); is.close(); String addr = ia.toString().substring(1); // clip off the "/" return (addr); } catch (Exception e) { e.printStackTrace(); } return (null); }
From source file:edu.umass.cs.nio.MessageNIOTransport.java
/** * @param bytes/* w ww . j a va 2 s . c o m*/ * @return Sender InetSocketAddress from bytes * @throws UnknownHostException */ public static InetSocketAddress getReceiverAddress(byte[] bytes) throws UnknownHostException { ByteBuffer bbuf = ByteBuffer.wrap(bytes, 6, NIOHeader.BYTES); byte[] addressBytes = new byte[4]; bbuf.get(addressBytes); InetAddress address = InetAddress.getByAddress(addressBytes); int port = (int) bbuf.getShort(); if (port < 0) port += 2 * (Short.MAX_VALUE + 1); return new InetSocketAddress(address, port); }
From source file:com.epam.reportportal.apache.http.impl.conn.TestPoolingHttpClientConnectionManager.java
@Test public void testProxyConnectAndUpgrade() throws Exception { final HttpHost target = new HttpHost("somehost", -1, "https"); final HttpHost proxy = new HttpHost("someproxy", 8080); final InetAddress remote = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 }); final InetAddress local = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }); final HttpRoute route = new HttpRoute(target, local, proxy, true); final CPoolEntry entry = new CPoolEntry(LogFactory.getLog(getClass()), "id", route, conn, -1, TimeUnit.MILLISECONDS); entry.markRouteComplete();/*from w w w . j ava 2s.com*/ Mockito.when(future.isCancelled()).thenReturn(Boolean.FALSE); Mockito.when(conn.isOpen()).thenReturn(true); Mockito.when(future.isCancelled()).thenReturn(false); Mockito.when(future.get(1, TimeUnit.SECONDS)).thenReturn(entry); Mockito.when(pool.lease(route, null, null)).thenReturn(future); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(conn1); final ConnectionSocketFactory plainsf = Mockito.mock(ConnectionSocketFactory.class); final LayeredConnectionSocketFactory sslsf = Mockito.mock(LayeredConnectionSocketFactory.class); final Socket socket = Mockito.mock(Socket.class); final HttpClientContext context = HttpClientContext.create(); final SocketConfig sconfig = SocketConfig.custom().build(); final ConnectionConfig cconfig = ConnectionConfig.custom().build(); mgr.setDefaultSocketConfig(sconfig); mgr.setDefaultConnectionConfig(cconfig); Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] { remote }); Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8080); Mockito.when(schemePortResolver.resolve(target)).thenReturn(8443); Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainsf); Mockito.when(socketFactoryRegistry.lookup("https")).thenReturn(sslsf); Mockito.when(plainsf.createSocket(Mockito.<HttpContext>any())).thenReturn(socket); Mockito.when(plainsf.connectSocket(Mockito.anyInt(), Mockito.eq(socket), Mockito.<HttpHost>any(), Mockito.<InetSocketAddress>any(), Mockito.<InetSocketAddress>any(), Mockito.<HttpContext>any())) .thenReturn(socket); mgr.connect(conn1, route, 123, context); Mockito.verify(dnsResolver, Mockito.times(1)).resolve("someproxy"); Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(proxy); Mockito.verify(plainsf, Mockito.times(1)).createSocket(context); Mockito.verify(plainsf, Mockito.times(1)).connectSocket(123, socket, proxy, new InetSocketAddress(remote, 8080), new InetSocketAddress(local, 0), context); Mockito.when(conn.getSocket()).thenReturn(socket); mgr.upgrade(conn1, route, context); Mockito.verify(schemePortResolver, Mockito.times(1)).resolve(target); Mockito.verify(sslsf, Mockito.times(1)).createLayeredSocket(socket, "somehost", 8443, context); mgr.routeComplete(conn1, route, context); }
From source file:com.jagornet.dhcp.db.JdbcIaAddressDAO.java
public List<InetAddress> findExistingIPs(final InetAddress startAddr, final InetAddress endAddr) { return getJdbcTemplate().query("select ipaddress from iaaddress" + " where ipaddress >= ? and ipaddress <= ?" + " order by ipaddress", new PreparedStatementSetter() { @Override//from w w w. j av a2 s . c o m public void setValues(PreparedStatement ps) throws SQLException { ps.setBytes(1, startAddr.getAddress()); ps.setBytes(2, endAddr.getAddress()); } }, new RowMapper<InetAddress>() { @Override public InetAddress mapRow(ResultSet rs, int rowNum) throws SQLException { InetAddress inetAddr = null; try { inetAddr = InetAddress.getByAddress(rs.getBytes("ipaddress")); } catch (UnknownHostException e) { // re-throw as SQLException throw new SQLException("Unable to map ipaddress", e); } return inetAddr; } }); }
From source file:com.jagornet.dhcp.db.JdbcIaPrefixDAO.java
public List<InetAddress> findExistingIPs(final InetAddress startAddr, final InetAddress endAddr) { return getJdbcTemplate().query("select prefixaddress from iaprefix" + " where prefixaddress >= ? and prefixaddress <= ?" + " order by prefixaddress", new PreparedStatementSetter() { @Override//from www. ja v a2 s . co m public void setValues(PreparedStatement ps) throws SQLException { ps.setBytes(1, startAddr.getAddress()); ps.setBytes(2, endAddr.getAddress()); } }, new RowMapper<InetAddress>() { @Override public InetAddress mapRow(ResultSet rs, int rowNum) throws SQLException { InetAddress inetAddr = null; try { inetAddr = InetAddress.getByAddress(rs.getBytes("prefixaddress")); } catch (UnknownHostException e) { // re-throw as SQLException throw new SQLException("Unable to map prefixaddress", e); } return inetAddr; } }); }