📄 tlstransport.java
字号:
/************************************************************************
*
* $Id: TlsTransport.java,v 1.2 2002/03/04 21:42:58 echtcherbina Exp $
*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*********************************************************************************/
package net.jxta.impl.endpoint.tls;
import net.jxta.document.Advertisement;
import net.jxta.endpoint.*;
import net.jxta.peergroup.*;
import net.jxta.id.*;
import net.jxta.protocol.*;
import net.jxta.impl.protocol.*;
import net.jxta.impl.endpoint.*;
import net.jxta.exception.*;
import net.jxta.peer.PeerID;
import net.jxta.impl.util.*;
// Get the ID.getIDEncodingType static from the impl for now.
// We need it because, since ther's no way to remove it from
// the string version of a peer ID, we must include it when we
// make a peerID out of strings.
import java.io.*;
import java.util.*;
import java.net.URL;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
import net.jxta.platform.Module;
import net.jxta.exception.*;
public class TlsTransport implements EndpointProtocol,
EndpointListener,
Module {
private static final Category LOG = Category.getInstance(TlsTransport.class.getName());
// As a stringified endpointAddress.
protected String localPeerAddr = null;
protected String localTlsPeerAddr = null;
// As a stringified peerID
protected String localPeerId = null;
protected String localPeerUID = null;
private EndpointService endpoint = null;
private PeerGroup group = null;
private static final String tlsSName = "TlsTransport";
private static String tlsSParam = null;
private String localPeerAdv = null;
// Until we decide otherwise, the tls is *by definition* handling
// peerID addressed messages.
private static String tlsPName = "jxtatls";
private TlsManager manager = null;
public TlsTransport() {
}
public boolean allowOverLoad() {
return true;
}
// Temporary
public int startApp(String[] args) { return 1; }
public void stopApp() {}
public void init (PeerGroup group, ID id, Advertisement impl)
throws PeerGroupException {
this.group = group;
endpoint = group.getEndpointService();
localPeerId = group.getPeerID().toString();
localPeerUID = group.getPeerID().getUniqueValue().toString();
localPeerAddr =
"jxta"
+ "://"
+ group.getPeerID().getUniqueValue().toString();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("localPeerId = " + localPeerId);
tlsSParam = group.getPeerGroupID().getUniqueValue().toString();
localTlsPeerAddr =
tlsPName
+ "://"
+ group.getPeerID().getUniqueValue().toString()
+ "/"
+ tlsSName
+ "/"
+ tlsSParam;
// Connect ourself to the EndpointService
try {
endpoint.addListener(tlsSName + tlsSParam, this);
} catch (Exception e2) {
if (LOG.isEnabledFor(Priority.ERROR))
LOG.error("TLS could not register...as good as dead");
if (LOG.isEnabledFor(Priority.ERROR)) LOG.error(e2);
}
// Create the TLS Manager
manager = new TlsManager (this);
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("TLS Transport is now initialized");
endpoint.addEndpointProtocol (this);
}
public boolean isConnectionOriented() {
return false;
}
/**
* Returns true if the endpoint protocol can be used by the EndpointRouter
*
* @return boolean true if the protocol can be used by the EndpointRouter
*/
public boolean allowRouting() {
// The TLS connection should not be used for default routing
return false;
}
/**
* Return the endpoint address by which this peer is accessible via this
* transport.
*/
public EndpointAddress getPublicAddress() {
return endpoint.newEndpointAddress(localTlsPeerAddr);
}
/**
* Returns the address family that this endpoint protocol handles.
* This endpoint protocol handles the address family that denotes
* peerId's.
*
* @return String the endpoint protocol name
*/
public String getProtocolName() {
return tlsPName;
}
/**
* Return the protocol name we support
**/
public static String getSupportedProtocolName() {
return "jxtatls";
}
/**
* Return the local peer group
**/
public PeerGroup getPeerGroup() {
return group;
}
public void propagate (Message srcMsg,
String pName,
String pParam,
String prunePeer) throws IOException {
// TLS provides a point to point secure channel. It is not intended
// to provide a one to many secure channel, therefore propagation is not
// possible on TlsTransport.
// All messages are lost in the ether
}
/**
* closes this EndpointProtocol.
*/
public void close() {
endpoint.removeListener(tlsSName + tlsSParam, this);
}
/**
* Returns true if the target address is reachable. Otherwise
* returns false.
*/
public boolean ping (EndpointAddress addr) {
// PLUGIN to be implemented
return true;
}
/**
* @param msg The message which has just been recieved.
* @param srcAddr The address from which this message was received. This
* is addr is only one hop away.
* @param dstAddr The destination for this message. Should be one of this
* peer's addresses.
*
* @return The message, which should be delivered to a service on this peer.
* null if the message has been routed onward, or if the
* message should be dropped.
*/
public void processIncomingMessage( Message msg,
EndpointAddress srcAddr,
EndpointAddress dstAddr )
{
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("processIncomingMessage starts");
try {
manager.processIncomingMessage (msg, srcAddr, dstAddr);
} catch (IOException e) {
// IF this happens, something is badly broken.
// We need a way to bail out the app.
if (LOG.isEnabledFor(Priority.ERROR))
LOG.debug ("Error processing incoming message", e);
}
}
// That's what upper layers call (through the endpoint API) to get
// a messenger to a peer-id based destination.
public EndpointMessenger getMessenger(EndpointAddress addr)
throws IOException {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug ("TlsTransport: getMessanger for " + addr.toString());
// Create a Peer EndpointAddress
String dstPeerID = addr.getProtocolAddress();
String dstPAddr = "jxta://" + dstPeerID;
// If the dest is the local peer, just loop it back without going
// through the TLS. Local communication do not use TLS.
if (dstPeerID.equals (localPeerUID)) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("getMessenger: return LoopbackMessenger");
return new LoopbackMessenger (endpoint,
endpoint.newEndpointAddress(localPeerAddr),
addr);
}
TlsConn conn = null;
try {
conn = manager.getTlsConn (endpoint.newEndpointAddress(dstPAddr));
} catch (HandshakeInProgressException e) {
throw new IOException("Cannot get TlsConn: " + e.getMessage());
}
if (conn == null) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug ("Cannot get a TLS connection");
// No connection was either available or created. Cannot do TLS
// with the destination address.
throw new IOException();
}
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("TlsTransport: TlsMessanger with TlsConn DONE");
// Build a TlsMessenger around it that will add our header.
return new TlsMessenger(addr, conn, this);
}
// sendToRemoteTls is used by the TlsManager and the TlsConn in order to send a message,
// either a TLS connection establishement, or TLS fragments to the remote TLS.
public void sendToRemoteTls (EndpointAddress destAddr, Message msg)
throws IOException {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("TlsTransport: sendToRemoteTLS, dest peer= " + destAddr.toString());
// We need to build an appropriate EndpointAddress. We know that the appropriate
// way to build an EndpointAddress that the EndpointService will understand is to
// form it as jxta://uuid-<id> where id is the unique identifier of the peer.
// Since jxtatls transport defines its EndpointService Address the same way, except that
// "jxta" is replaced by "jxtatls", all we have to do is to change the protocol name.
EndpointAddress realAddr = endpoint.newEndpointAddress (destAddr.toString());
realAddr.setServiceName (tlsSName);
realAddr.setServiceParameter (tlsSParam);
// Get a messenger. It would be a very good optimization to keep the messenger
// around for a while. To be done later.
EndpointMessenger messenger =
endpoint.getMessenger(realAddr);
if (messenger == null) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("getMessenger: no raw messenger");
throw new IOException("No raw messenger in getMessenger()");
}
// Good we have a messenger. Send the message.
messenger.sendMessage (msg);
}
// processReceivedMessage is invoked by the TLS Manager when a message has been
// completely received and is ready to be delivered to the service/application
public void processReceivedMessage (Message msg) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("processReceivedMessage starts");
InputStream ip = null;
String srcPeer = null;
String destPeer = null;
EndpointAddress origSrcAddr = null; // The origin endpointAddr (jxta:)
EndpointAddress origDstAddr = null; // The dest endpointAddr (jxta:)
TlsMessage tlsMsg = null;
MessageElement tlsElement = msg.getElement(TlsMessage.Name);
try {
if (tlsElement == null) {
// The sender did not use this tls
// Discard the message
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processReceivedMessage: no TLS header - discard message");
return;
}
ip = tlsElement.getStream();
if (ip == null) {
// The sender did not use this tls
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("processReceivedMessage: invalid TLS header - discard message");
return;
}
tlsMsg = new TlsMessage(ip);
origSrcAddr =
endpoint.newEndpointAddress(tlsMsg.getSrcAddress());
origDstAddr =
endpoint.newEndpointAddress(tlsMsg.getDestAddress());
srcPeer = origSrcAddr.getProtocolName() + "://"
+ origSrcAddr.getProtocolAddress();
destPeer = origDstAddr.getProtocolName() + "://"
+ origDstAddr.getProtocolAddress();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" srcPeer= " + srcPeer);
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" destPeer= " + destPeer);
} catch (Exception badHdr) {
// Drop it, we do not even know the destination
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Bad TLS header or bad message. Message dropped.");
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("exception: " + badHdr);
return;
}
// Give the message back to the other Services.
try {
// Set source and destination address to their original
// values.
msg.setSourceAddress(origSrcAddr);
msg.setDestinationAddress(origDstAddr);
//Call EndpointService demux
endpoint.demux(msg);
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug ("Error while demuxing an incoming message", e);
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -