Exemple #1
0
  /**
   * Called with a parameter "da" already verified and validated as new and possibly signed
   *
   * @param da
   * @param detected_sa
   * @param db
   * @param storeNAT
   * @return
   * @throws P2PDDSQLException
   */
  private static DirectoryAnnouncement_Answer _monitored_handleAnnouncement(
      DirectoryAnnouncement da,
      Address detected_sa,
      DBInterface db,
      boolean storeNAT,
      boolean TCP_not_UDP)
      throws P2PDDSQLException {

    if (DEBUG)
      System.out.println("DirectoryServer: _monitored_handleAnnouncement: Got announcement: " + da);
    if (da.address._addresses == null) {
      if (DEBUG)
        System.out.println(
            "DirectoryServer: _monitored_handleAnnouncement: Got empty announcement: "
                + da
                + " detected="
                + detected_sa);
    }

    String globalID = da.getGID();
    String globalIDhash = da.getGIDH();
    String instance = da.instance;

    // only use old NAT is current message is TCP
    if (TCP_not_UDP) {
      D_DirectoryEntry old_entry = DirectoryServerCache.getEntry(globalID, globalIDhash, instance);
      if (old_entry != null) {
        Address old_detected_NAT = old_entry.getNATAddress();
        if (detected_sa == null) detected_sa = old_detected_NAT;
        else {
          if (old_detected_NAT != null) {
            if (Address.sameDomain(detected_sa, old_detected_NAT)) {
              detected_sa = old_detected_NAT;
            } else {
              if (DEBUG)
                System.out.println(
                    "DirectoryServer: _monitored_handleAnnouncement: diff NATS: n="
                        + detected_sa
                        + " o="
                        + old_detected_NAT);
            }
          }
        }
      }
    }

    if (detected_sa != null)
      da.address._addresses = prependAddress(da.address._addresses, detected_sa);
    DirectoryServerCache.loadAndSetEntry(da, TCP_not_UDP);
    if (DEBUG)
      System.out.println(
          "DirectoryServer: _monitored_handleAnnouncement: loaded="
              + DirectoryServerCache.getEntry(globalID, globalIDhash, instance));

    // byte[] answer =
    return new DirectoryAnnouncement_Answer(Util.getString(detected_sa));
    // if (DEBUG) out.println("DS:_monitored_handleAnnouncement: sending answer:
    // "+Util.byteToHexDump(answer));
    // return answer;
  }
Exemple #2
0
 public void _run() {
   // this.setName("Directory Server");
   // ThreadsAccounting.registerThread();
   try {
     DirectoryServerCache.startSaverThread();
     __run();
   } catch (Exception e) {
     e.printStackTrace();
   }
   DirectoryServerCache.stopSaverThread();
   // ThreadsAccounting.unregisterThread();
 }
Exemple #3
0
 /**
  * get it from cache
  *
  * @param da
  * @return
  */
 private static String getGlobalPeerID(DirectoryAnnouncement da) {
   if (da.getGID() != null) return da.getGID();
   D_DirectoryEntry e = DirectoryServerCache.getEntry(da.getGID(), da.getGIDH());
   return e.globalID;
 }
