예제 #1
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);
  }