示例#1
0
 public static Inet4Address getCoercedIPv4Address(InetAddress paramInetAddress) {
   if ((paramInetAddress instanceof Inet4Address)) {
     return (Inet4Address) paramInetAddress;
   }
   byte[] arrayOfByte = paramInetAddress.getAddress();
   int i = 1;
   for (int j = 0; j < 15; j++) {
     if (arrayOfByte[j] != 0) {
       i = 0;
       break;
     }
   }
   if ((i != 0) && (arrayOfByte[15] == 1)) {
     return LOOPBACK4;
   }
   if ((i != 0) && (arrayOfByte[15] == 0)) {
     return ANY4;
   }
   Inet6Address localInet6Address = (Inet6Address) paramInetAddress;
   long l = 0L;
   if (hasEmbeddedIPv4ClientAddress(localInet6Address)) {
     l = getEmbeddedIPv4ClientAddress(localInet6Address).hashCode();
   } else {
     l = ByteBuffer.wrap(localInet6Address.getAddress(), 0, 8).getLong();
   }
   int k = Hashing.murmur3_32().hashLong(l).asInt();
   k |= 0xE0000000;
   if (k == -1) {
     k = -2;
   }
   return getInet4Address(Ints.toByteArray(k));
 }
示例#2
0
  /* check the two Ipv6 addresses and return false if they are both
   * non global address types, but not the same.
   * (ie. one is sitelocal and the other linklocal)
   * return true otherwise.
   */
  private boolean differentLocalAddressTypes(Inet6Address other) {

    if (isLinkLocalAddress() && !other.isLinkLocalAddress()) {
      return false;
    }
    if (isSiteLocalAddress() && !other.isSiteLocalAddress()) {
      return false;
    }
    return true;
  }
示例#3
0
 public static boolean isCompatIPv4Address(Inet6Address paramInet6Address) {
   if (!paramInet6Address.isIPv4CompatibleAddress()) {
     return false;
   }
   byte[] arrayOfByte = paramInet6Address.getAddress();
   return (arrayOfByte[12] != 0)
       || (arrayOfByte[13] != 0)
       || (arrayOfByte[14] != 0)
       || ((arrayOfByte[15] != 0) && (arrayOfByte[15] != 1));
 }
示例#4
0
 /**
  * Evaluate the provided IP address against the information processed during the IP bind rule
  * expression decode.
  *
  * @param remoteAddr A IP address to evaluate.
  * @return An enumeration representing the result of the evaluation.
  */
 public EnumEvalResult evaluate(InetAddress remoteAddr) {
   EnumEvalResult matched = EnumEvalResult.FALSE;
   IPType ipType = IPType.IPv4;
   byte[] addressBytes = remoteAddr.getAddress();
   if (remoteAddr instanceof Inet6Address) {
     ipType = IPType.IPv6;
     Inet6Address addr6 = (Inet6Address) remoteAddr;
     addressBytes = addr6.getAddress();
     if (addr6.isIPv4CompatibleAddress()) ipType = IPType.IPv4;
   }
   if (ipType != this.ipType) return EnumEvalResult.FALSE;
   if (matchAddress(addressBytes)) matched = EnumEvalResult.TRUE;
   return matched;
 }
示例#5
0
 public static Inet4Address get6to4IPv4Address(Inet6Address paramInet6Address) {
   Preconditions.checkArgument(
       is6to4Address(paramInet6Address),
       "Address '%s' is not a 6to4 address.",
       new Object[] {toAddrString(paramInet6Address)});
   return getInet4Address(Arrays.copyOfRange(paramInet6Address.getAddress(), 2, 6));
 }
  @Override
  protected Inet6Address ipAddressRandomizer(InetAddress realAddress) {
    byte[] addr = {
      realAddress.getAddress()[0],
      realAddress.getAddress()[1],
      realAddress.getAddress()[2],
      realAddress.getAddress()[3],
      realAddress.getAddress()[4],
      realAddress.getAddress()[5],
      realAddress.getAddress()[6],
      realAddress.getAddress()[7],
      realAddress.getAddress()[8],
      realAddress.getAddress()[9],
      realAddress.getAddress()[10],
      realAddress.getAddress()[11],
      (byte) 0,
      (byte) 0,
      (byte) 0,
      (byte) 0
    };

    Inet6Address fakeV4 = null;
    try {
      fakeV4 = (Inet6Address) Inet6Address.getByAddress(addr);
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }

    return fakeV4;
  }
  public static InetSocketAddress address(byte[] addr, int offset, int len) {
    // The last 4 bytes are always the port
    final int port = decodeInt(addr, offset + len - 4);
    final InetAddress address;

    try {
      switch (len) {
          // 8 bytes:
          // - 4  == ipaddress
          // - 4  == port
        case 8:
          byte[] ipv4 = new byte[4];
          System.arraycopy(addr, offset, ipv4, 0, 4);
          address = InetAddress.getByAddress(ipv4);
          break;

          // 24 bytes:
          // - 16  == ipaddress
          // - 4   == scopeId
          // - 4   == port
        case 24:
          byte[] ipv6 = new byte[16];
          System.arraycopy(addr, offset, ipv6, 0, 16);
          int scopeId = decodeInt(addr, offset + len - 8);
          address = Inet6Address.getByAddress(null, ipv6, scopeId);
          break;
        default:
          throw new Error();
      }
      return new InetSocketAddress(address, port);
    } catch (UnknownHostException e) {
      throw new Error("Should never happen", e);
    }
  }
