List of usage examples for java.net InetAddress getAddress
public byte[] getAddress()
From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java
/** * Creates a new task manager address reply packet. * /*from w ww .j a v a 2 s . co m*/ * @param taskManagerAddress * the address of the task manager which sent the request * @param magicNumber * the magic number to identify this discovery service * @return a new task manager address reply packet */ private static DatagramPacket createTaskManagerAddressReplyPacket(final InetAddress taskManagerAddress, final int magicNumber) { final byte[] addr = taskManagerAddress.getAddress(); final byte[] bytes = new byte[20 + addr.length]; integerToByteArray(magicNumber, MAGIC_NUMBER_OFFSET, bytes); integerToByteArray(generateRandomPacketID(), PACKET_ID_OFFSET, bytes); integerToByteArray(TM_ADDRESS_REPLY_ID, PACKET_TYPE_ID_OFFSET, bytes); integerToByteArray(addr.length, PAYLOAD_OFFSET, bytes); System.arraycopy(addr, 0, bytes, PAYLOAD_OFFSET + 4, addr.length); return new DatagramPacket(bytes, bytes.length); }
From source file:nl.nn.adapterframework.util.Misc.java
static private byte[] getIPAddress() { InetAddress inetAddress = null; try {/* ww w. ja va 2s.co m*/ inetAddress = InetAddress.getLocalHost(); return inetAddress.getAddress(); } catch (UnknownHostException uhe) { return new byte[] { 127, 0, 0, 1 }; } }
From source file:de.fhg.fokus.diameter.DiameterPeer.peer.StateMachine.java
private static byte[] convertIpAddToByteArray(String localIp) { byte[] address = null; try {/*from w w w . j a v a 2s .co m*/ InetAddress bindLocalAdd = InetAddress.getByName(localIp); address = bindLocalAdd.getAddress(); } catch (UnknownHostException e) { log.error(e, e); e.printStackTrace(); } return address; }
From source file:com.buaa.cfs.utils.NetUtils.java
/** * Create a socket address with the given host and port. The hostname might be replaced with another host that was * set via {@link #addStaticResolution(String, String)}. The value of hadoop.security.token.service.use_ip will * determine whether the standard java host resolver is used, or if the fully qualified resolver is used. * * @param host the hostname or IP use to instantiate the object * @param port the port number/*from ww w. j a v a 2s . c o m*/ * * @return InetSocketAddress */ public static InetSocketAddress createSocketAddrForHost(String host, int port) { String staticHost = getStaticResolution(host); String resolveHost = (staticHost != null) ? staticHost : host; InetSocketAddress addr; try { InetAddress iaddr = SecurityUtil.getByName(resolveHost); // if there is a static entry for the host, make the returned // address look like the original given host if (staticHost != null) { iaddr = InetAddress.getByAddress(host, iaddr.getAddress()); } addr = new InetSocketAddress(iaddr, port); } catch (UnknownHostException e) { addr = InetSocketAddress.createUnresolved(host, port); } return addr; }
From source file:com.eislab.af.translator.Translator_hub_i.java
public static boolean ipv6InRange(String localIp, int localMask, String remoteIp) { InetAddress remoteAddress = parseAddress(remoteIp); InetAddress requiredAddress = parseAddress(localIp); int nMaskBits = localMask; if (!requiredAddress.getClass().equals(remoteAddress.getClass())) { return false; }//from ww w . j a v a 2 s. c om if (nMaskBits <= 0) { return remoteAddress.equals(requiredAddress); } byte[] remAddr = remoteAddress.getAddress(); byte[] reqAddr = requiredAddress.getAddress(); int oddBits = nMaskBits % 8; int nMaskBytes = nMaskBits / 8 + (oddBits == 0 ? 0 : 1); byte[] mask = new byte[nMaskBytes]; Arrays.fill(mask, 0, oddBits == 0 ? mask.length : mask.length - 1, (byte) 0xFF); if (oddBits != 0) { int finalByte = (1 << oddBits) - 1; finalByte <<= 8 - oddBits; mask[mask.length - 1] = (byte) finalByte; } // System.out.println("Mask is " + new sun.misc.HexDumpEncoder().encode(mask)); for (int i = 0; i < mask.length; i++) { if ((remAddr[i] & mask[i]) != (reqAddr[i] & mask[i])) { return false; } } return true; }
From source file:com.edmunds.etm.loadbalancer.impl.OrderedInet4Address.java
public OrderedInet4Address(InetAddress inetAddress) { this(inetAddress.getAddress()); }
From source file:Uuid32Generator.java
public String generate() { StringBuilder strRetVal = new StringBuilder(); String strTemp;/*from w w w . j a v a2 s . co m*/ try { // IPAddress segment InetAddress addr = InetAddress.getLocalHost(); byte[] ipaddr = addr.getAddress(); for (byte anIpaddr : ipaddr) { Byte b = new Byte(anIpaddr); strTemp = Integer.toHexString(b.intValue() & 0x000000ff); strRetVal.append(ZEROS.substring(0, 2 - strTemp.length())); strRetVal.append(strTemp); } strRetVal.append(':'); // CurrentTimeMillis() segment strTemp = Long.toHexString(System.currentTimeMillis()); strRetVal.append(ZEROS.substring(0, 12 - strTemp.length())); strRetVal.append(strTemp).append(':'); // random segment SecureRandom prng = SecureRandom.getInstance("SHA1PRNG"); strTemp = Integer.toHexString(prng.nextInt()); while (strTemp.length() < 8) { strTemp = '0' + strTemp; } strRetVal.append(strTemp.substring(4)).append(':'); // IdentityHash() segment strTemp = Long.toHexString(System.identityHashCode(this)); strRetVal.append(ZEROS.substring(0, 8 - strTemp.length())); strRetVal.append(strTemp); } catch (UnknownHostException uhex) { throw new RuntimeException("Unknown host.", uhex); } catch (NoSuchAlgorithmException nsaex) { throw new RuntimeException("Algorithm 'SHA1PRNG' is unavailiable.", nsaex); } return strRetVal.toString().toUpperCase(); }
From source file:com.ok2c.lightmtp.util.InetAddressRange.java
public boolean contains(final InetAddress ip) { BigInteger bigint2 = new BigInteger(ip.getAddress()); if (this.shiftBy > 0) { bigint2 = bigint2.shiftRight(this.shiftBy); }// w w w .jav a 2s .co m return this.bigint.compareTo(bigint2) == 0; }
From source file:com.medsphere.ovid.common.uuid.KeyGenerator.java
public KeyGenerator() { try {/* ww w .ja v a 2 s. c o m*/ InetAddress inet = InetAddress.getLocalHost(); byte[] bytes = inet.getAddress(); String inetAddressInHex = new String(Hex.encodeHex(bytes)); String hashCodeForThis = new String(Hex.encodeHex(intToByteArray(System.identityHashCode(this)))); middle = inetAddressInHex + hashCodeForThis; seeder = new SecureRandom(); seeder.nextInt(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); seeder = new SecureRandom(); seeder.nextInt(); middle = new Integer(this.hashCode()).toString(); } }
From source file:pw.simplyintricate.bitcoin.models.datastructures.NetworkAddress.java
public NetworkAddress(UnsignedInteger services, String ipAddress, int port) { try {/*from ww w . j a va 2s . c om*/ InetAddress ip = InetAddress.getByName(ipAddress); this.ipAddress = ip.getAddress(); } catch (UnknownHostException e) { throw new IllegalArgumentException("The ip address is invalid!", e); } this.services = services; this.port = port; }