Example #1
0
  /**
   * Utility method for converting the non-firewalled elements of an AlternateLocationCollection to
   * a smaller set of endpoints.
   */
  private static Set getAsEndpoints(AlternateLocationCollection col) {
    if (col == null || !col.hasAlternateLocations()) return Collections.EMPTY_SET;

    long now = System.currentTimeMillis();
    synchronized (col) {
      Set endpoints = null;
      int i = 0;
      for (Iterator iter = col.iterator(); iter.hasNext() && i < MAX_LOCATIONS; ) {
        Object o = iter.next();
        if (!(o instanceof DirectAltLoc)) continue;
        DirectAltLoc al = (DirectAltLoc) o;
        if (al.canBeSent(AlternateLocation.MESH_RESPONSE)) {
          IpPort host = al.getHost();
          if (!NetworkUtils.isMe(host)) {
            if (endpoints == null) endpoints = new HashSet();

            if (!(host instanceof Endpoint)) host = new Endpoint(host.getAddress(), host.getPort());

            endpoints.add(host);
            i++;
            al.send(now, AlternateLocation.MESH_RESPONSE);
          }
        } else if (!al.canBeSentAny()) iter.remove();
      }
      return endpoints == null ? Collections.EMPTY_SET : endpoints;
    }
  }
Example #2
0
    private static Set parseLocations(byte[] locBytes) {
      Set locations = null;
      IPFilter ipFilter = IPFilter.instance();

      if (locBytes.length % 6 == 0) {
        for (int j = 0; j < locBytes.length; j += 6) {
          int port = ByteOrder.ushort2int(ByteOrder.leb2short(locBytes, j + 4));
          if (!NetworkUtils.isValidPort(port)) continue;
          byte[] ip = new byte[4];
          ip[0] = locBytes[j];
          ip[1] = locBytes[j + 1];
          ip[2] = locBytes[j + 2];
          ip[3] = locBytes[j + 3];
          if (!NetworkUtils.isValidAddress(ip)
              || !ipFilter.allow(ip)
              || NetworkUtils.isMe(ip, port)) continue;
          if (locations == null) locations = new HashSet();
          locations.add(new Endpoint(ip, port));
        }
      }
      return locations;
    }
Example #3
0
 /** @return whether this rfd points to myself. */
 public boolean isMe() {
   return needsPush()
       ? Arrays.equals(_clientGUID, RouterService.getMyGUID())
       : NetworkUtils.isMe(getHost(), getPort());
 }