示例#8
0
  protected InetSocketAddress readAddress() throws IOException {
    // Address
    int addressType = input.read();
    InetAddress address;
    switch (addressType) {
      case 1: // IPv4
        byte[] ip4 = new byte[4];
        readFully(ip4);
        address = Inet4Address.getByAddress(ip4);
        break;

      case 3: // Domain name
        int size = input.read();
        byte[] domain = new byte[size];
        readFully(domain);
        address = InetAddress.getByName(new String(domain, "US-ASCII"));
        break;

      case 4: // IPv6
        byte[] ip6 = new byte[16];
        readFully(ip6);
        address = Inet6Address.getByAddress(ip6);
        break;

      default:
        throw new SOCKS5Exception(
            "Unexpected address type in SOCKS server response: " + addressType);
    }

    // Port
    int port = (input.read() << 8) | input.read();

    return new InetSocketAddress(address, port);
  }
示例#9
0
 public static boolean isTeredoAddress(Inet6Address paramInet6Address) {
   byte[] arrayOfByte = paramInet6Address.getAddress();
   return (arrayOfByte[0] == 32)
       && (arrayOfByte[1] == 1)
       && (arrayOfByte[2] == 0)
       && (arrayOfByte[3] == 0);
 }
示例#10
0
 public static Inet4Address getIsatapIPv4Address(Inet6Address paramInet6Address) {
   Preconditions.checkArgument(
       isIsatapAddress(paramInet6Address),
       "Address '%s' is not an ISATAP address.",
       new Object[] {toAddrString(paramInet6Address)});
   return getInet4Address(Arrays.copyOfRange(paramInet6Address.getAddress(), 12, 16));
 }
示例#11
0
 public static Inet4Address getCompatIPv4Address(Inet6Address paramInet6Address) {
   Preconditions.checkArgument(
       isCompatIPv4Address(paramInet6Address),
       "Address '%s' is not IPv4-compatible.",
       new Object[] {toAddrString(paramInet6Address)});
   return getInet4Address(Arrays.copyOfRange(paramInet6Address.getAddress(), 12, 16));
 }
示例#12
0
 public static boolean isIsatapAddress(Inet6Address paramInet6Address) {
   if (isTeredoAddress(paramInet6Address)) {
     return false;
   }
   byte[] arrayOfByte = paramInet6Address.getAddress();
   if ((arrayOfByte[8] | 0x3) != 3) {
     return false;
   }
   return (arrayOfByte[9] == 0) && (arrayOfByte[10] == 94) && (arrayOfByte[11] == -2);
 }
示例#13
0
  @Nonnull
  public InetAddress getIp() {
    ByteBuffer buf = ByteBuffer.wrap(getData());
    final byte[] dst = new byte[16];
    buf.get(dst, 0, 16);

    try {
      return Inet6Address.getByAddress(dst);
    } catch (UnknownHostException e) {
      // This can happen only if the IP byte array is of illegal length
      throw new IllegalStateException("Illegal IP address", e);
    }
  }
示例#14
0
 public static TeredoInfo getTeredoInfo(Inet6Address paramInet6Address) {
   Preconditions.checkArgument(
       isTeredoAddress(paramInet6Address),
       "Address '%s' is not a Teredo address.",
       new Object[] {toAddrString(paramInet6Address)});
   byte[] arrayOfByte1 = paramInet6Address.getAddress();
   Inet4Address localInet4Address1 = getInet4Address(Arrays.copyOfRange(arrayOfByte1, 4, 8));
   int i = ByteStreams.newDataInput(arrayOfByte1, 8).readShort() & 0xFFFF;
   int j = (ByteStreams.newDataInput(arrayOfByte1, 10).readShort() ^ 0xFFFFFFFF) & 0xFFFF;
   byte[] arrayOfByte2 = Arrays.copyOfRange(arrayOfByte1, 12, 16);
   for (int k = 0; k < arrayOfByte2.length; k++) {
     arrayOfByte2[k] = ((byte) (arrayOfByte2[k] ^ 0xFFFFFFFF));
   }
   Inet4Address localInet4Address2 = getInet4Address(arrayOfByte2);
   return new TeredoInfo(localInet4Address1, localInet4Address2, j, i);
 }
