示例#1
0
 private static String getHostAddress(ThreadContext context, InetAddress addr, Boolean reverse) {
   String ret;
   if (reverse == null) {
     ret =
         context.runtime.isDoNotReverseLookupEnabled()
             ? addr.getHostAddress()
             : addr.getCanonicalHostName();
   } else if (reverse) {
     ret = addr.getCanonicalHostName();
   } else {
     ret = addr.getHostAddress();
   }
   return ret;
 }
示例#2
0
  /**
   * This method tries to compute the name of the host that's reachable by all the other nodes.
   *
   * <p>Since it's possible that the slave is not reachable from the master (it may be behind a
   * firewall, connecting to master via JNLP), this method may return null.
   *
   * <p>It's surprisingly tricky for a machine to know a name that other systems can get to,
   * especially between things like DNS search suffix, the hosts file, and YP.
   *
   * <p>So the technique here is to compute possible interfaces and names on the slave, then try to
   * ping them from the master, and pick the one that worked.
   *
   * <p>The computation may take some time, so it employs caching to make the successive lookups
   * faster.
   *
   * @since 1.300
   * @return null if the host name cannot be computed (for example because this computer is offline,
   *     because the slave is behind the firewall, etc.)
   */
  public String getHostName() throws IOException, InterruptedException {
    if (hostNameCached)
      // in the worst case we end up having multiple threads computing the host name simultaneously,
      // but that's not harmful, just wasteful.
      return cachedHostName;

    VirtualChannel channel = getChannel();
    if (channel == null) return null; // can't compute right now

    for (String address : channel.call(new ListPossibleNames())) {
      try {
        InetAddress ia = InetAddress.getByName(address);
        if (!(ia instanceof Inet4Address)) {
          LOGGER.fine(address + " is not an IPv4 address");
          continue;
        }
        if (!ComputerPinger.checkIsReachable(ia, 3)) {
          LOGGER.fine(address + " didn't respond to ping");
          continue;
        }
        cachedHostName = ia.getCanonicalHostName();
        hostNameCached = true;
        return cachedHostName;
      } catch (IOException e) {
        // if a given name fails to parse on this host, we get this error
        LOGGER.log(Level.FINE, "Failed to parse " + address, e);
      }
    }

    // allow the administrator to manually specify the host name as a fallback. HUDSON-5373
    cachedHostName = channel.call(new GetFallbackName());
    hostNameCached = true;
    return cachedHostName;
  }
  private static DistributedFileSystem getDFSForToken(
      Token<DelegationTokenIdentifier> token, final Configuration conf) throws Exception {
    DistributedFileSystem dfs = null;
    try {
      // TODO: The service is usually an IPaddress:port. We convert
      // it to dns name and then obtain the filesystem just so that
      // we reuse the existing filesystem handle (that the jobtracker
      // might have for this namenode; the namenode is usually
      // specified as the dns name in the jobtracker).
      // THIS IS A WORKAROUND FOR NOW. NEED TO SOLVE THIS PROBLEM
      // IN A BETTER WAY.
      String[] ipaddr = token.getService().toString().split(":");
      InetAddress iaddr = InetAddress.getByName(ipaddr[0]);
      String dnsName = iaddr.getCanonicalHostName();
      final URI uri = new URI(SCHEME + "://" + dnsName + ":" + ipaddr[1]);
      dfs =
          (DistributedFileSystem)
              UserGroupInformation.getLoginUser()
                  .doAs(
                      new PrivilegedExceptionAction<DistributedFileSystem>() {
                        public DistributedFileSystem run() throws IOException {
                          return (DistributedFileSystem) FileSystem.get(uri, conf);
                        }
                      });

    } catch (Exception e) {
      LOG.warn("Failed to create a dfs to renew for:" + token.getService(), e);
      throw e;
    }
    return dfs;
  }
  /** {@inheritDoc} */
  public void connect(InetAddress address, int port, int timeout) throws IOException, Exception {
    log().info("connecting to JDBC on " + address);
    if (log().isDebugEnabled()) {
      log().debug("Loading JDBC driver: '" + getDbDriver() + "'");
    }
    Driver driver = (Driver) Class.forName(getDbDriver()).newInstance();
    if (log().isDebugEnabled()) {
      log().debug("JDBC driver loaded: '" + getDbDriver() + "'");
    }

    String url = DBTools.constructUrl(getUrl(), address.getCanonicalHostName());
    if (log().isDebugEnabled()) {
      log().debug("Constructed JDBC url: '" + url + "'");
    }

    Properties props = new Properties();
    props.setProperty("user", getUser());
    props.setProperty("password", getPassword());
    props.setProperty("timeout", String.valueOf(timeout / 1000));
    m_connection = driver.connect(url, props);

    if (log().isDebugEnabled()) {
      log()
          .debug(
              "Got database connection: '"
                  + m_connection
                  + "' ("
                  + url
                  + ", "
                  + getUser()
                  + ", "
                  + getPassword()
                  + ")");
    }
  }
示例#5
0
    @Override
    protected Void doInBackground(Void... params) {

      try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {

          NetworkInterface thisInterface = networkInterfaces.nextElement();
          Enumeration<InetAddress> inetAddresses = thisInterface.getInetAddresses();
          if (inetAddresses.hasMoreElements()) {
            String niInfo = thisInterface.getDisplayName() + "\n";

            while (inetAddresses.hasMoreElements()) {
              InetAddress thisAddress = inetAddresses.nextElement();
              niInfo += "---\n";
              niInfo += "Address: " + thisAddress.getAddress() + "\n";
              niInfo += "CanonicalHostName: " + thisAddress.getCanonicalHostName() + "\n";
              niInfo += "HostAddress: " + thisAddress.getHostAddress() + "\n";
              niInfo += "HostName: " + thisAddress.getHostName() + "\n";
            }

            publishProgress(niInfo);
          }
        }
      } catch (SocketException e) {
        e.printStackTrace();
      }
      return null;
    }
