⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlsmanager.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	// Either originator of connection, or handshake is done.
	if (cinfo.handshakeDone) {

	  if (LOG.isEnabledFor(Priority.INFO)) 
	    LOG.info("TlsManager: continuing connection");

	  return cinfo.connection;
	}
      }

      if (LOG.isEnabledFor(Priority.INFO)) 
	    LOG.info("TlsManager: initiating handshake");    

      // OK. We are originating the connection:
      // Open the connection (returns when handshake is completed)
      // or throws an IOException if a TLS internal error occurs.

      conn.openTLSConnection();

      // Handshake is done
      cinfo.handshakeDone = true; // Mark as finished
      cons.put(paddr, cinfo);     // replace hashtable entry

      return conn;
    }

    // processIncomingTlsMessage is invoked by TlsTransport when a message has been received
    // TLS Manager. This includes messages sent by other TLS Managers in order to establish a connection
    // and the messages received from other TLS Managers containing TLS elements of message. It
    // is the responsability of processIncomingMessage to demultiplex those messages upon their type.
    // Note that a message element can be used in order to add a header. Message identifiers must also
    // be handled here: this the unique entry point for the TLS Manager.
    //
    // Once a TLS message has been completely received, the TLS Manager must invoke the TslTransport
    // processReceivedMessage method (using the transport variable).
    //
    // @param msg is the incoming message
    //

    // @param srcAddr is the address of the source of the message
    // @param dstAddr is the address of the destination of the message
    //
    // These messages are passed to JTlsInputStream to be decrypted
    // by TLS.
    private static int count = 1;

    public void processIncomingMessage (Message msg,
					EndpointAddress srcAddr, 
					EndpointAddress dstAddr)
	throws IOException 
    {
        TlsConn conn = null;

	// Extract unique part of source address
        String paddr = null;
	paddr = srcAddr.getProtocolAddress();

	count += 1;		// total input message counter

	// Will be in our hash table unless this is for
	// a first time incoming connection request
	TlsConnInfo cinfo = (TlsConnInfo)cons.get(paddr);

	// flags attempt to restart an old connection
	// where the other end restarting the app.
	boolean restart = false;


	if (cinfo != null) {

	  // In our connection table.
	  conn = cinfo.connection;

	  // See if input is dead
	  if (conn.getInputDead() == true) {
	
	    if (LOG.isEnabledFor(Priority.INFO)) {
	      LOG.info("Input died for " + paddr);
	    }

	    throw new IOException("TLS Input died");

          }

	  // see if handshake is done
	  boolean hsdone = conn.getHandshakeDone();
	  if (hsdone) {

	    int state = firstHandshakeMessage(msg);
	  
	    // if this is sequence number 1, ie, start of handshake,
	    // and we've already read this element(handshake done), restart.
	    //    It could be that the start handshake message is still 
	    //    in the input queue and not yet read(handshake not done).
	    //
	    //    In this case it will be discarded later as a duplicate.
	    if (state == STARTOFHS) {

	      if (LOG.isEnabledFor(Priority.INFO)) {

		int j = conn.jin.getSequenceNumber();
		LOG.info("Restart handshake, Seq#s: received 1" +
			  ", expected " + j + "\n");
	      }

	      cons.remove(paddr);
	      cinfo = null;	// restart handshake
	      restart = true;
	    }
	  }
	}

	// New incoming connection, or restarting handshake
	if (cinfo == null) {

	    if (!restart) {

	      // Make sure it is an initial message, and not something hangin
	      // around in a rendez-vous/router somewhere.
	      int first = firstHandshakeMessage(msg);
	      if (first != STARTOFHS && first != RETRSTARTOFHS) {

		if (LOG.isEnabledFor(Priority.INFO))
		  LOG.info ("NO CONNECTION, STATE = " + first +
			     ", Discard msg.");

		// Garbage from an old connection. 

		msg = null;
		return;
	      }
	    }
	      
	    if (LOG.isEnabledFor(Priority.INFO))
	      LOG.info ("New first contact server or handshake restart: ");
	  
	    try {

	      // This will initiate server side handshake response
	      // since the first TLS Record is always the client Hello.
	      conn = new TlsConn(this,
				 this.transport,
				 srcAddr,
				 false,	// Server
				 msg);

	      if (LOG.isEnabledFor(Priority.INFO)) {
		LOG.info("New Server tls connection = " + srcAddr.toString());
	      }

	      // Update the idle timer
	      cinfo = new TlsConnInfo(conn, System.currentTimeMillis());
	      cons.put(paddr, cinfo);     // replace hashtable entry
	
	    } catch (Exception e) {

	      if (LOG.isEnabledFor(Priority.INFO))
		LOG.info("TlsManager, cannot establish Server Connection:", e);

	      return;
	    }
	    

	    // Start the TLS Server and complete the handshake
	    try {

	      conn.openTLSConnection();	  // open the TLS connection

	      // update con info
	      cinfo.handshakeDone = true;
	      cinfo.lastAccessed = System.currentTimeMillis();
	      cons.put(paddr, cinfo);     // replace hashtable entry

	    } catch (Exception e) {

	      // Handshake failure or IOException
	      if (LOG.isEnabledFor(Priority.INFO)) 
		LOG.info("TlsManger, Handshake failure for TLS connection:", e);

	      cons.remove(paddr); // remove entry

	      return;
	    }

	    if (LOG.isEnabledFor(Priority.INFO)) {
	      LOG.info("TlsManger, NEW TLS Server, N = " + cons.size() +
			"]\n  New Server@" + paddr + "\n");
	    }

	    // msg was processed by TlsConn constructor.
	    return;
	}

	// Queue message up for TlsInputStream on that connection
	conn.jin.queueIncomingMessage(msg);

    }

    /**
     * firstHandshakeMessage
     *
     * @Param conn The connection for which msg is received
     * @Param msg  Input message
     *
     * @return STARTOFHS        if sequence#1 and not a retransmission
     *         RETRSTARTOFHS    if sequence#1 and retransmission
     *         NOTSTARTOFHS     not sequence#1
     */
    private int firstHandshakeMessage(Message msg) {

      MessageElement elt = null, elt1 = null;
      int seqN = 0;
      boolean startOfHandshake = false;
      boolean retrans = false;

      while ((elt = JTlsUtil.getNextElement(msg, elt1)) != null) {

	// see if a retransmission
	String name = elt.getName();

	if (name.compareTo(JTlsDefs.MARKRETR) == 0) {
	  // Retransmission
	  retrans = true;
	}

	try {

	  seqN = JTlsUtil.getSequenceNumber(elt);

	} catch (Exception e) {
	  // This element was not a TLS element. Get the next one
	  elt1 = elt;
	  continue;
	}
	// look for start of handshake
	if (seqN == 1) startOfHandshake = true;
	
	elt1 = elt;		// get elt's successor

      }
      if (startOfHandshake) {
	if (retrans) return RETRSTARTOFHS;
	else
	  return STARTOFHS;
      } else
	return NOTSTARTOFHS;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -