FileDocCategorySizeDatePackage
AZHandshake.javaAPI DocAzureus 3.0.3.48869Fri Mar 09 12:10:46 GMT 2007com.aelitis.azureus.core.peermanager.messaging.azureus

AZHandshake

public class AZHandshake extends Object implements AZMessage
AZ handshake message.

Fields Summary
public static final int
HANDSHAKE_TYPE_PLAIN
public static final int
HANDSHAKE_TYPE_CRYPTO
private static final byte
bss
private final byte
version
private DirectByteBuffer
buffer
private String
description
private final byte[]
identity
private final String
client
private final String
client_version
private final String[]
avail_ids
private final byte[]
avail_versions
private int
tcp_port
private int
udp_port
private int
udp_non_data_port
private final int
handshake_type
Constructors Summary
public AZHandshake(byte[] peer_identity, String _client, String version, int tcp_listen_port, int udp_listen_port, int udp_non_data_listen_port, String[] avail_msg_ids, byte[] avail_msg_versions, int _handshake_type, byte _version)

  
  
     
                       
                       
                       
                       
                       
                       
                       
                       
                         
    
    this.identity = peer_identity;
    this.client = _client;
    this.client_version = version;
    this.avail_ids = avail_msg_ids;
    this.avail_versions = avail_msg_versions;
    this.tcp_port = tcp_listen_port;
    this.udp_port = udp_listen_port;
    this.udp_non_data_port = udp_non_data_listen_port;
    this.handshake_type = _handshake_type;
    this.version = _version;
    
    //verify given port info is ok
    if( tcp_port < 0 || tcp_port > 65535 ) {
      Debug.out( "given TCP listen port is invalid: " +tcp_port );
      tcp_port = 0;
    }
    
    if( udp_port < 0 || udp_port > 65535 ) {
      Debug.out( "given UDP listen port is invalid: " +udp_port );
      udp_port = 0;
    }
    
    if( udp_non_data_port < 0 || udp_non_data_port > 65535 ) {
        Debug.out( "given UDP non-data listen port is invalid: " +udp_non_data_port );
        udp_non_data_port = 0;
      }
  
Methods Summary
public com.aelitis.azureus.core.peermanager.messaging.Messagedeserialize(DirectByteBuffer data, byte version)

    Map root = MessagingUtil.convertBencodedByteStreamToPayload( data, 100, getID() );

    byte[] id = (byte[])root.get( "identity" );
    if( id == null )  throw new MessageException( "id == null" );
    if( id.length != 20 )  throw new MessageException( "id.length != 20: " +id.length );
      
    byte[] raw_name = (byte[])root.get( "client" );
    if( raw_name == null )  throw new MessageException( "raw_name == null" );
    String name = new String( raw_name );

    byte[] raw_ver = (byte[])root.get( "version" );
    if( raw_ver == null )  throw new MessageException( "raw_ver == null" );
    String client_version = new String( raw_ver );

    Long tcp_lport = (Long)root.get( "tcp_port" );
    if( tcp_lport == null ) {  //old handshake
      tcp_lport = new Long( 0 );
    }

    Long udp_lport = (Long)root.get( "udp_port" );
    if( udp_lport == null ) {  //old handshake
      udp_lport = new Long( 0 );
    }

    Long udp2_lport = (Long)root.get( "udp2_port" );
    if( udp2_lport == null ) {  //old handshake
      udp2_lport = udp_lport;
    }
    
    Long h_type = (Long)root.get( "handshake_type" );
    if( h_type == null ) {  //only 2307+ send type
    	h_type = new Long( HANDSHAKE_TYPE_PLAIN );
    }

    List raw_msgs = (List) root.get("messages");
    if (raw_msgs == null)  throw new MessageException("raw_msgs == null");

    String[] ids = new String[raw_msgs.size()];
    byte[] vers = new byte[raw_msgs.size()];

    int pos = 0;

    for (Iterator i = raw_msgs.iterator(); i.hasNext();) {
      Map msg = (Map) i.next();

      byte[] mid = (byte[]) msg.get("id");
      if (mid == null)  throw new MessageException("mid == null");
      ids[pos] = new String(mid);

      byte[] ver = (byte[]) msg.get("ver");
      if (ver == null)  throw new MessageException("ver == null");
      
      if (ver.length != 1)  throw new MessageException("ver.length != 1");
      vers[pos] = ver[0];

      pos++;
    }

    return new AZHandshake( id, name, client_version, tcp_lport.intValue(), udp_lport.intValue(), udp2_lport.intValue(), ids, vers, h_type.intValue(), version );
  