示例#6
0
  // Print IP addresses and network interfaces
  private static void printIPAddresses() {
    try {
      InetAddress localhost = InetAddress.getLocalHost();
      System.out.println("Server: IP Address: " + localhost.getHostAddress());
      // Just in case this host has multiple IP addresses....
      InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
      if (allMyIps != null && allMyIps.length > 1) {
        System.out.println("Server: Full list of IP addresses:");
        for (InetAddress allMyIp : allMyIps) {
          System.out.println("    " + allMyIp);
        }
      }
    } catch (UnknownHostException ex) {
      System.out.println("Server: Cannot get IP address of local host");
      System.out.println("Server: UnknownHostException: " + ex.getMessage());
    }

    try {
      System.out.println("Server: Full list of network interfaces:");
      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
          en.hasMoreElements(); ) {
        NetworkInterface intf = en.nextElement();
        System.out.println("    " + intf.getName() + " " + intf.getDisplayName());
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
            enumIpAddr.hasMoreElements(); ) {
          System.out.println("        " + enumIpAddr.nextElement().toString());
        }
      }
    } catch (SocketException ex) {
      System.out.println("Server: Cannot retrieve network interface list");
      System.out.println("Server: UnknownHostException: " + ex.getMessage());
    }
  }
示例#7
0
 private String resolveCanonicalHostname(final String host) throws UnknownHostException {
   final InetAddress in = InetAddress.getByName(host);
   final String canonicalServer = in.getCanonicalHostName();
   if (in.getHostAddress().contentEquals(canonicalServer)) {
     return host;
   }
   return canonicalServer;
 }
示例#8
0
  public static void main(String[] args) throws Exception {
    InetAddress ip = InetAddress.getByName("www.baidu.com");
    System.out.println("baidu是否可达:" + ip.isReachable(2000));
    System.out.println(ip.getHostAddress());

    InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
    System.out.println("本机是否可达:" + local.isReachable(5000));
    System.out.println(local.getCanonicalHostName());
  }
示例#9
0
 public String getHostname() {
   if (hostname == null) {
     try {
       InetAddress localMachine = InetAddress.getLocalHost();
       hostname = localMachine.getCanonicalHostName();
     } catch (UnknownHostException e) {
       // we do nothing
       hostname = "localhost";
     }
     assert hostname != null;
   }
   return hostname;
 }
示例#10
0
 public static void main(String[] args) throws Exception {
   // 根据主机名来获取对应的InetAddress实例
   InetAddress ip = InetAddress.getByName("www.oneedu.cn");
   // 判断是否可达
   System.out.println("oneedu是否可达:" + ip.isReachable(2000));
   // 获取该InetAddress实例的IP字符串
   System.out.println(ip.getHostAddress());
   // 根据原始IP地址来获取对应的InetAddress实例
   InetAddress local = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
   System.out.println("本机是否可达:" + local.isReachable(5000));
   // 获取该InetAddress实例对应的全限定域名
   System.out.println(local.getCanonicalHostName());
 }
示例#11
0
 /**
  * Set the hostname in the passive check
  *
  * @param useCanonical true to use this machines fully qualified domain name, false to use the
  *     short hostname
  */
 public void setHostname(boolean useCanonical) {
   InetAddress ipAddress;
   try {
     ipAddress = InetAddress.getLocalHost();
   } catch (UnknownHostException e) {
     throw new UnknownHostRuntimeException(e);
   }
   if (useCanonical) {
     this.hostname = ipAddress.getCanonicalHostName();
   } else {
     this.hostname = ipAddress.getHostName();
   }
 }
  public LoggingAgentBackendImpl() {
    try {
      InetAddress addr = InetAddress.getLocalHost();
      String hostName = addr.getCanonicalHostName();
      String ip = addr.getHostAddress();

      prefix = String.format("host=%s ip=%s ", hostName, ip);
    } catch (UnknownHostException e) {
      // bummer, but not very important.  Log an error and set them to null and we just won't log
      // them later
      log.info(String.format("Unable to get host information: %s", e.getMessage()));
      prefix = "";
    }
  }
