Esempio n. 1
0
    /** Adds a GGEP block with the specified alternate locations to the output stream. */
    static void addGGEP(OutputStream out, GGEPContainer ggep) throws IOException {
      if (ggep == null || (ggep.locations.size() == 0 && ggep.createTime <= 0))
        throw new NullPointerException("null or empty locations");

      GGEP info = new GGEP();
      if (ggep.locations.size() > 0) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
          for (Iterator i = ggep.locations.iterator(); i.hasNext(); ) {
            try {
              Endpoint ep = (Endpoint) i.next();
              baos.write(ep.getHostBytes());
              ByteOrder.short2leb((short) ep.getPort(), baos);
            } catch (UnknownHostException uhe) {
              continue;
            }
          }
        } catch (IOException impossible) {
          ErrorService.error(impossible);
        }
        info.put(GGEP.GGEP_HEADER_ALTS, baos.toByteArray());
      }

      if (ggep.createTime > 0) info.put(GGEP.GGEP_HEADER_CREATE_TIME, ggep.createTime / 1000);

      info.write(out);
    }
Esempio n. 2
0
  /**
   * @requires this and other have dotted-quad addresses, or names that can be resolved.
   * @effects Returns true if this is on the same subnet as 'other', i.e., if this and other are in
   *     the same IP class and have the same network number.
   */
  public boolean isSameSubnet(Endpoint other) {
    byte[] a;
    byte[] b;
    int first;
    try {
      a = getHostBytes();
      first = ByteOrder.ubyte2int(a[0]);
      b = other.getHostBytes();
    } catch (UnknownHostException e) {
      return false;
    }

    // See http://www.3com.com/nsc/501302.html
    // class A
    if (first <= 127) return a[0] == b[0];
    // class B
    else if (first <= 191) return a[0] == b[0] && a[1] == b[1];
    // class C
    else return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
  }