Exemple #4
0
  public void __run(Socket client) {

    try {
      client.setSoTimeout(DirectoryServer.SOCKET_READ_TIMEOUT);
      // out.println("DirServ: Accepted... from: "+client.getRemoteSocketAddress());
      InetSocketAddress risa = null;
      try {
        // InetAddress risa = ((InetSocketAddress) client.getRemoteSocketAddress()).getAddress();
        risa = (InetSocketAddress) client.getRemoteSocketAddress();
      } catch (Exception e) {
        e.printStackTrace();
      }
      byte buffer[] = new byte[DirectoryServer.MAX_DR];
      int peek = client.getInputStream().read(buffer);
      if (peek < 0) {
        out.println("DirServTCP: got no bytes from: " + risa);
        return;
      } else {
        out.println("DirServTCP: __run got bytes from: " + risa);
      }
      // out.println("DirServ: Got ASN1 dump: "+Util.byteToHexDump(buffer,peek));
      Decoder test = new Decoder(buffer, 0, peek);
      // out.println("DirServTCP: __run got decoder: "+test);

      out.println(
          "DirServTCP: __run: Decoded ASN1: class="
              + test.typeClass()
              + " val="
              + test.tagVal()
              + " blen="
              + buffer.length);
      DirMessage m; // recording msgs
      if (test.typeClass() == Encoder.CLASS_APPLICATION
          && test.tagVal() == DirectoryAnnouncement.TAG) {
        // out.println("DirServ: Detected directory announcement");
        InetSocketAddress isa = (InetSocketAddress) client.getRemoteSocketAddress();
        DirectoryAnnouncement da = new DirectoryAnnouncement(buffer, peek, client.getInputStream());
        out.println("DirServTCP: got announcement: " + da + "\n from: " + isa);

        // here should first get the old detected address (probably from UDP) and keep it if has
        // same IP

        // this stores the message in the announcement_storage for this GID, and the GID - IP
        // relation in its hashtable
        DirectoryServer.recordAnnouncementMessage(
            isa, da, null, DirMessage.TCP, DirMessage.ANNOUNCEMENT);

        // creates an address object from the socket and reported udp port
        Address detected_sa = DirectoryServer.detectUDP_Address(isa, da.address.udp_port);
        out.println("DirServTCP: got announcement: detected = " + detected_sa);

        // Address detected_sa = new Address(_detected_sa);
        detected_sa = DirectoryServer.addr_NAT_detection(da, detected_sa);

        out.println("DirServTCP: got announcement: detected tuned = " + detected_sa);
        if (da.address.udp_port <= 0) detected_sa = null;

        boolean storeNAT;
        boolean TCP;
        DirectoryAnnouncement_Answer daa =
            DirectoryServer.handleAnnouncement(
                da, detected_sa, DirectoryServer.db_dir, storeNAT = false, TCP = true);
        byte[] answer = new byte[0];
        if (daa != null) answer = daa.encode();
        // byte[] answer = new D_DAAnswer(detected_sa).encode();
        client.getOutputStream().write(answer);

        DirectoryServer.recordAnnouncementMessage(
            isa, da, daa, DirMessage.TCP, DirMessage.ANN_ANSWER);
      } else {
        // boolean DEBUG = true;
        if (DEBUG) out.println("DSTCP: Potential directory request");
        // handling terms here
        DirectoryRequest dr = new DirectoryRequest(buffer, peek, client.getInputStream());
        if (dr.empty()) {
          out.println("DirServTCP:__run: potential message detected empty = " + risa + " dr=" + dr);
          return;
        }
        // boolean acceptedTerms = areTermsAccepted(dr);
        InetSocketAddress isa = (InetSocketAddress) client.getRemoteSocketAddress();
        if (DEBUG) out.println("Received directory request: " + dr);

        DirectoryServer.recordRequestMessage(isa, dr, null, DirMessage.TCP, DirMessage.REQUEST);
        if (DEBUG)
          out.println(
              "DirServ: Looking for: "
                  + D_Peer.getGIDHashFromGID(dr.globalID)
                  + "\n  by "
                  + D_Peer.getGIDHashFromGID(
                      dr.initiator_globalID)); // +"\n  with source udp="+dr.UDP_port);

        String globalID = dr.globalID; // looking for peer GID
        String globalIDhash = dr.globalIDhash; // looking for peer GID hash
        // de has the look-for-peer and all instances stored in the db
        D_DirectoryEntry de = DirectoryServerCache.getEntry(globalID, globalIDhash);
        if (DEBUG) out.println("DirServ: From cache got: " + de);
        ASNObj da = DirectoryServer.getDA(de, dr, dr.version);

        if ((da == null)) // || (da.date == null))
        {
          System.out.println(
              "DirectoryServer:__run: abandon: ?why da=" + da + "\n\tde=" + de + "\n\tdr=" + dr);
          return;
        }
        DirectoryServer.recordRequestMessage(
            isa, dr, da, DirMessage.TCP, DirMessage.REQUEST_ANSWER);

        byte msg[] = da.encode();
        if (DEBUG) {
          Decoder dec = new Decoder(msg);
          DirectoryAnswerMultipleIdentities dami = new DirectoryAnswerMultipleIdentities(dec);
          System.out.println("DirectoryServer:_run:encode " + da + "\nto " + dami);
        }
        // out.println("answer: "+Util.byteToHexDump(msg, " ")+"\n\tI.e.: "+da);
        /*
        if(_DEBUG&&(da.addresses.size()>0)){
        	out.println("DirServ: *******");
        	out.println("DirServ: Aanswer: "+client.getRemoteSocketAddress()+" <- "+da.toString());
        }
        */
        client.getOutputStream().write(msg);
      }
      client.close();
    } catch (SocketException e) {
      out.println("server: " + e);
      return;
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
  }