示例#13
0
 private synchronized void addLocalAddress(InetAddress addr) {
   int addrtype;
   if (addr.isLoopbackAddress()) {
     addrtype = HOSTMAP_LOOPBACK;
   } else if (addr.isLinkLocalAddress()) {
     addrtype = HOSTMAP_LINKLOCAL;
   } else if (addr.isSiteLocalAddress()) {
     addrtype = HOSTMAP_SITELOCAL;
   } else if (addr.isMulticastAddress()) {
     addrtype = HOSTMAP_MULTICAST;
   } else {
     addrtype = HOSTMAP_GLOBAL;
   }
   if (addr.getAddress().length == 4) {
     addrtype |= HOSTMAP_IPV4;
   } else {
     addrtype |= HOSTMAP_IPV6;
   }
   String addrAsString = addr.getHostAddress();
   fLocalHostAddresses.put(addrAsString, Integer.valueOf(addrtype | HOSTMAP_ADDR));
   if (0 == (addrtype & (HOSTMAP_LINKLOCAL | HOSTMAP_SITELOCAL | HOSTMAP_MULTICAST))) {
     // Don't do DNS Reverse Loopkup's for non-routable addresses.
     // They won't be known to the Name Server anyway, and they
     // make startup _much_ slower.
     String addrAsNameCan = addr.getCanonicalHostName().toLowerCase();
     // query the name after the canonical name, it will re-use
     // cached canonical name (if the name was not explicitly set)
     String addrAsName = addr.getHostName().toLowerCase();
     if (!addrAsNameCan.equals(addrAsString)) {
       // We must check if we really got a name, since InetAddress.getHostName()
       // returns the original address in case it thinks the name is spoofed!
       if (0 == (addrtype & HOSTMAP_LOOPBACK)) {
         // Not loopback --> found a Canonical Name.
         fLocalHostAddresses.put(
             addrAsNameCan, Integer.valueOf(addrtype | HOSTMAP_NAME | HOSTMAP_CANONICALNAME));
         // override the address as canonical-address
         fLocalHostAddresses.put(
             addrAsString, Integer.valueOf(addrtype | HOSTMAP_ADDR | HOSTMAP_CANONICALADDR));
       } else {
         // Loopback --> add the found name as non-canonical.
         fLocalHostAddresses.put(addrAsNameCan, Integer.valueOf(addrtype | HOSTMAP_NAME));
       }
     }
     if (!addrAsName.equals(addrAsString) && !addrAsName.equals(addrAsNameCan)) {
       // don't override the canonical name by the name.
       fLocalHostAddresses.put(addrAsName, Integer.valueOf(addrtype | HOSTMAP_NAME));
     }
   }
 }
示例#14
0
 /** @param */
 public static void main(String[] para) throws UnknownHostException {
   InetAddress IP0 = InetAddress.getLocalHost();
   String name = IP0.getCanonicalHostName();
   byte[] address = IP0.getAddress();
   String hostAddress = IP0.getHostAddress();
   String ip = IP0.toString();
   System.out.println(
       "CanonicalHostName is : "
           + name
           + " hostAddress is:"
           + hostAddress
           + " address is:"
           + address
           + " IP is:"
           + ip);
 }
示例#15
0
 public static String getFQDN(final InetAddress addr) {
   String hostname = addr.getHostName();
   if (hostname.indexOf('.') >= 0) {
     return hostname;
   }
   hostname = addr.getCanonicalHostName();
   if (hostname.indexOf('.') >= 0) {
     return hostname;
   }
   String hostAddr = addr.getHostAddress();
   try {
     return InetAddress.getByName(hostAddr).getHostName();
   } catch (UnknownHostException e) {
     return hostAddr;
   }
 }
示例#16
0
  public static IRubyObject gethostbyname(ThreadContext context, IRubyObject hostname) {
    Ruby runtime = context.runtime;

    try {
      InetAddress addr = getRubyInetAddress(hostname.convertToString().getByteList());
      IRubyObject[] ret = new IRubyObject[4];

      ret[0] = runtime.newString(addr.getCanonicalHostName());
      ret[1] = runtime.newArray();
      ret[2] = runtime.newFixnum(2); // AF_INET
      ret[3] = runtime.newString(new ByteList(addr.getAddress()));
      return runtime.newArrayNoCopy(ret);

    } catch (UnknownHostException e) {
      throw sockerr(runtime, "gethostbyname: name or service not known");
    }
  }
示例#17
0
  /**
   * @return a sorted set of all addresses
   * @param includeSiteLocal whether to include private like 192.168.x.x
   * @param includeLoopbackAndWildcard whether to include 127.x.x.x and 0.0.0.0
   * @param includeIPv6 whether to include IPV6
   * @return an array of all addresses
   * @since 0.9.4
   */
  public static SortedSet<String> getAddresses(
      boolean includeSiteLocal, boolean includeLoopbackAndWildcard, boolean includeIPv6) {
    boolean haveIPv4 = false;
    boolean haveIPv6 = false;
    SortedSet<String> rv = new TreeSet();
    try {
      InetAddress localhost = InetAddress.getLocalHost();
      InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
      if (allMyIps != null) {
        for (int i = 0; i < allMyIps.length; i++) {
          if (allMyIps[i] instanceof Inet4Address) haveIPv4 = true;
          else haveIPv6 = true;
          if (shouldInclude(allMyIps[i], includeSiteLocal, includeLoopbackAndWildcard, includeIPv6))
            rv.add(stripScope(allMyIps[i].getHostAddress()));
        }
      }
    } catch (UnknownHostException e) {
    }

    try {
      Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
      if (ifcs != null) {
        while (ifcs.hasMoreElements()) {
          NetworkInterface ifc = ifcs.nextElement();
          for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements(); ) {
            InetAddress addr = addrs.nextElement();
            if (addr instanceof Inet4Address) haveIPv4 = true;
            else haveIPv6 = true;
            if (shouldInclude(
                addr, includeSiteLocal,
                includeLoopbackAndWildcard, includeIPv6)) rv.add(stripScope(addr.getHostAddress()));
          }
        }
      }
    } catch (SocketException e) {
    }

    if (includeLoopbackAndWildcard) {
      if (haveIPv4) rv.add("0.0.0.0");
      if (includeIPv6 && haveIPv6)
        rv.add(
            "0:0:0:0:0:0:0:0"); // we could do "::" but all the other ones are probably in long form
    }
    return rv;
  }