public voiddestroy()

    if( buffer != null )  buffer.returnToPool();
  
public java.lang.StringgetClient()

  return client;  
public java.lang.StringgetClientVersion()

  return client_version;  
public DirectByteBuffer[]getData()

    if( buffer == null ) {
      Map payload_map = new HashMap();
      
      //client info
      payload_map.put( "identity", identity );
      payload_map.put( "client", client );
      payload_map.put( "version", client_version );
      payload_map.put( "tcp_port", new Long( tcp_port ) );
      payload_map.put( "udp_port", new Long( udp_port ) );
      payload_map.put( "udp2_port", new Long( udp_non_data_port ) );
      payload_map.put( "handshake_type", new Long( handshake_type ) );
          
      //available message list
      List message_list = new ArrayList();
      for( int i=0; i < avail_ids.length; i++ ) {
        String id = avail_ids[ i ];
        byte ver = avail_versions[ i ];
        
        if( id.equals( getID() ))  continue;  //skip ourself

        Map msg = new HashMap();
        msg.put( "id", id );
        msg.put( "ver", new byte[]{ ver } );
          
        message_list.add( msg );
      }
      payload_map.put( "messages", message_list );

      	// random padding if crypto
      
      if ( handshake_type == AZHandshake.HANDSHAKE_TYPE_CRYPTO ){
    	  
    	  payload_map.put( "pad", new byte[(int)( Math.random() * AZMessageFactory.AZ_HANDSHAKE_PAD_MAX )]);
      }
      
      buffer = MessagingUtil.convertPayloadToBencodedByteStream( payload_map, DirectByteBuffer.AL_MSG_AZ_HAND );

      if( buffer.remaining( bss ) > 1200 )  System.out.println( "Generated AZHandshake size = " +buffer.remaining( bss )+ " bytes" );
    }
    
    return new DirectByteBuffer[]{ buffer };
  
public java.lang.StringgetDescription()

    if( description == null ) {
      String msgs_desc = "";
      for( int i=0; i < avail_ids.length; i++ ) {
        String id = avail_ids[ i ];
        byte ver = avail_versions[ i ];
        if( id.equals( getID() ) )  continue;  //skip ourself
        msgs_desc += "[" +id+ ":" +ver+ "]";
      }
      description = getID()+ " from [" +ByteFormatter.nicePrint( identity, true )+ ", " +
      							client+ " " +client_version+ ", TCP/UDP ports " +tcp_port+ "/" +udp_port+ "/" + udp_non_data_port +
      							", handshake " + (getHandshakeType() == HANDSHAKE_TYPE_PLAIN ? "plain" : "crypto") +
      							"] supports " +msgs_desc;
    }
    
    return description;
  
public java.lang.StringgetFeatureID()

  return AZMessage.AZ_FEATURE_ID;  
public intgetFeatureSubID()

 return AZMessage.SUBID_AZ_HANDSHAKE;  
public intgetHandshakeType()

  return handshake_type;  
public java.lang.StringgetID()

  return AZMessage.ID_AZ_HANDSHAKE;  
public byte[]getIDBytes()

  return AZMessage.ID_AZ_HANDSHAKE_BYTES;  
public byte[]getIdentity()

  return identity;  
public java.lang.String[]getMessageIDs()

  return avail_ids;  
public byte[]getMessageVersions()

  return avail_versions;  
public intgetTCPListenPort()

  return tcp_port;  
public intgetType()

  return Message.TYPE_PROTOCOL_PAYLOAD;  
public intgetUDPListenPort()

  return udp_port;  
public intgetUDPNonDataListenPort()

  return udp_non_data_port;  
public bytegetVersion()

 return version;