Java Code Examples for java.net.Inet6Address.getByAddress()

The following are Jave code examples for showing how to use getByAddress() of the java.net.Inet6Address class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: monarch   File: JGAddress.java   Source Code and License Vote up 6 votes
@Override
public void readFrom(DataInput in) throws Exception {
  int len = in.readByte();
  if (len > 0 && (len != Global.IPV4_SIZE && len != Global.IPV6_SIZE))
    throw new IOException("length has to be " + Global.IPV4_SIZE + " or " + Global.IPV6_SIZE
        + " bytes (was " + len + " bytes)");
  byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
  in.readFully(a);
  if (len == Global.IPV6_SIZE) {
    int scope_id = in.readInt();
    this.ip_addr = Inet6Address.getByAddress(null, a, scope_id);
  } else {
    this.ip_addr = InetAddress.getByAddress(a);
  }

  // changed from readShort(): we need the full 65535, with a short we'd only get up to 32K !
  port = in.readUnsignedShort();
  vmViewId = in.readInt();
  mostSigBits = in.readLong();
  leastSigBits = in.readLong();
}
 
Example 2
Project: Daedalus   File: DaedalusVpnService.java   Source Code and License Vote up 6 votes
private InetAddress addDnsServer(Builder builder, String format, byte[] ipv6Template, InetAddress address) throws UnknownHostException {
    int size = dnsServers.size();
    size++;
    if (address instanceof Inet6Address && ipv6Template == null) {
        Log.i(TAG, "addDnsServer: Ignoring DNS server " + address);
    } else if (address instanceof Inet4Address) {
        String alias = String.format(format, size + 1);
        dnsServers.put(alias, address.getHostAddress());
        builder.addRoute(alias, 32);
        return InetAddress.getByName(alias);
    } else if (address instanceof Inet6Address) {
        ipv6Template[ipv6Template.length - 1] = (byte) (size + 1);
        InetAddress i6addr = Inet6Address.getByAddress(ipv6Template);
        dnsServers.put(i6addr.getHostAddress(), address.getHostAddress());
        return i6addr;
    }
    return null;
}
 
Example 3
Project: mac-address-detector-java   File: MacAddressHelper.java   Source Code and License Vote up 5 votes
/**
 * The broadcast ip for a icmp v6 request is replaced all bytes except the last three bytes with _IPv6_BROADCAST_IPADDRESS_PREFIX
 *
 * @param inet6Address
 * @return
 */
private Inet6Address _getBroadcastIPAddress4IPv6(Inet6Address inet6Address) throws UnknownHostException {
    //"fe80:0:0:0:250:56ff:febc:2688" -> "FF02::1:FFbc:2688"
    byte[] ipInBytes = inet6Address.getAddress();
    byte[] broadcastIpAddress = new byte[ipInBytes.length];
    System.arraycopy(_IPv6_BROADCAST_IPADDRESS_PREFIX, 0, broadcastIpAddress, 0, _IPv6_BROADCAST_IPADDRESS_PREFIX.length);
    int reservedBytes = 3;
    System.arraycopy(ipInBytes, ipInBytes.length - reservedBytes, broadcastIpAddress, _IPv6_BROADCAST_IPADDRESS_PREFIX.length -
            reservedBytes, reservedBytes);
    return (Inet6Address) Inet6Address.getByAddress(broadcastIpAddress);
}
 
Example 4
Project: elasticsearch_my   File: NetworkAddressTests.java   Source Code and License Vote up 4 votes
/** creates scoped ipv6 address without any lookups. hostname can be null, for missing */
private InetAddress forgeScoped(String hostname, String address, int scopeid) throws IOException {
    byte bytes[] = InetAddress.getByName(address).getAddress();
    return Inet6Address.getByAddress(hostname, bytes, scopeid);
}
 
Example 5
Project: RISE-V2G   File: V2GCommunicationSessionHandlerEVCC.java   Source Code and License Vote up 4 votes
private boolean startNewSession(SECCDiscoveryRes seccDiscoveryRes) {
	/*
	 * Establish a new V2GCommunicationSessionEVCC if SECCDiscoveryRes was successful and initiate
	 * the respective TCP client connection
	 */
	if (seccDiscoveryRes != null) {
		// Reset SECCDiscoveryReq retry counter
		setSeccDiscoveryRequestCounter(0);
		
		Inet6Address seccAddress;
		
		try {
			// TODO seems to work, but is the needed scope ID really the one of the UDP client?
			seccAddress = Inet6Address.getByAddress(
									InetAddress.getByAddress(seccDiscoveryRes.getSeccIPAddress()).getHostAddress(),
									seccDiscoveryRes.getSeccIPAddress(), 
									UDPClient.getInstance().getUdpClientAddress().getScopeId()
								);
		} catch (UnknownHostException e) {
			getLogger().fatal("SECC address could not be resolved", e);
			return false;
		}
		
		getLogger().info("UDP server responded: SECC reachable at address " + 
						 seccAddress.getHostAddress() + " and port " + 
						 ByteUtils.toIntFromByteArray(seccDiscoveryRes.getSeccPort())); 
		
		if (!startTransportLayerClient(seccDiscoveryRes, seccAddress)) return false;
		
		setV2gCommunicationSessionEVCC(new V2GCommunicationSessionEVCC(getTransportLayerClient()));
		
		/*
		 * Tell the TCP- or TLSClient to notify if 
		 * - a new V2GTPMessage has arrived
		 * - a timeout has occurred while waiting for the respective response message
		 */
		getTransportLayerClient().addObserver(getV2gCommunicationSessionEVCC());
		
		getV2gCommunicationSessionEVCC().addObserver(this);
		
		// Set TLS security flag for communication session
		boolean secureConn = (((Byte) getSecurity()).compareTo((Byte) GlobalValues.V2G_SECURITY_WITH_TLS.getByteValue()) == 0) ? true : false;
		getV2gCommunicationSessionEVCC().setTlsConnection(secureConn);
		
		sendSupportedAppProtocolReq();
	} else {
		getLogger().fatal("Maximum number of SECCDiscoveryReq messages reached");
		return false;
	}
	
	return true;
}
 
Example 6
Project: cyberduck   File: NetworkInterfaceAwareSocketFactory.java   Source Code and License Vote up 4 votes
/**
 * @param network Network interface index
 * @throws UnknownHostException
 */
private Inet6Address getByAddressForInterface(final NetworkInterface network, final InetAddress address) throws UnknownHostException {
    // Append network interface. Workaround for issue #8802
    return Inet6Address.getByAddress(address.getHostAddress(),
            IPAddressUtil.textToNumericFormatV6(address.getHostAddress()), network.getIndex());
}