示例#18
0
  private void parseRecord(Message message, InetAddress address) {
    // We really only care about the ADDITIONAL section (specifically the text records)
    Record[] responses = message.getSectionArray(Section.ADDITIONAL);
    // We only want to process records that actually have a length, have an ANSWER
    // section that has stuff in it and that the ANSWER to our query is what we sent
    if (responses.length != 0
        && message.getSectionArray(Section.ANSWER).length != 0
        && message
            .getSectionArray(Section.ANSWER)[0]
            .getName()
            .toString()
            .equals(NvmDNS.MDNS_QUERY)) {

      Log.v("NvmDNS Response", "Got a packet from " + address.getCanonicalHostName());
      Log.v(
          "NvmDNS Response",
          "Question: " + message.getSectionArray(Section.ANSWER)[0].getName().toString());
      Log.v("NvmDNS Response", "Response: " + responses[0].getName().toString());

      // TODO: The DNS entry we get is "XENITH._nvstream._tcp.local."
      // And the .'s in there are not actually periods. Or something.
      String hostname = responses[0].getName().toString();

      // The records can be returned in any order, so we need to figure out which one is the
      // TXTRecord
      // We get three records back: A TXTRecord, a SRVRecord and an ARecord
      TXTRecord txtRecord = null;

      for (Record record : responses) {
        Log.v(
            "NvmDNS Response",
            "We recieved a DNS repsonse with a " + record.getClass().getName() + " record.");
        if (record instanceof TXTRecord) {
          txtRecord = (TXTRecord) record;
        }
      }

      if (txtRecord == null) {
        Log.e("NvmDNS Response", "We recieved a malformed DNS repsonse with no TXTRecord");
        return;
      }

      this.parseTXTRecord(txtRecord, address, hostname);
    }
  }