示例#15
0
 public InetSocketAddress getLocalAddress() throws IOException {
   if (!nfd.isValid()) return null;
   ByteBuffer name = ByteBuffer.allocateDirect(18);
   int namelen = getsockname(nfd.getNativeFD(), name);
   if (namelen == 0) // not bound
   return null; // XXX return some wildcard?
   if (namelen == 4) {
     byte[] addr = new byte[4];
     name.get(addr);
     int port = name.getShort() & 0xFFFF;
     return new InetSocketAddress(Inet4Address.getByAddress(addr), port);
   }
   if (namelen == 16) {
     byte[] addr = new byte[16];
     name.get(addr);
     int port = name.getShort() & 0xFFFF;
     return new InetSocketAddress(Inet6Address.getByAddress(addr), port);
   }
   throw new SocketException("invalid address length");
 }
示例#16
0
 /**
  * Returns the socket address of the remote peer this channel is connected to, or null if this
  * channel is not yet connected.
  *
  * @return The peer address.
  * @throws IOException
  */
 public InetSocketAddress getPeerAddress() throws IOException {
   if (!nfd.isValid()) return null;
   ByteBuffer name = ByteBuffer.allocateDirect(18);
   int namelen = getpeername(nfd.getNativeFD(), name);
   if (namelen == 0) // not connected yet
   return null;
   if (namelen == 4) // IPv4
   {
     byte[] addr = new byte[4];
     name.get(addr);
     int port = name.getShort() & 0xFFFF;
     return new InetSocketAddress(Inet4Address.getByAddress(addr), port);
   } else if (namelen == 16) // IPv6
   {
     byte[] addr = new byte[16];
     name.get(addr);
     int port = name.getShort() & 0xFFFF;
     return new InetSocketAddress(Inet6Address.getByAddress(addr), port);
   }
   throw new SocketException("invalid address length");
 }
示例#17
0
  /**
   * Receive a datagram on this channel, returning the host address that sent the datagram.
   *
   * @param dst Where to store the datagram.
   * @return The host address that sent the datagram.
   * @throws IOException
   */
  public SocketAddress receive(ByteBuffer dst) throws IOException {
    if (kind != Kind.SOCK_DGRAM) throw new SocketException("not a datagram socket");
    ByteBuffer hostPort = ByteBuffer.allocateDirect(18);
    int hostlen = receive(nfd.getNativeFD(), dst, hostPort);
    if (hostlen == 0) return null;
    if (hostlen == 4) // IPv4
    {
      byte[] addr = new byte[4];
      hostPort.get(addr);
      int port = hostPort.getShort() & 0xFFFF;
      return new InetSocketAddress(Inet4Address.getByAddress(addr), port);
    }
    if (hostlen == 16) // IPv6
    {
      byte[] addr = new byte[16];
      hostPort.get(addr);
      int port = hostPort.getShort() & 0xFFFF;
      return new InetSocketAddress(Inet6Address.getByAddress(addr), port);
    }

    throw new SocketException("host address received with invalid length: " + hostlen);
  }
示例#18
0
 public static boolean is6to4Address(Inet6Address paramInet6Address) {
   byte[] arrayOfByte = paramInet6Address.getAddress();
   return (arrayOfByte[0] == 32) && (arrayOfByte[1] == 2);
 }
 /**
  * Compress an IPv6 address. Return compressed address, e.g. 0:0:0:0:0:0:0:01 => ::1
  *
  * @param inet6Address the IPv6 address to compress
  * @return compressed IPv6 address
  * @throws UnknownHostException if host with IPv6 address is not alive
  */
 public static String getCompressedInet6Address(String inet6Address) throws UnknownHostException {
   String longAddress = Inet6Address.getByName(inet6Address).getHostAddress();
   return longAddress.replaceFirst("(^|:)(0+(:|$)){2,8}", "::");
 }
 /**
  * Get an IPv6 address with long format. Return IPv6 address without ::, e.g. ::1 =>
  * 0:0:0:0:0:0:0:1
  *
  * @param inet6Address the IPv6 address to format
  * @return long format IPv6 address
  * @throws UnknownHostException if host with IPv6 address is not alive
  */
 public static String getLongInet6Address(String inet6Address) throws UnknownHostException {
   String longAddress = Inet6Address.getByName(inet6Address).getHostAddress();
   return longAddress;
 }