示例#19
0
  public static void main(String[] args) {

    try {
      InetAddress localMachine = InetAddress.getLocalHost();
      String address = localMachine.getCanonicalHostName();
      if (!address.contains("ecs.vuw.ac.nz")) {
        System.out.println("This can only be run on ecs machines");
        System.exit(1);
      }
    } catch (UnknownHostException e1) {
      // e1.printStackTrace();
      System.out.println("Unknown Error");
      System.exit(1);
    }

    if (args.length == 0) {
      try {
        Socket socket = new Socket("greta-pt", 55231); // DONE change to greta-pt
        new ChatGUI(socket);
      } catch (UnknownHostException e) {
        JOptionPane.showMessageDialog(
            null, "Could not find server", "Connection Error", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
      } catch (IOException e) {
        JOptionPane.showMessageDialog(
            null, "Could not connect to server", "Connection Error", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
        // e.printStackTrace();//DONE remove after debugging
      }
    } else if (args.length == 1) {
      if (args[0].equals("secretserver")) {
        new ChatServer();
      } else {
        System.out.println("Error, this program takes no arguments");
        System.exit(1);
      }
    } else {
      System.out.println("Error, this program takes no arguments");
      System.exit(1);
    }
  }
  public int compareTo(PersistentMemberPattern o) {
    int result = compare(diskStoreID, o.diskStoreID);
    if (result != 0) {
      return result;
    }
    result =
        compare(
            host == null ? null : host.getCanonicalHostName(),
            o.host == null ? null : o.host.getCanonicalHostName());
    if (result != 0) {
      return result;
    }
    result = compare(directory, o.directory);
    if (result != 0) {
      return result;
    }

    result = Long.signum(revokedTime - o.revokedTime);

    return result;
  }
示例#21
0
 Neighbor getByIpAndPort(InetAddress inetAddress, int port) {
   Neighbor neighbor = null;
   for (Neighbor n : neighbors) {
     try {
       if (n.getIp().equals(inetAddress) && n.getPort() == port) {
         neighbor = n;
         break;
       }
     } catch (NullPointerException e) {
       if (inetAddress != null) {
         System.out.println("Searching for: " + inetAddress.getCanonicalHostName() + ":" + port);
       } else {
         System.out.println(
             "Inet addres is null - happens if there was an error while comunicating with the client");
       }
       System.out.println("Neighbors size = " + neighbors.size());
       e.printStackTrace();
     }
   }
   return neighbor;
 }
示例#22
0
  private void parseTXTRecord(TXTRecord txtRecord, InetAddress address, String hostname) {
    // The DNS library we are using does not use inferred generics :(
    @SuppressWarnings("unchecked")
    ArrayList<String> txtRecordStringList = new ArrayList<String>(txtRecord.getStrings());

    if (txtRecordStringList.size() != 5) {
      Log.e(
          "NvmDNS Response",
          "We recieved a malformed DNS repsonse with the improper amount of TXTRecord Entries.");
      return;
    }

    // The general format of the text records is:
    // 	SERVICE_STATE=1
    //	SERVICE_NUMOFAPPS=5
    //	SERVICE_GPUTYPE=GeForce GTX 760 x2
    // 	SERVICE_MAC=DE:AD:BE:EF:CA:FE
    //	SERVICE_UNIQUEID={A Wild UUID Appeared!}
    // Every single record I've seen so far has been in this format
    try {
      int serviceState =
          Integer.parseInt(this.parseTXTRecordField(txtRecordStringList.get(0), "SERVICE_STATE"));
      int numberOfApps =
          Integer.parseInt(
              this.parseTXTRecordField(txtRecordStringList.get(1), "SERVICE_NUMOFAPPS"));
      String gpuType = this.parseTXTRecordField(txtRecordStringList.get(2), "SERVICE_GPUTYPE");
      String mac = this.parseTXTRecordField(txtRecordStringList.get(3), "SERVICE_MAC");
      UUID uniqueID =
          UUID.fromString(this.parseTXTRecordField(txtRecordStringList.get(4), "SERVICE_UNIQUEID"));

      // We need to resolve the hostname in this thread so that we can use it in the GUI
      address.getCanonicalHostName();

      NvComputer computer =
          new NvComputer(hostname, address, serviceState, numberOfApps, gpuType, mac, uniqueID);
      this.responses.add(computer);
    } catch (ArrayIndexOutOfBoundsException e) {
      Log.e("NvmDNS Response", "We recieved a malformed DNS repsonse.");
    }
  }
示例#23
0
  @SuppressWarnings("rawtypes")
  @Test
  public void conditionalConsoleApp_IF_THEN_True()
      throws JoranException, IOException, InterruptedException {
    InetAddress localhost = InetAddress.getLocalHost();
    System.out.println(
        "In conditionalConsoleApp_IF_THEN_True, canonicalHostName=\""
            + localhost.getCanonicalHostName()
            + "] and hostNmae=\""
            + localhost.getHostName()
            + "\"");
    context.putProperty("aHost", localhost.getHostName());

    String configFileAsStr =
        ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml";
    configure(configFileAsStr);
    FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
    assertNotNull(fileAppender);

    ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
    assertNotNull(consoleAppender);
    StatusChecker checker = new StatusChecker(context);
    checker.assertIsErrorFree();
  }
  /**
   * Gets the splits of the tables that have been set on the job by reading the metadata table for
   * the specified ranges.
   *
   * @return the splits from the tables based on the ranges.
   * @throws java.io.IOException if a table set on the job doesn't exist or an error occurs
   *     initializing the tablet locator
   */
  @Override
  public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
    Level logLevel = getLogLevel(job);
    log.setLevel(logLevel);
    validateOptions(job);

    Random random = new Random();
    LinkedList<InputSplit> splits = new LinkedList<InputSplit>();
    Map<String, InputTableConfig> tableConfigs = getInputTableConfigs(job);
    for (Map.Entry<String, InputTableConfig> tableConfigEntry : tableConfigs.entrySet()) {
      String tableName = tableConfigEntry.getKey();
      InputTableConfig tableConfig = tableConfigEntry.getValue();

      Instance instance = getInstance(job);
      String tableId;
      // resolve table name to id once, and use id from this point forward
      if (DeprecationUtil.isMockInstance(instance)) {
        tableId = "";
      } else {
        try {
          tableId = Tables.getTableId(instance, tableName);
        } catch (TableNotFoundException e) {
          throw new IOException(e);
        }
      }

      Authorizations auths = getScanAuthorizations(job);
      String principal = getPrincipal(job);
      AuthenticationToken token = getAuthenticationToken(job);

      boolean batchScan = InputConfigurator.isBatchScan(CLASS, job);
      boolean supportBatchScan =
          !(tableConfig.isOfflineScan()
              || tableConfig.shouldUseIsolatedScanners()
              || tableConfig.shouldUseLocalIterators());
      if (batchScan && !supportBatchScan)
        throw new IllegalArgumentException(
            "BatchScanner optimization not available for offline scan, isolated, or local iterators");

      boolean autoAdjust = tableConfig.shouldAutoAdjustRanges();
      if (batchScan && !autoAdjust)
        throw new IllegalArgumentException(
            "AutoAdjustRanges must be enabled when using BatchScanner optimization");

      List<Range> ranges =
          autoAdjust ? Range.mergeOverlapping(tableConfig.getRanges()) : tableConfig.getRanges();
      if (ranges.isEmpty()) {
        ranges = new ArrayList<Range>(1);
        ranges.add(new Range());
      }

      // get the metadata information for these ranges
      Map<String, Map<KeyExtent, List<Range>>> binnedRanges =
          new HashMap<String, Map<KeyExtent, List<Range>>>();
      TabletLocator tl;
      try {
        if (tableConfig.isOfflineScan()) {
          binnedRanges = binOfflineTable(job, tableId, ranges);
          while (binnedRanges == null) {
            // Some tablets were still online, try again
            // sleep randomly between 100 and 200 ms
            sleepUninterruptibly(100 + random.nextInt(100), TimeUnit.MILLISECONDS);
            binnedRanges = binOfflineTable(job, tableId, ranges);
          }
        } else {
          tl = InputConfigurator.getTabletLocator(CLASS, job, tableId);
          // its possible that the cache could contain complete, but old information about a tables
          // tablets... so clear it
          tl.invalidateCache();

          ClientContext context =
              new ClientContext(
                  getInstance(job),
                  new Credentials(getPrincipal(job), getAuthenticationToken(job)),
                  getClientConfiguration(job));
          while (!tl.binRanges(context, ranges, binnedRanges).isEmpty()) {
            if (!DeprecationUtil.isMockInstance(instance)) {
              if (!Tables.exists(instance, tableId)) throw new TableDeletedException(tableId);
              if (Tables.getTableState(instance, tableId) == TableState.OFFLINE)
                throw new TableOfflineException(instance, tableId);
            }
            binnedRanges.clear();
            log.warn("Unable to locate bins for specified ranges. Retrying.");
            // sleep randomly between 100 and 200 ms
            sleepUninterruptibly(100 + random.nextInt(100), TimeUnit.MILLISECONDS);
            tl.invalidateCache();
          }
        }
      } catch (Exception e) {
        throw new IOException(e);
      }

      HashMap<Range, ArrayList<String>> splitsToAdd = null;

      if (!autoAdjust) splitsToAdd = new HashMap<Range, ArrayList<String>>();

      HashMap<String, String> hostNameCache = new HashMap<String, String>();
      for (Map.Entry<String, Map<KeyExtent, List<Range>>> tserverBin : binnedRanges.entrySet()) {
        String ip = tserverBin.getKey().split(":", 2)[0];
        String location = hostNameCache.get(ip);
        if (location == null) {
          InetAddress inetAddress = InetAddress.getByName(ip);
          location = inetAddress.getCanonicalHostName();
          hostNameCache.put(ip, location);
        }
        for (Map.Entry<KeyExtent, List<Range>> extentRanges : tserverBin.getValue().entrySet()) {
          Range ke = extentRanges.getKey().toDataRange();
          if (batchScan) {
            // group ranges by tablet to be read by a BatchScanner
            ArrayList<Range> clippedRanges = new ArrayList<Range>();
            for (Range r : extentRanges.getValue()) clippedRanges.add(ke.clip(r));

            BatchInputSplit split =
                new BatchInputSplit(tableName, tableId, clippedRanges, new String[] {location});
            SplitUtils.updateSplit(split, instance, tableConfig, principal, token, auths, logLevel);

            splits.add(split);
          } else {
            // not grouping by tablet
            for (Range r : extentRanges.getValue()) {
              if (autoAdjust) {
                // divide ranges into smaller ranges, based on the tablets
                RangeInputSplit split =
                    new RangeInputSplit(tableName, tableId, ke.clip(r), new String[] {location});
                SplitUtils.updateSplit(
                    split, instance, tableConfig, principal, token, auths, logLevel);
                split.setOffline(tableConfig.isOfflineScan());
                split.setIsolatedScan(tableConfig.shouldUseIsolatedScanners());
                split.setUsesLocalIterators(tableConfig.shouldUseLocalIterators());

                splits.add(split);
              } else {
                // don't divide ranges
                ArrayList<String> locations = splitsToAdd.get(r);
                if (locations == null) locations = new ArrayList<String>(1);
                locations.add(location);
                splitsToAdd.put(r, locations);
              }
            }
          }
        }
      }

      if (!autoAdjust)
        for (Map.Entry<Range, ArrayList<String>> entry : splitsToAdd.entrySet()) {
          RangeInputSplit split =
              new RangeInputSplit(
                  tableName, tableId, entry.getKey(), entry.getValue().toArray(new String[0]));
          SplitUtils.updateSplit(split, instance, tableConfig, principal, token, auths, logLevel);
          split.setOffline(tableConfig.isOfflineScan());
          split.setIsolatedScan(tableConfig.shouldUseIsolatedScanners());
          split.setUsesLocalIterators(tableConfig.shouldUseLocalIterators());

          splits.add(split);
        }
    }

    return splits.toArray(new InputSplit[splits.size()]);
  }
  /**
   * Create an STS client, create dispatch Invoke dispatch, return response
   *
   * @param args
   * @return
   * @throws SOAPException
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   */
  public static String Go(
      String CSR,
      String SAN,
      String TemplateName,
      String CertFormat,
      String MEXuRI,
      String ValidUnit,
      String ValidValue,
      String OU1,
      String OU2,
      String OU3,
      String OU4,
      String OU5,
      String dnEmail)
      throws SOAPException, IOException, ParserConfigurationException, SAXException {

    MetadataClient mexClient = new MetadataClient();

    // the MEX URI is the service URL +/MEX
    Metadata metadata = mexClient.retrieveMetadata(MEXuRI + "/MEX");
    metadata.getOtherAttributes();

    QName serviceInfo = null;
    QName portName = null;
    String Address = null;
    // String namespace = null;

    List<PortInfo> ports = mexClient.getServiceInformation(metadata);
    for (PortInfo port : ports) {

      serviceInfo = port.getServiceName();
      portName = port.getPortName();
      Address = port.getAddress();
      // namespace = port.getPortNamespaceURI();

    }

    // an instance of SecurityTokenService
    Service STSS = Service.create(new URL(MEXuRI), serviceInfo);

    // a dispatch of SOAPMessage
    Dispatch<SOAPMessage> dispatch =
        STSS.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

    // Message factor instance of SOAP 1.2 protcol
    MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);

    // Create SOAPMessage Request
    SOAPMessage request = factory.createMessage();

    // Request Header
    SOAPHeader header = request.getSOAPHeader();

    // Soap Factory
    SOAPFactory factory1 = SOAPFactory.newInstance();

    // Enable WS-Addressing and Add the "To:" endpoint
    SOAPElement To = factory1.createElement("To", "", "http://www.w3.org/2005/08/addressing");
    To.addTextNode(Address);

    // add the Microsoft MS-STEP Action Element:
    SOAPElement ActionElem =
        factory1.createElement("Action", "", "http://www.w3.org/2005/08/addressing");
    ActionElem.addTextNode("http://schemas.microsoft.com/windows/pki/2009/01/enrollment/RST/wstep");

    // Add a unique message ID "UUID"
    SOAPElement MessageID =
        factory1.createElement("MessageID", "", "http://www.w3.org/2005/08/addressing");
    MessageID.addTextNode("uuid:" + UUID.randomUUID());

    // add all the required SOAP header items:
    header.addChildElement(To);
    header.addChildElement(ActionElem);
    header.addChildElement(MessageID);

    // create a "Request Body" to hold request elements:
    SOAPBody body = request.getSOAPBody();

    // Compose the soap:Body payload "RequestSecurityToken" as the body type
    QName payloadName =
        new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "RequestSecurityToken", "");
    SOAPBodyElement payload = body.addBodyElement(payloadName);

    // Add the WS-Trust TokenType and RequestType elements:
    payload
        .addChildElement("TokenType")
        .addTextNode(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
    payload
        .addChildElement("RequestType")
        .addTextNode("http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");

    // add the BinarySecurityToken type
    SOAPElement BinarySecurityToken =
        factory1.createElement(
            "BinarySecurityToken",
            "",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

    // set BinarySecurityToken type as PKCS10 also known as a CSR..
    BinarySecurityToken.setAttribute(
        "ValueType", "http://schemas.microsoft.com/windows/pki/2009/01/enrollment#PKCS10");

    // Set the EncodingType to base64binary"
    BinarySecurityToken.setAttribute(
        "EncodingType",
        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#base64binary");

    // add the WSS wssSecurity namespace:
    BinarySecurityToken.addNamespaceDeclaration(
        "a", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

    // The CSR is Base64 encoded PKCS10 CSR without the "Begin" and "End" tags
    // add the CSR as a "TextNode"
    BinarySecurityToken.addTextNode(CSR);

    // add the BinarySecurityToken element to Soap pay load
    payload.addChildElement(BinarySecurityToken);

    // Create "AdditionalContext" Element for additional context items such as policy file name,
    // etc..
    QName AdditionalContextName =
        new QName("http://schemas.xmlsoap.org/ws/2006/12/authorization", "AdditionalContext", "");
    SOAPBodyElement AdditionalContext = body.addBodyElement(AdditionalContextName);

    // Create a ContextItem to specify the CertificateTemplate name, get
    // element from "TemplateName" argument
    SOAPElement ContextItem1 = AdditionalContext.addChildElement("ContextItem");
    ContextItem1.setAttribute("Name", "CertificateTemplate");
    SOAPElement Value = AdditionalContext.addChildElement("Value");
    Value.setTextContent(TemplateName);
    ContextItem1.addChildElement(Value);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    SOAPElement ContextItem2 = AdditionalContext.addChildElement("ContextItem");
    ContextItem2.setAttribute("Name", "OU1");
    SOAPElement Value2 = AdditionalContext.addChildElement("Value");
    Value2.setTextContent(OU1);
    ContextItem2.addChildElement(Value2);

    // Create a ContextItem to specify the rmd (remote server) name
    SOAPElement ContextItem3 = AdditionalContext.addChildElement("ContextItem");
    ContextItem3.setAttribute("Name", "rmd");
    SOAPElement Value3 = AdditionalContext.addChildElement("Value");
    // Get application server FQDNS hostname
    InetAddress addr = InetAddress.getLocalHost();
    Value3.setTextContent(addr.getCanonicalHostName());
    ContextItem3.addChildElement(Value3);

    // Request specific validity period:

    // to enable client side to set validity period you must enable:
    // certutil -setreg Policy\EditFlags + EDITF_ATTRIBUTEENDDATE
    // on the CA!
    // Create a ContextItem to specify the rmd (remote server) name
    SOAPElement ContextItem4 = AdditionalContext.addChildElement("ContextItem");
    ContextItem4.setAttribute("Name", "ValidityPeriod");
    SOAPElement Value4 = AdditionalContext.addChildElement("Value");
    // Units can be "Seconds", "Minutes", "Hours", "Days", "Weeks", "Months", "Years"
    Value4.setTextContent(ValidUnit);
    ContextItem4.addChildElement(Value4);

    // Create a ContextItem to specify the rmd (remote server) name
    SOAPElement ContextItem5 = AdditionalContext.addChildElement("ContextItem");
    ContextItem5.setAttribute("Name", "ValidityPeriodUnits");
    SOAPElement Value5 = AdditionalContext.addChildElement("Value");
    Value5.setTextContent(ValidValue);
    ContextItem5.addChildElement(Value5);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    SOAPElement ContextItem7 = AdditionalContext.addChildElement("ContextItem");
    ContextItem7.setAttribute("Name", "OU2");
    SOAPElement Value7 = AdditionalContext.addChildElement("Value");
    Value7.setTextContent(OU2);
    ContextItem7.addChildElement(Value7);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    SOAPElement ContextItem8 = AdditionalContext.addChildElement("ContextItem");
    ContextItem8.setAttribute("Name", "OU3");
    SOAPElement Value8 = AdditionalContext.addChildElement("Value");
    Value8.setTextContent(OU3);
    ContextItem8.addChildElement(Value8);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    SOAPElement ContextItem9 = AdditionalContext.addChildElement("ContextItem");
    ContextItem9.setAttribute("Name", "OU4");
    SOAPElement Value9 = AdditionalContext.addChildElement("Value");
    Value9.setTextContent(OU4);
    ContextItem9.addChildElement(Value9);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    SOAPElement ContextItem10 = AdditionalContext.addChildElement("ContextItem");
    ContextItem10.setAttribute("Name", "OU5");
    SOAPElement Value10 = AdditionalContext.addChildElement("Value");
    Value10.setTextContent(OU5);
    ContextItem10.addChildElement(Value10);

    // KeyUsage=0xa0
    // SOAPElement ContextItem15 = AdditionalContext.addChildElement("ContextItem");
    // ContextItem15.setAttribute("Name", "CertificateUsage");
    // SOAPElement Value15 = AdditionalContext.addChildElement("Value");
    // Value15.setTextContent("1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2");
    // ContextItem15.addChildElement(Value15);

    // TODO:
    // let's try this an see what it does:
    // CertificateUsage

    // alternatively we can specify a specific end date:
    // ExpirationDate = L"Tue, 21 Nov 2000 01:06:53 GMT"

    // add the ContextItem child to the soap payload:
    AdditionalContext.addChildElement(ContextItem1);

    // TODO: document this, rename the context items
    AdditionalContext.addChildElement(ContextItem2);
    AdditionalContext.addChildElement(ContextItem3);
    // AdditionalContext.addChildElement(ContextItem4);
    // AdditionalContext.addChildElement(ContextItem5);
    AdditionalContext.addChildElement(ContextItem7);
    AdditionalContext.addChildElement(ContextItem8);
    AdditionalContext.addChildElement(ContextItem9);
    AdditionalContext.addChildElement(ContextItem10);
    // AdditionalContext.addChildElement(ContextItem15);

    // Create a ContextItem to specify the value of "Other" context item
    // element from "TemplateName" argument
    if (dnEmail != null) {
      SOAPElement ContextItem11 = AdditionalContext.addChildElement("ContextItem");
      ContextItem11.setAttribute("Name", "dnEmail");
      SOAPElement Value11 = AdditionalContext.addChildElement("Value");
      Value11.setTextContent(dnEmail);
      ContextItem11.addChildElement(Value11);
      AdditionalContext.addChildElement(ContextItem11);
    }

    // if SAN extention is specified add the ContextItem
    if (SAN != null) {
      // Create a ContextItem to specify the rmd (remote server) name
      SOAPElement ContextItem6 = AdditionalContext.addChildElement("ContextItem");
      ContextItem6.setAttribute("Name", "SAN");
      SOAPElement Value6 = AdditionalContext.addChildElement("Value");
      Value6.setTextContent(SAN);
      ContextItem6.addChildElement(Value6);
      AdditionalContext.addChildElement(ContextItem6);
    }

    payload.addChildElement(AdditionalContext);

    // place holder for RequestSecurityToken response soap message
    SOAPMessage reply = null;

    // Invoke the end point operation synchronously
    try {
      // and read response
      reply = dispatch.invoke(request);
    } catch (WebServiceException wse) {
      wse.printStackTrace();
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    reply.writeTo(baos);

    // to view the reply via STD-Out:
    // System.out.println(baos);
    StringBuffer ressponseSB = new StringBuffer();
    String resonseS = baos.toString();

    ressponseSB.append(resonseS);
    StringBuilder sBuilder = RequestTokenResponseParser.ParseResponse(ressponseSB, CertFormat);

    String Certs = sBuilder.toString();

    return Certs;
  }
示例#26
0
  public static IRubyObject getnameinfo(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    int flags = args.length == 2 ? RubyNumeric.num2int(args[1]) : 0;
    IRubyObject arg0 = args[0];
    String host, port;

    if (arg0 instanceof RubyArray) {
      List list = ((RubyArray) arg0).getList();
      int len = list.size();

      if (len < 3 || len > 4) {
        throw runtime.newArgumentError("array size should be 3 or 4, " + len + " given");
      }

      // if array has 4 elements, third element is ignored
      host = list.size() == 3 ? list.get(2).toString() : list.get(3).toString();
      port = list.get(1).toString();

    } else if (arg0 instanceof RubyString) {
      String arg = ((RubyString) arg0).toString();
      Matcher m = STRING_IPV4_ADDRESS_PATTERN.matcher(arg);

      if (!m.matches()) {
        IRubyObject obj = unpack_sockaddr_in(context, arg0);

        if (obj instanceof RubyArray) {
          List list = ((RubyArray) obj).getList();
          int len = list.size();

          if (len != 2) {
            throw runtime.newArgumentError("invalid address representation");
          }

          host = list.get(1).toString();
          port = list.get(0).toString();

        } else {
          throw runtime.newArgumentError("invalid address string");
        }

      } else if ((host = m.group(IPV4_HOST_GROUP)) == null
          || host.length() == 0
          || (port = m.group(IPV4_PORT_GROUP)) == null
          || port.length() == 0) {

        throw runtime.newArgumentError("invalid address string");

      } else {

        // Try IPv6
        try {
          InetAddress ipv6_addr = InetAddress.getByName(host);

          if (ipv6_addr instanceof Inet6Address) {
            host = ipv6_addr.getHostAddress();
          }

        } catch (UnknownHostException uhe) {
          throw runtime.newArgumentError("invalid address string");
        }
      }

    } else {
      throw runtime.newArgumentError("invalid args");
    }

    InetAddress addr;

    try {
      addr = InetAddress.getByName(host);

    } catch (UnknownHostException e) {
      throw sockerr(runtime, "unknown host: " + host);
    }

    if ((flags & NI_NUMERICHOST.intValue()) == 0) {
      host = addr.getCanonicalHostName();

    } else {
      host = addr.getHostAddress();
    }

    jnr.netdb.Service serv = jnr.netdb.Service.getServiceByPort(Integer.parseInt(port), null);

    if (serv != null) {

      if ((flags & NI_NUMERICSERV.intValue()) == 0) {
        port = serv.getName();

      } else {
        port = Integer.toString(serv.getPort());
      }
    }

    return runtime.newArray(runtime.newString(host), runtime.newString(port));
  }
 private static boolean isQualified(InetAddress inetAddress) {
   return !inetAddress.isLoopbackAddress()
       && !inetAddress.getHostAddress().equals(inetAddress.getCanonicalHostName());
 }
示例#28
0
 private String getHost(final InetAddress in) {
   if (Boolean.FALSE.equals(disableReverseDnsLookup)) {
     return in.getCanonicalHostName();
   }
   return in.getHostAddress();
 }
示例#29
0
 /**
  * Constructor.
  *
  * @param adr IP address
  * @param prt Port of server
  * @param user Login
  * @param priv Private SSH key
  * @throws IOException If fails
  * @checkstyle ParameterNumberCheck (6 lines)
  * @since 1.4
  */
 public SSH(final InetAddress adr, final int prt, final String user, final URL priv)
     throws IOException {
   this(adr.getCanonicalHostName(), prt, user, IOUtils.toString(priv));
 }
示例#30
0
 /**
  * Constructor.
  *
  * @param adr IP address
  * @param user Login
  * @param priv Private SSH key
  * @throws UnknownHostException If fails
  * @since 1.4
  */
 public SSH(final InetAddress adr, final String user, final String priv)
     throws UnknownHostException {
   this(adr.getCanonicalHostName(), SSH.PORT, user, priv);
 }